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