Blame view

fs/xfs/xfs_log_recover.c 97.9 KB
0b61f8a40   Dave Chinner   xfs: convert to S...
1
  // SPDX-License-Identifier: GPL-2.0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
  /*
87c199c2a   Tim Shimmin   [XFS] Over zealou...
3
   * Copyright (c) 2000-2006 Silicon Graphics, Inc.
7b7187698   Nathan Scott   [XFS] Update lice...
4
   * All Rights Reserved.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
5
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
6
  #include "xfs.h"
a844f4510   Nathan Scott   [XFS] Remove xfs_...
7
  #include "xfs_fs.h"
70a9883c5   Dave Chinner   xfs: create a sha...
8
  #include "xfs_shared.h"
239880ef6   Dave Chinner   xfs: decouple log...
9
10
11
  #include "xfs_format.h"
  #include "xfs_log_format.h"
  #include "xfs_trans_resv.h"
a844f4510   Nathan Scott   [XFS] Remove xfs_...
12
  #include "xfs_bit.h"
a844f4510   Nathan Scott   [XFS] Remove xfs_...
13
  #include "xfs_sb.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
14
  #include "xfs_mount.h"
509955823   Darrick J. Wong   xfs: log recovery...
15
  #include "xfs_defer.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
16
  #include "xfs_inode.h"
239880ef6   Dave Chinner   xfs: decouple log...
17
  #include "xfs_trans.h"
239880ef6   Dave Chinner   xfs: decouple log...
18
  #include "xfs_log.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
  #include "xfs_log_priv.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
  #include "xfs_log_recover.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
  #include "xfs_trans_priv.h"
a4fbe6ab1   Dave Chinner   xfs: decouple ino...
22
23
  #include "xfs_alloc.h"
  #include "xfs_ialloc.h"
0b1b213fc   Christoph Hellwig   xfs: event tracin...
24
  #include "xfs_trace.h"
33479e054   Dave Chinner   xfs: remove xfs_i...
25
  #include "xfs_icache.h"
a4fbe6ab1   Dave Chinner   xfs: decouple ino...
26
  #include "xfs_error.h"
60a4a2225   Brian Foster   xfs: update metad...
27
  #include "xfs_buf_item.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
28

fc06c6d06   Dave Chinner   xfs: separate out...
29
  #define BLK_AVG(blk1, blk2)	((blk1+blk2) >> 1)
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
30
31
32
33
34
35
36
37
  STATIC int
  xlog_find_zeroed(
  	struct xlog	*,
  	xfs_daddr_t	*);
  STATIC int
  xlog_clear_stale_blocks(
  	struct xlog	*,
  	xfs_lsn_t);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38
  #if defined(DEBUG)
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
39
40
41
  STATIC void
  xlog_recover_check_summary(
  	struct xlog *);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
42
43
  #else
  #define	xlog_recover_check_summary(log)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
44
  #endif
7088c4136   Brian Foster   xfs: detect and t...
45
46
47
  STATIC int
  xlog_do_recovery_pass(
          struct xlog *, xfs_daddr_t, xfs_daddr_t, int, xfs_daddr_t *);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
48

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49
50
51
  /*
   * Sector aligned buffer routines for buffer create/read/write/access
   */
ff30a6221   Alex Elder   xfs: encapsulate ...
52
  /*
99c265950   Brian Foster   xfs: more robust ...
53
54
55
   * Verify the log-relative block number and length in basic blocks are valid for
   * an operation involving the given XFS log buffer. Returns true if the fields
   * are valid, false otherwise.
ff30a6221   Alex Elder   xfs: encapsulate ...
56
   */
99c265950   Brian Foster   xfs: more robust ...
57
  static inline bool
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
58
  xlog_verify_bno(
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
59
  	struct xlog	*log,
99c265950   Brian Foster   xfs: more robust ...
60
  	xfs_daddr_t	blk_no,
ff30a6221   Alex Elder   xfs: encapsulate ...
61
62
  	int		bbcount)
  {
99c265950   Brian Foster   xfs: more robust ...
63
64
65
66
67
  	if (blk_no < 0 || blk_no >= log->l_logBBsize)
  		return false;
  	if (bbcount <= 0 || (blk_no + bbcount) > log->l_logBBsize)
  		return false;
  	return true;
ff30a6221   Alex Elder   xfs: encapsulate ...
68
  }
36adecff5   Alex Elder   xfs: nothing spec...
69
  /*
6ad5b3255   Christoph Hellwig   xfs: use bios dir...
70
71
   * Allocate a buffer to hold log data.  The buffer needs to be able to map to
   * a range of nbblks basic blocks at any valid offset within the log.
36adecff5   Alex Elder   xfs: nothing spec...
72
   */
6ad5b3255   Christoph Hellwig   xfs: use bios dir...
73
  static char *
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
74
  xlog_alloc_buffer(
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
75
  	struct xlog	*log,
3228149ce   Dave Chinner   xfs: Check buffer...
76
  	int		nbblks)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
