blob: 975f32166dfeffbe5605185b727f490002d49e17 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
9
David Howells1c2ea8a2009-11-20 21:50:40 +000010#include <linux/module.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000011#include <linux/slab.h>
12#include <linux/spinlock.h>
13#include <linux/completion.h>
14#include <linux/buffer_head.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050015#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050016#include <linux/crc32.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000017
18#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050019#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000020#include "bmap.h"
21#include "glock.h"
22#include "glops.h"
Bob Peterson588bff92017-12-18 12:48:29 -060023#include "log.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000024#include "lops.h"
25#include "meta_io.h"
26#include "recovery.h"
27#include "super.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050028#include "util.h"
Steven Whitehouse71b86f52006-03-28 14:14:04 -050029#include "dir.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000030
Tejun Heo6ecd7c22010-07-20 22:09:02 +020031struct workqueue_struct *gfs_recovery_wq;
32
David Teiglandb3b94fa2006-01-16 16:50:04 +000033int gfs2_replay_read_block(struct gfs2_jdesc *jd, unsigned int blk,
34 struct buffer_head **bh)
35{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040036 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
Steven Whitehouse5c676f62006-02-27 17:23:27 -050037 struct gfs2_glock *gl = ip->i_gl;
David Teiglandb3b94fa2006-01-16 16:50:04 +000038 int new = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -040039 u64 dblock;
40 u32 extlen;
David Teiglandb3b94fa2006-01-16 16:50:04 +000041 int error;
42
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040043 error = gfs2_extent_map(&ip->i_inode, blk, &new, &dblock, &extlen);
David Teiglandb3b94fa2006-01-16 16:50:04 +000044 if (error)
45 return error;
46 if (!dblock) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -050047 gfs2_consist_inode(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +000048 return -EIO;
49 }
50
Steven Whitehouse7276b3b2006-09-21 17:05:23 -040051 *bh = gfs2_meta_ra(gl, dblock, extlen);
David Teiglandb3b94fa2006-01-16 16:50:04 +000052
53 return error;
54}
55
Bob Petersona17d758b2014-03-06 17:19:15 -050056int gfs2_revoke_add(struct gfs2_jdesc *jd, u64 blkno, unsigned int where)
David Teiglandb3b94fa2006-01-16 16:50:04 +000057{
Bob Petersona17d758b2014-03-06 17:19:15 -050058 struct list_head *head = &jd->jd_revoke_list;
David Teiglandb3b94fa2006-01-16 16:50:04 +000059 struct gfs2_revoke_replay *rr;
60 int found = 0;
61
62 list_for_each_entry(rr, head, rr_list) {
63 if (rr->rr_blkno == blkno) {
64 found = 1;
65 break;
66 }
67 }
68
69 if (found) {
70 rr->rr_where = where;
71 return 0;
72 }
73
Josef Bacik16c5f062008-04-09 09:33:41 -040074 rr = kmalloc(sizeof(struct gfs2_revoke_replay), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +000075 if (!rr)
76 return -ENOMEM;
77
78 rr->rr_blkno = blkno;
79 rr->rr_where = where;
80 list_add(&rr->rr_list, head);
81
82 return 1;
83}
84
Bob Petersona17d758b2014-03-06 17:19:15 -050085int gfs2_revoke_check(struct gfs2_jdesc *jd, u64 blkno, unsigned int where)
David Teiglandb3b94fa2006-01-16 16:50:04 +000086{
87 struct gfs2_revoke_replay *rr;
88 int wrap, a, b, revoke;
89 int found = 0;
90
Bob Petersona17d758b2014-03-06 17:19:15 -050091 list_for_each_entry(rr, &jd->jd_revoke_list, rr_list) {
David Teiglandb3b94fa2006-01-16 16:50:04 +000092 if (rr->rr_blkno == blkno) {
93 found = 1;
94 break;
95 }
96 }
97
98 if (!found)
99 return 0;
100
Bob Petersona17d758b2014-03-06 17:19:15 -0500101 wrap = (rr->rr_where < jd->jd_replay_tail);
102 a = (jd->jd_replay_tail < where);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000103 b = (where < rr->rr_where);
104 revoke = (wrap) ? (a || b) : (a && b);
105
106 return revoke;
107}
108
Bob Petersona17d758b2014-03-06 17:19:15 -0500109void gfs2_revoke_clean(struct gfs2_jdesc *jd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000110{
Bob Petersona17d758b2014-03-06 17:19:15 -0500111 struct list_head *head = &jd->jd_revoke_list;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000112 struct gfs2_revoke_replay *rr;
113
114 while (!list_empty(head)) {
115 rr = list_entry(head->next, struct gfs2_revoke_replay, rr_list);
116 list_del(&rr->rr_list);
117 kfree(rr);
118 }
119}
120
121/**
122 * get_log_header - read the log header for a given segment
123 * @jd: the journal
124 * @blk: the block to look at
125 * @lh: the log header to return
126 *
127 * Read the log header for a given segement in a given journal. Do a few
128 * sanity checks on it.
129 *
130 * Returns: 0 on success,
131 * 1 if the header was invalid or incomplete,
132 * errno on error
133 */
134
135static int get_log_header(struct gfs2_jdesc *jd, unsigned int blk,
Al Viro55167622006-10-13 21:47:13 -0400136 struct gfs2_log_header_host *head)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000137{
Andreas Gruenbacher0ff59162018-01-16 23:07:57 +0100138 struct gfs2_log_header *lh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000139 struct buffer_head *bh;
Steven Whitehousecd915492006-09-04 12:49:07 -0400140 u32 hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000141 int error;
142
143 error = gfs2_replay_read_block(jd, blk, &bh);
144 if (error)
145 return error;
Andreas Gruenbacher0ff59162018-01-16 23:07:57 +0100146 lh = (void *)bh->b_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000147
Andreas Gruenbacher0ff59162018-01-16 23:07:57 +0100148 hash = crc32(~0, lh, sizeof(*lh) - 4);
149 hash = ~crc32_le_shift(hash, 4); /* assume lh_hash is zero */
150
151 error = lh->lh_header.mh_magic != cpu_to_be32(GFS2_MAGIC) ||
152 lh->lh_header.mh_type != cpu_to_be32(GFS2_METATYPE_LH) ||
153 be32_to_cpu(lh->lh_blkno) != blk ||
154 be32_to_cpu(lh->lh_hash) != hash;
155
David Teiglandb3b94fa2006-01-16 16:50:04 +0000156 brelse(bh);
157
Andreas Gruenbacher0ff59162018-01-16 23:07:57 +0100158 if (!error) {
159 head->lh_sequence = be64_to_cpu(lh->lh_sequence);
160 head->lh_flags = be32_to_cpu(lh->lh_flags);
161 head->lh_tail = be32_to_cpu(lh->lh_tail);
162 head->lh_blkno = be32_to_cpu(lh->lh_blkno);
163 }
164 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000165}
166
167/**
168 * find_good_lh - find a good log header
169 * @jd: the journal
170 * @blk: the segment to start searching from
171 * @lh: the log header to fill in
172 * @forward: if true search forward in the log, else search backward
173 *
174 * Call get_log_header() to get a log header for a segment, but if the
175 * segment is bad, either scan forward or backward until we find a good one.
176 *
177 * Returns: errno
178 */
179
180static int find_good_lh(struct gfs2_jdesc *jd, unsigned int *blk,
Al Viro55167622006-10-13 21:47:13 -0400181 struct gfs2_log_header_host *head)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000182{
183 unsigned int orig_blk = *blk;
184 int error;
185
186 for (;;) {
187 error = get_log_header(jd, *blk, head);
188 if (error <= 0)
189 return error;
190
191 if (++*blk == jd->jd_blocks)
192 *blk = 0;
193
194 if (*blk == orig_blk) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400195 gfs2_consist_inode(GFS2_I(jd->jd_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000196 return -EIO;
197 }
198 }
199}
200
201/**
202 * jhead_scan - make sure we've found the head of the log
203 * @jd: the journal
204 * @head: this is filled in with the log descriptor of the head
205 *
206 * At this point, seg and lh should be either the head of the log or just
207 * before. Scan forward until we find the head.
208 *
209 * Returns: errno
210 */
211
Al Viro55167622006-10-13 21:47:13 -0400212static int jhead_scan(struct gfs2_jdesc *jd, struct gfs2_log_header_host *head)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000213{
214 unsigned int blk = head->lh_blkno;
Al Viro55167622006-10-13 21:47:13 -0400215 struct gfs2_log_header_host lh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000216 int error;
217
218 for (;;) {
219 if (++blk == jd->jd_blocks)
220 blk = 0;
221
222 error = get_log_header(jd, blk, &lh);
223 if (error < 0)
224 return error;
225 if (error == 1)
226 continue;
227
228 if (lh.lh_sequence == head->lh_sequence) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400229 gfs2_consist_inode(GFS2_I(jd->jd_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000230 return -EIO;
231 }
232 if (lh.lh_sequence < head->lh_sequence)
233 break;
234
235 *head = lh;
236 }
237
238 return 0;
239}
240
241/**
242 * gfs2_find_jhead - find the head of a log
243 * @jd: the journal
244 * @head: the log descriptor for the head of the log is returned here
245 *
246 * Do a binary search of a journal and find the valid log entry with the
247 * highest sequence number. (i.e. the log head)
248 *
249 * Returns: errno
250 */
251
Al Viro55167622006-10-13 21:47:13 -0400252int gfs2_find_jhead(struct gfs2_jdesc *jd, struct gfs2_log_header_host *head)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000253{
Al Viro55167622006-10-13 21:47:13 -0400254 struct gfs2_log_header_host lh_1, lh_m;
Steven Whitehousecd915492006-09-04 12:49:07 -0400255 u32 blk_1, blk_2, blk_m;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000256 int error;
257
258 blk_1 = 0;
259 blk_2 = jd->jd_blocks - 1;
260
261 for (;;) {
262 blk_m = (blk_1 + blk_2) / 2;
263
264 error = find_good_lh(jd, &blk_1, &lh_1);
265 if (error)
266 return error;
267
268 error = find_good_lh(jd, &blk_m, &lh_m);
269 if (error)
270 return error;
271
272 if (blk_1 == blk_m || blk_m == blk_2)
273 break;
274
275 if (lh_1.lh_sequence <= lh_m.lh_sequence)
276 blk_1 = blk_m;
277 else
278 blk_2 = blk_m;
279 }
280
281 error = jhead_scan(jd, &lh_1);
282 if (error)
283 return error;
284
285 *head = lh_1;
286
287 return error;
288}
289
290/**
291 * foreach_descriptor - go through the active part of the log
292 * @jd: the journal
293 * @start: the first log header in the active region
294 * @end: the last log header (don't process the contents of this entry))
295 *
296 * Call a given function once for every log descriptor in the active
297 * portion of the log.
298 *
299 * Returns: errno
300 */
301
302static int foreach_descriptor(struct gfs2_jdesc *jd, unsigned int start,
303 unsigned int end, int pass)
304{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400305 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000306 struct buffer_head *bh;
307 struct gfs2_log_descriptor *ld;
308 int error = 0;
309 u32 length;
310 __be64 *ptr;
311 unsigned int offset = sizeof(struct gfs2_log_descriptor);
Steven Whitehousea67cdbd2006-09-05 14:41:30 -0400312 offset += sizeof(__be64) - 1;
313 offset &= ~(sizeof(__be64) - 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000314
315 while (start != end) {
316 error = gfs2_replay_read_block(jd, start, &bh);
317 if (error)
318 return error;
319 if (gfs2_meta_check(sdp, bh)) {
320 brelse(bh);
321 return -EIO;
322 }
323 ld = (struct gfs2_log_descriptor *)bh->b_data;
324 length = be32_to_cpu(ld->ld_length);
325
Steven Whitehousee3167de2006-03-30 15:46:23 -0500326 if (be32_to_cpu(ld->ld_header.mh_type) == GFS2_METATYPE_LH) {
Al Viro55167622006-10-13 21:47:13 -0400327 struct gfs2_log_header_host lh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000328 error = get_log_header(jd, start, &lh);
329 if (!error) {
Bob Petersone1cb6be2016-07-21 13:02:44 -0500330 gfs2_replay_incr_blk(jd, &start);
Russell Cattelan88721872006-08-10 11:08:40 -0500331 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000332 continue;
333 }
334 if (error == 1) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400335 gfs2_consist_inode(GFS2_I(jd->jd_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000336 error = -EIO;
337 }
338 brelse(bh);
339 return error;
340 } else if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LD)) {
341 brelse(bh);
342 return -EIO;
343 }
344 ptr = (__be64 *)(bh->b_data + offset);
345 error = lops_scan_elements(jd, start, ld, ptr, pass);
346 if (error) {
347 brelse(bh);
348 return error;
349 }
350
351 while (length--)
Bob Petersone1cb6be2016-07-21 13:02:44 -0500352 gfs2_replay_incr_blk(jd, &start);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000353
354 brelse(bh);
355 }
356
357 return 0;
358}
359
360/**
361 * clean_journal - mark a dirty journal as being clean
David Teiglandb3b94fa2006-01-16 16:50:04 +0000362 * @jd: the journal
David Teiglandb3b94fa2006-01-16 16:50:04 +0000363 * @head: the head journal to start from
364 *
365 * Returns: errno
366 */
367
Bob Peterson588bff92017-12-18 12:48:29 -0600368static void clean_journal(struct gfs2_jdesc *jd,
369 struct gfs2_log_header_host *head)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000370{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400371 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400372
Bob Peterson588bff92017-12-18 12:48:29 -0600373 sdp->sd_log_flush_head = head->lh_blkno;
374 gfs2_replay_incr_blk(jd, &sdp->sd_log_flush_head);
375 gfs2_write_log_header(sdp, head->lh_sequence + 1, 0,
376 GFS2_LOG_HEAD_UNMOUNT, REQ_PREFLUSH |
377 REQ_FUA | REQ_META | REQ_SYNC);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000378}
379
Steven Whitehouseda755fd2008-01-30 15:34:04 +0000380
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000381static void gfs2_recovery_done(struct gfs2_sbd *sdp, unsigned int jid,
382 unsigned int message)
Steven Whitehouseda755fd2008-01-30 15:34:04 +0000383{
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000384 char env_jid[20];
385 char env_status[20];
386 char *envp[] = { env_jid, env_status, NULL };
387 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
David Teiglande0c2a9a2012-01-09 17:18:05 -0500388
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000389 ls->ls_recover_jid_done = jid;
390 ls->ls_recover_jid_status = message;
alex chen3566c962015-01-12 19:01:03 +0800391 sprintf(env_jid, "JID=%u", jid);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000392 sprintf(env_status, "RECOVERY=%s",
393 message == LM_RD_SUCCESS ? "Done" : "Failed");
394 kobject_uevent_env(&sdp->sd_kobj, KOBJ_CHANGE, envp);
David Teiglande0c2a9a2012-01-09 17:18:05 -0500395
396 if (sdp->sd_lockstruct.ls_ops->lm_recovery_result)
397 sdp->sd_lockstruct.ls_ops->lm_recovery_result(sdp, jid, message);
Steven Whitehouseda755fd2008-01-30 15:34:04 +0000398}
399
Tejun Heo6ecd7c22010-07-20 22:09:02 +0200400void gfs2_recover_func(struct work_struct *work)
Steven Whitehousefe64d512009-05-19 10:01:18 +0100401{
402 struct gfs2_jdesc *jd = container_of(work, struct gfs2_jdesc, jd_work);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400403 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
404 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
Al Viro55167622006-10-13 21:47:13 -0400405 struct gfs2_log_header_host head;
Benjamin Marzinski24972552014-05-01 22:26:55 -0500406 struct gfs2_holder j_gh, ji_gh, thaw_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000407 unsigned long t;
408 int ro = 0;
409 unsigned int pass;
410 int error;
Steven Whitehousec741c452010-09-29 14:20:52 +0100411 int jlocked = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000412
Steven Whitehoused0795f92010-09-27 15:58:11 +0100413 if (sdp->sd_args.ar_spectator ||
414 (jd->jd_jid != sdp->sd_lockstruct.ls_jid)) {
Steven Whitehouse5965b1f2006-04-26 13:21:55 -0400415 fs_info(sdp, "jid=%u: Trying to acquire journal lock...\n",
416 jd->jd_jid);
Steven Whitehousec741c452010-09-29 14:20:52 +0100417 jlocked = 1;
Joe Perchesc78bad12008-02-03 17:33:42 +0200418 /* Acquire the journal lock so we can do recovery */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000419
Steven Whitehouse5965b1f2006-04-26 13:21:55 -0400420 error = gfs2_glock_nq_num(sdp, jd->jd_jid, &gfs2_journal_glops,
421 LM_ST_EXCLUSIVE,
422 LM_FLAG_NOEXP | LM_FLAG_TRY | GL_NOCACHE,
423 &j_gh);
424 switch (error) {
425 case 0:
426 break;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400427
Steven Whitehouse5965b1f2006-04-26 13:21:55 -0400428 case GLR_TRYFAILED:
429 fs_info(sdp, "jid=%u: Busy\n", jd->jd_jid);
430 error = 0;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400431
Steven Whitehouse5965b1f2006-04-26 13:21:55 -0400432 default:
433 goto fail;
434 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000435
Steven Whitehouse5965b1f2006-04-26 13:21:55 -0400436 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED,
Bob Peterson75be73a2007-08-08 17:08:14 -0500437 LM_FLAG_NOEXP | GL_NOCACHE, &ji_gh);
Steven Whitehouse5965b1f2006-04-26 13:21:55 -0400438 if (error)
439 goto fail_gunlock_j;
440 } else {
441 fs_info(sdp, "jid=%u, already locked for use\n", jd->jd_jid);
442 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000443
444 fs_info(sdp, "jid=%u: Looking at journal...\n", jd->jd_jid);
445
446 error = gfs2_jdesc_check(jd);
447 if (error)
448 goto fail_gunlock_ji;
449
450 error = gfs2_find_jhead(jd, &head);
451 if (error)
452 goto fail_gunlock_ji;
453
454 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
455 fs_info(sdp, "jid=%u: Acquiring the transaction lock...\n",
456 jd->jd_jid);
457
458 t = jiffies;
459
Benjamin Marzinski24972552014-05-01 22:26:55 -0500460 /* Acquire a shared hold on the freeze lock */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000461
Benjamin Marzinski24972552014-05-01 22:26:55 -0500462 error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED,
463 LM_FLAG_NOEXP | LM_FLAG_PRIORITY,
464 &thaw_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000465 if (error)
466 goto fail_gunlock_ji;
467
David Teiglande8ca5cc2012-01-09 14:40:06 -0500468 if (test_bit(SDF_RORECOVERY, &sdp->sd_flags)) {
469 ro = 1;
470 } else if (test_bit(SDF_JOURNAL_CHECKED, &sdp->sd_flags)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000471 if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
472 ro = 1;
473 } else {
David Howellsbc98a422017-07-17 08:45:34 +0100474 if (sb_rdonly(sdp->sd_vfs)) {
Abhijith Das7bc5c412008-01-18 14:06:37 -0600475 /* check if device itself is read-only */
476 ro = bdev_read_only(sdp->sd_vfs->s_bdev);
477 if (!ro) {
478 fs_info(sdp, "recovery required on "
479 "read-only filesystem.\n");
480 fs_info(sdp, "write access will be "
481 "enabled during recovery.\n");
482 }
483 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000484 }
485
486 if (ro) {
Abhijith Das7bc5c412008-01-18 14:06:37 -0600487 fs_warn(sdp, "jid=%u: Can't replay: read-only block "
488 "device\n", jd->jd_jid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000489 error = -EROFS;
Benjamin Marzinski24972552014-05-01 22:26:55 -0500490 goto fail_gunlock_thaw;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000491 }
492
493 fs_info(sdp, "jid=%u: Replaying journal...\n", jd->jd_jid);
494
495 for (pass = 0; pass < 2; pass++) {
496 lops_before_scan(jd, &head, pass);
497 error = foreach_descriptor(jd, head.lh_tail,
498 head.lh_blkno, pass);
499 lops_after_scan(jd, error, pass);
500 if (error)
Benjamin Marzinski24972552014-05-01 22:26:55 -0500501 goto fail_gunlock_thaw;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000502 }
503
Bob Peterson588bff92017-12-18 12:48:29 -0600504 clean_journal(jd, &head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000505
Benjamin Marzinski24972552014-05-01 22:26:55 -0500506 gfs2_glock_dq_uninit(&thaw_gh);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500507 t = DIV_ROUND_UP(jiffies - t, HZ);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000508 fs_info(sdp, "jid=%u: Journal replayed in %lus\n",
509 jd->jd_jid, t);
510 }
511
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000512 gfs2_recovery_done(sdp, jd->jd_jid, LM_RD_SUCCESS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000513
Steven Whitehousec741c452010-09-29 14:20:52 +0100514 if (jlocked) {
515 gfs2_glock_dq_uninit(&ji_gh);
Steven Whitehouse5965b1f2006-04-26 13:21:55 -0400516 gfs2_glock_dq_uninit(&j_gh);
Steven Whitehousec741c452010-09-29 14:20:52 +0100517 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000518
519 fs_info(sdp, "jid=%u: Done\n", jd->jd_jid);
Tejun Heo6ecd7c22010-07-20 22:09:02 +0200520 goto done;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000521
Benjamin Marzinski24972552014-05-01 22:26:55 -0500522fail_gunlock_thaw:
523 gfs2_glock_dq_uninit(&thaw_gh);
Steven Whitehouse5965b1f2006-04-26 13:21:55 -0400524fail_gunlock_ji:
Steven Whitehousec741c452010-09-29 14:20:52 +0100525 if (jlocked) {
Steven Whitehouse5965b1f2006-04-26 13:21:55 -0400526 gfs2_glock_dq_uninit(&ji_gh);
527fail_gunlock_j:
528 gfs2_glock_dq_uninit(&j_gh);
529 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000530
531 fs_info(sdp, "jid=%u: %s\n", jd->jd_jid, (error) ? "Failed" : "Done");
Steven Whitehouse5965b1f2006-04-26 13:21:55 -0400532fail:
David Teigland376d3772012-01-09 15:29:20 -0500533 jd->jd_recover_error = error;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000534 gfs2_recovery_done(sdp, jd->jd_jid, LM_RD_GAVEUP);
Tejun Heo6ecd7c22010-07-20 22:09:02 +0200535done:
536 clear_bit(JDF_RECOVERY, &jd->jd_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100537 smp_mb__after_atomic();
Tejun Heo6ecd7c22010-07-20 22:09:02 +0200538 wake_up_bit(&jd->jd_flags, JDF_RECOVERY);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000539}
540
Tejun Heo6ecd7c22010-07-20 22:09:02 +0200541int gfs2_recover_journal(struct gfs2_jdesc *jd, bool wait)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000542{
Steven Whitehousefe64d512009-05-19 10:01:18 +0100543 int rv;
Tejun Heo6ecd7c22010-07-20 22:09:02 +0200544
545 if (test_and_set_bit(JDF_RECOVERY, &jd->jd_flags))
546 return -EBUSY;
547
548 /* we have JDF_RECOVERY, queue should always succeed */
549 rv = queue_work(gfs_recovery_wq, &jd->jd_work);
550 BUG_ON(!rv);
551
552 if (wait)
NeilBrown74316202014-07-07 15:16:04 +1000553 wait_on_bit(&jd->jd_flags, JDF_RECOVERY,
Tejun Heo6ecd7c22010-07-20 22:09:02 +0200554 TASK_UNINTERRUPTIBLE);
555
David Teigland376d3772012-01-09 15:29:20 -0500556 return wait ? jd->jd_recover_error : 0;
Steven Whitehouse9ac1b4d2008-11-19 10:08:22 +0000557}
558