blob: 629e88b38bd1c1c205ef9c13cff92fd451e2a2b2 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
30#include "xfs_error.h"
31#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_alloc_btree.h"
33#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110038#include "xfs_inode_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110039#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_ialloc.h"
41#include "xfs_log_priv.h"
42#include "xfs_buf_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_log_recover.h"
44#include "xfs_extfree_item.h"
45#include "xfs_trans_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "xfs_quota.h"
47#include "xfs_rw.h"
Christoph Hellwig43355092008-03-27 18:01:08 +110048#include "xfs_utils.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000049#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51STATIC int xlog_find_zeroed(xlog_t *, xfs_daddr_t *);
52STATIC int xlog_clear_stale_blocks(xlog_t *, xfs_lsn_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#if defined(DEBUG)
54STATIC void xlog_recover_check_summary(xlog_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#else
56#define xlog_recover_check_summary(log)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#endif
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/*
60 * Sector aligned buffer routines for buffer create/read/write/access
61 */
62
Alex Elder6881a222010-04-13 15:22:29 +100063/* Number of basic blocks in a log sector */
64#define xlog_sectbb(log) (1 << (log)->l_sectbb_log)
65
Alex Elderff30a622010-04-13 15:22:58 +100066/*
67 * Verify the given count of basic blocks is valid number of blocks
68 * to specify for an operation involving the given XFS log buffer.
69 * Returns nonzero if the count is valid, 0 otherwise.
70 */
71
72static inline int
73xlog_buf_bbcount_valid(
74 xlog_t *log,
75 int bbcount)
76{
77 return bbcount > 0 && bbcount <= log->l_logBBsize;
78}
79
Alex Elder36adecf2010-04-13 15:21:13 +100080/*
81 * Allocate a buffer to hold log data. The buffer needs to be able
82 * to map to a range of nbblks basic blocks at any valid (basic
83 * block) offset within the log.
84 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +000085STATIC xfs_buf_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -070086xlog_get_bp(
87 xlog_t *log,
Dave Chinner32281492009-01-22 15:37:47 +110088 int nbblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Alex Elderff30a622010-04-13 15:22:58 +100090 if (!xlog_buf_bbcount_valid(log, nbblks)) {
91 xlog_warn("XFS: Invalid block length (0x%x) given for buffer",
92 nbblks);
93 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_HIGH, log->l_mp);
Dave Chinner32281492009-01-22 15:37:47 +110094 return NULL;
95 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Alex Elder36adecf2010-04-13 15:21:13 +100097 /*
98 * We do log I/O in units of log sectors (a power-of-2
99 * multiple of the basic block size), so we round up the
100 * requested size to acommodate the basic blocks required
101 * for complete log sectors.
102 *
103 * In addition, the buffer may be used for a non-sector-
104 * aligned block offset, in which case an I/O of the
105 * requested size could extend beyond the end of the
106 * buffer. If the requested size is only 1 basic block it
107 * will never straddle a sector boundary, so this won't be
108 * an issue. Nor will this be a problem if the log I/O is
109 * done in basic blocks (sector size 1). But otherwise we
110 * extend the buffer by one extra log sector to ensure
111 * there's space to accomodate this possiblility.
112 */
113 if (nbblks > 1 && log->l_sectbb_log)
114 nbblks += xlog_sectbb(log);
115 nbblks = round_up(nbblks, xlog_sectbb(log));
116
Dave Chinner32281492009-01-22 15:37:47 +1100117 return xfs_buf_get_noaddr(BBTOB(nbblks), log->l_mp->m_logdev_targp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000120STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121xlog_put_bp(
122 xfs_buf_t *bp)
123{
124 xfs_buf_free(bp);
125}
126
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100127STATIC xfs_caddr_t
128xlog_align(
129 xlog_t *log,
130 xfs_daddr_t blk_no,
131 int nbblks,
132 xfs_buf_t *bp)
133{
134 xfs_caddr_t ptr;
135
136 if (!log->l_sectbb_log)
137 return XFS_BUF_PTR(bp);
138
139 ptr = XFS_BUF_PTR(bp) + BBTOB((int)blk_no & log->l_sectbb_mask);
140 ASSERT(XFS_BUF_SIZE(bp) >=
141 BBTOB(nbblks + (blk_no & log->l_sectbb_mask)));
142 return ptr;
143}
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146/*
147 * nbblks should be uint, but oh well. Just want to catch that 32-bit length.
148 */
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100149STATIC int
150xlog_bread_noalign(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 xlog_t *log,
152 xfs_daddr_t blk_no,
153 int nbblks,
154 xfs_buf_t *bp)
155{
156 int error;
157
Alex Elderff30a622010-04-13 15:22:58 +1000158 if (!xlog_buf_bbcount_valid(log, nbblks)) {
159 xlog_warn("XFS: Invalid block length (0x%x) given for buffer",
160 nbblks);
161 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_HIGH, log->l_mp);
Dave Chinner32281492009-01-22 15:37:47 +1100162 return EFSCORRUPTED;
163 }
164
Alex Elder36adecf2010-04-13 15:21:13 +1000165 blk_no = round_down(blk_no, xlog_sectbb(log));
166 nbblks = round_up(nbblks, xlog_sectbb(log));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 ASSERT(nbblks > 0);
169 ASSERT(BBTOB(nbblks) <= XFS_BUF_SIZE(bp));
170 ASSERT(bp);
171
172 XFS_BUF_SET_ADDR(bp, log->l_logBBstart + blk_no);
173 XFS_BUF_READ(bp);
174 XFS_BUF_BUSY(bp);
175 XFS_BUF_SET_COUNT(bp, BBTOB(nbblks));
176 XFS_BUF_SET_TARGET(bp, log->l_mp->m_logdev_targp);
177
178 xfsbdstrat(log->l_mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +1000179 error = xfs_iowait(bp);
180 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 xfs_ioerror_alert("xlog_bread", log->l_mp,
182 bp, XFS_BUF_ADDR(bp));
183 return error;
184}
185
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100186STATIC int
187xlog_bread(
188 xlog_t *log,
189 xfs_daddr_t blk_no,
190 int nbblks,
191 xfs_buf_t *bp,
192 xfs_caddr_t *offset)
193{
194 int error;
195
196 error = xlog_bread_noalign(log, blk_no, nbblks, bp);
197 if (error)
198 return error;
199
200 *offset = xlog_align(log, blk_no, nbblks, bp);
201 return 0;
202}
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/*
205 * Write out the buffer at the given block for the given number of blocks.
206 * The buffer is kept locked across the write and is returned locked.
207 * This can only be used for synchronous log writes.
208 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000209STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210xlog_bwrite(
211 xlog_t *log,
212 xfs_daddr_t blk_no,
213 int nbblks,
214 xfs_buf_t *bp)
215{
216 int error;
217
Alex Elderff30a622010-04-13 15:22:58 +1000218 if (!xlog_buf_bbcount_valid(log, nbblks)) {
219 xlog_warn("XFS: Invalid block length (0x%x) given for buffer",
220 nbblks);
221 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_HIGH, log->l_mp);
Dave Chinner32281492009-01-22 15:37:47 +1100222 return EFSCORRUPTED;
223 }
224
Alex Elder36adecf2010-04-13 15:21:13 +1000225 blk_no = round_down(blk_no, xlog_sectbb(log));
226 nbblks = round_up(nbblks, xlog_sectbb(log));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 ASSERT(nbblks > 0);
229 ASSERT(BBTOB(nbblks) <= XFS_BUF_SIZE(bp));
230
231 XFS_BUF_SET_ADDR(bp, log->l_logBBstart + blk_no);
232 XFS_BUF_ZEROFLAGS(bp);
233 XFS_BUF_BUSY(bp);
234 XFS_BUF_HOLD(bp);
235 XFS_BUF_PSEMA(bp, PRIBIO);
236 XFS_BUF_SET_COUNT(bp, BBTOB(nbblks));
237 XFS_BUF_SET_TARGET(bp, log->l_mp->m_logdev_targp);
238
239 if ((error = xfs_bwrite(log->l_mp, bp)))
240 xfs_ioerror_alert("xlog_bwrite", log->l_mp,
241 bp, XFS_BUF_ADDR(bp));
242 return error;
243}
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245#ifdef DEBUG
246/*
247 * dump debug superblock and log record information
248 */
249STATIC void
250xlog_header_check_dump(
251 xfs_mount_t *mp,
252 xlog_rec_header_t *head)
253{
Joe Perches03daa572009-12-14 18:01:10 -0800254 cmn_err(CE_DEBUG, "%s: SB : uuid = %pU, fmt = %d\n",
255 __func__, &mp->m_sb.sb_uuid, XLOG_FMT);
256 cmn_err(CE_DEBUG, " log : uuid = %pU, fmt = %d\n",
257 &head->h_fs_uuid, be32_to_cpu(head->h_fmt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259#else
260#define xlog_header_check_dump(mp, head)
261#endif
262
263/*
264 * check log record header for recovery
265 */
266STATIC int
267xlog_header_check_recover(
268 xfs_mount_t *mp,
269 xlog_rec_header_t *head)
270{
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000271 ASSERT(be32_to_cpu(head->h_magicno) == XLOG_HEADER_MAGIC_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 /*
274 * IRIX doesn't write the h_fmt field and leaves it zeroed
275 * (XLOG_FMT_UNKNOWN). This stops us from trying to recover
276 * a dirty log created in IRIX.
277 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000278 if (unlikely(be32_to_cpu(head->h_fmt) != XLOG_FMT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 xlog_warn(
280 "XFS: dirty log written in incompatible format - can't recover");
281 xlog_header_check_dump(mp, head);
282 XFS_ERROR_REPORT("xlog_header_check_recover(1)",
283 XFS_ERRLEVEL_HIGH, mp);
284 return XFS_ERROR(EFSCORRUPTED);
285 } else if (unlikely(!uuid_equal(&mp->m_sb.sb_uuid, &head->h_fs_uuid))) {
286 xlog_warn(
287 "XFS: dirty log entry has mismatched uuid - can't recover");
288 xlog_header_check_dump(mp, head);
289 XFS_ERROR_REPORT("xlog_header_check_recover(2)",
290 XFS_ERRLEVEL_HIGH, mp);
291 return XFS_ERROR(EFSCORRUPTED);
292 }
293 return 0;
294}
295
296/*
297 * read the head block of the log and check the header
298 */
299STATIC int
300xlog_header_check_mount(
301 xfs_mount_t *mp,
302 xlog_rec_header_t *head)
303{
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000304 ASSERT(be32_to_cpu(head->h_magicno) == XLOG_HEADER_MAGIC_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 if (uuid_is_nil(&head->h_fs_uuid)) {
307 /*
308 * IRIX doesn't write the h_fs_uuid or h_fmt fields. If
309 * h_fs_uuid is nil, we assume this log was last mounted
310 * by IRIX and continue.
311 */
312 xlog_warn("XFS: nil uuid in log - IRIX style log");
313 } else if (unlikely(!uuid_equal(&mp->m_sb.sb_uuid, &head->h_fs_uuid))) {
314 xlog_warn("XFS: log has mismatched uuid - can't recover");
315 xlog_header_check_dump(mp, head);
316 XFS_ERROR_REPORT("xlog_header_check_mount",
317 XFS_ERRLEVEL_HIGH, mp);
318 return XFS_ERROR(EFSCORRUPTED);
319 }
320 return 0;
321}
322
323STATIC void
324xlog_recover_iodone(
325 struct xfs_buf *bp)
326{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (XFS_BUF_GETERROR(bp)) {
328 /*
329 * We're not going to bother about retrying
330 * this during recovery. One strike!
331 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 xfs_ioerror_alert("xlog_recover_iodone",
Christoph Hellwig15ac08a2008-12-09 04:47:30 -0500333 bp->b_mount, bp, XFS_BUF_ADDR(bp));
334 xfs_force_shutdown(bp->b_mount, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
Christoph Hellwig15ac08a2008-12-09 04:47:30 -0500336 bp->b_mount = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 XFS_BUF_CLR_IODONE_FUNC(bp);
338 xfs_biodone(bp);
339}
340
341/*
342 * This routine finds (to an approximation) the first block in the physical
343 * log which contains the given cycle. It uses a binary search algorithm.
344 * Note that the algorithm can not be perfect because the disk will not
345 * necessarily be perfect.
346 */
David Chinnera8272ce2007-11-23 16:28:09 +1100347STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348xlog_find_cycle_start(
349 xlog_t *log,
350 xfs_buf_t *bp,
351 xfs_daddr_t first_blk,
352 xfs_daddr_t *last_blk,
353 uint cycle)
354{
355 xfs_caddr_t offset;
356 xfs_daddr_t mid_blk;
357 uint mid_cycle;
358 int error;
359
360 mid_blk = BLK_AVG(first_blk, *last_blk);
361 while (mid_blk != first_blk && mid_blk != *last_blk) {
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100362 error = xlog_bread(log, mid_blk, 1, bp, &offset);
363 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return error;
Christoph Hellwig03bea6f2007-10-12 10:58:05 +1000365 mid_cycle = xlog_get_cycle(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (mid_cycle == cycle) {
367 *last_blk = mid_blk;
368 /* last_half_cycle == mid_cycle */
369 } else {
370 first_blk = mid_blk;
371 /* first_half_cycle == mid_cycle */
372 }
373 mid_blk = BLK_AVG(first_blk, *last_blk);
374 }
375 ASSERT((mid_blk == first_blk && mid_blk+1 == *last_blk) ||
376 (mid_blk == *last_blk && mid_blk-1 == first_blk));
377
378 return 0;
379}
380
381/*
382 * Check that the range of blocks does not contain the cycle number
383 * given. The scan needs to occur from front to back and the ptr into the
384 * region must be updated since a later routine will need to perform another
385 * test. If the region is completely good, we end up returning the same
386 * last block number.
387 *
388 * Set blkno to -1 if we encounter no errors. This is an invalid block number
389 * since we don't ever expect logs to get this large.
390 */
391STATIC int
392xlog_find_verify_cycle(
393 xlog_t *log,
394 xfs_daddr_t start_blk,
395 int nbblks,
396 uint stop_on_cycle_no,
397 xfs_daddr_t *new_blk)
398{
399 xfs_daddr_t i, j;
400 uint cycle;
401 xfs_buf_t *bp;
402 xfs_daddr_t bufblks;
403 xfs_caddr_t buf = NULL;
404 int error = 0;
405
Alex Elder6881a222010-04-13 15:22:29 +1000406 /*
407 * Greedily allocate a buffer big enough to handle the full
408 * range of basic blocks we'll be examining. If that fails,
409 * try a smaller size. We need to be able to read at least
410 * a log sector, or we're out of luck.
411 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 bufblks = 1 << ffs(nbblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 while (!(bp = xlog_get_bp(log, bufblks))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 bufblks >>= 1;
Alex Elder6881a222010-04-13 15:22:29 +1000415 if (bufblks < xlog_sectbb(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return ENOMEM;
417 }
418
419 for (i = start_blk; i < start_blk + nbblks; i += bufblks) {
420 int bcount;
421
422 bcount = min(bufblks, (start_blk + nbblks - i));
423
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100424 error = xlog_bread(log, i, bcount, bp, &buf);
425 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 goto out;
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 for (j = 0; j < bcount; j++) {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +1000429 cycle = xlog_get_cycle(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (cycle == stop_on_cycle_no) {
431 *new_blk = i+j;
432 goto out;
433 }
434
435 buf += BBSIZE;
436 }
437 }
438
439 *new_blk = -1;
440
441out:
442 xlog_put_bp(bp);
443 return error;
444}
445
446/*
447 * Potentially backup over partial log record write.
448 *
449 * In the typical case, last_blk is the number of the block directly after
450 * a good log record. Therefore, we subtract one to get the block number
451 * of the last block in the given buffer. extra_bblks contains the number
452 * of blocks we would have read on a previous read. This happens when the
453 * last log record is split over the end of the physical log.
454 *
455 * extra_bblks is the number of blocks potentially verified on a previous
456 * call to this routine.
457 */
458STATIC int
459xlog_find_verify_log_record(
460 xlog_t *log,
461 xfs_daddr_t start_blk,
462 xfs_daddr_t *last_blk,
463 int extra_bblks)
464{
465 xfs_daddr_t i;
466 xfs_buf_t *bp;
467 xfs_caddr_t offset = NULL;
468 xlog_rec_header_t *head = NULL;
469 int error = 0;
470 int smallmem = 0;
471 int num_blks = *last_blk - start_blk;
472 int xhdrs;
473
474 ASSERT(start_blk != 0 || *last_blk != start_blk);
475
476 if (!(bp = xlog_get_bp(log, num_blks))) {
477 if (!(bp = xlog_get_bp(log, 1)))
478 return ENOMEM;
479 smallmem = 1;
480 } else {
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100481 error = xlog_bread(log, start_blk, num_blks, bp, &offset);
482 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 offset += ((num_blks - 1) << BBSHIFT);
485 }
486
487 for (i = (*last_blk) - 1; i >= 0; i--) {
488 if (i < start_blk) {
489 /* valid log record not found */
490 xlog_warn(
491 "XFS: Log inconsistent (didn't find previous header)");
492 ASSERT(0);
493 error = XFS_ERROR(EIO);
494 goto out;
495 }
496
497 if (smallmem) {
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100498 error = xlog_bread(log, i, 1, bp, &offset);
499 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502
503 head = (xlog_rec_header_t *)offset;
504
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000505 if (XLOG_HEADER_MAGIC_NUM == be32_to_cpu(head->h_magicno))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 break;
507
508 if (!smallmem)
509 offset -= BBSIZE;
510 }
511
512 /*
513 * We hit the beginning of the physical log & still no header. Return
514 * to caller. If caller can handle a return of -1, then this routine
515 * will be called again for the end of the physical log.
516 */
517 if (i == -1) {
518 error = -1;
519 goto out;
520 }
521
522 /*
523 * We have the final block of the good log (the first block
524 * of the log record _before_ the head. So we check the uuid.
525 */
526 if ((error = xlog_header_check_mount(log->l_mp, head)))
527 goto out;
528
529 /*
530 * We may have found a log record header before we expected one.
531 * last_blk will be the 1st block # with a given cycle #. We may end
532 * up reading an entire log record. In this case, we don't want to
533 * reset last_blk. Only when last_blk points in the middle of a log
534 * record do we update last_blk.
535 */
Eric Sandeen62118702008-03-06 13:44:28 +1100536 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000537 uint h_size = be32_to_cpu(head->h_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 xhdrs = h_size / XLOG_HEADER_CYCLE_SIZE;
540 if (h_size % XLOG_HEADER_CYCLE_SIZE)
541 xhdrs++;
542 } else {
543 xhdrs = 1;
544 }
545
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000546 if (*last_blk - i + extra_bblks !=
547 BTOBB(be32_to_cpu(head->h_len)) + xhdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 *last_blk = i;
549
550out:
551 xlog_put_bp(bp);
552 return error;
553}
554
555/*
556 * Head is defined to be the point of the log where the next log write
557 * write could go. This means that incomplete LR writes at the end are
558 * eliminated when calculating the head. We aren't guaranteed that previous
559 * LR have complete transactions. We only know that a cycle number of
560 * current cycle number -1 won't be present in the log if we start writing
561 * from our current block number.
562 *
563 * last_blk contains the block number of the first block with a given
564 * cycle number.
565 *
566 * Return: zero if normal, non-zero if error.
567 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000568STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569xlog_find_head(
570 xlog_t *log,
571 xfs_daddr_t *return_head_blk)
572{
573 xfs_buf_t *bp;
574 xfs_caddr_t offset;
575 xfs_daddr_t new_blk, first_blk, start_blk, last_blk, head_blk;
576 int num_scan_bblks;
577 uint first_half_cycle, last_half_cycle;
578 uint stop_on_cycle;
579 int error, log_bbnum = log->l_logBBsize;
580
581 /* Is the end of the log device zeroed? */
582 if ((error = xlog_find_zeroed(log, &first_blk)) == -1) {
583 *return_head_blk = first_blk;
584
585 /* Is the whole lot zeroed? */
586 if (!first_blk) {
587 /* Linux XFS shouldn't generate totally zeroed logs -
588 * mkfs etc write a dummy unmount record to a fresh
589 * log so we can store the uuid in there
590 */
591 xlog_warn("XFS: totally zeroed log");
592 }
593
594 return 0;
595 } else if (error) {
596 xlog_warn("XFS: empty log check failed");
597 return error;
598 }
599
600 first_blk = 0; /* get cycle # of 1st block */
601 bp = xlog_get_bp(log, 1);
602 if (!bp)
603 return ENOMEM;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100604
605 error = xlog_bread(log, 0, 1, bp, &offset);
606 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 goto bp_err;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100608
Christoph Hellwig03bea6f2007-10-12 10:58:05 +1000609 first_half_cycle = xlog_get_cycle(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 last_blk = head_blk = log_bbnum - 1; /* get cycle # of last block */
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100612 error = xlog_bread(log, last_blk, 1, bp, &offset);
613 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 goto bp_err;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100615
Christoph Hellwig03bea6f2007-10-12 10:58:05 +1000616 last_half_cycle = xlog_get_cycle(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 ASSERT(last_half_cycle != 0);
618
619 /*
620 * If the 1st half cycle number is equal to the last half cycle number,
621 * then the entire log is stamped with the same cycle number. In this
622 * case, head_blk can't be set to zero (which makes sense). The below
623 * math doesn't work out properly with head_blk equal to zero. Instead,
624 * we set it to log_bbnum which is an invalid block number, but this
625 * value makes the math correct. If head_blk doesn't changed through
626 * all the tests below, *head_blk is set to zero at the very end rather
627 * than log_bbnum. In a sense, log_bbnum and zero are the same block
628 * in a circular file.
629 */
630 if (first_half_cycle == last_half_cycle) {
631 /*
632 * In this case we believe that the entire log should have
633 * cycle number last_half_cycle. We need to scan backwards
634 * from the end verifying that there are no holes still
635 * containing last_half_cycle - 1. If we find such a hole,
636 * then the start of that hole will be the new head. The
637 * simple case looks like
638 * x | x ... | x - 1 | x
639 * Another case that fits this picture would be
640 * x | x + 1 | x ... | x
Nathan Scottc41564b2006-03-29 08:55:14 +1000641 * In this case the head really is somewhere at the end of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 * log, as one of the latest writes at the beginning was
643 * incomplete.
644 * One more case is
645 * x | x + 1 | x ... | x - 1 | x
646 * This is really the combination of the above two cases, and
647 * the head has to end up at the start of the x-1 hole at the
648 * end of the log.
649 *
650 * In the 256k log case, we will read from the beginning to the
651 * end of the log and search for cycle numbers equal to x-1.
652 * We don't worry about the x+1 blocks that we encounter,
653 * because we know that they cannot be the head since the log
654 * started with x.
655 */
656 head_blk = log_bbnum;
657 stop_on_cycle = last_half_cycle - 1;
658 } else {
659 /*
660 * In this case we want to find the first block with cycle
661 * number matching last_half_cycle. We expect the log to be
662 * some variation on
663 * x + 1 ... | x ...
664 * The first block with cycle number x (last_half_cycle) will
665 * be where the new head belongs. First we do a binary search
666 * for the first occurrence of last_half_cycle. The binary
667 * search may not be totally accurate, so then we scan back
668 * from there looking for occurrences of last_half_cycle before
669 * us. If that backwards scan wraps around the beginning of
670 * the log, then we look for occurrences of last_half_cycle - 1
671 * at the end of the log. The cases we're looking for look
672 * like
673 * x + 1 ... | x | x + 1 | x ...
674 * ^ binary search stopped here
675 * or
676 * x + 1 ... | x ... | x - 1 | x
677 * <---------> less than scan distance
678 */
679 stop_on_cycle = last_half_cycle;
680 if ((error = xlog_find_cycle_start(log, bp, first_blk,
681 &head_blk, last_half_cycle)))
682 goto bp_err;
683 }
684
685 /*
686 * Now validate the answer. Scan back some number of maximum possible
687 * blocks and make sure each one has the expected cycle number. The
688 * maximum is determined by the total possible amount of buffering
689 * in the in-core log. The following number can be made tighter if
690 * we actually look at the block size of the filesystem.
691 */
692 num_scan_bblks = XLOG_TOTAL_REC_SHIFT(log);
693 if (head_blk >= num_scan_bblks) {
694 /*
695 * We are guaranteed that the entire check can be performed
696 * in one buffer.
697 */
698 start_blk = head_blk - num_scan_bblks;
699 if ((error = xlog_find_verify_cycle(log,
700 start_blk, num_scan_bblks,
701 stop_on_cycle, &new_blk)))
702 goto bp_err;
703 if (new_blk != -1)
704 head_blk = new_blk;
705 } else { /* need to read 2 parts of log */
706 /*
707 * We are going to scan backwards in the log in two parts.
708 * First we scan the physical end of the log. In this part
709 * of the log, we are looking for blocks with cycle number
710 * last_half_cycle - 1.
711 * If we find one, then we know that the log starts there, as
712 * we've found a hole that didn't get written in going around
713 * the end of the physical log. The simple case for this is
714 * x + 1 ... | x ... | x - 1 | x
715 * <---------> less than scan distance
716 * If all of the blocks at the end of the log have cycle number
717 * last_half_cycle, then we check the blocks at the start of
718 * the log looking for occurrences of last_half_cycle. If we
719 * find one, then our current estimate for the location of the
720 * first occurrence of last_half_cycle is wrong and we move
721 * back to the hole we've found. This case looks like
722 * x + 1 ... | x | x + 1 | x ...
723 * ^ binary search stopped here
724 * Another case we need to handle that only occurs in 256k
725 * logs is
726 * x + 1 ... | x ... | x+1 | x ...
727 * ^ binary search stops here
728 * In a 256k log, the scan at the end of the log will see the
729 * x + 1 blocks. We need to skip past those since that is
730 * certainly not the head of the log. By searching for
731 * last_half_cycle-1 we accomplish that.
732 */
733 start_blk = log_bbnum - num_scan_bblks + head_blk;
734 ASSERT(head_blk <= INT_MAX &&
735 (xfs_daddr_t) num_scan_bblks - head_blk >= 0);
736 if ((error = xlog_find_verify_cycle(log, start_blk,
737 num_scan_bblks - (int)head_blk,
738 (stop_on_cycle - 1), &new_blk)))
739 goto bp_err;
740 if (new_blk != -1) {
741 head_blk = new_blk;
742 goto bad_blk;
743 }
744
745 /*
746 * Scan beginning of log now. The last part of the physical
747 * log is good. This scan needs to verify that it doesn't find
748 * the last_half_cycle.
749 */
750 start_blk = 0;
751 ASSERT(head_blk <= INT_MAX);
752 if ((error = xlog_find_verify_cycle(log,
753 start_blk, (int)head_blk,
754 stop_on_cycle, &new_blk)))
755 goto bp_err;
756 if (new_blk != -1)
757 head_blk = new_blk;
758 }
759
760 bad_blk:
761 /*
762 * Now we need to make sure head_blk is not pointing to a block in
763 * the middle of a log record.
764 */
765 num_scan_bblks = XLOG_REC_SHIFT(log);
766 if (head_blk >= num_scan_bblks) {
767 start_blk = head_blk - num_scan_bblks; /* don't read head_blk */
768
769 /* start ptr at last block ptr before head_blk */
770 if ((error = xlog_find_verify_log_record(log, start_blk,
771 &head_blk, 0)) == -1) {
772 error = XFS_ERROR(EIO);
773 goto bp_err;
774 } else if (error)
775 goto bp_err;
776 } else {
777 start_blk = 0;
778 ASSERT(head_blk <= INT_MAX);
779 if ((error = xlog_find_verify_log_record(log, start_blk,
780 &head_blk, 0)) == -1) {
781 /* We hit the beginning of the log during our search */
782 start_blk = log_bbnum - num_scan_bblks + head_blk;
783 new_blk = log_bbnum;
784 ASSERT(start_blk <= INT_MAX &&
785 (xfs_daddr_t) log_bbnum-start_blk >= 0);
786 ASSERT(head_blk <= INT_MAX);
787 if ((error = xlog_find_verify_log_record(log,
788 start_blk, &new_blk,
789 (int)head_blk)) == -1) {
790 error = XFS_ERROR(EIO);
791 goto bp_err;
792 } else if (error)
793 goto bp_err;
794 if (new_blk != log_bbnum)
795 head_blk = new_blk;
796 } else if (error)
797 goto bp_err;
798 }
799
800 xlog_put_bp(bp);
801 if (head_blk == log_bbnum)
802 *return_head_blk = 0;
803 else
804 *return_head_blk = head_blk;
805 /*
806 * When returning here, we have a good block number. Bad block
807 * means that during a previous crash, we didn't have a clean break
808 * from cycle number N to cycle number N-1. In this case, we need
809 * to find the first block with cycle number N-1.
810 */
811 return 0;
812
813 bp_err:
814 xlog_put_bp(bp);
815
816 if (error)
817 xlog_warn("XFS: failed to find log head");
818 return error;
819}
820
821/*
822 * Find the sync block number or the tail of the log.
823 *
824 * This will be the block number of the last record to have its
825 * associated buffers synced to disk. Every log record header has
826 * a sync lsn embedded in it. LSNs hold block numbers, so it is easy
827 * to get a sync block number. The only concern is to figure out which
828 * log record header to believe.
829 *
830 * The following algorithm uses the log record header with the largest
831 * lsn. The entire log record does not need to be valid. We only care
832 * that the header is valid.
833 *
834 * We could speed up search by using current head_blk buffer, but it is not
835 * available.
836 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000837STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838xlog_find_tail(
839 xlog_t *log,
840 xfs_daddr_t *head_blk,
Eric Sandeen65be6052006-01-11 15:34:19 +1100841 xfs_daddr_t *tail_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 xlog_rec_header_t *rhead;
844 xlog_op_header_t *op_head;
845 xfs_caddr_t offset = NULL;
846 xfs_buf_t *bp;
847 int error, i, found;
848 xfs_daddr_t umount_data_blk;
849 xfs_daddr_t after_umount_blk;
850 xfs_lsn_t tail_lsn;
851 int hblks;
852
853 found = 0;
854
855 /*
856 * Find previous log record
857 */
858 if ((error = xlog_find_head(log, head_blk)))
859 return error;
860
861 bp = xlog_get_bp(log, 1);
862 if (!bp)
863 return ENOMEM;
864 if (*head_blk == 0) { /* special case */
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100865 error = xlog_bread(log, 0, 1, bp, &offset);
866 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 goto bread_err;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100868
Christoph Hellwig03bea6f2007-10-12 10:58:05 +1000869 if (xlog_get_cycle(offset) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 *tail_blk = 0;
871 /* leave all other log inited values alone */
872 goto exit;
873 }
874 }
875
876 /*
877 * Search backwards looking for log record header block
878 */
879 ASSERT(*head_blk < INT_MAX);
880 for (i = (int)(*head_blk) - 1; i >= 0; i--) {
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100881 error = xlog_bread(log, i, 1, bp, &offset);
882 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 goto bread_err;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100884
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000885 if (XLOG_HEADER_MAGIC_NUM == be32_to_cpu(*(__be32 *)offset)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 found = 1;
887 break;
888 }
889 }
890 /*
891 * If we haven't found the log record header block, start looking
892 * again from the end of the physical log. XXXmiken: There should be
893 * a check here to make sure we didn't search more than N blocks in
894 * the previous code.
895 */
896 if (!found) {
897 for (i = log->l_logBBsize - 1; i >= (int)(*head_blk); i--) {
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100898 error = xlog_bread(log, i, 1, bp, &offset);
899 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 goto bread_err;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if (XLOG_HEADER_MAGIC_NUM ==
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000903 be32_to_cpu(*(__be32 *)offset)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 found = 2;
905 break;
906 }
907 }
908 }
909 if (!found) {
910 xlog_warn("XFS: xlog_find_tail: couldn't find sync record");
911 ASSERT(0);
912 return XFS_ERROR(EIO);
913 }
914
915 /* find blk_no of tail of log */
916 rhead = (xlog_rec_header_t *)offset;
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000917 *tail_blk = BLOCK_LSN(be64_to_cpu(rhead->h_tail_lsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 /*
920 * Reset log values according to the state of the log when we
921 * crashed. In the case where head_blk == 0, we bump curr_cycle
922 * one because the next write starts a new cycle rather than
923 * continuing the cycle of the last good log record. At this
924 * point we have guaranteed that all partial log records have been
925 * accounted for. Therefore, we know that the last good log record
926 * written was complete and ended exactly on the end boundary
927 * of the physical log.
928 */
929 log->l_prev_block = i;
930 log->l_curr_block = (int)*head_blk;
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000931 log->l_curr_cycle = be32_to_cpu(rhead->h_cycle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (found == 2)
933 log->l_curr_cycle++;
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000934 log->l_tail_lsn = be64_to_cpu(rhead->h_tail_lsn);
935 log->l_last_sync_lsn = be64_to_cpu(rhead->h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 log->l_grant_reserve_cycle = log->l_curr_cycle;
937 log->l_grant_reserve_bytes = BBTOB(log->l_curr_block);
938 log->l_grant_write_cycle = log->l_curr_cycle;
939 log->l_grant_write_bytes = BBTOB(log->l_curr_block);
940
941 /*
942 * Look for unmount record. If we find it, then we know there
943 * was a clean unmount. Since 'i' could be the last block in
944 * the physical log, we convert to a log block before comparing
945 * to the head_blk.
946 *
947 * Save the current tail lsn to use to pass to
948 * xlog_clear_stale_blocks() below. We won't want to clear the
949 * unmount record if there is one, so we pass the lsn of the
950 * unmount record rather than the block after it.
951 */
Eric Sandeen62118702008-03-06 13:44:28 +1100952 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000953 int h_size = be32_to_cpu(rhead->h_size);
954 int h_version = be32_to_cpu(rhead->h_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 if ((h_version & XLOG_VERSION_2) &&
957 (h_size > XLOG_HEADER_CYCLE_SIZE)) {
958 hblks = h_size / XLOG_HEADER_CYCLE_SIZE;
959 if (h_size % XLOG_HEADER_CYCLE_SIZE)
960 hblks++;
961 } else {
962 hblks = 1;
963 }
964 } else {
965 hblks = 1;
966 }
967 after_umount_blk = (i + hblks + (int)
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000968 BTOBB(be32_to_cpu(rhead->h_len))) % log->l_logBBsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 tail_lsn = log->l_tail_lsn;
970 if (*head_blk == after_umount_blk &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000971 be32_to_cpu(rhead->h_num_logops) == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 umount_data_blk = (i + hblks) % log->l_logBBsize;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100973 error = xlog_bread(log, umount_data_blk, 1, bp, &offset);
974 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 goto bread_err;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 op_head = (xlog_op_header_t *)offset;
978 if (op_head->oh_flags & XLOG_UNMOUNT_TRANS) {
979 /*
980 * Set tail and last sync so that newly written
981 * log records will point recovery to after the
982 * current unmount record.
983 */
Christoph Hellwig03bea6f2007-10-12 10:58:05 +1000984 log->l_tail_lsn =
985 xlog_assign_lsn(log->l_curr_cycle,
986 after_umount_blk);
987 log->l_last_sync_lsn =
988 xlog_assign_lsn(log->l_curr_cycle,
989 after_umount_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 *tail_blk = after_umount_blk;
David Chinner92821e22007-05-24 15:26:31 +1000991
992 /*
993 * Note that the unmount was clean. If the unmount
994 * was not clean, we need to know this to rebuild the
995 * superblock counters from the perag headers if we
996 * have a filesystem using non-persistent counters.
997 */
998 log->l_mp->m_flags |= XFS_MOUNT_WAS_CLEAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
1000 }
1001
1002 /*
1003 * Make sure that there are no blocks in front of the head
1004 * with the same cycle number as the head. This can happen
1005 * because we allow multiple outstanding log writes concurrently,
1006 * and the later writes might make it out before earlier ones.
1007 *
1008 * We use the lsn from before modifying it so that we'll never
1009 * overwrite the unmount record after a clean unmount.
1010 *
1011 * Do this only if we are going to recover the filesystem
1012 *
1013 * NOTE: This used to say "if (!readonly)"
1014 * However on Linux, we can & do recover a read-only filesystem.
1015 * We only skip recovery if NORECOVERY is specified on mount,
1016 * in which case we would not be here.
1017 *
1018 * But... if the -device- itself is readonly, just skip this.
1019 * We can't recover this device anyway, so it won't matter.
1020 */
1021 if (!xfs_readonly_buftarg(log->l_mp->m_logdev_targp)) {
1022 error = xlog_clear_stale_blocks(log, tail_lsn);
1023 }
1024
1025bread_err:
1026exit:
1027 xlog_put_bp(bp);
1028
1029 if (error)
1030 xlog_warn("XFS: failed to locate log tail");
1031 return error;
1032}
1033
1034/*
1035 * Is the log zeroed at all?
1036 *
1037 * The last binary search should be changed to perform an X block read
1038 * once X becomes small enough. You can then search linearly through
1039 * the X blocks. This will cut down on the number of reads we need to do.
1040 *
1041 * If the log is partially zeroed, this routine will pass back the blkno
1042 * of the first block with cycle number 0. It won't have a complete LR
1043 * preceding it.
1044 *
1045 * Return:
1046 * 0 => the log is completely written to
1047 * -1 => use *blk_no as the first block of the log
1048 * >0 => error has occurred
1049 */
David Chinnera8272ce2007-11-23 16:28:09 +11001050STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051xlog_find_zeroed(
1052 xlog_t *log,
1053 xfs_daddr_t *blk_no)
1054{
1055 xfs_buf_t *bp;
1056 xfs_caddr_t offset;
1057 uint first_cycle, last_cycle;
1058 xfs_daddr_t new_blk, last_blk, start_blk;
1059 xfs_daddr_t num_scan_bblks;
1060 int error, log_bbnum = log->l_logBBsize;
1061
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10001062 *blk_no = 0;
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 /* check totally zeroed log */
1065 bp = xlog_get_bp(log, 1);
1066 if (!bp)
1067 return ENOMEM;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001068 error = xlog_bread(log, 0, 1, bp, &offset);
1069 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 goto bp_err;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001071
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001072 first_cycle = xlog_get_cycle(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (first_cycle == 0) { /* completely zeroed log */
1074 *blk_no = 0;
1075 xlog_put_bp(bp);
1076 return -1;
1077 }
1078
1079 /* check partially zeroed log */
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001080 error = xlog_bread(log, log_bbnum-1, 1, bp, &offset);
1081 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 goto bp_err;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001083
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001084 last_cycle = xlog_get_cycle(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (last_cycle != 0) { /* log completely written to */
1086 xlog_put_bp(bp);
1087 return 0;
1088 } else if (first_cycle != 1) {
1089 /*
1090 * If the cycle of the last block is zero, the cycle of
1091 * the first block must be 1. If it's not, maybe we're
1092 * not looking at a log... Bail out.
1093 */
1094 xlog_warn("XFS: Log inconsistent or not a log (last==0, first!=1)");
1095 return XFS_ERROR(EINVAL);
1096 }
1097
1098 /* we have a partially zeroed log */
1099 last_blk = log_bbnum-1;
1100 if ((error = xlog_find_cycle_start(log, bp, 0, &last_blk, 0)))
1101 goto bp_err;
1102
1103 /*
1104 * Validate the answer. Because there is no way to guarantee that
1105 * the entire log is made up of log records which are the same size,
1106 * we scan over the defined maximum blocks. At this point, the maximum
1107 * is not chosen to mean anything special. XXXmiken
1108 */
1109 num_scan_bblks = XLOG_TOTAL_REC_SHIFT(log);
1110 ASSERT(num_scan_bblks <= INT_MAX);
1111
1112 if (last_blk < num_scan_bblks)
1113 num_scan_bblks = last_blk;
1114 start_blk = last_blk - num_scan_bblks;
1115
1116 /*
1117 * We search for any instances of cycle number 0 that occur before
1118 * our current estimate of the head. What we're trying to detect is
1119 * 1 ... | 0 | 1 | 0...
1120 * ^ binary search ends here
1121 */
1122 if ((error = xlog_find_verify_cycle(log, start_blk,
1123 (int)num_scan_bblks, 0, &new_blk)))
1124 goto bp_err;
1125 if (new_blk != -1)
1126 last_blk = new_blk;
1127
1128 /*
1129 * Potentially backup over partial log record write. We don't need
1130 * to search the end of the log because we know it is zero.
1131 */
1132 if ((error = xlog_find_verify_log_record(log, start_blk,
1133 &last_blk, 0)) == -1) {
1134 error = XFS_ERROR(EIO);
1135 goto bp_err;
1136 } else if (error)
1137 goto bp_err;
1138
1139 *blk_no = last_blk;
1140bp_err:
1141 xlog_put_bp(bp);
1142 if (error)
1143 return error;
1144 return -1;
1145}
1146
1147/*
1148 * These are simple subroutines used by xlog_clear_stale_blocks() below
1149 * to initialize a buffer full of empty log record headers and write
1150 * them into the log.
1151 */
1152STATIC void
1153xlog_add_record(
1154 xlog_t *log,
1155 xfs_caddr_t buf,
1156 int cycle,
1157 int block,
1158 int tail_cycle,
1159 int tail_block)
1160{
1161 xlog_rec_header_t *recp = (xlog_rec_header_t *)buf;
1162
1163 memset(buf, 0, BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001164 recp->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1165 recp->h_cycle = cpu_to_be32(cycle);
1166 recp->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001167 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001168 recp->h_lsn = cpu_to_be64(xlog_assign_lsn(cycle, block));
1169 recp->h_tail_lsn = cpu_to_be64(xlog_assign_lsn(tail_cycle, tail_block));
1170 recp->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 memcpy(&recp->h_fs_uuid, &log->l_mp->m_sb.sb_uuid, sizeof(uuid_t));
1172}
1173
1174STATIC int
1175xlog_write_log_records(
1176 xlog_t *log,
1177 int cycle,
1178 int start_block,
1179 int blocks,
1180 int tail_cycle,
1181 int tail_block)
1182{
1183 xfs_caddr_t offset;
1184 xfs_buf_t *bp;
1185 int balign, ealign;
Alex Elder5c17f532010-04-13 15:22:48 +10001186 int sectbb = xlog_sectbb(log);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 int end_block = start_block + blocks;
1188 int bufblks;
1189 int error = 0;
1190 int i, j = 0;
1191
Alex Elder6881a222010-04-13 15:22:29 +10001192 /*
1193 * Greedily allocate a buffer big enough to handle the full
1194 * range of basic blocks to be written. If that fails, try
1195 * a smaller size. We need to be able to write at least a
1196 * log sector, or we're out of luck.
1197 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 bufblks = 1 << ffs(blocks);
1199 while (!(bp = xlog_get_bp(log, bufblks))) {
1200 bufblks >>= 1;
Alex Elder6881a222010-04-13 15:22:29 +10001201 if (bufblks < xlog_sectbb(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return ENOMEM;
1203 }
1204
1205 /* We may need to do a read at the start to fill in part of
1206 * the buffer in the starting sector not covered by the first
1207 * write below.
1208 */
Alex Elder5c17f532010-04-13 15:22:48 +10001209 balign = round_down(start_block, sectbb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (balign != start_block) {
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001211 error = xlog_bread_noalign(log, start_block, 1, bp);
1212 if (error)
1213 goto out_put_bp;
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 j = start_block - balign;
1216 }
1217
1218 for (i = start_block; i < end_block; i += bufblks) {
1219 int bcount, endcount;
1220
1221 bcount = min(bufblks, end_block - start_block);
1222 endcount = bcount - j;
1223
1224 /* We may need to do a read at the end to fill in part of
1225 * the buffer in the final sector not covered by the write.
1226 * If this is the same sector as the above read, skip it.
1227 */
Alex Elder5c17f532010-04-13 15:22:48 +10001228 ealign = round_down(end_block, sectbb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (j == 0 && (start_block + endcount > ealign)) {
1230 offset = XFS_BUF_PTR(bp);
1231 balign = BBTOB(ealign - start_block);
David Chinner234f56a2008-04-10 12:24:24 +10001232 error = XFS_BUF_SET_PTR(bp, offset + balign,
1233 BBTOB(sectbb));
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001234 if (error)
1235 break;
1236
1237 error = xlog_bread_noalign(log, ealign, sectbb, bp);
1238 if (error)
1239 break;
1240
1241 error = XFS_BUF_SET_PTR(bp, offset, bufblks);
David Chinner234f56a2008-04-10 12:24:24 +10001242 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
1245
1246 offset = xlog_align(log, start_block, endcount, bp);
1247 for (; j < endcount; j++) {
1248 xlog_add_record(log, offset, cycle, i+j,
1249 tail_cycle, tail_block);
1250 offset += BBSIZE;
1251 }
1252 error = xlog_bwrite(log, start_block, endcount, bp);
1253 if (error)
1254 break;
1255 start_block += endcount;
1256 j = 0;
1257 }
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001258
1259 out_put_bp:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 xlog_put_bp(bp);
1261 return error;
1262}
1263
1264/*
1265 * This routine is called to blow away any incomplete log writes out
1266 * in front of the log head. We do this so that we won't become confused
1267 * if we come up, write only a little bit more, and then crash again.
1268 * If we leave the partial log records out there, this situation could
1269 * cause us to think those partial writes are valid blocks since they
1270 * have the current cycle number. We get rid of them by overwriting them
1271 * with empty log records with the old cycle number rather than the
1272 * current one.
1273 *
1274 * The tail lsn is passed in rather than taken from
1275 * the log so that we will not write over the unmount record after a
1276 * clean unmount in a 512 block log. Doing so would leave the log without
1277 * any valid log records in it until a new one was written. If we crashed
1278 * during that time we would not be able to recover.
1279 */
1280STATIC int
1281xlog_clear_stale_blocks(
1282 xlog_t *log,
1283 xfs_lsn_t tail_lsn)
1284{
1285 int tail_cycle, head_cycle;
1286 int tail_block, head_block;
1287 int tail_distance, max_distance;
1288 int distance;
1289 int error;
1290
1291 tail_cycle = CYCLE_LSN(tail_lsn);
1292 tail_block = BLOCK_LSN(tail_lsn);
1293 head_cycle = log->l_curr_cycle;
1294 head_block = log->l_curr_block;
1295
1296 /*
1297 * Figure out the distance between the new head of the log
1298 * and the tail. We want to write over any blocks beyond the
1299 * head that we may have written just before the crash, but
1300 * we don't want to overwrite the tail of the log.
1301 */
1302 if (head_cycle == tail_cycle) {
1303 /*
1304 * The tail is behind the head in the physical log,
1305 * so the distance from the head to the tail is the
1306 * distance from the head to the end of the log plus
1307 * the distance from the beginning of the log to the
1308 * tail.
1309 */
1310 if (unlikely(head_block < tail_block || head_block >= log->l_logBBsize)) {
1311 XFS_ERROR_REPORT("xlog_clear_stale_blocks(1)",
1312 XFS_ERRLEVEL_LOW, log->l_mp);
1313 return XFS_ERROR(EFSCORRUPTED);
1314 }
1315 tail_distance = tail_block + (log->l_logBBsize - head_block);
1316 } else {
1317 /*
1318 * The head is behind the tail in the physical log,
1319 * so the distance from the head to the tail is just
1320 * the tail block minus the head block.
1321 */
1322 if (unlikely(head_block >= tail_block || head_cycle != (tail_cycle + 1))){
1323 XFS_ERROR_REPORT("xlog_clear_stale_blocks(2)",
1324 XFS_ERRLEVEL_LOW, log->l_mp);
1325 return XFS_ERROR(EFSCORRUPTED);
1326 }
1327 tail_distance = tail_block - head_block;
1328 }
1329
1330 /*
1331 * If the head is right up against the tail, we can't clear
1332 * anything.
1333 */
1334 if (tail_distance <= 0) {
1335 ASSERT(tail_distance == 0);
1336 return 0;
1337 }
1338
1339 max_distance = XLOG_TOTAL_REC_SHIFT(log);
1340 /*
1341 * Take the smaller of the maximum amount of outstanding I/O
1342 * we could have and the distance to the tail to clear out.
1343 * We take the smaller so that we don't overwrite the tail and
1344 * we don't waste all day writing from the head to the tail
1345 * for no reason.
1346 */
1347 max_distance = MIN(max_distance, tail_distance);
1348
1349 if ((head_block + max_distance) <= log->l_logBBsize) {
1350 /*
1351 * We can stomp all the blocks we need to without
1352 * wrapping around the end of the log. Just do it
1353 * in a single write. Use the cycle number of the
1354 * current cycle minus one so that the log will look like:
1355 * n ... | n - 1 ...
1356 */
1357 error = xlog_write_log_records(log, (head_cycle - 1),
1358 head_block, max_distance, tail_cycle,
1359 tail_block);
1360 if (error)
1361 return error;
1362 } else {
1363 /*
1364 * We need to wrap around the end of the physical log in
1365 * order to clear all the blocks. Do it in two separate
1366 * I/Os. The first write should be from the head to the
1367 * end of the physical log, and it should use the current
1368 * cycle number minus one just like above.
1369 */
1370 distance = log->l_logBBsize - head_block;
1371 error = xlog_write_log_records(log, (head_cycle - 1),
1372 head_block, distance, tail_cycle,
1373 tail_block);
1374
1375 if (error)
1376 return error;
1377
1378 /*
1379 * Now write the blocks at the start of the physical log.
1380 * This writes the remainder of the blocks we want to clear.
1381 * It uses the current cycle number since we're now on the
1382 * same cycle as the head so that we get:
1383 * n ... n ... | n - 1 ...
1384 * ^^^^^ blocks we're writing
1385 */
1386 distance = max_distance - (log->l_logBBsize - head_block);
1387 error = xlog_write_log_records(log, head_cycle, 0, distance,
1388 tail_cycle, tail_block);
1389 if (error)
1390 return error;
1391 }
1392
1393 return 0;
1394}
1395
1396/******************************************************************************
1397 *
1398 * Log recover routines
1399 *
1400 ******************************************************************************
1401 */
1402
1403STATIC xlog_recover_t *
1404xlog_recover_find_tid(
Dave Chinnerf0a76952010-01-11 11:49:57 +00001405 struct hlist_head *head,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 xlog_tid_t tid)
1407{
Dave Chinnerf0a76952010-01-11 11:49:57 +00001408 xlog_recover_t *trans;
1409 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Dave Chinnerf0a76952010-01-11 11:49:57 +00001411 hlist_for_each_entry(trans, n, head, r_list) {
1412 if (trans->r_log_tid == tid)
1413 return trans;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
Dave Chinnerf0a76952010-01-11 11:49:57 +00001415 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
1417
1418STATIC void
Dave Chinnerf0a76952010-01-11 11:49:57 +00001419xlog_recover_new_tid(
1420 struct hlist_head *head,
1421 xlog_tid_t tid,
1422 xfs_lsn_t lsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
Dave Chinnerf0a76952010-01-11 11:49:57 +00001424 xlog_recover_t *trans;
1425
1426 trans = kmem_zalloc(sizeof(xlog_recover_t), KM_SLEEP);
1427 trans->r_log_tid = tid;
1428 trans->r_lsn = lsn;
1429 INIT_LIST_HEAD(&trans->r_itemq);
1430
1431 INIT_HLIST_NODE(&trans->r_list);
1432 hlist_add_head(&trans->r_list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
1434
1435STATIC void
1436xlog_recover_add_item(
Dave Chinnerf0a76952010-01-11 11:49:57 +00001437 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
1439 xlog_recover_item_t *item;
1440
1441 item = kmem_zalloc(sizeof(xlog_recover_item_t), KM_SLEEP);
Dave Chinnerf0a76952010-01-11 11:49:57 +00001442 INIT_LIST_HEAD(&item->ri_list);
1443 list_add_tail(&item->ri_list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
1445
1446STATIC int
1447xlog_recover_add_to_cont_trans(
Dave Chinner9abbc532010-04-13 15:06:46 +10001448 struct log *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 xlog_recover_t *trans,
1450 xfs_caddr_t dp,
1451 int len)
1452{
1453 xlog_recover_item_t *item;
1454 xfs_caddr_t ptr, old_ptr;
1455 int old_len;
1456
Dave Chinnerf0a76952010-01-11 11:49:57 +00001457 if (list_empty(&trans->r_itemq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 /* finish copying rest of trans header */
1459 xlog_recover_add_item(&trans->r_itemq);
1460 ptr = (xfs_caddr_t) &trans->r_theader +
1461 sizeof(xfs_trans_header_t) - len;
1462 memcpy(ptr, dp, len); /* d, s, l */
1463 return 0;
1464 }
Dave Chinnerf0a76952010-01-11 11:49:57 +00001465 /* take the tail entry */
1466 item = list_entry(trans->r_itemq.prev, xlog_recover_item_t, ri_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 old_ptr = item->ri_buf[item->ri_cnt-1].i_addr;
1469 old_len = item->ri_buf[item->ri_cnt-1].i_len;
1470
Christoph Hellwig760dea62005-09-02 16:56:02 +10001471 ptr = kmem_realloc(old_ptr, len+old_len, old_len, 0u);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 memcpy(&ptr[old_len], dp, len); /* d, s, l */
1473 item->ri_buf[item->ri_cnt-1].i_len += len;
1474 item->ri_buf[item->ri_cnt-1].i_addr = ptr;
Dave Chinner9abbc532010-04-13 15:06:46 +10001475 trace_xfs_log_recover_item_add_cont(log, trans, item, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return 0;
1477}
1478
1479/*
1480 * The next region to add is the start of a new region. It could be
1481 * a whole region or it could be the first part of a new region. Because
1482 * of this, the assumption here is that the type and size fields of all
1483 * format structures fit into the first 32 bits of the structure.
1484 *
1485 * This works because all regions must be 32 bit aligned. Therefore, we
1486 * either have both fields or we have neither field. In the case we have
1487 * neither field, the data part of the region is zero length. We only have
1488 * a log_op_header and can throw away the header since a new one will appear
1489 * later. If we have at least 4 bytes, then we can determine how many regions
1490 * will appear in the current log item.
1491 */
1492STATIC int
1493xlog_recover_add_to_trans(
Dave Chinner9abbc532010-04-13 15:06:46 +10001494 struct log *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 xlog_recover_t *trans,
1496 xfs_caddr_t dp,
1497 int len)
1498{
1499 xfs_inode_log_format_t *in_f; /* any will do */
1500 xlog_recover_item_t *item;
1501 xfs_caddr_t ptr;
1502
1503 if (!len)
1504 return 0;
Dave Chinnerf0a76952010-01-11 11:49:57 +00001505 if (list_empty(&trans->r_itemq)) {
David Chinner5a792c42008-10-30 17:40:09 +11001506 /* we need to catch log corruptions here */
1507 if (*(uint *)dp != XFS_TRANS_HEADER_MAGIC) {
1508 xlog_warn("XFS: xlog_recover_add_to_trans: "
1509 "bad header magic number");
1510 ASSERT(0);
1511 return XFS_ERROR(EIO);
1512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 if (len == sizeof(xfs_trans_header_t))
1514 xlog_recover_add_item(&trans->r_itemq);
1515 memcpy(&trans->r_theader, dp, len); /* d, s, l */
1516 return 0;
1517 }
1518
1519 ptr = kmem_alloc(len, KM_SLEEP);
1520 memcpy(ptr, dp, len);
1521 in_f = (xfs_inode_log_format_t *)ptr;
1522
Dave Chinnerf0a76952010-01-11 11:49:57 +00001523 /* take the tail entry */
1524 item = list_entry(trans->r_itemq.prev, xlog_recover_item_t, ri_list);
1525 if (item->ri_total != 0 &&
1526 item->ri_total == item->ri_cnt) {
1527 /* tail item is in use, get a new one */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 xlog_recover_add_item(&trans->r_itemq);
Dave Chinnerf0a76952010-01-11 11:49:57 +00001529 item = list_entry(trans->r_itemq.prev,
1530 xlog_recover_item_t, ri_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533 if (item->ri_total == 0) { /* first region to be added */
Christoph Hellwige8fa6b42009-03-03 14:48:36 -05001534 if (in_f->ilf_size == 0 ||
1535 in_f->ilf_size > XLOG_MAX_REGIONS_IN_ITEM) {
1536 xlog_warn(
1537 "XFS: bad number of regions (%d) in inode log format",
1538 in_f->ilf_size);
1539 ASSERT(0);
1540 return XFS_ERROR(EIO);
1541 }
1542
1543 item->ri_total = in_f->ilf_size;
1544 item->ri_buf =
1545 kmem_zalloc(item->ri_total * sizeof(xfs_log_iovec_t),
1546 KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 }
1548 ASSERT(item->ri_total > item->ri_cnt);
1549 /* Description region is ri_buf[0] */
1550 item->ri_buf[item->ri_cnt].i_addr = ptr;
1551 item->ri_buf[item->ri_cnt].i_len = len;
1552 item->ri_cnt++;
Dave Chinner9abbc532010-04-13 15:06:46 +10001553 trace_xfs_log_recover_item_add(log, trans, item, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return 0;
1555}
1556
Dave Chinnerf0a76952010-01-11 11:49:57 +00001557/*
1558 * Sort the log items in the transaction. Cancelled buffers need
1559 * to be put first so they are processed before any items that might
1560 * modify the buffers. If they are cancelled, then the modifications
1561 * don't need to be replayed.
1562 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563STATIC int
1564xlog_recover_reorder_trans(
Dave Chinner9abbc532010-04-13 15:06:46 +10001565 struct log *log,
1566 xlog_recover_t *trans,
1567 int pass)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Dave Chinnerf0a76952010-01-11 11:49:57 +00001569 xlog_recover_item_t *item, *n;
1570 LIST_HEAD(sort_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Dave Chinnerf0a76952010-01-11 11:49:57 +00001572 list_splice_init(&trans->r_itemq, &sort_list);
1573 list_for_each_entry_safe(item, n, &sort_list, ri_list) {
1574 xfs_buf_log_format_t *buf_f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Dave Chinnerf0a76952010-01-11 11:49:57 +00001576 buf_f = (xfs_buf_log_format_t *)item->ri_buf[0].i_addr;
1577
1578 switch (ITEM_TYPE(item)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 case XFS_LI_BUF:
Dave Chinnerf0a76952010-01-11 11:49:57 +00001580 if (!(buf_f->blf_flags & XFS_BLI_CANCEL)) {
Dave Chinner9abbc532010-04-13 15:06:46 +10001581 trace_xfs_log_recover_item_reorder_head(log,
1582 trans, item, pass);
Dave Chinnerf0a76952010-01-11 11:49:57 +00001583 list_move(&item->ri_list, &trans->r_itemq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 break;
1585 }
1586 case XFS_LI_INODE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 case XFS_LI_DQUOT:
1588 case XFS_LI_QUOTAOFF:
1589 case XFS_LI_EFD:
1590 case XFS_LI_EFI:
Dave Chinner9abbc532010-04-13 15:06:46 +10001591 trace_xfs_log_recover_item_reorder_tail(log,
1592 trans, item, pass);
Dave Chinnerf0a76952010-01-11 11:49:57 +00001593 list_move_tail(&item->ri_list, &trans->r_itemq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 break;
1595 default:
1596 xlog_warn(
1597 "XFS: xlog_recover_reorder_trans: unrecognized type of log operation");
1598 ASSERT(0);
1599 return XFS_ERROR(EIO);
1600 }
Dave Chinnerf0a76952010-01-11 11:49:57 +00001601 }
1602 ASSERT(list_empty(&sort_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 return 0;
1604}
1605
1606/*
1607 * Build up the table of buf cancel records so that we don't replay
1608 * cancelled data in the second pass. For buffer records that are
1609 * not cancel records, there is nothing to do here so we just return.
1610 *
1611 * If we get a cancel record which is already in the table, this indicates
1612 * that the buffer was cancelled multiple times. In order to ensure
1613 * that during pass 2 we keep the record in the table until we reach its
1614 * last occurrence in the log, we keep a reference count in the cancel
1615 * record in the table to tell us how many times we expect to see this
1616 * record during the second pass.
1617 */
1618STATIC void
1619xlog_recover_do_buffer_pass1(
1620 xlog_t *log,
1621 xfs_buf_log_format_t *buf_f)
1622{
1623 xfs_buf_cancel_t *bcp;
1624 xfs_buf_cancel_t *nextp;
1625 xfs_buf_cancel_t *prevp;
1626 xfs_buf_cancel_t **bucket;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 xfs_daddr_t blkno = 0;
1628 uint len = 0;
1629 ushort flags = 0;
1630
1631 switch (buf_f->blf_type) {
1632 case XFS_LI_BUF:
1633 blkno = buf_f->blf_blkno;
1634 len = buf_f->blf_len;
1635 flags = buf_f->blf_flags;
1636 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
1638
1639 /*
1640 * If this isn't a cancel buffer item, then just return.
1641 */
Dave Chinner9abbc532010-04-13 15:06:46 +10001642 if (!(flags & XFS_BLI_CANCEL)) {
1643 trace_xfs_log_recover_buf_not_cancel(log, buf_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return;
Dave Chinner9abbc532010-04-13 15:06:46 +10001645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 /*
1648 * Insert an xfs_buf_cancel record into the hash table of
1649 * them. If there is already an identical record, bump
1650 * its reference count.
1651 */
1652 bucket = &log->l_buf_cancel_table[(__uint64_t)blkno %
1653 XLOG_BC_TABLE_SIZE];
1654 /*
1655 * If the hash bucket is empty then just insert a new record into
1656 * the bucket.
1657 */
1658 if (*bucket == NULL) {
1659 bcp = (xfs_buf_cancel_t *)kmem_alloc(sizeof(xfs_buf_cancel_t),
1660 KM_SLEEP);
1661 bcp->bc_blkno = blkno;
1662 bcp->bc_len = len;
1663 bcp->bc_refcount = 1;
1664 bcp->bc_next = NULL;
1665 *bucket = bcp;
1666 return;
1667 }
1668
1669 /*
1670 * The hash bucket is not empty, so search for duplicates of our
1671 * record. If we find one them just bump its refcount. If not
1672 * then add us at the end of the list.
1673 */
1674 prevp = NULL;
1675 nextp = *bucket;
1676 while (nextp != NULL) {
1677 if (nextp->bc_blkno == blkno && nextp->bc_len == len) {
1678 nextp->bc_refcount++;
Dave Chinner9abbc532010-04-13 15:06:46 +10001679 trace_xfs_log_recover_buf_cancel_ref_inc(log, buf_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 return;
1681 }
1682 prevp = nextp;
1683 nextp = nextp->bc_next;
1684 }
1685 ASSERT(prevp != NULL);
1686 bcp = (xfs_buf_cancel_t *)kmem_alloc(sizeof(xfs_buf_cancel_t),
1687 KM_SLEEP);
1688 bcp->bc_blkno = blkno;
1689 bcp->bc_len = len;
1690 bcp->bc_refcount = 1;
1691 bcp->bc_next = NULL;
1692 prevp->bc_next = bcp;
Dave Chinner9abbc532010-04-13 15:06:46 +10001693 trace_xfs_log_recover_buf_cancel_add(log, buf_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695
1696/*
1697 * Check to see whether the buffer being recovered has a corresponding
1698 * entry in the buffer cancel record table. If it does then return 1
1699 * so that it will be cancelled, otherwise return 0. If the buffer is
1700 * actually a buffer cancel item (XFS_BLI_CANCEL is set), then decrement
1701 * the refcount on the entry in the table and remove it from the table
1702 * if this is the last reference.
1703 *
1704 * We remove the cancel record from the table when we encounter its
1705 * last occurrence in the log so that if the same buffer is re-used
1706 * again after its last cancellation we actually replay the changes
1707 * made at that point.
1708 */
1709STATIC int
1710xlog_check_buffer_cancelled(
1711 xlog_t *log,
1712 xfs_daddr_t blkno,
1713 uint len,
1714 ushort flags)
1715{
1716 xfs_buf_cancel_t *bcp;
1717 xfs_buf_cancel_t *prevp;
1718 xfs_buf_cancel_t **bucket;
1719
1720 if (log->l_buf_cancel_table == NULL) {
1721 /*
1722 * There is nothing in the table built in pass one,
1723 * so this buffer must not be cancelled.
1724 */
1725 ASSERT(!(flags & XFS_BLI_CANCEL));
1726 return 0;
1727 }
1728
1729 bucket = &log->l_buf_cancel_table[(__uint64_t)blkno %
1730 XLOG_BC_TABLE_SIZE];
1731 bcp = *bucket;
1732 if (bcp == NULL) {
1733 /*
1734 * There is no corresponding entry in the table built
1735 * in pass one, so this buffer has not been cancelled.
1736 */
1737 ASSERT(!(flags & XFS_BLI_CANCEL));
1738 return 0;
1739 }
1740
1741 /*
1742 * Search for an entry in the buffer cancel table that
1743 * matches our buffer.
1744 */
1745 prevp = NULL;
1746 while (bcp != NULL) {
1747 if (bcp->bc_blkno == blkno && bcp->bc_len == len) {
1748 /*
1749 * We've go a match, so return 1 so that the
1750 * recovery of this buffer is cancelled.
1751 * If this buffer is actually a buffer cancel
1752 * log item, then decrement the refcount on the
1753 * one in the table and remove it if this is the
1754 * last reference.
1755 */
1756 if (flags & XFS_BLI_CANCEL) {
1757 bcp->bc_refcount--;
1758 if (bcp->bc_refcount == 0) {
1759 if (prevp == NULL) {
1760 *bucket = bcp->bc_next;
1761 } else {
1762 prevp->bc_next = bcp->bc_next;
1763 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001764 kmem_free(bcp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
1766 }
1767 return 1;
1768 }
1769 prevp = bcp;
1770 bcp = bcp->bc_next;
1771 }
1772 /*
1773 * We didn't find a corresponding entry in the table, so
1774 * return 0 so that the buffer is NOT cancelled.
1775 */
1776 ASSERT(!(flags & XFS_BLI_CANCEL));
1777 return 0;
1778}
1779
1780STATIC int
1781xlog_recover_do_buffer_pass2(
1782 xlog_t *log,
1783 xfs_buf_log_format_t *buf_f)
1784{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 xfs_daddr_t blkno = 0;
1786 ushort flags = 0;
1787 uint len = 0;
1788
1789 switch (buf_f->blf_type) {
1790 case XFS_LI_BUF:
1791 blkno = buf_f->blf_blkno;
1792 flags = buf_f->blf_flags;
1793 len = buf_f->blf_len;
1794 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
1796
1797 return xlog_check_buffer_cancelled(log, blkno, len, flags);
1798}
1799
1800/*
1801 * Perform recovery for a buffer full of inodes. In these buffers,
1802 * the only data which should be recovered is that which corresponds
1803 * to the di_next_unlinked pointers in the on disk inode structures.
1804 * The rest of the data for the inodes is always logged through the
1805 * inodes themselves rather than the inode buffer and is recovered
1806 * in xlog_recover_do_inode_trans().
1807 *
1808 * The only time when buffers full of inodes are fully recovered is
1809 * when the buffer is full of newly allocated inodes. In this case
1810 * the buffer will not be marked as an inode buffer and so will be
1811 * sent to xlog_recover_do_reg_buffer() below during recovery.
1812 */
1813STATIC int
1814xlog_recover_do_inode_buffer(
1815 xfs_mount_t *mp,
1816 xlog_recover_item_t *item,
1817 xfs_buf_t *bp,
1818 xfs_buf_log_format_t *buf_f)
1819{
1820 int i;
1821 int item_index;
1822 int bit;
1823 int nbits;
1824 int reg_buf_offset;
1825 int reg_buf_bytes;
1826 int next_unlinked_offset;
1827 int inodes_per_buf;
1828 xfs_agino_t *logged_nextp;
1829 xfs_agino_t *buffer_nextp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 unsigned int *data_map = NULL;
1831 unsigned int map_size = 0;
1832
Dave Chinner9abbc532010-04-13 15:06:46 +10001833 trace_xfs_log_recover_buf_inode_buf(mp->m_log, buf_f);
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 switch (buf_f->blf_type) {
1836 case XFS_LI_BUF:
1837 data_map = buf_f->blf_data_map;
1838 map_size = buf_f->blf_map_size;
1839 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 }
1841 /*
1842 * Set the variables corresponding to the current region to
1843 * 0 so that we'll initialize them on the first pass through
1844 * the loop.
1845 */
1846 reg_buf_offset = 0;
1847 reg_buf_bytes = 0;
1848 bit = 0;
1849 nbits = 0;
1850 item_index = 0;
1851 inodes_per_buf = XFS_BUF_COUNT(bp) >> mp->m_sb.sb_inodelog;
1852 for (i = 0; i < inodes_per_buf; i++) {
1853 next_unlinked_offset = (i * mp->m_sb.sb_inodesize) +
1854 offsetof(xfs_dinode_t, di_next_unlinked);
1855
1856 while (next_unlinked_offset >=
1857 (reg_buf_offset + reg_buf_bytes)) {
1858 /*
1859 * The next di_next_unlinked field is beyond
1860 * the current logged region. Find the next
1861 * logged region that contains or is beyond
1862 * the current di_next_unlinked field.
1863 */
1864 bit += nbits;
1865 bit = xfs_next_bit(data_map, map_size, bit);
1866
1867 /*
1868 * If there are no more logged regions in the
1869 * buffer, then we're done.
1870 */
1871 if (bit == -1) {
1872 return 0;
1873 }
1874
1875 nbits = xfs_contig_bits(data_map, map_size,
1876 bit);
1877 ASSERT(nbits > 0);
1878 reg_buf_offset = bit << XFS_BLI_SHIFT;
1879 reg_buf_bytes = nbits << XFS_BLI_SHIFT;
1880 item_index++;
1881 }
1882
1883 /*
1884 * If the current logged region starts after the current
1885 * di_next_unlinked field, then move on to the next
1886 * di_next_unlinked field.
1887 */
1888 if (next_unlinked_offset < reg_buf_offset) {
1889 continue;
1890 }
1891
1892 ASSERT(item->ri_buf[item_index].i_addr != NULL);
1893 ASSERT((item->ri_buf[item_index].i_len % XFS_BLI_CHUNK) == 0);
1894 ASSERT((reg_buf_offset + reg_buf_bytes) <= XFS_BUF_COUNT(bp));
1895
1896 /*
1897 * The current logged region contains a copy of the
1898 * current di_next_unlinked field. Extract its value
1899 * and copy it to the buffer copy.
1900 */
1901 logged_nextp = (xfs_agino_t *)
1902 ((char *)(item->ri_buf[item_index].i_addr) +
1903 (next_unlinked_offset - reg_buf_offset));
1904 if (unlikely(*logged_nextp == 0)) {
1905 xfs_fs_cmn_err(CE_ALERT, mp,
1906 "bad inode buffer log record (ptr = 0x%p, bp = 0x%p). XFS trying to replay bad (0) inode di_next_unlinked field",
1907 item, bp);
1908 XFS_ERROR_REPORT("xlog_recover_do_inode_buf",
1909 XFS_ERRLEVEL_LOW, mp);
1910 return XFS_ERROR(EFSCORRUPTED);
1911 }
1912
1913 buffer_nextp = (xfs_agino_t *)xfs_buf_offset(bp,
1914 next_unlinked_offset);
Tim Shimmin87c199c2006-06-09 14:56:16 +10001915 *buffer_nextp = *logged_nextp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 }
1917
1918 return 0;
1919}
1920
1921/*
1922 * Perform a 'normal' buffer recovery. Each logged region of the
1923 * buffer should be copied over the corresponding region in the
1924 * given buffer. The bitmap in the buf log format structure indicates
1925 * where to place the logged data.
1926 */
1927/*ARGSUSED*/
1928STATIC void
1929xlog_recover_do_reg_buffer(
Dave Chinner9abbc532010-04-13 15:06:46 +10001930 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 xlog_recover_item_t *item,
1932 xfs_buf_t *bp,
1933 xfs_buf_log_format_t *buf_f)
1934{
1935 int i;
1936 int bit;
1937 int nbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 unsigned int *data_map = NULL;
1939 unsigned int map_size = 0;
1940 int error;
1941
Dave Chinner9abbc532010-04-13 15:06:46 +10001942 trace_xfs_log_recover_buf_reg_buf(mp->m_log, buf_f);
1943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 switch (buf_f->blf_type) {
1945 case XFS_LI_BUF:
1946 data_map = buf_f->blf_data_map;
1947 map_size = buf_f->blf_map_size;
1948 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 }
1950 bit = 0;
1951 i = 1; /* 0 is the buf format structure */
1952 while (1) {
1953 bit = xfs_next_bit(data_map, map_size, bit);
1954 if (bit == -1)
1955 break;
1956 nbits = xfs_contig_bits(data_map, map_size, bit);
1957 ASSERT(nbits > 0);
Christoph Hellwig4b809162007-08-16 15:37:36 +10001958 ASSERT(item->ri_buf[i].i_addr != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 ASSERT(item->ri_buf[i].i_len % XFS_BLI_CHUNK == 0);
1960 ASSERT(XFS_BUF_COUNT(bp) >=
1961 ((uint)bit << XFS_BLI_SHIFT)+(nbits<<XFS_BLI_SHIFT));
1962
1963 /*
1964 * Do a sanity check if this is a dquot buffer. Just checking
1965 * the first dquot in the buffer should do. XXXThis is
1966 * probably a good thing to do for other buf types also.
1967 */
1968 error = 0;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001969 if (buf_f->blf_flags &
1970 (XFS_BLI_UDQUOT_BUF|XFS_BLI_PDQUOT_BUF|XFS_BLI_GDQUOT_BUF)) {
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02001971 if (item->ri_buf[i].i_addr == NULL) {
1972 cmn_err(CE_ALERT,
1973 "XFS: NULL dquot in %s.", __func__);
1974 goto next;
1975 }
Jan Rekorajski8ec6dba2009-11-16 11:57:02 +00001976 if (item->ri_buf[i].i_len < sizeof(xfs_disk_dquot_t)) {
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02001977 cmn_err(CE_ALERT,
1978 "XFS: dquot too small (%d) in %s.",
1979 item->ri_buf[i].i_len, __func__);
1980 goto next;
1981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 error = xfs_qm_dqcheck((xfs_disk_dquot_t *)
1983 item->ri_buf[i].i_addr,
1984 -1, 0, XFS_QMOPT_DOWARN,
1985 "dquot_buf_recover");
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02001986 if (error)
1987 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 }
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02001989
1990 memcpy(xfs_buf_offset(bp,
1991 (uint)bit << XFS_BLI_SHIFT), /* dest */
1992 item->ri_buf[i].i_addr, /* source */
1993 nbits<<XFS_BLI_SHIFT); /* length */
1994 next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 i++;
1996 bit += nbits;
1997 }
1998
1999 /* Shouldn't be any more regions */
2000 ASSERT(i == item->ri_total);
2001}
2002
2003/*
2004 * Do some primitive error checking on ondisk dquot data structures.
2005 */
2006int
2007xfs_qm_dqcheck(
2008 xfs_disk_dquot_t *ddq,
2009 xfs_dqid_t id,
2010 uint type, /* used only when IO_dorepair is true */
2011 uint flags,
2012 char *str)
2013{
2014 xfs_dqblk_t *d = (xfs_dqblk_t *)ddq;
2015 int errs = 0;
2016
2017 /*
2018 * We can encounter an uninitialized dquot buffer for 2 reasons:
2019 * 1. If we crash while deleting the quotainode(s), and those blks got
2020 * used for user data. This is because we take the path of regular
2021 * file deletion; however, the size field of quotainodes is never
2022 * updated, so all the tricks that we play in itruncate_finish
2023 * don't quite matter.
2024 *
2025 * 2. We don't play the quota buffers when there's a quotaoff logitem.
2026 * But the allocation will be replayed so we'll end up with an
2027 * uninitialized quota block.
2028 *
2029 * This is all fine; things are still consistent, and we haven't lost
2030 * any quota information. Just don't complain about bad dquot blks.
2031 */
Christoph Hellwig1149d962005-11-02 15:01:12 +11002032 if (be16_to_cpu(ddq->d_magic) != XFS_DQUOT_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 if (flags & XFS_QMOPT_DOWARN)
2034 cmn_err(CE_ALERT,
2035 "%s : XFS dquot ID 0x%x, magic 0x%x != 0x%x",
Christoph Hellwig1149d962005-11-02 15:01:12 +11002036 str, id, be16_to_cpu(ddq->d_magic), XFS_DQUOT_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 errs++;
2038 }
Christoph Hellwig1149d962005-11-02 15:01:12 +11002039 if (ddq->d_version != XFS_DQUOT_VERSION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 if (flags & XFS_QMOPT_DOWARN)
2041 cmn_err(CE_ALERT,
2042 "%s : XFS dquot ID 0x%x, version 0x%x != 0x%x",
Christoph Hellwig1149d962005-11-02 15:01:12 +11002043 str, id, ddq->d_version, XFS_DQUOT_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 errs++;
2045 }
2046
Christoph Hellwig1149d962005-11-02 15:01:12 +11002047 if (ddq->d_flags != XFS_DQ_USER &&
2048 ddq->d_flags != XFS_DQ_PROJ &&
2049 ddq->d_flags != XFS_DQ_GROUP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 if (flags & XFS_QMOPT_DOWARN)
2051 cmn_err(CE_ALERT,
2052 "%s : XFS dquot ID 0x%x, unknown flags 0x%x",
Christoph Hellwig1149d962005-11-02 15:01:12 +11002053 str, id, ddq->d_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 errs++;
2055 }
2056
Christoph Hellwig1149d962005-11-02 15:01:12 +11002057 if (id != -1 && id != be32_to_cpu(ddq->d_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 if (flags & XFS_QMOPT_DOWARN)
2059 cmn_err(CE_ALERT,
2060 "%s : ondisk-dquot 0x%p, ID mismatch: "
2061 "0x%x expected, found id 0x%x",
Christoph Hellwig1149d962005-11-02 15:01:12 +11002062 str, ddq, id, be32_to_cpu(ddq->d_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 errs++;
2064 }
2065
2066 if (!errs && ddq->d_id) {
Christoph Hellwig1149d962005-11-02 15:01:12 +11002067 if (ddq->d_blk_softlimit &&
2068 be64_to_cpu(ddq->d_bcount) >=
2069 be64_to_cpu(ddq->d_blk_softlimit)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 if (!ddq->d_btimer) {
2071 if (flags & XFS_QMOPT_DOWARN)
2072 cmn_err(CE_ALERT,
2073 "%s : Dquot ID 0x%x (0x%p) "
2074 "BLK TIMER NOT STARTED",
Christoph Hellwig1149d962005-11-02 15:01:12 +11002075 str, (int)be32_to_cpu(ddq->d_id), ddq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 errs++;
2077 }
2078 }
Christoph Hellwig1149d962005-11-02 15:01:12 +11002079 if (ddq->d_ino_softlimit &&
2080 be64_to_cpu(ddq->d_icount) >=
2081 be64_to_cpu(ddq->d_ino_softlimit)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 if (!ddq->d_itimer) {
2083 if (flags & XFS_QMOPT_DOWARN)
2084 cmn_err(CE_ALERT,
2085 "%s : Dquot ID 0x%x (0x%p) "
2086 "INODE TIMER NOT STARTED",
Christoph Hellwig1149d962005-11-02 15:01:12 +11002087 str, (int)be32_to_cpu(ddq->d_id), ddq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 errs++;
2089 }
2090 }
Christoph Hellwig1149d962005-11-02 15:01:12 +11002091 if (ddq->d_rtb_softlimit &&
2092 be64_to_cpu(ddq->d_rtbcount) >=
2093 be64_to_cpu(ddq->d_rtb_softlimit)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 if (!ddq->d_rtbtimer) {
2095 if (flags & XFS_QMOPT_DOWARN)
2096 cmn_err(CE_ALERT,
2097 "%s : Dquot ID 0x%x (0x%p) "
2098 "RTBLK TIMER NOT STARTED",
Christoph Hellwig1149d962005-11-02 15:01:12 +11002099 str, (int)be32_to_cpu(ddq->d_id), ddq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 errs++;
2101 }
2102 }
2103 }
2104
2105 if (!errs || !(flags & XFS_QMOPT_DQREPAIR))
2106 return errs;
2107
2108 if (flags & XFS_QMOPT_DOWARN)
2109 cmn_err(CE_NOTE, "Re-initializing dquot ID 0x%x", id);
2110
2111 /*
2112 * Typically, a repair is only requested by quotacheck.
2113 */
2114 ASSERT(id != -1);
2115 ASSERT(flags & XFS_QMOPT_DQREPAIR);
2116 memset(d, 0, sizeof(xfs_dqblk_t));
Christoph Hellwig1149d962005-11-02 15:01:12 +11002117
2118 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
2119 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
2120 d->dd_diskdq.d_flags = type;
2121 d->dd_diskdq.d_id = cpu_to_be32(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
2123 return errs;
2124}
2125
2126/*
2127 * Perform a dquot buffer recovery.
2128 * Simple algorithm: if we have found a QUOTAOFF logitem of the same type
2129 * (ie. USR or GRP), then just toss this buffer away; don't recover it.
2130 * Else, treat it as a regular buffer and do recovery.
2131 */
2132STATIC void
2133xlog_recover_do_dquot_buffer(
2134 xfs_mount_t *mp,
2135 xlog_t *log,
2136 xlog_recover_item_t *item,
2137 xfs_buf_t *bp,
2138 xfs_buf_log_format_t *buf_f)
2139{
2140 uint type;
2141
Dave Chinner9abbc532010-04-13 15:06:46 +10002142 trace_xfs_log_recover_buf_dquot_buf(log, buf_f);
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 /*
2145 * Filesystems are required to send in quota flags at mount time.
2146 */
2147 if (mp->m_qflags == 0) {
2148 return;
2149 }
2150
2151 type = 0;
2152 if (buf_f->blf_flags & XFS_BLI_UDQUOT_BUF)
2153 type |= XFS_DQ_USER;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002154 if (buf_f->blf_flags & XFS_BLI_PDQUOT_BUF)
2155 type |= XFS_DQ_PROJ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 if (buf_f->blf_flags & XFS_BLI_GDQUOT_BUF)
2157 type |= XFS_DQ_GROUP;
2158 /*
2159 * This type of quotas was turned off, so ignore this buffer
2160 */
2161 if (log->l_quotaoffs_flag & type)
2162 return;
2163
Dave Chinner9abbc532010-04-13 15:06:46 +10002164 xlog_recover_do_reg_buffer(mp, item, bp, buf_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
2166
2167/*
2168 * This routine replays a modification made to a buffer at runtime.
2169 * There are actually two types of buffer, regular and inode, which
2170 * are handled differently. Inode buffers are handled differently
2171 * in that we only recover a specific set of data from them, namely
2172 * the inode di_next_unlinked fields. This is because all other inode
2173 * data is actually logged via inode records and any data we replay
2174 * here which overlaps that may be stale.
2175 *
2176 * When meta-data buffers are freed at run time we log a buffer item
2177 * with the XFS_BLI_CANCEL bit set to indicate that previous copies
2178 * of the buffer in the log should not be replayed at recovery time.
2179 * This is so that if the blocks covered by the buffer are reused for
2180 * file data before we crash we don't end up replaying old, freed
2181 * meta-data into a user's file.
2182 *
2183 * To handle the cancellation of buffer log items, we make two passes
2184 * over the log during recovery. During the first we build a table of
2185 * those buffers which have been cancelled, and during the second we
2186 * only replay those buffers which do not have corresponding cancel
2187 * records in the table. See xlog_recover_do_buffer_pass[1,2] above
2188 * for more details on the implementation of the table of cancel records.
2189 */
2190STATIC int
2191xlog_recover_do_buffer_trans(
2192 xlog_t *log,
2193 xlog_recover_item_t *item,
2194 int pass)
2195{
2196 xfs_buf_log_format_t *buf_f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 xfs_mount_t *mp;
2198 xfs_buf_t *bp;
2199 int error;
2200 int cancel;
2201 xfs_daddr_t blkno;
2202 int len;
2203 ushort flags;
Christoph Hellwig6ad112b2009-11-24 18:02:23 +00002204 uint buf_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
2206 buf_f = (xfs_buf_log_format_t *)item->ri_buf[0].i_addr;
2207
2208 if (pass == XLOG_RECOVER_PASS1) {
2209 /*
2210 * In this pass we're only looking for buf items
2211 * with the XFS_BLI_CANCEL bit set.
2212 */
2213 xlog_recover_do_buffer_pass1(log, buf_f);
2214 return 0;
2215 } else {
2216 /*
2217 * In this pass we want to recover all the buffers
2218 * which have not been cancelled and are not
2219 * cancellation buffers themselves. The routine
2220 * we call here will tell us whether or not to
2221 * continue with the replay of this buffer.
2222 */
2223 cancel = xlog_recover_do_buffer_pass2(log, buf_f);
2224 if (cancel) {
Dave Chinner9abbc532010-04-13 15:06:46 +10002225 trace_xfs_log_recover_buf_cancel(log, buf_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 return 0;
2227 }
2228 }
Dave Chinner9abbc532010-04-13 15:06:46 +10002229 trace_xfs_log_recover_buf_recover(log, buf_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 switch (buf_f->blf_type) {
2231 case XFS_LI_BUF:
2232 blkno = buf_f->blf_blkno;
2233 len = buf_f->blf_len;
2234 flags = buf_f->blf_flags;
2235 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 default:
2237 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
Nathan Scottfc1f8c12005-11-02 11:44:33 +11002238 "xfs_log_recover: unknown buffer type 0x%x, logdev %s",
2239 buf_f->blf_type, log->l_mp->m_logname ?
2240 log->l_mp->m_logname : "internal");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 XFS_ERROR_REPORT("xlog_recover_do_buffer_trans",
2242 XFS_ERRLEVEL_LOW, log->l_mp);
2243 return XFS_ERROR(EFSCORRUPTED);
2244 }
2245
2246 mp = log->l_mp;
Christoph Hellwig0cadda12010-01-19 09:56:44 +00002247 buf_flags = XBF_LOCK;
Christoph Hellwig6ad112b2009-11-24 18:02:23 +00002248 if (!(flags & XFS_BLI_INODE_BUF))
Christoph Hellwig0cadda12010-01-19 09:56:44 +00002249 buf_flags |= XBF_MAPPED;
Christoph Hellwig6ad112b2009-11-24 18:02:23 +00002250
2251 bp = xfs_buf_read(mp->m_ddev_targp, blkno, len, buf_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 if (XFS_BUF_ISERROR(bp)) {
2253 xfs_ioerror_alert("xlog_recover_do..(read#1)", log->l_mp,
2254 bp, blkno);
2255 error = XFS_BUF_GETERROR(bp);
2256 xfs_buf_relse(bp);
2257 return error;
2258 }
2259
2260 error = 0;
2261 if (flags & XFS_BLI_INODE_BUF) {
2262 error = xlog_recover_do_inode_buffer(mp, item, bp, buf_f);
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002263 } else if (flags &
2264 (XFS_BLI_UDQUOT_BUF|XFS_BLI_PDQUOT_BUF|XFS_BLI_GDQUOT_BUF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 xlog_recover_do_dquot_buffer(mp, log, item, bp, buf_f);
2266 } else {
Dave Chinner9abbc532010-04-13 15:06:46 +10002267 xlog_recover_do_reg_buffer(mp, item, bp, buf_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 }
2269 if (error)
2270 return XFS_ERROR(error);
2271
2272 /*
2273 * Perform delayed write on the buffer. Asynchronous writes will be
2274 * slower when taking into account all the buffers to be flushed.
2275 *
2276 * Also make sure that only inode buffers with good sizes stay in
2277 * the buffer cache. The kernel moves inodes in buffers of 1 block
2278 * or XFS_INODE_CLUSTER_SIZE bytes, whichever is bigger. The inode
2279 * buffers in the log can be a different size if the log was generated
2280 * by an older kernel using unclustered inode buffers or a newer kernel
2281 * running with a different inode cluster size. Regardless, if the
2282 * the inode buffer size isn't MAX(blocksize, XFS_INODE_CLUSTER_SIZE)
2283 * for *our* value of XFS_INODE_CLUSTER_SIZE, then we need to keep
2284 * the buffer out of the buffer cache so that the buffer won't
2285 * overlap with future reads of those inodes.
2286 */
2287 if (XFS_DINODE_MAGIC ==
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002288 be16_to_cpu(*((__be16 *)xfs_buf_offset(bp, 0))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 (XFS_BUF_COUNT(bp) != MAX(log->l_mp->m_sb.sb_blocksize,
2290 (__uint32_t)XFS_INODE_CLUSTER_SIZE(log->l_mp)))) {
2291 XFS_BUF_STALE(bp);
2292 error = xfs_bwrite(mp, bp);
2293 } else {
Christoph Hellwig15ac08a2008-12-09 04:47:30 -05002294 ASSERT(bp->b_mount == NULL || bp->b_mount == mp);
2295 bp->b_mount = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 XFS_BUF_SET_IODONE_FUNC(bp, xlog_recover_iodone);
2297 xfs_bdwrite(mp, bp);
2298 }
2299
2300 return (error);
2301}
2302
2303STATIC int
2304xlog_recover_do_inode_trans(
2305 xlog_t *log,
2306 xlog_recover_item_t *item,
2307 int pass)
2308{
2309 xfs_inode_log_format_t *in_f;
2310 xfs_mount_t *mp;
2311 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 xfs_dinode_t *dip;
2313 xfs_ino_t ino;
2314 int len;
2315 xfs_caddr_t src;
2316 xfs_caddr_t dest;
2317 int error;
2318 int attr_index;
2319 uint fields;
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002320 xfs_icdinode_t *dicp;
Tim Shimmin6d192a92006-06-09 14:55:38 +10002321 int need_free = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 if (pass == XLOG_RECOVER_PASS1) {
2324 return 0;
2325 }
2326
Tim Shimmin6d192a92006-06-09 14:55:38 +10002327 if (item->ri_buf[0].i_len == sizeof(xfs_inode_log_format_t)) {
2328 in_f = (xfs_inode_log_format_t *)item->ri_buf[0].i_addr;
2329 } else {
2330 in_f = (xfs_inode_log_format_t *)kmem_alloc(
2331 sizeof(xfs_inode_log_format_t), KM_SLEEP);
2332 need_free = 1;
2333 error = xfs_inode_item_format_convert(&item->ri_buf[0], in_f);
2334 if (error)
2335 goto error;
2336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 ino = in_f->ilf_ino;
2338 mp = log->l_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
2340 /*
2341 * Inode buffers can be freed, look out for it,
2342 * and do not replay the inode.
2343 */
Christoph Hellwiga1941892008-11-28 14:23:40 +11002344 if (xlog_check_buffer_cancelled(log, in_f->ilf_blkno,
2345 in_f->ilf_len, 0)) {
Tim Shimmin6d192a92006-06-09 14:55:38 +10002346 error = 0;
Dave Chinner9abbc532010-04-13 15:06:46 +10002347 trace_xfs_log_recover_inode_cancel(log, in_f);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002348 goto error;
2349 }
Dave Chinner9abbc532010-04-13 15:06:46 +10002350 trace_xfs_log_recover_inode_recover(log, in_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Christoph Hellwig6ad112b2009-11-24 18:02:23 +00002352 bp = xfs_buf_read(mp->m_ddev_targp, in_f->ilf_blkno, in_f->ilf_len,
Christoph Hellwig0cadda12010-01-19 09:56:44 +00002353 XBF_LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 if (XFS_BUF_ISERROR(bp)) {
2355 xfs_ioerror_alert("xlog_recover_do..(read#2)", mp,
Christoph Hellwiga1941892008-11-28 14:23:40 +11002356 bp, in_f->ilf_blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 error = XFS_BUF_GETERROR(bp);
2358 xfs_buf_relse(bp);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002359 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 }
2361 error = 0;
2362 ASSERT(in_f->ilf_fields & XFS_ILOG_CORE);
Christoph Hellwiga1941892008-11-28 14:23:40 +11002363 dip = (xfs_dinode_t *)xfs_buf_offset(bp, in_f->ilf_boffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
2365 /*
2366 * Make sure the place we're flushing out to really looks
2367 * like an inode!
2368 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002369 if (unlikely(be16_to_cpu(dip->di_magic) != XFS_DINODE_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 xfs_buf_relse(bp);
2371 xfs_fs_cmn_err(CE_ALERT, mp,
2372 "xfs_inode_recover: Bad inode magic number, dino ptr = 0x%p, dino bp = 0x%p, ino = %Ld",
2373 dip, bp, ino);
2374 XFS_ERROR_REPORT("xlog_recover_do_inode_trans(1)",
2375 XFS_ERRLEVEL_LOW, mp);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002376 error = EFSCORRUPTED;
2377 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002379 dicp = (xfs_icdinode_t *)(item->ri_buf[1].i_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 if (unlikely(dicp->di_magic != XFS_DINODE_MAGIC)) {
2381 xfs_buf_relse(bp);
2382 xfs_fs_cmn_err(CE_ALERT, mp,
2383 "xfs_inode_recover: Bad inode log record, rec ptr 0x%p, ino %Ld",
2384 item, ino);
2385 XFS_ERROR_REPORT("xlog_recover_do_inode_trans(2)",
2386 XFS_ERRLEVEL_LOW, mp);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002387 error = EFSCORRUPTED;
2388 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 }
2390
2391 /* Skip replay when the on disk inode is newer than the log one */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002392 if (dicp->di_flushiter < be16_to_cpu(dip->di_flushiter)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 /*
2394 * Deal with the wrap case, DI_MAX_FLUSH is less
2395 * than smaller numbers
2396 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002397 if (be16_to_cpu(dip->di_flushiter) == DI_MAX_FLUSH &&
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002398 dicp->di_flushiter < (DI_MAX_FLUSH >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 /* do nothing */
2400 } else {
2401 xfs_buf_relse(bp);
Dave Chinner9abbc532010-04-13 15:06:46 +10002402 trace_xfs_log_recover_inode_skip(log, in_f);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002403 error = 0;
2404 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 }
2406 }
2407 /* Take the opportunity to reset the flush iteration count */
2408 dicp->di_flushiter = 0;
2409
2410 if (unlikely((dicp->di_mode & S_IFMT) == S_IFREG)) {
2411 if ((dicp->di_format != XFS_DINODE_FMT_EXTENTS) &&
2412 (dicp->di_format != XFS_DINODE_FMT_BTREE)) {
2413 XFS_CORRUPTION_ERROR("xlog_recover_do_inode_trans(3)",
2414 XFS_ERRLEVEL_LOW, mp, dicp);
2415 xfs_buf_relse(bp);
2416 xfs_fs_cmn_err(CE_ALERT, mp,
2417 "xfs_inode_recover: Bad regular inode log record, rec ptr 0x%p, ino ptr = 0x%p, ino bp = 0x%p, ino %Ld",
2418 item, dip, bp, ino);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002419 error = EFSCORRUPTED;
2420 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 }
2422 } else if (unlikely((dicp->di_mode & S_IFMT) == S_IFDIR)) {
2423 if ((dicp->di_format != XFS_DINODE_FMT_EXTENTS) &&
2424 (dicp->di_format != XFS_DINODE_FMT_BTREE) &&
2425 (dicp->di_format != XFS_DINODE_FMT_LOCAL)) {
2426 XFS_CORRUPTION_ERROR("xlog_recover_do_inode_trans(4)",
2427 XFS_ERRLEVEL_LOW, mp, dicp);
2428 xfs_buf_relse(bp);
2429 xfs_fs_cmn_err(CE_ALERT, mp,
2430 "xfs_inode_recover: Bad dir inode log record, rec ptr 0x%p, ino ptr = 0x%p, ino bp = 0x%p, ino %Ld",
2431 item, dip, bp, ino);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002432 error = EFSCORRUPTED;
2433 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 }
2435 }
2436 if (unlikely(dicp->di_nextents + dicp->di_anextents > dicp->di_nblocks)){
2437 XFS_CORRUPTION_ERROR("xlog_recover_do_inode_trans(5)",
2438 XFS_ERRLEVEL_LOW, mp, dicp);
2439 xfs_buf_relse(bp);
2440 xfs_fs_cmn_err(CE_ALERT, mp,
2441 "xfs_inode_recover: Bad inode log record, rec ptr 0x%p, dino ptr 0x%p, dino bp 0x%p, ino %Ld, total extents = %d, nblocks = %Ld",
2442 item, dip, bp, ino,
2443 dicp->di_nextents + dicp->di_anextents,
2444 dicp->di_nblocks);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002445 error = EFSCORRUPTED;
2446 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 }
2448 if (unlikely(dicp->di_forkoff > mp->m_sb.sb_inodesize)) {
2449 XFS_CORRUPTION_ERROR("xlog_recover_do_inode_trans(6)",
2450 XFS_ERRLEVEL_LOW, mp, dicp);
2451 xfs_buf_relse(bp);
2452 xfs_fs_cmn_err(CE_ALERT, mp,
2453 "xfs_inode_recover: Bad inode log rec ptr 0x%p, dino ptr 0x%p, dino bp 0x%p, ino %Ld, forkoff 0x%x",
2454 item, dip, bp, ino, dicp->di_forkoff);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002455 error = EFSCORRUPTED;
2456 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 }
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002458 if (unlikely(item->ri_buf[1].i_len > sizeof(struct xfs_icdinode))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 XFS_CORRUPTION_ERROR("xlog_recover_do_inode_trans(7)",
2460 XFS_ERRLEVEL_LOW, mp, dicp);
2461 xfs_buf_relse(bp);
2462 xfs_fs_cmn_err(CE_ALERT, mp,
2463 "xfs_inode_recover: Bad inode log record length %d, rec ptr 0x%p",
2464 item->ri_buf[1].i_len, item);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002465 error = EFSCORRUPTED;
2466 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 }
2468
2469 /* The core is in in-core format */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002470 xfs_dinode_to_disk(dip, (xfs_icdinode_t *)item->ri_buf[1].i_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
2472 /* the rest is in on-disk format */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002473 if (item->ri_buf[1].i_len > sizeof(struct xfs_icdinode)) {
2474 memcpy((xfs_caddr_t) dip + sizeof(struct xfs_icdinode),
2475 item->ri_buf[1].i_addr + sizeof(struct xfs_icdinode),
2476 item->ri_buf[1].i_len - sizeof(struct xfs_icdinode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 }
2478
2479 fields = in_f->ilf_fields;
2480 switch (fields & (XFS_ILOG_DEV | XFS_ILOG_UUID)) {
2481 case XFS_ILOG_DEV:
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002482 xfs_dinode_put_rdev(dip, in_f->ilf_u.ilfu_rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 break;
2484 case XFS_ILOG_UUID:
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002485 memcpy(XFS_DFORK_DPTR(dip),
2486 &in_f->ilf_u.ilfu_uuid,
2487 sizeof(uuid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 break;
2489 }
2490
2491 if (in_f->ilf_size == 2)
2492 goto write_inode_buffer;
2493 len = item->ri_buf[2].i_len;
2494 src = item->ri_buf[2].i_addr;
2495 ASSERT(in_f->ilf_size <= 4);
2496 ASSERT((in_f->ilf_size == 3) || (fields & XFS_ILOG_AFORK));
2497 ASSERT(!(fields & XFS_ILOG_DFORK) ||
2498 (len == in_f->ilf_dsize));
2499
2500 switch (fields & XFS_ILOG_DFORK) {
2501 case XFS_ILOG_DDATA:
2502 case XFS_ILOG_DEXT:
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002503 memcpy(XFS_DFORK_DPTR(dip), src, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 break;
2505
2506 case XFS_ILOG_DBROOT:
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002507 xfs_bmbt_to_bmdr(mp, (struct xfs_btree_block *)src, len,
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002508 (xfs_bmdr_block_t *)XFS_DFORK_DPTR(dip),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 XFS_DFORK_DSIZE(dip, mp));
2510 break;
2511
2512 default:
2513 /*
2514 * There are no data fork flags set.
2515 */
2516 ASSERT((fields & XFS_ILOG_DFORK) == 0);
2517 break;
2518 }
2519
2520 /*
2521 * If we logged any attribute data, recover it. There may or
2522 * may not have been any other non-core data logged in this
2523 * transaction.
2524 */
2525 if (in_f->ilf_fields & XFS_ILOG_AFORK) {
2526 if (in_f->ilf_fields & XFS_ILOG_DFORK) {
2527 attr_index = 3;
2528 } else {
2529 attr_index = 2;
2530 }
2531 len = item->ri_buf[attr_index].i_len;
2532 src = item->ri_buf[attr_index].i_addr;
2533 ASSERT(len == in_f->ilf_asize);
2534
2535 switch (in_f->ilf_fields & XFS_ILOG_AFORK) {
2536 case XFS_ILOG_ADATA:
2537 case XFS_ILOG_AEXT:
2538 dest = XFS_DFORK_APTR(dip);
2539 ASSERT(len <= XFS_DFORK_ASIZE(dip, mp));
2540 memcpy(dest, src, len);
2541 break;
2542
2543 case XFS_ILOG_ABROOT:
2544 dest = XFS_DFORK_APTR(dip);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002545 xfs_bmbt_to_bmdr(mp, (struct xfs_btree_block *)src,
2546 len, (xfs_bmdr_block_t*)dest,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 XFS_DFORK_ASIZE(dip, mp));
2548 break;
2549
2550 default:
2551 xlog_warn("XFS: xlog_recover_do_inode_trans: Invalid flag");
2552 ASSERT(0);
2553 xfs_buf_relse(bp);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002554 error = EIO;
2555 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 }
2557 }
2558
2559write_inode_buffer:
Christoph Hellwigdd0bbad2009-03-16 08:19:59 +01002560 ASSERT(bp->b_mount == NULL || bp->b_mount == mp);
2561 bp->b_mount = mp;
2562 XFS_BUF_SET_IODONE_FUNC(bp, xlog_recover_iodone);
2563 xfs_bdwrite(mp, bp);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002564error:
2565 if (need_free)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002566 kmem_free(in_f);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002567 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568}
2569
2570/*
2571 * Recover QUOTAOFF records. We simply make a note of it in the xlog_t
2572 * structure, so that we know not to do any dquot item or dquot buffer recovery,
2573 * of that type.
2574 */
2575STATIC int
2576xlog_recover_do_quotaoff_trans(
2577 xlog_t *log,
2578 xlog_recover_item_t *item,
2579 int pass)
2580{
2581 xfs_qoff_logformat_t *qoff_f;
2582
2583 if (pass == XLOG_RECOVER_PASS2) {
2584 return (0);
2585 }
2586
2587 qoff_f = (xfs_qoff_logformat_t *)item->ri_buf[0].i_addr;
2588 ASSERT(qoff_f);
2589
2590 /*
2591 * The logitem format's flag tells us if this was user quotaoff,
Nathan Scott77a7cce2006-01-11 15:35:57 +11002592 * group/project quotaoff or both.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 */
2594 if (qoff_f->qf_flags & XFS_UQUOTA_ACCT)
2595 log->l_quotaoffs_flag |= XFS_DQ_USER;
Nathan Scott77a7cce2006-01-11 15:35:57 +11002596 if (qoff_f->qf_flags & XFS_PQUOTA_ACCT)
2597 log->l_quotaoffs_flag |= XFS_DQ_PROJ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 if (qoff_f->qf_flags & XFS_GQUOTA_ACCT)
2599 log->l_quotaoffs_flag |= XFS_DQ_GROUP;
2600
2601 return (0);
2602}
2603
2604/*
2605 * Recover a dquot record
2606 */
2607STATIC int
2608xlog_recover_do_dquot_trans(
2609 xlog_t *log,
2610 xlog_recover_item_t *item,
2611 int pass)
2612{
2613 xfs_mount_t *mp;
2614 xfs_buf_t *bp;
2615 struct xfs_disk_dquot *ddq, *recddq;
2616 int error;
2617 xfs_dq_logformat_t *dq_f;
2618 uint type;
2619
2620 if (pass == XLOG_RECOVER_PASS1) {
2621 return 0;
2622 }
2623 mp = log->l_mp;
2624
2625 /*
2626 * Filesystems are required to send in quota flags at mount time.
2627 */
2628 if (mp->m_qflags == 0)
2629 return (0);
2630
2631 recddq = (xfs_disk_dquot_t *)item->ri_buf[1].i_addr;
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02002632
2633 if (item->ri_buf[1].i_addr == NULL) {
2634 cmn_err(CE_ALERT,
2635 "XFS: NULL dquot in %s.", __func__);
2636 return XFS_ERROR(EIO);
2637 }
Jan Rekorajski8ec6dba2009-11-16 11:57:02 +00002638 if (item->ri_buf[1].i_len < sizeof(xfs_disk_dquot_t)) {
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02002639 cmn_err(CE_ALERT,
2640 "XFS: dquot too small (%d) in %s.",
2641 item->ri_buf[1].i_len, __func__);
2642 return XFS_ERROR(EIO);
2643 }
2644
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 /*
2646 * This type of quotas was turned off, so ignore this record.
2647 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002648 type = recddq->d_flags & (XFS_DQ_USER | XFS_DQ_PROJ | XFS_DQ_GROUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 ASSERT(type);
2650 if (log->l_quotaoffs_flag & type)
2651 return (0);
2652
2653 /*
2654 * At this point we know that quota was _not_ turned off.
2655 * Since the mount flags are not indicating to us otherwise, this
2656 * must mean that quota is on, and the dquot needs to be replayed.
2657 * Remember that we may not have fully recovered the superblock yet,
2658 * so we can't do the usual trick of looking at the SB quota bits.
2659 *
2660 * The other possibility, of course, is that the quota subsystem was
2661 * removed since the last mount - ENOSYS.
2662 */
2663 dq_f = (xfs_dq_logformat_t *)item->ri_buf[0].i_addr;
2664 ASSERT(dq_f);
2665 if ((error = xfs_qm_dqcheck(recddq,
2666 dq_f->qlf_id,
2667 0, XFS_QMOPT_DOWARN,
2668 "xlog_recover_do_dquot_trans (log copy)"))) {
2669 return XFS_ERROR(EIO);
2670 }
2671 ASSERT(dq_f->qlf_len == 1);
2672
2673 error = xfs_read_buf(mp, mp->m_ddev_targp,
2674 dq_f->qlf_blkno,
2675 XFS_FSB_TO_BB(mp, dq_f->qlf_len),
2676 0, &bp);
2677 if (error) {
2678 xfs_ioerror_alert("xlog_recover_do..(read#3)", mp,
2679 bp, dq_f->qlf_blkno);
2680 return error;
2681 }
2682 ASSERT(bp);
2683 ddq = (xfs_disk_dquot_t *)xfs_buf_offset(bp, dq_f->qlf_boffset);
2684
2685 /*
2686 * At least the magic num portion should be on disk because this
2687 * was among a chunk of dquots created earlier, and we did some
2688 * minimal initialization then.
2689 */
2690 if (xfs_qm_dqcheck(ddq, dq_f->qlf_id, 0, XFS_QMOPT_DOWARN,
2691 "xlog_recover_do_dquot_trans")) {
2692 xfs_buf_relse(bp);
2693 return XFS_ERROR(EIO);
2694 }
2695
2696 memcpy(ddq, recddq, item->ri_buf[1].i_len);
2697
2698 ASSERT(dq_f->qlf_size == 2);
Christoph Hellwig15ac08a2008-12-09 04:47:30 -05002699 ASSERT(bp->b_mount == NULL || bp->b_mount == mp);
2700 bp->b_mount = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 XFS_BUF_SET_IODONE_FUNC(bp, xlog_recover_iodone);
2702 xfs_bdwrite(mp, bp);
2703
2704 return (0);
2705}
2706
2707/*
2708 * This routine is called to create an in-core extent free intent
2709 * item from the efi format structure which was logged on disk.
2710 * It allocates an in-core efi, copies the extents from the format
2711 * structure into it, and adds the efi to the AIL with the given
2712 * LSN.
2713 */
Tim Shimmin6d192a92006-06-09 14:55:38 +10002714STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715xlog_recover_do_efi_trans(
2716 xlog_t *log,
2717 xlog_recover_item_t *item,
2718 xfs_lsn_t lsn,
2719 int pass)
2720{
Tim Shimmin6d192a92006-06-09 14:55:38 +10002721 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 xfs_mount_t *mp;
2723 xfs_efi_log_item_t *efip;
2724 xfs_efi_log_format_t *efi_formatp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
2726 if (pass == XLOG_RECOVER_PASS1) {
Tim Shimmin6d192a92006-06-09 14:55:38 +10002727 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 }
2729
2730 efi_formatp = (xfs_efi_log_format_t *)item->ri_buf[0].i_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
2732 mp = log->l_mp;
2733 efip = xfs_efi_init(mp, efi_formatp->efi_nextents);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002734 if ((error = xfs_efi_copy_format(&(item->ri_buf[0]),
2735 &(efip->efi_format)))) {
2736 xfs_efi_item_free(efip);
2737 return error;
2738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 efip->efi_next_extent = efi_formatp->efi_nextents;
2740 efip->efi_flags |= XFS_EFI_COMMITTED;
2741
David Chinnera9c21c12008-10-30 17:39:35 +11002742 spin_lock(&log->l_ailp->xa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 /*
David Chinner783a2f62008-10-30 17:39:58 +11002744 * xfs_trans_ail_update() drops the AIL lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 */
David Chinner783a2f62008-10-30 17:39:58 +11002746 xfs_trans_ail_update(log->l_ailp, (xfs_log_item_t *)efip, lsn);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002747 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748}
2749
2750
2751/*
2752 * This routine is called when an efd format structure is found in
2753 * a committed transaction in the log. It's purpose is to cancel
2754 * the corresponding efi if it was still in the log. To do this
2755 * it searches the AIL for the efi with an id equal to that in the
2756 * efd format structure. If we find it, we remove the efi from the
2757 * AIL and free it.
2758 */
2759STATIC void
2760xlog_recover_do_efd_trans(
2761 xlog_t *log,
2762 xlog_recover_item_t *item,
2763 int pass)
2764{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 xfs_efd_log_format_t *efd_formatp;
2766 xfs_efi_log_item_t *efip = NULL;
2767 xfs_log_item_t *lip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 __uint64_t efi_id;
David Chinner27d8d5f2008-10-30 17:38:39 +11002769 struct xfs_ail_cursor cur;
David Chinner783a2f62008-10-30 17:39:58 +11002770 struct xfs_ail *ailp = log->l_ailp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
2772 if (pass == XLOG_RECOVER_PASS1) {
2773 return;
2774 }
2775
2776 efd_formatp = (xfs_efd_log_format_t *)item->ri_buf[0].i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +10002777 ASSERT((item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_32_t) +
2778 ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_32_t)))) ||
2779 (item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_64_t) +
2780 ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_64_t)))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 efi_id = efd_formatp->efd_efi_id;
2782
2783 /*
2784 * Search for the efi with the id in the efd format structure
2785 * in the AIL.
2786 */
David Chinnera9c21c12008-10-30 17:39:35 +11002787 spin_lock(&ailp->xa_lock);
2788 lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 while (lip != NULL) {
2790 if (lip->li_type == XFS_LI_EFI) {
2791 efip = (xfs_efi_log_item_t *)lip;
2792 if (efip->efi_format.efi_id == efi_id) {
2793 /*
David Chinner783a2f62008-10-30 17:39:58 +11002794 * xfs_trans_ail_delete() drops the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 * AIL lock.
2796 */
David Chinner783a2f62008-10-30 17:39:58 +11002797 xfs_trans_ail_delete(ailp, lip);
David Chinner8ae2c0f2007-11-23 16:28:17 +11002798 xfs_efi_item_free(efip);
David Chinnera9c21c12008-10-30 17:39:35 +11002799 spin_lock(&ailp->xa_lock);
David Chinner27d8d5f2008-10-30 17:38:39 +11002800 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 }
2802 }
David Chinnera9c21c12008-10-30 17:39:35 +11002803 lip = xfs_trans_ail_cursor_next(ailp, &cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 }
David Chinnera9c21c12008-10-30 17:39:35 +11002805 xfs_trans_ail_cursor_done(ailp, &cur);
2806 spin_unlock(&ailp->xa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807}
2808
2809/*
2810 * Perform the transaction
2811 *
2812 * If the transaction modifies a buffer or inode, do it now. Otherwise,
2813 * EFIs and EFDs get queued up by adding entries into the AIL for them.
2814 */
2815STATIC int
2816xlog_recover_do_trans(
2817 xlog_t *log,
2818 xlog_recover_t *trans,
2819 int pass)
2820{
2821 int error = 0;
Dave Chinnerf0a76952010-01-11 11:49:57 +00002822 xlog_recover_item_t *item;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Dave Chinner9abbc532010-04-13 15:06:46 +10002824 error = xlog_recover_reorder_trans(log, trans, pass);
Christoph Hellwigff0205e2009-03-16 08:20:52 +01002825 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 return error;
Christoph Hellwigff0205e2009-03-16 08:20:52 +01002827
Dave Chinnerf0a76952010-01-11 11:49:57 +00002828 list_for_each_entry(item, &trans->r_itemq, ri_list) {
Dave Chinner9abbc532010-04-13 15:06:46 +10002829 trace_xfs_log_recover_item_recover(log, trans, item, pass);
Christoph Hellwigff0205e2009-03-16 08:20:52 +01002830 switch (ITEM_TYPE(item)) {
2831 case XFS_LI_BUF:
2832 error = xlog_recover_do_buffer_trans(log, item, pass);
2833 break;
2834 case XFS_LI_INODE:
2835 error = xlog_recover_do_inode_trans(log, item, pass);
2836 break;
2837 case XFS_LI_EFI:
2838 error = xlog_recover_do_efi_trans(log, item,
2839 trans->r_lsn, pass);
2840 break;
2841 case XFS_LI_EFD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 xlog_recover_do_efd_trans(log, item, pass);
Christoph Hellwigff0205e2009-03-16 08:20:52 +01002843 error = 0;
2844 break;
2845 case XFS_LI_DQUOT:
2846 error = xlog_recover_do_dquot_trans(log, item, pass);
2847 break;
2848 case XFS_LI_QUOTAOFF:
2849 error = xlog_recover_do_quotaoff_trans(log, item,
2850 pass);
2851 break;
2852 default:
2853 xlog_warn(
2854 "XFS: invalid item type (%d) xlog_recover_do_trans", ITEM_TYPE(item));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 ASSERT(0);
2856 error = XFS_ERROR(EIO);
2857 break;
2858 }
Christoph Hellwigff0205e2009-03-16 08:20:52 +01002859
2860 if (error)
2861 return error;
Dave Chinnerf0a76952010-01-11 11:49:57 +00002862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Christoph Hellwigff0205e2009-03-16 08:20:52 +01002864 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865}
2866
2867/*
2868 * Free up any resources allocated by the transaction
2869 *
2870 * Remember that EFIs, EFDs, and IUNLINKs are handled later.
2871 */
2872STATIC void
2873xlog_recover_free_trans(
2874 xlog_recover_t *trans)
2875{
Dave Chinnerf0a76952010-01-11 11:49:57 +00002876 xlog_recover_item_t *item, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 int i;
2878
Dave Chinnerf0a76952010-01-11 11:49:57 +00002879 list_for_each_entry_safe(item, n, &trans->r_itemq, ri_list) {
2880 /* Free the regions in the item. */
2881 list_del(&item->ri_list);
2882 for (i = 0; i < item->ri_cnt; i++)
2883 kmem_free(item->ri_buf[i].i_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 /* Free the item itself */
Dave Chinnerf0a76952010-01-11 11:49:57 +00002885 kmem_free(item->ri_buf);
2886 kmem_free(item);
2887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 /* Free the transaction recover structure */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002889 kmem_free(trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890}
2891
2892STATIC int
2893xlog_recover_commit_trans(
2894 xlog_t *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 xlog_recover_t *trans,
2896 int pass)
2897{
2898 int error;
2899
Dave Chinnerf0a76952010-01-11 11:49:57 +00002900 hlist_del(&trans->r_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 if ((error = xlog_recover_do_trans(log, trans, pass)))
2902 return error;
2903 xlog_recover_free_trans(trans); /* no error */
2904 return 0;
2905}
2906
2907STATIC int
2908xlog_recover_unmount_trans(
2909 xlog_recover_t *trans)
2910{
2911 /* Do nothing now */
2912 xlog_warn("XFS: xlog_recover_unmount_trans: Unmount LR");
2913 return 0;
2914}
2915
2916/*
2917 * There are two valid states of the r_state field. 0 indicates that the
2918 * transaction structure is in a normal state. We have either seen the
2919 * start of the transaction or the last operation we added was not a partial
2920 * operation. If the last operation we added to the transaction was a
2921 * partial operation, we need to mark r_state with XLOG_WAS_CONT_TRANS.
2922 *
2923 * NOTE: skip LRs with 0 data length.
2924 */
2925STATIC int
2926xlog_recover_process_data(
2927 xlog_t *log,
Dave Chinnerf0a76952010-01-11 11:49:57 +00002928 struct hlist_head rhash[],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 xlog_rec_header_t *rhead,
2930 xfs_caddr_t dp,
2931 int pass)
2932{
2933 xfs_caddr_t lp;
2934 int num_logops;
2935 xlog_op_header_t *ohead;
2936 xlog_recover_t *trans;
2937 xlog_tid_t tid;
2938 int error;
2939 unsigned long hash;
2940 uint flags;
2941
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002942 lp = dp + be32_to_cpu(rhead->h_len);
2943 num_logops = be32_to_cpu(rhead->h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
2945 /* check the log format matches our own - else we can't recover */
2946 if (xlog_header_check_recover(log->l_mp, rhead))
2947 return (XFS_ERROR(EIO));
2948
2949 while ((dp < lp) && num_logops) {
2950 ASSERT(dp + sizeof(xlog_op_header_t) <= lp);
2951 ohead = (xlog_op_header_t *)dp;
2952 dp += sizeof(xlog_op_header_t);
2953 if (ohead->oh_clientid != XFS_TRANSACTION &&
2954 ohead->oh_clientid != XFS_LOG) {
2955 xlog_warn(
2956 "XFS: xlog_recover_process_data: bad clientid");
2957 ASSERT(0);
2958 return (XFS_ERROR(EIO));
2959 }
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10002960 tid = be32_to_cpu(ohead->oh_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 hash = XLOG_RHASH(tid);
Dave Chinnerf0a76952010-01-11 11:49:57 +00002962 trans = xlog_recover_find_tid(&rhash[hash], tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 if (trans == NULL) { /* not found; add new tid */
2964 if (ohead->oh_flags & XLOG_START_TRANS)
2965 xlog_recover_new_tid(&rhash[hash], tid,
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002966 be64_to_cpu(rhead->h_lsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 } else {
Lachlan McIlroy9742bb92008-01-10 16:43:36 +11002968 if (dp + be32_to_cpu(ohead->oh_len) > lp) {
2969 xlog_warn(
2970 "XFS: xlog_recover_process_data: bad length");
2971 WARN_ON(1);
2972 return (XFS_ERROR(EIO));
2973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 flags = ohead->oh_flags & ~XLOG_END_TRANS;
2975 if (flags & XLOG_WAS_CONT_TRANS)
2976 flags &= ~XLOG_CONTINUE_TRANS;
2977 switch (flags) {
2978 case XLOG_COMMIT_TRANS:
2979 error = xlog_recover_commit_trans(log,
Dave Chinnerf0a76952010-01-11 11:49:57 +00002980 trans, pass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 break;
2982 case XLOG_UNMOUNT_TRANS:
2983 error = xlog_recover_unmount_trans(trans);
2984 break;
2985 case XLOG_WAS_CONT_TRANS:
Dave Chinner9abbc532010-04-13 15:06:46 +10002986 error = xlog_recover_add_to_cont_trans(log,
2987 trans, dp,
2988 be32_to_cpu(ohead->oh_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 break;
2990 case XLOG_START_TRANS:
2991 xlog_warn(
2992 "XFS: xlog_recover_process_data: bad transaction");
2993 ASSERT(0);
2994 error = XFS_ERROR(EIO);
2995 break;
2996 case 0:
2997 case XLOG_CONTINUE_TRANS:
Dave Chinner9abbc532010-04-13 15:06:46 +10002998 error = xlog_recover_add_to_trans(log, trans,
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10002999 dp, be32_to_cpu(ohead->oh_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 break;
3001 default:
3002 xlog_warn(
3003 "XFS: xlog_recover_process_data: bad flag");
3004 ASSERT(0);
3005 error = XFS_ERROR(EIO);
3006 break;
3007 }
3008 if (error)
3009 return error;
3010 }
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003011 dp += be32_to_cpu(ohead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 num_logops--;
3013 }
3014 return 0;
3015}
3016
3017/*
3018 * Process an extent free intent item that was recovered from
3019 * the log. We need to free the extents that it describes.
3020 */
David Chinner3c1e2bb2008-04-10 12:21:11 +10003021STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022xlog_recover_process_efi(
3023 xfs_mount_t *mp,
3024 xfs_efi_log_item_t *efip)
3025{
3026 xfs_efd_log_item_t *efdp;
3027 xfs_trans_t *tp;
3028 int i;
David Chinner3c1e2bb2008-04-10 12:21:11 +10003029 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 xfs_extent_t *extp;
3031 xfs_fsblock_t startblock_fsb;
3032
3033 ASSERT(!(efip->efi_flags & XFS_EFI_RECOVERED));
3034
3035 /*
3036 * First check the validity of the extents described by the
3037 * EFI. If any are bad, then assume that all are bad and
3038 * just toss the EFI.
3039 */
3040 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
3041 extp = &(efip->efi_format.efi_extents[i]);
3042 startblock_fsb = XFS_BB_TO_FSB(mp,
3043 XFS_FSB_TO_DADDR(mp, extp->ext_start));
3044 if ((startblock_fsb == 0) ||
3045 (extp->ext_len == 0) ||
3046 (startblock_fsb >= mp->m_sb.sb_dblocks) ||
3047 (extp->ext_len >= mp->m_sb.sb_agblocks)) {
3048 /*
3049 * This will pull the EFI from the AIL and
3050 * free the memory associated with it.
3051 */
3052 xfs_efi_release(efip, efip->efi_format.efi_nextents);
David Chinner3c1e2bb2008-04-10 12:21:11 +10003053 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 }
3055 }
3056
3057 tp = xfs_trans_alloc(mp, 0);
David Chinner3c1e2bb2008-04-10 12:21:11 +10003058 error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0, 0, 0);
David Chinnerfc6149d2008-04-10 12:21:53 +10003059 if (error)
3060 goto abort_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents);
3062
3063 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
3064 extp = &(efip->efi_format.efi_extents[i]);
David Chinnerfc6149d2008-04-10 12:21:53 +10003065 error = xfs_free_extent(tp, extp->ext_start, extp->ext_len);
3066 if (error)
3067 goto abort_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 xfs_trans_log_efd_extent(tp, efdp, extp->ext_start,
3069 extp->ext_len);
3070 }
3071
3072 efip->efi_flags |= XFS_EFI_RECOVERED;
David Chinnere5720ee2008-04-10 12:21:18 +10003073 error = xfs_trans_commit(tp, 0);
David Chinner3c1e2bb2008-04-10 12:21:11 +10003074 return error;
David Chinnerfc6149d2008-04-10 12:21:53 +10003075
3076abort_error:
3077 xfs_trans_cancel(tp, XFS_TRANS_ABORT);
3078 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079}
3080
3081/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 * When this is called, all of the EFIs which did not have
3083 * corresponding EFDs should be in the AIL. What we do now
3084 * is free the extents associated with each one.
3085 *
3086 * Since we process the EFIs in normal transactions, they
3087 * will be removed at some point after the commit. This prevents
3088 * us from just walking down the list processing each one.
3089 * We'll use a flag in the EFI to skip those that we've already
3090 * processed and use the AIL iteration mechanism's generation
3091 * count to try to speed this up at least a bit.
3092 *
3093 * When we start, we know that the EFIs are the only things in
3094 * the AIL. As we process them, however, other items are added
3095 * to the AIL. Since everything added to the AIL must come after
3096 * everything already in the AIL, we stop processing as soon as
3097 * we see something other than an EFI in the AIL.
3098 */
David Chinner3c1e2bb2008-04-10 12:21:11 +10003099STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100xlog_recover_process_efis(
3101 xlog_t *log)
3102{
3103 xfs_log_item_t *lip;
3104 xfs_efi_log_item_t *efip;
David Chinner3c1e2bb2008-04-10 12:21:11 +10003105 int error = 0;
David Chinner27d8d5f2008-10-30 17:38:39 +11003106 struct xfs_ail_cursor cur;
David Chinnera9c21c12008-10-30 17:39:35 +11003107 struct xfs_ail *ailp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
David Chinnera9c21c12008-10-30 17:39:35 +11003109 ailp = log->l_ailp;
3110 spin_lock(&ailp->xa_lock);
3111 lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 while (lip != NULL) {
3113 /*
3114 * We're done when we see something other than an EFI.
David Chinner27d8d5f2008-10-30 17:38:39 +11003115 * There should be no EFIs left in the AIL now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 */
3117 if (lip->li_type != XFS_LI_EFI) {
David Chinner27d8d5f2008-10-30 17:38:39 +11003118#ifdef DEBUG
David Chinnera9c21c12008-10-30 17:39:35 +11003119 for (; lip; lip = xfs_trans_ail_cursor_next(ailp, &cur))
David Chinner27d8d5f2008-10-30 17:38:39 +11003120 ASSERT(lip->li_type != XFS_LI_EFI);
3121#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 break;
3123 }
3124
3125 /*
3126 * Skip EFIs that we've already processed.
3127 */
3128 efip = (xfs_efi_log_item_t *)lip;
3129 if (efip->efi_flags & XFS_EFI_RECOVERED) {
David Chinnera9c21c12008-10-30 17:39:35 +11003130 lip = xfs_trans_ail_cursor_next(ailp, &cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 continue;
3132 }
3133
David Chinnera9c21c12008-10-30 17:39:35 +11003134 spin_unlock(&ailp->xa_lock);
3135 error = xlog_recover_process_efi(log->l_mp, efip);
3136 spin_lock(&ailp->xa_lock);
David Chinner27d8d5f2008-10-30 17:38:39 +11003137 if (error)
3138 goto out;
David Chinnera9c21c12008-10-30 17:39:35 +11003139 lip = xfs_trans_ail_cursor_next(ailp, &cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 }
David Chinner27d8d5f2008-10-30 17:38:39 +11003141out:
David Chinnera9c21c12008-10-30 17:39:35 +11003142 xfs_trans_ail_cursor_done(ailp, &cur);
3143 spin_unlock(&ailp->xa_lock);
David Chinner3c1e2bb2008-04-10 12:21:11 +10003144 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145}
3146
3147/*
3148 * This routine performs a transaction to null out a bad inode pointer
3149 * in an agi unlinked inode hash bucket.
3150 */
3151STATIC void
3152xlog_recover_clear_agi_bucket(
3153 xfs_mount_t *mp,
3154 xfs_agnumber_t agno,
3155 int bucket)
3156{
3157 xfs_trans_t *tp;
3158 xfs_agi_t *agi;
3159 xfs_buf_t *agibp;
3160 int offset;
3161 int error;
3162
3163 tp = xfs_trans_alloc(mp, XFS_TRANS_CLEAR_AGI_BUCKET);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11003164 error = xfs_trans_reserve(tp, 0, XFS_CLEAR_AGI_BUCKET_LOG_RES(mp),
3165 0, 0, 0);
David Chinnere5720ee2008-04-10 12:21:18 +10003166 if (error)
3167 goto out_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11003169 error = xfs_read_agi(mp, tp, agno, &agibp);
3170 if (error)
David Chinnere5720ee2008-04-10 12:21:18 +10003171 goto out_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11003173 agi = XFS_BUF_TO_AGI(agibp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11003174 agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 offset = offsetof(xfs_agi_t, agi_unlinked) +
3176 (sizeof(xfs_agino_t) * bucket);
3177 xfs_trans_log_buf(tp, agibp, offset,
3178 (offset + sizeof(xfs_agino_t) - 1));
3179
David Chinnere5720ee2008-04-10 12:21:18 +10003180 error = xfs_trans_commit(tp, 0);
3181 if (error)
3182 goto out_error;
3183 return;
3184
3185out_abort:
3186 xfs_trans_cancel(tp, XFS_TRANS_ABORT);
3187out_error:
3188 xfs_fs_cmn_err(CE_WARN, mp, "xlog_recover_clear_agi_bucket: "
3189 "failed to clear agi %d. Continuing.", agno);
3190 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191}
3192
Christoph Hellwig23fac502008-11-28 14:23:40 +11003193STATIC xfs_agino_t
3194xlog_recover_process_one_iunlink(
3195 struct xfs_mount *mp,
3196 xfs_agnumber_t agno,
3197 xfs_agino_t agino,
3198 int bucket)
3199{
3200 struct xfs_buf *ibp;
3201 struct xfs_dinode *dip;
3202 struct xfs_inode *ip;
3203 xfs_ino_t ino;
3204 int error;
3205
3206 ino = XFS_AGINO_TO_INO(mp, agno, agino);
3207 error = xfs_iget(mp, NULL, ino, 0, 0, &ip, 0);
3208 if (error)
3209 goto fail;
3210
3211 /*
3212 * Get the on disk inode to find the next inode in the bucket.
3213 */
Christoph Hellwig0cadda12010-01-19 09:56:44 +00003214 error = xfs_itobp(mp, NULL, ip, &dip, &ibp, XBF_LOCK);
Christoph Hellwig23fac502008-11-28 14:23:40 +11003215 if (error)
Christoph Hellwig0e446672008-11-28 14:23:42 +11003216 goto fail_iput;
Christoph Hellwig23fac502008-11-28 14:23:40 +11003217
Christoph Hellwig23fac502008-11-28 14:23:40 +11003218 ASSERT(ip->i_d.di_nlink == 0);
Christoph Hellwig0e446672008-11-28 14:23:42 +11003219 ASSERT(ip->i_d.di_mode != 0);
Christoph Hellwig23fac502008-11-28 14:23:40 +11003220
3221 /* setup for the next pass */
3222 agino = be32_to_cpu(dip->di_next_unlinked);
3223 xfs_buf_relse(ibp);
3224
3225 /*
3226 * Prevent any DMAPI event from being sent when the reference on
3227 * the inode is dropped.
3228 */
3229 ip->i_d.di_dmevmask = 0;
3230
Christoph Hellwig0e446672008-11-28 14:23:42 +11003231 IRELE(ip);
Christoph Hellwig23fac502008-11-28 14:23:40 +11003232 return agino;
3233
Christoph Hellwig0e446672008-11-28 14:23:42 +11003234 fail_iput:
3235 IRELE(ip);
Christoph Hellwig23fac502008-11-28 14:23:40 +11003236 fail:
3237 /*
3238 * We can't read in the inode this bucket points to, or this inode
3239 * is messed up. Just ditch this bucket of inodes. We will lose
3240 * some inodes and space, but at least we won't hang.
3241 *
3242 * Call xlog_recover_clear_agi_bucket() to perform a transaction to
3243 * clear the inode pointer in the bucket.
3244 */
3245 xlog_recover_clear_agi_bucket(mp, agno, bucket);
3246 return NULLAGINO;
3247}
3248
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249/*
3250 * xlog_iunlink_recover
3251 *
3252 * This is called during recovery to process any inodes which
3253 * we unlinked but not freed when the system crashed. These
3254 * inodes will be on the lists in the AGI blocks. What we do
3255 * here is scan all the AGIs and fully truncate and free any
3256 * inodes found on the lists. Each inode is removed from the
3257 * lists when it has been fully truncated and is freed. The
3258 * freeing of the inode and its removal from the list must be
3259 * atomic.
3260 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003261STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262xlog_recover_process_iunlinks(
3263 xlog_t *log)
3264{
3265 xfs_mount_t *mp;
3266 xfs_agnumber_t agno;
3267 xfs_agi_t *agi;
3268 xfs_buf_t *agibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 xfs_agino_t agino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 int bucket;
3271 int error;
3272 uint mp_dmevmask;
3273
3274 mp = log->l_mp;
3275
3276 /*
3277 * Prevent any DMAPI event from being sent while in this function.
3278 */
3279 mp_dmevmask = mp->m_dmevmask;
3280 mp->m_dmevmask = 0;
3281
3282 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
3283 /*
3284 * Find the agi for this ag.
3285 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11003286 error = xfs_read_agi(mp, NULL, agno, &agibp);
3287 if (error) {
3288 /*
3289 * AGI is b0rked. Don't process it.
3290 *
3291 * We should probably mark the filesystem as corrupt
3292 * after we've recovered all the ag's we can....
3293 */
3294 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 }
3296 agi = XFS_BUF_TO_AGI(agibp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
3298 for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11003299 agino = be32_to_cpu(agi->agi_unlinked[bucket]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 while (agino != NULLAGINO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 /*
3302 * Release the agi buffer so that it can
3303 * be acquired in the normal course of the
3304 * transaction to truncate and free the inode.
3305 */
3306 xfs_buf_relse(agibp);
3307
Christoph Hellwig23fac502008-11-28 14:23:40 +11003308 agino = xlog_recover_process_one_iunlink(mp,
3309 agno, agino, bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
3311 /*
3312 * Reacquire the agibuffer and continue around
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11003313 * the loop. This should never fail as we know
3314 * the buffer was good earlier on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11003316 error = xfs_read_agi(mp, NULL, agno, &agibp);
3317 ASSERT(error == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 agi = XFS_BUF_TO_AGI(agibp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 }
3320 }
3321
3322 /*
3323 * Release the buffer for the current agi so we can
3324 * go on to the next one.
3325 */
3326 xfs_buf_relse(agibp);
3327 }
3328
3329 mp->m_dmevmask = mp_dmevmask;
3330}
3331
3332
3333#ifdef DEBUG
3334STATIC void
3335xlog_pack_data_checksum(
3336 xlog_t *log,
3337 xlog_in_core_t *iclog,
3338 int size)
3339{
3340 int i;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003341 __be32 *up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 uint chksum = 0;
3343
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003344 up = (__be32 *)iclog->ic_datap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 /* divide length by 4 to get # words */
3346 for (i = 0; i < (size >> 2); i++) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003347 chksum ^= be32_to_cpu(*up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 up++;
3349 }
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003350 iclog->ic_header.h_chksum = cpu_to_be32(chksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351}
3352#else
3353#define xlog_pack_data_checksum(log, iclog, size)
3354#endif
3355
3356/*
3357 * Stamp cycle number in every block
3358 */
3359void
3360xlog_pack_data(
3361 xlog_t *log,
3362 xlog_in_core_t *iclog,
3363 int roundoff)
3364{
3365 int i, j, k;
3366 int size = iclog->ic_offset + roundoff;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003367 __be32 cycle_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 xfs_caddr_t dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369
3370 xlog_pack_data_checksum(log, iclog, size);
3371
3372 cycle_lsn = CYCLE_LSN_DISK(iclog->ic_header.h_lsn);
3373
3374 dp = iclog->ic_datap;
3375 for (i = 0; i < BTOBB(size) &&
3376 i < (XLOG_HEADER_CYCLE_SIZE / BBSIZE); i++) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003377 iclog->ic_header.h_cycle_data[i] = *(__be32 *)dp;
3378 *(__be32 *)dp = cycle_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 dp += BBSIZE;
3380 }
3381
Eric Sandeen62118702008-03-06 13:44:28 +11003382 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
Christoph Hellwigb28708d2008-11-28 14:23:38 +11003383 xlog_in_core_2_t *xhdr = iclog->ic_data;
3384
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 for ( ; i < BTOBB(size); i++) {
3386 j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3387 k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003388 xhdr[j].hic_xheader.xh_cycle_data[k] = *(__be32 *)dp;
3389 *(__be32 *)dp = cycle_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 dp += BBSIZE;
3391 }
3392
3393 for (i = 1; i < log->l_iclog_heads; i++) {
3394 xhdr[i].hic_xheader.xh_cycle = cycle_lsn;
3395 }
3396 }
3397}
3398
3399#if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
3400STATIC void
3401xlog_unpack_data_checksum(
3402 xlog_rec_header_t *rhead,
3403 xfs_caddr_t dp,
3404 xlog_t *log)
3405{
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003406 __be32 *up = (__be32 *)dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 uint chksum = 0;
3408 int i;
3409
3410 /* divide length by 4 to get # words */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003411 for (i=0; i < be32_to_cpu(rhead->h_len) >> 2; i++) {
3412 chksum ^= be32_to_cpu(*up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 up++;
3414 }
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003415 if (chksum != be32_to_cpu(rhead->h_chksum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 if (rhead->h_chksum ||
3417 ((log->l_flags & XLOG_CHKSUM_MISMATCH) == 0)) {
3418 cmn_err(CE_DEBUG,
Nathan Scottb6574522006-06-09 15:29:40 +10003419 "XFS: LogR chksum mismatch: was (0x%x) is (0x%x)\n",
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003420 be32_to_cpu(rhead->h_chksum), chksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 cmn_err(CE_DEBUG,
3422"XFS: Disregard message if filesystem was created with non-DEBUG kernel");
Eric Sandeen62118702008-03-06 13:44:28 +11003423 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 cmn_err(CE_DEBUG,
Nathan Scottb6574522006-06-09 15:29:40 +10003425 "XFS: LogR this is a LogV2 filesystem\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 }
3427 log->l_flags |= XLOG_CHKSUM_MISMATCH;
3428 }
3429 }
3430}
3431#else
3432#define xlog_unpack_data_checksum(rhead, dp, log)
3433#endif
3434
3435STATIC void
3436xlog_unpack_data(
3437 xlog_rec_header_t *rhead,
3438 xfs_caddr_t dp,
3439 xlog_t *log)
3440{
3441 int i, j, k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003443 for (i = 0; i < BTOBB(be32_to_cpu(rhead->h_len)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 i < (XLOG_HEADER_CYCLE_SIZE / BBSIZE); i++) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003445 *(__be32 *)dp = *(__be32 *)&rhead->h_cycle_data[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 dp += BBSIZE;
3447 }
3448
Eric Sandeen62118702008-03-06 13:44:28 +11003449 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
Christoph Hellwigb28708d2008-11-28 14:23:38 +11003450 xlog_in_core_2_t *xhdr = (xlog_in_core_2_t *)rhead;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003451 for ( ; i < BTOBB(be32_to_cpu(rhead->h_len)); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3453 k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003454 *(__be32 *)dp = xhdr[j].hic_xheader.xh_cycle_data[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 dp += BBSIZE;
3456 }
3457 }
3458
3459 xlog_unpack_data_checksum(rhead, dp, log);
3460}
3461
3462STATIC int
3463xlog_valid_rec_header(
3464 xlog_t *log,
3465 xlog_rec_header_t *rhead,
3466 xfs_daddr_t blkno)
3467{
3468 int hlen;
3469
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003470 if (unlikely(be32_to_cpu(rhead->h_magicno) != XLOG_HEADER_MAGIC_NUM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 XFS_ERROR_REPORT("xlog_valid_rec_header(1)",
3472 XFS_ERRLEVEL_LOW, log->l_mp);
3473 return XFS_ERROR(EFSCORRUPTED);
3474 }
3475 if (unlikely(
3476 (!rhead->h_version ||
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003477 (be32_to_cpu(rhead->h_version) & (~XLOG_VERSION_OKBITS))))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 xlog_warn("XFS: %s: unrecognised log version (%d).",
Harvey Harrison34a622b2008-04-10 12:19:21 +10003479 __func__, be32_to_cpu(rhead->h_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 return XFS_ERROR(EIO);
3481 }
3482
3483 /* LR body must have data or it wouldn't have been written */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003484 hlen = be32_to_cpu(rhead->h_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 if (unlikely( hlen <= 0 || hlen > INT_MAX )) {
3486 XFS_ERROR_REPORT("xlog_valid_rec_header(2)",
3487 XFS_ERRLEVEL_LOW, log->l_mp);
3488 return XFS_ERROR(EFSCORRUPTED);
3489 }
3490 if (unlikely( blkno > log->l_logBBsize || blkno > INT_MAX )) {
3491 XFS_ERROR_REPORT("xlog_valid_rec_header(3)",
3492 XFS_ERRLEVEL_LOW, log->l_mp);
3493 return XFS_ERROR(EFSCORRUPTED);
3494 }
3495 return 0;
3496}
3497
3498/*
3499 * Read the log from tail to head and process the log records found.
3500 * Handle the two cases where the tail and head are in the same cycle
3501 * and where the active portion of the log wraps around the end of
3502 * the physical log separately. The pass parameter is passed through
3503 * to the routines called to process the data and is not looked at
3504 * here.
3505 */
3506STATIC int
3507xlog_do_recovery_pass(
3508 xlog_t *log,
3509 xfs_daddr_t head_blk,
3510 xfs_daddr_t tail_blk,
3511 int pass)
3512{
3513 xlog_rec_header_t *rhead;
3514 xfs_daddr_t blk_no;
Andy Polingfc5bc4c2009-11-03 17:26:47 +00003515 xfs_caddr_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 xfs_buf_t *hbp, *dbp;
3517 int error = 0, h_size;
3518 int bblks, split_bblks;
3519 int hblks, split_hblks, wrapped_hblks;
Dave Chinnerf0a76952010-01-11 11:49:57 +00003520 struct hlist_head rhash[XLOG_RHASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521
3522 ASSERT(head_blk != tail_blk);
3523
3524 /*
3525 * Read the header of the tail block and get the iclog buffer size from
3526 * h_size. Use this to tell how many sectors make up the log header.
3527 */
Eric Sandeen62118702008-03-06 13:44:28 +11003528 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 /*
3530 * When using variable length iclogs, read first sector of
3531 * iclog header and extract the header size from it. Get a
3532 * new hbp that is the correct size.
3533 */
3534 hbp = xlog_get_bp(log, 1);
3535 if (!hbp)
3536 return ENOMEM;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003537
3538 error = xlog_bread(log, tail_blk, 1, hbp, &offset);
3539 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 goto bread_err1;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003541
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 rhead = (xlog_rec_header_t *)offset;
3543 error = xlog_valid_rec_header(log, rhead, tail_blk);
3544 if (error)
3545 goto bread_err1;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003546 h_size = be32_to_cpu(rhead->h_size);
3547 if ((be32_to_cpu(rhead->h_version) & XLOG_VERSION_2) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 (h_size > XLOG_HEADER_CYCLE_SIZE)) {
3549 hblks = h_size / XLOG_HEADER_CYCLE_SIZE;
3550 if (h_size % XLOG_HEADER_CYCLE_SIZE)
3551 hblks++;
3552 xlog_put_bp(hbp);
3553 hbp = xlog_get_bp(log, hblks);
3554 } else {
3555 hblks = 1;
3556 }
3557 } else {
3558 ASSERT(log->l_sectbb_log == 0);
3559 hblks = 1;
3560 hbp = xlog_get_bp(log, 1);
3561 h_size = XLOG_BIG_RECORD_BSIZE;
3562 }
3563
3564 if (!hbp)
3565 return ENOMEM;
3566 dbp = xlog_get_bp(log, BTOBB(h_size));
3567 if (!dbp) {
3568 xlog_put_bp(hbp);
3569 return ENOMEM;
3570 }
3571
3572 memset(rhash, 0, sizeof(rhash));
3573 if (tail_blk <= head_blk) {
3574 for (blk_no = tail_blk; blk_no < head_blk; ) {
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003575 error = xlog_bread(log, blk_no, hblks, hbp, &offset);
3576 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 goto bread_err2;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003578
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 rhead = (xlog_rec_header_t *)offset;
3580 error = xlog_valid_rec_header(log, rhead, blk_no);
3581 if (error)
3582 goto bread_err2;
3583
3584 /* blocks in data section */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003585 bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003586 error = xlog_bread(log, blk_no + hblks, bblks, dbp,
3587 &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 if (error)
3589 goto bread_err2;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003590
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 xlog_unpack_data(rhead, offset, log);
3592 if ((error = xlog_recover_process_data(log,
3593 rhash, rhead, offset, pass)))
3594 goto bread_err2;
3595 blk_no += bblks + hblks;
3596 }
3597 } else {
3598 /*
3599 * Perform recovery around the end of the physical log.
3600 * When the head is not on the same cycle number as the tail,
3601 * we can't do a sequential recovery as above.
3602 */
3603 blk_no = tail_blk;
3604 while (blk_no < log->l_logBBsize) {
3605 /*
3606 * Check for header wrapping around physical end-of-log
3607 */
Andy Polingfc5bc4c2009-11-03 17:26:47 +00003608 offset = XFS_BUF_PTR(hbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 split_hblks = 0;
3610 wrapped_hblks = 0;
3611 if (blk_no + hblks <= log->l_logBBsize) {
3612 /* Read header in one read */
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003613 error = xlog_bread(log, blk_no, hblks, hbp,
3614 &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 if (error)
3616 goto bread_err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 } else {
3618 /* This LR is split across physical log end */
3619 if (blk_no != log->l_logBBsize) {
3620 /* some data before physical log end */
3621 ASSERT(blk_no <= INT_MAX);
3622 split_hblks = log->l_logBBsize - (int)blk_no;
3623 ASSERT(split_hblks > 0);
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003624 error = xlog_bread(log, blk_no,
3625 split_hblks, hbp,
3626 &offset);
3627 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 goto bread_err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 }
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003630
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 /*
3632 * Note: this black magic still works with
3633 * large sector sizes (non-512) only because:
3634 * - we increased the buffer size originally
3635 * by 1 sector giving us enough extra space
3636 * for the second read;
3637 * - the log start is guaranteed to be sector
3638 * aligned;
3639 * - we read the log end (LR header start)
3640 * _first_, then the log start (LR header end)
3641 * - order is important.
3642 */
David Chinner234f56a2008-04-10 12:24:24 +10003643 wrapped_hblks = hblks - split_hblks;
David Chinner234f56a2008-04-10 12:24:24 +10003644 error = XFS_BUF_SET_PTR(hbp,
Andy Polingfc5bc4c2009-11-03 17:26:47 +00003645 offset + BBTOB(split_hblks),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 BBTOB(hblks - split_hblks));
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003647 if (error)
3648 goto bread_err2;
3649
3650 error = xlog_bread_noalign(log, 0,
3651 wrapped_hblks, hbp);
3652 if (error)
3653 goto bread_err2;
3654
Andy Polingfc5bc4c2009-11-03 17:26:47 +00003655 error = XFS_BUF_SET_PTR(hbp, offset,
David Chinner234f56a2008-04-10 12:24:24 +10003656 BBTOB(hblks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 if (error)
3658 goto bread_err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 }
3660 rhead = (xlog_rec_header_t *)offset;
3661 error = xlog_valid_rec_header(log, rhead,
3662 split_hblks ? blk_no : 0);
3663 if (error)
3664 goto bread_err2;
3665
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003666 bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 blk_no += hblks;
3668
3669 /* Read in data for log record */
3670 if (blk_no + bblks <= log->l_logBBsize) {
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003671 error = xlog_bread(log, blk_no, bblks, dbp,
3672 &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 if (error)
3674 goto bread_err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 } else {
3676 /* This log record is split across the
3677 * physical end of log */
Andy Polingfc5bc4c2009-11-03 17:26:47 +00003678 offset = XFS_BUF_PTR(dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 split_bblks = 0;
3680 if (blk_no != log->l_logBBsize) {
3681 /* some data is before the physical
3682 * end of log */
3683 ASSERT(!wrapped_hblks);
3684 ASSERT(blk_no <= INT_MAX);
3685 split_bblks =
3686 log->l_logBBsize - (int)blk_no;
3687 ASSERT(split_bblks > 0);
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003688 error = xlog_bread(log, blk_no,
3689 split_bblks, dbp,
3690 &offset);
3691 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 goto bread_err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 }
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003694
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 /*
3696 * Note: this black magic still works with
3697 * large sector sizes (non-512) only because:
3698 * - we increased the buffer size originally
3699 * by 1 sector giving us enough extra space
3700 * for the second read;
3701 * - the log start is guaranteed to be sector
3702 * aligned;
3703 * - we read the log end (LR header start)
3704 * _first_, then the log start (LR header end)
3705 * - order is important.
3706 */
David Chinner234f56a2008-04-10 12:24:24 +10003707 error = XFS_BUF_SET_PTR(dbp,
Andy Polingfc5bc4c2009-11-03 17:26:47 +00003708 offset + BBTOB(split_bblks),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 BBTOB(bblks - split_bblks));
David Chinner234f56a2008-04-10 12:24:24 +10003710 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 goto bread_err2;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003712
3713 error = xlog_bread_noalign(log, wrapped_hblks,
3714 bblks - split_bblks,
3715 dbp);
3716 if (error)
3717 goto bread_err2;
3718
Andy Polingfc5bc4c2009-11-03 17:26:47 +00003719 error = XFS_BUF_SET_PTR(dbp, offset, h_size);
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003720 if (error)
3721 goto bread_err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 }
3723 xlog_unpack_data(rhead, offset, log);
3724 if ((error = xlog_recover_process_data(log, rhash,
3725 rhead, offset, pass)))
3726 goto bread_err2;
3727 blk_no += bblks;
3728 }
3729
3730 ASSERT(blk_no >= log->l_logBBsize);
3731 blk_no -= log->l_logBBsize;
3732
3733 /* read first part of physical log */
3734 while (blk_no < head_blk) {
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003735 error = xlog_bread(log, blk_no, hblks, hbp, &offset);
3736 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 goto bread_err2;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003738
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 rhead = (xlog_rec_header_t *)offset;
3740 error = xlog_valid_rec_header(log, rhead, blk_no);
3741 if (error)
3742 goto bread_err2;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003743
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003744 bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003745 error = xlog_bread(log, blk_no+hblks, bblks, dbp,
3746 &offset);
3747 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 goto bread_err2;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003749
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 xlog_unpack_data(rhead, offset, log);
3751 if ((error = xlog_recover_process_data(log, rhash,
3752 rhead, offset, pass)))
3753 goto bread_err2;
3754 blk_no += bblks + hblks;
3755 }
3756 }
3757
3758 bread_err2:
3759 xlog_put_bp(dbp);
3760 bread_err1:
3761 xlog_put_bp(hbp);
3762 return error;
3763}
3764
3765/*
3766 * Do the recovery of the log. We actually do this in two phases.
3767 * The two passes are necessary in order to implement the function
3768 * of cancelling a record written into the log. The first pass
3769 * determines those things which have been cancelled, and the
3770 * second pass replays log items normally except for those which
3771 * have been cancelled. The handling of the replay and cancellations
3772 * takes place in the log item type specific routines.
3773 *
3774 * The table of items which have cancel records in the log is allocated
3775 * and freed at this level, since only here do we know when all of
3776 * the log recovery has been completed.
3777 */
3778STATIC int
3779xlog_do_log_recovery(
3780 xlog_t *log,
3781 xfs_daddr_t head_blk,
3782 xfs_daddr_t tail_blk)
3783{
3784 int error;
3785
3786 ASSERT(head_blk != tail_blk);
3787
3788 /*
3789 * First do a pass to find all of the cancelled buf log items.
3790 * Store them in the buf_cancel_table for use in the second pass.
3791 */
3792 log->l_buf_cancel_table =
3793 (xfs_buf_cancel_t **)kmem_zalloc(XLOG_BC_TABLE_SIZE *
3794 sizeof(xfs_buf_cancel_t*),
3795 KM_SLEEP);
3796 error = xlog_do_recovery_pass(log, head_blk, tail_blk,
3797 XLOG_RECOVER_PASS1);
3798 if (error != 0) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003799 kmem_free(log->l_buf_cancel_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 log->l_buf_cancel_table = NULL;
3801 return error;
3802 }
3803 /*
3804 * Then do a second pass to actually recover the items in the log.
3805 * When it is complete free the table of buf cancel items.
3806 */
3807 error = xlog_do_recovery_pass(log, head_blk, tail_blk,
3808 XLOG_RECOVER_PASS2);
3809#ifdef DEBUG
Tim Shimmin6d192a92006-06-09 14:55:38 +10003810 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 int i;
3812
3813 for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
3814 ASSERT(log->l_buf_cancel_table[i] == NULL);
3815 }
3816#endif /* DEBUG */
3817
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003818 kmem_free(log->l_buf_cancel_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 log->l_buf_cancel_table = NULL;
3820
3821 return error;
3822}
3823
3824/*
3825 * Do the actual recovery
3826 */
3827STATIC int
3828xlog_do_recover(
3829 xlog_t *log,
3830 xfs_daddr_t head_blk,
3831 xfs_daddr_t tail_blk)
3832{
3833 int error;
3834 xfs_buf_t *bp;
3835 xfs_sb_t *sbp;
3836
3837 /*
3838 * First replay the images in the log.
3839 */
3840 error = xlog_do_log_recovery(log, head_blk, tail_blk);
3841 if (error) {
3842 return error;
3843 }
3844
3845 XFS_bflush(log->l_mp->m_ddev_targp);
3846
3847 /*
3848 * If IO errors happened during recovery, bail out.
3849 */
3850 if (XFS_FORCED_SHUTDOWN(log->l_mp)) {
3851 return (EIO);
3852 }
3853
3854 /*
3855 * We now update the tail_lsn since much of the recovery has completed
3856 * and there may be space available to use. If there were no extent
3857 * or iunlinks, we can free up the entire log and set the tail_lsn to
3858 * be the last_sync_lsn. This was set in xlog_find_tail to be the
3859 * lsn of the last known good LR on disk. If there are extent frees
3860 * or iunlinks they will have some entries in the AIL; so we look at
3861 * the AIL to determine how to set the tail_lsn.
3862 */
3863 xlog_assign_tail_lsn(log->l_mp);
3864
3865 /*
3866 * Now that we've finished replaying all buffer and inode
3867 * updates, re-read in the superblock.
3868 */
3869 bp = xfs_getsb(log->l_mp, 0);
3870 XFS_BUF_UNDONE(bp);
Lachlan McIlroybebf9632007-10-15 13:18:02 +10003871 ASSERT(!(XFS_BUF_ISWRITE(bp)));
3872 ASSERT(!(XFS_BUF_ISDELAYWRITE(bp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 XFS_BUF_READ(bp);
Lachlan McIlroybebf9632007-10-15 13:18:02 +10003874 XFS_BUF_UNASYNC(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 xfsbdstrat(log->l_mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10003876 error = xfs_iowait(bp);
3877 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 xfs_ioerror_alert("xlog_do_recover",
3879 log->l_mp, bp, XFS_BUF_ADDR(bp));
3880 ASSERT(0);
3881 xfs_buf_relse(bp);
3882 return error;
3883 }
3884
3885 /* Convert superblock from on-disk format */
3886 sbp = &log->l_mp->m_sb;
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +10003887 xfs_sb_from_disk(sbp, XFS_BUF_TO_SBP(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 ASSERT(sbp->sb_magicnum == XFS_SB_MAGIC);
Eric Sandeen62118702008-03-06 13:44:28 +11003889 ASSERT(xfs_sb_good_version(sbp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 xfs_buf_relse(bp);
3891
Lachlan McIlroy5478eea2007-02-10 18:36:29 +11003892 /* We've re-read the superblock so re-initialize per-cpu counters */
3893 xfs_icsb_reinit_counters(log->l_mp);
3894
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 xlog_recover_check_summary(log);
3896
3897 /* Normal transactions can now occur */
3898 log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
3899 return 0;
3900}
3901
3902/*
3903 * Perform recovery and re-initialize some log variables in xlog_find_tail.
3904 *
3905 * Return error or zero.
3906 */
3907int
3908xlog_recover(
Eric Sandeen65be6052006-01-11 15:34:19 +11003909 xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910{
3911 xfs_daddr_t head_blk, tail_blk;
3912 int error;
3913
3914 /* find the tail of the log */
Eric Sandeen65be6052006-01-11 15:34:19 +11003915 if ((error = xlog_find_tail(log, &head_blk, &tail_blk)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 return error;
3917
3918 if (tail_blk != head_blk) {
3919 /* There used to be a comment here:
3920 *
3921 * disallow recovery on read-only mounts. note -- mount
3922 * checks for ENOSPC and turns it into an intelligent
3923 * error message.
3924 * ...but this is no longer true. Now, unless you specify
3925 * NORECOVERY (in which case this function would never be
3926 * called), we just go ahead and recover. We do this all
3927 * under the vfs layer, so we can get away with it unless
3928 * the device itself is read-only, in which case we fail.
3929 */
Utako Kusaka3a02ee12007-05-08 13:50:06 +10003930 if ((error = xfs_dev_is_read_only(log->l_mp, "recovery"))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 return error;
3932 }
3933
3934 cmn_err(CE_NOTE,
Nathan Scottfc1f8c12005-11-02 11:44:33 +11003935 "Starting XFS recovery on filesystem: %s (logdev: %s)",
3936 log->l_mp->m_fsname, log->l_mp->m_logname ?
3937 log->l_mp->m_logname : "internal");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938
3939 error = xlog_do_recover(log, head_blk, tail_blk);
3940 log->l_flags |= XLOG_RECOVERY_NEEDED;
3941 }
3942 return error;
3943}
3944
3945/*
3946 * In the first part of recovery we replay inodes and buffers and build
3947 * up the list of extent free items which need to be processed. Here
3948 * we process the extent free items and clean up the on disk unlinked
3949 * inode lists. This is separated from the first part of recovery so
3950 * that the root and real-time bitmap inodes can be read in from disk in
3951 * between the two stages. This is necessary so that we can free space
3952 * in the real-time portion of the file system.
3953 */
3954int
3955xlog_recover_finish(
Christoph Hellwig42490232008-08-13 16:49:32 +10003956 xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957{
3958 /*
3959 * Now we're ready to do the transactions needed for the
3960 * rest of recovery. Start with completing all the extent
3961 * free intent records and then process the unlinked inode
3962 * lists. At this point, we essentially run in normal mode
3963 * except that we're still performing recovery actions
3964 * rather than accepting new requests.
3965 */
3966 if (log->l_flags & XLOG_RECOVERY_NEEDED) {
David Chinner3c1e2bb2008-04-10 12:21:11 +10003967 int error;
3968 error = xlog_recover_process_efis(log);
3969 if (error) {
3970 cmn_err(CE_ALERT,
3971 "Failed to recover EFIs on filesystem: %s",
3972 log->l_mp->m_fsname);
3973 return error;
3974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 /*
3976 * Sync the log to get all the EFIs out of the AIL.
3977 * This isn't absolutely necessary, but it helps in
3978 * case the unlink transactions would have problems
3979 * pushing the EFIs out of the way.
3980 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003981 xfs_log_force(log->l_mp, XFS_LOG_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
Christoph Hellwig42490232008-08-13 16:49:32 +10003983 xlog_recover_process_iunlinks(log);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984
3985 xlog_recover_check_summary(log);
3986
3987 cmn_err(CE_NOTE,
Nathan Scottfc1f8c12005-11-02 11:44:33 +11003988 "Ending XFS recovery on filesystem: %s (logdev: %s)",
3989 log->l_mp->m_fsname, log->l_mp->m_logname ?
3990 log->l_mp->m_logname : "internal");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 log->l_flags &= ~XLOG_RECOVERY_NEEDED;
3992 } else {
3993 cmn_err(CE_DEBUG,
Nathan Scottb6574522006-06-09 15:29:40 +10003994 "!Ending clean XFS mount for filesystem: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 log->l_mp->m_fsname);
3996 }
3997 return 0;
3998}
3999
4000
4001#if defined(DEBUG)
4002/*
4003 * Read all of the agf and agi counters and check that they
4004 * are consistent with the superblock counters.
4005 */
4006void
4007xlog_recover_check_summary(
4008 xlog_t *log)
4009{
4010 xfs_mount_t *mp;
4011 xfs_agf_t *agfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 xfs_buf_t *agfbp;
4013 xfs_buf_t *agibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 xfs_buf_t *sbbp;
4015#ifdef XFS_LOUD_RECOVERY
4016 xfs_sb_t *sbp;
4017#endif
4018 xfs_agnumber_t agno;
4019 __uint64_t freeblks;
4020 __uint64_t itotal;
4021 __uint64_t ifree;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11004022 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
4024 mp = log->l_mp;
4025
4026 freeblks = 0LL;
4027 itotal = 0LL;
4028 ifree = 0LL;
4029 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
From: Christoph Hellwig48056212008-11-28 14:23:38 +11004030 error = xfs_read_agf(mp, NULL, agno, 0, &agfbp);
4031 if (error) {
4032 xfs_fs_cmn_err(CE_ALERT, mp,
4033 "xlog_recover_check_summary(agf)"
4034 "agf read failed agno %d error %d",
4035 agno, error);
4036 } else {
4037 agfp = XFS_BUF_TO_AGF(agfbp);
4038 freeblks += be32_to_cpu(agfp->agf_freeblks) +
4039 be32_to_cpu(agfp->agf_flcount);
4040 xfs_buf_relse(agfbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11004043 error = xfs_read_agi(mp, NULL, agno, &agibp);
4044 if (!error) {
4045 struct xfs_agi *agi = XFS_BUF_TO_AGI(agibp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11004047 itotal += be32_to_cpu(agi->agi_count);
4048 ifree += be32_to_cpu(agi->agi_freecount);
4049 xfs_buf_relse(agibp);
4050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 }
4052
4053 sbbp = xfs_getsb(mp, 0);
4054#ifdef XFS_LOUD_RECOVERY
4055 sbp = &mp->m_sb;
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +10004056 xfs_sb_from_disk(sbp, XFS_BUF_TO_SBP(sbbp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 cmn_err(CE_NOTE,
4058 "xlog_recover_check_summary: sb_icount %Lu itotal %Lu",
4059 sbp->sb_icount, itotal);
4060 cmn_err(CE_NOTE,
4061 "xlog_recover_check_summary: sb_ifree %Lu itotal %Lu",
4062 sbp->sb_ifree, ifree);
4063 cmn_err(CE_NOTE,
4064 "xlog_recover_check_summary: sb_fdblocks %Lu freeblks %Lu",
4065 sbp->sb_fdblocks, freeblks);
4066#if 0
4067 /*
4068 * This is turned off until I account for the allocation
4069 * btree blocks which live in free space.
4070 */
4071 ASSERT(sbp->sb_icount == itotal);
4072 ASSERT(sbp->sb_ifree == ifree);
4073 ASSERT(sbp->sb_fdblocks == freeblks);
4074#endif
4075#endif
4076 xfs_buf_relse(sbbp);
4077}
4078#endif /* DEBUG */