Blame view

fs/gfs2/recovery.c 14.1 KB
b3b94faa5   David Teigland   [GFS2] The core o...
1
2
  /*
   * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3a8a9a103   Steven Whitehouse   [GFS2] Update cop...
3
   * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
b3b94faa5   David Teigland   [GFS2] The core o...
4
5
6
   *
   * This copyrighted material is made available to anyone wishing to use,
   * modify, copy, or redistribute it subject to the terms and conditions
e9fc2aa09   Steven Whitehouse   [GFS2] Update cop...
7
   * of the GNU General Public License version 2.
b3b94faa5   David Teigland   [GFS2] The core o...
8
   */
1c2ea8a2c   David Howells   SLOW_WORK: Fix GF...
9
  #include <linux/module.h>
b3b94faa5   David Teigland   [GFS2] The core o...
10
11
12
13
  #include <linux/slab.h>
  #include <linux/spinlock.h>
  #include <linux/completion.h>
  #include <linux/buffer_head.h>
5c676f6d3   Steven Whitehouse   [GFS2] Macros rem...
14
  #include <linux/gfs2_ondisk.h>
71b86f562   Steven Whitehouse   [GFS2] Further up...
15
  #include <linux/crc32.h>
b3b94faa5   David Teigland   [GFS2] The core o...
16
17
  
  #include "gfs2.h"
5c676f6d3   Steven Whitehouse   [GFS2] Macros rem...
18
  #include "incore.h"
b3b94faa5   David Teigland   [GFS2] The core o...
19
20
21
  #include "bmap.h"
  #include "glock.h"
  #include "glops.h"
b3b94faa5   David Teigland   [GFS2] The core o...
22
23
24
25
  #include "lops.h"
  #include "meta_io.h"
  #include "recovery.h"
  #include "super.h"
5c676f6d3   Steven Whitehouse   [GFS2] Macros rem...
26
  #include "util.h"
71b86f562   Steven Whitehouse   [GFS2] Further up...
27
  #include "dir.h"
b3b94faa5   David Teigland   [GFS2] The core o...
28

6ecd7c2dd   Tejun Heo   gfs2: use workque...
29
  struct workqueue_struct *gfs_recovery_wq;
b3b94faa5   David Teigland   [GFS2] The core o...
30
31
32
  int gfs2_replay_read_block(struct gfs2_jdesc *jd, unsigned int blk,
  			   struct buffer_head **bh)
  {
feaa7bba0   Steven Whitehouse   [GFS2] Fix unlink...
33
  	struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
5c676f6d3   Steven Whitehouse   [GFS2] Macros rem...
34
  	struct gfs2_glock *gl = ip->i_gl;
b3b94faa5   David Teigland   [GFS2] The core o...
35
  	int new = 0;
cd915493f   Steven Whitehouse   [GFS2] Change all...
36
37
  	u64 dblock;
  	u32 extlen;
b3b94faa5   David Teigland   [GFS2] The core o...
38
  	int error;
feaa7bba0   Steven Whitehouse   [GFS2] Fix unlink...
39
  	error = gfs2_extent_map(&ip->i_inode, blk, &new, &dblock, &extlen);
b3b94faa5   David Teigland   [GFS2] The core o...
40
41
42
  	if (error)
  		return error;
  	if (!dblock) {
5c676f6d3   Steven Whitehouse   [GFS2] Macros rem...
43
  		gfs2_consist_inode(ip);
b3b94faa5   David Teigland   [GFS2] The core o...
44
45
  		return -EIO;
  	}
7276b3b0c   Steven Whitehouse   [GFS2] Tidy up me...
46
  	*bh = gfs2_meta_ra(gl, dblock, extlen);
b3b94faa5   David Teigland   [GFS2] The core o...
47
48
49
  
  	return error;
  }
a17d758b6   Bob Peterson   GFS2: Move recove...
50
  int gfs2_revoke_add(struct gfs2_jdesc *jd, u64 blkno, unsigned int where)