77
  {
f8f9ee479   Dave Chinner   xfs: add kmem_all...
78
  	int align_mask = xfs_buftarg_dma_alignment(log->l_targ);
99c265950   Brian Foster   xfs: more robust ...
79
80
81
82
  	/*
  	 * Pass log block 0 since we don't have an addr yet, buffer will be
  	 * verified on read.
  	 */
a71895c5d   Darrick J. Wong   xfs: convert open...
83
  	if (XFS_IS_CORRUPT(log->l_mp, !xlog_verify_bno(log, 0, nbblks))) {
a0fa2b679   Dave Chinner   xfs: Convert xlog...
84
  		xfs_warn(log->l_mp, "Invalid block length (0x%x) for buffer",
ff30a6221   Alex Elder   xfs: encapsulate ...
85
  			nbblks);
3228149ce   Dave Chinner   xfs: Check buffer...
86
87
  		return NULL;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
88

36adecff5   Alex Elder   xfs: nothing spec...
89
  	/*
6ad5b3255   Christoph Hellwig   xfs: use bios dir...
90
91
92
  	 * We do log I/O in units of log sectors (a power-of-2 multiple of the
  	 * basic block size), so we round up the requested size to accommodate
  	 * the basic blocks required for complete log sectors.
36adecff5   Alex Elder   xfs: nothing spec...
93
  	 *
6ad5b3255   Christoph Hellwig   xfs: use bios dir...
94
95
96
97
98
99
100
101
  	 * In addition, the buffer may be used for a non-sector-aligned block
  	 * offset, in which case an I/O of the requested size could extend
  	 * beyond the end of the buffer.  If the requested size is only 1 basic
  	 * block it will never straddle a sector boundary, so this won't be an
  	 * issue.  Nor will this be a problem if the log I/O is done in basic
  	 * blocks (sector size 1).  But otherwise we extend the buffer by one
  	 * extra log sector to ensure there's space to accommodate this
  	 * possibility.
36adecff5   Alex Elder   xfs: nothing spec...
102
  	 */
69ce58f08   Alex Elder   xfs: record log s...
103
104
105
  	if (nbblks > 1 && log->l_sectBBsize > 1)
  		nbblks += log->l_sectBBsize;
  	nbblks = round_up(nbblks, log->l_sectBBsize);
3219e8cf0   Bill O'Donnell   xfs: assure zeroe...
106
  	return kmem_alloc_io(BBTOB(nbblks), align_mask, KM_MAYFAIL | KM_ZERO);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
107
  }
48389ef17   Alex Elder   xfs: kill off l_s...
108
109
110
111
  /*
   * Return the address of the start of the given block number's data
   * in a log buffer.  The buffer covers a log sector-aligned region.
   */
18ffb8c3f   Christoph Hellwig   xfs: return an of...
112
  static inline unsigned int
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
113
  xlog_align(
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
114
  	struct xlog	*log,
18ffb8c3f   Christoph Hellwig   xfs: return an of...
115
  	xfs_daddr_t	blk_no)
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
116
  {
18ffb8c3f   Christoph Hellwig   xfs: return an of...
117
  	return BBTOB(blk_no & ((xfs_daddr_t)log->l_sectBBsize - 1));
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
118
  }
6ad5b3255   Christoph Hellwig   xfs: use bios dir...
119
120
121
122
123
124
125
  static int
  xlog_do_io(
  	struct xlog		*log,
  	xfs_daddr_t		blk_no,
  	unsigned int		nbblks,
  	char			*data,
  	unsigned int		op)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126
  {
6ad5b3255   Christoph Hellwig   xfs: use bios dir...
127
  	int			error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
128

a71895c5d   Darrick J. Wong   xfs: convert open...
129
  	if (XFS_IS_CORRUPT(log->l_mp, !xlog_verify_bno(log, blk_no, nbblks))) {
99c265950   Brian Foster   xfs: more robust ...
130
131
132
  		xfs_warn(log->l_mp,
  			 "Invalid log block/length (0x%llx, 0x%x) for buffer",
  			 blk_no, nbblks);
2451337dd   Dave Chinner   xfs: global error...
133
  		return -EFSCORRUPTED;
3228149ce   Dave Chinner   xfs: Check buffer...
134
  	}
69ce58f08   Alex Elder   xfs: record log s...
135
136
  	blk_no = round_down(blk_no, log->l_sectBBsize);
  	nbblks = round_up(nbblks, log->l_sectBBsize);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
137
  	ASSERT(nbblks > 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
138

6ad5b3255   Christoph Hellwig   xfs: use bios dir...
139
140
141
142
143
144
145
146
  	error = xfs_rw_bdev(log->l_targ->bt_bdev, log->l_logBBstart + blk_no,
  			BBTOB(nbblks), data, op);
  	if (error && !XFS_FORCED_SHUTDOWN(log->l_mp)) {
  		xfs_alert(log->l_mp,
  			  "log recovery %s I/O error at daddr 0x%llx len %d error %d",
  			  op == REQ_OP_WRITE ? "write" : "read",
  			  blk_no, nbblks, error);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
147
148
  	return error;
  }
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
149
  STATIC int
6ad5b3255   Christoph Hellwig   xfs: use bios dir...
150
  xlog_bread_noalign(
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
151
  	struct xlog	*log,
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
152
153
  	xfs_daddr_t	blk_no,
  	int		nbblks,
6ad5b3255   Christoph Hellwig   xfs: use bios dir...
154
  	char		*data)
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
155
  {
6ad5b3255   Christoph Hellwig   xfs: use bios dir...
156
  	return xlog_do_io(log, blk_no, nbblks, data, REQ_OP_READ);
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
157
  }
44396476a   Dave Chinner   xfs: reset buffer...
158
  STATIC int
6ad5b3255   Christoph Hellwig   xfs: use bios dir...
159
  xlog_bread(
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
160
  	struct xlog	*log,
6ad5b3255   Christoph Hellwig   xfs: use bios dir...
161
162
163
164
  	xfs_daddr_t	blk_no,
  	int		nbblks,
  	char		*data,
  	char		**offset)
44396476a   Dave Chinner   xfs: reset buffer...
165
  {
6ad5b3255   Christoph Hellwig   xfs: use bios dir...
166
  	int		error;
44396476a   Dave Chinner   xfs: reset buffer...
167

6ad5b3255   Christoph Hellwig   xfs: use bios dir...
168
169
170
171
  	error = xlog_do_io(log, blk_no, nbblks, data, REQ_OP_READ);
  	if (!error)
  		*offset = data + xlog_align(log, blk_no);
  	return error;
44396476a   Dave Chinner   xfs: reset buffer...
172
  }
ba0f32d46   Christoph Hellwig   [XFS] mark variou...
173
  STATIC int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
174
  xlog_bwrite(
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
175
  	struct xlog	*log,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
176
177
  	xfs_daddr_t	blk_no,
  	int		nbblks,
6ad5b3255   Christoph Hellwig   xfs: use bios dir...
178
  	char		*data)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
179
  {
6ad5b3255   Christoph Hellwig   xfs: use bios dir...
180
  	return xlog_do_io(log, blk_no, nbblks, data, REQ_OP_WRITE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
181
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
182
183
184
185
186
187
188
189
190
  #ifdef DEBUG
  /*
   * dump debug superblock and log record information
   */
  STATIC void
  xlog_header_check_dump(
  	xfs_mount_t		*mp,
  	xlog_rec_header_t	*head)
  {
08e96e1a3   Eric Sandeen   xfs: remove newli...
191
  	xfs_debug(mp, "%s:  SB : uuid = %pU, fmt = %d",
03daa57cd   Joe Perches   fs/xfs/xfs_log_re...
192
  		__func__, &mp->m_sb.sb_uuid, XLOG_FMT);
08e96e1a3   Eric Sandeen   xfs: remove newli...
193
  	xfs_debug(mp, "    log : uuid = %pU, fmt = %d",
03daa57cd   Joe Perches   fs/xfs/xfs_log_re...
194
  		&head->h_fs_uuid, be32_to_cpu(head->h_fmt));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
195
196
197
198
199
200
201
202
203
204
205
206
207
  }
  #else
  #define xlog_header_check_dump(mp, head)
  #endif
  
  /*
   * check log record header for recovery
   */
  STATIC int
  xlog_header_check_recover(
  	xfs_mount_t		*mp,
  	xlog_rec_header_t	*head)
  {
69ef921b5   Christoph Hellwig   xfs: byteswap con...
208
  	ASSERT(head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
209
210
211
212
213
214
  
  	/*
  	 * IRIX doesn't write the h_fmt field and leaves it zeroed
  	 * (XLOG_FMT_UNKNOWN). This stops us from trying to recover
  	 * a dirty log created in IRIX.
  	 */
a71895c5d   Darrick J. Wong   xfs: convert open...
215
  	if (XFS_IS_CORRUPT(mp, head->h_fmt != cpu_to_be32(XLOG_FMT))) {
a0fa2b679   Dave Chinner   xfs: Convert xlog...
216
217
  		xfs_warn(mp,
  	"dirty log written in incompatible format - can't recover");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
218
  		xlog_header_check_dump(mp, head);
2451337dd   Dave Chinner   xfs: global error...
219
  		return -EFSCORRUPTED;
a71895c5d   Darrick J. Wong   xfs: convert open...
220
221
222
  	}
  	if (XFS_IS_CORRUPT(mp, !uuid_equal(&mp->m_sb.sb_uuid,
  					   &head->h_fs_uuid))) {
a0fa2b679   Dave Chinner   xfs: Convert xlog...
223
224
  		xfs_warn(mp,
  	"dirty log entry has mismatched uuid - can't recover");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
225
  		xlog_header_check_dump(mp, head);
2451337dd   Dave Chinner   xfs: global error...
226
  		return -EFSCORRUPTED;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227
228
229
230
231
232
233
234
235
236
237
238
  	}
  	return 0;
  }
  
  /*
   * read the head block of the log and check the header
   */
  STATIC int
  xlog_header_check_mount(
  	xfs_mount_t		*mp,
  	xlog_rec_header_t	*head)
  {
69ef921b5   Christoph Hellwig   xfs: byteswap con...
239
  	ASSERT(head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
240

d905fdaaa   Amir Goldstein   xfs: use the comm...
241
  	if (uuid_is_null(&head->h_fs_uuid)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
242
243
  		/*
  		 * IRIX doesn't write the h_fs_uuid or h_fmt fields. If
d905fdaaa   Amir Goldstein   xfs: use the comm...
244
  		 * h_fs_uuid is null, we assume this log was last mounted
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
245
246
  		 * by IRIX and continue.
  		 */
d905fdaaa   Amir Goldstein   xfs: use the comm...
247
  		xfs_warn(mp, "null uuid in log - IRIX style log");
a71895c5d   Darrick J. Wong   xfs: convert open...
248
249
  	} else if (XFS_IS_CORRUPT(mp, !uuid_equal(&mp->m_sb.sb_uuid,
  						  &head->h_fs_uuid))) {
a0fa2b679   Dave Chinner   xfs: Convert xlog...
250
  		xfs_warn(mp, "log has mismatched uuid - can't recover");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
251
  		xlog_header_check_dump(mp, head);
2451337dd   Dave Chinner   xfs: global error...
252
  		return -EFSCORRUPTED;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
253
254
255
  	}
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
256
257
258
259
260
261
  /*
   * This routine finds (to an approximation) the first block in the physical
   * log which contains the given cycle.  It uses a binary search algorithm.
   * Note that the algorithm can not be perfect because the disk will not
   * necessarily be perfect.
   */
a8272ce0c   David Chinner   [XFS] Fix up spar...
262
  STATIC int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
263
  xlog_find_cycle_start(
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
264
  	struct xlog	*log,
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
265
  	char		*buffer,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
266
267
268
269
  	xfs_daddr_t	first_blk,
  	xfs_daddr_t	*last_blk,
  	uint		cycle)
  {
b2a922cd6   Christoph Hellwig   xfs: remove xfs_c...
270
  	char		*offset;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
271
  	xfs_daddr_t	mid_blk;
e3bb2e30d   Alex Elder   xfs: avoid repeat...
272
  	xfs_daddr_t	end_blk;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
273
274
  	uint		mid_cycle;
  	int		error;
e3bb2e30d   Alex Elder   xfs: avoid repeat...
275
276
277
  	end_blk = *last_blk;
  	mid_blk = BLK_AVG(first_blk, end_blk);
  	while (mid_blk != first_blk && mid_blk != end_blk) {
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
278
  		error = xlog_bread(log, mid_blk, 1, buffer, &offset);
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
279
  		if (error)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
280
  			return error;
03bea6fe6   Christoph Hellwig   [XFS] clean up so...
281
  		mid_cycle = xlog_get_cycle(offset);
e3bb2e30d   Alex Elder   xfs: avoid repeat...
282
283
284
285
286
  		if (mid_cycle == cycle)
  			end_blk = mid_blk;   /* last_half_cycle == mid_cycle */
  		else
  			first_blk = mid_blk; /* first_half_cycle == mid_cycle */
  		mid_blk = BLK_AVG(first_blk, end_blk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
287
  	}
e3bb2e30d   Alex Elder   xfs: avoid repeat...
288
289
290
291
  	ASSERT((mid_blk == first_blk && mid_blk+1 == end_blk) ||
  	       (mid_blk == end_blk && mid_blk-1 == first_blk));
  
  	*last_blk = end_blk;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
292
293
294
295
296
  
  	return 0;
  }
  
  /*
3f943d853   Alex Elder   xfs: minor odds a...
297
298
299
300
301
302
   * Check that a range of blocks does not contain stop_on_cycle_no.
   * Fill in *new_blk with the block offset where such a block is
   * found, or with -1 (an invalid block number) if there is no such
   * block in the range.  The scan needs to occur from front to back
   * and the pointer into the region must be updated since a later
   * routine will need to perform another test.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
303
304
305
   */
  STATIC int
  xlog_find_verify_cycle(
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
306
  	struct xlog	*log,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
307
308
309
310
311
312
313
  	xfs_daddr_t	start_blk,
  	int		nbblks,
  	uint		stop_on_cycle_no,
  	xfs_daddr_t	*new_blk)
  {
  	xfs_daddr_t	i, j;
  	uint		cycle;
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
314
  	char		*buffer;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
315
  	xfs_daddr_t	bufblks;
b2a922cd6   Christoph Hellwig   xfs: remove xfs_c...
316
  	char		*buf = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
317
  	int		error = 0;
6881a229f   Alex Elder   xfs: fix min bufs...
318
319
320
321
322
323
  	/*
  	 * Greedily allocate a buffer big enough to handle the full
  	 * range of basic blocks we'll be examining.  If that fails,
  	 * try a smaller size.  We need to be able to read at least
  	 * a log sector, or we're out of luck.
  	 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
324
  	bufblks = 1 << ffs(nbblks);
81158e0ce   Dave Chinner   xfs: prevent need...
325
326
  	while (bufblks > log->l_logBBsize)
  		bufblks >>= 1;
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
327
  	while (!(buffer = xlog_alloc_buffer(log, bufblks))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
328
  		bufblks >>= 1;
69ce58f08   Alex Elder   xfs: record log s...
329
  		if (bufblks < log->l_sectBBsize)
2451337dd   Dave Chinner   xfs: global error...
330
  			return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
331
332
333
334
335
336
  	}
  
  	for (i = start_blk; i < start_blk + nbblks; i += bufblks) {
  		int	bcount;
  
  		bcount = min(bufblks, (start_blk + nbblks - i));
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
337
  		error = xlog_bread(log, i, bcount, buffer, &buf);
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
338
  		if (error)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
339
  			goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
340
  		for (j = 0; j < bcount; j++) {
03bea6fe6   Christoph Hellwig   [XFS] clean up so...
341
  			cycle = xlog_get_cycle(buf);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
342
343
344
345
346
347
348
349
350
351
352
353
  			if (cycle == stop_on_cycle_no) {
  				*new_blk = i+j;
  				goto out;
  			}
  
  			buf += BBSIZE;
  		}
  	}
  
  	*new_blk = -1;
  
  out:
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
354
  	kmem_free(buffer);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
355
356
  	return error;
  }
0c771b99d   Gao Xiang   xfs: clean up cal...
357
358
359
360
361
362
363
364
365
366
367
368
  static inline int
  xlog_logrec_hblks(struct xlog *log, struct xlog_rec_header *rh)
  {
  	if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
  		int	h_size = be32_to_cpu(rh->h_size);
  
  		if ((be32_to_cpu(rh->h_version) & XLOG_VERSION_2) &&
  		    h_size > XLOG_HEADER_CYCLE_SIZE)
  			return DIV_ROUND_UP(h_size, XLOG_HEADER_CYCLE_SIZE);
  	}
  	return 1;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
369
370
371
372
373
374
375
376
377
378
379
380
381
382
  /*
   * Potentially backup over partial log record write.
   *
   * In the typical case, last_blk is the number of the block directly after
   * a good log record.  Therefore, we subtract one to get the block number
   * of the last block in the given buffer.  extra_bblks contains the number
   * of blocks we would have read on a previous read.  This happens when the
   * last log record is split over the end of the physical log.
   *
   * extra_bblks is the number of blocks potentially verified on a previous
   * call to this routine.
   */
  STATIC int
  xlog_find_verify_log_record(
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
383
  	struct xlog		*log,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
384
385
386
387
388
  	xfs_daddr_t		start_blk,
  	xfs_daddr_t		*last_blk,
  	int			extra_bblks)
  {
  	xfs_daddr_t		i;
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
389
  	char			*buffer;
b2a922cd6   Christoph Hellwig   xfs: remove xfs_c...
390
  	char			*offset = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
391
392
393
394
395
396
397
  	xlog_rec_header_t	*head = NULL;
  	int			error = 0;
  	int			smallmem = 0;
  	int			num_blks = *last_blk - start_blk;
  	int			xhdrs;
  
  	ASSERT(start_blk != 0 || *last_blk != start_blk);
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
398
399
400
401
  	buffer = xlog_alloc_buffer(log, num_blks);
  	if (!buffer) {
  		buffer = xlog_alloc_buffer(log, 1);
  		if (!buffer)
2451337dd   Dave Chinner   xfs: global error...
402
  			return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
403
404
  		smallmem = 1;
  	} else {
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
405
  		error = xlog_bread(log, start_blk, num_blks, buffer, &offset);
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
406
  		if (error)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
407
  			goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
408
409
410
411
412
413
  		offset += ((num_blks - 1) << BBSHIFT);
  	}
  
  	for (i = (*last_blk) - 1; i >= 0; i--) {
  		if (i < start_blk) {
  			/* valid log record not found */
a0fa2b679   Dave Chinner   xfs: Convert xlog...
414
415
  			xfs_warn(log->l_mp,
  		"Log inconsistent (didn't find previous header)");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
416
  			ASSERT(0);
895e196fb   Darrick J. Wong   xfs: convert EIO ...
417
  			error = -EFSCORRUPTED;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
418
419
420
421
  			goto out;
  		}
  
  		if (smallmem) {
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
422
  			error = xlog_bread(log, i, 1, buffer, &offset);
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
423
  			if (error)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
424
  				goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
425
426
427
  		}
  
  		head = (xlog_rec_header_t *)offset;
69ef921b5   Christoph Hellwig   xfs: byteswap con...
428
  		if (head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
429
430
431
432
433
434
435
436
437
438
439
440
  			break;
  
  		if (!smallmem)
  			offset -= BBSIZE;
  	}
  
  	/*
  	 * We hit the beginning of the physical log & still no header.  Return
  	 * to caller.  If caller can handle a return of -1, then this routine
  	 * will be called again for the end of the physical log.
  	 */
  	if (i == -1) {
2451337dd   Dave Chinner   xfs: global error...
441
  		error = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
  		goto out;
  	}
  
  	/*
  	 * We have the final block of the good log (the first block
  	 * of the log record _before_ the head. So we check the uuid.
  	 */
  	if ((error = xlog_header_check_mount(log->l_mp, head)))
  		goto out;
  
  	/*
  	 * We may have found a log record header before we expected one.
  	 * last_blk will be the 1st block # with a given cycle #.  We may end
  	 * up reading an entire log record.  In this case, we don't want to
  	 * reset last_blk.  Only when last_blk points in the middle of a log
  	 * record do we update last_blk.
  	 */
0c771b99d   Gao Xiang   xfs: clean up cal...
459
  	xhdrs = xlog_logrec_hblks(log, head);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
460

b53e675dc   Christoph Hellwig   [XFS] xlog_rec_he...
461
462
  	if (*last_blk - i + extra_bblks !=
  	    BTOBB(be32_to_cpu(head->h_len)) + xhdrs)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
463
464
465
  		*last_blk = i;
  
  out:
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
466
  	kmem_free(buffer);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
467
468
469
470
471
  	return error;
  }
  
  /*
   * Head is defined to be the point of the log where the next log write
0a94da24b   Zhi Yong Wu   xfs: fix the comm...
472
   * could go.  This means that incomplete LR writes at the end are
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
473
474
475
476
477
478
479
480
481
482
   * eliminated when calculating the head.  We aren't guaranteed that previous
   * LR have complete transactions.  We only know that a cycle number of
   * current cycle number -1 won't be present in the log if we start writing
   * from our current block number.
   *
   * last_blk contains the block number of the first block with a given
   * cycle number.
   *
   * Return: zero if normal, non-zero if error.
   */
ba0f32d46   Christoph Hellwig   [XFS] mark variou...
483
  STATIC int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
484
  xlog_find_head(
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
485
  	struct xlog	*log,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
486
487
  	xfs_daddr_t	*return_head_blk)
  {
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
488
  	char		*buffer;
b2a922cd6   Christoph Hellwig   xfs: remove xfs_c...
489
  	char		*offset;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
490
491
492
493
494
495
496
  	xfs_daddr_t	new_blk, first_blk, start_blk, last_blk, head_blk;
  	int		num_scan_bblks;
  	uint		first_half_cycle, last_half_cycle;
  	uint		stop_on_cycle;
  	int		error, log_bbnum = log->l_logBBsize;
  
  	/* Is the end of the log device zeroed? */
2451337dd   Dave Chinner   xfs: global error...
497
498
499
500
501
502
  	error = xlog_find_zeroed(log, &first_blk);
  	if (error < 0) {
  		xfs_warn(log->l_mp, "empty log check failed");
  		return error;
  	}
  	if (error == 1) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
503
504
505
506
507
508
509
510
  		*return_head_blk = first_blk;
  
  		/* Is the whole lot zeroed? */
  		if (!first_blk) {
  			/* Linux XFS shouldn't generate totally zeroed logs -
  			 * mkfs etc write a dummy unmount record to a fresh
  			 * log so we can store the uuid in there
  			 */
a0fa2b679   Dave Chinner   xfs: Convert xlog...
511
  			xfs_warn(log->l_mp, "totally zeroed log");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
512
513
514
  		}
  
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
515
516
517
  	}
  
  	first_blk = 0;			/* get cycle # of 1st block */
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
518
519
  	buffer = xlog_alloc_buffer(log, 1);
  	if (!buffer)
2451337dd   Dave Chinner   xfs: global error...
520
  		return -ENOMEM;
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
521

6e9b3dd80   Christoph Hellwig   xfs: stop using b...
522
  	error = xlog_bread(log, 0, 1, buffer, &offset);
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
523
  	if (error)
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
524
  		goto out_free_buffer;
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
525

03bea6fe6   Christoph Hellwig   [XFS] clean up so...
526
  	first_half_cycle = xlog_get_cycle(offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
527
528
  
  	last_blk = head_blk = log_bbnum - 1;	/* get cycle # of last block */
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
529
  	error = xlog_bread(log, last_blk, 1, buffer, &offset);
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
530
  	if (error)
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
531
  		goto out_free_buffer;
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
532

03bea6fe6   Christoph Hellwig   [XFS] clean up so...
533
  	last_half_cycle = xlog_get_cycle(offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
  	ASSERT(last_half_cycle != 0);
  
  	/*
  	 * If the 1st half cycle number is equal to the last half cycle number,
  	 * then the entire log is stamped with the same cycle number.  In this
  	 * case, head_blk can't be set to zero (which makes sense).  The below
  	 * math doesn't work out properly with head_blk equal to zero.  Instead,
  	 * we set it to log_bbnum which is an invalid block number, but this
  	 * value makes the math correct.  If head_blk doesn't changed through
  	 * all the tests below, *head_blk is set to zero at the very end rather
  	 * than log_bbnum.  In a sense, log_bbnum and zero are the same block
  	 * in a circular file.
  	 */
  	if (first_half_cycle == last_half_cycle) {
  		/*
  		 * In this case we believe that the entire log should have
  		 * cycle number last_half_cycle.  We need to scan backwards
  		 * from the end verifying that there are no holes still
  		 * containing last_half_cycle - 1.  If we find such a hole,
  		 * then the start of that hole will be the new head.  The
  		 * simple case looks like
  		 *        x | x ... | x - 1 | x
  		 * Another case that fits this picture would be
  		 *        x | x + 1 | x ... | x
c41564b5a   Nathan Scott   [XFS] We really s...
558
  		 * In this case the head really is somewhere at the end of the
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
  		 * log, as one of the latest writes at the beginning was
  		 * incomplete.
  		 * One more case is
  		 *        x | x + 1 | x ... | x - 1 | x
  		 * This is really the combination of the above two cases, and
  		 * the head has to end up at the start of the x-1 hole at the
  		 * end of the log.
  		 *
  		 * In the 256k log case, we will read from the beginning to the
  		 * end of the log and search for cycle numbers equal to x-1.
  		 * We don't worry about the x+1 blocks that we encounter,
  		 * because we know that they cannot be the head since the log
  		 * started with x.
  		 */
  		head_blk = log_bbnum;
  		stop_on_cycle = last_half_cycle - 1;
  	} else {
  		/*
  		 * In this case we want to find the first block with cycle
  		 * number matching last_half_cycle.  We expect the log to be
  		 * some variation on
3f943d853   Alex Elder   xfs: minor odds a...
580
  		 *        x + 1 ... | x ... | x
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
581
582
583
584
585
586
587
588
589
  		 * The first block with cycle number x (last_half_cycle) will
  		 * be where the new head belongs.  First we do a binary search
  		 * for the first occurrence of last_half_cycle.  The binary
  		 * search may not be totally accurate, so then we scan back
  		 * from there looking for occurrences of last_half_cycle before
  		 * us.  If that backwards scan wraps around the beginning of
  		 * the log, then we look for occurrences of last_half_cycle - 1
  		 * at the end of the log.  The cases we're looking for look
  		 * like
3f943d853   Alex Elder   xfs: minor odds a...
590
591
592
  		 *                               v binary search stopped here
  		 *        x + 1 ... | x | x + 1 | x ... | x
  		 *                   ^ but we want to locate this spot
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
593
  		 * or
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
594
  		 *        <---------> less than scan distance
3f943d853   Alex Elder   xfs: minor odds a...
595
596
  		 *        x + 1 ... | x ... | x - 1 | x
  		 *                           ^ we want to locate this spot
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
597
598
  		 */
  		stop_on_cycle = last_half_cycle;
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
599
600
601
602
  		error = xlog_find_cycle_start(log, buffer, first_blk, &head_blk,
  				last_half_cycle);
  		if (error)
  			goto out_free_buffer;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
603
604
605
606
607
608
609
610
611
  	}
  
  	/*
  	 * Now validate the answer.  Scan back some number of maximum possible
  	 * blocks and make sure each one has the expected cycle number.  The
  	 * maximum is determined by the total possible amount of buffering
  	 * in the in-core log.  The following number can be made tighter if
  	 * we actually look at the block size of the filesystem.
  	 */
9f2a45058   Brian Foster   xfs: fix log bloc...
612
  	num_scan_bblks = min_t(int, log_bbnum, XLOG_TOTAL_REC_SHIFT(log));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
613
614
615
616
617
618
619
620
621
  	if (head_blk >= num_scan_bblks) {
  		/*
  		 * We are guaranteed that the entire check can be performed
  		 * in one buffer.
  		 */
  		start_blk = head_blk - num_scan_bblks;
  		if ((error = xlog_find_verify_cycle(log,
  						start_blk, num_scan_bblks,
  						stop_on_cycle, &new_blk)))
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
622
  			goto out_free_buffer;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
  		if (new_blk != -1)
  			head_blk = new_blk;
  	} else {		/* need to read 2 parts of log */
  		/*
  		 * We are going to scan backwards in the log in two parts.
  		 * First we scan the physical end of the log.  In this part
  		 * of the log, we are looking for blocks with cycle number
  		 * last_half_cycle - 1.
  		 * If we find one, then we know that the log starts there, as
  		 * we've found a hole that didn't get written in going around
  		 * the end of the physical log.  The simple case for this is
  		 *        x + 1 ... | x ... | x - 1 | x
  		 *        <---------> less than scan distance
  		 * If all of the blocks at the end of the log have cycle number
  		 * last_half_cycle, then we check the blocks at the start of
  		 * the log looking for occurrences of last_half_cycle.  If we
  		 * find one, then our current estimate for the location of the
  		 * first occurrence of last_half_cycle is wrong and we move
  		 * back to the hole we've found.  This case looks like
  		 *        x + 1 ... | x | x + 1 | x ...
  		 *                               ^ binary search stopped here
  		 * Another case we need to handle that only occurs in 256k
  		 * logs is
  		 *        x + 1 ... | x ... | x+1 | x ...
  		 *                   ^ binary search stops here
  		 * In a 256k log, the scan at the end of the log will see the
  		 * x + 1 blocks.  We need to skip past those since that is
  		 * certainly not the head of the log.  By searching for
  		 * last_half_cycle-1 we accomplish that.
  		 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
653
  		ASSERT(head_blk <= INT_MAX &&
3f943d853   Alex Elder   xfs: minor odds a...
654
655
  			(xfs_daddr_t) num_scan_bblks >= head_blk);
  		start_blk = log_bbnum - (num_scan_bblks - head_blk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
656
657
658
  		if ((error = xlog_find_verify_cycle(log, start_blk,
  					num_scan_bblks - (int)head_blk,
  					(stop_on_cycle - 1), &new_blk)))
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
659
  			goto out_free_buffer;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
660
661
  		if (new_blk != -1) {
  			head_blk = new_blk;
9db127edb   Alex Elder   xfs: change a few...
662
  			goto validate_head;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
663
664
665
666
667
668
669
670
671
672
673
674
  		}
  
  		/*
  		 * Scan beginning of log now.  The last part of the physical
  		 * log is good.  This scan needs to verify that it doesn't find
  		 * the last_half_cycle.
  		 */
  		start_blk = 0;
  		ASSERT(head_blk <= INT_MAX);
  		if ((error = xlog_find_verify_cycle(log,
  					start_blk, (int)head_blk,
  					stop_on_cycle, &new_blk)))
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
675
  			goto out_free_buffer;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
676
677
678
  		if (new_blk != -1)
  			head_blk = new_blk;
  	}
9db127edb   Alex Elder   xfs: change a few...
679
  validate_head:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
680
681
682
683
684
685
686
687
688
  	/*
  	 * Now we need to make sure head_blk is not pointing to a block in
  	 * the middle of a log record.
  	 */
  	num_scan_bblks = XLOG_REC_SHIFT(log);
  	if (head_blk >= num_scan_bblks) {
  		start_blk = head_blk - num_scan_bblks; /* don't read head_blk */
  
  		/* start ptr at last block ptr before head_blk */
2451337dd   Dave Chinner   xfs: global error...
689
690
691
692
  		error = xlog_find_verify_log_record(log, start_blk, &head_blk, 0);
  		if (error == 1)
  			error = -EIO;
  		if (error)
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
693
  			goto out_free_buffer;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
694
695
696
  	} else {
  		start_blk = 0;
  		ASSERT(head_blk <= INT_MAX);
2451337dd   Dave Chinner   xfs: global error...
697
698
  		error = xlog_find_verify_log_record(log, start_blk, &head_blk, 0);
  		if (error < 0)
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
699
  			goto out_free_buffer;
2451337dd   Dave Chinner   xfs: global error...
700
  		if (error == 1) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
701
  			/* We hit the beginning of the log during our search */
3f943d853   Alex Elder   xfs: minor odds a...
702
  			start_blk = log_bbnum - (num_scan_bblks - head_blk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
703
704
705
706
  			new_blk = log_bbnum;
  			ASSERT(start_blk <= INT_MAX &&
  				(xfs_daddr_t) log_bbnum-start_blk >= 0);
  			ASSERT(head_blk <= INT_MAX);
2451337dd   Dave Chinner   xfs: global error...
707
708
709
710
711
  			error = xlog_find_verify_log_record(log, start_blk,
  							&new_blk, (int)head_blk);
  			if (error == 1)
  				error = -EIO;
  			if (error)
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
712
  				goto out_free_buffer;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
713
714
715
  			if (new_blk != log_bbnum)
  				head_blk = new_blk;
  		} else if (error)
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
716
  			goto out_free_buffer;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
717
  	}
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
718
  	kmem_free(buffer);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
719
720
721
722
723
724
725
726
727
728
729
  	if (head_blk == log_bbnum)
  		*return_head_blk = 0;
  	else
  		*return_head_blk = head_blk;
  	/*
  	 * When returning here, we have a good block number.  Bad block
  	 * means that during a previous crash, we didn't have a clean break
  	 * from cycle number N to cycle number N-1.  In this case, we need
  	 * to find the first block with cycle number N-1.
  	 */
  	return 0;
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
730
731
  out_free_buffer:
  	kmem_free(buffer);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
732
  	if (error)
a0fa2b679   Dave Chinner   xfs: Convert xlog...
733
  		xfs_warn(log->l_mp, "failed to find log head");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
734
735
736
737
  	return error;
  }
  
  /*
eed6b462f   Brian Foster   xfs: refactor log...
738
739
740
741
742
743
744
745
746
747
748
749
750
   * Seek backwards in the log for log record headers.
   *
   * Given a starting log block, walk backwards until we find the provided number
   * of records or hit the provided tail block. The return value is the number of
   * records encountered or a negative error code. The log block and buffer
   * pointer of the last record seen are returned in rblk and rhead respectively.
   */
  STATIC int
  xlog_rseek_logrec_hdr(
  	struct xlog		*log,
  	xfs_daddr_t		head_blk,
  	xfs_daddr_t		tail_blk,
  	int			count,
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
751
  	char			*buffer,
eed6b462f   Brian Foster   xfs: refactor log...
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
  	xfs_daddr_t		*rblk,
  	struct xlog_rec_header	**rhead,
  	bool			*wrapped)
  {
  	int			i;
  	int			error;
  	int			found = 0;
  	char			*offset = NULL;
  	xfs_daddr_t		end_blk;
  
  	*wrapped = false;
  
  	/*
  	 * Walk backwards from the head block until we hit the tail or the first
  	 * block in the log.
  	 */
  	end_blk = head_blk > tail_blk ? tail_blk : 0;
  	for (i = (int) head_blk - 1; i >= end_blk; i--) {
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
770
  		error = xlog_bread(log, i, 1, buffer, &offset);
eed6b462f   Brian Foster   xfs: refactor log...
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
  		if (error)
  			goto out_error;
  
  		if (*(__be32 *) offset == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
  			*rblk = i;
  			*rhead = (struct xlog_rec_header *) offset;
  			if (++found == count)
  				break;
  		}
  	}
  
  	/*
  	 * If we haven't hit the tail block or the log record header count,
  	 * start looking again from the end of the physical log. Note that
  	 * callers can pass head == tail if the tail is not yet known.
  	 */
  	if (tail_blk >= head_blk && found != count) {
  		for (i = log->l_logBBsize - 1; i >= (int) tail_blk; i--) {
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
789
  			error = xlog_bread(log, i, 1, buffer, &offset);
eed6b462f   Brian Foster   xfs: refactor log...
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
  			if (error)
  				goto out_error;
  
  			if (*(__be32 *)offset ==
  			    cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
  				*wrapped = true;
  				*rblk = i;
  				*rhead = (struct xlog_rec_header *) offset;
  				if (++found == count)
  					break;
  			}
  		}
  	}
  
  	return found;
  
  out_error:
  	return error;
  }
  
  /*
7088c4136   Brian Foster   xfs: detect and t...
811
812
813
814
815
816
817
818
819
820
821
822
823
824
   * Seek forward in the log for log record headers.
   *
   * Given head and tail blocks, walk forward from the tail block until we find
   * the provided number of records or hit the head block. The return value is the
   * number of records encountered or a negative error code. The log block and
   * buffer pointer of the last record seen are returned in rblk and rhead
   * respectively.
   */
  STATIC int
  xlog_seek_logrec_hdr(
  	struct xlog		*log,
  	xfs_daddr_t		head_blk,
  	xfs_daddr_t		tail_blk,
  	int			count,
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
825
  	char			*buffer,
7088c4136   Brian Foster   xfs: detect and t...
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
  	xfs_daddr_t		*rblk,
  	struct xlog_rec_header	**rhead,
  	bool			*wrapped)
  {
  	int			i;
  	int			error;
  	int			found = 0;
  	char			*offset = NULL;
  	xfs_daddr_t		end_blk;
  
  	*wrapped = false;
  
  	/*
  	 * Walk forward from the tail block until we hit the head or the last
  	 * block in the log.
  	 */
  	end_blk = head_blk > tail_blk ? head_blk : log->l_logBBsize - 1;
  	for (i = (int) tail_blk; i <= end_blk; i++) {
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
844
  		error = xlog_bread(log, i, 1, buffer, &offset);
7088c4136   Brian Foster   xfs: detect and t...
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
  		if (error)
  			goto out_error;
  
  		if (*(__be32 *) offset == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
  			*rblk = i;
  			*rhead = (struct xlog_rec_header *) offset;
  			if (++found == count)
  				break;
  		}
  	}
  
  	/*
  	 * If we haven't hit the head block or the log record header count,
  	 * start looking again from the start of the physical log.
  	 */
  	if (tail_blk > head_blk && found != count) {
  		for (i = 0; i < (int) head_blk; i++) {
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
862
  			error = xlog_bread(log, i, 1, buffer, &offset);
7088c4136   Brian Foster   xfs: detect and t...
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
  			if (error)
  				goto out_error;
  
  			if (*(__be32 *)offset ==
  			    cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
  				*wrapped = true;
  				*rblk = i;
  				*rhead = (struct xlog_rec_header *) offset;
  				if (++found == count)
  					break;
  			}
  		}
  	}
  
  	return found;
  
  out_error:
  	return error;
  }
  
  /*
4a4f66eac   Brian Foster   xfs: fix log reco...
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
   * Calculate distance from head to tail (i.e., unused space in the log).
   */
  static inline int
  xlog_tail_distance(
  	struct xlog	*log,
  	xfs_daddr_t	head_blk,
  	xfs_daddr_t	tail_blk)
  {
  	if (head_blk < tail_blk)
  		return tail_blk - head_blk;
  
  	return tail_blk + (log->l_logBBsize - head_blk);
  }
  
  /*
   * Verify the log tail. This is particularly important when torn or incomplete
   * writes have been detected near the front of the log and the head has been
   * walked back accordingly.
   *
   * We also have to handle the case where the tail was pinned and the head
   * blocked behind the tail right before a crash. If the tail had been pushed
   * immediately prior to the crash and the subsequent checkpoint was only
   * partially written, it's possible it overwrote the last referenced tail in the
   * log with garbage. This is not a coherency problem because the tail must have
   * been pushed before it can be overwritten, but appears as log corruption to
   * recovery because we have no way to know the tail was updated if the
   * subsequent checkpoint didn't write successfully.
7088c4136   Brian Foster   xfs: detect and t...
911
   *
4a4f66eac   Brian Foster   xfs: fix log reco...
912
913
914
915
   * Therefore, CRC check the log from tail to head. If a failure occurs and the
   * offending record is within max iclog bufs from the head, walk the tail
   * forward and retry until a valid tail is found or corruption is detected out
   * of the range of a possible overwrite.
7088c4136   Brian Foster   xfs: detect and t...
916
917
918
919
920
   */
  STATIC int
  xlog_verify_tail(
  	struct xlog		*log,
  	xfs_daddr_t		head_blk,
4a4f66eac   Brian Foster   xfs: fix log reco...
921
922
  	xfs_daddr_t		*tail_blk,
  	int			hsize)
7088c4136   Brian Foster   xfs: detect and t...
923
924
  {
  	struct xlog_rec_header	*thead;
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
925
  	char			*buffer;
7088c4136   Brian Foster   xfs: detect and t...
926
  	xfs_daddr_t		first_bad;
7088c4136   Brian Foster   xfs: detect and t...
927
928
  	int			error = 0;
  	bool			wrapped;
4a4f66eac   Brian Foster   xfs: fix log reco...
929
930
  	xfs_daddr_t		tmp_tail;
  	xfs_daddr_t		orig_tail = *tail_blk;
7088c4136   Brian Foster   xfs: detect and t...
931

6e9b3dd80   Christoph Hellwig   xfs: stop using b...
932
933
  	buffer = xlog_alloc_buffer(log, 1);
  	if (!buffer)
7088c4136   Brian Foster   xfs: detect and t...
934
935
936
  		return -ENOMEM;
  
  	/*
4a4f66eac   Brian Foster   xfs: fix log reco...
937
938
  	 * Make sure the tail points to a record (returns positive count on
  	 * success).
7088c4136   Brian Foster   xfs: detect and t...
939
  	 */
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
940
  	error = xlog_seek_logrec_hdr(log, head_blk, *tail_blk, 1, buffer,
4a4f66eac   Brian Foster   xfs: fix log reco...
941
942
  			&tmp_tail, &thead, &wrapped);
  	if (error < 0)
7088c4136   Brian Foster   xfs: detect and t...
943
  		goto out;
4a4f66eac   Brian Foster   xfs: fix log reco...
944
945
  	if (*tail_blk != tmp_tail)
  		*tail_blk = tmp_tail;
7088c4136   Brian Foster   xfs: detect and t...
946
947
  
  	/*
4a4f66eac   Brian Foster   xfs: fix log reco...
948
949
950
951
952
  	 * Run a CRC check from the tail to the head. We can't just check
  	 * MAX_ICLOGS records past the tail because the tail may point to stale
  	 * blocks cleared during the search for the head/tail. These blocks are
  	 * overwritten with zero-length records and thus record count is not a
  	 * reliable indicator of the iclog state before a crash.
7088c4136   Brian Foster   xfs: detect and t...
953
  	 */
4a4f66eac   Brian Foster   xfs: fix log reco...
954
955
  	first_bad = 0;
  	error = xlog_do_recovery_pass(log, head_blk, *tail_blk,
7088c4136   Brian Foster   xfs: detect and t...
956
  				      XLOG_RECOVER_CRCPASS, &first_bad);
a4c9b34d6   Brian Foster   xfs: handle -EFSC...
957
  	while ((error == -EFSBADCRC || error == -EFSCORRUPTED) && first_bad) {
4a4f66eac   Brian Foster   xfs: fix log reco...
958
959
960
961
962
963
964
965
966
  		int	tail_distance;
  
  		/*
  		 * Is corruption within range of the head? If so, retry from
  		 * the next record. Otherwise return an error.
  		 */
  		tail_distance = xlog_tail_distance(log, head_blk, first_bad);
  		if (tail_distance > BTOBB(XLOG_MAX_ICLOGS * hsize))
  			break;
7088c4136   Brian Foster   xfs: detect and t...
967

4a4f66eac   Brian Foster   xfs: fix log reco...
968
  		/* skip to the next record; returns positive count on success */
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
969
970
  		error = xlog_seek_logrec_hdr(log, head_blk, first_bad, 2,
  				buffer, &tmp_tail, &thead, &wrapped);
4a4f66eac   Brian Foster   xfs: fix log reco...
971
972
973
974
975
976
977
978
979
980
981
982
983
  		if (error < 0)
  			goto out;
  
  		*tail_blk = tmp_tail;
  		first_bad = 0;
  		error = xlog_do_recovery_pass(log, head_blk, *tail_blk,
  					      XLOG_RECOVER_CRCPASS, &first_bad);
  	}
  
  	if (!error && *tail_blk != orig_tail)
  		xfs_warn(log->l_mp,
  		"Tail block (0x%llx) overwrite detected. Updated to 0x%llx",
  			 orig_tail, *tail_blk);
7088c4136   Brian Foster   xfs: detect and t...
984
  out:
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
985
  	kmem_free(buffer);
7088c4136   Brian Foster   xfs: detect and t...
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
  	return error;
  }
  
  /*
   * Detect and trim torn writes from the head of the log.
   *
   * Storage without sector atomicity guarantees can result in torn writes in the
   * log in the event of a crash. Our only means to detect this scenario is via
   * CRC verification. While we can't always be certain that CRC verification
   * failure is due to a torn write vs. an unrelated corruption, we do know that
   * only a certain number (XLOG_MAX_ICLOGS) of log records can be written out at
   * one time. Therefore, CRC verify up to XLOG_MAX_ICLOGS records at the head of
   * the log and treat failures in this range as torn writes as a matter of
   * policy. In the event of CRC failure, the head is walked back to the last good
   * record in the log and the tail is updated from that record and verified.
   */
  STATIC int
  xlog_verify_head(
  	struct xlog		*log,
  	xfs_daddr_t		*head_blk,	/* in/out: unverified head */
  	xfs_daddr_t		*tail_blk,	/* out: tail block */
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1007
  	char			*buffer,
7088c4136   Brian Foster   xfs: detect and t...
1008
1009
1010
1011
1012
  	xfs_daddr_t		*rhead_blk,	/* start blk of last record */
  	struct xlog_rec_header	**rhead,	/* ptr to last record */
  	bool			*wrapped)	/* last rec. wraps phys. log */
  {
  	struct xlog_rec_header	*tmp_rhead;
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1013
  	char			*tmp_buffer;
7088c4136   Brian Foster   xfs: detect and t...
1014
1015
1016
1017
1018
1019
1020
  	xfs_daddr_t		first_bad;
  	xfs_daddr_t		tmp_rhead_blk;
  	int			found;
  	int			error;
  	bool			tmp_wrapped;
  
  	/*
82ff6cc26   Brian Foster   xfs: separate log...
1021
1022
1023
  	 * Check the head of the log for torn writes. Search backwards from the
  	 * head until we hit the tail or the maximum number of log record I/Os
  	 * that could have been in flight at one time. Use a temporary buffer so
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1024
  	 * we don't trash the rhead/buffer pointers from the caller.
7088c4136   Brian Foster   xfs: detect and t...
1025
  	 */
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1026
1027
  	tmp_buffer = xlog_alloc_buffer(log, 1);
  	if (!tmp_buffer)
7088c4136   Brian Foster   xfs: detect and t...
1028
1029
  		return -ENOMEM;
  	error = xlog_rseek_logrec_hdr(log, *head_blk, *tail_blk,
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1030
1031
1032
  				      XLOG_MAX_ICLOGS, tmp_buffer,
  				      &tmp_rhead_blk, &tmp_rhead, &tmp_wrapped);
  	kmem_free(tmp_buffer);
7088c4136   Brian Foster   xfs: detect and t...
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
  	if (error < 0)
  		return error;
  
  	/*
  	 * Now run a CRC verification pass over the records starting at the
  	 * block found above to the current head. If a CRC failure occurs, the
  	 * log block of the first bad record is saved in first_bad.
  	 */
  	error = xlog_do_recovery_pass(log, *head_blk, tmp_rhead_blk,
  				      XLOG_RECOVER_CRCPASS, &first_bad);
a4c9b34d6   Brian Foster   xfs: handle -EFSC...
1043
  	if ((error == -EFSBADCRC || error == -EFSCORRUPTED) && first_bad) {
7088c4136   Brian Foster   xfs: detect and t...
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
  		/*
  		 * We've hit a potential torn write. Reset the error and warn
  		 * about it.
  		 */
  		error = 0;
  		xfs_warn(log->l_mp,
  "Torn write (CRC failure) detected at log block 0x%llx. Truncating head block from 0x%llx.",
  			 first_bad, *head_blk);
  
  		/*
  		 * Get the header block and buffer pointer for the last good
  		 * record before the bad record.
  		 *
  		 * Note that xlog_find_tail() clears the blocks at the new head
  		 * (i.e., the records with invalid CRC) if the cycle number
b63da6c8d   Randy Dunlap   xfs: delete dupli...
1059
  		 * matches the current cycle.
7088c4136   Brian Foster   xfs: detect and t...
1060
  		 */
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1061
1062
  		found = xlog_rseek_logrec_hdr(log, first_bad, *tail_blk, 1,
  				buffer, rhead_blk, rhead, wrapped);
7088c4136   Brian Foster   xfs: detect and t...
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
  		if (found < 0)
  			return found;
  		if (found == 0)		/* XXX: right thing to do here? */
  			return -EIO;
  
  		/*
  		 * Reset the head block to the starting block of the first bad
  		 * log record and set the tail block based on the last good
  		 * record.
  		 *
  		 * Bail out if the updated head/tail match as this indicates
  		 * possible corruption outside of the acceptable
  		 * (XLOG_MAX_ICLOGS) range. This is a job for xfs_repair...
  		 */
  		*head_blk = first_bad;
  		*tail_blk = BLOCK_LSN(be64_to_cpu((*rhead)->h_tail_lsn));
  		if (*head_blk == *tail_blk) {
  			ASSERT(0);
  			return 0;
  		}
7088c4136   Brian Foster   xfs: detect and t...
1083
  	}
5297ac1f6   Brian Foster   xfs: always verif...
1084
1085
  	if (error)
  		return error;
7088c4136   Brian Foster   xfs: detect and t...
1086

4a4f66eac   Brian Foster   xfs: fix log reco...
1087
1088
  	return xlog_verify_tail(log, *head_blk, tail_blk,
  				be32_to_cpu((*rhead)->h_size));
7088c4136   Brian Foster   xfs: detect and t...
1089
1090
1091
  }
  
  /*
0703a8e1c   Dave Chinner   xfs: replace do_m...
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
   * We need to make sure we handle log wrapping properly, so we can't use the
   * calculated logbno directly. Make sure it wraps to the correct bno inside the
   * log.
   *
   * The log is limited to 32 bit sizes, so we use the appropriate modulus
   * operation here and cast it back to a 64 bit daddr on return.
   */
  static inline xfs_daddr_t
  xlog_wrap_logbno(
  	struct xlog		*log,
  	xfs_daddr_t		bno)
  {
  	int			mod;
  
  	div_s64_rem(bno, log->l_logBBsize, &mod);
  	return mod;
  }
  
  /*
65b99a08b   Brian Foster   xfs: refactor unm...
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
   * Check whether the head of the log points to an unmount record. In other
   * words, determine whether the log is clean. If so, update the in-core state
   * appropriately.
   */
  static int
  xlog_check_unmount_rec(
  	struct xlog		*log,
  	xfs_daddr_t		*head_blk,
  	xfs_daddr_t		*tail_blk,
  	struct xlog_rec_header	*rhead,
  	xfs_daddr_t		rhead_blk,
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1122
  	char			*buffer,
65b99a08b   Brian Foster   xfs: refactor unm...
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
  	bool			*clean)
  {
  	struct xlog_op_header	*op_head;
  	xfs_daddr_t		umount_data_blk;
  	xfs_daddr_t		after_umount_blk;
  	int			hblks;
  	int			error;
  	char			*offset;
  
  	*clean = false;
  
  	/*
  	 * Look for unmount record. If we find it, then we know there was a
  	 * clean unmount. Since 'i' could be the last block in the physical
  	 * log, we convert to a log block before comparing to the head_blk.
  	 *
  	 * Save the current tail lsn to use to pass to xlog_clear_stale_blocks()
  	 * below. We won't want to clear the unmount record if there is one, so
  	 * we pass the lsn of the unmount record rather than the block after it.
  	 */
0c771b99d   Gao Xiang   xfs: clean up cal...
1143
  	hblks = xlog_logrec_hblks(log, rhead);
0703a8e1c   Dave Chinner   xfs: replace do_m...
1144
1145
  	after_umount_blk = xlog_wrap_logbno(log,
  			rhead_blk + hblks + BTOBB(be32_to_cpu(rhead->h_len)));
65b99a08b   Brian Foster   xfs: refactor unm...
1146
1147
  	if (*head_blk == after_umount_blk &&
  	    be32_to_cpu(rhead->h_num_logops) == 1) {
0703a8e1c   Dave Chinner   xfs: replace do_m...
1148
  		umount_data_blk = xlog_wrap_logbno(log, rhead_blk + hblks);
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1149
  		error = xlog_bread(log, umount_data_blk, 1, buffer, &offset);
65b99a08b   Brian Foster   xfs: refactor unm...
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
  		if (error)
  			return error;
  
  		op_head = (struct xlog_op_header *)offset;
  		if (op_head->oh_flags & XLOG_UNMOUNT_TRANS) {
  			/*
  			 * Set tail and last sync so that newly written log
  			 * records will point recovery to after the current
  			 * unmount record.
  			 */
  			xlog_assign_atomic_lsn(&log->l_tail_lsn,
  					log->l_curr_cycle, after_umount_blk);
  			xlog_assign_atomic_lsn(&log->l_last_sync_lsn,
  					log->l_curr_cycle, after_umount_blk);
  			*tail_blk = after_umount_blk;
  
  			*clean = true;
  		}
  	}
  
  	return 0;
  }
717bc0ebc   Brian Foster   xfs: refactor in-...
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
  static void
  xlog_set_state(
  	struct xlog		*log,
  	xfs_daddr_t		head_blk,
  	struct xlog_rec_header	*rhead,
  	xfs_daddr_t		rhead_blk,
  	bool			bump_cycle)
  {
  	/*
  	 * Reset log values according to the state of the log when we
  	 * crashed.  In the case where head_blk == 0, we bump curr_cycle
  	 * one because the next write starts a new cycle rather than
  	 * continuing the cycle of the last good log record.  At this
  	 * point we have guaranteed that all partial log records have been
  	 * accounted for.  Therefore, we know that the last good log record
  	 * written was complete and ended exactly on the end boundary
  	 * of the physical log.
  	 */
  	log->l_prev_block = rhead_blk;
  	log->l_curr_block = (int)head_blk;
  	log->l_curr_cycle = be32_to_cpu(rhead->h_cycle);
  	if (bump_cycle)
  		log->l_curr_cycle++;
  	atomic64_set(&log->l_tail_lsn, be64_to_cpu(rhead->h_tail_lsn));
  	atomic64_set(&log->l_last_sync_lsn, be64_to_cpu(rhead->h_lsn));
  	xlog_assign_grant_head(&log->l_reserve_head.grant, log->l_curr_cycle,
  					BBTOB(log->l_curr_block));
  	xlog_assign_grant_head(&log->l_write_head.grant, log->l_curr_cycle,
  					BBTOB(log->l_curr_block));
  }
65b99a08b   Brian Foster   xfs: refactor unm...
1202
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
   * Find the sync block number or the tail of the log.
   *
   * This will be the block number of the last record to have its
   * associated buffers synced to disk.  Every log record header has
   * a sync lsn embedded in it.  LSNs hold block numbers, so it is easy
   * to get a sync block number.  The only concern is to figure out which
   * log record header to believe.
   *
   * The following algorithm uses the log record header with the largest
   * lsn.  The entire log record does not need to be valid.  We only care
   * that the header is valid.
   *
   * We could speed up search by using current head_blk buffer, but it is not
   * available.
   */
5d77c0dc0   Eric Sandeen   xfs: make several...
1218
  STATIC int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1219
  xlog_find_tail(
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
1220
  	struct xlog		*log,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1221
  	xfs_daddr_t		*head_blk,
65be60541   Eric Sandeen   [XFS] remove unus...
1222
  	xfs_daddr_t		*tail_blk)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1223
1224
  {
  	xlog_rec_header_t	*rhead;
b2a922cd6   Christoph Hellwig   xfs: remove xfs_c...
1225
  	char			*offset = NULL;
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1226
  	char			*buffer;
7088c4136   Brian Foster   xfs: detect and t...
1227
  	int			error;
7088c4136   Brian Foster   xfs: detect and t...
1228
  	xfs_daddr_t		rhead_blk;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1229
  	xfs_lsn_t		tail_lsn;
eed6b462f   Brian Foster   xfs: refactor log...
1230
  	bool			wrapped = false;
65b99a08b   Brian Foster   xfs: refactor unm...
1231
  	bool			clean = false;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1232
1233
1234
1235
1236
1237
  
  	/*
  	 * Find previous log record
  	 */
  	if ((error = xlog_find_head(log, head_blk)))
  		return error;
82ff6cc26   Brian Foster   xfs: separate log...
1238
  	ASSERT(*head_blk < INT_MAX);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1239

6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1240
1241
  	buffer = xlog_alloc_buffer(log, 1);
  	if (!buffer)
2451337dd   Dave Chinner   xfs: global error...
1242
  		return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1243
  	if (*head_blk == 0) {				/* special case */
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1244
  		error = xlog_bread(log, 0, 1, buffer, &offset);
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
1245
  		if (error)
9db127edb   Alex Elder   xfs: change a few...
1246
  			goto done;
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
1247

03bea6fe6   Christoph Hellwig   [XFS] clean up so...
1248
  		if (xlog_get_cycle(offset) == 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1249
1250
  			*tail_blk = 0;
  			/* leave all other log inited values alone */
9db127edb   Alex Elder   xfs: change a few...
1251
  			goto done;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1252
1253
1254
1255
  		}
  	}
  
  	/*
82ff6cc26   Brian Foster   xfs: separate log...
1256
1257
1258
  	 * Search backwards through the log looking for the log record header
  	 * block. This wraps all the way back around to the head so something is
  	 * seriously wrong if we can't find it.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1259
  	 */
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1260
  	error = xlog_rseek_logrec_hdr(log, *head_blk, *head_blk, 1, buffer,
82ff6cc26   Brian Foster   xfs: separate log...
1261
1262
  				      &rhead_blk, &rhead, &wrapped);
  	if (error < 0)
050552cbe   Darrick J. Wong   xfs: fix some mem...
1263
  		goto done;
82ff6cc26   Brian Foster   xfs: separate log...
1264
1265
  	if (!error) {
  		xfs_warn(log->l_mp, "%s: couldn't find sync record", __func__);
050552cbe   Darrick J. Wong   xfs: fix some mem...
1266
1267
  		error = -EFSCORRUPTED;
  		goto done;
82ff6cc26   Brian Foster   xfs: separate log...
1268
1269
  	}
  	*tail_blk = BLOCK_LSN(be64_to_cpu(rhead->h_tail_lsn));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1270
1271
  
  	/*
717bc0ebc   Brian Foster   xfs: refactor in-...
1272
  	 * Set the log state based on the current head record.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1273
  	 */
717bc0ebc   Brian Foster   xfs: refactor in-...
1274
  	xlog_set_state(log, *head_blk, rhead, rhead_blk, wrapped);
65b99a08b   Brian Foster   xfs: refactor unm...
1275
  	tail_lsn = atomic64_read(&log->l_tail_lsn);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1276
1277
  
  	/*
65b99a08b   Brian Foster   xfs: refactor unm...
1278
1279
  	 * Look for an unmount record at the head of the log. This sets the log
  	 * state to determine whether recovery is necessary.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1280
  	 */
65b99a08b   Brian Foster   xfs: refactor unm...
1281
  	error = xlog_check_unmount_rec(log, head_blk, tail_blk, rhead,
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1282
  				       rhead_blk, buffer, &clean);
65b99a08b   Brian Foster   xfs: refactor unm...
1283
1284
  	if (error)
  		goto done;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1285
1286
  
  	/*
7f6aff3a2   Brian Foster   xfs: only run tor...
1287
1288
1289
1290
  	 * Verify the log head if the log is not clean (e.g., we have anything
  	 * but an unmount record at the head). This uses CRC verification to
  	 * detect and trim torn writes. If discovered, CRC failures are
  	 * considered torn writes and the log head is trimmed accordingly.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1291
  	 *
7f6aff3a2   Brian Foster   xfs: only run tor...
1292
1293
1294
  	 * Note that we can only run CRC verification when the log is dirty
  	 * because there's no guarantee that the log data behind an unmount
  	 * record is compatible with the current architecture.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1295
  	 */
7f6aff3a2   Brian Foster   xfs: only run tor...
1296
1297
  	if (!clean) {
  		xfs_daddr_t	orig_head = *head_blk;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1298

6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1299
  		error = xlog_verify_head(log, head_blk, tail_blk, buffer,
7f6aff3a2   Brian Foster   xfs: only run tor...
1300
  					 &rhead_blk, &rhead, &wrapped);
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
1301
  		if (error)
9db127edb   Alex Elder   xfs: change a few...
1302
  			goto done;
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
1303

7f6aff3a2   Brian Foster   xfs: only run tor...
1304
1305
1306
1307
1308
1309
  		/* update in-core state again if the head changed */
  		if (*head_blk != orig_head) {
  			xlog_set_state(log, *head_blk, rhead, rhead_blk,
  				       wrapped);
  			tail_lsn = atomic64_read(&log->l_tail_lsn);
  			error = xlog_check_unmount_rec(log, head_blk, tail_blk,
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1310
  						       rhead, rhead_blk, buffer,
7f6aff3a2   Brian Foster   xfs: only run tor...
1311
1312
1313
  						       &clean);
  			if (error)
  				goto done;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1314
1315
1316
1317
  		}
  	}
  
  	/*
65b99a08b   Brian Foster   xfs: refactor unm...
1318
1319
1320
1321
1322
1323
  	 * Note that the unmount was clean. If the unmount was not clean, we
  	 * need to know this to rebuild the superblock counters from the perag
  	 * headers if we have a filesystem using non-persistent counters.
  	 */
  	if (clean)
  		log->l_mp->m_flags |= XFS_MOUNT_WAS_CLEAN;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
  
  	/*
  	 * Make sure that there are no blocks in front of the head
  	 * with the same cycle number as the head.  This can happen
  	 * because we allow multiple outstanding log writes concurrently,
  	 * and the later writes might make it out before earlier ones.
  	 *
  	 * We use the lsn from before modifying it so that we'll never
  	 * overwrite the unmount record after a clean unmount.
  	 *
  	 * Do this only if we are going to recover the filesystem
  	 *
  	 * NOTE: This used to say "if (!readonly)"
  	 * However on Linux, we can & do recover a read-only filesystem.
  	 * We only skip recovery if NORECOVERY is specified on mount,
  	 * in which case we would not be here.
  	 *
  	 * But... if the -device- itself is readonly, just skip this.
  	 * We can't recover this device anyway, so it won't matter.
  	 */
2d15d2c0e   Christoph Hellwig   xfs: make use of ...
1344
  	if (!xfs_readonly_buftarg(log->l_targ))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1345
  		error = xlog_clear_stale_blocks(log, tail_lsn);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1346

9db127edb   Alex Elder   xfs: change a few...
1347
  done:
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1348
  	kmem_free(buffer);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1349
1350
  
  	if (error)
a0fa2b679   Dave Chinner   xfs: Convert xlog...
1351
  		xfs_warn(log->l_mp, "failed to locate log tail");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
  	return error;
  }
  
  /*
   * Is the log zeroed at all?
   *
   * The last binary search should be changed to perform an X block read
   * once X becomes small enough.  You can then search linearly through
   * the X blocks.  This will cut down on the number of reads we need to do.
   *
   * If the log is partially zeroed, this routine will pass back the blkno
   * of the first block with cycle number 0.  It won't have a complete LR
   * preceding it.
   *
   * Return:
   *	0  => the log is completely written to
2451337dd   Dave Chinner   xfs: global error...
1368
1369
   *	1 => use *blk_no as the first block of the log
   *	<0 => error has occurred
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1370
   */
a8272ce0c   David Chinner   [XFS] Fix up spar...
1371
  STATIC int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1372
  xlog_find_zeroed(
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
1373
  	struct xlog	*log,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1374
1375
  	xfs_daddr_t	*blk_no)
  {
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1376
  	char		*buffer;
b2a922cd6   Christoph Hellwig   xfs: remove xfs_c...
1377
  	char		*offset;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1378
1379
1380
1381
  	uint	        first_cycle, last_cycle;
  	xfs_daddr_t	new_blk, last_blk, start_blk;
  	xfs_daddr_t     num_scan_bblks;
  	int	        error, log_bbnum = log->l_logBBsize;
6fdf8ccc0   Nathan Scott   [XFS] Rework code...
1382
  	*blk_no = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1383
  	/* check totally zeroed log */
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1384
1385
  	buffer = xlog_alloc_buffer(log, 1);
  	if (!buffer)
2451337dd   Dave Chinner   xfs: global error...
1386
  		return -ENOMEM;
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1387
  	error = xlog_bread(log, 0, 1, buffer, &offset);
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
1388
  	if (error)
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1389
  		goto out_free_buffer;
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
1390

03bea6fe6   Christoph Hellwig   [XFS] clean up so...
1391
  	first_cycle = xlog_get_cycle(offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1392
1393
  	if (first_cycle == 0) {		/* completely zeroed log */
  		*blk_no = 0;
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1394
  		kmem_free(buffer);
2451337dd   Dave Chinner   xfs: global error...
1395
  		return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1396
1397
1398
  	}
  
  	/* check partially zeroed log */
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1399
  	error = xlog_bread(log, log_bbnum-1, 1, buffer, &offset);
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
1400
  	if (error)
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1401
  		goto out_free_buffer;
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
1402

03bea6fe6   Christoph Hellwig   [XFS] clean up so...
1403
  	last_cycle = xlog_get_cycle(offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1404
  	if (last_cycle != 0) {		/* log completely written to */
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1405
  		kmem_free(buffer);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1406
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1407
1408
1409
1410
  	}
  
  	/* we have a partially zeroed log */
  	last_blk = log_bbnum-1;
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1411
1412
1413
  	error = xlog_find_cycle_start(log, buffer, 0, &last_blk, 0);
  	if (error)
  		goto out_free_buffer;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
  
  	/*
  	 * Validate the answer.  Because there is no way to guarantee that
  	 * the entire log is made up of log records which are the same size,
  	 * we scan over the defined maximum blocks.  At this point, the maximum
  	 * is not chosen to mean anything special.   XXXmiken
  	 */
  	num_scan_bblks = XLOG_TOTAL_REC_SHIFT(log);
  	ASSERT(num_scan_bblks <= INT_MAX);
  
  	if (last_blk < num_scan_bblks)
  		num_scan_bblks = last_blk;
  	start_blk = last_blk - num_scan_bblks;
  
  	/*
  	 * We search for any instances of cycle number 0 that occur before
  	 * our current estimate of the head.  What we're trying to detect is
  	 *        1 ... | 0 | 1 | 0...
  	 *                       ^ binary search ends here
  	 */
  	if ((error = xlog_find_verify_cycle(log, start_blk,
  					 (int)num_scan_bblks, 0, &new_blk)))
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1436
  		goto out_free_buffer;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1437
1438
1439
1440
1441
1442
1443
  	if (new_blk != -1)
  		last_blk = new_blk;
  
  	/*
  	 * Potentially backup over partial log record write.  We don't need
  	 * to search the end of the log because we know it is zero.
  	 */
2451337dd   Dave Chinner   xfs: global error...
1444
1445
1446
1447
  	error = xlog_find_verify_log_record(log, start_blk, &last_blk, 0);
  	if (error == 1)
  		error = -EIO;
  	if (error)
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1448
  		goto out_free_buffer;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1449
1450
  
  	*blk_no = last_blk;
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1451
1452
  out_free_buffer:
  	kmem_free(buffer);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1453
1454
  	if (error)
  		return error;
2451337dd   Dave Chinner   xfs: global error...
1455
  	return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1456
1457
1458
1459
1460
1461
1462
1463
1464
  }
  
  /*
   * These are simple subroutines used by xlog_clear_stale_blocks() below
   * to initialize a buffer full of empty log record headers and write
   * them into the log.
   */
  STATIC void
  xlog_add_record(
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
1465
  	struct xlog		*log,
b2a922cd6   Christoph Hellwig   xfs: remove xfs_c...
1466
  	char			*buf,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1467
1468
1469
1470
1471
1472
1473
1474
  	int			cycle,
  	int			block,
  	int			tail_cycle,
  	int			tail_block)
  {
  	xlog_rec_header_t	*recp = (xlog_rec_header_t *)buf;
  
  	memset(buf, 0, BBSIZE);
b53e675dc   Christoph Hellwig   [XFS] xlog_rec_he...
1475
1476
1477
  	recp->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
  	recp->h_cycle = cpu_to_be32(cycle);
  	recp->h_version = cpu_to_be32(
621187099   Eric Sandeen   [XFS] remove shou...
1478
  			xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
b53e675dc   Christoph Hellwig   [XFS] xlog_rec_he...
1479
1480
1481
  	recp->h_lsn = cpu_to_be64(xlog_assign_lsn(cycle, block));
  	recp->h_tail_lsn = cpu_to_be64(xlog_assign_lsn(tail_cycle, tail_block));
  	recp->h_fmt = cpu_to_be32(XLOG_FMT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1482
1483
1484
1485
1486
  	memcpy(&recp->h_fs_uuid, &log->l_mp->m_sb.sb_uuid, sizeof(uuid_t));
  }
  
  STATIC int
  xlog_write_log_records(
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
1487
  	struct xlog	*log,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1488
1489
1490
1491
1492
1493
  	int		cycle,
  	int		start_block,
  	int		blocks,
  	int		tail_cycle,
  	int		tail_block)
  {
b2a922cd6   Christoph Hellwig   xfs: remove xfs_c...
1494
  	char		*offset;
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1495
  	char		*buffer;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1496
  	int		balign, ealign;
69ce58f08   Alex Elder   xfs: record log s...
1497
  	int		sectbb = log->l_sectBBsize;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1498
1499
1500
1501
  	int		end_block = start_block + blocks;
  	int		bufblks;
  	int		error = 0;
  	int		i, j = 0;
6881a229f   Alex Elder   xfs: fix min bufs...
1502
1503
1504
1505
1506
1507
  	/*
  	 * Greedily allocate a buffer big enough to handle the full
  	 * range of basic blocks to be written.  If that fails, try
  	 * a smaller size.  We need to be able to write at least a
  	 * log sector, or we're out of luck.
  	 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1508
  	bufblks = 1 << ffs(blocks);
81158e0ce   Dave Chinner   xfs: prevent need...
1509
1510
  	while (bufblks > log->l_logBBsize)
  		bufblks >>= 1;
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1511
  	while (!(buffer = xlog_alloc_buffer(log, bufblks))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1512
  		bufblks >>= 1;
69ce58f08   Alex Elder   xfs: record log s...
1513
  		if (bufblks < sectbb)
2451337dd   Dave Chinner   xfs: global error...
1514
  			return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1515
1516
1517
1518
1519
1520
  	}
  
  	/* We may need to do a read at the start to fill in part of
  	 * the buffer in the starting sector not covered by the first
  	 * write below.
  	 */
5c17f5339   Alex Elder   xfs: kill XLOG_SE...
1521
  	balign = round_down(start_block, sectbb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1522
  	if (balign != start_block) {
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1523
  		error = xlog_bread_noalign(log, start_block, 1, buffer);
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
1524
  		if (error)
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1525
  			goto out_free_buffer;
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
1526

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
  		j = start_block - balign;
  	}
  
  	for (i = start_block; i < end_block; i += bufblks) {
  		int		bcount, endcount;
  
  		bcount = min(bufblks, end_block - start_block);
  		endcount = bcount - j;
  
  		/* We may need to do a read at the end to fill in part of
  		 * the buffer in the final sector not covered by the write.
  		 * If this is the same sector as the above read, skip it.
  		 */
5c17f5339   Alex Elder   xfs: kill XLOG_SE...
1540
  		ealign = round_down(end_block, sectbb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1541
  		if (j == 0 && (start_block + endcount > ealign)) {
6ad5b3255   Christoph Hellwig   xfs: use bios dir...
1542
  			error = xlog_bread_noalign(log, ealign, sectbb,
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1543
  					buffer + BBTOB(ealign - start_block));
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
1544
1545
  			if (error)
  				break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1546
  		}
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1547
  		offset = buffer + xlog_align(log, start_block);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1548
1549
1550
1551
1552
  		for (; j < endcount; j++) {
  			xlog_add_record(log, offset, cycle, i+j,
  					tail_cycle, tail_block);
  			offset += BBSIZE;
  		}
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1553
  		error = xlog_bwrite(log, start_block, endcount, buffer);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1554
1555
1556
1557
1558
  		if (error)
  			break;
  		start_block += endcount;
  		j = 0;
  	}
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
1559

6e9b3dd80   Christoph Hellwig   xfs: stop using b...
1560
1561
  out_free_buffer:
  	kmem_free(buffer);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
  	return error;
  }
  
  /*
   * This routine is called to blow away any incomplete log writes out
   * in front of the log head.  We do this so that we won't become confused
   * if we come up, write only a little bit more, and then crash again.
   * If we leave the partial log records out there, this situation could
   * cause us to think those partial writes are valid blocks since they
   * have the current cycle number.  We get rid of them by overwriting them
   * with empty log records with the old cycle number rather than the
   * current one.
   *
   * The tail lsn is passed in rather than taken from
   * the log so that we will not write over the unmount record after a
   * clean unmount in a 512 block log.  Doing so would leave the log without
   * any valid log records in it until a new one was written.  If we crashed
   * during that time we would not be able to recover.
   */
  STATIC int
  xlog_clear_stale_blocks(
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
1583
  	struct xlog	*log,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
  	xfs_lsn_t	tail_lsn)
  {
  	int		tail_cycle, head_cycle;
  	int		tail_block, head_block;
  	int		tail_distance, max_distance;
  	int		distance;
  	int		error;
  
  	tail_cycle = CYCLE_LSN(tail_lsn);
  	tail_block = BLOCK_LSN(tail_lsn);
  	head_cycle = log->l_curr_cycle;
  	head_block = log->l_curr_block;
  
  	/*
  	 * Figure out the distance between the new head of the log
  	 * and the tail.  We want to write over any blocks beyond the
  	 * head that we may have written just before the crash, but
  	 * we don't want to overwrite the tail of the log.
  	 */
  	if (head_cycle == tail_cycle) {
  		/*
  		 * The tail is behind the head in the physical log,
  		 * so the distance from the head to the tail is the
  		 * distance from the head to the end of the log plus
  		 * the distance from the beginning of the log to the
  		 * tail.
  		 */
a71895c5d   Darrick J. Wong   xfs: convert open...
1611
1612
1613
  		if (XFS_IS_CORRUPT(log->l_mp,
  				   head_block < tail_block ||
  				   head_block >= log->l_logBBsize))
2451337dd   Dave Chinner   xfs: global error...
1614
  			return -EFSCORRUPTED;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1615
1616
1617
1618
1619
1620
1621
  		tail_distance = tail_block + (log->l_logBBsize - head_block);
  	} else {
  		/*
  		 * The head is behind the tail in the physical log,
  		 * so the distance from the head to the tail is just
  		 * the tail block minus the head block.
  		 */
a71895c5d   Darrick J. Wong   xfs: convert open...
1622
1623
1624
  		if (XFS_IS_CORRUPT(log->l_mp,
  				   head_block >= tail_block ||
  				   head_cycle != tail_cycle + 1))
2451337dd   Dave Chinner   xfs: global error...
1625
  			return -EFSCORRUPTED;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
  		tail_distance = tail_block - head_block;
  	}
  
  	/*
  	 * If the head is right up against the tail, we can't clear
  	 * anything.
  	 */
  	if (tail_distance <= 0) {
  		ASSERT(tail_distance == 0);
  		return 0;
  	}
  
  	max_distance = XLOG_TOTAL_REC_SHIFT(log);
  	/*
  	 * Take the smaller of the maximum amount of outstanding I/O
  	 * we could have and the distance to the tail to clear out.
  	 * We take the smaller so that we don't overwrite the tail and
  	 * we don't waste all day writing from the head to the tail
  	 * for no reason.
  	 */
9bb54cb56   Dave Chinner   xfs: clean up MIN...
1646
  	max_distance = min(max_distance, tail_distance);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
  
  	if ((head_block + max_distance) <= log->l_logBBsize) {
  		/*
  		 * We can stomp all the blocks we need to without
  		 * wrapping around the end of the log.  Just do it
  		 * in a single write.  Use the cycle number of the
  		 * current cycle minus one so that the log will look like:
  		 *     n ... | n - 1 ...
  		 */
  		error = xlog_write_log_records(log, (head_cycle - 1),
  				head_block, max_distance, tail_cycle,
  				tail_block);
  		if (error)
  			return error;
  	} else {
  		/*
  		 * We need to wrap around the end of the physical log in
  		 * order to clear all the blocks.  Do it in two separate
  		 * I/Os.  The first write should be from the head to the
  		 * end of the physical log, and it should use the current
  		 * cycle number minus one just like above.
  		 */
  		distance = log->l_logBBsize - head_block;
  		error = xlog_write_log_records(log, (head_cycle - 1),
  				head_block, distance, tail_cycle,
  				tail_block);
  
  		if (error)
  			return error;
  
  		/*
  		 * Now write the blocks at the start of the physical log.
  		 * This writes the remainder of the blocks we want to clear.
  		 * It uses the current cycle number since we're now on the
  		 * same cycle as the head so that we get:
  		 *    n ... n ... | n - 1 ...
  		 *    ^^^^^ blocks we're writing
  		 */
  		distance = max_distance - (log->l_logBBsize - head_block);
  		error = xlog_write_log_records(log, head_cycle, 0, distance,
  				tail_cycle, tail_block);
  		if (error)
  			return error;
  	}
  
  	return 0;
  }
154c733a3   Darrick J. Wong   xfs: refactor rel...
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
  /*
   * Release the recovered intent item in the AIL that matches the given intent
   * type and intent id.
   */
  void
  xlog_recover_release_intent(
  	struct xlog		*log,
  	unsigned short		intent_type,
  	uint64_t		intent_id)
  {
  	struct xfs_ail_cursor	cur;
  	struct xfs_log_item	*lip;
  	struct xfs_ail		*ailp = log->l_ailp;
  
  	spin_lock(&ailp->ail_lock);
  	for (lip = xfs_trans_ail_cursor_first(ailp, &cur, 0); lip != NULL;
  	     lip = xfs_trans_ail_cursor_next(ailp, &cur)) {
  		if (lip->li_type != intent_type)
  			continue;
  		if (!lip->li_ops->iop_match(lip, intent_id))
  			continue;
  
  		spin_unlock(&ailp->ail_lock);
  		lip->li_ops->iop_release(lip);
  		spin_lock(&ailp->ail_lock);
  		break;
  	}
  
  	xfs_trans_ail_cursor_done(&cur);
  	spin_unlock(&ailp->ail_lock);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1725
1726
1727
1728
1729
1730
  /******************************************************************************
   *
   *		Log recover routines
   *
   ******************************************************************************
   */
86ffa471d   Darrick J. Wong   xfs: refactor log...
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
  static const struct xlog_recover_item_ops *xlog_recover_item_ops[] = {
  	&xlog_buf_item_ops,
  	&xlog_inode_item_ops,
  	&xlog_dquot_item_ops,
  	&xlog_quotaoff_item_ops,
  	&xlog_icreate_item_ops,
  	&xlog_efi_item_ops,
  	&xlog_efd_item_ops,
  	&xlog_rui_item_ops,
  	&xlog_rud_item_ops,
  	&xlog_cui_item_ops,
  	&xlog_cud_item_ops,
  	&xlog_bui_item_ops,
  	&xlog_bud_item_ops,
  };
  
  static const struct xlog_recover_item_ops *
  xlog_find_item_ops(
  	struct xlog_recover_item		*item)
  {
  	unsigned int				i;
  
  	for (i = 0; i < ARRAY_SIZE(xlog_recover_item_ops); i++)
  		if (ITEM_TYPE(item) == xlog_recover_item_ops[i]->item_type)
  			return xlog_recover_item_ops[i];
  
  	return NULL;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1759

f0a769538   Dave Chinner   xfs: Use list_hea...
1760
  /*
a775ad778   Dave Chinner   xfs: fix log reco...
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
   * Sort the log items in the transaction.
   *
   * The ordering constraints are defined by the inode allocation and unlink
   * behaviour. The rules are:
   *
   *	1. Every item is only logged once in a given transaction. Hence it
   *	   represents the last logged state of the item. Hence ordering is
   *	   dependent on the order in which operations need to be performed so
   *	   required initial conditions are always met.
   *
   *	2. Cancelled buffers are recorded in pass 1 in a separate table and
   *	   there's nothing to replay from them so we can simply cull them
   *	   from the transaction. However, we can't do that until after we've
   *	   replayed all the other items because they may be dependent on the
   *	   cancelled buffer and replaying the cancelled buffer can remove it
   *	   form the cancelled buffer table. Hence they have tobe done last.
   *
   *	3. Inode allocation buffers must be replayed before inode items that
28c8e41af   Dave Chinner   xfs: Inode create...
1779
1780
1781
1782
   *	   read the buffer and replay changes into it. For filesystems using the
   *	   ICREATE transactions, this means XFS_LI_ICREATE objects need to get
   *	   treated the same as inode allocation buffers as they create and
   *	   initialise the buffers directly.
a775ad778   Dave Chinner   xfs: fix log reco...
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
   *
   *	4. Inode unlink buffers must be replayed after inode items are replayed.
   *	   This ensures that inodes are completely flushed to the inode buffer
   *	   in a "free" state before we remove the unlinked inode list pointer.
   *
   * Hence the ordering needs to be inode allocation buffers first, inode items
   * second, inode unlink buffers third and cancelled buffers last.
   *
   * But there's a problem with that - we can't tell an inode allocation buffer
   * apart from a regular buffer, so we can't separate them. We can, however,
   * tell an inode unlink buffer from the others, and so we can separate them out
   * from all the other buffers and move them to last.
   *
   * Hence, 4 lists, in order from head to tail:
28c8e41af   Dave Chinner   xfs: Inode create...
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
   *	- buffer_list for all buffers except cancelled/inode unlink buffers
   *	- item_list for all non-buffer items
   *	- inode_buffer_list for inode unlink buffers
   *	- cancel_list for the cancelled buffers
   *
   * Note that we add objects to the tail of the lists so that first-to-last
   * ordering is preserved within the lists. Adding objects to the head of the
   * list means when we traverse from the head we walk them in last-to-first
   * order. For cancelled buffers and inode unlink buffers this doesn't matter,
   * but for all other items there may be specific ordering that we need to
   * preserve.
f0a769538   Dave Chinner   xfs: Use list_hea...
1808
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1809
1810
  STATIC int
  xlog_recover_reorder_trans(
ad223e603   Mark Tinguely   xfs: rename log s...
1811
1812
  	struct xlog		*log,
  	struct xlog_recover	*trans,
9abbc539b   Dave Chinner   xfs: add log item...
1813
  	int			pass)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1814
  {
35f4521fd   Darrick J. Wong   xfs: convert xfs_...
1815
  	struct xlog_recover_item *item, *n;
2a84108fe   Mark Tinguely   xfs: free the lis...
1816
  	int			error = 0;
f0a769538   Dave Chinner   xfs: Use list_hea...
1817
  	LIST_HEAD(sort_list);
a775ad778   Dave Chinner   xfs: fix log reco...
1818
1819
1820
  	LIST_HEAD(cancel_list);
  	LIST_HEAD(buffer_list);
  	LIST_HEAD(inode_buffer_list);
5ce70b770   Christoph Hellwig   xfs: rename inode...
1821
  	LIST_HEAD(item_list);
f0a769538   Dave Chinner   xfs: Use list_hea...
1822
1823
1824
  
  	list_splice_init(&trans->r_itemq, &sort_list);
  	list_for_each_entry_safe(item, n, &sort_list, ri_list) {
86ffa471d   Darrick J. Wong   xfs: refactor log...
1825
  		enum xlog_recover_reorder	fate = XLOG_REORDER_ITEM_LIST;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1826

86ffa471d   Darrick J. Wong   xfs: refactor log...
1827
1828
  		item->ri_ops = xlog_find_item_ops(item);
  		if (!item->ri_ops) {
a0fa2b679   Dave Chinner   xfs: Convert xlog...
1829
  			xfs_warn(log->l_mp,
0d2d35a33   Darrick J. Wong   xfs: report unrec...
1830
1831
  				"%s: unrecognized type of log operation (%d)",
  				__func__, ITEM_TYPE(item));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1832
  			ASSERT(0);
2a84108fe   Mark Tinguely   xfs: free the lis...
1833
1834
1835
1836
1837
1838
  			/*
  			 * return the remaining items back to the transaction
  			 * item list so they can be freed in caller.
  			 */
  			if (!list_empty(&sort_list))
  				list_splice_init(&sort_list, &trans->r_itemq);
86ffa471d   Darrick J. Wong   xfs: refactor log...
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
  			error = -EFSCORRUPTED;
  			break;
  		}
  
  		if (item->ri_ops->reorder)
  			fate = item->ri_ops->reorder(item);
  
  		switch (fate) {
  		case XLOG_REORDER_BUFFER_LIST:
  			list_move_tail(&item->ri_list, &buffer_list);
  			break;
  		case XLOG_REORDER_CANCEL_LIST:
  			trace_xfs_log_recover_item_reorder_head(log,
  					trans, item, pass);
  			list_move(&item->ri_list, &cancel_list);
  			break;
  		case XLOG_REORDER_INODE_BUFFER_LIST:
  			list_move(&item->ri_list, &inode_buffer_list);
  			break;
  		case XLOG_REORDER_ITEM_LIST:
  			trace_xfs_log_recover_item_reorder_tail(log,
  							trans, item, pass);
  			list_move_tail(&item->ri_list, &item_list);
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1863
  		}
f0a769538   Dave Chinner   xfs: Use list_hea...
1864
  	}
86ffa471d   Darrick J. Wong   xfs: refactor log...
1865

f0a769538   Dave Chinner   xfs: Use list_hea...
1866
  	ASSERT(list_empty(&sort_list));
a775ad778   Dave Chinner   xfs: fix log reco...
1867
1868
  	if (!list_empty(&buffer_list))
  		list_splice(&buffer_list, &trans->r_itemq);
5ce70b770   Christoph Hellwig   xfs: rename inode...
1869
1870
  	if (!list_empty(&item_list))
  		list_splice_tail(&item_list, &trans->r_itemq);
a775ad778   Dave Chinner   xfs: fix log reco...
1871
1872
1873
1874
  	if (!list_empty(&inode_buffer_list))
  		list_splice_tail(&inode_buffer_list, &trans->r_itemq);
  	if (!list_empty(&cancel_list))
  		list_splice_tail(&cancel_list, &trans->r_itemq);
2a84108fe   Mark Tinguely   xfs: free the lis...
1875
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1876
  }
8ea5682d0   Darrick J. Wong   xfs: refactor log...
1877
  void
7d4894b4c   Christoph Hellwig   xfs: factor out a...
1878
1879
1880
1881
1882
1883
1884
1885
1886
  xlog_buf_readahead(
  	struct xlog		*log,
  	xfs_daddr_t		blkno,
  	uint			len,
  	const struct xfs_buf_ops *ops)
  {
  	if (!xlog_is_buffer_cancelled(log, blkno, len))
  		xfs_buf_readahead(log->l_mp->m_ddev_targp, blkno, len, ops);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1887
  STATIC int
00574da19   Zhi Yong Wu   xfs: introduce ob...
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
  xlog_recover_items_pass2(
  	struct xlog                     *log,
  	struct xlog_recover             *trans,
  	struct list_head                *buffer_list,
  	struct list_head                *item_list)
  {
  	struct xlog_recover_item	*item;
  	int				error = 0;
  
  	list_for_each_entry(item, item_list, ri_list) {
2565a11b2   Darrick J. Wong   xfs: remove log r...
1898
1899
1900
1901
1902
1903
  		trace_xfs_log_recover_item_recover(log, trans, item,
  				XLOG_RECOVER_PASS2);
  
  		if (item->ri_ops->commit_pass2)
  			error = item->ri_ops->commit_pass2(log, buffer_list,
  					item, trans->r_lsn);
00574da19   Zhi Yong Wu   xfs: introduce ob...
1904
1905
1906
1907
1908
1909
  		if (error)
  			return error;
  	}
  
  	return error;
  }
d04509486   Christoph Hellwig   xfs: refactor xlo...
1910
1911
1912
1913
1914
1915
1916
  /*
   * Perform the transaction.
   *
   * If the transaction modifies a buffer or inode, do it now.  Otherwise,
   * EFIs and EFDs get queued up by adding entries into the AIL for them.
   */
  STATIC int
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1917
  xlog_recover_commit_trans(
ad223e603   Mark Tinguely   xfs: rename log s...
1918
  	struct xlog		*log,
d04509486   Christoph Hellwig   xfs: refactor xlo...
1919
  	struct xlog_recover	*trans,
12818d24d   Brian Foster   xfs: rework log r...
1920
1921
  	int			pass,
  	struct list_head	*buffer_list)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1922
  {
00574da19   Zhi Yong Wu   xfs: introduce ob...
1923
  	int				error = 0;
00574da19   Zhi Yong Wu   xfs: introduce ob...
1924
1925
1926
  	int				items_queued = 0;
  	struct xlog_recover_item	*item;
  	struct xlog_recover_item	*next;
00574da19   Zhi Yong Wu   xfs: introduce ob...
1927
1928
1929
1930
  	LIST_HEAD			(ra_list);
  	LIST_HEAD			(done_list);
  
  	#define XLOG_RECOVER_COMMIT_QUEUE_MAX 100
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1931

39775431f   Brian Foster   xfs: free uncommi...
1932
  	hlist_del_init(&trans->r_list);
d04509486   Christoph Hellwig   xfs: refactor xlo...
1933
1934
1935
  
  	error = xlog_recover_reorder_trans(log, trans, pass);
  	if (error)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1936
  		return error;
d04509486   Christoph Hellwig   xfs: refactor xlo...
1937

00574da19   Zhi Yong Wu   xfs: introduce ob...
1938
  	list_for_each_entry_safe(item, next, &trans->r_itemq, ri_list) {
3304a4fab   Darrick J. Wong   xfs: refactor log...
1939
  		trace_xfs_log_recover_item_recover(log, trans, item, pass);
43ff2122e   Christoph Hellwig   xfs: on-stack del...
1940
1941
  		switch (pass) {
  		case XLOG_RECOVER_PASS1:
3304a4fab   Darrick J. Wong   xfs: refactor log...
1942
1943
  			if (item->ri_ops->commit_pass1)
  				error = item->ri_ops->commit_pass1(log, item);
43ff2122e   Christoph Hellwig   xfs: on-stack del...
1944
1945
  			break;
  		case XLOG_RECOVER_PASS2:
8ea5682d0   Darrick J. Wong   xfs: refactor log...
1946
1947
  			if (item->ri_ops->ra_pass2)
  				item->ri_ops->ra_pass2(log, item);
00574da19   Zhi Yong Wu   xfs: introduce ob...
1948
1949
1950
1951
  			list_move_tail(&item->ri_list, &ra_list);
  			items_queued++;
  			if (items_queued >= XLOG_RECOVER_COMMIT_QUEUE_MAX) {
  				error = xlog_recover_items_pass2(log, trans,
12818d24d   Brian Foster   xfs: rework log r...
1952
  						buffer_list, &ra_list);
00574da19   Zhi Yong Wu   xfs: introduce ob...
1953
1954
1955
  				list_splice_tail_init(&ra_list, &done_list);
  				items_queued = 0;
  			}
43ff2122e   Christoph Hellwig   xfs: on-stack del...
1956
1957
1958
1959
  			break;
  		default:
  			ASSERT(0);
  		}
d04509486   Christoph Hellwig   xfs: refactor xlo...
1960
  		if (error)
43ff2122e   Christoph Hellwig   xfs: on-stack del...
1961
  			goto out;
d04509486   Christoph Hellwig   xfs: refactor xlo...
1962
  	}
00574da19   Zhi Yong Wu   xfs: introduce ob...
1963
1964
1965
1966
  out:
  	if (!list_empty(&ra_list)) {
  		if (!error)
  			error = xlog_recover_items_pass2(log, trans,
12818d24d   Brian Foster   xfs: rework log r...
1967
  					buffer_list, &ra_list);
00574da19   Zhi Yong Wu   xfs: introduce ob...
1968
1969
1970
1971
1972
  		list_splice_tail_init(&ra_list, &done_list);
  	}
  
  	if (!list_empty(&done_list))
  		list_splice_init(&done_list, &trans->r_itemq);
12818d24d   Brian Foster   xfs: rework log r...
1973
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1974
  }
765606698   Dave Chinner   xfs: reorganise t...
1975
1976
1977
1978
  STATIC void
  xlog_recover_add_item(
  	struct list_head	*head)
  {
35f4521fd   Darrick J. Wong   xfs: convert xfs_...
1979
  	struct xlog_recover_item *item;
765606698   Dave Chinner   xfs: reorganise t...
1980

35f4521fd   Darrick J. Wong   xfs: convert xfs_...
1981
  	item = kmem_zalloc(sizeof(struct xlog_recover_item), 0);
765606698   Dave Chinner   xfs: reorganise t...
1982
1983
1984
  	INIT_LIST_HEAD(&item->ri_list);
  	list_add_tail(&item->ri_list, head);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1985
  STATIC int
765606698   Dave Chinner   xfs: reorganise t...
1986
1987
1988
  xlog_recover_add_to_cont_trans(
  	struct xlog		*log,
  	struct xlog_recover	*trans,
b2a922cd6   Christoph Hellwig   xfs: remove xfs_c...
1989
  	char			*dp,
765606698   Dave Chinner   xfs: reorganise t...
1990
  	int			len)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1991
  {
35f4521fd   Darrick J. Wong   xfs: convert xfs_...
1992
  	struct xlog_recover_item *item;
b2a922cd6   Christoph Hellwig   xfs: remove xfs_c...
1993
  	char			*ptr, *old_ptr;
765606698   Dave Chinner   xfs: reorganise t...
1994
  	int			old_len;
89cebc847   Brian Foster   xfs: validate tra...
1995
1996
1997
1998
  	/*
  	 * If the transaction is empty, the header was split across this and the
  	 * previous record. Copy the rest of the header.
  	 */
765606698   Dave Chinner   xfs: reorganise t...
1999
  	if (list_empty(&trans->r_itemq)) {
848ccfc8f   Brian Foster   xfs: fix log reco...
2000
  		ASSERT(len <= sizeof(struct xfs_trans_header));
89cebc847   Brian Foster   xfs: validate tra...
2001
2002
  		if (len > sizeof(struct xfs_trans_header)) {
  			xfs_warn(log->l_mp, "%s: bad header length", __func__);
895e196fb   Darrick J. Wong   xfs: convert EIO ...
2003
  			return -EFSCORRUPTED;
89cebc847   Brian Foster   xfs: validate tra...
2004
  		}
765606698   Dave Chinner   xfs: reorganise t...
2005
  		xlog_recover_add_item(&trans->r_itemq);
b2a922cd6   Christoph Hellwig   xfs: remove xfs_c...
2006
  		ptr = (char *)&trans->r_theader +
89cebc847   Brian Foster   xfs: validate tra...
2007
  				sizeof(struct xfs_trans_header) - len;
765606698   Dave Chinner   xfs: reorganise t...
2008
2009
2010
  		memcpy(ptr, dp, len);
  		return 0;
  	}
89cebc847   Brian Foster   xfs: validate tra...
2011

765606698   Dave Chinner   xfs: reorganise t...
2012
  	/* take the tail entry */
35f4521fd   Darrick J. Wong   xfs: convert xfs_...
2013
2014
  	item = list_entry(trans->r_itemq.prev, struct xlog_recover_item,
  			  ri_list);
765606698   Dave Chinner   xfs: reorganise t...
2015
2016
2017
  
  	old_ptr = item->ri_buf[item->ri_cnt-1].i_addr;
  	old_len = item->ri_buf[item->ri_cnt-1].i_len;
771915c4f   Carlos Maiolino   xfs: remove kmem_...
2018
  	ptr = krealloc(old_ptr, len + old_len, GFP_KERNEL | __GFP_NOFAIL);
765606698   Dave Chinner   xfs: reorganise t...
2019
2020
2021
2022
  	memcpy(&ptr[old_len], dp, len);
  	item->ri_buf[item->ri_cnt-1].i_len += len;
  	item->ri_buf[item->ri_cnt-1].i_addr = ptr;
  	trace_xfs_log_recover_item_add_cont(log, trans, item, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2023
2024
2025
2026
  	return 0;
  }
  
  /*
765606698   Dave Chinner   xfs: reorganise t...
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
   * The next region to add is the start of a new region.  It could be
   * a whole region or it could be the first part of a new region.  Because
   * of this, the assumption here is that the type and size fields of all
   * format structures fit into the first 32 bits of the structure.
   *
   * This works because all regions must be 32 bit aligned.  Therefore, we
   * either have both fields or we have neither field.  In the case we have
   * neither field, the data part of the region is zero length.  We only have
   * a log_op_header and can throw away the header since a new one will appear
   * later.  If we have at least 4 bytes, then we can determine how many regions
   * will appear in the current log item.
   */
  STATIC int
  xlog_recover_add_to_trans(
  	struct xlog		*log,
  	struct xlog_recover	*trans,
b2a922cd6   Christoph Hellwig   xfs: remove xfs_c...
2043
  	char			*dp,
765606698   Dave Chinner   xfs: reorganise t...
2044
2045
  	int			len)
  {
06b113212   Darrick J. Wong   xfs: remove inode...
2046
  	struct xfs_inode_log_format	*in_f;			/* any will do */
35f4521fd   Darrick J. Wong   xfs: convert xfs_...
2047
  	struct xlog_recover_item *item;
b2a922cd6   Christoph Hellwig   xfs: remove xfs_c...
2048
  	char			*ptr;
765606698   Dave Chinner   xfs: reorganise t...
2049
2050
2051
2052
2053
2054
2055
2056
2057
  
  	if (!len)
  		return 0;
  	if (list_empty(&trans->r_itemq)) {
  		/* we need to catch log corruptions here */
  		if (*(uint *)dp != XFS_TRANS_HEADER_MAGIC) {
  			xfs_warn(log->l_mp, "%s: bad header magic number",
  				__func__);
  			ASSERT(0);
895e196fb   Darrick J. Wong   xfs: convert EIO ...
2058
  			return -EFSCORRUPTED;
765606698   Dave Chinner   xfs: reorganise t...
2059
  		}
89cebc847   Brian Foster   xfs: validate tra...
2060
2061
2062
2063
  
  		if (len > sizeof(struct xfs_trans_header)) {
  			xfs_warn(log->l_mp, "%s: bad header length", __func__);
  			ASSERT(0);
895e196fb   Darrick J. Wong   xfs: convert EIO ...
2064
  			return -EFSCORRUPTED;
89cebc847   Brian Foster   xfs: validate tra...
2065
2066
2067
2068
2069
2070
2071
2072
  		}
  
  		/*
  		 * The transaction header can be arbitrarily split across op
  		 * records. If we don't have the whole thing here, copy what we
  		 * do have and handle the rest in the next record.
  		 */
  		if (len == sizeof(struct xfs_trans_header))
765606698   Dave Chinner   xfs: reorganise t...
2073
2074
2075
2076
  			xlog_recover_add_item(&trans->r_itemq);
  		memcpy(&trans->r_theader, dp, len);
  		return 0;
  	}
707e0ddaf   Tetsuo Handa   fs: xfs: Remove K...
2077
  	ptr = kmem_alloc(len, 0);
765606698   Dave Chinner   xfs: reorganise t...
2078
  	memcpy(ptr, dp, len);
06b113212   Darrick J. Wong   xfs: remove inode...
2079
  	in_f = (struct xfs_inode_log_format *)ptr;
765606698   Dave Chinner   xfs: reorganise t...
2080
2081
  
  	/* take the tail entry */
35f4521fd   Darrick J. Wong   xfs: convert xfs_...
2082
2083
  	item = list_entry(trans->r_itemq.prev, struct xlog_recover_item,
  			  ri_list);
765606698   Dave Chinner   xfs: reorganise t...
2084
2085
2086
2087
2088
  	if (item->ri_total != 0 &&
  	     item->ri_total == item->ri_cnt) {
  		/* tail item is in use, get a new one */
  		xlog_recover_add_item(&trans->r_itemq);
  		item = list_entry(trans->r_itemq.prev,
35f4521fd   Darrick J. Wong   xfs: convert xfs_...
2089
  					struct xlog_recover_item, ri_list);
765606698   Dave Chinner   xfs: reorganise t...
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
  	}
  
  	if (item->ri_total == 0) {		/* first region to be added */
  		if (in_f->ilf_size == 0 ||
  		    in_f->ilf_size > XLOG_MAX_REGIONS_IN_ITEM) {
  			xfs_warn(log->l_mp,
  		"bad number of regions (%d) in inode log format",
  				  in_f->ilf_size);
  			ASSERT(0);
  			kmem_free(ptr);
895e196fb   Darrick J. Wong   xfs: convert EIO ...
2100
  			return -EFSCORRUPTED;
765606698   Dave Chinner   xfs: reorganise t...
2101
2102
2103
2104
2105
  		}
  
  		item->ri_total = in_f->ilf_size;
  		item->ri_buf =
  			kmem_zalloc(item->ri_total * sizeof(xfs_log_iovec_t),
707e0ddaf   Tetsuo Handa   fs: xfs: Remove K...
2106
  				    0);
765606698   Dave Chinner   xfs: reorganise t...
2107
  	}
d6abecb82   Darrick J. Wong   xfs: range check ...
2108
2109
2110
2111
2112
2113
2114
2115
2116
  
  	if (item->ri_total <= item->ri_cnt) {
  		xfs_warn(log->l_mp,
  	"log item region count (%d) overflowed size (%d)",
  				item->ri_cnt, item->ri_total);
  		ASSERT(0);
  		kmem_free(ptr);
  		return -EFSCORRUPTED;
  	}
765606698   Dave Chinner   xfs: reorganise t...
2117
2118
2119
2120
2121
2122
2123
  	/* Description region is ri_buf[0] */
  	item->ri_buf[item->ri_cnt].i_addr = ptr;
  	item->ri_buf[item->ri_cnt].i_len  = len;
  	item->ri_cnt++;
  	trace_xfs_log_recover_item_add(log, trans, item, 0);
  	return 0;
  }
b818cca19   Dave Chinner   xfs: refactor rec...
2124

765606698   Dave Chinner   xfs: reorganise t...
2125
2126
2127
2128
2129
2130
2131
2132
2133
  /*
   * Free up any resources allocated by the transaction
   *
   * Remember that EFIs, EFDs, and IUNLINKs are handled later.
   */
  STATIC void
  xlog_recover_free_trans(
  	struct xlog_recover	*trans)
  {
35f4521fd   Darrick J. Wong   xfs: convert xfs_...
2134
  	struct xlog_recover_item *item, *n;
765606698   Dave Chinner   xfs: reorganise t...
2135
  	int			i;
39775431f   Brian Foster   xfs: free uncommi...
2136
  	hlist_del_init(&trans->r_list);
765606698   Dave Chinner   xfs: reorganise t...
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
  	list_for_each_entry_safe(item, n, &trans->r_itemq, ri_list) {
  		/* Free the regions in the item. */
  		list_del(&item->ri_list);
  		for (i = 0; i < item->ri_cnt; i++)
  			kmem_free(item->ri_buf[i].i_addr);
  		/* Free the item itself */
  		kmem_free(item->ri_buf);
  		kmem_free(item);
  	}
  	/* Free the transaction recover structure */
  	kmem_free(trans);
  }
e9131e50f   Dave Chinner   xfs: recovery of ...
2149
2150
2151
  /*
   * On error or completion, trans is freed.
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2152
  STATIC int
eeb116881   Dave Chinner   xfs: refactor xlo...
2153
2154
2155
  xlog_recovery_process_trans(
  	struct xlog		*log,
  	struct xlog_recover	*trans,
b2a922cd6   Christoph Hellwig   xfs: remove xfs_c...
2156
  	char			*dp,
eeb116881   Dave Chinner   xfs: refactor xlo...
2157
2158
  	unsigned int		len,
  	unsigned int		flags,
12818d24d   Brian Foster   xfs: rework log r...
2159
2160
  	int			pass,
  	struct list_head	*buffer_list)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2161
  {
e9131e50f   Dave Chinner   xfs: recovery of ...
2162
2163
  	int			error = 0;
  	bool			freeit = false;
eeb116881   Dave Chinner   xfs: refactor xlo...
2164
2165
2166
2167
2168
  
  	/* mask off ophdr transaction container flags */
  	flags &= ~XLOG_END_TRANS;
  	if (flags & XLOG_WAS_CONT_TRANS)
  		flags &= ~XLOG_CONTINUE_TRANS;
88b863db9   Dave Chinner   xfs: fix double f...
2169
2170
2171
2172
  	/*
  	 * Callees must not free the trans structure. We'll decide if we need to
  	 * free it or not based on the operation being done and it's result.
  	 */
eeb116881   Dave Chinner   xfs: refactor xlo...
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
  	switch (flags) {
  	/* expected flag values */
  	case 0:
  	case XLOG_CONTINUE_TRANS:
  		error = xlog_recover_add_to_trans(log, trans, dp, len);
  		break;
  	case XLOG_WAS_CONT_TRANS:
  		error = xlog_recover_add_to_cont_trans(log, trans, dp, len);
  		break;
  	case XLOG_COMMIT_TRANS:
12818d24d   Brian Foster   xfs: rework log r...
2183
2184
  		error = xlog_recover_commit_trans(log, trans, pass,
  						  buffer_list);
88b863db9   Dave Chinner   xfs: fix double f...
2185
2186
  		/* success or fail, we are now done with this transaction. */
  		freeit = true;
eeb116881   Dave Chinner   xfs: refactor xlo...
2187
2188
2189
2190
  		break;
  
  	/* unexpected flag values */
  	case XLOG_UNMOUNT_TRANS:
e9131e50f   Dave Chinner   xfs: recovery of ...
2191
  		/* just skip trans */
eeb116881   Dave Chinner   xfs: refactor xlo...
2192
  		xfs_warn(log->l_mp, "%s: Unmount LR", __func__);
e9131e50f   Dave Chinner   xfs: recovery of ...
2193
  		freeit = true;
eeb116881   Dave Chinner   xfs: refactor xlo...
2194
2195
  		break;
  	case XLOG_START_TRANS:
eeb116881   Dave Chinner   xfs: refactor xlo...
2196
2197
2198
  	default:
  		xfs_warn(log->l_mp, "%s: bad flag 0x%x", __func__, flags);
  		ASSERT(0);
895e196fb   Darrick J. Wong   xfs: convert EIO ...
2199
  		error = -EFSCORRUPTED;
eeb116881   Dave Chinner   xfs: refactor xlo...
2200
2201
  		break;
  	}
e9131e50f   Dave Chinner   xfs: recovery of ...
2202
2203
  	if (error || freeit)
  		xlog_recover_free_trans(trans);
eeb116881   Dave Chinner   xfs: refactor xlo...
2204
2205
  	return error;
  }
b818cca19   Dave Chinner   xfs: refactor rec...
2206
2207
2208
2209
2210
2211
2212
  /*
   * Lookup the transaction recovery structure associated with the ID in the
   * current ophdr. If the transaction doesn't exist and the start flag is set in
   * the ophdr, then allocate a new transaction for future ID matches to find.
   * Either way, return what we found during the lookup - an existing transaction
   * or nothing.
   */
eeb116881   Dave Chinner   xfs: refactor xlo...
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
  STATIC struct xlog_recover *
  xlog_recover_ophdr_to_trans(
  	struct hlist_head	rhash[],
  	struct xlog_rec_header	*rhead,
  	struct xlog_op_header	*ohead)
  {
  	struct xlog_recover	*trans;
  	xlog_tid_t		tid;
  	struct hlist_head	*rhp;
  
  	tid = be32_to_cpu(ohead->oh_tid);
  	rhp = &rhash[XLOG_RHASH(tid)];
b818cca19   Dave Chinner   xfs: refactor rec...
2225
2226
2227
2228
  	hlist_for_each_entry(trans, rhp, r_list) {
  		if (trans->r_log_tid == tid)
  			return trans;
  	}
eeb116881   Dave Chinner   xfs: refactor xlo...
2229
2230
  
  	/*
b818cca19   Dave Chinner   xfs: refactor rec...
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
  	 * skip over non-start transaction headers - we could be
  	 * processing slack space before the next transaction starts
  	 */
  	if (!(ohead->oh_flags & XLOG_START_TRANS))
  		return NULL;
  
  	ASSERT(be32_to_cpu(ohead->oh_len) == 0);
  
  	/*
  	 * This is a new transaction so allocate a new recovery container to
  	 * hold the recovery ops that will follow.
  	 */
707e0ddaf   Tetsuo Handa   fs: xfs: Remove K...
2243
  	trans = kmem_zalloc(sizeof(struct xlog_recover), 0);
b818cca19   Dave Chinner   xfs: refactor rec...
2244
2245
2246
2247
2248
2249
2250
2251
2252
  	trans->r_log_tid = tid;
  	trans->r_lsn = be64_to_cpu(rhead->h_lsn);
  	INIT_LIST_HEAD(&trans->r_itemq);
  	INIT_HLIST_NODE(&trans->r_list);
  	hlist_add_head(&trans->r_list, rhp);
  
  	/*
  	 * Nothing more to do for this ophdr. Items to be added to this new
  	 * transaction will be in subsequent ophdr containers.
eeb116881   Dave Chinner   xfs: refactor xlo...
2253
  	 */
eeb116881   Dave Chinner   xfs: refactor xlo...
2254
2255
2256
2257
2258
2259
2260
2261
2262
  	return NULL;
  }
  
  STATIC int
  xlog_recover_process_ophdr(
  	struct xlog		*log,
  	struct hlist_head	rhash[],
  	struct xlog_rec_header	*rhead,
  	struct xlog_op_header	*ohead,
b2a922cd6   Christoph Hellwig   xfs: remove xfs_c...
2263
2264
  	char			*dp,
  	char			*end,
12818d24d   Brian Foster   xfs: rework log r...
2265
2266
  	int			pass,
  	struct list_head	*buffer_list)
eeb116881   Dave Chinner   xfs: refactor xlo...
2267
2268
  {
  	struct xlog_recover	*trans;
eeb116881   Dave Chinner   xfs: refactor xlo...
2269
  	unsigned int		len;
12818d24d   Brian Foster   xfs: rework log r...
2270
  	int			error;
eeb116881   Dave Chinner   xfs: refactor xlo...
2271
2272
2273
2274
2275
2276
2277
  
  	/* Do we understand who wrote this op? */
  	if (ohead->oh_clientid != XFS_TRANSACTION &&
  	    ohead->oh_clientid != XFS_LOG) {
  		xfs_warn(log->l_mp, "%s: bad clientid 0x%x",
  			__func__, ohead->oh_clientid);
  		ASSERT(0);
895e196fb   Darrick J. Wong   xfs: convert EIO ...
2278
  		return -EFSCORRUPTED;
eeb116881   Dave Chinner   xfs: refactor xlo...
2279
2280
2281
2282
2283
2284
2285
2286
2287
  	}
  
  	/*
  	 * Check the ophdr contains all the data it is supposed to contain.
  	 */
  	len = be32_to_cpu(ohead->oh_len);
  	if (dp + len > end) {
  		xfs_warn(log->l_mp, "%s: bad length 0x%x", __func__, len);
  		WARN_ON(1);
895e196fb   Darrick J. Wong   xfs: convert EIO ...
2288
  		return -EFSCORRUPTED;
eeb116881   Dave Chinner   xfs: refactor xlo...
2289
2290
2291
2292
2293
2294
2295
  	}
  
  	trans = xlog_recover_ophdr_to_trans(rhash, rhead, ohead);
  	if (!trans) {
  		/* nothing to do, so skip over this ophdr */
  		return 0;
  	}
12818d24d   Brian Foster   xfs: rework log r...
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
  	/*
  	 * The recovered buffer queue is drained only once we know that all
  	 * recovery items for the current LSN have been processed. This is
  	 * required because:
  	 *
  	 * - Buffer write submission updates the metadata LSN of the buffer.
  	 * - Log recovery skips items with a metadata LSN >= the current LSN of
  	 *   the recovery item.
  	 * - Separate recovery items against the same metadata buffer can share
  	 *   a current LSN. I.e., consider that the LSN of a recovery item is
  	 *   defined as the starting LSN of the first record in which its
  	 *   transaction appears, that a record can hold multiple transactions,
  	 *   and/or that a transaction can span multiple records.
  	 *
  	 * In other words, we are allowed to submit a buffer from log recovery
  	 * once per current LSN. Otherwise, we may incorrectly skip recovery
  	 * items and cause corruption.
  	 *
  	 * We don't know up front whether buffers are updated multiple times per
  	 * LSN. Therefore, track the current LSN of each commit log record as it
  	 * is processed and drain the queue when it changes. Use commit records
  	 * because they are ordered correctly by the logging code.
  	 */
  	if (log->l_recovery_lsn != trans->r_lsn &&
  	    ohead->oh_flags & XLOG_COMMIT_TRANS) {
  		error = xfs_buf_delwri_submit(buffer_list);
  		if (error)
  			return error;
  		log->l_recovery_lsn = trans->r_lsn;
  	}
e9131e50f   Dave Chinner   xfs: recovery of ...
2326
  	return xlog_recovery_process_trans(log, trans, dp, len,
12818d24d   Brian Foster   xfs: rework log r...
2327
  					   ohead->oh_flags, pass, buffer_list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
  }
  
  /*
   * There are two valid states of the r_state field.  0 indicates that the
   * transaction structure is in a normal state.  We have either seen the
   * start of the transaction or the last operation we added was not a partial
   * operation.  If the last operation we added to the transaction was a
   * partial operation, we need to mark r_state with XLOG_WAS_CONT_TRANS.
   *
   * NOTE: skip LRs with 0 data length.
   */
  STATIC int
  xlog_recover_process_data(
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
2341
  	struct xlog		*log,
f0a769538   Dave Chinner   xfs: Use list_hea...
2342
  	struct hlist_head	rhash[],
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
2343
  	struct xlog_rec_header	*rhead,
b2a922cd6   Christoph Hellwig   xfs: remove xfs_c...
2344
  	char			*dp,
12818d24d   Brian Foster   xfs: rework log r...
2345
2346
  	int			pass,
  	struct list_head	*buffer_list)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2347
  {
eeb116881   Dave Chinner   xfs: refactor xlo...
2348
  	struct xlog_op_header	*ohead;
b2a922cd6   Christoph Hellwig   xfs: remove xfs_c...
2349
  	char			*end;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2350
  	int			num_logops;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2351
  	int			error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2352

eeb116881   Dave Chinner   xfs: refactor xlo...
2353
  	end = dp + be32_to_cpu(rhead->h_len);
b53e675dc   Christoph Hellwig   [XFS] xlog_rec_he...
2354
  	num_logops = be32_to_cpu(rhead->h_num_logops);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2355
2356
2357
  
  	/* check the log format matches our own - else we can't recover */
  	if (xlog_header_check_recover(log->l_mp, rhead))
2451337dd   Dave Chinner   xfs: global error...
2358
  		return -EIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2359

5cd9cee98   Brian Foster   xfs: log recovery...
2360
  	trace_xfs_log_recover_record(log, rhead, pass);
eeb116881   Dave Chinner   xfs: refactor xlo...
2361
2362
2363
2364
2365
2366
2367
2368
  	while ((dp < end) && num_logops) {
  
  		ohead = (struct xlog_op_header *)dp;
  		dp += sizeof(*ohead);
  		ASSERT(dp <= end);
  
  		/* errors will abort recovery */
  		error = xlog_recover_process_ophdr(log, rhash, rhead, ohead,
12818d24d   Brian Foster   xfs: rework log r...
2369
  						   dp, end, pass, buffer_list);
eeb116881   Dave Chinner   xfs: refactor xlo...
2370
2371
  		if (error)
  			return error;
67fcb7bfb   Christoph Hellwig   [XFS] clean up so...
2372
  		dp += be32_to_cpu(ohead->oh_len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2373
2374
2375
2376
  		num_logops--;
  	}
  	return 0;
  }
509955823   Darrick J. Wong   xfs: log recovery...
2377
2378
2379
  /* Take all the collected deferred ops and finish them in order. */
  static int
  xlog_finish_defer_ops(
e6fff81e4   Darrick J. Wong   xfs: proper repla...
2380
2381
  	struct xfs_mount	*mp,
  	struct list_head	*capture_list)
509955823   Darrick J. Wong   xfs: log recovery...
2382
  {
e6fff81e4   Darrick J. Wong   xfs: proper repla...
2383
  	struct xfs_defer_capture *dfc, *next;
509955823   Darrick J. Wong   xfs: log recovery...
2384
  	struct xfs_trans	*tp;
ff4ab5e02   Darrick J. Wong   xfs: fix an incor...
2385
  	struct xfs_inode	*ip;
e6fff81e4   Darrick J. Wong   xfs: proper repla...
2386
  	int			error = 0;
509955823   Darrick J. Wong   xfs: log recovery...
2387

e6fff81e4   Darrick J. Wong   xfs: proper repla...
2388
  	list_for_each_entry_safe(dfc, next, capture_list, dfc_list) {
929b92f64   Darrick J. Wong   xfs: xfs_defer_ca...
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
  		struct xfs_trans_res	resv;
  
  		/*
  		 * Create a new transaction reservation from the captured
  		 * information.  Set logcount to 1 to force the new transaction
  		 * to regrant every roll so that we can make forward progress
  		 * in recovery no matter how full the log might be.
  		 */
  		resv.tr_logres = dfc->dfc_logres;
  		resv.tr_logcount = 1;
  		resv.tr_logflags = XFS_TRANS_PERM_LOG_RES;
  
  		error = xfs_trans_alloc(mp, &resv, dfc->dfc_blkres,
  				dfc->dfc_rtxres, XFS_TRANS_RESERVE, &tp);
e6fff81e4   Darrick J. Wong   xfs: proper repla...
2403
2404
  		if (error)
  			return error;
509955823   Darrick J. Wong   xfs: log recovery...
2405

e6fff81e4   Darrick J. Wong   xfs: proper repla...
2406
2407
2408
2409
2410
  		/*
  		 * Transfer to this new transaction all the dfops we captured
  		 * from recovering a single intent item.
  		 */
  		list_del_init(&dfc->dfc_list);
ff4ab5e02   Darrick J. Wong   xfs: fix an incor...
2411
  		xfs_defer_ops_continue(dfc, tp, &ip);
e6fff81e4   Darrick J. Wong   xfs: proper repla...
2412
2413
  
  		error = xfs_trans_commit(tp);
ff4ab5e02   Darrick J. Wong   xfs: fix an incor...
2414
2415
2416
2417
  		if (ip) {
  			xfs_iunlock(ip, XFS_ILOCK_EXCL);
  			xfs_irele(ip);
  		}
e6fff81e4   Darrick J. Wong   xfs: proper repla...
2418
2419
2420
2421
2422
2423
  		if (error)
  			return error;
  	}
  
  	ASSERT(list_empty(capture_list));
  	return 0;
509955823   Darrick J. Wong   xfs: log recovery...
2424
  }
e6fff81e4   Darrick J. Wong   xfs: proper repla...
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
  /* Release all the captured defer ops and capture structures in this list. */
  static void
  xlog_abort_defer_ops(
  	struct xfs_mount		*mp,
  	struct list_head		*capture_list)
  {
  	struct xfs_defer_capture	*dfc;
  	struct xfs_defer_capture	*next;
  
  	list_for_each_entry_safe(dfc, next, capture_list, dfc_list) {
  		list_del_init(&dfc->dfc_list);
  		xfs_defer_ops_release(mp, dfc);
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2439
  /*
dc42375d5   Darrick J. Wong   xfs: refactor red...
2440
2441
2442
   * When this is called, all of the log intent items which did not have
   * corresponding log done items should be in the AIL.  What we do now
   * is update the data structures associated with each one.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2443
   *
dc42375d5   Darrick J. Wong   xfs: refactor red...
2444
2445
2446
2447
2448
2449
   * Since we process the log intent items in normal transactions, they
   * will be removed at some point after the commit.  This prevents us
   * from just walking down the list processing each one.  We'll use a
   * flag in the intent item to skip those that we've already processed
   * and use the AIL iteration mechanism's generation count to try to
   * speed this up at least a bit.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2450
   *
dc42375d5   Darrick J. Wong   xfs: refactor red...
2451
2452
2453
   * When we start, we know that the intents are the only things in the
   * AIL.  As we process them, however, other items are added to the
   * AIL.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2454
   */
3c1e2bbe5   David Chinner   [XFS] Propagate x...
2455
  STATIC int
dc42375d5   Darrick J. Wong   xfs: refactor red...
2456
  xlog_recover_process_intents(
f0b2efad1   Brian Foster   xfs: don't leave ...
2457
  	struct xlog		*log)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2458
  {
e6fff81e4   Darrick J. Wong   xfs: proper repla...
2459
  	LIST_HEAD(capture_list);
27d8d5fe0   David Chinner   [XFS] Use a curso...
2460
  	struct xfs_ail_cursor	cur;
509955823   Darrick J. Wong   xfs: log recovery...
2461
  	struct xfs_log_item	*lip;
a9c21c1b9   David Chinner   [XFS] Given the l...
2462
  	struct xfs_ail		*ailp;
e6fff81e4   Darrick J. Wong   xfs: proper repla...
2463
  	int			error = 0;
7bf7a193a   Darrick J. Wong   xfs: fix compiler...
2464
  #if defined(DEBUG) || defined(XFS_WARN)
dc42375d5   Darrick J. Wong   xfs: refactor red...
2465
  	xfs_lsn_t		last_lsn;
7bf7a193a   Darrick J. Wong   xfs: fix compiler...
2466
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2467

a9c21c1b9   David Chinner   [XFS] Given the l...
2468
  	ailp = log->l_ailp;
57e809561   Matthew Wilcox   xfs: Rename xa_ e...
2469
  	spin_lock(&ailp->ail_lock);
7bf7a193a   Darrick J. Wong   xfs: fix compiler...
2470
  #if defined(DEBUG) || defined(XFS_WARN)
dc42375d5   Darrick J. Wong   xfs: refactor red...
2471
  	last_lsn = xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block);
7bf7a193a   Darrick J. Wong   xfs: fix compiler...
2472
  #endif
e6fff81e4   Darrick J. Wong   xfs: proper repla...
2473
2474
2475
  	for (lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
  	     lip != NULL;
  	     lip = xfs_trans_ail_cursor_next(ailp, &cur)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2476
  		/*
dc42375d5   Darrick J. Wong   xfs: refactor red...
2477
2478
  		 * We're done when we see something other than an intent.
  		 * There should be no intents left in the AIL now.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2479
  		 */
dc42375d5   Darrick J. Wong   xfs: refactor red...
2480
  		if (!xlog_item_is_intent(lip)) {
27d8d5fe0   David Chinner   [XFS] Use a curso...
2481
  #ifdef DEBUG
a9c21c1b9   David Chinner   [XFS] Given the l...
2482
  			for (; lip; lip = xfs_trans_ail_cursor_next(ailp, &cur))
dc42375d5   Darrick J. Wong   xfs: refactor red...
2483
  				ASSERT(!xlog_item_is_intent(lip));
27d8d5fe0   David Chinner   [XFS] Use a curso...
2484
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2485
2486
2487
2488
  			break;
  		}
  
  		/*
dc42375d5   Darrick J. Wong   xfs: refactor red...
2489
2490
2491
  		 * We should never see a redo item with a LSN higher than
  		 * the last transaction we found in the log at the start
  		 * of recovery.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2492
  		 */
dc42375d5   Darrick J. Wong   xfs: refactor red...
2493
  		ASSERT(XFS_LSN_CMP(last_lsn, lip->li_lsn) >= 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2494

509955823   Darrick J. Wong   xfs: log recovery...
2495
2496
  		/*
  		 * NOTE: If your intent processing routine can create more
e6fff81e4   Darrick J. Wong   xfs: proper repla...
2497
2498
  		 * deferred ops, you /must/ attach them to the capture list in
  		 * the recover routine or else those subsequent intents will be
509955823   Darrick J. Wong   xfs: log recovery...
2499
2500
  		 * replayed in the wrong order!
  		 */
901219bb2   Darrick J. Wong   xfs: remove XFS_L...
2501
  		spin_unlock(&ailp->ail_lock);
e6fff81e4   Darrick J. Wong   xfs: proper repla...
2502
  		error = lip->li_ops->iop_recover(lip, &capture_list);
901219bb2   Darrick J. Wong   xfs: remove XFS_L...
2503
  		spin_lock(&ailp->ail_lock);
27d8d5fe0   David Chinner   [XFS] Use a curso...
2504
  		if (error)
e6fff81e4   Darrick J. Wong   xfs: proper repla...
2505
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2506
  	}
e6fff81e4   Darrick J. Wong   xfs: proper repla...
2507

e4a1e29cb   Eric Sandeen   xfs: remove unuse...
2508
  	xfs_trans_ail_cursor_done(&cur);
57e809561   Matthew Wilcox   xfs: Rename xa_ e...
2509
  	spin_unlock(&ailp->ail_lock);
e6fff81e4   Darrick J. Wong   xfs: proper repla...
2510
2511
  	if (error)
  		goto err;
509955823   Darrick J. Wong   xfs: log recovery...
2512

e6fff81e4   Darrick J. Wong   xfs: proper repla...
2513
2514
2515
2516
2517
2518
2519
  	error = xlog_finish_defer_ops(log->l_mp, &capture_list);
  	if (error)
  		goto err;
  
  	return 0;
  err:
  	xlog_abort_defer_ops(log->l_mp, &capture_list);
3c1e2bbe5   David Chinner   [XFS] Propagate x...
2520
  	return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2521
2522
2523
  }
  
  /*
dc42375d5   Darrick J. Wong   xfs: refactor red...
2524
2525
   * A cancel occurs when the mount has failed and we're bailing out.
   * Release all pending log intent items so they don't pin the AIL.
f0b2efad1   Brian Foster   xfs: don't leave ...
2526
   */
a7a9250e1   Hariprasad Kelam   fs: xfs: xfs_log:...
2527
  STATIC void
dc42375d5   Darrick J. Wong   xfs: refactor red...
2528
  xlog_recover_cancel_intents(
f0b2efad1   Brian Foster   xfs: don't leave ...
2529
2530
2531
  	struct xlog		*log)
  {
  	struct xfs_log_item	*lip;
f0b2efad1   Brian Foster   xfs: don't leave ...
2532
2533
2534
2535
  	struct xfs_ail_cursor	cur;
  	struct xfs_ail		*ailp;
  
  	ailp = log->l_ailp;
57e809561   Matthew Wilcox   xfs: Rename xa_ e...
2536
  	spin_lock(&ailp->ail_lock);
f0b2efad1   Brian Foster   xfs: don't leave ...
2537
2538
2539
  	lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
  	while (lip != NULL) {
  		/*
dc42375d5   Darrick J. Wong   xfs: refactor red...
2540
2541
  		 * We're done when we see something other than an intent.
  		 * There should be no intents left in the AIL now.
f0b2efad1   Brian Foster   xfs: don't leave ...
2542
  		 */
dc42375d5   Darrick J. Wong   xfs: refactor red...
2543
  		if (!xlog_item_is_intent(lip)) {
f0b2efad1   Brian Foster   xfs: don't leave ...
2544
2545
  #ifdef DEBUG
  			for (; lip; lip = xfs_trans_ail_cursor_next(ailp, &cur))
dc42375d5   Darrick J. Wong   xfs: refactor red...
2546
  				ASSERT(!xlog_item_is_intent(lip));
f0b2efad1   Brian Foster   xfs: don't leave ...
2547
2548
2549
  #endif
  			break;
  		}
9329ba89c   Darrick J. Wong   xfs: refactor rec...
2550
2551
2552
  		spin_unlock(&ailp->ail_lock);
  		lip->li_ops->iop_release(lip);
  		spin_lock(&ailp->ail_lock);
f0b2efad1   Brian Foster   xfs: don't leave ...
2553
2554
2555
2556
  		lip = xfs_trans_ail_cursor_next(ailp, &cur);
  	}
  
  	xfs_trans_ail_cursor_done(&cur);
57e809561   Matthew Wilcox   xfs: Rename xa_ e...
2557
  	spin_unlock(&ailp->ail_lock);
f0b2efad1   Brian Foster   xfs: don't leave ...
2558
2559
2560
  }
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
   * This routine performs a transaction to null out a bad inode pointer
   * in an agi unlinked inode hash bucket.
   */
  STATIC void
  xlog_recover_clear_agi_bucket(
  	xfs_mount_t	*mp,
  	xfs_agnumber_t	agno,
  	int		bucket)
  {
  	xfs_trans_t	*tp;
  	xfs_agi_t	*agi;
  	xfs_buf_t	*agibp;
  	int		offset;
  	int		error;
253f4911f   Christoph Hellwig   xfs: better xfs_t...
2575
  	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_clearagi, 0, 0, 0, &tp);
e5720eec0   David Chinner   [XFS] Propagate e...
2576
  	if (error)
253f4911f   Christoph Hellwig   xfs: better xfs_t...
2577
  		goto out_error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2578

5e1be0fb1   Christoph Hellwig   [XFS] factor out ...
2579
2580
  	error = xfs_read_agi(mp, tp, agno, &agibp);
  	if (error)
e5720eec0   David Chinner   [XFS] Propagate e...
2581
  		goto out_abort;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2582

370c782b9   Christoph Hellwig   xfs: remove XFS_B...
2583
  	agi = agibp->b_addr;
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
2584
  	agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2585
2586
2587
2588
  	offset = offsetof(xfs_agi_t, agi_unlinked) +
  		 (sizeof(xfs_agino_t) * bucket);
  	xfs_trans_log_buf(tp, agibp, offset,
  			  (offset + sizeof(xfs_agino_t) - 1));
70393313d   Christoph Hellwig   xfs: saner xfs_tr...
2589
  	error = xfs_trans_commit(tp);
e5720eec0   David Chinner   [XFS] Propagate e...
2590
2591
2592
2593
2594
  	if (error)
  		goto out_error;
  	return;
  
  out_abort:
4906e2154   Christoph Hellwig   xfs: remove the f...
2595
  	xfs_trans_cancel(tp);
e5720eec0   David Chinner   [XFS] Propagate e...
2596
  out_error:
a0fa2b679   Dave Chinner   xfs: Convert xlog...
2597
  	xfs_warn(mp, "%s: failed to clear agi %d. Continuing.", __func__, agno);
e5720eec0   David Chinner   [XFS] Propagate e...
2598
  	return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2599
  }
23fac50f9   Christoph Hellwig   [XFS] split up xl...
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
  STATIC xfs_agino_t
  xlog_recover_process_one_iunlink(
  	struct xfs_mount		*mp,
  	xfs_agnumber_t			agno,
  	xfs_agino_t			agino,
  	int				bucket)
  {
  	struct xfs_buf			*ibp;
  	struct xfs_dinode		*dip;
  	struct xfs_inode		*ip;
  	xfs_ino_t			ino;
  	int				error;
  
  	ino = XFS_AGINO_TO_INO(mp, agno, agino);
7b6259e7a   Dave Chinner   xfs: remove block...
2614
  	error = xfs_iget(mp, NULL, ino, 0, 0, &ip);
23fac50f9   Christoph Hellwig   [XFS] split up xl...
2615
2616
2617
2618
2619
2620
  	if (error)
  		goto fail;
  
  	/*
  	 * Get the on disk inode to find the next inode in the bucket.
  	 */
c19950799   Brian Foster   xfs: remove unuse...
2621
  	error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &ibp, 0);
23fac50f9   Christoph Hellwig   [XFS] split up xl...
2622
  	if (error)
0e446673a   Christoph Hellwig   [XFS] fix error h...
2623
  		goto fail_iput;
23fac50f9   Christoph Hellwig   [XFS] split up xl...
2624

17c12bcd3   Darrick J. Wong   xfs: when replayi...
2625
  	xfs_iflags_clear(ip, XFS_IRECOVERY);
54d7b5c1d   Dave Chinner   xfs: use vfs inod...
2626
  	ASSERT(VFS_I(ip)->i_nlink == 0);
c19b3b05a   Dave Chinner   xfs: mode di_mode...
2627
  	ASSERT(VFS_I(ip)->i_mode != 0);
23fac50f9   Christoph Hellwig   [XFS] split up xl...
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
  
  	/* setup for the next pass */
  	agino = be32_to_cpu(dip->di_next_unlinked);
  	xfs_buf_relse(ibp);
  
  	/*
  	 * Prevent any DMAPI event from being sent when the reference on
  	 * the inode is dropped.
  	 */
  	ip->i_d.di_dmevmask = 0;
44a8736bd   Darrick J. Wong   xfs: clean up IRE...
2638
  	xfs_irele(ip);
23fac50f9   Christoph Hellwig   [XFS] split up xl...
2639
  	return agino;
0e446673a   Christoph Hellwig   [XFS] fix error h...
2640
   fail_iput:
44a8736bd   Darrick J. Wong   xfs: clean up IRE...
2641
  	xfs_irele(ip);
23fac50f9   Christoph Hellwig   [XFS] split up xl...
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
   fail:
  	/*
  	 * We can't read in the inode this bucket points to, or this inode
  	 * is messed up.  Just ditch this bucket of inodes.  We will lose
  	 * some inodes and space, but at least we won't hang.
  	 *
  	 * Call xlog_recover_clear_agi_bucket() to perform a transaction to
  	 * clear the inode pointer in the bucket.
  	 */
  	xlog_recover_clear_agi_bucket(mp, agno, bucket);
  	return NULLAGINO;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2654
  /*
8ab39f11d   Dave Chinner   xfs: prevent CIL ...
2655
   * Recover AGI unlinked lists
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2656
   *
8ab39f11d   Dave Chinner   xfs: prevent CIL ...
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
   * This is called during recovery to process any inodes which we unlinked but
   * not freed when the system crashed.  These inodes will be on the lists in the
   * AGI blocks. What we do here is scan all the AGIs and fully truncate and free
   * any inodes found on the lists. Each inode is removed from the lists when it
   * has been fully truncated and is freed. The freeing of the inode and its
   * removal from the list must be atomic.
   *
   * If everything we touch in the agi processing loop is already in memory, this
   * loop can hold the cpu for a long time. It runs without lock contention,
   * memory allocation contention, the need wait for IO, etc, and so will run
   * until we either run out of inodes to process, run low on memory or we run out
   * of log space.
   *
   * This behaviour is bad for latency on single CPU and non-preemptible kernels,
   * and can prevent other filesytem work (such as CIL pushes) from running. This
   * can lead to deadlocks if the recovery process runs out of log reservation
   * space. Hence we need to yield the CPU when there is other kernel work
   * scheduled on this CPU to ensure other scheduled work can run without undue
   * latency.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2676
   */
d96f8f891   Eric Sandeen   xfs: add more sta...
2677
  STATIC void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2678
  xlog_recover_process_iunlinks(
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
2679
  	struct xlog	*log)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2680
2681
2682
2683
2684
  {
  	xfs_mount_t	*mp;
  	xfs_agnumber_t	agno;
  	xfs_agi_t	*agi;
  	xfs_buf_t	*agibp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2685
  	xfs_agino_t	agino;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2686
2687
  	int		bucket;
  	int		error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2688
2689
  
  	mp = log->l_mp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2690
2691
2692
2693
  	for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
  		/*
  		 * Find the agi for this ag.
  		 */
5e1be0fb1   Christoph Hellwig   [XFS] factor out ...
2694
2695
2696
2697
2698
2699
2700
2701
2702
  		error = xfs_read_agi(mp, NULL, agno, &agibp);
  		if (error) {
  			/*
  			 * AGI is b0rked. Don't process it.
  			 *
  			 * We should probably mark the filesystem as corrupt
  			 * after we've recovered all the ag's we can....
  			 */
  			continue;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2703
  		}
d97d32edc   Jan Kara   xfs: Fix oops on ...
2704
2705
2706
2707
2708
2709
2710
2711
2712
  		/*
  		 * Unlock the buffer so that it can be acquired in the normal
  		 * course of the transaction to truncate and free each inode.
  		 * Because we are not racing with anyone else here for the AGI
  		 * buffer, we don't even need to hold it locked to read the
  		 * initial unlinked bucket entries out of the buffer. We keep
  		 * buffer reference though, so that it stays pinned in memory
  		 * while we need the buffer.
  		 */
370c782b9   Christoph Hellwig   xfs: remove XFS_B...
2713
  		agi = agibp->b_addr;
d97d32edc   Jan Kara   xfs: Fix oops on ...
2714
  		xfs_buf_unlock(agibp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2715
2716
  
  		for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++) {
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
2717
  			agino = be32_to_cpu(agi->agi_unlinked[bucket]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2718
  			while (agino != NULLAGINO) {
23fac50f9   Christoph Hellwig   [XFS] split up xl...
2719
2720
  				agino = xlog_recover_process_one_iunlink(mp,
  							agno, agino, bucket);
8ab39f11d   Dave Chinner   xfs: prevent CIL ...
2721
  				cond_resched();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2722
2723
  			}
  		}
d97d32edc   Jan Kara   xfs: Fix oops on ...
2724
  		xfs_buf_rele(agibp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2725
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2726
  }
910832697   Eric Sandeen   xfs: change some ...
2727
  STATIC void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2728
  xlog_unpack_data(
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
2729
  	struct xlog_rec_header	*rhead,
b2a922cd6   Christoph Hellwig   xfs: remove xfs_c...
2730
  	char			*dp,
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
2731
  	struct xlog		*log)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2732
2733
  {
  	int			i, j, k;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2734

b53e675dc   Christoph Hellwig   [XFS] xlog_rec_he...
2735
  	for (i = 0; i < BTOBB(be32_to_cpu(rhead->h_len)) &&
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2736
  		  i < (XLOG_HEADER_CYCLE_SIZE / BBSIZE); i++) {
b53e675dc   Christoph Hellwig   [XFS] xlog_rec_he...
2737
  		*(__be32 *)dp = *(__be32 *)&rhead->h_cycle_data[i];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2738
2739
  		dp += BBSIZE;
  	}
621187099   Eric Sandeen   [XFS] remove shou...
2740
  	if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
b28708d6a   Christoph Hellwig   [XFS] sanitize xl...
2741
  		xlog_in_core_2_t *xhdr = (xlog_in_core_2_t *)rhead;
b53e675dc   Christoph Hellwig   [XFS] xlog_rec_he...
2742
  		for ( ; i < BTOBB(be32_to_cpu(rhead->h_len)); i++) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2743
2744
  			j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
  			k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
b53e675dc   Christoph Hellwig   [XFS] xlog_rec_he...
2745
  			*(__be32 *)dp = xhdr[j].hic_xheader.xh_cycle_data[k];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2746
2747
2748
  			dp += BBSIZE;
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2749
  }
9d94901f6   Brian Foster   xfs: refactor log...
2750
  /*
b94fb2d17   Brian Foster   xfs: refactor and...
2751
   * CRC check, unpack and process a log record.
9d94901f6   Brian Foster   xfs: refactor log...
2752
2753
2754
2755
2756
2757
2758
   */
  STATIC int
  xlog_recover_process(
  	struct xlog		*log,
  	struct hlist_head	rhash[],
  	struct xlog_rec_header	*rhead,
  	char			*dp,
12818d24d   Brian Foster   xfs: rework log r...
2759
2760
  	int			pass,
  	struct list_head	*buffer_list)
9d94901f6   Brian Foster   xfs: refactor log...
2761
  {
cae028df5   Dave Chinner   xfs: optimise CRC...
2762
  	__le32			old_crc = rhead->h_crc;
b94fb2d17   Brian Foster   xfs: refactor and...
2763
  	__le32			crc;
6528250b7   Brian Foster   xfs: support a cr...
2764
  	crc = xlog_cksum(log, rhead, dp, be32_to_cpu(rhead->h_len));
b94fb2d17   Brian Foster   xfs: refactor and...
2765
  	/*
6528250b7   Brian Foster   xfs: support a cr...
2766
2767
  	 * Nothing else to do if this is a CRC verification pass. Just return
  	 * if this a record with a non-zero crc. Unfortunately, mkfs always
cae028df5   Dave Chinner   xfs: optimise CRC...
2768
  	 * sets old_crc to 0 so we must consider this valid even on v5 supers.
6528250b7   Brian Foster   xfs: support a cr...
2769
2770
2771
2772
  	 * Otherwise, return EFSBADCRC on failure so the callers up the stack
  	 * know precisely what failed.
  	 */
  	if (pass == XLOG_RECOVER_CRCPASS) {
cae028df5   Dave Chinner   xfs: optimise CRC...
2773
  		if (old_crc && crc != old_crc)
6528250b7   Brian Foster   xfs: support a cr...
2774
2775
2776
2777
2778
2779
2780
2781
2782
  			return -EFSBADCRC;
  		return 0;
  	}
  
  	/*
  	 * We're in the normal recovery path. Issue a warning if and only if the
  	 * CRC in the header is non-zero. This is an advisory warning and the
  	 * zero CRC check prevents warnings from being emitted when upgrading
  	 * the kernel from one that does not add CRCs by default.
b94fb2d17   Brian Foster   xfs: refactor and...
2783
  	 */
cae028df5   Dave Chinner   xfs: optimise CRC...
2784
2785
  	if (crc != old_crc) {
  		if (old_crc || xfs_sb_version_hascrc(&log->l_mp->m_sb)) {
b94fb2d17   Brian Foster   xfs: refactor and...
2786
2787
  			xfs_alert(log->l_mp,
  		"log record CRC mismatch: found 0x%x, expected 0x%x.",
cae028df5   Dave Chinner   xfs: optimise CRC...
2788
  					le32_to_cpu(old_crc),
b94fb2d17   Brian Foster   xfs: refactor and...
2789
2790
2791
2792
2793
2794
2795
2796
  					le32_to_cpu(crc));
  			xfs_hex_dump(dp, 32);
  		}
  
  		/*
  		 * If the filesystem is CRC enabled, this mismatch becomes a
  		 * fatal log corruption failure.
  		 */
a5155b870   Darrick J. Wong   xfs: always log c...
2797
2798
  		if (xfs_sb_version_hascrc(&log->l_mp->m_sb)) {
  			XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, log->l_mp);
b94fb2d17   Brian Foster   xfs: refactor and...
2799
  			return -EFSCORRUPTED;
a5155b870   Darrick J. Wong   xfs: always log c...
2800
  		}
b94fb2d17   Brian Foster   xfs: refactor and...
2801
  	}
9d94901f6   Brian Foster   xfs: refactor log...
2802

910832697   Eric Sandeen   xfs: change some ...
2803
  	xlog_unpack_data(rhead, dp, log);
9d94901f6   Brian Foster   xfs: refactor log...
2804

12818d24d   Brian Foster   xfs: rework log r...
2805
2806
  	return xlog_recover_process_data(log, rhash, rhead, dp, pass,
  					 buffer_list);
9d94901f6   Brian Foster   xfs: refactor log...
2807
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2808
2809
  STATIC int
  xlog_valid_rec_header(
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
2810
2811
  	struct xlog		*log,
  	struct xlog_rec_header	*rhead,
f692d09e9   Gao Xiang   xfs: avoid LR buf...
2812
2813
  	xfs_daddr_t		blkno,
  	int			bufsize)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2814
2815
  {
  	int			hlen;
a71895c5d   Darrick J. Wong   xfs: convert open...
2816
2817
  	if (XFS_IS_CORRUPT(log->l_mp,
  			   rhead->h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM)))
2451337dd   Dave Chinner   xfs: global error...
2818
  		return -EFSCORRUPTED;
a71895c5d   Darrick J. Wong   xfs: convert open...
2819
2820
2821
2822
  	if (XFS_IS_CORRUPT(log->l_mp,
  			   (!rhead->h_version ||
  			   (be32_to_cpu(rhead->h_version) &
  			    (~XLOG_VERSION_OKBITS))))) {
a0fa2b679   Dave Chinner   xfs: Convert xlog...
2823
  		xfs_warn(log->l_mp, "%s: unrecognised log version (%d).",
34a622b2e   Harvey Harrison   [XFS] replace rem...
2824
  			__func__, be32_to_cpu(rhead->h_version));
895e196fb   Darrick J. Wong   xfs: convert EIO ...
2825
  		return -EFSCORRUPTED;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2826
  	}
f692d09e9   Gao Xiang   xfs: avoid LR buf...
2827
2828
2829
2830
  	/*
  	 * LR body must have data (or it wouldn't have been written)
  	 * and h_len must not be greater than LR buffer size.
  	 */
b53e675dc   Christoph Hellwig   [XFS] xlog_rec_he...
2831
  	hlen = be32_to_cpu(rhead->h_len);
f692d09e9   Gao Xiang   xfs: avoid LR buf...
2832
  	if (XFS_IS_CORRUPT(log->l_mp, hlen <= 0 || hlen > bufsize))
2451337dd   Dave Chinner   xfs: global error...
2833
  		return -EFSCORRUPTED;
f692d09e9   Gao Xiang   xfs: avoid LR buf...
2834

a71895c5d   Darrick J. Wong   xfs: convert open...
2835
2836
  	if (XFS_IS_CORRUPT(log->l_mp,
  			   blkno > log->l_logBBsize || blkno > INT_MAX))
2451337dd   Dave Chinner   xfs: global error...
2837
  		return -EFSCORRUPTED;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
  	return 0;
  }
  
  /*
   * Read the log from tail to head and process the log records found.
   * Handle the two cases where the tail and head are in the same cycle
   * and where the active portion of the log wraps around the end of
   * the physical log separately.  The pass parameter is passed through
   * to the routines called to process the data and is not looked at
   * here.
   */
  STATIC int
  xlog_do_recovery_pass(
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
2851
  	struct xlog		*log,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2852
2853
  	xfs_daddr_t		head_blk,
  	xfs_daddr_t		tail_blk,
d7f37692e   Brian Foster   xfs: return start...
2854
2855
  	int			pass,
  	xfs_daddr_t		*first_bad)	/* out: first bad log rec */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2856
2857
  {
  	xlog_rec_header_t	*rhead;
284f1c2c9   Brian Foster   xfs: fix recovery...
2858
  	xfs_daddr_t		blk_no, rblk_no;
d7f37692e   Brian Foster   xfs: return start...
2859
  	xfs_daddr_t		rhead_blk;
b2a922cd6   Christoph Hellwig   xfs: remove xfs_c...
2860
  	char			*offset;
6ad5b3255   Christoph Hellwig   xfs: use bios dir...
2861
  	char			*hbp, *dbp;
a70f9fe52   Brian Foster   xfs: detect and h...
2862
  	int			error = 0, h_size, h_len;
12818d24d   Brian Foster   xfs: rework log r...
2863
  	int			error2 = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2864
2865
  	int			bblks, split_bblks;
  	int			hblks, split_hblks, wrapped_hblks;
39775431f   Brian Foster   xfs: free uncommi...
2866
  	int			i;
f0a769538   Dave Chinner   xfs: Use list_hea...
2867
  	struct hlist_head	rhash[XLOG_RHASH_SIZE];
12818d24d   Brian Foster   xfs: rework log r...
2868
  	LIST_HEAD		(buffer_list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2869
2870
  
  	ASSERT(head_blk != tail_blk);
a4c9b34d6   Brian Foster   xfs: handle -EFSC...
2871
  	blk_no = rhead_blk = tail_blk;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2872

39775431f   Brian Foster   xfs: free uncommi...
2873
2874
  	for (i = 0; i < XLOG_RHASH_SIZE; i++)
  		INIT_HLIST_HEAD(&rhash[i]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2875
2876
2877
2878
  	/*
  	 * Read the header of the tail block and get the iclog buffer size from
  	 * h_size.  Use this to tell how many sectors make up the log header.
  	 */
621187099   Eric Sandeen   [XFS] remove shou...
2879
  	if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2880
2881
2882
2883
2884
  		/*
  		 * When using variable length iclogs, read first sector of
  		 * iclog header and extract the header size from it.  Get a
  		 * new hbp that is the correct size.
  		 */
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
2885
  		hbp = xlog_alloc_buffer(log, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2886
  		if (!hbp)
2451337dd   Dave Chinner   xfs: global error...
2887
  			return -ENOMEM;
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
2888
2889
2890
  
  		error = xlog_bread(log, tail_blk, 1, hbp, &offset);
  		if (error)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2891
  			goto bread_err1;
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
2892

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2893
  		rhead = (xlog_rec_header_t *)offset;
a70f9fe52   Brian Foster   xfs: detect and h...
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
  
  		/*
  		 * xfsprogs has a bug where record length is based on lsunit but
  		 * h_size (iclog size) is hardcoded to 32k. Now that we
  		 * unconditionally CRC verify the unmount record, this means the
  		 * log buffer can be too small for the record and cause an
  		 * overrun.
  		 *
  		 * Detect this condition here. Use lsunit for the buffer size as
  		 * long as this looks like the mkfs case. Otherwise, return an
  		 * error to avoid a buffer overrun.
  		 */
b53e675dc   Christoph Hellwig   [XFS] xlog_rec_he...
2906
  		h_size = be32_to_cpu(rhead->h_size);
a70f9fe52   Brian Foster   xfs: detect and h...
2907
  		h_len = be32_to_cpu(rhead->h_len);
f692d09e9   Gao Xiang   xfs: avoid LR buf...
2908
2909
2910
  		if (h_len > h_size && h_len <= log->l_mp->m_logbsize &&
  		    rhead->h_num_logops == cpu_to_be32(1)) {
  			xfs_warn(log->l_mp,
a70f9fe52   Brian Foster   xfs: detect and h...
2911
  		"invalid iclog size (%d bytes), using lsunit (%d bytes)",
f692d09e9   Gao Xiang   xfs: avoid LR buf...
2912
2913
  				 h_size, log->l_mp->m_logbsize);
  			h_size = log->l_mp->m_logbsize;
a70f9fe52   Brian Foster   xfs: detect and h...
2914
  		}
f692d09e9   Gao Xiang   xfs: avoid LR buf...
2915
2916
2917
  		error = xlog_valid_rec_header(log, rhead, tail_blk, h_size);
  		if (error)
  			goto bread_err1;
0c771b99d   Gao Xiang   xfs: clean up cal...
2918
2919
  		hblks = xlog_logrec_hblks(log, rhead);
  		if (hblks != 1) {
6ad5b3255   Christoph Hellwig   xfs: use bios dir...
2920
  			kmem_free(hbp);
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
2921
  			hbp = xlog_alloc_buffer(log, hblks);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2922
2923
  		}
  	} else {
69ce58f08   Alex Elder   xfs: record log s...
2924
  		ASSERT(log->l_sectBBsize == 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2925
  		hblks = 1;
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
2926
  		hbp = xlog_alloc_buffer(log, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2927
2928
2929
2930
  		h_size = XLOG_BIG_RECORD_BSIZE;
  	}
  
  	if (!hbp)
2451337dd   Dave Chinner   xfs: global error...
2931
  		return -ENOMEM;
6e9b3dd80   Christoph Hellwig   xfs: stop using b...
2932
  	dbp = xlog_alloc_buffer(log, BTOBB(h_size));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2933
  	if (!dbp) {
6ad5b3255   Christoph Hellwig   xfs: use bios dir...
2934
  		kmem_free(hbp);
2451337dd   Dave Chinner   xfs: global error...
2935
  		return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2936
2937
2938
  	}
  
  	memset(rhash, 0, sizeof(rhash));
970fd3f04   Eric Sandeen   xfs: deduplicate ...
2939
  	if (tail_blk > head_blk) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2940
2941
2942
  		/*
  		 * Perform recovery around the end of the physical log.
  		 * When the head is not on the same cycle number as the tail,
970fd3f04   Eric Sandeen   xfs: deduplicate ...
2943
  		 * we can't do a sequential recovery.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2944
  		 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2945
2946
2947
2948
  		while (blk_no < log->l_logBBsize) {
  			/*
  			 * Check for header wrapping around physical end-of-log
  			 */
6ad5b3255   Christoph Hellwig   xfs: use bios dir...
2949
  			offset = hbp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2950
2951
2952
2953
  			split_hblks = 0;
  			wrapped_hblks = 0;
  			if (blk_no + hblks <= log->l_logBBsize) {
  				/* Read header in one read */
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
2954
2955
  				error = xlog_bread(log, blk_no, hblks, hbp,
  						   &offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2956
2957
  				if (error)
  					goto bread_err2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2958
2959
2960
2961
2962
2963
2964
  			} else {
  				/* This LR is split across physical log end */
  				if (blk_no != log->l_logBBsize) {
  					/* some data before physical log end */
  					ASSERT(blk_no <= INT_MAX);
  					split_hblks = log->l_logBBsize - (int)blk_no;
  					ASSERT(split_hblks > 0);
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
2965
2966
2967
2968
  					error = xlog_bread(log, blk_no,
  							   split_hblks, hbp,
  							   &offset);
  					if (error)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2969
  						goto bread_err2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2970
  				}
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
2971

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
  				/*
  				 * Note: this black magic still works with
  				 * large sector sizes (non-512) only because:
  				 * - we increased the buffer size originally
  				 *   by 1 sector giving us enough extra space
  				 *   for the second read;
  				 * - the log start is guaranteed to be sector
  				 *   aligned;
  				 * - we read the log end (LR header start)
  				 *   _first_, then the log start (LR header end)
  				 *   - order is important.
  				 */
234f56aca   David Chinner   [XFS] Check for e...
2984
  				wrapped_hblks = hblks - split_hblks;
6ad5b3255   Christoph Hellwig   xfs: use bios dir...
2985
2986
  				error = xlog_bread_noalign(log, 0,
  						wrapped_hblks,
44396476a   Dave Chinner   xfs: reset buffer...
2987
  						offset + BBTOB(split_hblks));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2988
2989
  				if (error)
  					goto bread_err2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2990
2991
2992
  			}
  			rhead = (xlog_rec_header_t *)offset;
  			error = xlog_valid_rec_header(log, rhead,
f692d09e9   Gao Xiang   xfs: avoid LR buf...
2993
  					split_hblks ? blk_no : 0, h_size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2994
2995
  			if (error)
  				goto bread_err2;
b53e675dc   Christoph Hellwig   [XFS] xlog_rec_he...
2996
  			bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2997
  			blk_no += hblks;
284f1c2c9   Brian Foster   xfs: fix recovery...
2998
2999
3000
3001
3002
3003
3004
3005
3006
  			/*
  			 * Read the log record data in multiple reads if it
  			 * wraps around the end of the log. Note that if the
  			 * header already wrapped, blk_no could point past the
  			 * end of the log. The record data is contiguous in
  			 * that case.
  			 */
  			if (blk_no + bblks <= log->l_logBBsize ||
  			    blk_no >= log->l_logBBsize) {
0703a8e1c   Dave Chinner   xfs: replace do_m...
3007
  				rblk_no = xlog_wrap_logbno(log, blk_no);
284f1c2c9   Brian Foster   xfs: fix recovery...
3008
  				error = xlog_bread(log, rblk_no, bblks, dbp,
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
3009
  						   &offset);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3010
3011
  				if (error)
  					goto bread_err2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3012
3013
3014
  			} else {
  				/* This log record is split across the
  				 * physical end of log */
6ad5b3255   Christoph Hellwig   xfs: use bios dir...
3015
  				offset = dbp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3016
3017
3018
3019
3020
3021
3022
3023
3024
  				split_bblks = 0;
  				if (blk_no != log->l_logBBsize) {
  					/* some data is before the physical
  					 * end of log */
  					ASSERT(!wrapped_hblks);
  					ASSERT(blk_no <= INT_MAX);
  					split_bblks =
  						log->l_logBBsize - (int)blk_no;
  					ASSERT(split_bblks > 0);
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
3025
3026
3027
3028
  					error = xlog_bread(log, blk_no,
  							split_bblks, dbp,
  							&offset);
  					if (error)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3029
  						goto bread_err2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3030
  				}
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
3031

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
  				/*
  				 * Note: this black magic still works with
  				 * large sector sizes (non-512) only because:
  				 * - we increased the buffer size originally
  				 *   by 1 sector giving us enough extra space
  				 *   for the second read;
  				 * - the log start is guaranteed to be sector
  				 *   aligned;
  				 * - we read the log end (LR header start)
  				 *   _first_, then the log start (LR header end)
  				 *   - order is important.
  				 */
6ad5b3255   Christoph Hellwig   xfs: use bios dir...
3044
3045
  				error = xlog_bread_noalign(log, 0,
  						bblks - split_bblks,
44396476a   Dave Chinner   xfs: reset buffer...
3046
  						offset + BBTOB(split_bblks));
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
3047
3048
  				if (error)
  					goto bread_err2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3049
  			}
0e446be44   Christoph Hellwig   xfs: add CRC chec...
3050

9d94901f6   Brian Foster   xfs: refactor log...
3051
  			error = xlog_recover_process(log, rhash, rhead, offset,
12818d24d   Brian Foster   xfs: rework log r...
3052
  						     pass, &buffer_list);
0e446be44   Christoph Hellwig   xfs: add CRC chec...
3053
  			if (error)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3054
  				goto bread_err2;
d7f37692e   Brian Foster   xfs: return start...
3055

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3056
  			blk_no += bblks;
d7f37692e   Brian Foster   xfs: return start...
3057
  			rhead_blk = blk_no;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3058
3059
3060
3061
  		}
  
  		ASSERT(blk_no >= log->l_logBBsize);
  		blk_no -= log->l_logBBsize;
d7f37692e   Brian Foster   xfs: return start...
3062
  		rhead_blk = blk_no;
970fd3f04   Eric Sandeen   xfs: deduplicate ...
3063
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3064

970fd3f04   Eric Sandeen   xfs: deduplicate ...
3065
3066
3067
3068
3069
  	/* read first part of physical log */
  	while (blk_no < head_blk) {
  		error = xlog_bread(log, blk_no, hblks, hbp, &offset);
  		if (error)
  			goto bread_err2;
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
3070

970fd3f04   Eric Sandeen   xfs: deduplicate ...
3071
  		rhead = (xlog_rec_header_t *)offset;
f692d09e9   Gao Xiang   xfs: avoid LR buf...
3072
  		error = xlog_valid_rec_header(log, rhead, blk_no, h_size);
970fd3f04   Eric Sandeen   xfs: deduplicate ...
3073
3074
  		if (error)
  			goto bread_err2;
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
3075

970fd3f04   Eric Sandeen   xfs: deduplicate ...
3076
3077
3078
3079
3080
3081
  		/* blocks in data section */
  		bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
  		error = xlog_bread(log, blk_no+hblks, bblks, dbp,
  				   &offset);
  		if (error)
  			goto bread_err2;
076e6acb8   Christoph Hellwig   xfs: cleanup xlog...
3082

12818d24d   Brian Foster   xfs: rework log r...
3083
3084
  		error = xlog_recover_process(log, rhash, rhead, offset, pass,
  					     &buffer_list);
970fd3f04   Eric Sandeen   xfs: deduplicate ...
3085
3086
  		if (error)
  			goto bread_err2;
d7f37692e   Brian Foster   xfs: return start...
3087

970fd3f04   Eric Sandeen   xfs: deduplicate ...
3088
  		blk_no += bblks + hblks;
d7f37692e   Brian Foster   xfs: return start...
3089
  		rhead_blk = blk_no;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3090
3091
3092
  	}
  
   bread_err2:
6ad5b3255   Christoph Hellwig   xfs: use bios dir...
3093
  	kmem_free(dbp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3094
   bread_err1:
6ad5b3255   Christoph Hellwig   xfs: use bios dir...
3095
  	kmem_free(hbp);
d7f37692e   Brian Foster   xfs: return start...
3096

12818d24d   Brian Foster   xfs: rework log r...
3097
3098
3099
3100
3101
3102
  	/*
  	 * Submit buffers that have been added from the last record processed,
  	 * regardless of error status.
  	 */
  	if (!list_empty(&buffer_list))
  		error2 = xfs_buf_delwri_submit(&buffer_list);
d7f37692e   Brian Foster   xfs: return start...
3103
3104
  	if (error && first_bad)
  		*first_bad = rhead_blk;
39775431f   Brian Foster   xfs: free uncommi...
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
  	/*
  	 * Transactions are freed at commit time but transactions without commit
  	 * records on disk are never committed. Free any that may be left in the
  	 * hash table.
  	 */
  	for (i = 0; i < XLOG_RHASH_SIZE; i++) {
  		struct hlist_node	*tmp;
  		struct xlog_recover	*trans;
  
  		hlist_for_each_entry_safe(trans, tmp, &rhash[i], r_list)
  			xlog_recover_free_trans(trans);
  	}
12818d24d   Brian Foster   xfs: rework log r...
3117
  	return error ? error : error2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
  }
  
  /*
   * Do the recovery of the log.  We actually do this in two phases.
   * The two passes are necessary in order to implement the function
   * of cancelling a record written into the log.  The first pass
   * determines those things which have been cancelled, and the
   * second pass replays log items normally except for those which
   * have been cancelled.  The handling of the replay and cancellations
   * takes place in the log item type specific routines.
   *
   * The table of items which have cancel records in the log is allocated
   * and freed at this level, since only here do we know when all of
   * the log recovery has been completed.
   */
  STATIC int
  xlog_do_log_recovery(
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
3135
  	struct xlog	*log,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3136
3137
3138
  	xfs_daddr_t	head_blk,
  	xfs_daddr_t	tail_blk)
  {
d5689eaa0   Christoph Hellwig   xfs: use struct l...
3139
  	int		error, i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3140
3141
3142
3143
3144
3145
3146
  
  	ASSERT(head_blk != tail_blk);
  
  	/*
  	 * First do a pass to find all of the cancelled buf log items.
  	 * Store them in the buf_cancel_table for use in the second pass.
  	 */
d5689eaa0   Christoph Hellwig   xfs: use struct l...
3147
3148
  	log->l_buf_cancel_table = kmem_zalloc(XLOG_BC_TABLE_SIZE *
  						 sizeof(struct list_head),
707e0ddaf   Tetsuo Handa   fs: xfs: Remove K...
3149
  						 0);
d5689eaa0   Christoph Hellwig   xfs: use struct l...
3150
3151
  	for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
  		INIT_LIST_HEAD(&log->l_buf_cancel_table[i]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3152
  	error = xlog_do_recovery_pass(log, head_blk, tail_blk,
d7f37692e   Brian Foster   xfs: return start...
3153
  				      XLOG_RECOVER_PASS1, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3154
  	if (error != 0) {
f0e2d93c2   Denys Vlasenko   [XFS] Remove unus...
3155
  		kmem_free(log->l_buf_cancel_table);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3156
3157
3158
3159
3160
3161
3162
3163
  		log->l_buf_cancel_table = NULL;
  		return error;
  	}
  	/*
  	 * Then do a second pass to actually recover the items in the log.
  	 * When it is complete free the table of buf cancel items.
  	 */
  	error = xlog_do_recovery_pass(log, head_blk, tail_blk,
d7f37692e   Brian Foster   xfs: return start...
3164
  				      XLOG_RECOVER_PASS2, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3165
  #ifdef DEBUG
6d192a9b8   Tim Shimmin   [XFS] inode items...
3166
  	if (!error) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3167
3168
3169
  		int	i;
  
  		for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
d5689eaa0   Christoph Hellwig   xfs: use struct l...
3170
  			ASSERT(list_empty(&log->l_buf_cancel_table[i]));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3171
3172
  	}
  #endif	/* DEBUG */
f0e2d93c2   Denys Vlasenko   [XFS] Remove unus...
3173
  	kmem_free(log->l_buf_cancel_table);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
  	log->l_buf_cancel_table = NULL;
  
  	return error;
  }
  
  /*
   * Do the actual recovery
   */
  STATIC int
  xlog_do_recover(
b3f8e08ca   Christoph Hellwig   xfs: remove xfs_g...
3184
3185
3186
  	struct xlog		*log,
  	xfs_daddr_t		head_blk,
  	xfs_daddr_t		tail_blk)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3187
  {
b3f8e08ca   Christoph Hellwig   xfs: remove xfs_g...
3188
3189
3190
3191
  	struct xfs_mount	*mp = log->l_mp;
  	struct xfs_buf		*bp = mp->m_sb_bp;
  	struct xfs_sb		*sbp = &mp->m_sb;
  	int			error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3192

e67d3d424   Brian Foster   xfs: add log reco...
3193
  	trace_xfs_log_recover(log, head_blk, tail_blk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3194
3195
3196
3197
  	/*
  	 * First replay the images in the log.
  	 */
  	error = xlog_do_log_recovery(log, head_blk, tail_blk);
43ff2122e   Christoph Hellwig   xfs: on-stack del...
3198
  	if (error)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3199
  		return error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3200
3201
3202
3203
  
  	/*
  	 * If IO errors happened during recovery, bail out.
  	 */
b3f8e08ca   Christoph Hellwig   xfs: remove xfs_g...
3204
  	if (XFS_FORCED_SHUTDOWN(mp))
2451337dd   Dave Chinner   xfs: global error...
3205
  		return -EIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
  
  	/*
  	 * We now update the tail_lsn since much of the recovery has completed
  	 * and there may be space available to use.  If there were no extent
  	 * or iunlinks, we can free up the entire log and set the tail_lsn to
  	 * be the last_sync_lsn.  This was set in xlog_find_tail to be the
  	 * lsn of the last known good LR on disk.  If there are extent frees
  	 * or iunlinks they will have some entries in the AIL; so we look at
  	 * the AIL to determine how to set the tail_lsn.
  	 */
a798011c8   Dave Chinner   xfs: reinitialise...
3216
  	xlog_assign_tail_lsn(mp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3217
3218
  
  	/*
b3f8e08ca   Christoph Hellwig   xfs: remove xfs_g...
3219
3220
  	 * Now that we've finished replaying all buffer and inode updates,
  	 * re-read the superblock and reverify it.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3221
  	 */
b3f8e08ca   Christoph Hellwig   xfs: remove xfs_g...
3222
3223
  	xfs_buf_lock(bp);
  	xfs_buf_hold(bp);
26e328759   Christoph Hellwig   xfs: reuse _xfs_b...
3224
  	error = _xfs_buf_read(bp, XBF_READ);
d64e31a2f   David Chinner   [XFS] Ensure erro...
3225
  	if (error) {
a798011c8   Dave Chinner   xfs: reinitialise...
3226
  		if (!XFS_FORCED_SHUTDOWN(mp)) {
cdbcf82b8   Darrick J. Wong   xfs: fix xfs_buf_...
3227
  			xfs_buf_ioerror_alert(bp, __this_address);
595bff75d   Dave Chinner   xfs: introduce xf...
3228
3229
  			ASSERT(0);
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3230
3231
3232
3233
3234
  		xfs_buf_relse(bp);
  		return error;
  	}
  
  	/* Convert superblock from on-disk format */
3e6e8afd3   Christoph Hellwig   xfs: remove XFS_B...
3235
  	xfs_sb_from_disk(sbp, bp->b_addr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3236
  	xfs_buf_relse(bp);
a798011c8   Dave Chinner   xfs: reinitialise...
3237
3238
3239
3240
3241
3242
3243
  	/* re-initialise in-core superblock and geometry structures */
  	xfs_reinit_percpu_counters(mp);
  	error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
  	if (error) {
  		xfs_warn(mp, "Failed post-recovery per-ag init: %d", error);
  		return error;
  	}
525488520   Darrick J. Wong   xfs: rmap btree r...
3244
  	mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
5478eead8   Lachlan McIlroy   [XFS] Re-initiali...
3245

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
  	xlog_recover_check_summary(log);
  
  	/* Normal transactions can now occur */
  	log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
  	return 0;
  }
  
  /*
   * Perform recovery and re-initialize some log variables in xlog_find_tail.
   *
   * Return error or zero.
   */
  int
  xlog_recover(
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
3260
  	struct xlog	*log)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3261
3262
3263
3264
3265
  {
  	xfs_daddr_t	head_blk, tail_blk;
  	int		error;
  
  	/* find the tail of the log */
a45086e27   Brian Foster   xfs: validate met...
3266
3267
  	error = xlog_find_tail(log, &head_blk, &tail_blk);
  	if (error)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3268
  		return error;
a45086e27   Brian Foster   xfs: validate met...
3269
3270
3271
3272
3273
3274
3275
3276
  	/*
  	 * The superblock was read before the log was available and thus the LSN
  	 * could not be verified. Check the superblock LSN against the current
  	 * LSN now that it's known.
  	 */
  	if (xfs_sb_version_hascrc(&log->l_mp->m_sb) &&
  	    !xfs_log_check_lsn(log->l_mp, log->l_mp->m_sb.sb_lsn))
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
  	if (tail_blk != head_blk) {
  		/* There used to be a comment here:
  		 *
  		 * disallow recovery on read-only mounts.  note -- mount
  		 * checks for ENOSPC and turns it into an intelligent
  		 * error message.
  		 * ...but this is no longer true.  Now, unless you specify
  		 * NORECOVERY (in which case this function would never be
  		 * called), we just go ahead and recover.  We do this all
  		 * under the vfs layer, so we can get away with it unless
  		 * the device itself is read-only, in which case we fail.
  		 */
3a02ee182   Utako Kusaka   [XFS] Get rid of ...
3289
  		if ((error = xfs_dev_is_read_only(log->l_mp, "recovery"))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3290
3291
  			return error;
  		}
e721f504c   Dave Chinner   xfs: implement ex...
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
  		/*
  		 * Version 5 superblock log feature mask validation. We know the
  		 * log is dirty so check if there are any unknown log features
  		 * in what we need to recover. If there are unknown features
  		 * (e.g. unsupported transactions, then simply reject the
  		 * attempt at recovery before touching anything.
  		 */
  		if (XFS_SB_VERSION_NUM(&log->l_mp->m_sb) == XFS_SB_VERSION_5 &&
  		    xfs_sb_has_incompat_log_feature(&log->l_mp->m_sb,
  					XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)) {
  			xfs_warn(log->l_mp,
f41febd2e   Joe Perches   xfs: Use consiste...
3303
  "Superblock has unknown incompatible log features (0x%x) enabled.",
e721f504c   Dave Chinner   xfs: implement ex...
3304
3305
  				(log->l_mp->m_sb.sb_features_log_incompat &
  					XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN));
f41febd2e   Joe Perches   xfs: Use consiste...
3306
3307
3308
3309
  			xfs_warn(log->l_mp,
  "The log can not be fully and/or safely recovered by this kernel.");
  			xfs_warn(log->l_mp,
  "Please recover the log on a kernel that supports the unknown features.");
2451337dd   Dave Chinner   xfs: global error...
3310
  			return -EINVAL;
e721f504c   Dave Chinner   xfs: implement ex...
3311
  		}
2e2271787   Brian Foster   xfs: export log_r...
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
  		/*
  		 * Delay log recovery if the debug hook is set. This is debug
  		 * instrumention to coordinate simulation of I/O failures with
  		 * log recovery.
  		 */
  		if (xfs_globals.log_recovery_delay) {
  			xfs_notice(log->l_mp,
  				"Delaying log recovery for %d seconds.",
  				xfs_globals.log_recovery_delay);
  			msleep(xfs_globals.log_recovery_delay * 1000);
  		}
a0fa2b679   Dave Chinner   xfs: Convert xlog...
3323
3324
3325
  		xfs_notice(log->l_mp, "Starting recovery (logdev: %s)",
  				log->l_mp->m_logname ? log->l_mp->m_logname
  						     : "internal");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
  
  		error = xlog_do_recover(log, head_blk, tail_blk);
  		log->l_flags |= XLOG_RECOVERY_NEEDED;
  	}
  	return error;
  }
  
  /*
   * In the first part of recovery we replay inodes and buffers and build
   * up the list of extent free items which need to be processed.  Here
   * we process the extent free items and clean up the on disk unlinked
   * inode lists.  This is separated from the first part of recovery so
   * that the root and real-time bitmap inodes can be read in from disk in
   * between the two stages.  This is necessary so that we can free space
   * in the real-time portion of the file system.
   */
  int
  xlog_recover_finish(
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
3344
  	struct xlog	*log)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
  {
  	/*
  	 * Now we're ready to do the transactions needed for the
  	 * rest of recovery.  Start with completing all the extent
  	 * free intent records and then process the unlinked inode
  	 * lists.  At this point, we essentially run in normal mode
  	 * except that we're still performing recovery actions
  	 * rather than accepting new requests.
  	 */
  	if (log->l_flags & XLOG_RECOVERY_NEEDED) {
3c1e2bbe5   David Chinner   [XFS] Propagate x...
3355
  		int	error;
dc42375d5   Darrick J. Wong   xfs: refactor red...
3356
  		error = xlog_recover_process_intents(log);
3c1e2bbe5   David Chinner   [XFS] Propagate x...
3357
  		if (error) {
2e76f188f   Darrick J. Wong   xfs: cancel inten...
3358
3359
3360
3361
3362
3363
3364
3365
  			/*
  			 * Cancel all the unprocessed intent items now so that
  			 * we don't leave them pinned in the AIL.  This can
  			 * cause the AIL to livelock on the pinned item if
  			 * anyone tries to push the AIL (inode reclaim does
  			 * this) before we get around to xfs_log_mount_cancel.
  			 */
  			xlog_recover_cancel_intents(log);
dc42375d5   Darrick J. Wong   xfs: refactor red...
3366
  			xfs_alert(log->l_mp, "Failed to recover intents");
3c1e2bbe5   David Chinner   [XFS] Propagate x...
3367
3368
  			return error;
  		}
9e88b5d86   Darrick J. Wong   xfs: log rmap int...
3369

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3370
  		/*
dc42375d5   Darrick J. Wong   xfs: refactor red...
3371
  		 * Sync the log to get all the intents out of the AIL.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3372
3373
  		 * This isn't absolutely necessary, but it helps in
  		 * case the unlink transactions would have problems
dc42375d5   Darrick J. Wong   xfs: refactor red...
3374
  		 * pushing the intents out of the way.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3375
  		 */
a14a348bf   Christoph Hellwig   xfs: cleanup up x...
3376
  		xfs_log_force(log->l_mp, XFS_LOG_SYNC);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3377

4249023a5   Christoph Hellwig   [XFS] cleanup xfs...
3378
  		xlog_recover_process_iunlinks(log);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3379
3380
  
  		xlog_recover_check_summary(log);
a0fa2b679   Dave Chinner   xfs: Convert xlog...
3381
3382
3383
  		xfs_notice(log->l_mp, "Ending recovery (logdev: %s)",
  				log->l_mp->m_logname ? log->l_mp->m_logname
  						     : "internal");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3384
3385
  		log->l_flags &= ~XLOG_RECOVERY_NEEDED;
  	} else {
a0fa2b679   Dave Chinner   xfs: Convert xlog...
3386
  		xfs_info(log->l_mp, "Ending clean mount");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3387
3388
3389
  	}
  	return 0;
  }
a7a9250e1   Hariprasad Kelam   fs: xfs: xfs_log:...
3390
  void
f0b2efad1   Brian Foster   xfs: don't leave ...
3391
3392
3393
  xlog_recover_cancel(
  	struct xlog	*log)
  {
f0b2efad1   Brian Foster   xfs: don't leave ...
3394
  	if (log->l_flags & XLOG_RECOVERY_NEEDED)
a7a9250e1   Hariprasad Kelam   fs: xfs: xfs_log:...
3395
  		xlog_recover_cancel_intents(log);
f0b2efad1   Brian Foster   xfs: don't leave ...
3396
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3397
3398
3399
3400
3401
3402
  
  #if defined(DEBUG)
  /*
   * Read all of the agf and agi counters and check that they
   * are consistent with the superblock counters.
   */
e89fbb5ee   Christoph Hellwig   xfs: mark xlog_re...
3403
  STATIC void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3404
  xlog_recover_check_summary(
9a8d2fdbb   Mark Tinguely   xfs: remove xlog_...
3405
  	struct xlog	*log)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3406
3407
  {
  	xfs_mount_t	*mp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3408
3409
  	xfs_buf_t	*agfbp;
  	xfs_buf_t	*agibp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3410
  	xfs_agnumber_t	agno;
c8ce540db   Darrick J. Wong   xfs: remove doubl...
3411
3412
3413
  	uint64_t	freeblks;
  	uint64_t	itotal;
  	uint64_t	ifree;
5e1be0fb1   Christoph Hellwig   [XFS] factor out ...
3414
  	int		error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3415
3416
3417
3418
3419
3420
3421
  
  	mp = log->l_mp;
  
  	freeblks = 0LL;
  	itotal = 0LL;
  	ifree = 0LL;
  	for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
4805621a3   Christoph Hellwig   [XFS] factor out ...
3422
3423
  		error = xfs_read_agf(mp, NULL, agno, 0, &agfbp);
  		if (error) {
a0fa2b679   Dave Chinner   xfs: Convert xlog...
3424
3425
  			xfs_alert(mp, "%s agf read failed agno %d error %d",
  						__func__, agno, error);
4805621a3   Christoph Hellwig   [XFS] factor out ...
3426
  		} else {
9798f615a   Christoph Hellwig   xfs: remove XFS_B...
3427
  			struct xfs_agf	*agfp = agfbp->b_addr;
4805621a3   Christoph Hellwig   [XFS] factor out ...
3428
3429
3430
  			freeblks += be32_to_cpu(agfp->agf_freeblks) +
  				    be32_to_cpu(agfp->agf_flcount);
  			xfs_buf_relse(agfbp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3431
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3432

5e1be0fb1   Christoph Hellwig   [XFS] factor out ...
3433
  		error = xfs_read_agi(mp, NULL, agno, &agibp);
a0fa2b679   Dave Chinner   xfs: Convert xlog...
3434
3435
3436
3437
  		if (error) {
  			xfs_alert(mp, "%s agi read failed agno %d error %d",
  						__func__, agno, error);
  		} else {
370c782b9   Christoph Hellwig   xfs: remove XFS_B...
3438
  			struct xfs_agi	*agi = agibp->b_addr;
16259e7d9   Christoph Hellwig   [XFS] Endianess a...
3439

5e1be0fb1   Christoph Hellwig   [XFS] factor out ...
3440
3441
3442
3443
  			itotal += be32_to_cpu(agi->agi_count);
  			ifree += be32_to_cpu(agi->agi_freecount);
  			xfs_buf_relse(agibp);
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3444
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3445
3446
  }
  #endif /* DEBUG */