blob: 5d34312194252a9969ea3392ad242249cbe008d9 [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 Petersona17d7582014-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 Petersona17d7582014-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 Petersona17d7582014-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 Petersona17d7582014-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 Petersona17d7582014-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 Petersona17d7582014-03-06 17:19:15 -0500109void gfs2_revoke_clean(struct gfs2_jdesc *jd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000110{
Bob Petersona17d7582014-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
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100121static int gfs2_log_header_in(struct gfs2_log_header_host *lh, const void *buf)
122{
123 const struct gfs2_log_header *str = buf;
124
125 if (str->lh_header.mh_magic != cpu_to_be32(GFS2_MAGIC) ||
126 str->lh_header.mh_type != cpu_to_be32(GFS2_METATYPE_LH))
127 return 1;
128
129 lh->lh_sequence = be64_to_cpu(str->lh_sequence);
130 lh->lh_flags = be32_to_cpu(str->lh_flags);
131 lh->lh_tail = be32_to_cpu(str->lh_tail);
132 lh->lh_blkno = be32_to_cpu(str->lh_blkno);
133 lh->lh_hash = be32_to_cpu(str->lh_hash);
134 return 0;
135}
136
David Teiglandb3b94fa2006-01-16 16:50:04 +0000137/**
138 * get_log_header - read the log header for a given segment
139 * @jd: the journal
140 * @blk: the block to look at
141 * @lh: the log header to return
142 *
143 * Read the log header for a given segement in a given journal. Do a few
144 * sanity checks on it.
145 *
146 * Returns: 0 on success,
147 * 1 if the header was invalid or incomplete,
148 * errno on error
149 */
150
151static int get_log_header(struct gfs2_jdesc *jd, unsigned int blk,
Al Viro55167622006-10-13 21:47:13 -0400152 struct gfs2_log_header_host *head)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000153{
154 struct buffer_head *bh;
akpm@linux-foundation.orgf5a8cd02008-03-12 14:01:29 -0700155 struct gfs2_log_header_host uninitialized_var(lh);
Steven Whitehouse887bc5d2006-12-05 13:34:17 -0500156 const u32 nothing = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -0400157 u32 hash;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000158 int error;
159
160 error = gfs2_replay_read_block(jd, blk, &bh);
161 if (error)
162 return error;
163
Steven Whitehouse2a2c9822006-10-31 14:44:50 -0500164 hash = crc32_le((u32)~0, bh->b_data, sizeof(struct gfs2_log_header) -
165 sizeof(u32));
166 hash = crc32_le(hash, (unsigned char const *)&nothing, sizeof(nothing));
167 hash ^= (u32)~0;
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100168 error = gfs2_log_header_in(&lh, bh->b_data);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000169 brelse(bh);
170
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100171 if (error || lh.lh_blkno != blk || lh.lh_hash != hash)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172 return 1;
173
174 *head = lh;
175
176 return 0;
177}
178
179/**
180 * find_good_lh - find a good log header
181 * @jd: the journal
182 * @blk: the segment to start searching from
183 * @lh: the log header to fill in
184 * @forward: if true search forward in the log, else search backward
185 *
186 * Call get_log_header() to get a log header for a segment, but if the
187 * segment is bad, either scan forward or backward until we find a good one.
188 *
189 * Returns: errno
190 */
191
192static int find_good_lh(struct gfs2_jdesc *jd, unsigned int *blk,
Al Viro55167622006-10-13 21:47:13 -0400193 struct gfs2_log_header_host *head)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000194{
195 unsigned int orig_blk = *blk;
196 int error;
197
198 for (;;) {
199 error = get_log_header(jd, *blk, head);
200 if (error <= 0)
201 return error;
202
203 if (++*blk == jd->jd_blocks)
204 *blk = 0;
205
206 if (*blk == orig_blk) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400207 gfs2_consist_inode(GFS2_I(jd->jd_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000208 return -EIO;
209 }
210 }
211}
212
213/**
214 * jhead_scan - make sure we've found the head of the log
215 * @jd: the journal
216 * @head: this is filled in with the log descriptor of the head
217 *
218 * At this point, seg and lh should be either the head of the log or just
219 * before. Scan forward until we find the head.
220 *
221 * Returns: errno
222 */
223
Al Viro55167622006-10-13 21:47:13 -0400224static int jhead_scan(struct gfs2_jdesc *jd, struct gfs2_log_header_host *head)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000225{
226 unsigned int blk = head->lh_blkno;
Al Viro55167622006-10-13 21:47:13 -0400227 struct gfs2_log_header_host lh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000228 int error;
229
230 for (;;) {
231 if (++blk == jd->jd_blocks)
232 blk = 0;
233
234 error = get_log_header(jd, blk, &lh);
235 if (error < 0)
236 return error;
237 if (error == 1)
238 continue;
239
240 if (lh.lh_sequence == head->lh_sequence) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400241 gfs2_consist_inode(GFS2_I(jd->jd_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000242 return -EIO;
243 }
244 if (lh.lh_sequence < head->lh_sequence)
245 break;
246
247 *head = lh;
248 }
249
250 return 0;
251}
252
253/**
254 * gfs2_find_jhead - find the head of a log
255 * @jd: the journal
256 * @head: the log descriptor for the head of the log is returned here
257 *
258 * Do a binary search of a journal and find the valid log entry with the
259 * highest sequence number. (i.e. the log head)
260 *
261 * Returns: errno
262 */
263
Al Viro55167622006-10-13 21:47:13 -0400264int gfs2_find_jhead(struct gfs2_jdesc *jd, struct gfs2_log_header_host *head)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000265{
Al Viro55167622006-10-13 21:47:13 -0400266 struct gfs2_log_header_host lh_1, lh_m;
Steven Whitehousecd915492006-09-04 12:49:07 -0400267 u32 blk_1, blk_2, blk_m;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000268 int error;
269
270 blk_1 = 0;
271 blk_2 = jd->jd_blocks - 1;
272
273 for (;;) {
274 blk_m = (blk_1 + blk_2) / 2;
275
276 error = find_good_lh(jd, &blk_1, &lh_1);
277 if (error)
278 return error;
279
280 error = find_good_lh(jd, &blk_m, &lh_m);
281 if (error)
282 return error;
283
284 if (blk_1 == blk_m || blk_m == blk_2)
285 break;
286
287 if (lh_1.lh_sequence <= lh_m.lh_sequence)
288 blk_1 = blk_m;
289 else
290 blk_2 = blk_m;
291 }
292
293 error = jhead_scan(jd, &lh_1);
294 if (error)
295 return error;
296
297 *head = lh_1;
298
299 return error;
300}
301
302/**
303 * foreach_descriptor - go through the active part of the log
304 * @jd: the journal
305 * @start: the first log header in the active region
306 * @end: the last log header (don't process the contents of this entry))
307 *
308 * Call a given function once for every log descriptor in the active
309 * portion of the log.
310 *
311 * Returns: errno
312 */
313
314static int foreach_descriptor(struct gfs2_jdesc *jd, unsigned int start,
315 unsigned int end, int pass)
316{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400317 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000318 struct buffer_head *bh;
319 struct gfs2_log_descriptor *ld;
320 int error = 0;
321 u32 length;
322 __be64 *ptr;
323 unsigned int offset = sizeof(struct gfs2_log_descriptor);
Steven Whitehousea67cdbd2006-09-05 14:41:30 -0400324 offset += sizeof(__be64) - 1;
325 offset &= ~(sizeof(__be64) - 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000326
327 while (start != end) {
328 error = gfs2_replay_read_block(jd, start, &bh);
329 if (error)
330 return error;
331 if (gfs2_meta_check(sdp, bh)) {
332 brelse(bh);
333 return -EIO;
334 }
335 ld = (struct gfs2_log_descriptor *)bh->b_data;
336 length = be32_to_cpu(ld->ld_length);
337
Steven Whitehousee3167de2006-03-30 15:46:23 -0500338 if (be32_to_cpu(ld->ld_header.mh_type) == GFS2_METATYPE_LH) {
Al Viro55167622006-10-13 21:47:13 -0400339 struct gfs2_log_header_host lh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000340 error = get_log_header(jd, start, &lh);
341 if (!error) {
Bob Petersone1cb6be2016-07-21 13:02:44 -0500342 gfs2_replay_incr_blk(jd, &start);
Russell Cattelan88721872006-08-10 11:08:40 -0500343 brelse(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000344 continue;
345 }
346 if (error == 1) {
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400347 gfs2_consist_inode(GFS2_I(jd->jd_inode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000348 error = -EIO;
349 }
350 brelse(bh);
351 return error;
352 } else if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LD)) {
353 brelse(bh);
354 return -EIO;
355 }
356 ptr = (__be64 *)(bh->b_data + offset);
357 error = lops_scan_elements(jd, start, ld, ptr, pass);
358 if (error) {
359 brelse(bh);
360 return error;
361 }
362
363 while (length--)
Bob Petersone1cb6be2016-07-21 13:02:44 -0500364 gfs2_replay_incr_blk(jd, &start);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000365
366 brelse(bh);
367 }
368
369 return 0;
370}
371
372/**
373 * clean_journal - mark a dirty journal as being clean
David Teiglandb3b94fa2006-01-16 16:50:04 +0000374 * @jd: the journal
David Teiglandb3b94fa2006-01-16 16:50:04 +0000375 * @head: the head journal to start from
376 *
377 * Returns: errno
378 */
379
Bob Peterson588bff92017-12-18 12:48:29 -0600380static void clean_journal(struct gfs2_jdesc *jd,
381 struct gfs2_log_header_host *head)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000382{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400383 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400384
Bob Peterson588bff92017-12-18 12:48:29 -0600385 sdp->sd_log_flush_head = head->lh_blkno;
386 gfs2_replay_incr_blk(jd, &sdp->sd_log_flush_head);
387 gfs2_write_log_header(sdp, head->lh_sequence + 1, 0,
388 GFS2_LOG_HEAD_UNMOUNT, REQ_PREFLUSH |
389 REQ_FUA | REQ_META | REQ_SYNC);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000390}
391
Steven Whitehouseda755fd2008-01-30 15:34:04 +0000392
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000393static void gfs2_recovery_done(struct gfs2_sbd *sdp, unsigned int jid,
394 unsigned int message)
Steven Whitehouseda755fd2008-01-30 15:34:04 +0000395{
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000396 char env_jid[20];
397 char env_status[20];
398 char *envp[] = { env_jid, env_status, NULL };
399 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
David Teiglande0c2a9a2012-01-09 17:18:05 -0500400
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000401 ls->ls_recover_jid_done = jid;
402 ls->ls_recover_jid_status = message;
alex chen3566c962015-01-12 19:01:03 +0800403 sprintf(env_jid, "JID=%u", jid);
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000404 sprintf(env_status, "RECOVERY=%s",
405 message == LM_RD_SUCCESS ? "Done" : "Failed");
406 kobject_uevent_env(&sdp->sd_kobj, KOBJ_CHANGE, envp);
David Teiglande0c2a9a2012-01-09 17:18:05 -0500407
408 if (sdp->sd_lockstruct.ls_ops->lm_recovery_result)
409 sdp->sd_lockstruct.ls_ops->lm_recovery_result(sdp, jid, message);
Steven Whitehouseda755fd2008-01-30 15:34:04 +0000410}
411
Tejun Heo6ecd7c22010-07-20 22:09:02 +0200412void gfs2_recover_func(struct work_struct *work)
Steven Whitehousefe64d512009-05-19 10:01:18 +0100413{
414 struct gfs2_jdesc *jd = container_of(work, struct gfs2_jdesc, jd_work);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400415 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
416 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
Al Viro55167622006-10-13 21:47:13 -0400417 struct gfs2_log_header_host head;
Benjamin Marzinski24972552014-05-01 22:26:55 -0500418 struct gfs2_holder j_gh, ji_gh, thaw_gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000419 unsigned long t;
420 int ro = 0;
421 unsigned int pass;
422 int error;
Steven Whitehousec741c452010-09-29 14:20:52 +0100423 int jlocked = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000424
Steven Whitehoused0795f92010-09-27 15:58:11 +0100425 if (sdp->sd_args.ar_spectator ||
426 (jd->jd_jid != sdp->sd_lockstruct.ls_jid)) {
Steven Whitehouse5965b1f2006-04-26 13:21:55 -0400427 fs_info(sdp, "jid=%u: Trying to acquire journal lock...\n",
428 jd->jd_jid);
Steven Whitehousec741c452010-09-29 14:20:52 +0100429 jlocked = 1;
Joe Perchesc78bad12008-02-03 17:33:42 +0200430 /* Acquire the journal lock so we can do recovery */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000431
Steven Whitehouse5965b1f2006-04-26 13:21:55 -0400432 error = gfs2_glock_nq_num(sdp, jd->jd_jid, &gfs2_journal_glops,
433 LM_ST_EXCLUSIVE,
434 LM_FLAG_NOEXP | LM_FLAG_TRY | GL_NOCACHE,
435 &j_gh);
436 switch (error) {
437 case 0:
438 break;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400439
Steven Whitehouse5965b1f2006-04-26 13:21:55 -0400440 case GLR_TRYFAILED:
441 fs_info(sdp, "jid=%u: Busy\n", jd->jd_jid);
442 error = 0;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400443
Steven Whitehouse5965b1f2006-04-26 13:21:55 -0400444 default:
445 goto fail;
446 };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000447
Steven Whitehouse5965b1f2006-04-26 13:21:55 -0400448 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED,
Bob Peterson75be73a2007-08-08 17:08:14 -0500449 LM_FLAG_NOEXP | GL_NOCACHE, &ji_gh);
Steven Whitehouse5965b1f2006-04-26 13:21:55 -0400450 if (error)
451 goto fail_gunlock_j;
452 } else {
453 fs_info(sdp, "jid=%u, already locked for use\n", jd->jd_jid);
454 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000455
456 fs_info(sdp, "jid=%u: Looking at journal...\n", jd->jd_jid);
457
458 error = gfs2_jdesc_check(jd);
459 if (error)
460 goto fail_gunlock_ji;
461
462 error = gfs2_find_jhead(jd, &head);
463 if (error)
464 goto fail_gunlock_ji;
465
466 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
467 fs_info(sdp, "jid=%u: Acquiring the transaction lock...\n",
468 jd->jd_jid);
469
470 t = jiffies;
471
Benjamin Marzinski24972552014-05-01 22:26:55 -0500472 /* Acquire a shared hold on the freeze lock */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000473
Benjamin Marzinski24972552014-05-01 22:26:55 -0500474 error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED,
475 LM_FLAG_NOEXP | LM_FLAG_PRIORITY,
476 &thaw_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000477 if (error)
478 goto fail_gunlock_ji;
479
David Teiglande8ca5cc2012-01-09 14:40:06 -0500480 if (test_bit(SDF_RORECOVERY, &sdp->sd_flags)) {
481 ro = 1;
482 } else if (test_bit(SDF_JOURNAL_CHECKED, &sdp->sd_flags)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000483 if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
484 ro = 1;
485 } else {
David Howellsbc98a422017-07-17 08:45:34 +0100486 if (sb_rdonly(sdp->sd_vfs)) {
Abhijith Das7bc5c412008-01-18 14:06:37 -0600487 /* check if device itself is read-only */
488 ro = bdev_read_only(sdp->sd_vfs->s_bdev);
489 if (!ro) {
490 fs_info(sdp, "recovery required on "
491 "read-only filesystem.\n");
492 fs_info(sdp, "write access will be "
493 "enabled during recovery.\n");
494 }
495 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000496 }
497
498 if (ro) {
Abhijith Das7bc5c412008-01-18 14:06:37 -0600499 fs_warn(sdp, "jid=%u: Can't replay: read-only block "
500 "device\n", jd->jd_jid);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000501 error = -EROFS;
Benjamin Marzinski24972552014-05-01 22:26:55 -0500502 goto fail_gunlock_thaw;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000503 }
504
505 fs_info(sdp, "jid=%u: Replaying journal...\n", jd->jd_jid);
506
507 for (pass = 0; pass < 2; pass++) {
508 lops_before_scan(jd, &head, pass);
509 error = foreach_descriptor(jd, head.lh_tail,
510 head.lh_blkno, pass);
511 lops_after_scan(jd, error, pass);
512 if (error)
Benjamin Marzinski24972552014-05-01 22:26:55 -0500513 goto fail_gunlock_thaw;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000514 }
515
Bob Peterson588bff92017-12-18 12:48:29 -0600516 clean_journal(jd, &head);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000517
Benjamin Marzinski24972552014-05-01 22:26:55 -0500518 gfs2_glock_dq_uninit(&thaw_gh);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500519 t = DIV_ROUND_UP(jiffies - t, HZ);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000520 fs_info(sdp, "jid=%u: Journal replayed in %lus\n",
521 jd->jd_jid, t);
522 }
523
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000524 gfs2_recovery_done(sdp, jd->jd_jid, LM_RD_SUCCESS);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000525
Steven Whitehousec741c452010-09-29 14:20:52 +0100526 if (jlocked) {
527 gfs2_glock_dq_uninit(&ji_gh);
Steven Whitehouse5965b1f2006-04-26 13:21:55 -0400528 gfs2_glock_dq_uninit(&j_gh);
Steven Whitehousec741c452010-09-29 14:20:52 +0100529 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000530
531 fs_info(sdp, "jid=%u: Done\n", jd->jd_jid);
Tejun Heo6ecd7c22010-07-20 22:09:02 +0200532 goto done;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000533
Benjamin Marzinski24972552014-05-01 22:26:55 -0500534fail_gunlock_thaw:
535 gfs2_glock_dq_uninit(&thaw_gh);
Steven Whitehouse5965b1f2006-04-26 13:21:55 -0400536fail_gunlock_ji:
Steven Whitehousec741c452010-09-29 14:20:52 +0100537 if (jlocked) {
Steven Whitehouse5965b1f2006-04-26 13:21:55 -0400538 gfs2_glock_dq_uninit(&ji_gh);
539fail_gunlock_j:
540 gfs2_glock_dq_uninit(&j_gh);
541 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000542
543 fs_info(sdp, "jid=%u: %s\n", jd->jd_jid, (error) ? "Failed" : "Done");
Steven Whitehouse5965b1f2006-04-26 13:21:55 -0400544fail:
David Teigland376d3772012-01-09 15:29:20 -0500545 jd->jd_recover_error = error;
Steven Whitehousef057f6c2009-01-12 10:43:39 +0000546 gfs2_recovery_done(sdp, jd->jd_jid, LM_RD_GAVEUP);
Tejun Heo6ecd7c22010-07-20 22:09:02 +0200547done:
548 clear_bit(JDF_RECOVERY, &jd->jd_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100549 smp_mb__after_atomic();
Tejun Heo6ecd7c22010-07-20 22:09:02 +0200550 wake_up_bit(&jd->jd_flags, JDF_RECOVERY);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000551}
552
Tejun Heo6ecd7c22010-07-20 22:09:02 +0200553int gfs2_recover_journal(struct gfs2_jdesc *jd, bool wait)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000554{
Steven Whitehousefe64d512009-05-19 10:01:18 +0100555 int rv;
Tejun Heo6ecd7c22010-07-20 22:09:02 +0200556
557 if (test_and_set_bit(JDF_RECOVERY, &jd->jd_flags))
558 return -EBUSY;
559
560 /* we have JDF_RECOVERY, queue should always succeed */
561 rv = queue_work(gfs_recovery_wq, &jd->jd_work);
562 BUG_ON(!rv);
563
564 if (wait)
NeilBrown74316202014-07-07 15:16:04 +1000565 wait_on_bit(&jd->jd_flags, JDF_RECOVERY,
Tejun Heo6ecd7c22010-07-20 22:09:02 +0200566 TASK_UNINTERRUPTIBLE);
567
David Teigland376d3772012-01-09 15:29:20 -0500568 return wait ? jd->jd_recover_error : 0;
Steven Whitehouse9ac1b4d2008-11-19 10:08:22 +0000569}
570