b3b94faa5   David Teigland   [GFS2] The core o...
51
  {
a17d758b6   Bob Peterson   GFS2: Move recove...
52
  	struct list_head *head = &jd->jd_revoke_list;
b3b94faa5   David Teigland   [GFS2] The core o...
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  	struct gfs2_revoke_replay *rr;
  	int found = 0;
  
  	list_for_each_entry(rr, head, rr_list) {
  		if (rr->rr_blkno == blkno) {
  			found = 1;
  			break;
  		}
  	}
  
  	if (found) {
  		rr->rr_where = where;
  		return 0;
  	}
16c5f06f1   Josef Bacik   [GFS2] fix GFP_KE...
67
  	rr = kmalloc(sizeof(struct gfs2_revoke_replay), GFP_NOFS);
b3b94faa5   David Teigland   [GFS2] The core o...
68
69
70
71
72
73
74
75
76
  	if (!rr)
  		return -ENOMEM;
  
  	rr->rr_blkno = blkno;
  	rr->rr_where = where;
  	list_add(&rr->rr_list, head);
  
  	return 1;
  }
a17d758b6   Bob Peterson   GFS2: Move recove...
77
  int gfs2_revoke_check(struct gfs2_jdesc *jd, u64 blkno, unsigned int where)
b3b94faa5   David Teigland   [GFS2] The core o...
78
79
80
81
  {
  	struct gfs2_revoke_replay *rr;
  	int wrap, a, b, revoke;
  	int found = 0;
a17d758b6   Bob Peterson   GFS2: Move recove...
82
  	list_for_each_entry(rr, &jd->jd_revoke_list, rr_list) {
b3b94faa5   David Teigland   [GFS2] The core o...
83
84
85
86
87
88
89
90
  		if (rr->rr_blkno == blkno) {
  			found = 1;
  			break;
  		}
  	}
  
  	if (!found)
  		return 0;
a17d758b6   Bob Peterson   GFS2: Move recove...
91
92
  	wrap = (rr->rr_where < jd->jd_replay_tail);
  	a = (jd->jd_replay_tail < where);
b3b94faa5   David Teigland   [GFS2] The core o...
93
94
95
96
97
  	b = (where < rr->rr_where);
  	revoke = (wrap) ? (a || b) : (a && b);
  
  	return revoke;
  }
a17d758b6   Bob Peterson   GFS2: Move recove...
98
  void gfs2_revoke_clean(struct gfs2_jdesc *jd)
b3b94faa5   David Teigland   [GFS2] The core o...
99
  {
a17d758b6   Bob Peterson   GFS2: Move recove...
100
  	struct list_head *head = &jd->jd_revoke_list;
b3b94faa5   David Teigland   [GFS2] The core o...
101
102
103
104
105
106
107
108
  	struct gfs2_revoke_replay *rr;
  
  	while (!list_empty(head)) {
  		rr = list_entry(head->next, struct gfs2_revoke_replay, rr_list);
  		list_del(&rr->rr_list);
  		kfree(rr);
  	}
  }
bb8d8a6f5   Steven Whitehouse   [GFS2] Fix sign p...
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  static int gfs2_log_header_in(struct gfs2_log_header_host *lh, const void *buf)
  {
  	const struct gfs2_log_header *str = buf;
  
  	if (str->lh_header.mh_magic != cpu_to_be32(GFS2_MAGIC) ||
  	    str->lh_header.mh_type != cpu_to_be32(GFS2_METATYPE_LH))
  		return 1;
  
  	lh->lh_sequence = be64_to_cpu(str->lh_sequence);
  	lh->lh_flags = be32_to_cpu(str->lh_flags);
  	lh->lh_tail = be32_to_cpu(str->lh_tail);
  	lh->lh_blkno = be32_to_cpu(str->lh_blkno);
  	lh->lh_hash = be32_to_cpu(str->lh_hash);
  	return 0;
  }
b3b94faa5   David Teigland   [GFS2] The core o...
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  /**
   * get_log_header - read the log header for a given segment
   * @jd: the journal
   * @blk: the block to look at
   * @lh: the log header to return
   *
   * Read the log header for a given segement in a given journal.  Do a few
   * sanity checks on it.
   *
   * Returns: 0 on success,
   *          1 if the header was invalid or incomplete,
   *          errno on error
   */
  
  static int get_log_header(struct gfs2_jdesc *jd, unsigned int blk,
551676226   Al Viro   [GFS2] split and ...
139
  			  struct gfs2_log_header_host *head)
