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