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 | a17d758b | 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 | a17d758b | 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 | a17d758b | 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 | a17d758b | 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 | a17d758b | 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 | a17d758b | 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 | a17d758b | 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 | |
| 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 | |
| 135 | static int get_log_header(struct gfs2_jdesc *jd, unsigned int blk, |
Al Viro | 5516762 | 2006-10-13 21:47:13 -0400 | [diff] [blame] | 136 | struct gfs2_log_header_host *head) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 137 | { |
Andreas Gruenbacher | 0ff5916 | 2018-01-16 23:07:57 +0100 | [diff] [blame^] | 138 | struct gfs2_log_header *lh; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 139 | struct buffer_head *bh; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 140 | u32 hash; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 141 | int error; |
| 142 | |
| 143 | error = gfs2_replay_read_block(jd, blk, &bh); |
| 144 | if (error) |
| 145 | return error; |
Andreas Gruenbacher | 0ff5916 | 2018-01-16 23:07:57 +0100 | [diff] [blame^] | 146 | lh = (void *)bh->b_data; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 147 | |
Andreas Gruenbacher | 0ff5916 | 2018-01-16 23:07:57 +0100 | [diff] [blame^] | 148 | 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 Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 156 | brelse(bh); |
| 157 | |
Andreas Gruenbacher | 0ff5916 | 2018-01-16 23:07:57 +0100 | [diff] [blame^] | 158 | 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 Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 165 | } |
| 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 | |
| 180 | static int find_good_lh(struct gfs2_jdesc *jd, unsigned int *blk, |
Al Viro | 5516762 | 2006-10-13 21:47:13 -0400 | [diff] [blame] | 181 | struct gfs2_log_header_host *head) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 182 | { |
| 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 Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 195 | gfs2_consist_inode(GFS2_I(jd->jd_inode)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 196 | 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 Viro | 5516762 | 2006-10-13 21:47:13 -0400 | [diff] [blame] | 212 | 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] | 213 | { |
| 214 | unsigned int blk = head->lh_blkno; |
Al Viro | 5516762 | 2006-10-13 21:47:13 -0400 | [diff] [blame] | 215 | struct gfs2_log_header_host lh; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 216 | 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 Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 229 | gfs2_consist_inode(GFS2_I(jd->jd_inode)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 230 | 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 Viro | 5516762 | 2006-10-13 21:47:13 -0400 | [diff] [blame] | 252 | 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] | 253 | { |
Al Viro | 5516762 | 2006-10-13 21:47:13 -0400 | [diff] [blame] | 254 | struct gfs2_log_header_host lh_1, lh_m; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 255 | u32 blk_1, blk_2, blk_m; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 256 | 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 | |
| 302 | static int foreach_descriptor(struct gfs2_jdesc *jd, unsigned int start, |
| 303 | unsigned int end, int pass) |
| 304 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 305 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 306 | 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 Whitehouse | a67cdbd | 2006-09-05 14:41:30 -0400 | [diff] [blame] | 312 | offset += sizeof(__be64) - 1; |
| 313 | offset &= ~(sizeof(__be64) - 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 314 | |
| 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 Whitehouse | e3167de | 2006-03-30 15:46:23 -0500 | [diff] [blame] | 326 | if (be32_to_cpu(ld->ld_header.mh_type) == GFS2_METATYPE_LH) { |
Al Viro | 5516762 | 2006-10-13 21:47:13 -0400 | [diff] [blame] | 327 | struct gfs2_log_header_host lh; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 328 | error = get_log_header(jd, start, &lh); |
| 329 | if (!error) { |
Bob Peterson | e1cb6be | 2016-07-21 13:02:44 -0500 | [diff] [blame] | 330 | gfs2_replay_incr_blk(jd, &start); |
Russell Cattelan | 8872187 | 2006-08-10 11:08:40 -0500 | [diff] [blame] | 331 | brelse(bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 332 | continue; |
| 333 | } |
| 334 | if (error == 1) { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 335 | gfs2_consist_inode(GFS2_I(jd->jd_inode)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 336 | 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 Peterson | e1cb6be | 2016-07-21 13:02:44 -0500 | [diff] [blame] | 352 | gfs2_replay_incr_blk(jd, &start); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 353 | |
| 354 | brelse(bh); |
| 355 | } |
| 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * clean_journal - mark a dirty journal as being clean |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 362 | * @jd: the journal |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 363 | * @head: the head journal to start from |
| 364 | * |
| 365 | * Returns: errno |
| 366 | */ |
| 367 | |
Bob Peterson | 588bff9 | 2017-12-18 12:48:29 -0600 | [diff] [blame] | 368 | static void clean_journal(struct gfs2_jdesc *jd, |
| 369 | struct gfs2_log_header_host *head) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 370 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 371 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 372 | |
Bob Peterson | 588bff9 | 2017-12-18 12:48:29 -0600 | [diff] [blame] | 373 | 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 Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Steven Whitehouse | da755fd | 2008-01-30 15:34:04 +0000 | [diff] [blame] | 380 | |
Steven Whitehouse | f057f6c | 2009-01-12 10:43:39 +0000 | [diff] [blame] | 381 | static void gfs2_recovery_done(struct gfs2_sbd *sdp, unsigned int jid, |
| 382 | unsigned int message) |
Steven Whitehouse | da755fd | 2008-01-30 15:34:04 +0000 | [diff] [blame] | 383 | { |
Steven Whitehouse | f057f6c | 2009-01-12 10:43:39 +0000 | [diff] [blame] | 384 | 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 Teigland | e0c2a9a | 2012-01-09 17:18:05 -0500 | [diff] [blame] | 388 | |
Steven Whitehouse | f057f6c | 2009-01-12 10:43:39 +0000 | [diff] [blame] | 389 | ls->ls_recover_jid_done = jid; |
| 390 | ls->ls_recover_jid_status = message; |
alex chen | 3566c96 | 2015-01-12 19:01:03 +0800 | [diff] [blame] | 391 | sprintf(env_jid, "JID=%u", jid); |
Steven Whitehouse | f057f6c | 2009-01-12 10:43:39 +0000 | [diff] [blame] | 392 | sprintf(env_status, "RECOVERY=%s", |
| 393 | message == LM_RD_SUCCESS ? "Done" : "Failed"); |
| 394 | kobject_uevent_env(&sdp->sd_kobj, KOBJ_CHANGE, envp); |
David Teigland | e0c2a9a | 2012-01-09 17:18:05 -0500 | [diff] [blame] | 395 | |
| 396 | if (sdp->sd_lockstruct.ls_ops->lm_recovery_result) |
| 397 | sdp->sd_lockstruct.ls_ops->lm_recovery_result(sdp, jid, message); |
Steven Whitehouse | da755fd | 2008-01-30 15:34:04 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Tejun Heo | 6ecd7c2 | 2010-07-20 22:09:02 +0200 | [diff] [blame] | 400 | void gfs2_recover_func(struct work_struct *work) |
Steven Whitehouse | fe64d51 | 2009-05-19 10:01:18 +0100 | [diff] [blame] | 401 | { |
| 402 | struct gfs2_jdesc *jd = container_of(work, struct gfs2_jdesc, jd_work); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 403 | struct gfs2_inode *ip = GFS2_I(jd->jd_inode); |
| 404 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
Al Viro | 5516762 | 2006-10-13 21:47:13 -0400 | [diff] [blame] | 405 | struct gfs2_log_header_host head; |
Benjamin Marzinski | 2497255 | 2014-05-01 22:26:55 -0500 | [diff] [blame] | 406 | struct gfs2_holder j_gh, ji_gh, thaw_gh; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 407 | unsigned long t; |
| 408 | int ro = 0; |
| 409 | unsigned int pass; |
| 410 | int error; |
Steven Whitehouse | c741c45 | 2010-09-29 14:20:52 +0100 | [diff] [blame] | 411 | int jlocked = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 412 | |
Steven Whitehouse | d0795f9 | 2010-09-27 15:58:11 +0100 | [diff] [blame] | 413 | if (sdp->sd_args.ar_spectator || |
| 414 | (jd->jd_jid != sdp->sd_lockstruct.ls_jid)) { |
Steven Whitehouse | 5965b1f | 2006-04-26 13:21:55 -0400 | [diff] [blame] | 415 | fs_info(sdp, "jid=%u: Trying to acquire journal lock...\n", |
| 416 | jd->jd_jid); |
Steven Whitehouse | c741c45 | 2010-09-29 14:20:52 +0100 | [diff] [blame] | 417 | jlocked = 1; |
Joe Perches | c78bad1 | 2008-02-03 17:33:42 +0200 | [diff] [blame] | 418 | /* Acquire the journal lock so we can do recovery */ |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 419 | |
Steven Whitehouse | 5965b1f | 2006-04-26 13:21:55 -0400 | [diff] [blame] | 420 | 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 Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 427 | |
Steven Whitehouse | 5965b1f | 2006-04-26 13:21:55 -0400 | [diff] [blame] | 428 | case GLR_TRYFAILED: |
| 429 | fs_info(sdp, "jid=%u: Busy\n", jd->jd_jid); |
| 430 | error = 0; |
Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 431 | |
Steven Whitehouse | 5965b1f | 2006-04-26 13:21:55 -0400 | [diff] [blame] | 432 | default: |
| 433 | goto fail; |
| 434 | }; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 435 | |
Steven Whitehouse | 5965b1f | 2006-04-26 13:21:55 -0400 | [diff] [blame] | 436 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, |
Bob Peterson | 75be73a | 2007-08-08 17:08:14 -0500 | [diff] [blame] | 437 | LM_FLAG_NOEXP | GL_NOCACHE, &ji_gh); |
Steven Whitehouse | 5965b1f | 2006-04-26 13:21:55 -0400 | [diff] [blame] | 438 | 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 Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 443 | |
| 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 Marzinski | 2497255 | 2014-05-01 22:26:55 -0500 | [diff] [blame] | 460 | /* Acquire a shared hold on the freeze lock */ |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 461 | |
Benjamin Marzinski | 2497255 | 2014-05-01 22:26:55 -0500 | [diff] [blame] | 462 | error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED, |
| 463 | LM_FLAG_NOEXP | LM_FLAG_PRIORITY, |
| 464 | &thaw_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 465 | if (error) |
| 466 | goto fail_gunlock_ji; |
| 467 | |
David Teigland | e8ca5cc | 2012-01-09 14:40:06 -0500 | [diff] [blame] | 468 | if (test_bit(SDF_RORECOVERY, &sdp->sd_flags)) { |
| 469 | ro = 1; |
| 470 | } else if (test_bit(SDF_JOURNAL_CHECKED, &sdp->sd_flags)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 471 | if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) |
| 472 | ro = 1; |
| 473 | } else { |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 474 | if (sb_rdonly(sdp->sd_vfs)) { |
Abhijith Das | 7bc5c41 | 2008-01-18 14:06:37 -0600 | [diff] [blame] | 475 | /* 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 Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | if (ro) { |
Abhijith Das | 7bc5c41 | 2008-01-18 14:06:37 -0600 | [diff] [blame] | 487 | fs_warn(sdp, "jid=%u: Can't replay: read-only block " |
| 488 | "device\n", jd->jd_jid); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 489 | error = -EROFS; |
Benjamin Marzinski | 2497255 | 2014-05-01 22:26:55 -0500 | [diff] [blame] | 490 | goto fail_gunlock_thaw; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 491 | } |
| 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 Marzinski | 2497255 | 2014-05-01 22:26:55 -0500 | [diff] [blame] | 501 | goto fail_gunlock_thaw; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Bob Peterson | 588bff9 | 2017-12-18 12:48:29 -0600 | [diff] [blame] | 504 | clean_journal(jd, &head); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 505 | |
Benjamin Marzinski | 2497255 | 2014-05-01 22:26:55 -0500 | [diff] [blame] | 506 | gfs2_glock_dq_uninit(&thaw_gh); |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 507 | t = DIV_ROUND_UP(jiffies - t, HZ); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 508 | fs_info(sdp, "jid=%u: Journal replayed in %lus\n", |
| 509 | jd->jd_jid, t); |
| 510 | } |
| 511 | |
Steven Whitehouse | f057f6c | 2009-01-12 10:43:39 +0000 | [diff] [blame] | 512 | gfs2_recovery_done(sdp, jd->jd_jid, LM_RD_SUCCESS); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 513 | |
Steven Whitehouse | c741c45 | 2010-09-29 14:20:52 +0100 | [diff] [blame] | 514 | if (jlocked) { |
| 515 | gfs2_glock_dq_uninit(&ji_gh); |
Steven Whitehouse | 5965b1f | 2006-04-26 13:21:55 -0400 | [diff] [blame] | 516 | gfs2_glock_dq_uninit(&j_gh); |
Steven Whitehouse | c741c45 | 2010-09-29 14:20:52 +0100 | [diff] [blame] | 517 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 518 | |
| 519 | fs_info(sdp, "jid=%u: Done\n", jd->jd_jid); |
Tejun Heo | 6ecd7c2 | 2010-07-20 22:09:02 +0200 | [diff] [blame] | 520 | goto done; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 521 | |
Benjamin Marzinski | 2497255 | 2014-05-01 22:26:55 -0500 | [diff] [blame] | 522 | fail_gunlock_thaw: |
| 523 | gfs2_glock_dq_uninit(&thaw_gh); |
Steven Whitehouse | 5965b1f | 2006-04-26 13:21:55 -0400 | [diff] [blame] | 524 | fail_gunlock_ji: |
Steven Whitehouse | c741c45 | 2010-09-29 14:20:52 +0100 | [diff] [blame] | 525 | if (jlocked) { |
Steven Whitehouse | 5965b1f | 2006-04-26 13:21:55 -0400 | [diff] [blame] | 526 | gfs2_glock_dq_uninit(&ji_gh); |
| 527 | fail_gunlock_j: |
| 528 | gfs2_glock_dq_uninit(&j_gh); |
| 529 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 530 | |
| 531 | 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] | 532 | fail: |
David Teigland | 376d377 | 2012-01-09 15:29:20 -0500 | [diff] [blame] | 533 | jd->jd_recover_error = error; |
Steven Whitehouse | f057f6c | 2009-01-12 10:43:39 +0000 | [diff] [blame] | 534 | gfs2_recovery_done(sdp, jd->jd_jid, LM_RD_GAVEUP); |
Tejun Heo | 6ecd7c2 | 2010-07-20 22:09:02 +0200 | [diff] [blame] | 535 | done: |
| 536 | clear_bit(JDF_RECOVERY, &jd->jd_flags); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 537 | smp_mb__after_atomic(); |
Tejun Heo | 6ecd7c2 | 2010-07-20 22:09:02 +0200 | [diff] [blame] | 538 | wake_up_bit(&jd->jd_flags, JDF_RECOVERY); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Tejun Heo | 6ecd7c2 | 2010-07-20 22:09:02 +0200 | [diff] [blame] | 541 | int gfs2_recover_journal(struct gfs2_jdesc *jd, bool wait) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 542 | { |
Steven Whitehouse | fe64d51 | 2009-05-19 10:01:18 +0100 | [diff] [blame] | 543 | int rv; |
Tejun Heo | 6ecd7c2 | 2010-07-20 22:09:02 +0200 | [diff] [blame] | 544 | |
| 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) |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 553 | wait_on_bit(&jd->jd_flags, JDF_RECOVERY, |
Tejun Heo | 6ecd7c2 | 2010-07-20 22:09:02 +0200 | [diff] [blame] | 554 | TASK_UNINTERRUPTIBLE); |
| 555 | |
David Teigland | 376d377 | 2012-01-09 15:29:20 -0500 | [diff] [blame] | 556 | return wait ? jd->jd_recover_error : 0; |
Steven Whitehouse | 9ac1b4d | 2008-11-19 10:08:22 +0000 | [diff] [blame] | 557 | } |
| 558 | |