b3b94faa5   David Teigland   [GFS2] The core o...
140
141
  {
  	struct buffer_head *bh;
f5a8cd020   akpm@linux-foundation.org   [GFS2] fs/gfs2/re...
142
  	struct gfs2_log_header_host uninitialized_var(lh);
887bc5d00   Steven Whitehouse   [GFS2] Fix indent...
143
  	const u32 nothing = 0;
cd915493f   Steven Whitehouse   [GFS2] Change all...
144
  	u32 hash;
b3b94faa5   David Teigland   [GFS2] The core o...
145
146
147
148
149
  	int error;
  
  	error = gfs2_replay_read_block(jd, blk, &bh);
  	if (error)
  		return error;
2a2c98247   Steven Whitehouse   [GFS2] Fix crc32 ...
150
151
152
153
  	hash = crc32_le((u32)~0, bh->b_data, sizeof(struct gfs2_log_header) -
  					     sizeof(u32));
  	hash = crc32_le(hash, (unsigned char const *)&nothing, sizeof(nothing));
  	hash ^= (u32)~0;
bb8d8a6f5   Steven Whitehouse   [GFS2] Fix sign p...
154
  	error = gfs2_log_header_in(&lh, bh->b_data);
b3b94faa5   David Teigland   [GFS2] The core o...
155
  	brelse(bh);
bb8d8a6f5   Steven Whitehouse   [GFS2] Fix sign p...
156
  	if (error || lh.lh_blkno != blk || lh.lh_hash != hash)
b3b94faa5   David Teigland   [GFS2] The core o...
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  		return 1;
  
  	*head = lh;
  
  	return 0;
  }
  
  /**
   * find_good_lh - find a good log header
   * @jd: the journal
   * @blk: the segment to start searching from
   * @lh: the log header to fill in
   * @forward: if true search forward in the log, else search backward
   *
   * Call get_log_header() to get a log header for a segment, but if the
   * segment is bad, either scan forward or backward until we find a good one.
   *
   * Returns: errno
   */
  
  static int find_good_lh(struct gfs2_jdesc *jd, unsigned int *blk,
551676226   Al Viro   [GFS2] split and ...
178
  			struct gfs2_log_header_host *head)
b3b94faa5   David Teigland   [GFS2] The core o...
179
180
181
182
183
184
185
186
187
188
189
190
191
  {
  	unsigned int orig_blk = *blk;
  	int error;
  
  	for (;;) {
  		error = get_log_header(jd, *blk, head);
  		if (error <= 0)
  			return error;
  
  		if (++*blk == jd->jd_blocks)
  			*blk = 0;
  
  		if (*blk == orig_blk) {
feaa7bba0   Steven Whitehouse   [GFS2] Fix unlink...
192
  			gfs2_consist_inode(GFS2_I(jd->jd_inode));
b3b94faa5   David Teigland   [GFS2] The core o...
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
  			return -EIO;
  		}
  	}
  }
  
  /**
   * jhead_scan - make sure we've found the head of the log
   * @jd: the journal
   * @head: this is filled in with the log descriptor of the head
   *
   * At this point, seg and lh should be either the head of the log or just
   * before.  Scan forward until we find the head.
   *
   * Returns: errno
   */
551676226   Al Viro   [GFS2] split and ...
208
  static int jhead_scan(struct gfs2_jdesc *jd, struct gfs2_log_header_host *head)
b3b94faa5   David Teigland   [GFS2] The core o...
209
210
  {
  	unsigned int blk = head->lh_blkno;
551676226   Al Viro   [GFS2] split and ...
211
  	struct gfs2_log_header_host lh;
b3b94faa5   David Teigland   [GFS2] The core o...
212
213
214
215
216
217
218
219
220
221
222
223
224
  	int error;
  
  	for (;;) {
  		if (++blk == jd->jd_blocks)
  			blk = 0;
  
  		error = get_log_header(jd, blk, &lh);
  		if (error < 0)
  			return error;
  		if (error == 1)
  			continue;
  
  		if (lh.lh_sequence == head->lh_sequence) {
feaa7bba0   Steven Whitehouse   [GFS2] Fix unlink...
225
  			gfs2_consist_inode(GFS2_I(jd->jd_inode));
b3b94faa5   David Teigland   [GFS2] The core o...
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
  			return -EIO;
  		}
  		if (lh.lh_sequence < head->lh_sequence)
  			break;
  
  		*head = lh;
  	}
  
  	return 0;
  }
  
  /**
   * gfs2_find_jhead - find the head of a log
   * @jd: the journal
   * @head: the log descriptor for the head of the log is returned here
   *
   * Do a binary search of a journal and find the valid log entry with the
   * highest sequence number.  (i.e. the log head)
   *
   * Returns: errno
   */
