David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
Steven Whitehouse | 3a8a9a1 | 2006-05-18 15:09:15 -0400 | [diff] [blame] | 3 | * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 4 | * |
| 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 Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 7 | * of the GNU General Public License version 2. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
David Howells | 1c2ea8a | 2009-11-20 21:50:40 +0000 | [diff] [blame] | 10 | #include <linux/module.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 11 | #include <linux/slab.h> |
| 12 | #include <linux/spinlock.h> |
| 13 | #include <linux/completion.h> |
| 14 | #include <linux/buffer_head.h> |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 15 | #include <linux/gfs2_ondisk.h> |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 16 | #include <linux/crc32.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 17 | |
| 18 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 19 | #include "incore.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 20 | #include "bmap.h" |
| 21 | #include "glock.h" |
| 22 | #include "glops.h" |
Bob Peterson | 588bff9 | 2017-12-18 12:48:29 -0600 | [diff] [blame^] | 23 | #include "log.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 24 | #include "lops.h" |
| 25 | #include "meta_io.h" |
| 26 | #include "recovery.h" |
| 27 | #include "super.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 28 | #include "util.h" |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 29 | #include "dir.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 30 | |
Tejun Heo | 6ecd7c2 | 2010-07-20 22:09:02 +0200 | [diff] [blame] | 31 | struct workqueue_struct *gfs_recovery_wq; |
| 32 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 33 | int gfs2_replay_read_block(struct gfs2_jdesc *jd, unsigned int blk, |
| 34 | struct buffer_head **bh) |
| 35 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 36 | struct gfs2_inode *ip = GFS2_I(jd->jd_inode); |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 37 | struct gfs2_glock *gl = ip->i_gl; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 38 | int new = 0; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 39 | u64 dblock; |
| 40 | u32 extlen; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 41 | int error; |
| 42 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 43 | error = gfs2_extent_map(&ip->i_inode, blk, &new, &dblock, &extlen); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 44 | if (error) |
| 45 | return error; |
| 46 | if (!dblock) { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 47 | gfs2_consist_inode(ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 48 | return -EIO; |
| 49 | } |
| 50 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 51 | *bh = gfs2_meta_ra(gl, dblock, extlen); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 52 | |
| 53 | return error; |
| 54 | } |
| 55 | |
Bob Peterson | a17d758 | 2014-03-06 17:19:15 -0500 | [diff] [blame] | 56 | int gfs2_revoke_add(struct gfs2_jdesc *jd, u64 blkno, unsigned int where) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 57 | { |
Bob Peterson | a17d758 | 2014-03-06 17:19:15 -0500 | [diff] [blame] | 58 | struct list_head *head = &jd->jd_revoke_list; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 59 | 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 Bacik | 16c5f06 | 2008-04-09 09:33:41 -0400 | [diff] [blame] | 74 | rr = kmalloc(sizeof(struct gfs2_revoke_replay), GFP_NOFS); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 75 | 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 Peterson | a17d758 | 2014-03-06 17:19:15 -0500 | [diff] [blame] | 85 | int gfs2_revoke_check(struct gfs2_jdesc *jd, u64 blkno, unsigned int where) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 86 | { |
| 87 | struct gfs2_revoke_replay *rr; |
| 88 | int wrap, a, b, revoke; |
| 89 | int found = 0; |
| 90 | |
Bob Peterson | a17d758 | 2014-03-06 17:19:15 -0500 | [diff] [blame] | 91 | list_for_each_entry(rr, &jd->jd_revoke_list, rr_list) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 92 | if (rr->rr_blkno == blkno) { |
| 93 | found = 1; |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | if (!found) |
| 99 | return 0; |
| 100 | |
Bob Peterson | a17d758 | 2014-03-06 17:19:15 -0500 | [diff] [blame] | 101 | wrap = (rr->rr_where < jd->jd_replay_tail); |
| 102 | a = (jd->jd_replay_tail < where); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 103 | b = (where < rr->rr_where); |
| 104 | revoke = (wrap) ? (a || b) : (a && b); |
| 105 | |
| 106 | return revoke; |
| 107 | } |
| 108 | |
Bob Peterson | a17d758 | 2014-03-06 17:19:15 -0500 | [diff] [blame] | 109 | void gfs2_revoke_clean(struct gfs2_jdesc *jd) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 110 | { |
Bob Peterson | a17d758 | 2014-03-06 17:19:15 -0500 | [diff] [blame] | 111 | struct list_head *head = &jd->jd_revoke_list; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 112 | 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 Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 121 | static 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 Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 137 | /** |
| 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 | |
| 151 | static int get_log_header(struct gfs2_jdesc *jd, unsigned int blk, |
Al Viro | 5516762 | 2006-10-13 21:47:13 -0400 | [diff] [blame] | 152 | struct gfs2_log_header_host *head) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 153 | { |
| 154 | struct buffer_head *bh; |
akpm@linux-foundation.org | f5a8cd0 | 2008-03-12 14:01:29 -0700 | [diff] [blame] | 155 | struct gfs2_log_header_host uninitialized_var(lh); |
Steven Whitehouse | 887bc5d | 2006-12-05 13:34:17 -0500 | [diff] [blame] | 156 | const u32 nothing = 0; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 157 | u32 hash; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 158 | int error; |
| 159 | |
| 160 | error = gfs2_replay_read_block(jd, blk, &bh); |
| 161 | if (error) |
| 162 | return error; |
| 163 | |
Steven Whitehouse | 2a2c982 | 2006-10-31 14:44:50 -0500 | [diff] [blame] | 164 | hash = crc32_le((u32)~0, bh->b_data, sizeof(struct gfs2_log_header) - |
| 165 | sizeof(u32)); |
| 166 | hash = crc32_le(hash, (unsigned char const *)¬hing, sizeof(nothing)); |
| 167 | hash ^= (u32)~0; |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 168 | error = gfs2_log_header_in(&lh, bh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 169 | brelse(bh); |
| 170 | |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 171 | if (error || lh.lh_blkno != blk || lh.lh_hash != hash) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 172 | 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 | |
| 192 | static int find_good_lh(struct gfs2_jdesc *jd, unsigned int *blk, |
Al Viro | 5516762 | 2006-10-13 21:47:13 -0400 | [diff] [blame] | 193 | struct gfs2_log_header_host *head) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 194 | { |
| 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 Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 207 | gfs2_consist_inode(GFS2_I(jd->jd_inode)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 208 | 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 Viro | 5516762 | 2006-10-13 21:47:13 -0400 | [diff] [blame] | 224 | static int jhead_scan(struct gfs2_jdesc *jd, struct gfs2_log_header_host *head) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 225 | { |
| 226 | unsigned int blk = head->lh_blkno; |
Al Viro | 5516762 | 2006-10-13 21:47:13 -0400 | [diff] [blame] | 227 | struct gfs2_log_header_host lh; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 228 | 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 Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 241 | gfs2_consist_inode(GFS2_I(jd->jd_inode)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 242 | 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 Viro | 5516762 | 2006-10-13 21:47:13 -0400 | [diff] [blame] | 264 | int gfs2_find_jhead(struct gfs2_jdesc *jd, struct gfs2_log_header_host *head) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 265 | { |
Al Viro | 5516762 | 2006-10-13 21:47:13 -0400 | [diff] [blame] | 266 | struct gfs2_log_header_host lh_1, lh_m; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 267 | u32 blk_1, blk_2, blk_m; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 268 | 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 | |
| 314 | static int foreach_descriptor(struct gfs2_jdesc *jd, unsigned int start, |
| 315 | unsigned int end, int pass) |
| 316 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 317 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 318 | 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 Whitehouse | a67cdbd | 2006-09-05 14:41:30 -0400 | [diff] [blame] | 324 | offset += sizeof(__be64) - 1; |
| 325 | offset &= ~(sizeof(__be64) - 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 326 | |
| 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 Whitehouse | e3167de | 2006-03-30 15:46:23 -0500 | [diff] [blame] | 338 | if (be32_to_cpu(ld->ld_header.mh_type) == GFS2_METATYPE_LH) { |
Al Viro | 5516762 | 2006-10-13 21:47:13 -0400 | [diff] [blame] | 339 | struct gfs2_log_header_host lh; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 340 | error = get_log_header(jd, start, &lh); |
| 341 | if (!error) { |
Bob Peterson | e1cb6be | 2016-07-21 13:02:44 -0500 | [diff] [blame] | 342 | gfs2_replay_incr_blk(jd, &start); |
Russell Cattelan | 8872187 | 2006-08-10 11:08:40 -0500 | [diff] [blame] | 343 | brelse(bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 344 | continue; |
| 345 | } |
| 346 | if (error == 1) { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 347 | gfs2_consist_inode(GFS2_I(jd->jd_inode)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 348 | 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 Peterson | e1cb6be | 2016-07-21 13:02:44 -0500 | [diff] [blame] | 364 | gfs2_replay_incr_blk(jd, &start); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 365 | |
| 366 | brelse(bh); |
| 367 | } |
| 368 | |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * clean_journal - mark a dirty journal as being clean |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 374 | * @jd: the journal |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 375 | * @head: the head journal to start from |
| 376 | * |
| 377 | * Returns: errno |
| 378 | */ |
| 379 | |
Bob Peterson | 588bff9 | 2017-12-18 12:48:29 -0600 | [diff] [blame^] | 380 | static void clean_journal(struct gfs2_jdesc *jd, |
| 381 | struct gfs2_log_header_host *head) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 382 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 383 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 384 | |
Bob Peterson | 588bff9 | 2017-12-18 12:48:29 -0600 | [diff] [blame^] | 385 | 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 Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Steven Whitehouse | da755fd | 2008-01-30 15:34:04 +0000 | [diff] [blame] | 392 | |
Steven Whitehouse | f057f6c | 2009-01-12 10:43:39 +0000 | [diff] [blame] | 393 | static void gfs2_recovery_done(struct gfs2_sbd *sdp, unsigned int jid, |
| 394 | unsigned int message) |
Steven Whitehouse | da755fd | 2008-01-30 15:34:04 +0000 | [diff] [blame] | 395 | { |
Steven Whitehouse | f057f6c | 2009-01-12 10:43:39 +0000 | [diff] [blame] | 396 | 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 Teigland | e0c2a9a | 2012-01-09 17:18:05 -0500 | [diff] [blame] | 400 | |
Steven Whitehouse | f057f6c | 2009-01-12 10:43:39 +0000 | [diff] [blame] | 401 | ls->ls_recover_jid_done = jid; |
| 402 | ls->ls_recover_jid_status = message; |
alex chen | 3566c96 | 2015-01-12 19:01:03 +0800 | [diff] [blame] | 403 | sprintf(env_jid, "JID=%u", jid); |
Steven Whitehouse | f057f6c | 2009-01-12 10:43:39 +0000 | [diff] [blame] | 404 | sprintf(env_status, "RECOVERY=%s", |
| 405 | message == LM_RD_SUCCESS ? "Done" : "Failed"); |
| 406 | kobject_uevent_env(&sdp->sd_kobj, KOBJ_CHANGE, envp); |
David Teigland | e0c2a9a | 2012-01-09 17:18:05 -0500 | [diff] [blame] | 407 | |
| 408 | if (sdp->sd_lockstruct.ls_ops->lm_recovery_result) |
| 409 | sdp->sd_lockstruct.ls_ops->lm_recovery_result(sdp, jid, message); |
Steven Whitehouse | da755fd | 2008-01-30 15:34:04 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Tejun Heo | 6ecd7c2 | 2010-07-20 22:09:02 +0200 | [diff] [blame] | 412 | void gfs2_recover_func(struct work_struct *work) |
Steven Whitehouse | fe64d51 | 2009-05-19 10:01:18 +0100 | [diff] [blame] | 413 | { |
| 414 | struct gfs2_jdesc *jd = container_of(work, struct gfs2_jdesc, jd_work); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 415 | struct gfs2_inode *ip = GFS2_I(jd->jd_inode); |
| 416 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
Al Viro | 5516762 | 2006-10-13 21:47:13 -0400 | [diff] [blame] | 417 | struct gfs2_log_header_host head; |
Benjamin Marzinski | 2497255 | 2014-05-01 22:26:55 -0500 | [diff] [blame] | 418 | struct gfs2_holder j_gh, ji_gh, thaw_gh; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 419 | unsigned long t; |
| 420 | int ro = 0; |
| 421 | unsigned int pass; |
| 422 | int error; |
Steven Whitehouse | c741c45 | 2010-09-29 14:20:52 +0100 | [diff] [blame] | 423 | int jlocked = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 424 | |
Steven Whitehouse | d0795f9 | 2010-09-27 15:58:11 +0100 | [diff] [blame] | 425 | if (sdp->sd_args.ar_spectator || |
| 426 | (jd->jd_jid != sdp->sd_lockstruct.ls_jid)) { |
Steven Whitehouse | 5965b1f | 2006-04-26 13:21:55 -0400 | [diff] [blame] | 427 | fs_info(sdp, "jid=%u: Trying to acquire journal lock...\n", |
| 428 | jd->jd_jid); |
Steven Whitehouse | c741c45 | 2010-09-29 14:20:52 +0100 | [diff] [blame] | 429 | jlocked = 1; |
Joe Perches | c78bad1 | 2008-02-03 17:33:42 +0200 | [diff] [blame] | 430 | /* Acquire the journal lock so we can do recovery */ |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 431 | |
Steven Whitehouse | 5965b1f | 2006-04-26 13:21:55 -0400 | [diff] [blame] | 432 | 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 Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 439 | |
Steven Whitehouse | 5965b1f | 2006-04-26 13:21:55 -0400 | [diff] [blame] | 440 | case GLR_TRYFAILED: |
| 441 | fs_info(sdp, "jid=%u: Busy\n", jd->jd_jid); |
| 442 | error = 0; |
Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 443 | |
Steven Whitehouse | 5965b1f | 2006-04-26 13:21:55 -0400 | [diff] [blame] | 444 | default: |
| 445 | goto fail; |
| 446 | }; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 447 | |
Steven Whitehouse | 5965b1f | 2006-04-26 13:21:55 -0400 | [diff] [blame] | 448 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, |
Bob Peterson | 75be73a | 2007-08-08 17:08:14 -0500 | [diff] [blame] | 449 | LM_FLAG_NOEXP | GL_NOCACHE, &ji_gh); |
Steven Whitehouse | 5965b1f | 2006-04-26 13:21:55 -0400 | [diff] [blame] | 450 | 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 Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 455 | |
| 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 Marzinski | 2497255 | 2014-05-01 22:26:55 -0500 | [diff] [blame] | 472 | /* Acquire a shared hold on the freeze lock */ |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 473 | |
Benjamin Marzinski | 2497255 | 2014-05-01 22:26:55 -0500 | [diff] [blame] | 474 | error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED, |
| 475 | LM_FLAG_NOEXP | LM_FLAG_PRIORITY, |
| 476 | &thaw_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 477 | if (error) |
| 478 | goto fail_gunlock_ji; |
| 479 | |
David Teigland | e8ca5cc | 2012-01-09 14:40:06 -0500 | [diff] [blame] | 480 | if (test_bit(SDF_RORECOVERY, &sdp->sd_flags)) { |
| 481 | ro = 1; |
| 482 | } else if (test_bit(SDF_JOURNAL_CHECKED, &sdp->sd_flags)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 483 | if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) |
| 484 | ro = 1; |
| 485 | } else { |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 486 | if (sb_rdonly(sdp->sd_vfs)) { |
Abhijith Das | 7bc5c41 | 2008-01-18 14:06:37 -0600 | [diff] [blame] | 487 | /* 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 Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | if (ro) { |
Abhijith Das | 7bc5c41 | 2008-01-18 14:06:37 -0600 | [diff] [blame] | 499 | fs_warn(sdp, "jid=%u: Can't replay: read-only block " |
| 500 | "device\n", jd->jd_jid); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 501 | error = -EROFS; |
Benjamin Marzinski | 2497255 | 2014-05-01 22:26:55 -0500 | [diff] [blame] | 502 | goto fail_gunlock_thaw; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 503 | } |
| 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 Marzinski | 2497255 | 2014-05-01 22:26:55 -0500 | [diff] [blame] | 513 | goto fail_gunlock_thaw; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Bob Peterson | 588bff9 | 2017-12-18 12:48:29 -0600 | [diff] [blame^] | 516 | clean_journal(jd, &head); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 517 | |
Benjamin Marzinski | 2497255 | 2014-05-01 22:26:55 -0500 | [diff] [blame] | 518 | gfs2_glock_dq_uninit(&thaw_gh); |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 519 | t = DIV_ROUND_UP(jiffies - t, HZ); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 520 | fs_info(sdp, "jid=%u: Journal replayed in %lus\n", |
| 521 | jd->jd_jid, t); |
| 522 | } |
| 523 | |
Steven Whitehouse | f057f6c | 2009-01-12 10:43:39 +0000 | [diff] [blame] | 524 | gfs2_recovery_done(sdp, jd->jd_jid, LM_RD_SUCCESS); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 525 | |
Steven Whitehouse | c741c45 | 2010-09-29 14:20:52 +0100 | [diff] [blame] | 526 | if (jlocked) { |
| 527 | gfs2_glock_dq_uninit(&ji_gh); |
Steven Whitehouse | 5965b1f | 2006-04-26 13:21:55 -0400 | [diff] [blame] | 528 | gfs2_glock_dq_uninit(&j_gh); |
Steven Whitehouse | c741c45 | 2010-09-29 14:20:52 +0100 | [diff] [blame] | 529 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 530 | |
| 531 | fs_info(sdp, "jid=%u: Done\n", jd->jd_jid); |
Tejun Heo | 6ecd7c2 | 2010-07-20 22:09:02 +0200 | [diff] [blame] | 532 | goto done; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 533 | |
Benjamin Marzinski | 2497255 | 2014-05-01 22:26:55 -0500 | [diff] [blame] | 534 | fail_gunlock_thaw: |
| 535 | gfs2_glock_dq_uninit(&thaw_gh); |
Steven Whitehouse | 5965b1f | 2006-04-26 13:21:55 -0400 | [diff] [blame] | 536 | fail_gunlock_ji: |
Steven Whitehouse | c741c45 | 2010-09-29 14:20:52 +0100 | [diff] [blame] | 537 | if (jlocked) { |
Steven Whitehouse | 5965b1f | 2006-04-26 13:21:55 -0400 | [diff] [blame] | 538 | gfs2_glock_dq_uninit(&ji_gh); |
| 539 | fail_gunlock_j: |
| 540 | gfs2_glock_dq_uninit(&j_gh); |
| 541 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 542 | |
| 543 | fs_info(sdp, "jid=%u: %s\n", jd->jd_jid, (error) ? "Failed" : "Done"); |
Steven Whitehouse | 5965b1f | 2006-04-26 13:21:55 -0400 | [diff] [blame] | 544 | fail: |
David Teigland | 376d377 | 2012-01-09 15:29:20 -0500 | [diff] [blame] | 545 | jd->jd_recover_error = error; |
Steven Whitehouse | f057f6c | 2009-01-12 10:43:39 +0000 | [diff] [blame] | 546 | gfs2_recovery_done(sdp, jd->jd_jid, LM_RD_GAVEUP); |
Tejun Heo | 6ecd7c2 | 2010-07-20 22:09:02 +0200 | [diff] [blame] | 547 | done: |
| 548 | clear_bit(JDF_RECOVERY, &jd->jd_flags); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 549 | smp_mb__after_atomic(); |
Tejun Heo | 6ecd7c2 | 2010-07-20 22:09:02 +0200 | [diff] [blame] | 550 | wake_up_bit(&jd->jd_flags, JDF_RECOVERY); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Tejun Heo | 6ecd7c2 | 2010-07-20 22:09:02 +0200 | [diff] [blame] | 553 | int gfs2_recover_journal(struct gfs2_jdesc *jd, bool wait) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 554 | { |
Steven Whitehouse | fe64d51 | 2009-05-19 10:01:18 +0100 | [diff] [blame] | 555 | int rv; |
Tejun Heo | 6ecd7c2 | 2010-07-20 22:09:02 +0200 | [diff] [blame] | 556 | |
| 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) |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 565 | wait_on_bit(&jd->jd_flags, JDF_RECOVERY, |
Tejun Heo | 6ecd7c2 | 2010-07-20 22:09:02 +0200 | [diff] [blame] | 566 | TASK_UNINTERRUPTIBLE); |
| 567 | |
David Teigland | 376d377 | 2012-01-09 15:29:20 -0500 | [diff] [blame] | 568 | return wait ? jd->jd_recover_error : 0; |
Steven Whitehouse | 9ac1b4d | 2008-11-19 10:08:22 +0000 | [diff] [blame] | 569 | } |
| 570 | |