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 | |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/spinlock.h> |
| 13 | #include <linux/completion.h> |
| 14 | #include <linux/buffer_head.h> |
Steven Whitehouse | 75ca61c | 2012-03-08 12:10:23 +0000 | [diff] [blame] | 15 | #include <linux/mempool.h> |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 16 | #include <linux/gfs2_ondisk.h> |
Steven Whitehouse | c969f58 | 2009-04-07 14:13:01 +0100 | [diff] [blame] | 17 | #include <linux/bio.h> |
| 18 | #include <linux/fs.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 19 | |
| 20 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 21 | #include "incore.h" |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 22 | #include "inode.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 23 | #include "glock.h" |
| 24 | #include "log.h" |
| 25 | #include "lops.h" |
| 26 | #include "meta_io.h" |
| 27 | #include "recovery.h" |
| 28 | #include "rgrp.h" |
| 29 | #include "trans.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 30 | #include "util.h" |
Steven Whitehouse | 6399777 | 2009-06-12 08:49:20 +0100 | [diff] [blame] | 31 | #include "trace_gfs2.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 32 | |
Steven Whitehouse | 9b9107a | 2007-08-27 13:54:05 +0100 | [diff] [blame] | 33 | /** |
| 34 | * gfs2_pin - Pin a buffer in memory |
| 35 | * @sdp: The superblock |
| 36 | * @bh: The buffer to be pinned |
| 37 | * |
| 38 | * The log lock must be held when calling this function |
| 39 | */ |
| 40 | static void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh) |
| 41 | { |
| 42 | struct gfs2_bufdata *bd; |
| 43 | |
Steven Whitehouse | 29687a2 | 2011-03-30 16:33:25 +0100 | [diff] [blame] | 44 | BUG_ON(!current->journal_info); |
Steven Whitehouse | 9b9107a | 2007-08-27 13:54:05 +0100 | [diff] [blame] | 45 | |
| 46 | clear_buffer_dirty(bh); |
| 47 | if (test_set_buffer_pinned(bh)) |
| 48 | gfs2_assert_withdraw(sdp, 0); |
| 49 | if (!buffer_uptodate(bh)) |
| 50 | gfs2_io_error_bh(sdp, bh); |
| 51 | bd = bh->b_private; |
| 52 | /* If this buffer is in the AIL and it has already been written |
| 53 | * to in-place disk block, remove it from the AIL. |
| 54 | */ |
Steven Whitehouse | c618e87 | 2011-03-14 12:40:29 +0000 | [diff] [blame] | 55 | spin_lock(&sdp->sd_ail_lock); |
Steven Whitehouse | 9b9107a | 2007-08-27 13:54:05 +0100 | [diff] [blame] | 56 | if (bd->bd_ail) |
| 57 | list_move(&bd->bd_ail_st_list, &bd->bd_ail->ai_ail2_list); |
Steven Whitehouse | c618e87 | 2011-03-14 12:40:29 +0000 | [diff] [blame] | 58 | spin_unlock(&sdp->sd_ail_lock); |
Steven Whitehouse | 9b9107a | 2007-08-27 13:54:05 +0100 | [diff] [blame] | 59 | get_bh(bh); |
Benjamin Marzinski | 5e687ea | 2010-05-04 14:29:16 -0500 | [diff] [blame] | 60 | atomic_inc(&sdp->sd_log_pinned); |
Steven Whitehouse | 6399777 | 2009-06-12 08:49:20 +0100 | [diff] [blame] | 61 | trace_gfs2_pin(bd, 1); |
Steven Whitehouse | 9b9107a | 2007-08-27 13:54:05 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 64 | static bool buffer_is_rgrp(const struct gfs2_bufdata *bd) |
| 65 | { |
| 66 | return bd->bd_gl->gl_name.ln_type == LM_TYPE_RGRP; |
| 67 | } |
| 68 | |
| 69 | static void maybe_release_space(struct gfs2_bufdata *bd) |
| 70 | { |
| 71 | struct gfs2_glock *gl = bd->bd_gl; |
| 72 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 73 | struct gfs2_rgrpd *rgd = gl->gl_object; |
| 74 | unsigned int index = bd->bd_bh->b_blocknr - gl->gl_name.ln_number; |
| 75 | struct gfs2_bitmap *bi = rgd->rd_bits + index; |
| 76 | |
| 77 | if (bi->bi_clone == 0) |
| 78 | return; |
| 79 | if (sdp->sd_args.ar_discard) |
Steven Whitehouse | 66fc061 | 2012-02-08 12:58:32 +0000 | [diff] [blame] | 80 | gfs2_rgrp_send_discards(sdp, rgd->rd_data0, bd->bd_bh, bi, 1, NULL); |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 81 | memcpy(bi->bi_clone + bi->bi_offset, |
| 82 | bd->bd_bh->b_data + bi->bi_offset, bi->bi_len); |
| 83 | clear_bit(GBF_FULL, &bi->bi_flags); |
| 84 | rgd->rd_free_clone = rgd->rd_free; |
| 85 | } |
| 86 | |
Steven Whitehouse | 9b9107a | 2007-08-27 13:54:05 +0100 | [diff] [blame] | 87 | /** |
| 88 | * gfs2_unpin - Unpin a buffer |
| 89 | * @sdp: the filesystem the buffer belongs to |
| 90 | * @bh: The buffer to unpin |
| 91 | * @ai: |
Steven Whitehouse | 29687a2 | 2011-03-30 16:33:25 +0100 | [diff] [blame] | 92 | * @flags: The inode dirty flags |
Steven Whitehouse | 9b9107a | 2007-08-27 13:54:05 +0100 | [diff] [blame] | 93 | * |
| 94 | */ |
| 95 | |
| 96 | static void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh, |
| 97 | struct gfs2_ail *ai) |
| 98 | { |
| 99 | struct gfs2_bufdata *bd = bh->b_private; |
| 100 | |
Steven Whitehouse | 29687a2 | 2011-03-30 16:33:25 +0100 | [diff] [blame] | 101 | BUG_ON(!buffer_uptodate(bh)); |
| 102 | BUG_ON(!buffer_pinned(bh)); |
Steven Whitehouse | 9b9107a | 2007-08-27 13:54:05 +0100 | [diff] [blame] | 103 | |
| 104 | lock_buffer(bh); |
| 105 | mark_buffer_dirty(bh); |
| 106 | clear_buffer_pinned(bh); |
| 107 | |
Bob Peterson | 7c9ca62 | 2011-08-31 09:53:19 +0100 | [diff] [blame] | 108 | if (buffer_is_rgrp(bd)) |
| 109 | maybe_release_space(bd); |
| 110 | |
Dave Chinner | d6a079e | 2011-03-11 11:52:25 +0000 | [diff] [blame] | 111 | spin_lock(&sdp->sd_ail_lock); |
Steven Whitehouse | 9b9107a | 2007-08-27 13:54:05 +0100 | [diff] [blame] | 112 | if (bd->bd_ail) { |
| 113 | list_del(&bd->bd_ail_st_list); |
| 114 | brelse(bh); |
| 115 | } else { |
| 116 | struct gfs2_glock *gl = bd->bd_gl; |
| 117 | list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list); |
| 118 | atomic_inc(&gl->gl_ail_count); |
| 119 | } |
| 120 | bd->bd_ail = ai; |
| 121 | list_add(&bd->bd_ail_st_list, &ai->ai_ail1_list); |
Dave Chinner | d6a079e | 2011-03-11 11:52:25 +0000 | [diff] [blame] | 122 | spin_unlock(&sdp->sd_ail_lock); |
| 123 | |
Steven Whitehouse | 29687a2 | 2011-03-30 16:33:25 +0100 | [diff] [blame] | 124 | clear_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags); |
Steven Whitehouse | 6399777 | 2009-06-12 08:49:20 +0100 | [diff] [blame] | 125 | trace_gfs2_pin(bd, 0); |
Steven Whitehouse | 9b9107a | 2007-08-27 13:54:05 +0100 | [diff] [blame] | 126 | unlock_buffer(bh); |
Benjamin Marzinski | 5e687ea | 2010-05-04 14:29:16 -0500 | [diff] [blame] | 127 | atomic_dec(&sdp->sd_log_pinned); |
Steven Whitehouse | 9b9107a | 2007-08-27 13:54:05 +0100 | [diff] [blame] | 128 | } |
| 129 | |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 130 | |
| 131 | static inline struct gfs2_log_descriptor *bh_log_desc(struct buffer_head *bh) |
| 132 | { |
| 133 | return (struct gfs2_log_descriptor *)bh->b_data; |
| 134 | } |
| 135 | |
| 136 | static inline __be64 *bh_log_ptr(struct buffer_head *bh) |
| 137 | { |
| 138 | struct gfs2_log_descriptor *ld = bh_log_desc(bh); |
| 139 | return (__force __be64 *)(ld + 1); |
| 140 | } |
| 141 | |
| 142 | static inline __be64 *bh_ptr_end(struct buffer_head *bh) |
| 143 | { |
| 144 | return (__force __be64 *)(bh->b_data + bh->b_size); |
| 145 | } |
| 146 | |
Steven Whitehouse | 47ac553 | 2012-02-03 15:21:59 +0000 | [diff] [blame] | 147 | /** |
| 148 | * gfs2_log_write_endio - End of I/O for a log buffer |
| 149 | * @bh: The buffer head |
| 150 | * @uptodate: I/O Status |
| 151 | * |
| 152 | */ |
| 153 | |
| 154 | static void gfs2_log_write_endio(struct buffer_head *bh, int uptodate) |
| 155 | { |
| 156 | struct gfs2_sbd *sdp = bh->b_private; |
| 157 | bh->b_private = NULL; |
| 158 | |
| 159 | end_buffer_write_sync(bh, uptodate); |
| 160 | if (atomic_dec_and_test(&sdp->sd_log_in_flight)) |
| 161 | wake_up(&sdp->sd_log_flush_wait); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * gfs2_log_get_buf - Get and initialize a buffer to use for log control data |
| 166 | * @sdp: The GFS2 superblock |
| 167 | * |
| 168 | * tReturns: the buffer_head |
| 169 | */ |
| 170 | |
| 171 | static struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp) |
| 172 | { |
| 173 | u64 blkno = gfs2_log_bmap(sdp, sdp->sd_log_flush_head); |
| 174 | struct buffer_head *bh; |
| 175 | |
| 176 | bh = sb_getblk(sdp->sd_vfs, blkno); |
| 177 | lock_buffer(bh); |
| 178 | memset(bh->b_data, 0, bh->b_size); |
| 179 | set_buffer_uptodate(bh); |
| 180 | clear_buffer_dirty(bh); |
| 181 | gfs2_log_incr_head(sdp); |
| 182 | atomic_inc(&sdp->sd_log_in_flight); |
| 183 | bh->b_private = sdp; |
| 184 | bh->b_end_io = gfs2_log_write_endio; |
| 185 | |
| 186 | return bh; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * gfs2_fake_write_endio - |
| 191 | * @bh: The buffer head |
| 192 | * @uptodate: The I/O Status |
| 193 | * |
| 194 | */ |
| 195 | |
| 196 | static void gfs2_fake_write_endio(struct buffer_head *bh, int uptodate) |
| 197 | { |
| 198 | struct buffer_head *real_bh = bh->b_private; |
| 199 | struct gfs2_bufdata *bd = real_bh->b_private; |
| 200 | struct gfs2_sbd *sdp = bd->bd_gl->gl_sbd; |
| 201 | |
| 202 | end_buffer_write_sync(bh, uptodate); |
Steven Whitehouse | 75ca61c | 2012-03-08 12:10:23 +0000 | [diff] [blame] | 203 | mempool_free(bh, gfs2_bh_pool); |
Steven Whitehouse | 47ac553 | 2012-02-03 15:21:59 +0000 | [diff] [blame] | 204 | unlock_buffer(real_bh); |
| 205 | brelse(real_bh); |
| 206 | if (atomic_dec_and_test(&sdp->sd_log_in_flight)) |
| 207 | wake_up(&sdp->sd_log_flush_wait); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log |
| 212 | * @sdp: the filesystem |
| 213 | * @data: the data the buffer_head should point to |
| 214 | * |
| 215 | * Returns: the log buffer descriptor |
| 216 | */ |
| 217 | |
| 218 | static struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp, |
| 219 | struct buffer_head *real) |
| 220 | { |
| 221 | u64 blkno = gfs2_log_bmap(sdp, sdp->sd_log_flush_head); |
| 222 | struct buffer_head *bh; |
| 223 | |
Steven Whitehouse | 75ca61c | 2012-03-08 12:10:23 +0000 | [diff] [blame] | 224 | bh = mempool_alloc(gfs2_bh_pool, GFP_NOFS); |
Steven Whitehouse | 47ac553 | 2012-02-03 15:21:59 +0000 | [diff] [blame] | 225 | atomic_set(&bh->b_count, 1); |
| 226 | bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate) | (1 << BH_Lock); |
| 227 | set_bh_page(bh, real->b_page, bh_offset(real)); |
| 228 | bh->b_blocknr = blkno; |
| 229 | bh->b_size = sdp->sd_sb.sb_bsize; |
| 230 | bh->b_bdev = sdp->sd_vfs->s_bdev; |
| 231 | bh->b_private = real; |
| 232 | bh->b_end_io = gfs2_fake_write_endio; |
| 233 | |
| 234 | gfs2_log_incr_head(sdp); |
| 235 | atomic_inc(&sdp->sd_log_in_flight); |
| 236 | |
| 237 | return bh; |
| 238 | } |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 239 | |
| 240 | static struct buffer_head *gfs2_get_log_desc(struct gfs2_sbd *sdp, u32 ld_type) |
| 241 | { |
| 242 | struct buffer_head *bh = gfs2_log_get_buf(sdp); |
| 243 | struct gfs2_log_descriptor *ld = bh_log_desc(bh); |
| 244 | ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC); |
| 245 | ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD); |
| 246 | ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD); |
| 247 | ld->ld_type = cpu_to_be32(ld_type); |
| 248 | ld->ld_length = 0; |
| 249 | ld->ld_data1 = 0; |
| 250 | ld->ld_data2 = 0; |
| 251 | memset(ld->ld_reserved, 0, sizeof(ld->ld_reserved)); |
| 252 | return bh; |
| 253 | } |
| 254 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 255 | static void buf_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le) |
| 256 | { |
| 257 | struct gfs2_bufdata *bd = container_of(le, struct gfs2_bufdata, bd_le); |
Steven Whitehouse | 0ab7d13 | 2009-11-06 16:20:51 +0000 | [diff] [blame] | 258 | struct gfs2_meta_header *mh; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 259 | struct gfs2_trans *tr; |
| 260 | |
Steven Whitehouse | 9b9107a | 2007-08-27 13:54:05 +0100 | [diff] [blame] | 261 | if (!list_empty(&bd->bd_list_tr)) |
Benjamin Marzinski | 84bd6d1 | 2012-11-07 00:38:06 -0600 | [diff] [blame] | 262 | return; |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 263 | tr = current->journal_info; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 264 | tr->tr_touched = 1; |
| 265 | tr->tr_num_buf++; |
| 266 | list_add(&bd->bd_list_tr, &tr->tr_list_buf); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 267 | if (!list_empty(&le->le_list)) |
Benjamin Marzinski | 84bd6d1 | 2012-11-07 00:38:06 -0600 | [diff] [blame] | 268 | return; |
Steven Whitehouse | 2bcd610 | 2007-11-08 14:25:12 +0000 | [diff] [blame] | 269 | set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags); |
| 270 | set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 271 | gfs2_meta_check(sdp, bd->bd_bh); |
Steven Whitehouse | a98ab22 | 2006-01-18 13:38:44 +0000 | [diff] [blame] | 272 | gfs2_pin(sdp, bd->bd_bh); |
Steven Whitehouse | 0ab7d13 | 2009-11-06 16:20:51 +0000 | [diff] [blame] | 273 | mh = (struct gfs2_meta_header *)bd->bd_bh->b_data; |
| 274 | mh->__pad0 = cpu_to_be64(0); |
| 275 | mh->mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 276 | sdp->sd_log_num_buf++; |
| 277 | list_add(&le->le_list, &sdp->sd_log_le_buf); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 278 | tr->tr_num_buf_new++; |
| 279 | } |
| 280 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 281 | static void buf_lo_before_commit(struct gfs2_sbd *sdp) |
| 282 | { |
| 283 | struct buffer_head *bh; |
| 284 | struct gfs2_log_descriptor *ld; |
| 285 | struct gfs2_bufdata *bd1 = NULL, *bd2; |
Bob Peterson | 905d2ae | 2007-07-24 14:05:31 -0500 | [diff] [blame] | 286 | unsigned int total; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 287 | unsigned int limit; |
| 288 | unsigned int num; |
| 289 | unsigned n; |
| 290 | __be64 *ptr; |
| 291 | |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 292 | limit = buf_limit(sdp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 293 | /* for 4k blocks, limit = 503 */ |
| 294 | |
Bob Peterson | 905d2ae | 2007-07-24 14:05:31 -0500 | [diff] [blame] | 295 | gfs2_log_lock(sdp); |
| 296 | total = sdp->sd_log_num_buf; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 297 | bd1 = bd2 = list_prepare_entry(bd1, &sdp->sd_log_le_buf, bd_le.le_list); |
| 298 | while(total) { |
| 299 | num = total; |
| 300 | if (total > limit) |
| 301 | num = limit; |
Bob Peterson | 905d2ae | 2007-07-24 14:05:31 -0500 | [diff] [blame] | 302 | gfs2_log_unlock(sdp); |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 303 | bh = gfs2_get_log_desc(sdp, GFS2_LOG_DESC_METADATA); |
Bob Peterson | 905d2ae | 2007-07-24 14:05:31 -0500 | [diff] [blame] | 304 | gfs2_log_lock(sdp); |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 305 | ld = bh_log_desc(bh); |
| 306 | ptr = bh_log_ptr(bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 307 | ld->ld_length = cpu_to_be32(num + 1); |
| 308 | ld->ld_data1 = cpu_to_be32(num); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 309 | |
| 310 | n = 0; |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 311 | list_for_each_entry_continue(bd1, &sdp->sd_log_le_buf, |
| 312 | bd_le.le_list) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 313 | *ptr++ = cpu_to_be64(bd1->bd_bh->b_blocknr); |
| 314 | if (++n >= num) |
| 315 | break; |
| 316 | } |
| 317 | |
Bob Peterson | 905d2ae | 2007-07-24 14:05:31 -0500 | [diff] [blame] | 318 | gfs2_log_unlock(sdp); |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 319 | submit_bh(WRITE_SYNC, bh); |
Bob Peterson | 905d2ae | 2007-07-24 14:05:31 -0500 | [diff] [blame] | 320 | gfs2_log_lock(sdp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 321 | |
| 322 | n = 0; |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 323 | list_for_each_entry_continue(bd2, &sdp->sd_log_le_buf, |
| 324 | bd_le.le_list) { |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 325 | get_bh(bd2->bd_bh); |
Bob Peterson | 905d2ae | 2007-07-24 14:05:31 -0500 | [diff] [blame] | 326 | gfs2_log_unlock(sdp); |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 327 | lock_buffer(bd2->bd_bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 328 | bh = gfs2_log_fake_buf(sdp, bd2->bd_bh); |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 329 | submit_bh(WRITE_SYNC, bh); |
Bob Peterson | 905d2ae | 2007-07-24 14:05:31 -0500 | [diff] [blame] | 330 | gfs2_log_lock(sdp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 331 | if (++n >= num) |
| 332 | break; |
| 333 | } |
| 334 | |
Bob Peterson | 905d2ae | 2007-07-24 14:05:31 -0500 | [diff] [blame] | 335 | BUG_ON(total < num); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 336 | total -= num; |
| 337 | } |
Bob Peterson | 905d2ae | 2007-07-24 14:05:31 -0500 | [diff] [blame] | 338 | gfs2_log_unlock(sdp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | static void buf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai) |
| 342 | { |
| 343 | struct list_head *head = &sdp->sd_log_le_buf; |
| 344 | struct gfs2_bufdata *bd; |
| 345 | |
| 346 | while (!list_empty(head)) { |
| 347 | bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list); |
| 348 | list_del_init(&bd->bd_le.le_list); |
| 349 | sdp->sd_log_num_buf--; |
| 350 | |
Steven Whitehouse | a98ab22 | 2006-01-18 13:38:44 +0000 | [diff] [blame] | 351 | gfs2_unpin(sdp, bd->bd_bh, ai); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 352 | } |
| 353 | gfs2_assert_warn(sdp, !sdp->sd_log_num_buf); |
| 354 | } |
| 355 | |
| 356 | static void buf_lo_before_scan(struct gfs2_jdesc *jd, |
Al Viro | 5516762 | 2006-10-13 21:47:13 -0400 | [diff] [blame] | 357 | struct gfs2_log_header_host *head, int pass) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 358 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 359 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 360 | |
| 361 | if (pass != 0) |
| 362 | return; |
| 363 | |
| 364 | sdp->sd_found_blocks = 0; |
| 365 | sdp->sd_replayed_blocks = 0; |
| 366 | } |
| 367 | |
| 368 | static int buf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start, |
| 369 | struct gfs2_log_descriptor *ld, __be64 *ptr, |
| 370 | int pass) |
| 371 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 372 | struct gfs2_inode *ip = GFS2_I(jd->jd_inode); |
| 373 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 374 | struct gfs2_glock *gl = ip->i_gl; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 375 | unsigned int blks = be32_to_cpu(ld->ld_data1); |
| 376 | struct buffer_head *bh_log, *bh_ip; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 377 | u64 blkno; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 378 | int error = 0; |
| 379 | |
| 380 | if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_METADATA) |
| 381 | return 0; |
| 382 | |
| 383 | gfs2_replay_incr_blk(sdp, &start); |
| 384 | |
| 385 | for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) { |
| 386 | blkno = be64_to_cpu(*ptr++); |
| 387 | |
| 388 | sdp->sd_found_blocks++; |
| 389 | |
| 390 | if (gfs2_revoke_check(sdp, blkno, start)) |
| 391 | continue; |
| 392 | |
| 393 | error = gfs2_replay_read_block(jd, start, &bh_log); |
Steven Whitehouse | 82ffa51 | 2006-09-04 14:47:06 -0400 | [diff] [blame] | 394 | if (error) |
| 395 | return error; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 396 | |
| 397 | bh_ip = gfs2_meta_new(gl, blkno); |
| 398 | memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size); |
| 399 | |
| 400 | if (gfs2_meta_check(sdp, bh_ip)) |
| 401 | error = -EIO; |
| 402 | else |
| 403 | mark_buffer_dirty(bh_ip); |
| 404 | |
| 405 | brelse(bh_log); |
| 406 | brelse(bh_ip); |
| 407 | |
| 408 | if (error) |
| 409 | break; |
| 410 | |
| 411 | sdp->sd_replayed_blocks++; |
| 412 | } |
| 413 | |
| 414 | return error; |
| 415 | } |
| 416 | |
| 417 | static void buf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass) |
| 418 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 419 | struct gfs2_inode *ip = GFS2_I(jd->jd_inode); |
| 420 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 421 | |
| 422 | if (error) { |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 423 | gfs2_meta_sync(ip->i_gl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 424 | return; |
| 425 | } |
| 426 | if (pass != 1) |
| 427 | return; |
| 428 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 429 | gfs2_meta_sync(ip->i_gl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 430 | |
| 431 | fs_info(sdp, "jid=%u: Replayed %u of %u blocks\n", |
| 432 | jd->jd_jid, sdp->sd_replayed_blocks, sdp->sd_found_blocks); |
| 433 | } |
| 434 | |
| 435 | static void revoke_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le) |
| 436 | { |
Steven Whitehouse | f42ab08 | 2011-04-14 16:50:31 +0100 | [diff] [blame] | 437 | struct gfs2_bufdata *bd = container_of(le, struct gfs2_bufdata, bd_le); |
| 438 | struct gfs2_glock *gl = bd->bd_gl; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 439 | struct gfs2_trans *tr; |
| 440 | |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 441 | tr = current->journal_info; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 442 | tr->tr_touched = 1; |
| 443 | tr->tr_num_revoke++; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 444 | sdp->sd_log_num_revoke++; |
Steven Whitehouse | f42ab08 | 2011-04-14 16:50:31 +0100 | [diff] [blame] | 445 | atomic_inc(&gl->gl_revokes); |
| 446 | set_bit(GLF_LFLUSH, &gl->gl_flags); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 447 | list_add(&le->le_list, &sdp->sd_log_le_revoke); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | static void revoke_lo_before_commit(struct gfs2_sbd *sdp) |
| 451 | { |
| 452 | struct gfs2_log_descriptor *ld; |
| 453 | struct gfs2_meta_header *mh; |
| 454 | struct buffer_head *bh; |
| 455 | unsigned int offset; |
| 456 | struct list_head *head = &sdp->sd_log_le_revoke; |
Steven Whitehouse | 82e8608 | 2007-09-02 15:39:43 +0100 | [diff] [blame] | 457 | struct gfs2_bufdata *bd; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 458 | |
| 459 | if (!sdp->sd_log_num_revoke) |
| 460 | return; |
| 461 | |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 462 | bh = gfs2_get_log_desc(sdp, GFS2_LOG_DESC_REVOKE); |
| 463 | ld = bh_log_desc(bh); |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 464 | ld->ld_length = cpu_to_be32(gfs2_struct2blk(sdp, sdp->sd_log_num_revoke, |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 465 | sizeof(u64))); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 466 | ld->ld_data1 = cpu_to_be32(sdp->sd_log_num_revoke); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 467 | offset = sizeof(struct gfs2_log_descriptor); |
| 468 | |
Steven Whitehouse | f42ab08 | 2011-04-14 16:50:31 +0100 | [diff] [blame] | 469 | list_for_each_entry(bd, head, bd_le.le_list) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 470 | sdp->sd_log_num_revoke--; |
| 471 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 472 | if (offset + sizeof(u64) > sdp->sd_sb.sb_bsize) { |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 473 | submit_bh(WRITE_SYNC, bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 474 | |
| 475 | bh = gfs2_log_get_buf(sdp); |
| 476 | mh = (struct gfs2_meta_header *)bh->b_data; |
| 477 | mh->mh_magic = cpu_to_be32(GFS2_MAGIC); |
Steven Whitehouse | e3167de | 2006-03-30 15:46:23 -0500 | [diff] [blame] | 478 | mh->mh_type = cpu_to_be32(GFS2_METATYPE_LB); |
| 479 | mh->mh_format = cpu_to_be32(GFS2_FORMAT_LB); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 480 | offset = sizeof(struct gfs2_meta_header); |
| 481 | } |
| 482 | |
Steven Whitehouse | 82e8608 | 2007-09-02 15:39:43 +0100 | [diff] [blame] | 483 | *(__be64 *)(bh->b_data + offset) = cpu_to_be64(bd->bd_blkno); |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 484 | offset += sizeof(u64); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 485 | } |
| 486 | gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke); |
| 487 | |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 488 | submit_bh(WRITE_SYNC, bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Steven Whitehouse | f42ab08 | 2011-04-14 16:50:31 +0100 | [diff] [blame] | 491 | static void revoke_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai) |
| 492 | { |
| 493 | struct list_head *head = &sdp->sd_log_le_revoke; |
| 494 | struct gfs2_bufdata *bd; |
| 495 | struct gfs2_glock *gl; |
| 496 | |
| 497 | while (!list_empty(head)) { |
| 498 | bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list); |
| 499 | list_del_init(&bd->bd_le.le_list); |
| 500 | gl = bd->bd_gl; |
| 501 | atomic_dec(&gl->gl_revokes); |
| 502 | clear_bit(GLF_LFLUSH, &gl->gl_flags); |
| 503 | kmem_cache_free(gfs2_bufdata_cachep, bd); |
| 504 | } |
| 505 | } |
| 506 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 507 | static void revoke_lo_before_scan(struct gfs2_jdesc *jd, |
Al Viro | 5516762 | 2006-10-13 21:47:13 -0400 | [diff] [blame] | 508 | struct gfs2_log_header_host *head, int pass) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 509 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 510 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 511 | |
| 512 | if (pass != 0) |
| 513 | return; |
| 514 | |
| 515 | sdp->sd_found_revokes = 0; |
| 516 | sdp->sd_replay_tail = head->lh_tail; |
| 517 | } |
| 518 | |
| 519 | static int revoke_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start, |
| 520 | struct gfs2_log_descriptor *ld, __be64 *ptr, |
| 521 | int pass) |
| 522 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 523 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 524 | unsigned int blks = be32_to_cpu(ld->ld_length); |
| 525 | unsigned int revokes = be32_to_cpu(ld->ld_data1); |
| 526 | struct buffer_head *bh; |
| 527 | unsigned int offset; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 528 | u64 blkno; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 529 | int first = 1; |
| 530 | int error; |
| 531 | |
| 532 | if (pass != 0 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_REVOKE) |
| 533 | return 0; |
| 534 | |
| 535 | offset = sizeof(struct gfs2_log_descriptor); |
| 536 | |
| 537 | for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) { |
| 538 | error = gfs2_replay_read_block(jd, start, &bh); |
| 539 | if (error) |
| 540 | return error; |
| 541 | |
| 542 | if (!first) |
| 543 | gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LB); |
| 544 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 545 | while (offset + sizeof(u64) <= sdp->sd_sb.sb_bsize) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 546 | blkno = be64_to_cpu(*(__be64 *)(bh->b_data + offset)); |
| 547 | |
| 548 | error = gfs2_revoke_add(sdp, blkno, start); |
Bob Peterson | 3ad62e8 | 2008-01-28 16:35:13 -0600 | [diff] [blame] | 549 | if (error < 0) { |
| 550 | brelse(bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 551 | return error; |
Bob Peterson | 3ad62e8 | 2008-01-28 16:35:13 -0600 | [diff] [blame] | 552 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 553 | else if (error) |
| 554 | sdp->sd_found_revokes++; |
| 555 | |
| 556 | if (!--revokes) |
| 557 | break; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 558 | offset += sizeof(u64); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | brelse(bh); |
| 562 | offset = sizeof(struct gfs2_meta_header); |
| 563 | first = 0; |
| 564 | } |
| 565 | |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | static void revoke_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass) |
| 570 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 571 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 572 | |
| 573 | if (error) { |
| 574 | gfs2_revoke_clean(sdp); |
| 575 | return; |
| 576 | } |
| 577 | if (pass != 1) |
| 578 | return; |
| 579 | |
| 580 | fs_info(sdp, "jid=%u: Found %u revoke tags\n", |
| 581 | jd->jd_jid, sdp->sd_found_revokes); |
| 582 | |
| 583 | gfs2_revoke_clean(sdp); |
| 584 | } |
| 585 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 586 | /** |
| 587 | * databuf_lo_add - Add a databuf to the transaction. |
| 588 | * |
| 589 | * This is used in two distinct cases: |
| 590 | * i) In ordered write mode |
| 591 | * We put the data buffer on a list so that we can ensure that its |
| 592 | * synced to disk at the right time |
| 593 | * ii) In journaled data mode |
| 594 | * We need to journal the data block in the same way as metadata in |
| 595 | * the functions above. The difference is that here we have a tag |
| 596 | * which is two __be64's being the block number (as per meta data) |
| 597 | * and a flag which says whether the data block needs escaping or |
| 598 | * not. This means we need a new log entry for each 251 or so data |
| 599 | * blocks, which isn't an enormous overhead but twice as much as |
| 600 | * for normal metadata blocks. |
| 601 | */ |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 602 | static void databuf_lo_add(struct gfs2_sbd *sdp, struct gfs2_log_element *le) |
| 603 | { |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 604 | struct gfs2_bufdata *bd = container_of(le, struct gfs2_bufdata, bd_le); |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 605 | struct gfs2_trans *tr = current->journal_info; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 606 | struct address_space *mapping = bd->bd_bh->b_page->mapping; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 607 | struct gfs2_inode *ip = GFS2_I(mapping->host); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 608 | |
Steven Whitehouse | 9ff8ec3 | 2007-09-28 13:49:05 +0100 | [diff] [blame] | 609 | if (tr) { |
| 610 | if (!list_empty(&bd->bd_list_tr)) |
Benjamin Marzinski | 84bd6d1 | 2012-11-07 00:38:06 -0600 | [diff] [blame] | 611 | return; |
Steven Whitehouse | 9ff8ec3 | 2007-09-28 13:49:05 +0100 | [diff] [blame] | 612 | tr->tr_touched = 1; |
| 613 | if (gfs2_is_jdata(ip)) { |
| 614 | tr->tr_num_buf++; |
| 615 | list_add(&bd->bd_list_tr, &tr->tr_list_buf); |
| 616 | } |
Robert Peterson | 773ed1a | 2007-06-20 08:34:06 -0500 | [diff] [blame] | 617 | } |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 618 | if (!list_empty(&le->le_list)) |
Benjamin Marzinski | 84bd6d1 | 2012-11-07 00:38:06 -0600 | [diff] [blame] | 619 | return; |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 620 | |
Steven Whitehouse | 2bcd610 | 2007-11-08 14:25:12 +0000 | [diff] [blame] | 621 | set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags); |
| 622 | set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags); |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 623 | if (gfs2_is_jdata(ip)) { |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 624 | gfs2_pin(sdp, bd->bd_bh); |
| 625 | tr->tr_num_databuf_new++; |
Steven Whitehouse | d7b616e | 2007-09-02 10:48:13 +0100 | [diff] [blame] | 626 | sdp->sd_log_num_databuf++; |
Dave Chinner | e588463 | 2010-02-05 16:45:25 +1100 | [diff] [blame] | 627 | list_add_tail(&le->le_list, &sdp->sd_log_le_databuf); |
Steven Whitehouse | d7b616e | 2007-09-02 10:48:13 +0100 | [diff] [blame] | 628 | } else { |
Dave Chinner | e588463 | 2010-02-05 16:45:25 +1100 | [diff] [blame] | 629 | list_add_tail(&le->le_list, &sdp->sd_log_le_ordered); |
Steven Whitehouse | 9b9107a | 2007-08-27 13:54:05 +0100 | [diff] [blame] | 630 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 633 | static void gfs2_check_magic(struct buffer_head *bh) |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 634 | { |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 635 | void *kaddr; |
| 636 | __be32 *ptr; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 637 | |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 638 | clear_buffer_escaped(bh); |
Cong Wang | d934928 | 2011-11-25 23:14:30 +0800 | [diff] [blame] | 639 | kaddr = kmap_atomic(bh->b_page); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 640 | ptr = kaddr + bh_offset(bh); |
| 641 | if (*ptr == cpu_to_be32(GFS2_MAGIC)) |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 642 | set_buffer_escaped(bh); |
Cong Wang | d934928 | 2011-11-25 23:14:30 +0800 | [diff] [blame] | 643 | kunmap_atomic(kaddr); |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 644 | } |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 645 | |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 646 | static void gfs2_write_blocks(struct gfs2_sbd *sdp, struct buffer_head *bh, |
| 647 | struct list_head *list, struct list_head *done, |
| 648 | unsigned int n) |
| 649 | { |
| 650 | struct buffer_head *bh1; |
| 651 | struct gfs2_log_descriptor *ld; |
| 652 | struct gfs2_bufdata *bd; |
| 653 | __be64 *ptr; |
| 654 | |
| 655 | if (!bh) |
| 656 | return; |
| 657 | |
| 658 | ld = bh_log_desc(bh); |
| 659 | ld->ld_length = cpu_to_be32(n + 1); |
| 660 | ld->ld_data1 = cpu_to_be32(n); |
| 661 | |
| 662 | ptr = bh_log_ptr(bh); |
| 663 | |
| 664 | get_bh(bh); |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 665 | submit_bh(WRITE_SYNC, bh); |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 666 | gfs2_log_lock(sdp); |
| 667 | while(!list_empty(list)) { |
| 668 | bd = list_entry(list->next, struct gfs2_bufdata, bd_le.le_list); |
| 669 | list_move_tail(&bd->bd_le.le_list, done); |
| 670 | get_bh(bd->bd_bh); |
| 671 | while (be64_to_cpu(*ptr) != bd->bd_bh->b_blocknr) { |
| 672 | gfs2_log_incr_head(sdp); |
| 673 | ptr += 2; |
| 674 | } |
| 675 | gfs2_log_unlock(sdp); |
| 676 | lock_buffer(bd->bd_bh); |
| 677 | if (buffer_escaped(bd->bd_bh)) { |
| 678 | void *kaddr; |
| 679 | bh1 = gfs2_log_get_buf(sdp); |
Cong Wang | d934928 | 2011-11-25 23:14:30 +0800 | [diff] [blame] | 680 | kaddr = kmap_atomic(bd->bd_bh->b_page); |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 681 | memcpy(bh1->b_data, kaddr + bh_offset(bd->bd_bh), |
| 682 | bh1->b_size); |
Cong Wang | d934928 | 2011-11-25 23:14:30 +0800 | [diff] [blame] | 683 | kunmap_atomic(kaddr); |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 684 | *(__be32 *)bh1->b_data = 0; |
| 685 | clear_buffer_escaped(bd->bd_bh); |
| 686 | unlock_buffer(bd->bd_bh); |
| 687 | brelse(bd->bd_bh); |
| 688 | } else { |
| 689 | bh1 = gfs2_log_fake_buf(sdp, bd->bd_bh); |
| 690 | } |
Jens Axboe | 721a960 | 2011-03-09 11:56:30 +0100 | [diff] [blame] | 691 | submit_bh(WRITE_SYNC, bh1); |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 692 | gfs2_log_lock(sdp); |
| 693 | ptr += 2; |
| 694 | } |
| 695 | gfs2_log_unlock(sdp); |
| 696 | brelse(bh); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | /** |
| 700 | * databuf_lo_before_commit - Scan the data buffers, writing as we go |
| 701 | * |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 702 | */ |
Steven Whitehouse | d7b616e | 2007-09-02 10:48:13 +0100 | [diff] [blame] | 703 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 704 | static void databuf_lo_before_commit(struct gfs2_sbd *sdp) |
| 705 | { |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 706 | struct gfs2_bufdata *bd = NULL; |
| 707 | struct buffer_head *bh = NULL; |
| 708 | unsigned int n = 0; |
| 709 | __be64 *ptr = NULL, *end = NULL; |
| 710 | LIST_HEAD(processed); |
| 711 | LIST_HEAD(in_progress); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 712 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 713 | gfs2_log_lock(sdp); |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 714 | while (!list_empty(&sdp->sd_log_le_databuf)) { |
| 715 | if (ptr == end) { |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 716 | gfs2_log_unlock(sdp); |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 717 | gfs2_write_blocks(sdp, bh, &in_progress, &processed, n); |
| 718 | n = 0; |
| 719 | bh = gfs2_get_log_desc(sdp, GFS2_LOG_DESC_JDATA); |
| 720 | ptr = bh_log_ptr(bh); |
| 721 | end = bh_ptr_end(bh) - 1; |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 722 | gfs2_log_lock(sdp); |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 723 | continue; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 724 | } |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 725 | bd = list_entry(sdp->sd_log_le_databuf.next, struct gfs2_bufdata, bd_le.le_list); |
| 726 | list_move_tail(&bd->bd_le.le_list, &in_progress); |
| 727 | gfs2_check_magic(bd->bd_bh); |
| 728 | *ptr++ = cpu_to_be64(bd->bd_bh->b_blocknr); |
| 729 | *ptr++ = cpu_to_be64(buffer_escaped(bh) ? 1 : 0); |
| 730 | n++; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 731 | } |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 732 | gfs2_log_unlock(sdp); |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 733 | gfs2_write_blocks(sdp, bh, &in_progress, &processed, n); |
| 734 | gfs2_log_lock(sdp); |
| 735 | list_splice(&processed, &sdp->sd_log_le_databuf); |
| 736 | gfs2_log_unlock(sdp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 739 | static int databuf_lo_scan_elements(struct gfs2_jdesc *jd, unsigned int start, |
| 740 | struct gfs2_log_descriptor *ld, |
| 741 | __be64 *ptr, int pass) |
| 742 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 743 | struct gfs2_inode *ip = GFS2_I(jd->jd_inode); |
| 744 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 745 | struct gfs2_glock *gl = ip->i_gl; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 746 | unsigned int blks = be32_to_cpu(ld->ld_data1); |
| 747 | struct buffer_head *bh_log, *bh_ip; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 748 | u64 blkno; |
| 749 | u64 esc; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 750 | int error = 0; |
| 751 | |
| 752 | if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_JDATA) |
| 753 | return 0; |
| 754 | |
| 755 | gfs2_replay_incr_blk(sdp, &start); |
| 756 | for (; blks; gfs2_replay_incr_blk(sdp, &start), blks--) { |
| 757 | blkno = be64_to_cpu(*ptr++); |
| 758 | esc = be64_to_cpu(*ptr++); |
| 759 | |
| 760 | sdp->sd_found_blocks++; |
| 761 | |
| 762 | if (gfs2_revoke_check(sdp, blkno, start)) |
| 763 | continue; |
| 764 | |
| 765 | error = gfs2_replay_read_block(jd, start, &bh_log); |
| 766 | if (error) |
| 767 | return error; |
| 768 | |
| 769 | bh_ip = gfs2_meta_new(gl, blkno); |
| 770 | memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size); |
| 771 | |
| 772 | /* Unescape */ |
| 773 | if (esc) { |
| 774 | __be32 *eptr = (__be32 *)bh_ip->b_data; |
| 775 | *eptr = cpu_to_be32(GFS2_MAGIC); |
| 776 | } |
| 777 | mark_buffer_dirty(bh_ip); |
| 778 | |
| 779 | brelse(bh_log); |
| 780 | brelse(bh_ip); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 781 | |
| 782 | sdp->sd_replayed_blocks++; |
| 783 | } |
| 784 | |
| 785 | return error; |
| 786 | } |
| 787 | |
| 788 | /* FIXME: sort out accounting for log blocks etc. */ |
| 789 | |
| 790 | static void databuf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass) |
| 791 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 792 | struct gfs2_inode *ip = GFS2_I(jd->jd_inode); |
| 793 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 794 | |
| 795 | if (error) { |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 796 | gfs2_meta_sync(ip->i_gl); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 797 | return; |
| 798 | } |
| 799 | if (pass != 1) |
| 800 | return; |
| 801 | |
| 802 | /* data sync? */ |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 803 | gfs2_meta_sync(ip->i_gl); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 804 | |
| 805 | fs_info(sdp, "jid=%u: Replayed %u of %u data blocks\n", |
| 806 | jd->jd_jid, sdp->sd_replayed_blocks, sdp->sd_found_blocks); |
| 807 | } |
| 808 | |
| 809 | static void databuf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_ail *ai) |
| 810 | { |
| 811 | struct list_head *head = &sdp->sd_log_le_databuf; |
| 812 | struct gfs2_bufdata *bd; |
| 813 | |
| 814 | while (!list_empty(head)) { |
| 815 | bd = list_entry(head->next, struct gfs2_bufdata, bd_le.le_list); |
Steven Whitehouse | b8e1aab | 2006-08-22 16:25:50 -0400 | [diff] [blame] | 816 | list_del_init(&bd->bd_le.le_list); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 817 | sdp->sd_log_num_databuf--; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 818 | gfs2_unpin(sdp, bd->bd_bh, ai); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 819 | } |
| 820 | gfs2_assert_warn(sdp, !sdp->sd_log_num_databuf); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 824 | const struct gfs2_log_operations gfs2_buf_lops = { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 825 | .lo_add = buf_lo_add, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 826 | .lo_before_commit = buf_lo_before_commit, |
| 827 | .lo_after_commit = buf_lo_after_commit, |
| 828 | .lo_before_scan = buf_lo_before_scan, |
| 829 | .lo_scan_elements = buf_lo_scan_elements, |
| 830 | .lo_after_scan = buf_lo_after_scan, |
Steven Whitehouse | ea67eed | 2006-09-05 10:53:09 -0400 | [diff] [blame] | 831 | .lo_name = "buf", |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 832 | }; |
| 833 | |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 834 | const struct gfs2_log_operations gfs2_revoke_lops = { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 835 | .lo_add = revoke_lo_add, |
| 836 | .lo_before_commit = revoke_lo_before_commit, |
Steven Whitehouse | f42ab08 | 2011-04-14 16:50:31 +0100 | [diff] [blame] | 837 | .lo_after_commit = revoke_lo_after_commit, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 838 | .lo_before_scan = revoke_lo_before_scan, |
| 839 | .lo_scan_elements = revoke_lo_scan_elements, |
| 840 | .lo_after_scan = revoke_lo_after_scan, |
Steven Whitehouse | ea67eed | 2006-09-05 10:53:09 -0400 | [diff] [blame] | 841 | .lo_name = "revoke", |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 842 | }; |
| 843 | |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 844 | const struct gfs2_log_operations gfs2_rg_lops = { |
Steven Whitehouse | ea67eed | 2006-09-05 10:53:09 -0400 | [diff] [blame] | 845 | .lo_name = "rg", |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 846 | }; |
| 847 | |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 848 | const struct gfs2_log_operations gfs2_databuf_lops = { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 849 | .lo_add = databuf_lo_add, |
| 850 | .lo_before_commit = databuf_lo_before_commit, |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 851 | .lo_after_commit = databuf_lo_after_commit, |
| 852 | .lo_scan_elements = databuf_lo_scan_elements, |
| 853 | .lo_after_scan = databuf_lo_after_scan, |
Steven Whitehouse | ea67eed | 2006-09-05 10:53:09 -0400 | [diff] [blame] | 854 | .lo_name = "databuf", |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 855 | }; |
| 856 | |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 857 | const struct gfs2_log_operations *gfs2_log_ops[] = { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 858 | &gfs2_databuf_lops, |
Steven Whitehouse | 16615be | 2007-09-17 10:59:52 +0100 | [diff] [blame] | 859 | &gfs2_buf_lops, |
| 860 | &gfs2_rg_lops, |
| 861 | &gfs2_revoke_lops, |
Steven Whitehouse | ea67eed | 2006-09-05 10:53:09 -0400 | [diff] [blame] | 862 | NULL, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 863 | }; |
| 864 | |