551676226   Al Viro   [GFS2] split and ...
247
  int gfs2_find_jhead(struct gfs2_jdesc *jd, struct gfs2_log_header_host *head)
b3b94faa5   David Teigland   [GFS2] The core o...
248
  {
551676226   Al Viro   [GFS2] split and ...
249
  	struct gfs2_log_header_host lh_1, lh_m;
cd915493f   Steven Whitehouse   [GFS2] Change all...
250
  	u32 blk_1, blk_2, blk_m;
b3b94faa5   David Teigland   [GFS2] The core o...
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
  	int error;
  
  	blk_1 = 0;
  	blk_2 = jd->jd_blocks - 1;
  
  	for (;;) {
  		blk_m = (blk_1 + blk_2) / 2;
  
  		error = find_good_lh(jd, &blk_1, &lh_1);
  		if (error)
  			return error;
  
  		error = find_good_lh(jd, &blk_m, &lh_m);
  		if (error)
  			return error;
  
  		if (blk_1 == blk_m || blk_m == blk_2)
  			break;
  
  		if (lh_1.lh_sequence <= lh_m.lh_sequence)
  			blk_1 = blk_m;
  		else
  			blk_2 = blk_m;
  	}
  
  	error = jhead_scan(jd, &lh_1);
  	if (error)
  		return error;
  
  	*head = lh_1;
  
  	return error;
  }
  
  /**
   * foreach_descriptor - go through the active part of the log
   * @jd: the journal
   * @start: the first log header in the active region
   * @end: the last log header (don't process the contents of this entry))
   *
   * Call a given function once for every log descriptor in the active
   * portion of the log.
   *
   * Returns: errno
   */
  
  static int foreach_descriptor(struct gfs2_jdesc *jd, unsigned int start,
  			      unsigned int end, int pass)
  {
feaa7bba0   Steven Whitehouse   [GFS2] Fix unlink...
300
  	struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
b3b94faa5   David Teigland   [GFS2] The core o...
301
302
303
304
305
306
  	struct buffer_head *bh;
  	struct gfs2_log_descriptor *ld;
  	int error = 0;
  	u32 length;
  	__be64 *ptr;
  	unsigned int offset = sizeof(struct gfs2_log_descriptor);
a67cdbd45   Steven Whitehouse   [GFS2] Style chan...
307
308
  	offset += sizeof(__be64) - 1;
  	offset &= ~(sizeof(__be64) - 1);
b3b94faa5   David Teigland   [GFS2] The core o...
309
310
311
312
313
314
315
316
317
318
319
  
  	while (start != end) {
  		error = gfs2_replay_read_block(jd, start, &bh);
  		if (error)
  			return error;
  		if (gfs2_meta_check(sdp, bh)) {
  			brelse(bh);
  			return -EIO;
  		}
  		ld = (struct gfs2_log_descriptor *)bh->b_data;
  		length = be32_to_cpu(ld->ld_length);
e3167ded1   Steven Whitehouse   [GFS] Fix bug in ...
320
  		if (be32_to_cpu(ld->ld_header.mh_type) == GFS2_METATYPE_LH) {
551676226   Al Viro   [GFS2] split and ...
321
  			struct gfs2_log_header_host lh;
b3b94faa5   David Teigland   [GFS2] The core o...
322
323
  			error = get_log_header(jd, start, &lh);
  			if (!error) {
e1cb6be9e   Bob Peterson   GFS2: Fix gfs2_re...
324
  				gfs2_replay_incr_blk(jd, &start);
887218778   Russell Cattelan   [GFS2] Fix a coup...
325
  				brelse(bh);
b3b94faa5   David Teigland   [GFS2] The core o...
326
327
328
  				continue;
  			}
  			if (error == 1) {
feaa7bba0   Steven Whitehouse   [GFS2] Fix unlink...
329
  				gfs2_consist_inode(GFS2_I(jd->jd_inode));
b3b94faa5   David Teigland   [GFS2] The core o...
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
  				error = -EIO;
  			}
  			brelse(bh);
  			return error;
  		} else if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LD)) {
  			brelse(bh);
  			return -EIO;
  		}
  		ptr = (__be64 *)(bh->b_data + offset);
  		error = lops_scan_elements(jd, start, ld, ptr, pass);
  		if (error) {
  			brelse(bh);
  			return error;
  		}
  
  		while (length--)
