blob: 0e51bdd910a7a620a3b0b4c8ddd29e9085cfc71e [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
Eric Sandeen5d77c0d2009-11-19 15:52:00 +000066STATIC xfs_buf_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -070067xlog_get_bp(
68 xlog_t *log,
Dave Chinner32281492009-01-22 15:37:47 +110069 int nbblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Dave Chinner32281492009-01-22 15:37:47 +110071 if (nbblks <= 0 || nbblks > log->l_logBBsize) {
72 xlog_warn("XFS: Invalid block length (0x%x) given for buffer", nbblks);
73 XFS_ERROR_REPORT("xlog_get_bp(1)",
74 XFS_ERRLEVEL_HIGH, log->l_mp);
75 return NULL;
76 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 if (log->l_sectbb_log) {
Dave Chinner32281492009-01-22 15:37:47 +110079 if (nbblks > 1)
Alex Elder5c17f532010-04-13 15:22:48 +100080 nbblks += xlog_sectbb(log);
81 nbblks = round_up(nbblks, xlog_sectbb(log));
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
Dave Chinner32281492009-01-22 15:37:47 +110083 return xfs_buf_get_noaddr(BBTOB(nbblks), log->l_mp->m_logdev_targp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
Eric Sandeen5d77c0d2009-11-19 15:52:00 +000086STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -070087xlog_put_bp(
88 xfs_buf_t *bp)
89{
90 xfs_buf_free(bp);
91}
92
Christoph Hellwig076e6ac2009-03-16 08:24:13 +010093STATIC xfs_caddr_t
94xlog_align(
95 xlog_t *log,
96 xfs_daddr_t blk_no,
97 int nbblks,
98 xfs_buf_t *bp)
99{
100 xfs_caddr_t ptr;
101
102 if (!log->l_sectbb_log)
103 return XFS_BUF_PTR(bp);
104
105 ptr = XFS_BUF_PTR(bp) + BBTOB((int)blk_no & log->l_sectbb_mask);
106 ASSERT(XFS_BUF_SIZE(bp) >=
107 BBTOB(nbblks + (blk_no & log->l_sectbb_mask)));
108 return ptr;
109}
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112/*
113 * nbblks should be uint, but oh well. Just want to catch that 32-bit length.
114 */
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100115STATIC int
116xlog_bread_noalign(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 xlog_t *log,
118 xfs_daddr_t blk_no,
119 int nbblks,
120 xfs_buf_t *bp)
121{
122 int error;
123
Dave Chinner32281492009-01-22 15:37:47 +1100124 if (nbblks <= 0 || nbblks > log->l_logBBsize) {
125 xlog_warn("XFS: Invalid block length (0x%x) given for buffer", nbblks);
126 XFS_ERROR_REPORT("xlog_bread(1)",
127 XFS_ERRLEVEL_HIGH, log->l_mp);
128 return EFSCORRUPTED;
129 }
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (log->l_sectbb_log) {
Alex Elder5c17f532010-04-13 15:22:48 +1000132 blk_no = round_down(blk_no, xlog_sectbb(log));
133 nbblks = round_up(nbblks, xlog_sectbb(log));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135
136 ASSERT(nbblks > 0);
137 ASSERT(BBTOB(nbblks) <= XFS_BUF_SIZE(bp));
138 ASSERT(bp);
139
140 XFS_BUF_SET_ADDR(bp, log->l_logBBstart + blk_no);
141 XFS_BUF_READ(bp);
142 XFS_BUF_BUSY(bp);
143 XFS_BUF_SET_COUNT(bp, BBTOB(nbblks));
144 XFS_BUF_SET_TARGET(bp, log->l_mp->m_logdev_targp);
145
146 xfsbdstrat(log->l_mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +1000147 error = xfs_iowait(bp);
148 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 xfs_ioerror_alert("xlog_bread", log->l_mp,
150 bp, XFS_BUF_ADDR(bp));
151 return error;
152}
153
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100154STATIC int
155xlog_bread(
156 xlog_t *log,
157 xfs_daddr_t blk_no,
158 int nbblks,
159 xfs_buf_t *bp,
160 xfs_caddr_t *offset)
161{
162 int error;
163
164 error = xlog_bread_noalign(log, blk_no, nbblks, bp);
165 if (error)
166 return error;
167
168 *offset = xlog_align(log, blk_no, nbblks, bp);
169 return 0;
170}
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/*
173 * Write out the buffer at the given block for the given number of blocks.
174 * The buffer is kept locked across the write and is returned locked.
175 * This can only be used for synchronous log writes.
176 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000177STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178xlog_bwrite(
179 xlog_t *log,
180 xfs_daddr_t blk_no,
181 int nbblks,
182 xfs_buf_t *bp)
183{
184 int error;
185
Dave Chinner32281492009-01-22 15:37:47 +1100186 if (nbblks <= 0 || nbblks > log->l_logBBsize) {
187 xlog_warn("XFS: Invalid block length (0x%x) given for buffer", nbblks);
188 XFS_ERROR_REPORT("xlog_bwrite(1)",
189 XFS_ERRLEVEL_HIGH, log->l_mp);
190 return EFSCORRUPTED;
191 }
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (log->l_sectbb_log) {
Alex Elder5c17f532010-04-13 15:22:48 +1000194 blk_no = round_down(blk_no, xlog_sectbb(log));
195 nbblks = round_up(nbblks, xlog_sectbb(log));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197
198 ASSERT(nbblks > 0);
199 ASSERT(BBTOB(nbblks) <= XFS_BUF_SIZE(bp));
200
201 XFS_BUF_SET_ADDR(bp, log->l_logBBstart + blk_no);
202 XFS_BUF_ZEROFLAGS(bp);
203 XFS_BUF_BUSY(bp);
204 XFS_BUF_HOLD(bp);
205 XFS_BUF_PSEMA(bp, PRIBIO);
206 XFS_BUF_SET_COUNT(bp, BBTOB(nbblks));
207 XFS_BUF_SET_TARGET(bp, log->l_mp->m_logdev_targp);
208
209 if ((error = xfs_bwrite(log->l_mp, bp)))
210 xfs_ioerror_alert("xlog_bwrite", log->l_mp,
211 bp, XFS_BUF_ADDR(bp));
212 return error;
213}
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215#ifdef DEBUG
216/*
217 * dump debug superblock and log record information
218 */
219STATIC void
220xlog_header_check_dump(
221 xfs_mount_t *mp,
222 xlog_rec_header_t *head)
223{
Joe Perches03daa572009-12-14 18:01:10 -0800224 cmn_err(CE_DEBUG, "%s: SB : uuid = %pU, fmt = %d\n",
225 __func__, &mp->m_sb.sb_uuid, XLOG_FMT);
226 cmn_err(CE_DEBUG, " log : uuid = %pU, fmt = %d\n",
227 &head->h_fs_uuid, be32_to_cpu(head->h_fmt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229#else
230#define xlog_header_check_dump(mp, head)
231#endif
232
233/*
234 * check log record header for recovery
235 */
236STATIC int
237xlog_header_check_recover(
238 xfs_mount_t *mp,
239 xlog_rec_header_t *head)
240{
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000241 ASSERT(be32_to_cpu(head->h_magicno) == XLOG_HEADER_MAGIC_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 /*
244 * IRIX doesn't write the h_fmt field and leaves it zeroed
245 * (XLOG_FMT_UNKNOWN). This stops us from trying to recover
246 * a dirty log created in IRIX.
247 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000248 if (unlikely(be32_to_cpu(head->h_fmt) != XLOG_FMT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 xlog_warn(
250 "XFS: dirty log written in incompatible format - can't recover");
251 xlog_header_check_dump(mp, head);
252 XFS_ERROR_REPORT("xlog_header_check_recover(1)",
253 XFS_ERRLEVEL_HIGH, mp);
254 return XFS_ERROR(EFSCORRUPTED);
255 } else if (unlikely(!uuid_equal(&mp->m_sb.sb_uuid, &head->h_fs_uuid))) {
256 xlog_warn(
257 "XFS: dirty log entry has mismatched uuid - can't recover");
258 xlog_header_check_dump(mp, head);
259 XFS_ERROR_REPORT("xlog_header_check_recover(2)",
260 XFS_ERRLEVEL_HIGH, mp);
261 return XFS_ERROR(EFSCORRUPTED);
262 }
263 return 0;
264}
265
266/*
267 * read the head block of the log and check the header
268 */
269STATIC int
270xlog_header_check_mount(
271 xfs_mount_t *mp,
272 xlog_rec_header_t *head)
273{
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000274 ASSERT(be32_to_cpu(head->h_magicno) == XLOG_HEADER_MAGIC_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 if (uuid_is_nil(&head->h_fs_uuid)) {
277 /*
278 * IRIX doesn't write the h_fs_uuid or h_fmt fields. If
279 * h_fs_uuid is nil, we assume this log was last mounted
280 * by IRIX and continue.
281 */
282 xlog_warn("XFS: nil uuid in log - IRIX style log");
283 } else if (unlikely(!uuid_equal(&mp->m_sb.sb_uuid, &head->h_fs_uuid))) {
284 xlog_warn("XFS: log has mismatched uuid - can't recover");
285 xlog_header_check_dump(mp, head);
286 XFS_ERROR_REPORT("xlog_header_check_mount",
287 XFS_ERRLEVEL_HIGH, mp);
288 return XFS_ERROR(EFSCORRUPTED);
289 }
290 return 0;
291}
292
293STATIC void
294xlog_recover_iodone(
295 struct xfs_buf *bp)
296{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 if (XFS_BUF_GETERROR(bp)) {
298 /*
299 * We're not going to bother about retrying
300 * this during recovery. One strike!
301 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 xfs_ioerror_alert("xlog_recover_iodone",
Christoph Hellwig15ac08a2008-12-09 04:47:30 -0500303 bp->b_mount, bp, XFS_BUF_ADDR(bp));
304 xfs_force_shutdown(bp->b_mount, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
Christoph Hellwig15ac08a2008-12-09 04:47:30 -0500306 bp->b_mount = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 XFS_BUF_CLR_IODONE_FUNC(bp);
308 xfs_biodone(bp);
309}
310
311/*
312 * This routine finds (to an approximation) the first block in the physical
313 * log which contains the given cycle. It uses a binary search algorithm.
314 * Note that the algorithm can not be perfect because the disk will not
315 * necessarily be perfect.
316 */
David Chinnera8272ce2007-11-23 16:28:09 +1100317STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318xlog_find_cycle_start(
319 xlog_t *log,
320 xfs_buf_t *bp,
321 xfs_daddr_t first_blk,
322 xfs_daddr_t *last_blk,
323 uint cycle)
324{
325 xfs_caddr_t offset;
326 xfs_daddr_t mid_blk;
327 uint mid_cycle;
328 int error;
329
330 mid_blk = BLK_AVG(first_blk, *last_blk);
331 while (mid_blk != first_blk && mid_blk != *last_blk) {
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100332 error = xlog_bread(log, mid_blk, 1, bp, &offset);
333 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return error;
Christoph Hellwig03bea6f2007-10-12 10:58:05 +1000335 mid_cycle = xlog_get_cycle(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (mid_cycle == cycle) {
337 *last_blk = mid_blk;
338 /* last_half_cycle == mid_cycle */
339 } else {
340 first_blk = mid_blk;
341 /* first_half_cycle == mid_cycle */
342 }
343 mid_blk = BLK_AVG(first_blk, *last_blk);
344 }
345 ASSERT((mid_blk == first_blk && mid_blk+1 == *last_blk) ||
346 (mid_blk == *last_blk && mid_blk-1 == first_blk));
347
348 return 0;
349}
350
351/*
352 * Check that the range of blocks does not contain the cycle number
353 * given. The scan needs to occur from front to back and the ptr into the
354 * region must be updated since a later routine will need to perform another
355 * test. If the region is completely good, we end up returning the same
356 * last block number.
357 *
358 * Set blkno to -1 if we encounter no errors. This is an invalid block number
359 * since we don't ever expect logs to get this large.
360 */
361STATIC int
362xlog_find_verify_cycle(
363 xlog_t *log,
364 xfs_daddr_t start_blk,
365 int nbblks,
366 uint stop_on_cycle_no,
367 xfs_daddr_t *new_blk)
368{
369 xfs_daddr_t i, j;
370 uint cycle;
371 xfs_buf_t *bp;
372 xfs_daddr_t bufblks;
373 xfs_caddr_t buf = NULL;
374 int error = 0;
375
Alex Elder6881a222010-04-13 15:22:29 +1000376 /*
377 * Greedily allocate a buffer big enough to handle the full
378 * range of basic blocks we'll be examining. If that fails,
379 * try a smaller size. We need to be able to read at least
380 * a log sector, or we're out of luck.
381 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 bufblks = 1 << ffs(nbblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 while (!(bp = xlog_get_bp(log, bufblks))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 bufblks >>= 1;
Alex Elder6881a222010-04-13 15:22:29 +1000385 if (bufblks < xlog_sectbb(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return ENOMEM;
387 }
388
389 for (i = start_blk; i < start_blk + nbblks; i += bufblks) {
390 int bcount;
391
392 bcount = min(bufblks, (start_blk + nbblks - i));
393
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100394 error = xlog_bread(log, i, bcount, bp, &buf);
395 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 goto out;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 for (j = 0; j < bcount; j++) {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +1000399 cycle = xlog_get_cycle(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (cycle == stop_on_cycle_no) {
401 *new_blk = i+j;
402 goto out;
403 }
404
405 buf += BBSIZE;
406 }
407 }
408
409 *new_blk = -1;
410
411out:
412 xlog_put_bp(bp);
413 return error;
414}
415
416/*
417 * Potentially backup over partial log record write.
418 *
419 * In the typical case, last_blk is the number of the block directly after
420 * a good log record. Therefore, we subtract one to get the block number
421 * of the last block in the given buffer. extra_bblks contains the number
422 * of blocks we would have read on a previous read. This happens when the
423 * last log record is split over the end of the physical log.
424 *
425 * extra_bblks is the number of blocks potentially verified on a previous
426 * call to this routine.
427 */
428STATIC int
429xlog_find_verify_log_record(
430 xlog_t *log,
431 xfs_daddr_t start_blk,
432 xfs_daddr_t *last_blk,
433 int extra_bblks)
434{
435 xfs_daddr_t i;
436 xfs_buf_t *bp;
437 xfs_caddr_t offset = NULL;
438 xlog_rec_header_t *head = NULL;
439 int error = 0;
440 int smallmem = 0;
441 int num_blks = *last_blk - start_blk;
442 int xhdrs;
443
444 ASSERT(start_blk != 0 || *last_blk != start_blk);
445
446 if (!(bp = xlog_get_bp(log, num_blks))) {
447 if (!(bp = xlog_get_bp(log, 1)))
448 return ENOMEM;
449 smallmem = 1;
450 } else {
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100451 error = xlog_bread(log, start_blk, num_blks, bp, &offset);
452 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 offset += ((num_blks - 1) << BBSHIFT);
455 }
456
457 for (i = (*last_blk) - 1; i >= 0; i--) {
458 if (i < start_blk) {
459 /* valid log record not found */
460 xlog_warn(
461 "XFS: Log inconsistent (didn't find previous header)");
462 ASSERT(0);
463 error = XFS_ERROR(EIO);
464 goto out;
465 }
466
467 if (smallmem) {
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100468 error = xlog_bread(log, i, 1, bp, &offset);
469 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472
473 head = (xlog_rec_header_t *)offset;
474
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000475 if (XLOG_HEADER_MAGIC_NUM == be32_to_cpu(head->h_magicno))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 break;
477
478 if (!smallmem)
479 offset -= BBSIZE;
480 }
481
482 /*
483 * We hit the beginning of the physical log & still no header. Return
484 * to caller. If caller can handle a return of -1, then this routine
485 * will be called again for the end of the physical log.
486 */
487 if (i == -1) {
488 error = -1;
489 goto out;
490 }
491
492 /*
493 * We have the final block of the good log (the first block
494 * of the log record _before_ the head. So we check the uuid.
495 */
496 if ((error = xlog_header_check_mount(log->l_mp, head)))
497 goto out;
498
499 /*
500 * We may have found a log record header before we expected one.
501 * last_blk will be the 1st block # with a given cycle #. We may end
502 * up reading an entire log record. In this case, we don't want to
503 * reset last_blk. Only when last_blk points in the middle of a log
504 * record do we update last_blk.
505 */
Eric Sandeen62118702008-03-06 13:44:28 +1100506 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000507 uint h_size = be32_to_cpu(head->h_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 xhdrs = h_size / XLOG_HEADER_CYCLE_SIZE;
510 if (h_size % XLOG_HEADER_CYCLE_SIZE)
511 xhdrs++;
512 } else {
513 xhdrs = 1;
514 }
515
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000516 if (*last_blk - i + extra_bblks !=
517 BTOBB(be32_to_cpu(head->h_len)) + xhdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 *last_blk = i;
519
520out:
521 xlog_put_bp(bp);
522 return error;
523}
524
525/*
526 * Head is defined to be the point of the log where the next log write
527 * write could go. This means that incomplete LR writes at the end are
528 * eliminated when calculating the head. We aren't guaranteed that previous
529 * LR have complete transactions. We only know that a cycle number of
530 * current cycle number -1 won't be present in the log if we start writing
531 * from our current block number.
532 *
533 * last_blk contains the block number of the first block with a given
534 * cycle number.
535 *
536 * Return: zero if normal, non-zero if error.
537 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000538STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539xlog_find_head(
540 xlog_t *log,
541 xfs_daddr_t *return_head_blk)
542{
543 xfs_buf_t *bp;
544 xfs_caddr_t offset;
545 xfs_daddr_t new_blk, first_blk, start_blk, last_blk, head_blk;
546 int num_scan_bblks;
547 uint first_half_cycle, last_half_cycle;
548 uint stop_on_cycle;
549 int error, log_bbnum = log->l_logBBsize;
550
551 /* Is the end of the log device zeroed? */
552 if ((error = xlog_find_zeroed(log, &first_blk)) == -1) {
553 *return_head_blk = first_blk;
554
555 /* Is the whole lot zeroed? */
556 if (!first_blk) {
557 /* Linux XFS shouldn't generate totally zeroed logs -
558 * mkfs etc write a dummy unmount record to a fresh
559 * log so we can store the uuid in there
560 */
561 xlog_warn("XFS: totally zeroed log");
562 }
563
564 return 0;
565 } else if (error) {
566 xlog_warn("XFS: empty log check failed");
567 return error;
568 }
569
570 first_blk = 0; /* get cycle # of 1st block */
571 bp = xlog_get_bp(log, 1);
572 if (!bp)
573 return ENOMEM;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100574
575 error = xlog_bread(log, 0, 1, bp, &offset);
576 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 goto bp_err;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100578
Christoph Hellwig03bea6f2007-10-12 10:58:05 +1000579 first_half_cycle = xlog_get_cycle(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 last_blk = head_blk = log_bbnum - 1; /* get cycle # of last block */
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100582 error = xlog_bread(log, last_blk, 1, bp, &offset);
583 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 goto bp_err;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100585
Christoph Hellwig03bea6f2007-10-12 10:58:05 +1000586 last_half_cycle = xlog_get_cycle(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 ASSERT(last_half_cycle != 0);
588
589 /*
590 * If the 1st half cycle number is equal to the last half cycle number,
591 * then the entire log is stamped with the same cycle number. In this
592 * case, head_blk can't be set to zero (which makes sense). The below
593 * math doesn't work out properly with head_blk equal to zero. Instead,
594 * we set it to log_bbnum which is an invalid block number, but this
595 * value makes the math correct. If head_blk doesn't changed through
596 * all the tests below, *head_blk is set to zero at the very end rather
597 * than log_bbnum. In a sense, log_bbnum and zero are the same block
598 * in a circular file.
599 */
600 if (first_half_cycle == last_half_cycle) {
601 /*
602 * In this case we believe that the entire log should have
603 * cycle number last_half_cycle. We need to scan backwards
604 * from the end verifying that there are no holes still
605 * containing last_half_cycle - 1. If we find such a hole,
606 * then the start of that hole will be the new head. The
607 * simple case looks like
608 * x | x ... | x - 1 | x
609 * Another case that fits this picture would be
610 * x | x + 1 | x ... | x
Nathan Scottc41564b2006-03-29 08:55:14 +1000611 * In this case the head really is somewhere at the end of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 * log, as one of the latest writes at the beginning was
613 * incomplete.
614 * One more case is
615 * x | x + 1 | x ... | x - 1 | x
616 * This is really the combination of the above two cases, and
617 * the head has to end up at the start of the x-1 hole at the
618 * end of the log.
619 *
620 * In the 256k log case, we will read from the beginning to the
621 * end of the log and search for cycle numbers equal to x-1.
622 * We don't worry about the x+1 blocks that we encounter,
623 * because we know that they cannot be the head since the log
624 * started with x.
625 */
626 head_blk = log_bbnum;
627 stop_on_cycle = last_half_cycle - 1;
628 } else {
629 /*
630 * In this case we want to find the first block with cycle
631 * number matching last_half_cycle. We expect the log to be
632 * some variation on
633 * x + 1 ... | x ...
634 * The first block with cycle number x (last_half_cycle) will
635 * be where the new head belongs. First we do a binary search
636 * for the first occurrence of last_half_cycle. The binary
637 * search may not be totally accurate, so then we scan back
638 * from there looking for occurrences of last_half_cycle before
639 * us. If that backwards scan wraps around the beginning of
640 * the log, then we look for occurrences of last_half_cycle - 1
641 * at the end of the log. The cases we're looking for look
642 * like
643 * x + 1 ... | x | x + 1 | x ...
644 * ^ binary search stopped here
645 * or
646 * x + 1 ... | x ... | x - 1 | x
647 * <---------> less than scan distance
648 */
649 stop_on_cycle = last_half_cycle;
650 if ((error = xlog_find_cycle_start(log, bp, first_blk,
651 &head_blk, last_half_cycle)))
652 goto bp_err;
653 }
654
655 /*
656 * Now validate the answer. Scan back some number of maximum possible
657 * blocks and make sure each one has the expected cycle number. The
658 * maximum is determined by the total possible amount of buffering
659 * in the in-core log. The following number can be made tighter if
660 * we actually look at the block size of the filesystem.
661 */
662 num_scan_bblks = XLOG_TOTAL_REC_SHIFT(log);
663 if (head_blk >= num_scan_bblks) {
664 /*
665 * We are guaranteed that the entire check can be performed
666 * in one buffer.
667 */
668 start_blk = head_blk - num_scan_bblks;
669 if ((error = xlog_find_verify_cycle(log,
670 start_blk, num_scan_bblks,
671 stop_on_cycle, &new_blk)))
672 goto bp_err;
673 if (new_blk != -1)
674 head_blk = new_blk;
675 } else { /* need to read 2 parts of log */
676 /*
677 * We are going to scan backwards in the log in two parts.
678 * First we scan the physical end of the log. In this part
679 * of the log, we are looking for blocks with cycle number
680 * last_half_cycle - 1.
681 * If we find one, then we know that the log starts there, as
682 * we've found a hole that didn't get written in going around
683 * the end of the physical log. The simple case for this is
684 * x + 1 ... | x ... | x - 1 | x
685 * <---------> less than scan distance
686 * If all of the blocks at the end of the log have cycle number
687 * last_half_cycle, then we check the blocks at the start of
688 * the log looking for occurrences of last_half_cycle. If we
689 * find one, then our current estimate for the location of the
690 * first occurrence of last_half_cycle is wrong and we move
691 * back to the hole we've found. This case looks like
692 * x + 1 ... | x | x + 1 | x ...
693 * ^ binary search stopped here
694 * Another case we need to handle that only occurs in 256k
695 * logs is
696 * x + 1 ... | x ... | x+1 | x ...
697 * ^ binary search stops here
698 * In a 256k log, the scan at the end of the log will see the
699 * x + 1 blocks. We need to skip past those since that is
700 * certainly not the head of the log. By searching for
701 * last_half_cycle-1 we accomplish that.
702 */
703 start_blk = log_bbnum - num_scan_bblks + head_blk;
704 ASSERT(head_blk <= INT_MAX &&
705 (xfs_daddr_t) num_scan_bblks - head_blk >= 0);
706 if ((error = xlog_find_verify_cycle(log, start_blk,
707 num_scan_bblks - (int)head_blk,
708 (stop_on_cycle - 1), &new_blk)))
709 goto bp_err;
710 if (new_blk != -1) {
711 head_blk = new_blk;
712 goto bad_blk;
713 }
714
715 /*
716 * Scan beginning of log now. The last part of the physical
717 * log is good. This scan needs to verify that it doesn't find
718 * the last_half_cycle.
719 */
720 start_blk = 0;
721 ASSERT(head_blk <= INT_MAX);
722 if ((error = xlog_find_verify_cycle(log,
723 start_blk, (int)head_blk,
724 stop_on_cycle, &new_blk)))
725 goto bp_err;
726 if (new_blk != -1)
727 head_blk = new_blk;
728 }
729
730 bad_blk:
731 /*
732 * Now we need to make sure head_blk is not pointing to a block in
733 * the middle of a log record.
734 */
735 num_scan_bblks = XLOG_REC_SHIFT(log);
736 if (head_blk >= num_scan_bblks) {
737 start_blk = head_blk - num_scan_bblks; /* don't read head_blk */
738
739 /* start ptr at last block ptr before head_blk */
740 if ((error = xlog_find_verify_log_record(log, start_blk,
741 &head_blk, 0)) == -1) {
742 error = XFS_ERROR(EIO);
743 goto bp_err;
744 } else if (error)
745 goto bp_err;
746 } else {
747 start_blk = 0;
748 ASSERT(head_blk <= INT_MAX);
749 if ((error = xlog_find_verify_log_record(log, start_blk,
750 &head_blk, 0)) == -1) {
751 /* We hit the beginning of the log during our search */
752 start_blk = log_bbnum - num_scan_bblks + head_blk;
753 new_blk = log_bbnum;
754 ASSERT(start_blk <= INT_MAX &&
755 (xfs_daddr_t) log_bbnum-start_blk >= 0);
756 ASSERT(head_blk <= INT_MAX);
757 if ((error = xlog_find_verify_log_record(log,
758 start_blk, &new_blk,
759 (int)head_blk)) == -1) {
760 error = XFS_ERROR(EIO);
761 goto bp_err;
762 } else if (error)
763 goto bp_err;
764 if (new_blk != log_bbnum)
765 head_blk = new_blk;
766 } else if (error)
767 goto bp_err;
768 }
769
770 xlog_put_bp(bp);
771 if (head_blk == log_bbnum)
772 *return_head_blk = 0;
773 else
774 *return_head_blk = head_blk;
775 /*
776 * When returning here, we have a good block number. Bad block
777 * means that during a previous crash, we didn't have a clean break
778 * from cycle number N to cycle number N-1. In this case, we need
779 * to find the first block with cycle number N-1.
780 */
781 return 0;
782
783 bp_err:
784 xlog_put_bp(bp);
785
786 if (error)
787 xlog_warn("XFS: failed to find log head");
788 return error;
789}
790
791/*
792 * Find the sync block number or the tail of the log.
793 *
794 * This will be the block number of the last record to have its
795 * associated buffers synced to disk. Every log record header has
796 * a sync lsn embedded in it. LSNs hold block numbers, so it is easy
797 * to get a sync block number. The only concern is to figure out which
798 * log record header to believe.
799 *
800 * The following algorithm uses the log record header with the largest
801 * lsn. The entire log record does not need to be valid. We only care
802 * that the header is valid.
803 *
804 * We could speed up search by using current head_blk buffer, but it is not
805 * available.
806 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000807STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808xlog_find_tail(
809 xlog_t *log,
810 xfs_daddr_t *head_blk,
Eric Sandeen65be6052006-01-11 15:34:19 +1100811 xfs_daddr_t *tail_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 xlog_rec_header_t *rhead;
814 xlog_op_header_t *op_head;
815 xfs_caddr_t offset = NULL;
816 xfs_buf_t *bp;
817 int error, i, found;
818 xfs_daddr_t umount_data_blk;
819 xfs_daddr_t after_umount_blk;
820 xfs_lsn_t tail_lsn;
821 int hblks;
822
823 found = 0;
824
825 /*
826 * Find previous log record
827 */
828 if ((error = xlog_find_head(log, head_blk)))
829 return error;
830
831 bp = xlog_get_bp(log, 1);
832 if (!bp)
833 return ENOMEM;
834 if (*head_blk == 0) { /* special case */
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100835 error = xlog_bread(log, 0, 1, bp, &offset);
836 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 goto bread_err;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100838
Christoph Hellwig03bea6f2007-10-12 10:58:05 +1000839 if (xlog_get_cycle(offset) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 *tail_blk = 0;
841 /* leave all other log inited values alone */
842 goto exit;
843 }
844 }
845
846 /*
847 * Search backwards looking for log record header block
848 */
849 ASSERT(*head_blk < INT_MAX);
850 for (i = (int)(*head_blk) - 1; i >= 0; i--) {
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100851 error = xlog_bread(log, i, 1, bp, &offset);
852 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 goto bread_err;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100854
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000855 if (XLOG_HEADER_MAGIC_NUM == be32_to_cpu(*(__be32 *)offset)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 found = 1;
857 break;
858 }
859 }
860 /*
861 * If we haven't found the log record header block, start looking
862 * again from the end of the physical log. XXXmiken: There should be
863 * a check here to make sure we didn't search more than N blocks in
864 * the previous code.
865 */
866 if (!found) {
867 for (i = log->l_logBBsize - 1; i >= (int)(*head_blk); i--) {
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100868 error = xlog_bread(log, i, 1, bp, &offset);
869 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 goto bread_err;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (XLOG_HEADER_MAGIC_NUM ==
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000873 be32_to_cpu(*(__be32 *)offset)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 found = 2;
875 break;
876 }
877 }
878 }
879 if (!found) {
880 xlog_warn("XFS: xlog_find_tail: couldn't find sync record");
881 ASSERT(0);
882 return XFS_ERROR(EIO);
883 }
884
885 /* find blk_no of tail of log */
886 rhead = (xlog_rec_header_t *)offset;
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000887 *tail_blk = BLOCK_LSN(be64_to_cpu(rhead->h_tail_lsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 /*
890 * Reset log values according to the state of the log when we
891 * crashed. In the case where head_blk == 0, we bump curr_cycle
892 * one because the next write starts a new cycle rather than
893 * continuing the cycle of the last good log record. At this
894 * point we have guaranteed that all partial log records have been
895 * accounted for. Therefore, we know that the last good log record
896 * written was complete and ended exactly on the end boundary
897 * of the physical log.
898 */
899 log->l_prev_block = i;
900 log->l_curr_block = (int)*head_blk;
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000901 log->l_curr_cycle = be32_to_cpu(rhead->h_cycle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if (found == 2)
903 log->l_curr_cycle++;
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000904 log->l_tail_lsn = be64_to_cpu(rhead->h_tail_lsn);
905 log->l_last_sync_lsn = be64_to_cpu(rhead->h_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 log->l_grant_reserve_cycle = log->l_curr_cycle;
907 log->l_grant_reserve_bytes = BBTOB(log->l_curr_block);
908 log->l_grant_write_cycle = log->l_curr_cycle;
909 log->l_grant_write_bytes = BBTOB(log->l_curr_block);
910
911 /*
912 * Look for unmount record. If we find it, then we know there
913 * was a clean unmount. Since 'i' could be the last block in
914 * the physical log, we convert to a log block before comparing
915 * to the head_blk.
916 *
917 * Save the current tail lsn to use to pass to
918 * xlog_clear_stale_blocks() below. We won't want to clear the
919 * unmount record if there is one, so we pass the lsn of the
920 * unmount record rather than the block after it.
921 */
Eric Sandeen62118702008-03-06 13:44:28 +1100922 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000923 int h_size = be32_to_cpu(rhead->h_size);
924 int h_version = be32_to_cpu(rhead->h_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 if ((h_version & XLOG_VERSION_2) &&
927 (h_size > XLOG_HEADER_CYCLE_SIZE)) {
928 hblks = h_size / XLOG_HEADER_CYCLE_SIZE;
929 if (h_size % XLOG_HEADER_CYCLE_SIZE)
930 hblks++;
931 } else {
932 hblks = 1;
933 }
934 } else {
935 hblks = 1;
936 }
937 after_umount_blk = (i + hblks + (int)
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000938 BTOBB(be32_to_cpu(rhead->h_len))) % log->l_logBBsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 tail_lsn = log->l_tail_lsn;
940 if (*head_blk == after_umount_blk &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000941 be32_to_cpu(rhead->h_num_logops) == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 umount_data_blk = (i + hblks) % log->l_logBBsize;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100943 error = xlog_bread(log, umount_data_blk, 1, bp, &offset);
944 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 goto bread_err;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 op_head = (xlog_op_header_t *)offset;
948 if (op_head->oh_flags & XLOG_UNMOUNT_TRANS) {
949 /*
950 * Set tail and last sync so that newly written
951 * log records will point recovery to after the
952 * current unmount record.
953 */
Christoph Hellwig03bea6f2007-10-12 10:58:05 +1000954 log->l_tail_lsn =
955 xlog_assign_lsn(log->l_curr_cycle,
956 after_umount_blk);
957 log->l_last_sync_lsn =
958 xlog_assign_lsn(log->l_curr_cycle,
959 after_umount_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 *tail_blk = after_umount_blk;
David Chinner92821e22007-05-24 15:26:31 +1000961
962 /*
963 * Note that the unmount was clean. If the unmount
964 * was not clean, we need to know this to rebuild the
965 * superblock counters from the perag headers if we
966 * have a filesystem using non-persistent counters.
967 */
968 log->l_mp->m_flags |= XFS_MOUNT_WAS_CLEAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970 }
971
972 /*
973 * Make sure that there are no blocks in front of the head
974 * with the same cycle number as the head. This can happen
975 * because we allow multiple outstanding log writes concurrently,
976 * and the later writes might make it out before earlier ones.
977 *
978 * We use the lsn from before modifying it so that we'll never
979 * overwrite the unmount record after a clean unmount.
980 *
981 * Do this only if we are going to recover the filesystem
982 *
983 * NOTE: This used to say "if (!readonly)"
984 * However on Linux, we can & do recover a read-only filesystem.
985 * We only skip recovery if NORECOVERY is specified on mount,
986 * in which case we would not be here.
987 *
988 * But... if the -device- itself is readonly, just skip this.
989 * We can't recover this device anyway, so it won't matter.
990 */
991 if (!xfs_readonly_buftarg(log->l_mp->m_logdev_targp)) {
992 error = xlog_clear_stale_blocks(log, tail_lsn);
993 }
994
995bread_err:
996exit:
997 xlog_put_bp(bp);
998
999 if (error)
1000 xlog_warn("XFS: failed to locate log tail");
1001 return error;
1002}
1003
1004/*
1005 * Is the log zeroed at all?
1006 *
1007 * The last binary search should be changed to perform an X block read
1008 * once X becomes small enough. You can then search linearly through
1009 * the X blocks. This will cut down on the number of reads we need to do.
1010 *
1011 * If the log is partially zeroed, this routine will pass back the blkno
1012 * of the first block with cycle number 0. It won't have a complete LR
1013 * preceding it.
1014 *
1015 * Return:
1016 * 0 => the log is completely written to
1017 * -1 => use *blk_no as the first block of the log
1018 * >0 => error has occurred
1019 */
David Chinnera8272ce2007-11-23 16:28:09 +11001020STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021xlog_find_zeroed(
1022 xlog_t *log,
1023 xfs_daddr_t *blk_no)
1024{
1025 xfs_buf_t *bp;
1026 xfs_caddr_t offset;
1027 uint first_cycle, last_cycle;
1028 xfs_daddr_t new_blk, last_blk, start_blk;
1029 xfs_daddr_t num_scan_bblks;
1030 int error, log_bbnum = log->l_logBBsize;
1031
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10001032 *blk_no = 0;
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 /* check totally zeroed log */
1035 bp = xlog_get_bp(log, 1);
1036 if (!bp)
1037 return ENOMEM;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001038 error = xlog_bread(log, 0, 1, bp, &offset);
1039 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 goto bp_err;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001041
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001042 first_cycle = xlog_get_cycle(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (first_cycle == 0) { /* completely zeroed log */
1044 *blk_no = 0;
1045 xlog_put_bp(bp);
1046 return -1;
1047 }
1048
1049 /* check partially zeroed log */
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001050 error = xlog_bread(log, log_bbnum-1, 1, bp, &offset);
1051 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 goto bp_err;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001053
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001054 last_cycle = xlog_get_cycle(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (last_cycle != 0) { /* log completely written to */
1056 xlog_put_bp(bp);
1057 return 0;
1058 } else if (first_cycle != 1) {
1059 /*
1060 * If the cycle of the last block is zero, the cycle of
1061 * the first block must be 1. If it's not, maybe we're
1062 * not looking at a log... Bail out.
1063 */
1064 xlog_warn("XFS: Log inconsistent or not a log (last==0, first!=1)");
1065 return XFS_ERROR(EINVAL);
1066 }
1067
1068 /* we have a partially zeroed log */
1069 last_blk = log_bbnum-1;
1070 if ((error = xlog_find_cycle_start(log, bp, 0, &last_blk, 0)))
1071 goto bp_err;
1072
1073 /*
1074 * Validate the answer. Because there is no way to guarantee that
1075 * the entire log is made up of log records which are the same size,
1076 * we scan over the defined maximum blocks. At this point, the maximum
1077 * is not chosen to mean anything special. XXXmiken
1078 */
1079 num_scan_bblks = XLOG_TOTAL_REC_SHIFT(log);
1080 ASSERT(num_scan_bblks <= INT_MAX);
1081
1082 if (last_blk < num_scan_bblks)
1083 num_scan_bblks = last_blk;
1084 start_blk = last_blk - num_scan_bblks;
1085
1086 /*
1087 * We search for any instances of cycle number 0 that occur before
1088 * our current estimate of the head. What we're trying to detect is
1089 * 1 ... | 0 | 1 | 0...
1090 * ^ binary search ends here
1091 */
1092 if ((error = xlog_find_verify_cycle(log, start_blk,
1093 (int)num_scan_bblks, 0, &new_blk)))
1094 goto bp_err;
1095 if (new_blk != -1)
1096 last_blk = new_blk;
1097
1098 /*
1099 * Potentially backup over partial log record write. We don't need
1100 * to search the end of the log because we know it is zero.
1101 */
1102 if ((error = xlog_find_verify_log_record(log, start_blk,
1103 &last_blk, 0)) == -1) {
1104 error = XFS_ERROR(EIO);
1105 goto bp_err;
1106 } else if (error)
1107 goto bp_err;
1108
1109 *blk_no = last_blk;
1110bp_err:
1111 xlog_put_bp(bp);
1112 if (error)
1113 return error;
1114 return -1;
1115}
1116
1117/*
1118 * These are simple subroutines used by xlog_clear_stale_blocks() below
1119 * to initialize a buffer full of empty log record headers and write
1120 * them into the log.
1121 */
1122STATIC void
1123xlog_add_record(
1124 xlog_t *log,
1125 xfs_caddr_t buf,
1126 int cycle,
1127 int block,
1128 int tail_cycle,
1129 int tail_block)
1130{
1131 xlog_rec_header_t *recp = (xlog_rec_header_t *)buf;
1132
1133 memset(buf, 0, BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001134 recp->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1135 recp->h_cycle = cpu_to_be32(cycle);
1136 recp->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001137 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001138 recp->h_lsn = cpu_to_be64(xlog_assign_lsn(cycle, block));
1139 recp->h_tail_lsn = cpu_to_be64(xlog_assign_lsn(tail_cycle, tail_block));
1140 recp->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 memcpy(&recp->h_fs_uuid, &log->l_mp->m_sb.sb_uuid, sizeof(uuid_t));
1142}
1143
1144STATIC int
1145xlog_write_log_records(
1146 xlog_t *log,
1147 int cycle,
1148 int start_block,
1149 int blocks,
1150 int tail_cycle,
1151 int tail_block)
1152{
1153 xfs_caddr_t offset;
1154 xfs_buf_t *bp;
1155 int balign, ealign;
Alex Elder5c17f532010-04-13 15:22:48 +10001156 int sectbb = xlog_sectbb(log);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 int end_block = start_block + blocks;
1158 int bufblks;
1159 int error = 0;
1160 int i, j = 0;
1161
Alex Elder6881a222010-04-13 15:22:29 +10001162 /*
1163 * Greedily allocate a buffer big enough to handle the full
1164 * range of basic blocks to be written. If that fails, try
1165 * a smaller size. We need to be able to write at least a
1166 * log sector, or we're out of luck.
1167 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 bufblks = 1 << ffs(blocks);
1169 while (!(bp = xlog_get_bp(log, bufblks))) {
1170 bufblks >>= 1;
Alex Elder6881a222010-04-13 15:22:29 +10001171 if (bufblks < xlog_sectbb(log))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return ENOMEM;
1173 }
1174
1175 /* We may need to do a read at the start to fill in part of
1176 * the buffer in the starting sector not covered by the first
1177 * write below.
1178 */
Alex Elder5c17f532010-04-13 15:22:48 +10001179 balign = round_down(start_block, sectbb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (balign != start_block) {
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001181 error = xlog_bread_noalign(log, start_block, 1, bp);
1182 if (error)
1183 goto out_put_bp;
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 j = start_block - balign;
1186 }
1187
1188 for (i = start_block; i < end_block; i += bufblks) {
1189 int bcount, endcount;
1190
1191 bcount = min(bufblks, end_block - start_block);
1192 endcount = bcount - j;
1193
1194 /* We may need to do a read at the end to fill in part of
1195 * the buffer in the final sector not covered by the write.
1196 * If this is the same sector as the above read, skip it.
1197 */
Alex Elder5c17f532010-04-13 15:22:48 +10001198 ealign = round_down(end_block, sectbb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if (j == 0 && (start_block + endcount > ealign)) {
1200 offset = XFS_BUF_PTR(bp);
1201 balign = BBTOB(ealign - start_block);
David Chinner234f56a2008-04-10 12:24:24 +10001202 error = XFS_BUF_SET_PTR(bp, offset + balign,
1203 BBTOB(sectbb));
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001204 if (error)
1205 break;
1206
1207 error = xlog_bread_noalign(log, ealign, sectbb, bp);
1208 if (error)
1209 break;
1210
1211 error = XFS_BUF_SET_PTR(bp, offset, bufblks);
David Chinner234f56a2008-04-10 12:24:24 +10001212 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
1215
1216 offset = xlog_align(log, start_block, endcount, bp);
1217 for (; j < endcount; j++) {
1218 xlog_add_record(log, offset, cycle, i+j,
1219 tail_cycle, tail_block);
1220 offset += BBSIZE;
1221 }
1222 error = xlog_bwrite(log, start_block, endcount, bp);
1223 if (error)
1224 break;
1225 start_block += endcount;
1226 j = 0;
1227 }
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001228
1229 out_put_bp:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 xlog_put_bp(bp);
1231 return error;
1232}
1233
1234/*
1235 * This routine is called to blow away any incomplete log writes out
1236 * in front of the log head. We do this so that we won't become confused
1237 * if we come up, write only a little bit more, and then crash again.
1238 * If we leave the partial log records out there, this situation could
1239 * cause us to think those partial writes are valid blocks since they
1240 * have the current cycle number. We get rid of them by overwriting them
1241 * with empty log records with the old cycle number rather than the
1242 * current one.
1243 *
1244 * The tail lsn is passed in rather than taken from
1245 * the log so that we will not write over the unmount record after a
1246 * clean unmount in a 512 block log. Doing so would leave the log without
1247 * any valid log records in it until a new one was written. If we crashed
1248 * during that time we would not be able to recover.
1249 */
1250STATIC int
1251xlog_clear_stale_blocks(
1252 xlog_t *log,
1253 xfs_lsn_t tail_lsn)
1254{
1255 int tail_cycle, head_cycle;
1256 int tail_block, head_block;
1257 int tail_distance, max_distance;
1258 int distance;
1259 int error;
1260
1261 tail_cycle = CYCLE_LSN(tail_lsn);
1262 tail_block = BLOCK_LSN(tail_lsn);
1263 head_cycle = log->l_curr_cycle;
1264 head_block = log->l_curr_block;
1265
1266 /*
1267 * Figure out the distance between the new head of the log
1268 * and the tail. We want to write over any blocks beyond the
1269 * head that we may have written just before the crash, but
1270 * we don't want to overwrite the tail of the log.
1271 */
1272 if (head_cycle == tail_cycle) {
1273 /*
1274 * The tail is behind the head in the physical log,
1275 * so the distance from the head to the tail is the
1276 * distance from the head to the end of the log plus
1277 * the distance from the beginning of the log to the
1278 * tail.
1279 */
1280 if (unlikely(head_block < tail_block || head_block >= log->l_logBBsize)) {
1281 XFS_ERROR_REPORT("xlog_clear_stale_blocks(1)",
1282 XFS_ERRLEVEL_LOW, log->l_mp);
1283 return XFS_ERROR(EFSCORRUPTED);
1284 }
1285 tail_distance = tail_block + (log->l_logBBsize - head_block);
1286 } else {
1287 /*
1288 * The head is behind the tail in the physical log,
1289 * so the distance from the head to the tail is just
1290 * the tail block minus the head block.
1291 */
1292 if (unlikely(head_block >= tail_block || head_cycle != (tail_cycle + 1))){
1293 XFS_ERROR_REPORT("xlog_clear_stale_blocks(2)",
1294 XFS_ERRLEVEL_LOW, log->l_mp);
1295 return XFS_ERROR(EFSCORRUPTED);
1296 }
1297 tail_distance = tail_block - head_block;
1298 }
1299
1300 /*
1301 * If the head is right up against the tail, we can't clear
1302 * anything.
1303 */
1304 if (tail_distance <= 0) {
1305 ASSERT(tail_distance == 0);
1306 return 0;
1307 }
1308
1309 max_distance = XLOG_TOTAL_REC_SHIFT(log);
1310 /*
1311 * Take the smaller of the maximum amount of outstanding I/O
1312 * we could have and the distance to the tail to clear out.
1313 * We take the smaller so that we don't overwrite the tail and
1314 * we don't waste all day writing from the head to the tail
1315 * for no reason.
1316 */
1317 max_distance = MIN(max_distance, tail_distance);
1318
1319 if ((head_block + max_distance) <= log->l_logBBsize) {
1320 /*
1321 * We can stomp all the blocks we need to without
1322 * wrapping around the end of the log. Just do it
1323 * in a single write. Use the cycle number of the
1324 * current cycle minus one so that the log will look like:
1325 * n ... | n - 1 ...
1326 */
1327 error = xlog_write_log_records(log, (head_cycle - 1),
1328 head_block, max_distance, tail_cycle,
1329 tail_block);
1330 if (error)
1331 return error;
1332 } else {
1333 /*
1334 * We need to wrap around the end of the physical log in
1335 * order to clear all the blocks. Do it in two separate
1336 * I/Os. The first write should be from the head to the
1337 * end of the physical log, and it should use the current
1338 * cycle number minus one just like above.
1339 */
1340 distance = log->l_logBBsize - head_block;
1341 error = xlog_write_log_records(log, (head_cycle - 1),
1342 head_block, distance, tail_cycle,
1343 tail_block);
1344
1345 if (error)
1346 return error;
1347
1348 /*
1349 * Now write the blocks at the start of the physical log.
1350 * This writes the remainder of the blocks we want to clear.
1351 * It uses the current cycle number since we're now on the
1352 * same cycle as the head so that we get:
1353 * n ... n ... | n - 1 ...
1354 * ^^^^^ blocks we're writing
1355 */
1356 distance = max_distance - (log->l_logBBsize - head_block);
1357 error = xlog_write_log_records(log, head_cycle, 0, distance,
1358 tail_cycle, tail_block);
1359 if (error)
1360 return error;
1361 }
1362
1363 return 0;
1364}
1365
1366/******************************************************************************
1367 *
1368 * Log recover routines
1369 *
1370 ******************************************************************************
1371 */
1372
1373STATIC xlog_recover_t *
1374xlog_recover_find_tid(
Dave Chinnerf0a76952010-01-11 11:49:57 +00001375 struct hlist_head *head,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 xlog_tid_t tid)
1377{
Dave Chinnerf0a76952010-01-11 11:49:57 +00001378 xlog_recover_t *trans;
1379 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Dave Chinnerf0a76952010-01-11 11:49:57 +00001381 hlist_for_each_entry(trans, n, head, r_list) {
1382 if (trans->r_log_tid == tid)
1383 return trans;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
Dave Chinnerf0a76952010-01-11 11:49:57 +00001385 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
1388STATIC void
Dave Chinnerf0a76952010-01-11 11:49:57 +00001389xlog_recover_new_tid(
1390 struct hlist_head *head,
1391 xlog_tid_t tid,
1392 xfs_lsn_t lsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
Dave Chinnerf0a76952010-01-11 11:49:57 +00001394 xlog_recover_t *trans;
1395
1396 trans = kmem_zalloc(sizeof(xlog_recover_t), KM_SLEEP);
1397 trans->r_log_tid = tid;
1398 trans->r_lsn = lsn;
1399 INIT_LIST_HEAD(&trans->r_itemq);
1400
1401 INIT_HLIST_NODE(&trans->r_list);
1402 hlist_add_head(&trans->r_list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
1404
1405STATIC void
1406xlog_recover_add_item(
Dave Chinnerf0a76952010-01-11 11:49:57 +00001407 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
1409 xlog_recover_item_t *item;
1410
1411 item = kmem_zalloc(sizeof(xlog_recover_item_t), KM_SLEEP);
Dave Chinnerf0a76952010-01-11 11:49:57 +00001412 INIT_LIST_HEAD(&item->ri_list);
1413 list_add_tail(&item->ri_list, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
1416STATIC int
1417xlog_recover_add_to_cont_trans(
Dave Chinner9abbc532010-04-13 15:06:46 +10001418 struct log *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 xlog_recover_t *trans,
1420 xfs_caddr_t dp,
1421 int len)
1422{
1423 xlog_recover_item_t *item;
1424 xfs_caddr_t ptr, old_ptr;
1425 int old_len;
1426
Dave Chinnerf0a76952010-01-11 11:49:57 +00001427 if (list_empty(&trans->r_itemq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 /* finish copying rest of trans header */
1429 xlog_recover_add_item(&trans->r_itemq);
1430 ptr = (xfs_caddr_t) &trans->r_theader +
1431 sizeof(xfs_trans_header_t) - len;
1432 memcpy(ptr, dp, len); /* d, s, l */
1433 return 0;
1434 }
Dave Chinnerf0a76952010-01-11 11:49:57 +00001435 /* take the tail entry */
1436 item = list_entry(trans->r_itemq.prev, xlog_recover_item_t, ri_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 old_ptr = item->ri_buf[item->ri_cnt-1].i_addr;
1439 old_len = item->ri_buf[item->ri_cnt-1].i_len;
1440
Christoph Hellwig760dea62005-09-02 16:56:02 +10001441 ptr = kmem_realloc(old_ptr, len+old_len, old_len, 0u);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 memcpy(&ptr[old_len], dp, len); /* d, s, l */
1443 item->ri_buf[item->ri_cnt-1].i_len += len;
1444 item->ri_buf[item->ri_cnt-1].i_addr = ptr;
Dave Chinner9abbc532010-04-13 15:06:46 +10001445 trace_xfs_log_recover_item_add_cont(log, trans, item, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return 0;
1447}
1448
1449/*
1450 * The next region to add is the start of a new region. It could be
1451 * a whole region or it could be the first part of a new region. Because
1452 * of this, the assumption here is that the type and size fields of all
1453 * format structures fit into the first 32 bits of the structure.
1454 *
1455 * This works because all regions must be 32 bit aligned. Therefore, we
1456 * either have both fields or we have neither field. In the case we have
1457 * neither field, the data part of the region is zero length. We only have
1458 * a log_op_header and can throw away the header since a new one will appear
1459 * later. If we have at least 4 bytes, then we can determine how many regions
1460 * will appear in the current log item.
1461 */
1462STATIC int
1463xlog_recover_add_to_trans(
Dave Chinner9abbc532010-04-13 15:06:46 +10001464 struct log *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 xlog_recover_t *trans,
1466 xfs_caddr_t dp,
1467 int len)
1468{
1469 xfs_inode_log_format_t *in_f; /* any will do */
1470 xlog_recover_item_t *item;
1471 xfs_caddr_t ptr;
1472
1473 if (!len)
1474 return 0;
Dave Chinnerf0a76952010-01-11 11:49:57 +00001475 if (list_empty(&trans->r_itemq)) {
David Chinner5a792c42008-10-30 17:40:09 +11001476 /* we need to catch log corruptions here */
1477 if (*(uint *)dp != XFS_TRANS_HEADER_MAGIC) {
1478 xlog_warn("XFS: xlog_recover_add_to_trans: "
1479 "bad header magic number");
1480 ASSERT(0);
1481 return XFS_ERROR(EIO);
1482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 if (len == sizeof(xfs_trans_header_t))
1484 xlog_recover_add_item(&trans->r_itemq);
1485 memcpy(&trans->r_theader, dp, len); /* d, s, l */
1486 return 0;
1487 }
1488
1489 ptr = kmem_alloc(len, KM_SLEEP);
1490 memcpy(ptr, dp, len);
1491 in_f = (xfs_inode_log_format_t *)ptr;
1492
Dave Chinnerf0a76952010-01-11 11:49:57 +00001493 /* take the tail entry */
1494 item = list_entry(trans->r_itemq.prev, xlog_recover_item_t, ri_list);
1495 if (item->ri_total != 0 &&
1496 item->ri_total == item->ri_cnt) {
1497 /* tail item is in use, get a new one */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 xlog_recover_add_item(&trans->r_itemq);
Dave Chinnerf0a76952010-01-11 11:49:57 +00001499 item = list_entry(trans->r_itemq.prev,
1500 xlog_recover_item_t, ri_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 if (item->ri_total == 0) { /* first region to be added */
Christoph Hellwige8fa6b42009-03-03 14:48:36 -05001504 if (in_f->ilf_size == 0 ||
1505 in_f->ilf_size > XLOG_MAX_REGIONS_IN_ITEM) {
1506 xlog_warn(
1507 "XFS: bad number of regions (%d) in inode log format",
1508 in_f->ilf_size);
1509 ASSERT(0);
1510 return XFS_ERROR(EIO);
1511 }
1512
1513 item->ri_total = in_f->ilf_size;
1514 item->ri_buf =
1515 kmem_zalloc(item->ri_total * sizeof(xfs_log_iovec_t),
1516 KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
1518 ASSERT(item->ri_total > item->ri_cnt);
1519 /* Description region is ri_buf[0] */
1520 item->ri_buf[item->ri_cnt].i_addr = ptr;
1521 item->ri_buf[item->ri_cnt].i_len = len;
1522 item->ri_cnt++;
Dave Chinner9abbc532010-04-13 15:06:46 +10001523 trace_xfs_log_recover_item_add(log, trans, item, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return 0;
1525}
1526
Dave Chinnerf0a76952010-01-11 11:49:57 +00001527/*
1528 * Sort the log items in the transaction. Cancelled buffers need
1529 * to be put first so they are processed before any items that might
1530 * modify the buffers. If they are cancelled, then the modifications
1531 * don't need to be replayed.
1532 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533STATIC int
1534xlog_recover_reorder_trans(
Dave Chinner9abbc532010-04-13 15:06:46 +10001535 struct log *log,
1536 xlog_recover_t *trans,
1537 int pass)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
Dave Chinnerf0a76952010-01-11 11:49:57 +00001539 xlog_recover_item_t *item, *n;
1540 LIST_HEAD(sort_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Dave Chinnerf0a76952010-01-11 11:49:57 +00001542 list_splice_init(&trans->r_itemq, &sort_list);
1543 list_for_each_entry_safe(item, n, &sort_list, ri_list) {
1544 xfs_buf_log_format_t *buf_f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Dave Chinnerf0a76952010-01-11 11:49:57 +00001546 buf_f = (xfs_buf_log_format_t *)item->ri_buf[0].i_addr;
1547
1548 switch (ITEM_TYPE(item)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 case XFS_LI_BUF:
Dave Chinnerf0a76952010-01-11 11:49:57 +00001550 if (!(buf_f->blf_flags & XFS_BLI_CANCEL)) {
Dave Chinner9abbc532010-04-13 15:06:46 +10001551 trace_xfs_log_recover_item_reorder_head(log,
1552 trans, item, pass);
Dave Chinnerf0a76952010-01-11 11:49:57 +00001553 list_move(&item->ri_list, &trans->r_itemq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 break;
1555 }
1556 case XFS_LI_INODE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 case XFS_LI_DQUOT:
1558 case XFS_LI_QUOTAOFF:
1559 case XFS_LI_EFD:
1560 case XFS_LI_EFI:
Dave Chinner9abbc532010-04-13 15:06:46 +10001561 trace_xfs_log_recover_item_reorder_tail(log,
1562 trans, item, pass);
Dave Chinnerf0a76952010-01-11 11:49:57 +00001563 list_move_tail(&item->ri_list, &trans->r_itemq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 break;
1565 default:
1566 xlog_warn(
1567 "XFS: xlog_recover_reorder_trans: unrecognized type of log operation");
1568 ASSERT(0);
1569 return XFS_ERROR(EIO);
1570 }
Dave Chinnerf0a76952010-01-11 11:49:57 +00001571 }
1572 ASSERT(list_empty(&sort_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 return 0;
1574}
1575
1576/*
1577 * Build up the table of buf cancel records so that we don't replay
1578 * cancelled data in the second pass. For buffer records that are
1579 * not cancel records, there is nothing to do here so we just return.
1580 *
1581 * If we get a cancel record which is already in the table, this indicates
1582 * that the buffer was cancelled multiple times. In order to ensure
1583 * that during pass 2 we keep the record in the table until we reach its
1584 * last occurrence in the log, we keep a reference count in the cancel
1585 * record in the table to tell us how many times we expect to see this
1586 * record during the second pass.
1587 */
1588STATIC void
1589xlog_recover_do_buffer_pass1(
1590 xlog_t *log,
1591 xfs_buf_log_format_t *buf_f)
1592{
1593 xfs_buf_cancel_t *bcp;
1594 xfs_buf_cancel_t *nextp;
1595 xfs_buf_cancel_t *prevp;
1596 xfs_buf_cancel_t **bucket;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 xfs_daddr_t blkno = 0;
1598 uint len = 0;
1599 ushort flags = 0;
1600
1601 switch (buf_f->blf_type) {
1602 case XFS_LI_BUF:
1603 blkno = buf_f->blf_blkno;
1604 len = buf_f->blf_len;
1605 flags = buf_f->blf_flags;
1606 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 }
1608
1609 /*
1610 * If this isn't a cancel buffer item, then just return.
1611 */
Dave Chinner9abbc532010-04-13 15:06:46 +10001612 if (!(flags & XFS_BLI_CANCEL)) {
1613 trace_xfs_log_recover_buf_not_cancel(log, buf_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return;
Dave Chinner9abbc532010-04-13 15:06:46 +10001615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 /*
1618 * Insert an xfs_buf_cancel record into the hash table of
1619 * them. If there is already an identical record, bump
1620 * its reference count.
1621 */
1622 bucket = &log->l_buf_cancel_table[(__uint64_t)blkno %
1623 XLOG_BC_TABLE_SIZE];
1624 /*
1625 * If the hash bucket is empty then just insert a new record into
1626 * the bucket.
1627 */
1628 if (*bucket == NULL) {
1629 bcp = (xfs_buf_cancel_t *)kmem_alloc(sizeof(xfs_buf_cancel_t),
1630 KM_SLEEP);
1631 bcp->bc_blkno = blkno;
1632 bcp->bc_len = len;
1633 bcp->bc_refcount = 1;
1634 bcp->bc_next = NULL;
1635 *bucket = bcp;
1636 return;
1637 }
1638
1639 /*
1640 * The hash bucket is not empty, so search for duplicates of our
1641 * record. If we find one them just bump its refcount. If not
1642 * then add us at the end of the list.
1643 */
1644 prevp = NULL;
1645 nextp = *bucket;
1646 while (nextp != NULL) {
1647 if (nextp->bc_blkno == blkno && nextp->bc_len == len) {
1648 nextp->bc_refcount++;
Dave Chinner9abbc532010-04-13 15:06:46 +10001649 trace_xfs_log_recover_buf_cancel_ref_inc(log, buf_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return;
1651 }
1652 prevp = nextp;
1653 nextp = nextp->bc_next;
1654 }
1655 ASSERT(prevp != NULL);
1656 bcp = (xfs_buf_cancel_t *)kmem_alloc(sizeof(xfs_buf_cancel_t),
1657 KM_SLEEP);
1658 bcp->bc_blkno = blkno;
1659 bcp->bc_len = len;
1660 bcp->bc_refcount = 1;
1661 bcp->bc_next = NULL;
1662 prevp->bc_next = bcp;
Dave Chinner9abbc532010-04-13 15:06:46 +10001663 trace_xfs_log_recover_buf_cancel_add(log, buf_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
1665
1666/*
1667 * Check to see whether the buffer being recovered has a corresponding
1668 * entry in the buffer cancel record table. If it does then return 1
1669 * so that it will be cancelled, otherwise return 0. If the buffer is
1670 * actually a buffer cancel item (XFS_BLI_CANCEL is set), then decrement
1671 * the refcount on the entry in the table and remove it from the table
1672 * if this is the last reference.
1673 *
1674 * We remove the cancel record from the table when we encounter its
1675 * last occurrence in the log so that if the same buffer is re-used
1676 * again after its last cancellation we actually replay the changes
1677 * made at that point.
1678 */
1679STATIC int
1680xlog_check_buffer_cancelled(
1681 xlog_t *log,
1682 xfs_daddr_t blkno,
1683 uint len,
1684 ushort flags)
1685{
1686 xfs_buf_cancel_t *bcp;
1687 xfs_buf_cancel_t *prevp;
1688 xfs_buf_cancel_t **bucket;
1689
1690 if (log->l_buf_cancel_table == NULL) {
1691 /*
1692 * There is nothing in the table built in pass one,
1693 * so this buffer must not be cancelled.
1694 */
1695 ASSERT(!(flags & XFS_BLI_CANCEL));
1696 return 0;
1697 }
1698
1699 bucket = &log->l_buf_cancel_table[(__uint64_t)blkno %
1700 XLOG_BC_TABLE_SIZE];
1701 bcp = *bucket;
1702 if (bcp == NULL) {
1703 /*
1704 * There is no corresponding entry in the table built
1705 * in pass one, so this buffer has not been cancelled.
1706 */
1707 ASSERT(!(flags & XFS_BLI_CANCEL));
1708 return 0;
1709 }
1710
1711 /*
1712 * Search for an entry in the buffer cancel table that
1713 * matches our buffer.
1714 */
1715 prevp = NULL;
1716 while (bcp != NULL) {
1717 if (bcp->bc_blkno == blkno && bcp->bc_len == len) {
1718 /*
1719 * We've go a match, so return 1 so that the
1720 * recovery of this buffer is cancelled.
1721 * If this buffer is actually a buffer cancel
1722 * log item, then decrement the refcount on the
1723 * one in the table and remove it if this is the
1724 * last reference.
1725 */
1726 if (flags & XFS_BLI_CANCEL) {
1727 bcp->bc_refcount--;
1728 if (bcp->bc_refcount == 0) {
1729 if (prevp == NULL) {
1730 *bucket = bcp->bc_next;
1731 } else {
1732 prevp->bc_next = bcp->bc_next;
1733 }
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001734 kmem_free(bcp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 }
1736 }
1737 return 1;
1738 }
1739 prevp = bcp;
1740 bcp = bcp->bc_next;
1741 }
1742 /*
1743 * We didn't find a corresponding entry in the table, so
1744 * return 0 so that the buffer is NOT cancelled.
1745 */
1746 ASSERT(!(flags & XFS_BLI_CANCEL));
1747 return 0;
1748}
1749
1750STATIC int
1751xlog_recover_do_buffer_pass2(
1752 xlog_t *log,
1753 xfs_buf_log_format_t *buf_f)
1754{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 xfs_daddr_t blkno = 0;
1756 ushort flags = 0;
1757 uint len = 0;
1758
1759 switch (buf_f->blf_type) {
1760 case XFS_LI_BUF:
1761 blkno = buf_f->blf_blkno;
1762 flags = buf_f->blf_flags;
1763 len = buf_f->blf_len;
1764 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
1766
1767 return xlog_check_buffer_cancelled(log, blkno, len, flags);
1768}
1769
1770/*
1771 * Perform recovery for a buffer full of inodes. In these buffers,
1772 * the only data which should be recovered is that which corresponds
1773 * to the di_next_unlinked pointers in the on disk inode structures.
1774 * The rest of the data for the inodes is always logged through the
1775 * inodes themselves rather than the inode buffer and is recovered
1776 * in xlog_recover_do_inode_trans().
1777 *
1778 * The only time when buffers full of inodes are fully recovered is
1779 * when the buffer is full of newly allocated inodes. In this case
1780 * the buffer will not be marked as an inode buffer and so will be
1781 * sent to xlog_recover_do_reg_buffer() below during recovery.
1782 */
1783STATIC int
1784xlog_recover_do_inode_buffer(
1785 xfs_mount_t *mp,
1786 xlog_recover_item_t *item,
1787 xfs_buf_t *bp,
1788 xfs_buf_log_format_t *buf_f)
1789{
1790 int i;
1791 int item_index;
1792 int bit;
1793 int nbits;
1794 int reg_buf_offset;
1795 int reg_buf_bytes;
1796 int next_unlinked_offset;
1797 int inodes_per_buf;
1798 xfs_agino_t *logged_nextp;
1799 xfs_agino_t *buffer_nextp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 unsigned int *data_map = NULL;
1801 unsigned int map_size = 0;
1802
Dave Chinner9abbc532010-04-13 15:06:46 +10001803 trace_xfs_log_recover_buf_inode_buf(mp->m_log, buf_f);
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 switch (buf_f->blf_type) {
1806 case XFS_LI_BUF:
1807 data_map = buf_f->blf_data_map;
1808 map_size = buf_f->blf_map_size;
1809 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
1811 /*
1812 * Set the variables corresponding to the current region to
1813 * 0 so that we'll initialize them on the first pass through
1814 * the loop.
1815 */
1816 reg_buf_offset = 0;
1817 reg_buf_bytes = 0;
1818 bit = 0;
1819 nbits = 0;
1820 item_index = 0;
1821 inodes_per_buf = XFS_BUF_COUNT(bp) >> mp->m_sb.sb_inodelog;
1822 for (i = 0; i < inodes_per_buf; i++) {
1823 next_unlinked_offset = (i * mp->m_sb.sb_inodesize) +
1824 offsetof(xfs_dinode_t, di_next_unlinked);
1825
1826 while (next_unlinked_offset >=
1827 (reg_buf_offset + reg_buf_bytes)) {
1828 /*
1829 * The next di_next_unlinked field is beyond
1830 * the current logged region. Find the next
1831 * logged region that contains or is beyond
1832 * the current di_next_unlinked field.
1833 */
1834 bit += nbits;
1835 bit = xfs_next_bit(data_map, map_size, bit);
1836
1837 /*
1838 * If there are no more logged regions in the
1839 * buffer, then we're done.
1840 */
1841 if (bit == -1) {
1842 return 0;
1843 }
1844
1845 nbits = xfs_contig_bits(data_map, map_size,
1846 bit);
1847 ASSERT(nbits > 0);
1848 reg_buf_offset = bit << XFS_BLI_SHIFT;
1849 reg_buf_bytes = nbits << XFS_BLI_SHIFT;
1850 item_index++;
1851 }
1852
1853 /*
1854 * If the current logged region starts after the current
1855 * di_next_unlinked field, then move on to the next
1856 * di_next_unlinked field.
1857 */
1858 if (next_unlinked_offset < reg_buf_offset) {
1859 continue;
1860 }
1861
1862 ASSERT(item->ri_buf[item_index].i_addr != NULL);
1863 ASSERT((item->ri_buf[item_index].i_len % XFS_BLI_CHUNK) == 0);
1864 ASSERT((reg_buf_offset + reg_buf_bytes) <= XFS_BUF_COUNT(bp));
1865
1866 /*
1867 * The current logged region contains a copy of the
1868 * current di_next_unlinked field. Extract its value
1869 * and copy it to the buffer copy.
1870 */
1871 logged_nextp = (xfs_agino_t *)
1872 ((char *)(item->ri_buf[item_index].i_addr) +
1873 (next_unlinked_offset - reg_buf_offset));
1874 if (unlikely(*logged_nextp == 0)) {
1875 xfs_fs_cmn_err(CE_ALERT, mp,
1876 "bad inode buffer log record (ptr = 0x%p, bp = 0x%p). XFS trying to replay bad (0) inode di_next_unlinked field",
1877 item, bp);
1878 XFS_ERROR_REPORT("xlog_recover_do_inode_buf",
1879 XFS_ERRLEVEL_LOW, mp);
1880 return XFS_ERROR(EFSCORRUPTED);
1881 }
1882
1883 buffer_nextp = (xfs_agino_t *)xfs_buf_offset(bp,
1884 next_unlinked_offset);
Tim Shimmin87c199c2006-06-09 14:56:16 +10001885 *buffer_nextp = *logged_nextp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
1887
1888 return 0;
1889}
1890
1891/*
1892 * Perform a 'normal' buffer recovery. Each logged region of the
1893 * buffer should be copied over the corresponding region in the
1894 * given buffer. The bitmap in the buf log format structure indicates
1895 * where to place the logged data.
1896 */
1897/*ARGSUSED*/
1898STATIC void
1899xlog_recover_do_reg_buffer(
Dave Chinner9abbc532010-04-13 15:06:46 +10001900 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 xlog_recover_item_t *item,
1902 xfs_buf_t *bp,
1903 xfs_buf_log_format_t *buf_f)
1904{
1905 int i;
1906 int bit;
1907 int nbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 unsigned int *data_map = NULL;
1909 unsigned int map_size = 0;
1910 int error;
1911
Dave Chinner9abbc532010-04-13 15:06:46 +10001912 trace_xfs_log_recover_buf_reg_buf(mp->m_log, buf_f);
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 switch (buf_f->blf_type) {
1915 case XFS_LI_BUF:
1916 data_map = buf_f->blf_data_map;
1917 map_size = buf_f->blf_map_size;
1918 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
1920 bit = 0;
1921 i = 1; /* 0 is the buf format structure */
1922 while (1) {
1923 bit = xfs_next_bit(data_map, map_size, bit);
1924 if (bit == -1)
1925 break;
1926 nbits = xfs_contig_bits(data_map, map_size, bit);
1927 ASSERT(nbits > 0);
Christoph Hellwig4b809162007-08-16 15:37:36 +10001928 ASSERT(item->ri_buf[i].i_addr != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 ASSERT(item->ri_buf[i].i_len % XFS_BLI_CHUNK == 0);
1930 ASSERT(XFS_BUF_COUNT(bp) >=
1931 ((uint)bit << XFS_BLI_SHIFT)+(nbits<<XFS_BLI_SHIFT));
1932
1933 /*
1934 * Do a sanity check if this is a dquot buffer. Just checking
1935 * the first dquot in the buffer should do. XXXThis is
1936 * probably a good thing to do for other buf types also.
1937 */
1938 error = 0;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001939 if (buf_f->blf_flags &
1940 (XFS_BLI_UDQUOT_BUF|XFS_BLI_PDQUOT_BUF|XFS_BLI_GDQUOT_BUF)) {
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02001941 if (item->ri_buf[i].i_addr == NULL) {
1942 cmn_err(CE_ALERT,
1943 "XFS: NULL dquot in %s.", __func__);
1944 goto next;
1945 }
Jan Rekorajski8ec6dba2009-11-16 11:57:02 +00001946 if (item->ri_buf[i].i_len < sizeof(xfs_disk_dquot_t)) {
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02001947 cmn_err(CE_ALERT,
1948 "XFS: dquot too small (%d) in %s.",
1949 item->ri_buf[i].i_len, __func__);
1950 goto next;
1951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 error = xfs_qm_dqcheck((xfs_disk_dquot_t *)
1953 item->ri_buf[i].i_addr,
1954 -1, 0, XFS_QMOPT_DOWARN,
1955 "dquot_buf_recover");
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02001956 if (error)
1957 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02001959
1960 memcpy(xfs_buf_offset(bp,
1961 (uint)bit << XFS_BLI_SHIFT), /* dest */
1962 item->ri_buf[i].i_addr, /* source */
1963 nbits<<XFS_BLI_SHIFT); /* length */
1964 next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 i++;
1966 bit += nbits;
1967 }
1968
1969 /* Shouldn't be any more regions */
1970 ASSERT(i == item->ri_total);
1971}
1972
1973/*
1974 * Do some primitive error checking on ondisk dquot data structures.
1975 */
1976int
1977xfs_qm_dqcheck(
1978 xfs_disk_dquot_t *ddq,
1979 xfs_dqid_t id,
1980 uint type, /* used only when IO_dorepair is true */
1981 uint flags,
1982 char *str)
1983{
1984 xfs_dqblk_t *d = (xfs_dqblk_t *)ddq;
1985 int errs = 0;
1986
1987 /*
1988 * We can encounter an uninitialized dquot buffer for 2 reasons:
1989 * 1. If we crash while deleting the quotainode(s), and those blks got
1990 * used for user data. This is because we take the path of regular
1991 * file deletion; however, the size field of quotainodes is never
1992 * updated, so all the tricks that we play in itruncate_finish
1993 * don't quite matter.
1994 *
1995 * 2. We don't play the quota buffers when there's a quotaoff logitem.
1996 * But the allocation will be replayed so we'll end up with an
1997 * uninitialized quota block.
1998 *
1999 * This is all fine; things are still consistent, and we haven't lost
2000 * any quota information. Just don't complain about bad dquot blks.
2001 */
Christoph Hellwig1149d962005-11-02 15:01:12 +11002002 if (be16_to_cpu(ddq->d_magic) != XFS_DQUOT_MAGIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 if (flags & XFS_QMOPT_DOWARN)
2004 cmn_err(CE_ALERT,
2005 "%s : XFS dquot ID 0x%x, magic 0x%x != 0x%x",
Christoph Hellwig1149d962005-11-02 15:01:12 +11002006 str, id, be16_to_cpu(ddq->d_magic), XFS_DQUOT_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 errs++;
2008 }
Christoph Hellwig1149d962005-11-02 15:01:12 +11002009 if (ddq->d_version != XFS_DQUOT_VERSION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 if (flags & XFS_QMOPT_DOWARN)
2011 cmn_err(CE_ALERT,
2012 "%s : XFS dquot ID 0x%x, version 0x%x != 0x%x",
Christoph Hellwig1149d962005-11-02 15:01:12 +11002013 str, id, ddq->d_version, XFS_DQUOT_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 errs++;
2015 }
2016
Christoph Hellwig1149d962005-11-02 15:01:12 +11002017 if (ddq->d_flags != XFS_DQ_USER &&
2018 ddq->d_flags != XFS_DQ_PROJ &&
2019 ddq->d_flags != XFS_DQ_GROUP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 if (flags & XFS_QMOPT_DOWARN)
2021 cmn_err(CE_ALERT,
2022 "%s : XFS dquot ID 0x%x, unknown flags 0x%x",
Christoph Hellwig1149d962005-11-02 15:01:12 +11002023 str, id, ddq->d_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 errs++;
2025 }
2026
Christoph Hellwig1149d962005-11-02 15:01:12 +11002027 if (id != -1 && id != be32_to_cpu(ddq->d_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if (flags & XFS_QMOPT_DOWARN)
2029 cmn_err(CE_ALERT,
2030 "%s : ondisk-dquot 0x%p, ID mismatch: "
2031 "0x%x expected, found id 0x%x",
Christoph Hellwig1149d962005-11-02 15:01:12 +11002032 str, ddq, id, be32_to_cpu(ddq->d_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 errs++;
2034 }
2035
2036 if (!errs && ddq->d_id) {
Christoph Hellwig1149d962005-11-02 15:01:12 +11002037 if (ddq->d_blk_softlimit &&
2038 be64_to_cpu(ddq->d_bcount) >=
2039 be64_to_cpu(ddq->d_blk_softlimit)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 if (!ddq->d_btimer) {
2041 if (flags & XFS_QMOPT_DOWARN)
2042 cmn_err(CE_ALERT,
2043 "%s : Dquot ID 0x%x (0x%p) "
2044 "BLK TIMER NOT STARTED",
Christoph Hellwig1149d962005-11-02 15:01:12 +11002045 str, (int)be32_to_cpu(ddq->d_id), ddq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 errs++;
2047 }
2048 }
Christoph Hellwig1149d962005-11-02 15:01:12 +11002049 if (ddq->d_ino_softlimit &&
2050 be64_to_cpu(ddq->d_icount) >=
2051 be64_to_cpu(ddq->d_ino_softlimit)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 if (!ddq->d_itimer) {
2053 if (flags & XFS_QMOPT_DOWARN)
2054 cmn_err(CE_ALERT,
2055 "%s : Dquot ID 0x%x (0x%p) "
2056 "INODE TIMER NOT STARTED",
Christoph Hellwig1149d962005-11-02 15:01:12 +11002057 str, (int)be32_to_cpu(ddq->d_id), ddq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 errs++;
2059 }
2060 }
Christoph Hellwig1149d962005-11-02 15:01:12 +11002061 if (ddq->d_rtb_softlimit &&
2062 be64_to_cpu(ddq->d_rtbcount) >=
2063 be64_to_cpu(ddq->d_rtb_softlimit)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 if (!ddq->d_rtbtimer) {
2065 if (flags & XFS_QMOPT_DOWARN)
2066 cmn_err(CE_ALERT,
2067 "%s : Dquot ID 0x%x (0x%p) "
2068 "RTBLK TIMER NOT STARTED",
Christoph Hellwig1149d962005-11-02 15:01:12 +11002069 str, (int)be32_to_cpu(ddq->d_id), ddq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 errs++;
2071 }
2072 }
2073 }
2074
2075 if (!errs || !(flags & XFS_QMOPT_DQREPAIR))
2076 return errs;
2077
2078 if (flags & XFS_QMOPT_DOWARN)
2079 cmn_err(CE_NOTE, "Re-initializing dquot ID 0x%x", id);
2080
2081 /*
2082 * Typically, a repair is only requested by quotacheck.
2083 */
2084 ASSERT(id != -1);
2085 ASSERT(flags & XFS_QMOPT_DQREPAIR);
2086 memset(d, 0, sizeof(xfs_dqblk_t));
Christoph Hellwig1149d962005-11-02 15:01:12 +11002087
2088 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
2089 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
2090 d->dd_diskdq.d_flags = type;
2091 d->dd_diskdq.d_id = cpu_to_be32(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093 return errs;
2094}
2095
2096/*
2097 * Perform a dquot buffer recovery.
2098 * Simple algorithm: if we have found a QUOTAOFF logitem of the same type
2099 * (ie. USR or GRP), then just toss this buffer away; don't recover it.
2100 * Else, treat it as a regular buffer and do recovery.
2101 */
2102STATIC void
2103xlog_recover_do_dquot_buffer(
2104 xfs_mount_t *mp,
2105 xlog_t *log,
2106 xlog_recover_item_t *item,
2107 xfs_buf_t *bp,
2108 xfs_buf_log_format_t *buf_f)
2109{
2110 uint type;
2111
Dave Chinner9abbc532010-04-13 15:06:46 +10002112 trace_xfs_log_recover_buf_dquot_buf(log, buf_f);
2113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 /*
2115 * Filesystems are required to send in quota flags at mount time.
2116 */
2117 if (mp->m_qflags == 0) {
2118 return;
2119 }
2120
2121 type = 0;
2122 if (buf_f->blf_flags & XFS_BLI_UDQUOT_BUF)
2123 type |= XFS_DQ_USER;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002124 if (buf_f->blf_flags & XFS_BLI_PDQUOT_BUF)
2125 type |= XFS_DQ_PROJ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 if (buf_f->blf_flags & XFS_BLI_GDQUOT_BUF)
2127 type |= XFS_DQ_GROUP;
2128 /*
2129 * This type of quotas was turned off, so ignore this buffer
2130 */
2131 if (log->l_quotaoffs_flag & type)
2132 return;
2133
Dave Chinner9abbc532010-04-13 15:06:46 +10002134 xlog_recover_do_reg_buffer(mp, item, bp, buf_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
2136
2137/*
2138 * This routine replays a modification made to a buffer at runtime.
2139 * There are actually two types of buffer, regular and inode, which
2140 * are handled differently. Inode buffers are handled differently
2141 * in that we only recover a specific set of data from them, namely
2142 * the inode di_next_unlinked fields. This is because all other inode
2143 * data is actually logged via inode records and any data we replay
2144 * here which overlaps that may be stale.
2145 *
2146 * When meta-data buffers are freed at run time we log a buffer item
2147 * with the XFS_BLI_CANCEL bit set to indicate that previous copies
2148 * of the buffer in the log should not be replayed at recovery time.
2149 * This is so that if the blocks covered by the buffer are reused for
2150 * file data before we crash we don't end up replaying old, freed
2151 * meta-data into a user's file.
2152 *
2153 * To handle the cancellation of buffer log items, we make two passes
2154 * over the log during recovery. During the first we build a table of
2155 * those buffers which have been cancelled, and during the second we
2156 * only replay those buffers which do not have corresponding cancel
2157 * records in the table. See xlog_recover_do_buffer_pass[1,2] above
2158 * for more details on the implementation of the table of cancel records.
2159 */
2160STATIC int
2161xlog_recover_do_buffer_trans(
2162 xlog_t *log,
2163 xlog_recover_item_t *item,
2164 int pass)
2165{
2166 xfs_buf_log_format_t *buf_f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 xfs_mount_t *mp;
2168 xfs_buf_t *bp;
2169 int error;
2170 int cancel;
2171 xfs_daddr_t blkno;
2172 int len;
2173 ushort flags;
Christoph Hellwig6ad112b2009-11-24 18:02:23 +00002174 uint buf_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
2176 buf_f = (xfs_buf_log_format_t *)item->ri_buf[0].i_addr;
2177
2178 if (pass == XLOG_RECOVER_PASS1) {
2179 /*
2180 * In this pass we're only looking for buf items
2181 * with the XFS_BLI_CANCEL bit set.
2182 */
2183 xlog_recover_do_buffer_pass1(log, buf_f);
2184 return 0;
2185 } else {
2186 /*
2187 * In this pass we want to recover all the buffers
2188 * which have not been cancelled and are not
2189 * cancellation buffers themselves. The routine
2190 * we call here will tell us whether or not to
2191 * continue with the replay of this buffer.
2192 */
2193 cancel = xlog_recover_do_buffer_pass2(log, buf_f);
2194 if (cancel) {
Dave Chinner9abbc532010-04-13 15:06:46 +10002195 trace_xfs_log_recover_buf_cancel(log, buf_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 return 0;
2197 }
2198 }
Dave Chinner9abbc532010-04-13 15:06:46 +10002199 trace_xfs_log_recover_buf_recover(log, buf_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 switch (buf_f->blf_type) {
2201 case XFS_LI_BUF:
2202 blkno = buf_f->blf_blkno;
2203 len = buf_f->blf_len;
2204 flags = buf_f->blf_flags;
2205 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 default:
2207 xfs_fs_cmn_err(CE_ALERT, log->l_mp,
Nathan Scottfc1f8c12005-11-02 11:44:33 +11002208 "xfs_log_recover: unknown buffer type 0x%x, logdev %s",
2209 buf_f->blf_type, log->l_mp->m_logname ?
2210 log->l_mp->m_logname : "internal");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 XFS_ERROR_REPORT("xlog_recover_do_buffer_trans",
2212 XFS_ERRLEVEL_LOW, log->l_mp);
2213 return XFS_ERROR(EFSCORRUPTED);
2214 }
2215
2216 mp = log->l_mp;
Christoph Hellwig0cadda12010-01-19 09:56:44 +00002217 buf_flags = XBF_LOCK;
Christoph Hellwig6ad112b2009-11-24 18:02:23 +00002218 if (!(flags & XFS_BLI_INODE_BUF))
Christoph Hellwig0cadda12010-01-19 09:56:44 +00002219 buf_flags |= XBF_MAPPED;
Christoph Hellwig6ad112b2009-11-24 18:02:23 +00002220
2221 bp = xfs_buf_read(mp->m_ddev_targp, blkno, len, buf_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 if (XFS_BUF_ISERROR(bp)) {
2223 xfs_ioerror_alert("xlog_recover_do..(read#1)", log->l_mp,
2224 bp, blkno);
2225 error = XFS_BUF_GETERROR(bp);
2226 xfs_buf_relse(bp);
2227 return error;
2228 }
2229
2230 error = 0;
2231 if (flags & XFS_BLI_INODE_BUF) {
2232 error = xlog_recover_do_inode_buffer(mp, item, bp, buf_f);
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002233 } else if (flags &
2234 (XFS_BLI_UDQUOT_BUF|XFS_BLI_PDQUOT_BUF|XFS_BLI_GDQUOT_BUF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 xlog_recover_do_dquot_buffer(mp, log, item, bp, buf_f);
2236 } else {
Dave Chinner9abbc532010-04-13 15:06:46 +10002237 xlog_recover_do_reg_buffer(mp, item, bp, buf_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 }
2239 if (error)
2240 return XFS_ERROR(error);
2241
2242 /*
2243 * Perform delayed write on the buffer. Asynchronous writes will be
2244 * slower when taking into account all the buffers to be flushed.
2245 *
2246 * Also make sure that only inode buffers with good sizes stay in
2247 * the buffer cache. The kernel moves inodes in buffers of 1 block
2248 * or XFS_INODE_CLUSTER_SIZE bytes, whichever is bigger. The inode
2249 * buffers in the log can be a different size if the log was generated
2250 * by an older kernel using unclustered inode buffers or a newer kernel
2251 * running with a different inode cluster size. Regardless, if the
2252 * the inode buffer size isn't MAX(blocksize, XFS_INODE_CLUSTER_SIZE)
2253 * for *our* value of XFS_INODE_CLUSTER_SIZE, then we need to keep
2254 * the buffer out of the buffer cache so that the buffer won't
2255 * overlap with future reads of those inodes.
2256 */
2257 if (XFS_DINODE_MAGIC ==
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002258 be16_to_cpu(*((__be16 *)xfs_buf_offset(bp, 0))) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 (XFS_BUF_COUNT(bp) != MAX(log->l_mp->m_sb.sb_blocksize,
2260 (__uint32_t)XFS_INODE_CLUSTER_SIZE(log->l_mp)))) {
2261 XFS_BUF_STALE(bp);
2262 error = xfs_bwrite(mp, bp);
2263 } else {
Christoph Hellwig15ac08a2008-12-09 04:47:30 -05002264 ASSERT(bp->b_mount == NULL || bp->b_mount == mp);
2265 bp->b_mount = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 XFS_BUF_SET_IODONE_FUNC(bp, xlog_recover_iodone);
2267 xfs_bdwrite(mp, bp);
2268 }
2269
2270 return (error);
2271}
2272
2273STATIC int
2274xlog_recover_do_inode_trans(
2275 xlog_t *log,
2276 xlog_recover_item_t *item,
2277 int pass)
2278{
2279 xfs_inode_log_format_t *in_f;
2280 xfs_mount_t *mp;
2281 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 xfs_dinode_t *dip;
2283 xfs_ino_t ino;
2284 int len;
2285 xfs_caddr_t src;
2286 xfs_caddr_t dest;
2287 int error;
2288 int attr_index;
2289 uint fields;
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002290 xfs_icdinode_t *dicp;
Tim Shimmin6d192a92006-06-09 14:55:38 +10002291 int need_free = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
2293 if (pass == XLOG_RECOVER_PASS1) {
2294 return 0;
2295 }
2296
Tim Shimmin6d192a92006-06-09 14:55:38 +10002297 if (item->ri_buf[0].i_len == sizeof(xfs_inode_log_format_t)) {
2298 in_f = (xfs_inode_log_format_t *)item->ri_buf[0].i_addr;
2299 } else {
2300 in_f = (xfs_inode_log_format_t *)kmem_alloc(
2301 sizeof(xfs_inode_log_format_t), KM_SLEEP);
2302 need_free = 1;
2303 error = xfs_inode_item_format_convert(&item->ri_buf[0], in_f);
2304 if (error)
2305 goto error;
2306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 ino = in_f->ilf_ino;
2308 mp = log->l_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
2310 /*
2311 * Inode buffers can be freed, look out for it,
2312 * and do not replay the inode.
2313 */
Christoph Hellwiga1941892008-11-28 14:23:40 +11002314 if (xlog_check_buffer_cancelled(log, in_f->ilf_blkno,
2315 in_f->ilf_len, 0)) {
Tim Shimmin6d192a92006-06-09 14:55:38 +10002316 error = 0;
Dave Chinner9abbc532010-04-13 15:06:46 +10002317 trace_xfs_log_recover_inode_cancel(log, in_f);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002318 goto error;
2319 }
Dave Chinner9abbc532010-04-13 15:06:46 +10002320 trace_xfs_log_recover_inode_recover(log, in_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Christoph Hellwig6ad112b2009-11-24 18:02:23 +00002322 bp = xfs_buf_read(mp->m_ddev_targp, in_f->ilf_blkno, in_f->ilf_len,
Christoph Hellwig0cadda12010-01-19 09:56:44 +00002323 XBF_LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 if (XFS_BUF_ISERROR(bp)) {
2325 xfs_ioerror_alert("xlog_recover_do..(read#2)", mp,
Christoph Hellwiga1941892008-11-28 14:23:40 +11002326 bp, in_f->ilf_blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 error = XFS_BUF_GETERROR(bp);
2328 xfs_buf_relse(bp);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002329 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 }
2331 error = 0;
2332 ASSERT(in_f->ilf_fields & XFS_ILOG_CORE);
Christoph Hellwiga1941892008-11-28 14:23:40 +11002333 dip = (xfs_dinode_t *)xfs_buf_offset(bp, in_f->ilf_boffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335 /*
2336 * Make sure the place we're flushing out to really looks
2337 * like an inode!
2338 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002339 if (unlikely(be16_to_cpu(dip->di_magic) != XFS_DINODE_MAGIC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 xfs_buf_relse(bp);
2341 xfs_fs_cmn_err(CE_ALERT, mp,
2342 "xfs_inode_recover: Bad inode magic number, dino ptr = 0x%p, dino bp = 0x%p, ino = %Ld",
2343 dip, bp, ino);
2344 XFS_ERROR_REPORT("xlog_recover_do_inode_trans(1)",
2345 XFS_ERRLEVEL_LOW, mp);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002346 error = EFSCORRUPTED;
2347 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 }
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002349 dicp = (xfs_icdinode_t *)(item->ri_buf[1].i_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 if (unlikely(dicp->di_magic != XFS_DINODE_MAGIC)) {
2351 xfs_buf_relse(bp);
2352 xfs_fs_cmn_err(CE_ALERT, mp,
2353 "xfs_inode_recover: Bad inode log record, rec ptr 0x%p, ino %Ld",
2354 item, ino);
2355 XFS_ERROR_REPORT("xlog_recover_do_inode_trans(2)",
2356 XFS_ERRLEVEL_LOW, mp);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002357 error = EFSCORRUPTED;
2358 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 }
2360
2361 /* Skip replay when the on disk inode is newer than the log one */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002362 if (dicp->di_flushiter < be16_to_cpu(dip->di_flushiter)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 /*
2364 * Deal with the wrap case, DI_MAX_FLUSH is less
2365 * than smaller numbers
2366 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002367 if (be16_to_cpu(dip->di_flushiter) == DI_MAX_FLUSH &&
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002368 dicp->di_flushiter < (DI_MAX_FLUSH >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 /* do nothing */
2370 } else {
2371 xfs_buf_relse(bp);
Dave Chinner9abbc532010-04-13 15:06:46 +10002372 trace_xfs_log_recover_inode_skip(log, in_f);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002373 error = 0;
2374 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 }
2376 }
2377 /* Take the opportunity to reset the flush iteration count */
2378 dicp->di_flushiter = 0;
2379
2380 if (unlikely((dicp->di_mode & S_IFMT) == S_IFREG)) {
2381 if ((dicp->di_format != XFS_DINODE_FMT_EXTENTS) &&
2382 (dicp->di_format != XFS_DINODE_FMT_BTREE)) {
2383 XFS_CORRUPTION_ERROR("xlog_recover_do_inode_trans(3)",
2384 XFS_ERRLEVEL_LOW, mp, dicp);
2385 xfs_buf_relse(bp);
2386 xfs_fs_cmn_err(CE_ALERT, mp,
2387 "xfs_inode_recover: Bad regular inode log record, rec ptr 0x%p, ino ptr = 0x%p, ino bp = 0x%p, ino %Ld",
2388 item, dip, bp, ino);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002389 error = EFSCORRUPTED;
2390 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 }
2392 } else if (unlikely((dicp->di_mode & S_IFMT) == S_IFDIR)) {
2393 if ((dicp->di_format != XFS_DINODE_FMT_EXTENTS) &&
2394 (dicp->di_format != XFS_DINODE_FMT_BTREE) &&
2395 (dicp->di_format != XFS_DINODE_FMT_LOCAL)) {
2396 XFS_CORRUPTION_ERROR("xlog_recover_do_inode_trans(4)",
2397 XFS_ERRLEVEL_LOW, mp, dicp);
2398 xfs_buf_relse(bp);
2399 xfs_fs_cmn_err(CE_ALERT, mp,
2400 "xfs_inode_recover: Bad dir inode log record, rec ptr 0x%p, ino ptr = 0x%p, ino bp = 0x%p, ino %Ld",
2401 item, dip, bp, ino);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002402 error = EFSCORRUPTED;
2403 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 }
2405 }
2406 if (unlikely(dicp->di_nextents + dicp->di_anextents > dicp->di_nblocks)){
2407 XFS_CORRUPTION_ERROR("xlog_recover_do_inode_trans(5)",
2408 XFS_ERRLEVEL_LOW, mp, dicp);
2409 xfs_buf_relse(bp);
2410 xfs_fs_cmn_err(CE_ALERT, mp,
2411 "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",
2412 item, dip, bp, ino,
2413 dicp->di_nextents + dicp->di_anextents,
2414 dicp->di_nblocks);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002415 error = EFSCORRUPTED;
2416 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 }
2418 if (unlikely(dicp->di_forkoff > mp->m_sb.sb_inodesize)) {
2419 XFS_CORRUPTION_ERROR("xlog_recover_do_inode_trans(6)",
2420 XFS_ERRLEVEL_LOW, mp, dicp);
2421 xfs_buf_relse(bp);
2422 xfs_fs_cmn_err(CE_ALERT, mp,
2423 "xfs_inode_recover: Bad inode log rec ptr 0x%p, dino ptr 0x%p, dino bp 0x%p, ino %Ld, forkoff 0x%x",
2424 item, dip, bp, ino, dicp->di_forkoff);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002425 error = EFSCORRUPTED;
2426 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 }
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002428 if (unlikely(item->ri_buf[1].i_len > sizeof(struct xfs_icdinode))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 XFS_CORRUPTION_ERROR("xlog_recover_do_inode_trans(7)",
2430 XFS_ERRLEVEL_LOW, mp, dicp);
2431 xfs_buf_relse(bp);
2432 xfs_fs_cmn_err(CE_ALERT, mp,
2433 "xfs_inode_recover: Bad inode log record length %d, rec ptr 0x%p",
2434 item->ri_buf[1].i_len, item);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002435 error = EFSCORRUPTED;
2436 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 }
2438
2439 /* The core is in in-core format */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002440 xfs_dinode_to_disk(dip, (xfs_icdinode_t *)item->ri_buf[1].i_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
2442 /* the rest is in on-disk format */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002443 if (item->ri_buf[1].i_len > sizeof(struct xfs_icdinode)) {
2444 memcpy((xfs_caddr_t) dip + sizeof(struct xfs_icdinode),
2445 item->ri_buf[1].i_addr + sizeof(struct xfs_icdinode),
2446 item->ri_buf[1].i_len - sizeof(struct xfs_icdinode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 }
2448
2449 fields = in_f->ilf_fields;
2450 switch (fields & (XFS_ILOG_DEV | XFS_ILOG_UUID)) {
2451 case XFS_ILOG_DEV:
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002452 xfs_dinode_put_rdev(dip, in_f->ilf_u.ilfu_rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 break;
2454 case XFS_ILOG_UUID:
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002455 memcpy(XFS_DFORK_DPTR(dip),
2456 &in_f->ilf_u.ilfu_uuid,
2457 sizeof(uuid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 break;
2459 }
2460
2461 if (in_f->ilf_size == 2)
2462 goto write_inode_buffer;
2463 len = item->ri_buf[2].i_len;
2464 src = item->ri_buf[2].i_addr;
2465 ASSERT(in_f->ilf_size <= 4);
2466 ASSERT((in_f->ilf_size == 3) || (fields & XFS_ILOG_AFORK));
2467 ASSERT(!(fields & XFS_ILOG_DFORK) ||
2468 (len == in_f->ilf_dsize));
2469
2470 switch (fields & XFS_ILOG_DFORK) {
2471 case XFS_ILOG_DDATA:
2472 case XFS_ILOG_DEXT:
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002473 memcpy(XFS_DFORK_DPTR(dip), src, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 break;
2475
2476 case XFS_ILOG_DBROOT:
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002477 xfs_bmbt_to_bmdr(mp, (struct xfs_btree_block *)src, len,
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002478 (xfs_bmdr_block_t *)XFS_DFORK_DPTR(dip),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 XFS_DFORK_DSIZE(dip, mp));
2480 break;
2481
2482 default:
2483 /*
2484 * There are no data fork flags set.
2485 */
2486 ASSERT((fields & XFS_ILOG_DFORK) == 0);
2487 break;
2488 }
2489
2490 /*
2491 * If we logged any attribute data, recover it. There may or
2492 * may not have been any other non-core data logged in this
2493 * transaction.
2494 */
2495 if (in_f->ilf_fields & XFS_ILOG_AFORK) {
2496 if (in_f->ilf_fields & XFS_ILOG_DFORK) {
2497 attr_index = 3;
2498 } else {
2499 attr_index = 2;
2500 }
2501 len = item->ri_buf[attr_index].i_len;
2502 src = item->ri_buf[attr_index].i_addr;
2503 ASSERT(len == in_f->ilf_asize);
2504
2505 switch (in_f->ilf_fields & XFS_ILOG_AFORK) {
2506 case XFS_ILOG_ADATA:
2507 case XFS_ILOG_AEXT:
2508 dest = XFS_DFORK_APTR(dip);
2509 ASSERT(len <= XFS_DFORK_ASIZE(dip, mp));
2510 memcpy(dest, src, len);
2511 break;
2512
2513 case XFS_ILOG_ABROOT:
2514 dest = XFS_DFORK_APTR(dip);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002515 xfs_bmbt_to_bmdr(mp, (struct xfs_btree_block *)src,
2516 len, (xfs_bmdr_block_t*)dest,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 XFS_DFORK_ASIZE(dip, mp));
2518 break;
2519
2520 default:
2521 xlog_warn("XFS: xlog_recover_do_inode_trans: Invalid flag");
2522 ASSERT(0);
2523 xfs_buf_relse(bp);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002524 error = EIO;
2525 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 }
2527 }
2528
2529write_inode_buffer:
Christoph Hellwigdd0bbad2009-03-16 08:19:59 +01002530 ASSERT(bp->b_mount == NULL || bp->b_mount == mp);
2531 bp->b_mount = mp;
2532 XFS_BUF_SET_IODONE_FUNC(bp, xlog_recover_iodone);
2533 xfs_bdwrite(mp, bp);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002534error:
2535 if (need_free)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002536 kmem_free(in_f);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002537 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538}
2539
2540/*
2541 * Recover QUOTAOFF records. We simply make a note of it in the xlog_t
2542 * structure, so that we know not to do any dquot item or dquot buffer recovery,
2543 * of that type.
2544 */
2545STATIC int
2546xlog_recover_do_quotaoff_trans(
2547 xlog_t *log,
2548 xlog_recover_item_t *item,
2549 int pass)
2550{
2551 xfs_qoff_logformat_t *qoff_f;
2552
2553 if (pass == XLOG_RECOVER_PASS2) {
2554 return (0);
2555 }
2556
2557 qoff_f = (xfs_qoff_logformat_t *)item->ri_buf[0].i_addr;
2558 ASSERT(qoff_f);
2559
2560 /*
2561 * The logitem format's flag tells us if this was user quotaoff,
Nathan Scott77a7cce2006-01-11 15:35:57 +11002562 * group/project quotaoff or both.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 */
2564 if (qoff_f->qf_flags & XFS_UQUOTA_ACCT)
2565 log->l_quotaoffs_flag |= XFS_DQ_USER;
Nathan Scott77a7cce2006-01-11 15:35:57 +11002566 if (qoff_f->qf_flags & XFS_PQUOTA_ACCT)
2567 log->l_quotaoffs_flag |= XFS_DQ_PROJ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 if (qoff_f->qf_flags & XFS_GQUOTA_ACCT)
2569 log->l_quotaoffs_flag |= XFS_DQ_GROUP;
2570
2571 return (0);
2572}
2573
2574/*
2575 * Recover a dquot record
2576 */
2577STATIC int
2578xlog_recover_do_dquot_trans(
2579 xlog_t *log,
2580 xlog_recover_item_t *item,
2581 int pass)
2582{
2583 xfs_mount_t *mp;
2584 xfs_buf_t *bp;
2585 struct xfs_disk_dquot *ddq, *recddq;
2586 int error;
2587 xfs_dq_logformat_t *dq_f;
2588 uint type;
2589
2590 if (pass == XLOG_RECOVER_PASS1) {
2591 return 0;
2592 }
2593 mp = log->l_mp;
2594
2595 /*
2596 * Filesystems are required to send in quota flags at mount time.
2597 */
2598 if (mp->m_qflags == 0)
2599 return (0);
2600
2601 recddq = (xfs_disk_dquot_t *)item->ri_buf[1].i_addr;
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02002602
2603 if (item->ri_buf[1].i_addr == NULL) {
2604 cmn_err(CE_ALERT,
2605 "XFS: NULL dquot in %s.", __func__);
2606 return XFS_ERROR(EIO);
2607 }
Jan Rekorajski8ec6dba2009-11-16 11:57:02 +00002608 if (item->ri_buf[1].i_len < sizeof(xfs_disk_dquot_t)) {
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02002609 cmn_err(CE_ALERT,
2610 "XFS: dquot too small (%d) in %s.",
2611 item->ri_buf[1].i_len, __func__);
2612 return XFS_ERROR(EIO);
2613 }
2614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 /*
2616 * This type of quotas was turned off, so ignore this record.
2617 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002618 type = recddq->d_flags & (XFS_DQ_USER | XFS_DQ_PROJ | XFS_DQ_GROUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 ASSERT(type);
2620 if (log->l_quotaoffs_flag & type)
2621 return (0);
2622
2623 /*
2624 * At this point we know that quota was _not_ turned off.
2625 * Since the mount flags are not indicating to us otherwise, this
2626 * must mean that quota is on, and the dquot needs to be replayed.
2627 * Remember that we may not have fully recovered the superblock yet,
2628 * so we can't do the usual trick of looking at the SB quota bits.
2629 *
2630 * The other possibility, of course, is that the quota subsystem was
2631 * removed since the last mount - ENOSYS.
2632 */
2633 dq_f = (xfs_dq_logformat_t *)item->ri_buf[0].i_addr;
2634 ASSERT(dq_f);
2635 if ((error = xfs_qm_dqcheck(recddq,
2636 dq_f->qlf_id,
2637 0, XFS_QMOPT_DOWARN,
2638 "xlog_recover_do_dquot_trans (log copy)"))) {
2639 return XFS_ERROR(EIO);
2640 }
2641 ASSERT(dq_f->qlf_len == 1);
2642
2643 error = xfs_read_buf(mp, mp->m_ddev_targp,
2644 dq_f->qlf_blkno,
2645 XFS_FSB_TO_BB(mp, dq_f->qlf_len),
2646 0, &bp);
2647 if (error) {
2648 xfs_ioerror_alert("xlog_recover_do..(read#3)", mp,
2649 bp, dq_f->qlf_blkno);
2650 return error;
2651 }
2652 ASSERT(bp);
2653 ddq = (xfs_disk_dquot_t *)xfs_buf_offset(bp, dq_f->qlf_boffset);
2654
2655 /*
2656 * At least the magic num portion should be on disk because this
2657 * was among a chunk of dquots created earlier, and we did some
2658 * minimal initialization then.
2659 */
2660 if (xfs_qm_dqcheck(ddq, dq_f->qlf_id, 0, XFS_QMOPT_DOWARN,
2661 "xlog_recover_do_dquot_trans")) {
2662 xfs_buf_relse(bp);
2663 return XFS_ERROR(EIO);
2664 }
2665
2666 memcpy(ddq, recddq, item->ri_buf[1].i_len);
2667
2668 ASSERT(dq_f->qlf_size == 2);
Christoph Hellwig15ac08a2008-12-09 04:47:30 -05002669 ASSERT(bp->b_mount == NULL || bp->b_mount == mp);
2670 bp->b_mount = mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 XFS_BUF_SET_IODONE_FUNC(bp, xlog_recover_iodone);
2672 xfs_bdwrite(mp, bp);
2673
2674 return (0);
2675}
2676
2677/*
2678 * This routine is called to create an in-core extent free intent
2679 * item from the efi format structure which was logged on disk.
2680 * It allocates an in-core efi, copies the extents from the format
2681 * structure into it, and adds the efi to the AIL with the given
2682 * LSN.
2683 */
Tim Shimmin6d192a92006-06-09 14:55:38 +10002684STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685xlog_recover_do_efi_trans(
2686 xlog_t *log,
2687 xlog_recover_item_t *item,
2688 xfs_lsn_t lsn,
2689 int pass)
2690{
Tim Shimmin6d192a92006-06-09 14:55:38 +10002691 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 xfs_mount_t *mp;
2693 xfs_efi_log_item_t *efip;
2694 xfs_efi_log_format_t *efi_formatp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
2696 if (pass == XLOG_RECOVER_PASS1) {
Tim Shimmin6d192a92006-06-09 14:55:38 +10002697 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 }
2699
2700 efi_formatp = (xfs_efi_log_format_t *)item->ri_buf[0].i_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
2702 mp = log->l_mp;
2703 efip = xfs_efi_init(mp, efi_formatp->efi_nextents);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002704 if ((error = xfs_efi_copy_format(&(item->ri_buf[0]),
2705 &(efip->efi_format)))) {
2706 xfs_efi_item_free(efip);
2707 return error;
2708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 efip->efi_next_extent = efi_formatp->efi_nextents;
2710 efip->efi_flags |= XFS_EFI_COMMITTED;
2711
David Chinnera9c21c12008-10-30 17:39:35 +11002712 spin_lock(&log->l_ailp->xa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 /*
David Chinner783a2f62008-10-30 17:39:58 +11002714 * xfs_trans_ail_update() drops the AIL lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 */
David Chinner783a2f62008-10-30 17:39:58 +11002716 xfs_trans_ail_update(log->l_ailp, (xfs_log_item_t *)efip, lsn);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002717 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718}
2719
2720
2721/*
2722 * This routine is called when an efd format structure is found in
2723 * a committed transaction in the log. It's purpose is to cancel
2724 * the corresponding efi if it was still in the log. To do this
2725 * it searches the AIL for the efi with an id equal to that in the
2726 * efd format structure. If we find it, we remove the efi from the
2727 * AIL and free it.
2728 */
2729STATIC void
2730xlog_recover_do_efd_trans(
2731 xlog_t *log,
2732 xlog_recover_item_t *item,
2733 int pass)
2734{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 xfs_efd_log_format_t *efd_formatp;
2736 xfs_efi_log_item_t *efip = NULL;
2737 xfs_log_item_t *lip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 __uint64_t efi_id;
David Chinner27d8d5f2008-10-30 17:38:39 +11002739 struct xfs_ail_cursor cur;
David Chinner783a2f62008-10-30 17:39:58 +11002740 struct xfs_ail *ailp = log->l_ailp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
2742 if (pass == XLOG_RECOVER_PASS1) {
2743 return;
2744 }
2745
2746 efd_formatp = (xfs_efd_log_format_t *)item->ri_buf[0].i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +10002747 ASSERT((item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_32_t) +
2748 ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_32_t)))) ||
2749 (item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_64_t) +
2750 ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_64_t)))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 efi_id = efd_formatp->efd_efi_id;
2752
2753 /*
2754 * Search for the efi with the id in the efd format structure
2755 * in the AIL.
2756 */
David Chinnera9c21c12008-10-30 17:39:35 +11002757 spin_lock(&ailp->xa_lock);
2758 lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 while (lip != NULL) {
2760 if (lip->li_type == XFS_LI_EFI) {
2761 efip = (xfs_efi_log_item_t *)lip;
2762 if (efip->efi_format.efi_id == efi_id) {
2763 /*
David Chinner783a2f62008-10-30 17:39:58 +11002764 * xfs_trans_ail_delete() drops the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 * AIL lock.
2766 */
David Chinner783a2f62008-10-30 17:39:58 +11002767 xfs_trans_ail_delete(ailp, lip);
David Chinner8ae2c0f2007-11-23 16:28:17 +11002768 xfs_efi_item_free(efip);
David Chinnera9c21c12008-10-30 17:39:35 +11002769 spin_lock(&ailp->xa_lock);
David Chinner27d8d5f2008-10-30 17:38:39 +11002770 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 }
2772 }
David Chinnera9c21c12008-10-30 17:39:35 +11002773 lip = xfs_trans_ail_cursor_next(ailp, &cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 }
David Chinnera9c21c12008-10-30 17:39:35 +11002775 xfs_trans_ail_cursor_done(ailp, &cur);
2776 spin_unlock(&ailp->xa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777}
2778
2779/*
2780 * Perform the transaction
2781 *
2782 * If the transaction modifies a buffer or inode, do it now. Otherwise,
2783 * EFIs and EFDs get queued up by adding entries into the AIL for them.
2784 */
2785STATIC int
2786xlog_recover_do_trans(
2787 xlog_t *log,
2788 xlog_recover_t *trans,
2789 int pass)
2790{
2791 int error = 0;
Dave Chinnerf0a76952010-01-11 11:49:57 +00002792 xlog_recover_item_t *item;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
Dave Chinner9abbc532010-04-13 15:06:46 +10002794 error = xlog_recover_reorder_trans(log, trans, pass);
Christoph Hellwigff0205e2009-03-16 08:20:52 +01002795 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 return error;
Christoph Hellwigff0205e2009-03-16 08:20:52 +01002797
Dave Chinnerf0a76952010-01-11 11:49:57 +00002798 list_for_each_entry(item, &trans->r_itemq, ri_list) {
Dave Chinner9abbc532010-04-13 15:06:46 +10002799 trace_xfs_log_recover_item_recover(log, trans, item, pass);
Christoph Hellwigff0205e2009-03-16 08:20:52 +01002800 switch (ITEM_TYPE(item)) {
2801 case XFS_LI_BUF:
2802 error = xlog_recover_do_buffer_trans(log, item, pass);
2803 break;
2804 case XFS_LI_INODE:
2805 error = xlog_recover_do_inode_trans(log, item, pass);
2806 break;
2807 case XFS_LI_EFI:
2808 error = xlog_recover_do_efi_trans(log, item,
2809 trans->r_lsn, pass);
2810 break;
2811 case XFS_LI_EFD:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 xlog_recover_do_efd_trans(log, item, pass);
Christoph Hellwigff0205e2009-03-16 08:20:52 +01002813 error = 0;
2814 break;
2815 case XFS_LI_DQUOT:
2816 error = xlog_recover_do_dquot_trans(log, item, pass);
2817 break;
2818 case XFS_LI_QUOTAOFF:
2819 error = xlog_recover_do_quotaoff_trans(log, item,
2820 pass);
2821 break;
2822 default:
2823 xlog_warn(
2824 "XFS: invalid item type (%d) xlog_recover_do_trans", ITEM_TYPE(item));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 ASSERT(0);
2826 error = XFS_ERROR(EIO);
2827 break;
2828 }
Christoph Hellwigff0205e2009-03-16 08:20:52 +01002829
2830 if (error)
2831 return error;
Dave Chinnerf0a76952010-01-11 11:49:57 +00002832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
Christoph Hellwigff0205e2009-03-16 08:20:52 +01002834 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835}
2836
2837/*
2838 * Free up any resources allocated by the transaction
2839 *
2840 * Remember that EFIs, EFDs, and IUNLINKs are handled later.
2841 */
2842STATIC void
2843xlog_recover_free_trans(
2844 xlog_recover_t *trans)
2845{
Dave Chinnerf0a76952010-01-11 11:49:57 +00002846 xlog_recover_item_t *item, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 int i;
2848
Dave Chinnerf0a76952010-01-11 11:49:57 +00002849 list_for_each_entry_safe(item, n, &trans->r_itemq, ri_list) {
2850 /* Free the regions in the item. */
2851 list_del(&item->ri_list);
2852 for (i = 0; i < item->ri_cnt; i++)
2853 kmem_free(item->ri_buf[i].i_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 /* Free the item itself */
Dave Chinnerf0a76952010-01-11 11:49:57 +00002855 kmem_free(item->ri_buf);
2856 kmem_free(item);
2857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 /* Free the transaction recover structure */
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002859 kmem_free(trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860}
2861
2862STATIC int
2863xlog_recover_commit_trans(
2864 xlog_t *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 xlog_recover_t *trans,
2866 int pass)
2867{
2868 int error;
2869
Dave Chinnerf0a76952010-01-11 11:49:57 +00002870 hlist_del(&trans->r_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 if ((error = xlog_recover_do_trans(log, trans, pass)))
2872 return error;
2873 xlog_recover_free_trans(trans); /* no error */
2874 return 0;
2875}
2876
2877STATIC int
2878xlog_recover_unmount_trans(
2879 xlog_recover_t *trans)
2880{
2881 /* Do nothing now */
2882 xlog_warn("XFS: xlog_recover_unmount_trans: Unmount LR");
2883 return 0;
2884}
2885
2886/*
2887 * There are two valid states of the r_state field. 0 indicates that the
2888 * transaction structure is in a normal state. We have either seen the
2889 * start of the transaction or the last operation we added was not a partial
2890 * operation. If the last operation we added to the transaction was a
2891 * partial operation, we need to mark r_state with XLOG_WAS_CONT_TRANS.
2892 *
2893 * NOTE: skip LRs with 0 data length.
2894 */
2895STATIC int
2896xlog_recover_process_data(
2897 xlog_t *log,
Dave Chinnerf0a76952010-01-11 11:49:57 +00002898 struct hlist_head rhash[],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 xlog_rec_header_t *rhead,
2900 xfs_caddr_t dp,
2901 int pass)
2902{
2903 xfs_caddr_t lp;
2904 int num_logops;
2905 xlog_op_header_t *ohead;
2906 xlog_recover_t *trans;
2907 xlog_tid_t tid;
2908 int error;
2909 unsigned long hash;
2910 uint flags;
2911
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002912 lp = dp + be32_to_cpu(rhead->h_len);
2913 num_logops = be32_to_cpu(rhead->h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
2915 /* check the log format matches our own - else we can't recover */
2916 if (xlog_header_check_recover(log->l_mp, rhead))
2917 return (XFS_ERROR(EIO));
2918
2919 while ((dp < lp) && num_logops) {
2920 ASSERT(dp + sizeof(xlog_op_header_t) <= lp);
2921 ohead = (xlog_op_header_t *)dp;
2922 dp += sizeof(xlog_op_header_t);
2923 if (ohead->oh_clientid != XFS_TRANSACTION &&
2924 ohead->oh_clientid != XFS_LOG) {
2925 xlog_warn(
2926 "XFS: xlog_recover_process_data: bad clientid");
2927 ASSERT(0);
2928 return (XFS_ERROR(EIO));
2929 }
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10002930 tid = be32_to_cpu(ohead->oh_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 hash = XLOG_RHASH(tid);
Dave Chinnerf0a76952010-01-11 11:49:57 +00002932 trans = xlog_recover_find_tid(&rhash[hash], tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 if (trans == NULL) { /* not found; add new tid */
2934 if (ohead->oh_flags & XLOG_START_TRANS)
2935 xlog_recover_new_tid(&rhash[hash], tid,
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002936 be64_to_cpu(rhead->h_lsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 } else {
Lachlan McIlroy9742bb92008-01-10 16:43:36 +11002938 if (dp + be32_to_cpu(ohead->oh_len) > lp) {
2939 xlog_warn(
2940 "XFS: xlog_recover_process_data: bad length");
2941 WARN_ON(1);
2942 return (XFS_ERROR(EIO));
2943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 flags = ohead->oh_flags & ~XLOG_END_TRANS;
2945 if (flags & XLOG_WAS_CONT_TRANS)
2946 flags &= ~XLOG_CONTINUE_TRANS;
2947 switch (flags) {
2948 case XLOG_COMMIT_TRANS:
2949 error = xlog_recover_commit_trans(log,
Dave Chinnerf0a76952010-01-11 11:49:57 +00002950 trans, pass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 break;
2952 case XLOG_UNMOUNT_TRANS:
2953 error = xlog_recover_unmount_trans(trans);
2954 break;
2955 case XLOG_WAS_CONT_TRANS:
Dave Chinner9abbc532010-04-13 15:06:46 +10002956 error = xlog_recover_add_to_cont_trans(log,
2957 trans, dp,
2958 be32_to_cpu(ohead->oh_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 break;
2960 case XLOG_START_TRANS:
2961 xlog_warn(
2962 "XFS: xlog_recover_process_data: bad transaction");
2963 ASSERT(0);
2964 error = XFS_ERROR(EIO);
2965 break;
2966 case 0:
2967 case XLOG_CONTINUE_TRANS:
Dave Chinner9abbc532010-04-13 15:06:46 +10002968 error = xlog_recover_add_to_trans(log, trans,
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10002969 dp, be32_to_cpu(ohead->oh_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 break;
2971 default:
2972 xlog_warn(
2973 "XFS: xlog_recover_process_data: bad flag");
2974 ASSERT(0);
2975 error = XFS_ERROR(EIO);
2976 break;
2977 }
2978 if (error)
2979 return error;
2980 }
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10002981 dp += be32_to_cpu(ohead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 num_logops--;
2983 }
2984 return 0;
2985}
2986
2987/*
2988 * Process an extent free intent item that was recovered from
2989 * the log. We need to free the extents that it describes.
2990 */
David Chinner3c1e2bb2008-04-10 12:21:11 +10002991STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992xlog_recover_process_efi(
2993 xfs_mount_t *mp,
2994 xfs_efi_log_item_t *efip)
2995{
2996 xfs_efd_log_item_t *efdp;
2997 xfs_trans_t *tp;
2998 int i;
David Chinner3c1e2bb2008-04-10 12:21:11 +10002999 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 xfs_extent_t *extp;
3001 xfs_fsblock_t startblock_fsb;
3002
3003 ASSERT(!(efip->efi_flags & XFS_EFI_RECOVERED));
3004
3005 /*
3006 * First check the validity of the extents described by the
3007 * EFI. If any are bad, then assume that all are bad and
3008 * just toss the EFI.
3009 */
3010 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
3011 extp = &(efip->efi_format.efi_extents[i]);
3012 startblock_fsb = XFS_BB_TO_FSB(mp,
3013 XFS_FSB_TO_DADDR(mp, extp->ext_start));
3014 if ((startblock_fsb == 0) ||
3015 (extp->ext_len == 0) ||
3016 (startblock_fsb >= mp->m_sb.sb_dblocks) ||
3017 (extp->ext_len >= mp->m_sb.sb_agblocks)) {
3018 /*
3019 * This will pull the EFI from the AIL and
3020 * free the memory associated with it.
3021 */
3022 xfs_efi_release(efip, efip->efi_format.efi_nextents);
David Chinner3c1e2bb2008-04-10 12:21:11 +10003023 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 }
3025 }
3026
3027 tp = xfs_trans_alloc(mp, 0);
David Chinner3c1e2bb2008-04-10 12:21:11 +10003028 error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0, 0, 0);
David Chinnerfc6149d2008-04-10 12:21:53 +10003029 if (error)
3030 goto abort_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents);
3032
3033 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
3034 extp = &(efip->efi_format.efi_extents[i]);
David Chinnerfc6149d2008-04-10 12:21:53 +10003035 error = xfs_free_extent(tp, extp->ext_start, extp->ext_len);
3036 if (error)
3037 goto abort_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 xfs_trans_log_efd_extent(tp, efdp, extp->ext_start,
3039 extp->ext_len);
3040 }
3041
3042 efip->efi_flags |= XFS_EFI_RECOVERED;
David Chinnere5720ee2008-04-10 12:21:18 +10003043 error = xfs_trans_commit(tp, 0);
David Chinner3c1e2bb2008-04-10 12:21:11 +10003044 return error;
David Chinnerfc6149d2008-04-10 12:21:53 +10003045
3046abort_error:
3047 xfs_trans_cancel(tp, XFS_TRANS_ABORT);
3048 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049}
3050
3051/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 * When this is called, all of the EFIs which did not have
3053 * corresponding EFDs should be in the AIL. What we do now
3054 * is free the extents associated with each one.
3055 *
3056 * Since we process the EFIs in normal transactions, they
3057 * will be removed at some point after the commit. This prevents
3058 * us from just walking down the list processing each one.
3059 * We'll use a flag in the EFI to skip those that we've already
3060 * processed and use the AIL iteration mechanism's generation
3061 * count to try to speed this up at least a bit.
3062 *
3063 * When we start, we know that the EFIs are the only things in
3064 * the AIL. As we process them, however, other items are added
3065 * to the AIL. Since everything added to the AIL must come after
3066 * everything already in the AIL, we stop processing as soon as
3067 * we see something other than an EFI in the AIL.
3068 */
David Chinner3c1e2bb2008-04-10 12:21:11 +10003069STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070xlog_recover_process_efis(
3071 xlog_t *log)
3072{
3073 xfs_log_item_t *lip;
3074 xfs_efi_log_item_t *efip;
David Chinner3c1e2bb2008-04-10 12:21:11 +10003075 int error = 0;
David Chinner27d8d5f2008-10-30 17:38:39 +11003076 struct xfs_ail_cursor cur;
David Chinnera9c21c12008-10-30 17:39:35 +11003077 struct xfs_ail *ailp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
David Chinnera9c21c12008-10-30 17:39:35 +11003079 ailp = log->l_ailp;
3080 spin_lock(&ailp->xa_lock);
3081 lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 while (lip != NULL) {
3083 /*
3084 * We're done when we see something other than an EFI.
David Chinner27d8d5f2008-10-30 17:38:39 +11003085 * There should be no EFIs left in the AIL now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 */
3087 if (lip->li_type != XFS_LI_EFI) {
David Chinner27d8d5f2008-10-30 17:38:39 +11003088#ifdef DEBUG
David Chinnera9c21c12008-10-30 17:39:35 +11003089 for (; lip; lip = xfs_trans_ail_cursor_next(ailp, &cur))
David Chinner27d8d5f2008-10-30 17:38:39 +11003090 ASSERT(lip->li_type != XFS_LI_EFI);
3091#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 break;
3093 }
3094
3095 /*
3096 * Skip EFIs that we've already processed.
3097 */
3098 efip = (xfs_efi_log_item_t *)lip;
3099 if (efip->efi_flags & XFS_EFI_RECOVERED) {
David Chinnera9c21c12008-10-30 17:39:35 +11003100 lip = xfs_trans_ail_cursor_next(ailp, &cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 continue;
3102 }
3103
David Chinnera9c21c12008-10-30 17:39:35 +11003104 spin_unlock(&ailp->xa_lock);
3105 error = xlog_recover_process_efi(log->l_mp, efip);
3106 spin_lock(&ailp->xa_lock);
David Chinner27d8d5f2008-10-30 17:38:39 +11003107 if (error)
3108 goto out;
David Chinnera9c21c12008-10-30 17:39:35 +11003109 lip = xfs_trans_ail_cursor_next(ailp, &cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 }
David Chinner27d8d5f2008-10-30 17:38:39 +11003111out:
David Chinnera9c21c12008-10-30 17:39:35 +11003112 xfs_trans_ail_cursor_done(ailp, &cur);
3113 spin_unlock(&ailp->xa_lock);
David Chinner3c1e2bb2008-04-10 12:21:11 +10003114 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115}
3116
3117/*
3118 * This routine performs a transaction to null out a bad inode pointer
3119 * in an agi unlinked inode hash bucket.
3120 */
3121STATIC void
3122xlog_recover_clear_agi_bucket(
3123 xfs_mount_t *mp,
3124 xfs_agnumber_t agno,
3125 int bucket)
3126{
3127 xfs_trans_t *tp;
3128 xfs_agi_t *agi;
3129 xfs_buf_t *agibp;
3130 int offset;
3131 int error;
3132
3133 tp = xfs_trans_alloc(mp, XFS_TRANS_CLEAR_AGI_BUCKET);
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11003134 error = xfs_trans_reserve(tp, 0, XFS_CLEAR_AGI_BUCKET_LOG_RES(mp),
3135 0, 0, 0);
David Chinnere5720ee2008-04-10 12:21:18 +10003136 if (error)
3137 goto out_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11003139 error = xfs_read_agi(mp, tp, agno, &agibp);
3140 if (error)
David Chinnere5720ee2008-04-10 12:21:18 +10003141 goto out_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11003143 agi = XFS_BUF_TO_AGI(agibp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11003144 agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 offset = offsetof(xfs_agi_t, agi_unlinked) +
3146 (sizeof(xfs_agino_t) * bucket);
3147 xfs_trans_log_buf(tp, agibp, offset,
3148 (offset + sizeof(xfs_agino_t) - 1));
3149
David Chinnere5720ee2008-04-10 12:21:18 +10003150 error = xfs_trans_commit(tp, 0);
3151 if (error)
3152 goto out_error;
3153 return;
3154
3155out_abort:
3156 xfs_trans_cancel(tp, XFS_TRANS_ABORT);
3157out_error:
3158 xfs_fs_cmn_err(CE_WARN, mp, "xlog_recover_clear_agi_bucket: "
3159 "failed to clear agi %d. Continuing.", agno);
3160 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161}
3162
Christoph Hellwig23fac502008-11-28 14:23:40 +11003163STATIC xfs_agino_t
3164xlog_recover_process_one_iunlink(
3165 struct xfs_mount *mp,
3166 xfs_agnumber_t agno,
3167 xfs_agino_t agino,
3168 int bucket)
3169{
3170 struct xfs_buf *ibp;
3171 struct xfs_dinode *dip;
3172 struct xfs_inode *ip;
3173 xfs_ino_t ino;
3174 int error;
3175
3176 ino = XFS_AGINO_TO_INO(mp, agno, agino);
3177 error = xfs_iget(mp, NULL, ino, 0, 0, &ip, 0);
3178 if (error)
3179 goto fail;
3180
3181 /*
3182 * Get the on disk inode to find the next inode in the bucket.
3183 */
Christoph Hellwig0cadda12010-01-19 09:56:44 +00003184 error = xfs_itobp(mp, NULL, ip, &dip, &ibp, XBF_LOCK);
Christoph Hellwig23fac502008-11-28 14:23:40 +11003185 if (error)
Christoph Hellwig0e446672008-11-28 14:23:42 +11003186 goto fail_iput;
Christoph Hellwig23fac502008-11-28 14:23:40 +11003187
Christoph Hellwig23fac502008-11-28 14:23:40 +11003188 ASSERT(ip->i_d.di_nlink == 0);
Christoph Hellwig0e446672008-11-28 14:23:42 +11003189 ASSERT(ip->i_d.di_mode != 0);
Christoph Hellwig23fac502008-11-28 14:23:40 +11003190
3191 /* setup for the next pass */
3192 agino = be32_to_cpu(dip->di_next_unlinked);
3193 xfs_buf_relse(ibp);
3194
3195 /*
3196 * Prevent any DMAPI event from being sent when the reference on
3197 * the inode is dropped.
3198 */
3199 ip->i_d.di_dmevmask = 0;
3200
Christoph Hellwig0e446672008-11-28 14:23:42 +11003201 IRELE(ip);
Christoph Hellwig23fac502008-11-28 14:23:40 +11003202 return agino;
3203
Christoph Hellwig0e446672008-11-28 14:23:42 +11003204 fail_iput:
3205 IRELE(ip);
Christoph Hellwig23fac502008-11-28 14:23:40 +11003206 fail:
3207 /*
3208 * We can't read in the inode this bucket points to, or this inode
3209 * is messed up. Just ditch this bucket of inodes. We will lose
3210 * some inodes and space, but at least we won't hang.
3211 *
3212 * Call xlog_recover_clear_agi_bucket() to perform a transaction to
3213 * clear the inode pointer in the bucket.
3214 */
3215 xlog_recover_clear_agi_bucket(mp, agno, bucket);
3216 return NULLAGINO;
3217}
3218
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219/*
3220 * xlog_iunlink_recover
3221 *
3222 * This is called during recovery to process any inodes which
3223 * we unlinked but not freed when the system crashed. These
3224 * inodes will be on the lists in the AGI blocks. What we do
3225 * here is scan all the AGIs and fully truncate and free any
3226 * inodes found on the lists. Each inode is removed from the
3227 * lists when it has been fully truncated and is freed. The
3228 * freeing of the inode and its removal from the list must be
3229 * atomic.
3230 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003231STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232xlog_recover_process_iunlinks(
3233 xlog_t *log)
3234{
3235 xfs_mount_t *mp;
3236 xfs_agnumber_t agno;
3237 xfs_agi_t *agi;
3238 xfs_buf_t *agibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 xfs_agino_t agino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 int bucket;
3241 int error;
3242 uint mp_dmevmask;
3243
3244 mp = log->l_mp;
3245
3246 /*
3247 * Prevent any DMAPI event from being sent while in this function.
3248 */
3249 mp_dmevmask = mp->m_dmevmask;
3250 mp->m_dmevmask = 0;
3251
3252 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
3253 /*
3254 * Find the agi for this ag.
3255 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11003256 error = xfs_read_agi(mp, NULL, agno, &agibp);
3257 if (error) {
3258 /*
3259 * AGI is b0rked. Don't process it.
3260 *
3261 * We should probably mark the filesystem as corrupt
3262 * after we've recovered all the ag's we can....
3263 */
3264 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 }
3266 agi = XFS_BUF_TO_AGI(agibp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
3268 for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11003269 agino = be32_to_cpu(agi->agi_unlinked[bucket]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 while (agino != NULLAGINO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 /*
3272 * Release the agi buffer so that it can
3273 * be acquired in the normal course of the
3274 * transaction to truncate and free the inode.
3275 */
3276 xfs_buf_relse(agibp);
3277
Christoph Hellwig23fac502008-11-28 14:23:40 +11003278 agino = xlog_recover_process_one_iunlink(mp,
3279 agno, agino, bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
3281 /*
3282 * Reacquire the agibuffer and continue around
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11003283 * the loop. This should never fail as we know
3284 * the buffer was good earlier on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11003286 error = xfs_read_agi(mp, NULL, agno, &agibp);
3287 ASSERT(error == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 agi = XFS_BUF_TO_AGI(agibp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 }
3290 }
3291
3292 /*
3293 * Release the buffer for the current agi so we can
3294 * go on to the next one.
3295 */
3296 xfs_buf_relse(agibp);
3297 }
3298
3299 mp->m_dmevmask = mp_dmevmask;
3300}
3301
3302
3303#ifdef DEBUG
3304STATIC void
3305xlog_pack_data_checksum(
3306 xlog_t *log,
3307 xlog_in_core_t *iclog,
3308 int size)
3309{
3310 int i;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003311 __be32 *up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 uint chksum = 0;
3313
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003314 up = (__be32 *)iclog->ic_datap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 /* divide length by 4 to get # words */
3316 for (i = 0; i < (size >> 2); i++) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003317 chksum ^= be32_to_cpu(*up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 up++;
3319 }
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003320 iclog->ic_header.h_chksum = cpu_to_be32(chksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321}
3322#else
3323#define xlog_pack_data_checksum(log, iclog, size)
3324#endif
3325
3326/*
3327 * Stamp cycle number in every block
3328 */
3329void
3330xlog_pack_data(
3331 xlog_t *log,
3332 xlog_in_core_t *iclog,
3333 int roundoff)
3334{
3335 int i, j, k;
3336 int size = iclog->ic_offset + roundoff;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003337 __be32 cycle_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 xfs_caddr_t dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339
3340 xlog_pack_data_checksum(log, iclog, size);
3341
3342 cycle_lsn = CYCLE_LSN_DISK(iclog->ic_header.h_lsn);
3343
3344 dp = iclog->ic_datap;
3345 for (i = 0; i < BTOBB(size) &&
3346 i < (XLOG_HEADER_CYCLE_SIZE / BBSIZE); i++) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003347 iclog->ic_header.h_cycle_data[i] = *(__be32 *)dp;
3348 *(__be32 *)dp = cycle_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 dp += BBSIZE;
3350 }
3351
Eric Sandeen62118702008-03-06 13:44:28 +11003352 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
Christoph Hellwigb28708d2008-11-28 14:23:38 +11003353 xlog_in_core_2_t *xhdr = iclog->ic_data;
3354
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 for ( ; i < BTOBB(size); i++) {
3356 j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3357 k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003358 xhdr[j].hic_xheader.xh_cycle_data[k] = *(__be32 *)dp;
3359 *(__be32 *)dp = cycle_lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 dp += BBSIZE;
3361 }
3362
3363 for (i = 1; i < log->l_iclog_heads; i++) {
3364 xhdr[i].hic_xheader.xh_cycle = cycle_lsn;
3365 }
3366 }
3367}
3368
3369#if defined(DEBUG) && defined(XFS_LOUD_RECOVERY)
3370STATIC void
3371xlog_unpack_data_checksum(
3372 xlog_rec_header_t *rhead,
3373 xfs_caddr_t dp,
3374 xlog_t *log)
3375{
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003376 __be32 *up = (__be32 *)dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 uint chksum = 0;
3378 int i;
3379
3380 /* divide length by 4 to get # words */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003381 for (i=0; i < be32_to_cpu(rhead->h_len) >> 2; i++) {
3382 chksum ^= be32_to_cpu(*up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 up++;
3384 }
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003385 if (chksum != be32_to_cpu(rhead->h_chksum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 if (rhead->h_chksum ||
3387 ((log->l_flags & XLOG_CHKSUM_MISMATCH) == 0)) {
3388 cmn_err(CE_DEBUG,
Nathan Scottb6574522006-06-09 15:29:40 +10003389 "XFS: LogR chksum mismatch: was (0x%x) is (0x%x)\n",
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003390 be32_to_cpu(rhead->h_chksum), chksum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 cmn_err(CE_DEBUG,
3392"XFS: Disregard message if filesystem was created with non-DEBUG kernel");
Eric Sandeen62118702008-03-06 13:44:28 +11003393 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 cmn_err(CE_DEBUG,
Nathan Scottb6574522006-06-09 15:29:40 +10003395 "XFS: LogR this is a LogV2 filesystem\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 }
3397 log->l_flags |= XLOG_CHKSUM_MISMATCH;
3398 }
3399 }
3400}
3401#else
3402#define xlog_unpack_data_checksum(rhead, dp, log)
3403#endif
3404
3405STATIC void
3406xlog_unpack_data(
3407 xlog_rec_header_t *rhead,
3408 xfs_caddr_t dp,
3409 xlog_t *log)
3410{
3411 int i, j, k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003413 for (i = 0; i < BTOBB(be32_to_cpu(rhead->h_len)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 i < (XLOG_HEADER_CYCLE_SIZE / BBSIZE); i++) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003415 *(__be32 *)dp = *(__be32 *)&rhead->h_cycle_data[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 dp += BBSIZE;
3417 }
3418
Eric Sandeen62118702008-03-06 13:44:28 +11003419 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
Christoph Hellwigb28708d2008-11-28 14:23:38 +11003420 xlog_in_core_2_t *xhdr = (xlog_in_core_2_t *)rhead;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003421 for ( ; i < BTOBB(be32_to_cpu(rhead->h_len)); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
3423 k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003424 *(__be32 *)dp = xhdr[j].hic_xheader.xh_cycle_data[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 dp += BBSIZE;
3426 }
3427 }
3428
3429 xlog_unpack_data_checksum(rhead, dp, log);
3430}
3431
3432STATIC int
3433xlog_valid_rec_header(
3434 xlog_t *log,
3435 xlog_rec_header_t *rhead,
3436 xfs_daddr_t blkno)
3437{
3438 int hlen;
3439
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003440 if (unlikely(be32_to_cpu(rhead->h_magicno) != XLOG_HEADER_MAGIC_NUM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 XFS_ERROR_REPORT("xlog_valid_rec_header(1)",
3442 XFS_ERRLEVEL_LOW, log->l_mp);
3443 return XFS_ERROR(EFSCORRUPTED);
3444 }
3445 if (unlikely(
3446 (!rhead->h_version ||
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003447 (be32_to_cpu(rhead->h_version) & (~XLOG_VERSION_OKBITS))))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 xlog_warn("XFS: %s: unrecognised log version (%d).",
Harvey Harrison34a622b2008-04-10 12:19:21 +10003449 __func__, be32_to_cpu(rhead->h_version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 return XFS_ERROR(EIO);
3451 }
3452
3453 /* LR body must have data or it wouldn't have been written */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003454 hlen = be32_to_cpu(rhead->h_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 if (unlikely( hlen <= 0 || hlen > INT_MAX )) {
3456 XFS_ERROR_REPORT("xlog_valid_rec_header(2)",
3457 XFS_ERRLEVEL_LOW, log->l_mp);
3458 return XFS_ERROR(EFSCORRUPTED);
3459 }
3460 if (unlikely( blkno > log->l_logBBsize || blkno > INT_MAX )) {
3461 XFS_ERROR_REPORT("xlog_valid_rec_header(3)",
3462 XFS_ERRLEVEL_LOW, log->l_mp);
3463 return XFS_ERROR(EFSCORRUPTED);
3464 }
3465 return 0;
3466}
3467
3468/*
3469 * Read the log from tail to head and process the log records found.
3470 * Handle the two cases where the tail and head are in the same cycle
3471 * and where the active portion of the log wraps around the end of
3472 * the physical log separately. The pass parameter is passed through
3473 * to the routines called to process the data and is not looked at
3474 * here.
3475 */
3476STATIC int
3477xlog_do_recovery_pass(
3478 xlog_t *log,
3479 xfs_daddr_t head_blk,
3480 xfs_daddr_t tail_blk,
3481 int pass)
3482{
3483 xlog_rec_header_t *rhead;
3484 xfs_daddr_t blk_no;
Andy Polingfc5bc4c2009-11-03 17:26:47 +00003485 xfs_caddr_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 xfs_buf_t *hbp, *dbp;
3487 int error = 0, h_size;
3488 int bblks, split_bblks;
3489 int hblks, split_hblks, wrapped_hblks;
Dave Chinnerf0a76952010-01-11 11:49:57 +00003490 struct hlist_head rhash[XLOG_RHASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
3492 ASSERT(head_blk != tail_blk);
3493
3494 /*
3495 * Read the header of the tail block and get the iclog buffer size from
3496 * h_size. Use this to tell how many sectors make up the log header.
3497 */
Eric Sandeen62118702008-03-06 13:44:28 +11003498 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 /*
3500 * When using variable length iclogs, read first sector of
3501 * iclog header and extract the header size from it. Get a
3502 * new hbp that is the correct size.
3503 */
3504 hbp = xlog_get_bp(log, 1);
3505 if (!hbp)
3506 return ENOMEM;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003507
3508 error = xlog_bread(log, tail_blk, 1, hbp, &offset);
3509 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 goto bread_err1;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003511
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 rhead = (xlog_rec_header_t *)offset;
3513 error = xlog_valid_rec_header(log, rhead, tail_blk);
3514 if (error)
3515 goto bread_err1;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003516 h_size = be32_to_cpu(rhead->h_size);
3517 if ((be32_to_cpu(rhead->h_version) & XLOG_VERSION_2) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 (h_size > XLOG_HEADER_CYCLE_SIZE)) {
3519 hblks = h_size / XLOG_HEADER_CYCLE_SIZE;
3520 if (h_size % XLOG_HEADER_CYCLE_SIZE)
3521 hblks++;
3522 xlog_put_bp(hbp);
3523 hbp = xlog_get_bp(log, hblks);
3524 } else {
3525 hblks = 1;
3526 }
3527 } else {
3528 ASSERT(log->l_sectbb_log == 0);
3529 hblks = 1;
3530 hbp = xlog_get_bp(log, 1);
3531 h_size = XLOG_BIG_RECORD_BSIZE;
3532 }
3533
3534 if (!hbp)
3535 return ENOMEM;
3536 dbp = xlog_get_bp(log, BTOBB(h_size));
3537 if (!dbp) {
3538 xlog_put_bp(hbp);
3539 return ENOMEM;
3540 }
3541
3542 memset(rhash, 0, sizeof(rhash));
3543 if (tail_blk <= head_blk) {
3544 for (blk_no = tail_blk; blk_no < head_blk; ) {
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003545 error = xlog_bread(log, blk_no, hblks, hbp, &offset);
3546 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 goto bread_err2;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003548
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 rhead = (xlog_rec_header_t *)offset;
3550 error = xlog_valid_rec_header(log, rhead, blk_no);
3551 if (error)
3552 goto bread_err2;
3553
3554 /* blocks in data section */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003555 bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003556 error = xlog_bread(log, blk_no + hblks, bblks, dbp,
3557 &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 if (error)
3559 goto bread_err2;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003560
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 xlog_unpack_data(rhead, offset, log);
3562 if ((error = xlog_recover_process_data(log,
3563 rhash, rhead, offset, pass)))
3564 goto bread_err2;
3565 blk_no += bblks + hblks;
3566 }
3567 } else {
3568 /*
3569 * Perform recovery around the end of the physical log.
3570 * When the head is not on the same cycle number as the tail,
3571 * we can't do a sequential recovery as above.
3572 */
3573 blk_no = tail_blk;
3574 while (blk_no < log->l_logBBsize) {
3575 /*
3576 * Check for header wrapping around physical end-of-log
3577 */
Andy Polingfc5bc4c2009-11-03 17:26:47 +00003578 offset = XFS_BUF_PTR(hbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 split_hblks = 0;
3580 wrapped_hblks = 0;
3581 if (blk_no + hblks <= log->l_logBBsize) {
3582 /* Read header in one read */
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003583 error = xlog_bread(log, blk_no, hblks, hbp,
3584 &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 if (error)
3586 goto bread_err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 } else {
3588 /* This LR is split across physical log end */
3589 if (blk_no != log->l_logBBsize) {
3590 /* some data before physical log end */
3591 ASSERT(blk_no <= INT_MAX);
3592 split_hblks = log->l_logBBsize - (int)blk_no;
3593 ASSERT(split_hblks > 0);
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003594 error = xlog_bread(log, blk_no,
3595 split_hblks, hbp,
3596 &offset);
3597 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 goto bread_err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 }
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003600
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 /*
3602 * Note: this black magic still works with
3603 * large sector sizes (non-512) only because:
3604 * - we increased the buffer size originally
3605 * by 1 sector giving us enough extra space
3606 * for the second read;
3607 * - the log start is guaranteed to be sector
3608 * aligned;
3609 * - we read the log end (LR header start)
3610 * _first_, then the log start (LR header end)
3611 * - order is important.
3612 */
David Chinner234f56a2008-04-10 12:24:24 +10003613 wrapped_hblks = hblks - split_hblks;
David Chinner234f56a2008-04-10 12:24:24 +10003614 error = XFS_BUF_SET_PTR(hbp,
Andy Polingfc5bc4c2009-11-03 17:26:47 +00003615 offset + BBTOB(split_hblks),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 BBTOB(hblks - split_hblks));
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003617 if (error)
3618 goto bread_err2;
3619
3620 error = xlog_bread_noalign(log, 0,
3621 wrapped_hblks, hbp);
3622 if (error)
3623 goto bread_err2;
3624
Andy Polingfc5bc4c2009-11-03 17:26:47 +00003625 error = XFS_BUF_SET_PTR(hbp, offset,
David Chinner234f56a2008-04-10 12:24:24 +10003626 BBTOB(hblks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 if (error)
3628 goto bread_err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 }
3630 rhead = (xlog_rec_header_t *)offset;
3631 error = xlog_valid_rec_header(log, rhead,
3632 split_hblks ? blk_no : 0);
3633 if (error)
3634 goto bread_err2;
3635
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003636 bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 blk_no += hblks;
3638
3639 /* Read in data for log record */
3640 if (blk_no + bblks <= log->l_logBBsize) {
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003641 error = xlog_bread(log, blk_no, bblks, dbp,
3642 &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 if (error)
3644 goto bread_err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 } else {
3646 /* This log record is split across the
3647 * physical end of log */
Andy Polingfc5bc4c2009-11-03 17:26:47 +00003648 offset = XFS_BUF_PTR(dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 split_bblks = 0;
3650 if (blk_no != log->l_logBBsize) {
3651 /* some data is before the physical
3652 * end of log */
3653 ASSERT(!wrapped_hblks);
3654 ASSERT(blk_no <= INT_MAX);
3655 split_bblks =
3656 log->l_logBBsize - (int)blk_no;
3657 ASSERT(split_bblks > 0);
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003658 error = xlog_bread(log, blk_no,
3659 split_bblks, dbp,
3660 &offset);
3661 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 goto bread_err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 }
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003664
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 /*
3666 * Note: this black magic still works with
3667 * large sector sizes (non-512) only because:
3668 * - we increased the buffer size originally
3669 * by 1 sector giving us enough extra space
3670 * for the second read;
3671 * - the log start is guaranteed to be sector
3672 * aligned;
3673 * - we read the log end (LR header start)
3674 * _first_, then the log start (LR header end)
3675 * - order is important.
3676 */
David Chinner234f56a2008-04-10 12:24:24 +10003677 error = XFS_BUF_SET_PTR(dbp,
Andy Polingfc5bc4c2009-11-03 17:26:47 +00003678 offset + BBTOB(split_bblks),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 BBTOB(bblks - split_bblks));
David Chinner234f56a2008-04-10 12:24:24 +10003680 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 goto bread_err2;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003682
3683 error = xlog_bread_noalign(log, wrapped_hblks,
3684 bblks - split_bblks,
3685 dbp);
3686 if (error)
3687 goto bread_err2;
3688
Andy Polingfc5bc4c2009-11-03 17:26:47 +00003689 error = XFS_BUF_SET_PTR(dbp, offset, h_size);
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003690 if (error)
3691 goto bread_err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 }
3693 xlog_unpack_data(rhead, offset, log);
3694 if ((error = xlog_recover_process_data(log, rhash,
3695 rhead, offset, pass)))
3696 goto bread_err2;
3697 blk_no += bblks;
3698 }
3699
3700 ASSERT(blk_no >= log->l_logBBsize);
3701 blk_no -= log->l_logBBsize;
3702
3703 /* read first part of physical log */
3704 while (blk_no < head_blk) {
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003705 error = xlog_bread(log, blk_no, hblks, hbp, &offset);
3706 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 goto bread_err2;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003708
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 rhead = (xlog_rec_header_t *)offset;
3710 error = xlog_valid_rec_header(log, rhead, blk_no);
3711 if (error)
3712 goto bread_err2;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003713
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003714 bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003715 error = xlog_bread(log, blk_no+hblks, bblks, dbp,
3716 &offset);
3717 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 goto bread_err2;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01003719
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 xlog_unpack_data(rhead, offset, log);
3721 if ((error = xlog_recover_process_data(log, rhash,
3722 rhead, offset, pass)))
3723 goto bread_err2;
3724 blk_no += bblks + hblks;
3725 }
3726 }
3727
3728 bread_err2:
3729 xlog_put_bp(dbp);
3730 bread_err1:
3731 xlog_put_bp(hbp);
3732 return error;
3733}
3734
3735/*
3736 * Do the recovery of the log. We actually do this in two phases.
3737 * The two passes are necessary in order to implement the function
3738 * of cancelling a record written into the log. The first pass
3739 * determines those things which have been cancelled, and the
3740 * second pass replays log items normally except for those which
3741 * have been cancelled. The handling of the replay and cancellations
3742 * takes place in the log item type specific routines.
3743 *
3744 * The table of items which have cancel records in the log is allocated
3745 * and freed at this level, since only here do we know when all of
3746 * the log recovery has been completed.
3747 */
3748STATIC int
3749xlog_do_log_recovery(
3750 xlog_t *log,
3751 xfs_daddr_t head_blk,
3752 xfs_daddr_t tail_blk)
3753{
3754 int error;
3755
3756 ASSERT(head_blk != tail_blk);
3757
3758 /*
3759 * First do a pass to find all of the cancelled buf log items.
3760 * Store them in the buf_cancel_table for use in the second pass.
3761 */
3762 log->l_buf_cancel_table =
3763 (xfs_buf_cancel_t **)kmem_zalloc(XLOG_BC_TABLE_SIZE *
3764 sizeof(xfs_buf_cancel_t*),
3765 KM_SLEEP);
3766 error = xlog_do_recovery_pass(log, head_blk, tail_blk,
3767 XLOG_RECOVER_PASS1);
3768 if (error != 0) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003769 kmem_free(log->l_buf_cancel_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 log->l_buf_cancel_table = NULL;
3771 return error;
3772 }
3773 /*
3774 * Then do a second pass to actually recover the items in the log.
3775 * When it is complete free the table of buf cancel items.
3776 */
3777 error = xlog_do_recovery_pass(log, head_blk, tail_blk,
3778 XLOG_RECOVER_PASS2);
3779#ifdef DEBUG
Tim Shimmin6d192a92006-06-09 14:55:38 +10003780 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 int i;
3782
3783 for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
3784 ASSERT(log->l_buf_cancel_table[i] == NULL);
3785 }
3786#endif /* DEBUG */
3787
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003788 kmem_free(log->l_buf_cancel_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 log->l_buf_cancel_table = NULL;
3790
3791 return error;
3792}
3793
3794/*
3795 * Do the actual recovery
3796 */
3797STATIC int
3798xlog_do_recover(
3799 xlog_t *log,
3800 xfs_daddr_t head_blk,
3801 xfs_daddr_t tail_blk)
3802{
3803 int error;
3804 xfs_buf_t *bp;
3805 xfs_sb_t *sbp;
3806
3807 /*
3808 * First replay the images in the log.
3809 */
3810 error = xlog_do_log_recovery(log, head_blk, tail_blk);
3811 if (error) {
3812 return error;
3813 }
3814
3815 XFS_bflush(log->l_mp->m_ddev_targp);
3816
3817 /*
3818 * If IO errors happened during recovery, bail out.
3819 */
3820 if (XFS_FORCED_SHUTDOWN(log->l_mp)) {
3821 return (EIO);
3822 }
3823
3824 /*
3825 * We now update the tail_lsn since much of the recovery has completed
3826 * and there may be space available to use. If there were no extent
3827 * or iunlinks, we can free up the entire log and set the tail_lsn to
3828 * be the last_sync_lsn. This was set in xlog_find_tail to be the
3829 * lsn of the last known good LR on disk. If there are extent frees
3830 * or iunlinks they will have some entries in the AIL; so we look at
3831 * the AIL to determine how to set the tail_lsn.
3832 */
3833 xlog_assign_tail_lsn(log->l_mp);
3834
3835 /*
3836 * Now that we've finished replaying all buffer and inode
3837 * updates, re-read in the superblock.
3838 */
3839 bp = xfs_getsb(log->l_mp, 0);
3840 XFS_BUF_UNDONE(bp);
Lachlan McIlroybebf9632007-10-15 13:18:02 +10003841 ASSERT(!(XFS_BUF_ISWRITE(bp)));
3842 ASSERT(!(XFS_BUF_ISDELAYWRITE(bp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 XFS_BUF_READ(bp);
Lachlan McIlroybebf9632007-10-15 13:18:02 +10003844 XFS_BUF_UNASYNC(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 xfsbdstrat(log->l_mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10003846 error = xfs_iowait(bp);
3847 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 xfs_ioerror_alert("xlog_do_recover",
3849 log->l_mp, bp, XFS_BUF_ADDR(bp));
3850 ASSERT(0);
3851 xfs_buf_relse(bp);
3852 return error;
3853 }
3854
3855 /* Convert superblock from on-disk format */
3856 sbp = &log->l_mp->m_sb;
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +10003857 xfs_sb_from_disk(sbp, XFS_BUF_TO_SBP(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 ASSERT(sbp->sb_magicnum == XFS_SB_MAGIC);
Eric Sandeen62118702008-03-06 13:44:28 +11003859 ASSERT(xfs_sb_good_version(sbp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 xfs_buf_relse(bp);
3861
Lachlan McIlroy5478eea2007-02-10 18:36:29 +11003862 /* We've re-read the superblock so re-initialize per-cpu counters */
3863 xfs_icsb_reinit_counters(log->l_mp);
3864
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 xlog_recover_check_summary(log);
3866
3867 /* Normal transactions can now occur */
3868 log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
3869 return 0;
3870}
3871
3872/*
3873 * Perform recovery and re-initialize some log variables in xlog_find_tail.
3874 *
3875 * Return error or zero.
3876 */
3877int
3878xlog_recover(
Eric Sandeen65be6052006-01-11 15:34:19 +11003879 xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880{
3881 xfs_daddr_t head_blk, tail_blk;
3882 int error;
3883
3884 /* find the tail of the log */
Eric Sandeen65be6052006-01-11 15:34:19 +11003885 if ((error = xlog_find_tail(log, &head_blk, &tail_blk)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 return error;
3887
3888 if (tail_blk != head_blk) {
3889 /* There used to be a comment here:
3890 *
3891 * disallow recovery on read-only mounts. note -- mount
3892 * checks for ENOSPC and turns it into an intelligent
3893 * error message.
3894 * ...but this is no longer true. Now, unless you specify
3895 * NORECOVERY (in which case this function would never be
3896 * called), we just go ahead and recover. We do this all
3897 * under the vfs layer, so we can get away with it unless
3898 * the device itself is read-only, in which case we fail.
3899 */
Utako Kusaka3a02ee12007-05-08 13:50:06 +10003900 if ((error = xfs_dev_is_read_only(log->l_mp, "recovery"))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 return error;
3902 }
3903
3904 cmn_err(CE_NOTE,
Nathan Scottfc1f8c12005-11-02 11:44:33 +11003905 "Starting XFS recovery on filesystem: %s (logdev: %s)",
3906 log->l_mp->m_fsname, log->l_mp->m_logname ?
3907 log->l_mp->m_logname : "internal");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908
3909 error = xlog_do_recover(log, head_blk, tail_blk);
3910 log->l_flags |= XLOG_RECOVERY_NEEDED;
3911 }
3912 return error;
3913}
3914
3915/*
3916 * In the first part of recovery we replay inodes and buffers and build
3917 * up the list of extent free items which need to be processed. Here
3918 * we process the extent free items and clean up the on disk unlinked
3919 * inode lists. This is separated from the first part of recovery so
3920 * that the root and real-time bitmap inodes can be read in from disk in
3921 * between the two stages. This is necessary so that we can free space
3922 * in the real-time portion of the file system.
3923 */
3924int
3925xlog_recover_finish(
Christoph Hellwig42490232008-08-13 16:49:32 +10003926 xlog_t *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927{
3928 /*
3929 * Now we're ready to do the transactions needed for the
3930 * rest of recovery. Start with completing all the extent
3931 * free intent records and then process the unlinked inode
3932 * lists. At this point, we essentially run in normal mode
3933 * except that we're still performing recovery actions
3934 * rather than accepting new requests.
3935 */
3936 if (log->l_flags & XLOG_RECOVERY_NEEDED) {
David Chinner3c1e2bb2008-04-10 12:21:11 +10003937 int error;
3938 error = xlog_recover_process_efis(log);
3939 if (error) {
3940 cmn_err(CE_ALERT,
3941 "Failed to recover EFIs on filesystem: %s",
3942 log->l_mp->m_fsname);
3943 return error;
3944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 /*
3946 * Sync the log to get all the EFIs out of the AIL.
3947 * This isn't absolutely necessary, but it helps in
3948 * case the unlink transactions would have problems
3949 * pushing the EFIs out of the way.
3950 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00003951 xfs_log_force(log->l_mp, XFS_LOG_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952
Christoph Hellwig42490232008-08-13 16:49:32 +10003953 xlog_recover_process_iunlinks(log);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954
3955 xlog_recover_check_summary(log);
3956
3957 cmn_err(CE_NOTE,
Nathan Scottfc1f8c12005-11-02 11:44:33 +11003958 "Ending XFS recovery on filesystem: %s (logdev: %s)",
3959 log->l_mp->m_fsname, log->l_mp->m_logname ?
3960 log->l_mp->m_logname : "internal");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 log->l_flags &= ~XLOG_RECOVERY_NEEDED;
3962 } else {
3963 cmn_err(CE_DEBUG,
Nathan Scottb6574522006-06-09 15:29:40 +10003964 "!Ending clean XFS mount for filesystem: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 log->l_mp->m_fsname);
3966 }
3967 return 0;
3968}
3969
3970
3971#if defined(DEBUG)
3972/*
3973 * Read all of the agf and agi counters and check that they
3974 * are consistent with the superblock counters.
3975 */
3976void
3977xlog_recover_check_summary(
3978 xlog_t *log)
3979{
3980 xfs_mount_t *mp;
3981 xfs_agf_t *agfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 xfs_buf_t *agfbp;
3983 xfs_buf_t *agibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 xfs_buf_t *sbbp;
3985#ifdef XFS_LOUD_RECOVERY
3986 xfs_sb_t *sbp;
3987#endif
3988 xfs_agnumber_t agno;
3989 __uint64_t freeblks;
3990 __uint64_t itotal;
3991 __uint64_t ifree;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11003992 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
3994 mp = log->l_mp;
3995
3996 freeblks = 0LL;
3997 itotal = 0LL;
3998 ifree = 0LL;
3999 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
From: Christoph Hellwig48056212008-11-28 14:23:38 +11004000 error = xfs_read_agf(mp, NULL, agno, 0, &agfbp);
4001 if (error) {
4002 xfs_fs_cmn_err(CE_ALERT, mp,
4003 "xlog_recover_check_summary(agf)"
4004 "agf read failed agno %d error %d",
4005 agno, error);
4006 } else {
4007 agfp = XFS_BUF_TO_AGF(agfbp);
4008 freeblks += be32_to_cpu(agfp->agf_freeblks) +
4009 be32_to_cpu(agfp->agf_flcount);
4010 xfs_buf_relse(agfbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11004013 error = xfs_read_agi(mp, NULL, agno, &agibp);
4014 if (!error) {
4015 struct xfs_agi *agi = XFS_BUF_TO_AGI(agibp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11004017 itotal += be32_to_cpu(agi->agi_count);
4018 ifree += be32_to_cpu(agi->agi_freecount);
4019 xfs_buf_relse(agibp);
4020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 }
4022
4023 sbbp = xfs_getsb(mp, 0);
4024#ifdef XFS_LOUD_RECOVERY
4025 sbp = &mp->m_sb;
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +10004026 xfs_sb_from_disk(sbp, XFS_BUF_TO_SBP(sbbp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 cmn_err(CE_NOTE,
4028 "xlog_recover_check_summary: sb_icount %Lu itotal %Lu",
4029 sbp->sb_icount, itotal);
4030 cmn_err(CE_NOTE,
4031 "xlog_recover_check_summary: sb_ifree %Lu itotal %Lu",
4032 sbp->sb_ifree, ifree);
4033 cmn_err(CE_NOTE,
4034 "xlog_recover_check_summary: sb_fdblocks %Lu freeblks %Lu",
4035 sbp->sb_fdblocks, freeblks);
4036#if 0
4037 /*
4038 * This is turned off until I account for the allocation
4039 * btree blocks which live in free space.
4040 */
4041 ASSERT(sbp->sb_icount == itotal);
4042 ASSERT(sbp->sb_ifree == ifree);
4043 ASSERT(sbp->sb_fdblocks == freeblks);
4044#endif
4045#endif
4046 xfs_buf_relse(sbbp);
4047}
4048#endif /* DEBUG */