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 | 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> |
Fabio Massimo Di Nitto | 7d30859 | 2006-09-19 07:56:29 +0200 | [diff] [blame] | 17 | #include <linux/lm_interface.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 18 | |
| 19 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 20 | #include "incore.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 21 | #include "bmap.h" |
| 22 | #include "glock.h" |
| 23 | #include "log.h" |
| 24 | #include "lops.h" |
| 25 | #include "meta_io.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 26 | #include "util.h" |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 27 | #include "dir.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 28 | |
| 29 | #define PULL 1 |
| 30 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 31 | /** |
| 32 | * gfs2_struct2blk - compute stuff |
| 33 | * @sdp: the filesystem |
| 34 | * @nstruct: the number of structures |
| 35 | * @ssize: the size of the structures |
| 36 | * |
| 37 | * Compute the number of log descriptor blocks needed to hold a certain number |
| 38 | * of structures of a certain size. |
| 39 | * |
| 40 | * Returns: the number of blocks needed (minimum is always 1) |
| 41 | */ |
| 42 | |
| 43 | unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct, |
| 44 | unsigned int ssize) |
| 45 | { |
| 46 | unsigned int blks; |
| 47 | unsigned int first, second; |
| 48 | |
| 49 | blks = 1; |
Steven Whitehouse | faa31ce | 2006-09-13 11:13:27 -0400 | [diff] [blame] | 50 | first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 51 | |
| 52 | if (nstruct > first) { |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 53 | second = (sdp->sd_sb.sb_bsize - |
| 54 | sizeof(struct gfs2_meta_header)) / ssize; |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 55 | blks += DIV_ROUND_UP(nstruct - first, second); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | return blks; |
| 59 | } |
| 60 | |
| 61 | void gfs2_ail1_start(struct gfs2_sbd *sdp, int flags) |
| 62 | { |
| 63 | struct list_head *head = &sdp->sd_ail1_list; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 64 | u64 sync_gen; |
Steven Whitehouse | 7466941 | 2006-09-19 11:17:38 -0400 | [diff] [blame^] | 65 | struct list_head *first; |
| 66 | struct gfs2_ail *first_ai, *ai, *tmp; |
| 67 | int done = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 68 | |
| 69 | gfs2_log_lock(sdp); |
| 70 | if (list_empty(head)) { |
| 71 | gfs2_log_unlock(sdp); |
| 72 | return; |
| 73 | } |
| 74 | sync_gen = sdp->sd_ail_sync_gen++; |
| 75 | |
| 76 | first = head->prev; |
| 77 | first_ai = list_entry(first, struct gfs2_ail, ai_list); |
| 78 | first_ai->ai_sync_gen = sync_gen; |
Steven Whitehouse | 7466941 | 2006-09-19 11:17:38 -0400 | [diff] [blame^] | 79 | gfs2_ail1_start_one(sdp, first_ai); /* This may drop log lock */ |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 80 | |
| 81 | if (flags & DIO_ALL) |
| 82 | first = NULL; |
| 83 | |
Steven Whitehouse | 7466941 | 2006-09-19 11:17:38 -0400 | [diff] [blame^] | 84 | while(!done) { |
Steven Whitehouse | 484adff | 2006-03-29 09:12:12 -0500 | [diff] [blame] | 85 | if (first && (head->prev != first || |
| 86 | gfs2_ail1_empty_one(sdp, first_ai, 0))) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 87 | break; |
| 88 | |
Steven Whitehouse | 7466941 | 2006-09-19 11:17:38 -0400 | [diff] [blame^] | 89 | done = 1; |
| 90 | list_for_each_entry_safe_reverse(ai, tmp, head, ai_list) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 91 | if (ai->ai_sync_gen >= sync_gen) |
| 92 | continue; |
| 93 | ai->ai_sync_gen = sync_gen; |
Steven Whitehouse | 7466941 | 2006-09-19 11:17:38 -0400 | [diff] [blame^] | 94 | gfs2_ail1_start_one(sdp, ai); /* This may drop log lock */ |
| 95 | done = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 96 | break; |
| 97 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | gfs2_log_unlock(sdp); |
| 101 | } |
| 102 | |
| 103 | int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags) |
| 104 | { |
| 105 | struct gfs2_ail *ai, *s; |
| 106 | int ret; |
| 107 | |
| 108 | gfs2_log_lock(sdp); |
| 109 | |
| 110 | list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) { |
| 111 | if (gfs2_ail1_empty_one(sdp, ai, flags)) |
| 112 | list_move(&ai->ai_list, &sdp->sd_ail2_list); |
| 113 | else if (!(flags & DIO_ALL)) |
| 114 | break; |
| 115 | } |
| 116 | |
| 117 | ret = list_empty(&sdp->sd_ail1_list); |
| 118 | |
| 119 | gfs2_log_unlock(sdp); |
| 120 | |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail) |
| 125 | { |
| 126 | struct gfs2_ail *ai, *safe; |
| 127 | unsigned int old_tail = sdp->sd_log_tail; |
| 128 | int wrap = (new_tail < old_tail); |
| 129 | int a, b, rm; |
| 130 | |
| 131 | gfs2_log_lock(sdp); |
| 132 | |
| 133 | list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) { |
| 134 | a = (old_tail <= ai->ai_first); |
| 135 | b = (ai->ai_first < new_tail); |
| 136 | rm = (wrap) ? (a || b) : (a && b); |
| 137 | if (!rm) |
| 138 | continue; |
| 139 | |
| 140 | gfs2_ail2_empty_one(sdp, ai); |
| 141 | list_del(&ai->ai_list); |
| 142 | gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list)); |
| 143 | gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list)); |
| 144 | kfree(ai); |
| 145 | } |
| 146 | |
| 147 | gfs2_log_unlock(sdp); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * gfs2_log_reserve - Make a log reservation |
| 152 | * @sdp: The GFS2 superblock |
| 153 | * @blks: The number of blocks to reserve |
| 154 | * |
| 155 | * Returns: errno |
| 156 | */ |
| 157 | |
| 158 | int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks) |
| 159 | { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 160 | unsigned int try = 0; |
| 161 | |
| 162 | if (gfs2_assert_warn(sdp, blks) || |
| 163 | gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks)) |
| 164 | return -EINVAL; |
| 165 | |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 166 | mutex_lock(&sdp->sd_log_reserve_mutex); |
Steven Whitehouse | 484adff | 2006-03-29 09:12:12 -0500 | [diff] [blame] | 167 | gfs2_log_lock(sdp); |
| 168 | while(sdp->sd_log_blks_free <= blks) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 169 | gfs2_log_unlock(sdp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 170 | gfs2_ail1_empty(sdp, 0); |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 171 | gfs2_log_flush(sdp, NULL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 172 | |
| 173 | if (try++) |
| 174 | gfs2_ail1_start(sdp, 0); |
Steven Whitehouse | 484adff | 2006-03-29 09:12:12 -0500 | [diff] [blame] | 175 | gfs2_log_lock(sdp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 176 | } |
Steven Whitehouse | 484adff | 2006-03-29 09:12:12 -0500 | [diff] [blame] | 177 | sdp->sd_log_blks_free -= blks; |
| 178 | gfs2_log_unlock(sdp); |
| 179 | mutex_unlock(&sdp->sd_log_reserve_mutex); |
| 180 | |
| 181 | down_read(&sdp->sd_log_flush_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * gfs2_log_release - Release a given number of log blocks |
| 188 | * @sdp: The GFS2 superblock |
| 189 | * @blks: The number of blocks |
| 190 | * |
| 191 | */ |
| 192 | |
| 193 | void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks) |
| 194 | { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 195 | |
| 196 | gfs2_log_lock(sdp); |
| 197 | sdp->sd_log_blks_free += blks; |
| 198 | gfs2_assert_withdraw(sdp, |
| 199 | sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks); |
| 200 | gfs2_log_unlock(sdp); |
Steven Whitehouse | ed38650 | 2006-04-07 16:28:07 -0400 | [diff] [blame] | 201 | up_read(&sdp->sd_log_flush_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 204 | static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 205 | { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 206 | int error; |
Steven Whitehouse | 7a6bbac | 2006-09-18 17:18:23 -0400 | [diff] [blame] | 207 | struct buffer_head bh_map; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 208 | |
Steven Whitehouse | 7a6bbac | 2006-09-18 17:18:23 -0400 | [diff] [blame] | 209 | error = gfs2_block_map(sdp->sd_jdesc->jd_inode, lbn, 0, &bh_map, 1); |
| 210 | if (error || !bh_map.b_blocknr) |
| 211 | printk(KERN_INFO "error=%d, dbn=%llu lbn=%u", error, bh_map.b_blocknr, lbn); |
| 212 | gfs2_assert_withdraw(sdp, !error && bh_map.b_blocknr); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 213 | |
Steven Whitehouse | 7a6bbac | 2006-09-18 17:18:23 -0400 | [diff] [blame] | 214 | return bh_map.b_blocknr; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /** |
| 218 | * log_distance - Compute distance between two journal blocks |
| 219 | * @sdp: The GFS2 superblock |
| 220 | * @newer: The most recent journal block of the pair |
| 221 | * @older: The older journal block of the pair |
| 222 | * |
| 223 | * Compute the distance (in the journal direction) between two |
| 224 | * blocks in the journal |
| 225 | * |
| 226 | * Returns: the distance in blocks |
| 227 | */ |
| 228 | |
Steven Whitehouse | faa31ce | 2006-09-13 11:13:27 -0400 | [diff] [blame] | 229 | static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 230 | unsigned int older) |
| 231 | { |
| 232 | int dist; |
| 233 | |
| 234 | dist = newer - older; |
| 235 | if (dist < 0) |
| 236 | dist += sdp->sd_jdesc->jd_blocks; |
| 237 | |
| 238 | return dist; |
| 239 | } |
| 240 | |
| 241 | static unsigned int current_tail(struct gfs2_sbd *sdp) |
| 242 | { |
| 243 | struct gfs2_ail *ai; |
| 244 | unsigned int tail; |
| 245 | |
| 246 | gfs2_log_lock(sdp); |
| 247 | |
Steven Whitehouse | faa31ce | 2006-09-13 11:13:27 -0400 | [diff] [blame] | 248 | if (list_empty(&sdp->sd_ail1_list)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 249 | tail = sdp->sd_log_head; |
Steven Whitehouse | faa31ce | 2006-09-13 11:13:27 -0400 | [diff] [blame] | 250 | } else { |
| 251 | ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 252 | tail = ai->ai_first; |
| 253 | } |
| 254 | |
| 255 | gfs2_log_unlock(sdp); |
| 256 | |
| 257 | return tail; |
| 258 | } |
| 259 | |
| 260 | static inline void log_incr_head(struct gfs2_sbd *sdp) |
| 261 | { |
| 262 | if (sdp->sd_log_flush_head == sdp->sd_log_tail) |
Steven Whitehouse | faa31ce | 2006-09-13 11:13:27 -0400 | [diff] [blame] | 263 | gfs2_assert_withdraw(sdp, sdp->sd_log_flush_head == sdp->sd_log_head); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 264 | |
| 265 | if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) { |
| 266 | sdp->sd_log_flush_head = 0; |
| 267 | sdp->sd_log_flush_wrapped = 1; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * gfs2_log_get_buf - Get and initialize a buffer to use for log control data |
| 273 | * @sdp: The GFS2 superblock |
| 274 | * |
| 275 | * Returns: the buffer_head |
| 276 | */ |
| 277 | |
| 278 | struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp) |
| 279 | { |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 280 | u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 281 | struct gfs2_log_buf *lb; |
| 282 | struct buffer_head *bh; |
| 283 | |
Steven Whitehouse | 4f3df04 | 2006-01-18 13:20:16 +0000 | [diff] [blame] | 284 | lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 285 | list_add(&lb->lb_list, &sdp->sd_log_flush_list); |
| 286 | |
| 287 | bh = lb->lb_bh = sb_getblk(sdp->sd_vfs, blkno); |
| 288 | lock_buffer(bh); |
| 289 | memset(bh->b_data, 0, bh->b_size); |
| 290 | set_buffer_uptodate(bh); |
| 291 | clear_buffer_dirty(bh); |
| 292 | unlock_buffer(bh); |
| 293 | |
| 294 | log_incr_head(sdp); |
| 295 | |
| 296 | return bh; |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log |
| 301 | * @sdp: the filesystem |
| 302 | * @data: the data the buffer_head should point to |
| 303 | * |
| 304 | * Returns: the log buffer descriptor |
| 305 | */ |
| 306 | |
| 307 | struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp, |
| 308 | struct buffer_head *real) |
| 309 | { |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 310 | u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 311 | struct gfs2_log_buf *lb; |
| 312 | struct buffer_head *bh; |
| 313 | |
Steven Whitehouse | 4f3df04 | 2006-01-18 13:20:16 +0000 | [diff] [blame] | 314 | lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 315 | list_add(&lb->lb_list, &sdp->sd_log_flush_list); |
| 316 | lb->lb_real = real; |
| 317 | |
| 318 | bh = lb->lb_bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL); |
| 319 | atomic_set(&bh->b_count, 1); |
| 320 | bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 321 | set_bh_page(bh, real->b_page, bh_offset(real)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 322 | bh->b_blocknr = blkno; |
| 323 | bh->b_size = sdp->sd_sb.sb_bsize; |
| 324 | bh->b_bdev = sdp->sd_vfs->s_bdev; |
| 325 | |
| 326 | log_incr_head(sdp); |
| 327 | |
| 328 | return bh; |
| 329 | } |
| 330 | |
| 331 | static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail, int pull) |
| 332 | { |
| 333 | unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail); |
| 334 | |
| 335 | ail2_empty(sdp, new_tail); |
| 336 | |
| 337 | gfs2_log_lock(sdp); |
Jan Engelhardt | c5392124 | 2006-09-05 14:30:40 +0200 | [diff] [blame] | 338 | sdp->sd_log_blks_free += dist - (pull ? 1 : 0); |
Steven Whitehouse | faa31ce | 2006-09-13 11:13:27 -0400 | [diff] [blame] | 339 | gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 340 | gfs2_log_unlock(sdp); |
| 341 | |
| 342 | sdp->sd_log_tail = new_tail; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * log_write_header - Get and initialize a journal header buffer |
| 347 | * @sdp: The GFS2 superblock |
| 348 | * |
| 349 | * Returns: the initialized log buffer descriptor |
| 350 | */ |
| 351 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 352 | static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 353 | { |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 354 | u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 355 | struct buffer_head *bh; |
| 356 | struct gfs2_log_header *lh; |
| 357 | unsigned int tail; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 358 | u32 hash; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 359 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 360 | bh = sb_getblk(sdp->sd_vfs, blkno); |
| 361 | lock_buffer(bh); |
| 362 | memset(bh->b_data, 0, bh->b_size); |
| 363 | set_buffer_uptodate(bh); |
| 364 | clear_buffer_dirty(bh); |
| 365 | unlock_buffer(bh); |
| 366 | |
| 367 | gfs2_ail1_empty(sdp, 0); |
| 368 | tail = current_tail(sdp); |
| 369 | |
| 370 | lh = (struct gfs2_log_header *)bh->b_data; |
| 371 | memset(lh, 0, sizeof(struct gfs2_log_header)); |
| 372 | lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC); |
Steven Whitehouse | e3167de | 2006-03-30 15:46:23 -0500 | [diff] [blame] | 373 | lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH); |
| 374 | lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH); |
Steven Whitehouse | e0f2bf7 | 2006-07-17 09:36:28 -0400 | [diff] [blame] | 375 | lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++); |
| 376 | lh->lh_flags = cpu_to_be32(flags); |
| 377 | lh->lh_tail = cpu_to_be32(tail); |
| 378 | lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 379 | hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header)); |
| 380 | lh->lh_hash = cpu_to_be32(hash); |
| 381 | |
| 382 | set_buffer_dirty(bh); |
| 383 | if (sync_dirty_buffer(bh)) |
| 384 | gfs2_io_error_bh(sdp, bh); |
| 385 | brelse(bh); |
| 386 | |
| 387 | if (sdp->sd_log_tail != tail) |
| 388 | log_pull_tail(sdp, tail, pull); |
| 389 | else |
| 390 | gfs2_assert_withdraw(sdp, !pull); |
| 391 | |
| 392 | sdp->sd_log_idle = (tail == sdp->sd_log_flush_head); |
| 393 | log_incr_head(sdp); |
| 394 | } |
| 395 | |
| 396 | static void log_flush_commit(struct gfs2_sbd *sdp) |
| 397 | { |
| 398 | struct list_head *head = &sdp->sd_log_flush_list; |
| 399 | struct gfs2_log_buf *lb; |
| 400 | struct buffer_head *bh; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 401 | |
| 402 | while (!list_empty(head)) { |
| 403 | lb = list_entry(head->next, struct gfs2_log_buf, lb_list); |
| 404 | list_del(&lb->lb_list); |
| 405 | bh = lb->lb_bh; |
| 406 | |
| 407 | wait_on_buffer(bh); |
| 408 | if (!buffer_uptodate(bh)) |
| 409 | gfs2_io_error_bh(sdp, bh); |
| 410 | if (lb->lb_real) { |
| 411 | while (atomic_read(&bh->b_count) != 1) /* Grrrr... */ |
| 412 | schedule(); |
| 413 | free_buffer_head(bh); |
| 414 | } else |
| 415 | brelse(bh); |
| 416 | kfree(lb); |
| 417 | } |
| 418 | |
| 419 | log_write_header(sdp, 0, 0); |
| 420 | } |
| 421 | |
| 422 | /** |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 423 | * gfs2_log_flush - flush incore transaction(s) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 424 | * @sdp: the filesystem |
| 425 | * @gl: The glock structure to flush. If NULL, flush the whole incore log |
| 426 | * |
| 427 | */ |
| 428 | |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 429 | void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 430 | { |
| 431 | struct gfs2_ail *ai; |
| 432 | |
Steven Whitehouse | 484adff | 2006-03-29 09:12:12 -0500 | [diff] [blame] | 433 | down_write(&sdp->sd_log_flush_lock); |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 434 | |
| 435 | if (gl) { |
| 436 | gfs2_log_lock(sdp); |
| 437 | if (list_empty(&gl->gl_le.le_list)) { |
| 438 | gfs2_log_unlock(sdp); |
Steven Whitehouse | 484adff | 2006-03-29 09:12:12 -0500 | [diff] [blame] | 439 | up_write(&sdp->sd_log_flush_lock); |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 440 | return; |
| 441 | } |
| 442 | gfs2_log_unlock(sdp); |
| 443 | } |
| 444 | |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 445 | ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL); |
| 446 | INIT_LIST_HEAD(&ai->ai_ail1_list); |
| 447 | INIT_LIST_HEAD(&ai->ai_ail2_list); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 448 | |
Steven Whitehouse | faa31ce | 2006-09-13 11:13:27 -0400 | [diff] [blame] | 449 | gfs2_assert_withdraw(sdp, sdp->sd_log_num_buf == sdp->sd_log_commited_buf); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 450 | gfs2_assert_withdraw(sdp, |
| 451 | sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke); |
| 452 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 453 | sdp->sd_log_flush_head = sdp->sd_log_head; |
| 454 | sdp->sd_log_flush_wrapped = 0; |
| 455 | ai->ai_first = sdp->sd_log_flush_head; |
| 456 | |
| 457 | lops_before_commit(sdp); |
| 458 | if (!list_empty(&sdp->sd_log_flush_list)) |
| 459 | log_flush_commit(sdp); |
| 460 | else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle) |
| 461 | log_write_header(sdp, 0, PULL); |
| 462 | lops_after_commit(sdp, ai); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 463 | sdp->sd_log_head = sdp->sd_log_flush_head; |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 464 | |
Steven Whitehouse | f4154ea | 2006-04-11 14:49:06 -0400 | [diff] [blame] | 465 | sdp->sd_log_blks_free -= sdp->sd_log_num_hdrs; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 466 | |
Steven Whitehouse | faa31ce | 2006-09-13 11:13:27 -0400 | [diff] [blame] | 467 | sdp->sd_log_blks_reserved = 0; |
| 468 | sdp->sd_log_commited_buf = 0; |
| 469 | sdp->sd_log_num_hdrs = 0; |
| 470 | sdp->sd_log_commited_revoke = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 471 | |
| 472 | gfs2_log_lock(sdp); |
| 473 | if (!list_empty(&ai->ai_ail1_list)) { |
| 474 | list_add(&ai->ai_list, &sdp->sd_ail1_list); |
| 475 | ai = NULL; |
| 476 | } |
| 477 | gfs2_log_unlock(sdp); |
| 478 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 479 | sdp->sd_vfs->s_dirt = 0; |
Steven Whitehouse | 484adff | 2006-03-29 09:12:12 -0500 | [diff] [blame] | 480 | up_write(&sdp->sd_log_flush_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 481 | |
| 482 | kfree(ai); |
| 483 | } |
| 484 | |
| 485 | static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr) |
| 486 | { |
Benjamin Marzinski | 5dc39fe | 2006-08-24 14:47:17 -0500 | [diff] [blame] | 487 | unsigned int reserved = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 488 | unsigned int old; |
| 489 | |
| 490 | gfs2_log_lock(sdp); |
| 491 | |
| 492 | sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm; |
| 493 | gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_buf) >= 0); |
| 494 | sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm; |
| 495 | gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0); |
| 496 | |
| 497 | if (sdp->sd_log_commited_buf) |
Steven Whitehouse | f4154ea | 2006-04-11 14:49:06 -0400 | [diff] [blame] | 498 | reserved += sdp->sd_log_commited_buf; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 499 | if (sdp->sd_log_commited_revoke) |
| 500 | reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke, |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 501 | sizeof(u64)); |
Benjamin Marzinski | 5dc39fe | 2006-08-24 14:47:17 -0500 | [diff] [blame] | 502 | if (reserved) |
| 503 | reserved++; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 504 | |
| 505 | old = sdp->sd_log_blks_free; |
| 506 | sdp->sd_log_blks_free += tr->tr_reserved - |
| 507 | (reserved - sdp->sd_log_blks_reserved); |
| 508 | |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 509 | gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free >= old); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 510 | gfs2_assert_withdraw(sdp, |
Steven Whitehouse | f4154ea | 2006-04-11 14:49:06 -0400 | [diff] [blame] | 511 | sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks + |
| 512 | sdp->sd_log_num_hdrs); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 513 | |
| 514 | sdp->sd_log_blks_reserved = reserved; |
| 515 | |
| 516 | gfs2_log_unlock(sdp); |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * gfs2_log_commit - Commit a transaction to the log |
| 521 | * @sdp: the filesystem |
| 522 | * @tr: the transaction |
| 523 | * |
| 524 | * Returns: errno |
| 525 | */ |
| 526 | |
| 527 | void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr) |
| 528 | { |
| 529 | log_refund(sdp, tr); |
| 530 | lops_incore_commit(sdp, tr); |
| 531 | |
| 532 | sdp->sd_vfs->s_dirt = 1; |
Steven Whitehouse | 484adff | 2006-03-29 09:12:12 -0500 | [diff] [blame] | 533 | up_read(&sdp->sd_log_flush_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 534 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 535 | gfs2_log_lock(sdp); |
| 536 | if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks)) { |
| 537 | gfs2_log_unlock(sdp); |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 538 | gfs2_log_flush(sdp, NULL); |
Steven Whitehouse | faa31ce | 2006-09-13 11:13:27 -0400 | [diff] [blame] | 539 | } else { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 540 | gfs2_log_unlock(sdp); |
Steven Whitehouse | faa31ce | 2006-09-13 11:13:27 -0400 | [diff] [blame] | 541 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /** |
| 545 | * gfs2_log_shutdown - write a shutdown header into a journal |
| 546 | * @sdp: the filesystem |
| 547 | * |
| 548 | */ |
| 549 | |
| 550 | void gfs2_log_shutdown(struct gfs2_sbd *sdp) |
| 551 | { |
Steven Whitehouse | 484adff | 2006-03-29 09:12:12 -0500 | [diff] [blame] | 552 | down_write(&sdp->sd_log_flush_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 553 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 554 | gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved); |
| 555 | gfs2_assert_withdraw(sdp, !sdp->sd_log_num_gl); |
| 556 | gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 557 | gfs2_assert_withdraw(sdp, !sdp->sd_log_num_jdata); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 558 | gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke); |
| 559 | gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg); |
| 560 | gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf); |
Steven Whitehouse | 190562b | 2006-04-20 16:57:23 -0400 | [diff] [blame] | 561 | gfs2_assert_withdraw(sdp, !sdp->sd_log_num_hdrs); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 562 | gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list)); |
| 563 | |
| 564 | sdp->sd_log_flush_head = sdp->sd_log_head; |
| 565 | sdp->sd_log_flush_wrapped = 0; |
| 566 | |
| 567 | log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT, 0); |
| 568 | |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 569 | gfs2_assert_warn(sdp, sdp->sd_log_blks_free == sdp->sd_jdesc->jd_blocks); |
| 570 | gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail); |
| 571 | gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 572 | |
| 573 | sdp->sd_log_head = sdp->sd_log_flush_head; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 574 | sdp->sd_log_tail = sdp->sd_log_head; |
| 575 | |
Steven Whitehouse | 484adff | 2006-03-29 09:12:12 -0500 | [diff] [blame] | 576 | up_write(&sdp->sd_log_flush_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 577 | } |
| 578 | |