e1cb6be9e   Bob Peterson   GFS2: Fix gfs2_re...
346
  			gfs2_replay_incr_blk(jd, &start);
b3b94faa5   David Teigland   [GFS2] The core o...
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
  
  		brelse(bh);
  	}
  
  	return 0;
  }
  
  /**
   * clean_journal - mark a dirty journal as being clean
   * @sdp: the filesystem
   * @jd: the journal
   * @gl: the journal's glock
   * @head: the head journal to start from
   *
   * Returns: errno
   */
551676226   Al Viro   [GFS2] split and ...
363
  static int clean_journal(struct gfs2_jdesc *jd, struct gfs2_log_header_host *head)
b3b94faa5   David Teigland   [GFS2] The core o...
364
  {
feaa7bba0   Steven Whitehouse   [GFS2] Fix unlink...
365
366
  	struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  	struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
b3b94faa5   David Teigland   [GFS2] The core o...
367
  	unsigned int lblock;
b3b94faa5   David Teigland   [GFS2] The core o...
368
  	struct gfs2_log_header *lh;
cd915493f   Steven Whitehouse   [GFS2] Change all...
369
  	u32 hash;
b3b94faa5   David Teigland   [GFS2] The core o...
370
371
  	struct buffer_head *bh;
  	int error;
23591256d   Steven Whitehouse   [GFS2] Fix bmap t...
372
  	struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
fd88de569   Steven Whitehouse   [GFS2] Readpages ...
373

b3b94faa5   David Teigland   [GFS2] The core o...
374
  	lblock = head->lh_blkno;
e1cb6be9e   Bob Peterson   GFS2: Fix gfs2_re...
375
  	gfs2_replay_incr_blk(jd, &lblock);
23591256d   Steven Whitehouse   [GFS2] Fix bmap t...
376
  	bh_map.b_size = 1 << ip->i_inode.i_blkbits;
e9e1ef2b6   Bob Peterson   [GFS2] Remove fun...
377
  	error = gfs2_block_map(&ip->i_inode, lblock, &bh_map, 0);
b3b94faa5   David Teigland   [GFS2] The core o...
378
379
  	if (error)
  		return error;
7a6bbacbb   Steven Whitehouse   [GFS2] Map multip...
380
  	if (!bh_map.b_blocknr) {
b3b94faa5   David Teigland   [GFS2] The core o...
381
382
383
  		gfs2_consist_inode(ip);
  		return -EIO;
  	}
7a6bbacbb   Steven Whitehouse   [GFS2] Map multip...
384
  	bh = sb_getblk(sdp->sd_vfs, bh_map.b_blocknr);
b3b94faa5   David Teigland   [GFS2] The core o...
385
386
387
388
389
390
391
392
393
  	lock_buffer(bh);
  	memset(bh->b_data, 0, bh->b_size);
  	set_buffer_uptodate(bh);
  	clear_buffer_dirty(bh);
  	unlock_buffer(bh);
  
  	lh = (struct gfs2_log_header *)bh->b_data;
  	memset(lh, 0, sizeof(struct gfs2_log_header));
  	lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
887218778   Russell Cattelan   [GFS2] Fix a coup...
394
  	lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
0ab7d13fc   Steven Whitehouse   GFS2: Tag all met...
395
  	lh->lh_header.__pad0 = cpu_to_be64(0);
e3167ded1   Steven Whitehouse   [GFS] Fix bug in ...
396
  	lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
0ab7d13fc   Steven Whitehouse   GFS2: Tag all met...
397
  	lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
b3b94faa5   David Teigland   [GFS2] The core o...
398
399
400
401
402
403
404
405
406
407
408
409
410
  	lh->lh_sequence = cpu_to_be64(head->lh_sequence + 1);
  	lh->lh_flags = cpu_to_be32(GFS2_LOG_HEAD_UNMOUNT);
  	lh->lh_blkno = cpu_to_be32(lblock);
  	hash = gfs2_disk_hash((const char *)lh, sizeof(struct gfs2_log_header));
  	lh->lh_hash = cpu_to_be32(hash);
  
  	set_buffer_dirty(bh);
  	if (sync_dirty_buffer(bh))
  		gfs2_io_error_bh(sdp, bh);
  	brelse(bh);
  
  	return error;
  }
da755fdb4   Steven Whitehouse   [GFS2] Remove lm....
411

f057f6cdf   Steven Whitehouse   GFS2: Merge lock_...
412
413
  static void gfs2_recovery_done(struct gfs2_sbd *sdp, unsigned int jid,
                                 unsigned int message)
da755fdb4   Steven Whitehouse   [GFS2] Remove lm....
414
  {
f057f6cdf   Steven Whitehouse   GFS2: Merge lock_...
415
416
417
418
  	char env_jid[20];
  	char env_status[20];
  	char *envp[] = { env_jid, env_status, NULL };
  	struct lm_lockstruct *ls = &sdp->sd_lockstruct;
e0c2a9aa1   David Teigland   GFS2: dlm based r...
419

f057f6cdf   Steven Whitehouse   GFS2: Merge lock_...
420
421
          ls->ls_recover_jid_done = jid;
          ls->ls_recover_jid_status = message;
3566c9647   alex chen   GFS2: fix sprintf...
422
  	sprintf(env_jid, "JID=%u", jid);
f057f6cdf   Steven Whitehouse   GFS2: Merge lock_...
423
424
425
  	sprintf(env_status, "RECOVERY=%s",
  		message == LM_RD_SUCCESS ? "Done" : "Failed");
          kobject_uevent_env(&sdp->sd_kobj, KOBJ_CHANGE, envp);
e0c2a9aa1   David Teigland   GFS2: dlm based r...
426
427
428
  
  	if (sdp->sd_lockstruct.ls_ops->lm_recovery_result)
  		sdp->sd_lockstruct.ls_ops->lm_recovery_result(sdp, jid, message);
da755fdb4   Steven Whitehouse   [GFS2] Remove lm....
429
  }
6ecd7c2dd   Tejun Heo   gfs2: use workque...
430
  void gfs2_recover_func(struct work_struct *work)
b3b94faa5   David Teigland   [GFS2] The core o...
431
  {
fe64d517d   Steven Whitehouse   GFS2: Umount reco...
432
  	struct gfs2_jdesc *jd = container_of(work, struct gfs2_jdesc, jd_work);
feaa7bba0   Steven Whitehouse   [GFS2] Fix unlink...
433
434
  	struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  	struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
551676226   Al Viro   [GFS2] split and ...
435
  	struct gfs2_log_header_host head;
24972557b   Benjamin Marzinski   GFS2: remove tran...
436
  	struct gfs2_holder j_gh, ji_gh, thaw_gh;
b3b94faa5   David Teigland   [GFS2] The core o...
437
438
439
440
  	unsigned long t;
  	int ro = 0;
  	unsigned int pass;
  	int error;
c741c4551   Steven Whitehouse   GFS2: Fix spectat...
441
  	int jlocked = 0;
b3b94faa5   David Teigland   [GFS2] The core o...
442

d0795f912   Steven Whitehouse   GFS2: Fix journal...
443
444
  	if (sdp->sd_args.ar_spectator ||
  	    (jd->jd_jid != sdp->sd_lockstruct.ls_jid)) {
5965b1f47   Steven Whitehouse   [GFS2] Don't do r...
445
446
447
  		fs_info(sdp, "jid=%u: Trying to acquire journal lock...
  ",
  			jd->jd_jid);
c741c4551   Steven Whitehouse   GFS2: Fix spectat...
448
  		jlocked = 1;
c78bad11f   Joe Perches   fs/: Spelling fixes
449
  		/* Acquire the journal lock so we can do recovery */
b3b94faa5   David Teigland   [GFS2] The core o...
450

5965b1f47   Steven Whitehouse   [GFS2] Don't do r...
451
452
453
454
455
456
457
  		error = gfs2_glock_nq_num(sdp, jd->jd_jid, &gfs2_journal_glops,
  					  LM_ST_EXCLUSIVE,
  					  LM_FLAG_NOEXP | LM_FLAG_TRY | GL_NOCACHE,
  					  &j_gh);
  		switch (error) {
  		case 0:
  			break;
907b9bceb   Steven Whitehouse   [GFS2/DLM] Fix tr...
458

5965b1f47   Steven Whitehouse   [GFS2] Don't do r...
459
460
461
462
  		case GLR_TRYFAILED:
  			fs_info(sdp, "jid=%u: Busy
  ", jd->jd_jid);
  			error = 0;
907b9bceb   Steven Whitehouse   [GFS2/DLM] Fix tr...
463

5965b1f47   Steven Whitehouse   [GFS2] Don't do r...
464
465
466
  		default:
  			goto fail;
  		};
b3b94faa5   David Teigland   [GFS2] The core o...
467

5965b1f47   Steven Whitehouse   [GFS2] Don't do r...
468
  		error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED,
75be73a82   Bob Peterson   [GFS2] Ensure jou...
469
  					   LM_FLAG_NOEXP | GL_NOCACHE, &ji_gh);
5965b1f47   Steven Whitehouse   [GFS2] Don't do r...
470
471
472
473
474
475
  		if (error)
  			goto fail_gunlock_j;
  	} else {
  		fs_info(sdp, "jid=%u, already locked for use
  ", jd->jd_jid);
  	}
b3b94faa5   David Teigland   [GFS2] The core o...
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
  
  	fs_info(sdp, "jid=%u: Looking at journal...
  ", jd->jd_jid);
  
  	error = gfs2_jdesc_check(jd);
  	if (error)
  		goto fail_gunlock_ji;
  
  	error = gfs2_find_jhead(jd, &head);
  	if (error)
  		goto fail_gunlock_ji;
  
  	if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
  		fs_info(sdp, "jid=%u: Acquiring the transaction lock...
  ",
  			jd->jd_jid);
  
  		t = jiffies;
24972557b   Benjamin Marzinski   GFS2: remove tran...
494
  		/* Acquire a shared hold on the freeze lock */
b3b94faa5   David Teigland   [GFS2] The core o...
495

24972557b   Benjamin Marzinski   GFS2: remove tran...
496
497
498
  		error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED,
  					   LM_FLAG_NOEXP | LM_FLAG_PRIORITY,
  					   &thaw_gh);
b3b94faa5   David Teigland   [GFS2] The core o...
499
500
  		if (error)
  			goto fail_gunlock_ji;
e8ca5cc57   David Teigland   GFS2: let spectat...
501
502
503
  		if (test_bit(SDF_RORECOVERY, &sdp->sd_flags)) {
  			ro = 1;
  		} else if (test_bit(SDF_JOURNAL_CHECKED, &sdp->sd_flags)) {
b3b94faa5   David Teigland   [GFS2] The core o...
504
505
506
  			if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
  				ro = 1;
  		} else {
7bc5c414f   Abhijith Das   [GFS2] Allow jour...
507
508
509
510
511
512
513
514
515
516
517
518
  			if (sdp->sd_vfs->s_flags & MS_RDONLY) {
  				/* check if device itself is read-only */
  				ro = bdev_read_only(sdp->sd_vfs->s_bdev);
  				if (!ro) {
  					fs_info(sdp, "recovery required on "
  						"read-only filesystem.
  ");
  					fs_info(sdp, "write access will be "
  						"enabled during recovery.
  ");
  				}
  			}
b3b94faa5   David Teigland   [GFS2] The core o...
519
520
521
  		}
  
  		if (ro) {
7bc5c414f   Abhijith Das   [GFS2] Allow jour...
522
523
524
  			fs_warn(sdp, "jid=%u: Can't replay: read-only block "
  				"device
  ", jd->jd_jid);
b3b94faa5   David Teigland   [GFS2] The core o...
525
  			error = -EROFS;
24972557b   Benjamin Marzinski   GFS2: remove tran...
526
  			goto fail_gunlock_thaw;
b3b94faa5   David Teigland   [GFS2] The core o...
527
528
529
530
531
532
533
534
535
536
537
  		}
  
  		fs_info(sdp, "jid=%u: Replaying journal...
  ", jd->jd_jid);
  
  		for (pass = 0; pass < 2; pass++) {
  			lops_before_scan(jd, &head, pass);
  			error = foreach_descriptor(jd, head.lh_tail,
  						   head.lh_blkno, pass);
  			lops_after_scan(jd, error, pass);
  			if (error)
24972557b   Benjamin Marzinski   GFS2: remove tran...
538
  				goto fail_gunlock_thaw;
b3b94faa5   David Teigland   [GFS2] The core o...
539
540
541
542
  		}
  
  		error = clean_journal(jd, &head);
  		if (error)
24972557b   Benjamin Marzinski   GFS2: remove tran...
543
  			goto fail_gunlock_thaw;
b3b94faa5   David Teigland   [GFS2] The core o...
544

24972557b   Benjamin Marzinski   GFS2: remove tran...
545
  		gfs2_glock_dq_uninit(&thaw_gh);
5c676f6d3   Steven Whitehouse   [GFS2] Macros rem...
546
  		t = DIV_ROUND_UP(jiffies - t, HZ);
b3b94faa5   David Teigland   [GFS2] The core o...
547
548
549
550
  		fs_info(sdp, "jid=%u: Journal replayed in %lus
  ",
  			jd->jd_jid, t);
  	}
f057f6cdf   Steven Whitehouse   GFS2: Merge lock_...
551
  	gfs2_recovery_done(sdp, jd->jd_jid, LM_RD_SUCCESS);
b3b94faa5   David Teigland   [GFS2] The core o...
552

c741c4551   Steven Whitehouse   GFS2: Fix spectat...
553
554
  	if (jlocked) {
  		gfs2_glock_dq_uninit(&ji_gh);
5965b1f47   Steven Whitehouse   [GFS2] Don't do r...
555
  		gfs2_glock_dq_uninit(&j_gh);
c741c4551   Steven Whitehouse   GFS2: Fix spectat...
556
  	}
b3b94faa5   David Teigland   [GFS2] The core o...
557
558
559
  
  	fs_info(sdp, "jid=%u: Done
  ", jd->jd_jid);
6ecd7c2dd   Tejun Heo   gfs2: use workque...
560
  	goto done;
b3b94faa5   David Teigland   [GFS2] The core o...
561

24972557b   Benjamin Marzinski   GFS2: remove tran...
562
563
  fail_gunlock_thaw:
  	gfs2_glock_dq_uninit(&thaw_gh);
5965b1f47   Steven Whitehouse   [GFS2] Don't do r...
564
  fail_gunlock_ji:
c741c4551   Steven Whitehouse   GFS2: Fix spectat...
565
  	if (jlocked) {
5965b1f47   Steven Whitehouse   [GFS2] Don't do r...
566
567
568
569
  		gfs2_glock_dq_uninit(&ji_gh);
  fail_gunlock_j:
  		gfs2_glock_dq_uninit(&j_gh);
  	}
b3b94faa5   David Teigland   [GFS2] The core o...
570
571
572
  
  	fs_info(sdp, "jid=%u: %s
  ", jd->jd_jid, (error) ? "Failed" : "Done");
5965b1f47   Steven Whitehouse   [GFS2] Don't do r...
573
  fail:
376d37788   David Teigland   GFS2: fail mount ...
574
  	jd->jd_recover_error = error;
f057f6cdf   Steven Whitehouse   GFS2: Merge lock_...
575
  	gfs2_recovery_done(sdp, jd->jd_jid, LM_RD_GAVEUP);
6ecd7c2dd   Tejun Heo   gfs2: use workque...
576
577
  done:
  	clear_bit(JDF_RECOVERY, &jd->jd_flags);
4e857c58e   Peter Zijlstra   arch: Mass conver...
578
  	smp_mb__after_atomic();
6ecd7c2dd   Tejun Heo   gfs2: use workque...
579
  	wake_up_bit(&jd->jd_flags, JDF_RECOVERY);
b3b94faa5   David Teigland   [GFS2] The core o...
580
  }
6ecd7c2dd   Tejun Heo   gfs2: use workque...
581
  int gfs2_recover_journal(struct gfs2_jdesc *jd, bool wait)
9ac1b4d9b   Steven Whitehouse   GFS2: Move gfs2_r...
582
  {
fe64d517d   Steven Whitehouse   GFS2: Umount reco...
583
  	int rv;
6ecd7c2dd   Tejun Heo   gfs2: use workque...
584
585
586
587
588
589
590
591
592
  
  	if (test_and_set_bit(JDF_RECOVERY, &jd->jd_flags))
  		return -EBUSY;
  
  	/* we have JDF_RECOVERY, queue should always succeed */
  	rv = queue_work(gfs_recovery_wq, &jd->jd_work);
  	BUG_ON(!rv);
  
  	if (wait)
743162013   NeilBrown   sched: Remove pro...
593
  		wait_on_bit(&jd->jd_flags, JDF_RECOVERY,
6ecd7c2dd   Tejun Heo   gfs2: use workque...
594
  			    TASK_UNINTERRUPTIBLE);
376d37788   David Teigland   GFS2: fail mount ...
595
  	return wait ? jd->jd_recover_error : 0;
9ac1b4d9b   Steven Whitehouse   GFS2: Move gfs2_r...
596
  }