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> |
Steven Whitehouse | a25311c | 2006-11-23 11:06:35 -0500 | [diff] [blame] | 18 | #include <linux/delay.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" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 22 | #include "bmap.h" |
| 23 | #include "glock.h" |
| 24 | #include "log.h" |
| 25 | #include "lops.h" |
| 26 | #include "meta_io.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 27 | #include "util.h" |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 28 | #include "dir.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 29 | |
| 30 | #define PULL 1 |
| 31 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 32 | /** |
| 33 | * gfs2_struct2blk - compute stuff |
| 34 | * @sdp: the filesystem |
| 35 | * @nstruct: the number of structures |
| 36 | * @ssize: the size of the structures |
| 37 | * |
| 38 | * Compute the number of log descriptor blocks needed to hold a certain number |
| 39 | * of structures of a certain size. |
| 40 | * |
| 41 | * Returns: the number of blocks needed (minimum is always 1) |
| 42 | */ |
| 43 | |
| 44 | unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct, |
| 45 | unsigned int ssize) |
| 46 | { |
| 47 | unsigned int blks; |
| 48 | unsigned int first, second; |
| 49 | |
| 50 | blks = 1; |
Steven Whitehouse | faa31ce | 2006-09-13 11:13:27 -0400 | [diff] [blame] | 51 | first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / 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 | |
Steven Whitehouse | ddacfaf | 2006-10-03 11:10:41 -0400 | [diff] [blame] | 62 | /** |
Steven Whitehouse | 1e1a3d0 | 2007-08-27 09:45:26 +0100 | [diff] [blame] | 63 | * gfs2_remove_from_ail - Remove an entry from the ail lists, updating counters |
| 64 | * @mapping: The associated mapping (maybe NULL) |
| 65 | * @bd: The gfs2_bufdata to remove |
| 66 | * |
| 67 | * The log lock _must_ be held when calling this function |
| 68 | * |
| 69 | */ |
| 70 | |
| 71 | void gfs2_remove_from_ail(struct address_space *mapping, struct gfs2_bufdata *bd) |
| 72 | { |
| 73 | bd->bd_ail = NULL; |
Steven Whitehouse | 1ad38c4 | 2007-09-03 11:01:33 +0100 | [diff] [blame^] | 74 | list_del_init(&bd->bd_ail_st_list); |
| 75 | list_del_init(&bd->bd_ail_gl_list); |
Steven Whitehouse | 1e1a3d0 | 2007-08-27 09:45:26 +0100 | [diff] [blame] | 76 | atomic_dec(&bd->bd_gl->gl_ail_count); |
| 77 | if (mapping) |
| 78 | gfs2_meta_cache_flush(GFS2_I(mapping->host)); |
| 79 | brelse(bd->bd_bh); |
| 80 | } |
| 81 | |
| 82 | /** |
Steven Whitehouse | ddacfaf | 2006-10-03 11:10:41 -0400 | [diff] [blame] | 83 | * gfs2_ail1_start_one - Start I/O on a part of the AIL |
| 84 | * @sdp: the filesystem |
| 85 | * @tr: the part of the AIL |
| 86 | * |
| 87 | */ |
| 88 | |
| 89 | static void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai) |
| 90 | { |
| 91 | struct gfs2_bufdata *bd, *s; |
| 92 | struct buffer_head *bh; |
| 93 | int retry; |
| 94 | |
| 95 | BUG_ON(!spin_is_locked(&sdp->sd_log_lock)); |
| 96 | |
| 97 | do { |
| 98 | retry = 0; |
| 99 | |
| 100 | list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list, |
| 101 | bd_ail_st_list) { |
| 102 | bh = bd->bd_bh; |
| 103 | |
| 104 | gfs2_assert(sdp, bd->bd_ail == ai); |
| 105 | |
| 106 | if (!buffer_busy(bh)) { |
| 107 | if (!buffer_uptodate(bh)) { |
| 108 | gfs2_log_unlock(sdp); |
| 109 | gfs2_io_error_bh(sdp, bh); |
| 110 | gfs2_log_lock(sdp); |
| 111 | } |
| 112 | list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list); |
| 113 | continue; |
| 114 | } |
| 115 | |
| 116 | if (!buffer_dirty(bh)) |
| 117 | continue; |
| 118 | |
| 119 | list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list); |
| 120 | |
| 121 | gfs2_log_unlock(sdp); |
| 122 | wait_on_buffer(bh); |
| 123 | ll_rw_block(WRITE, 1, &bh); |
| 124 | gfs2_log_lock(sdp); |
| 125 | |
| 126 | retry = 1; |
| 127 | break; |
| 128 | } |
| 129 | } while (retry); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced |
| 134 | * @sdp: the filesystem |
| 135 | * @ai: the AIL entry |
| 136 | * |
| 137 | */ |
| 138 | |
| 139 | static int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags) |
| 140 | { |
| 141 | struct gfs2_bufdata *bd, *s; |
| 142 | struct buffer_head *bh; |
| 143 | |
| 144 | list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list, |
| 145 | bd_ail_st_list) { |
| 146 | bh = bd->bd_bh; |
| 147 | |
| 148 | gfs2_assert(sdp, bd->bd_ail == ai); |
| 149 | |
| 150 | if (buffer_busy(bh)) { |
| 151 | if (flags & DIO_ALL) |
| 152 | continue; |
| 153 | else |
| 154 | break; |
| 155 | } |
| 156 | |
| 157 | if (!buffer_uptodate(bh)) |
| 158 | gfs2_io_error_bh(sdp, bh); |
| 159 | |
| 160 | list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list); |
| 161 | } |
| 162 | |
| 163 | return list_empty(&ai->ai_ail1_list); |
| 164 | } |
| 165 | |
Steven Whitehouse | a25311c | 2006-11-23 11:06:35 -0500 | [diff] [blame] | 166 | static void gfs2_ail1_start(struct gfs2_sbd *sdp, int flags) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 167 | { |
Bob Peterson | 693ddea | 2007-07-24 14:07:33 -0500 | [diff] [blame] | 168 | struct list_head *head; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 169 | u64 sync_gen; |
Steven Whitehouse | 7466941 | 2006-09-19 11:17:38 -0400 | [diff] [blame] | 170 | struct list_head *first; |
| 171 | struct gfs2_ail *first_ai, *ai, *tmp; |
| 172 | int done = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 173 | |
| 174 | gfs2_log_lock(sdp); |
Bob Peterson | 693ddea | 2007-07-24 14:07:33 -0500 | [diff] [blame] | 175 | head = &sdp->sd_ail1_list; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 176 | if (list_empty(head)) { |
| 177 | gfs2_log_unlock(sdp); |
| 178 | return; |
| 179 | } |
| 180 | sync_gen = sdp->sd_ail_sync_gen++; |
| 181 | |
| 182 | first = head->prev; |
| 183 | first_ai = list_entry(first, struct gfs2_ail, ai_list); |
| 184 | first_ai->ai_sync_gen = sync_gen; |
Steven Whitehouse | 7466941 | 2006-09-19 11:17:38 -0400 | [diff] [blame] | 185 | gfs2_ail1_start_one(sdp, first_ai); /* This may drop log lock */ |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 186 | |
| 187 | if (flags & DIO_ALL) |
| 188 | first = NULL; |
| 189 | |
Steven Whitehouse | 7466941 | 2006-09-19 11:17:38 -0400 | [diff] [blame] | 190 | while(!done) { |
Steven Whitehouse | 484adff | 2006-03-29 09:12:12 -0500 | [diff] [blame] | 191 | if (first && (head->prev != first || |
| 192 | gfs2_ail1_empty_one(sdp, first_ai, 0))) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 193 | break; |
| 194 | |
Steven Whitehouse | 7466941 | 2006-09-19 11:17:38 -0400 | [diff] [blame] | 195 | done = 1; |
| 196 | list_for_each_entry_safe_reverse(ai, tmp, head, ai_list) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 197 | if (ai->ai_sync_gen >= sync_gen) |
| 198 | continue; |
| 199 | ai->ai_sync_gen = sync_gen; |
Steven Whitehouse | 7466941 | 2006-09-19 11:17:38 -0400 | [diff] [blame] | 200 | gfs2_ail1_start_one(sdp, ai); /* This may drop log lock */ |
| 201 | done = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 202 | break; |
| 203 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | gfs2_log_unlock(sdp); |
| 207 | } |
| 208 | |
| 209 | int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags) |
| 210 | { |
| 211 | struct gfs2_ail *ai, *s; |
| 212 | int ret; |
| 213 | |
| 214 | gfs2_log_lock(sdp); |
| 215 | |
| 216 | list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) { |
| 217 | if (gfs2_ail1_empty_one(sdp, ai, flags)) |
| 218 | list_move(&ai->ai_list, &sdp->sd_ail2_list); |
| 219 | else if (!(flags & DIO_ALL)) |
| 220 | break; |
| 221 | } |
| 222 | |
| 223 | ret = list_empty(&sdp->sd_ail1_list); |
| 224 | |
| 225 | gfs2_log_unlock(sdp); |
| 226 | |
| 227 | return ret; |
| 228 | } |
| 229 | |
Steven Whitehouse | ddacfaf | 2006-10-03 11:10:41 -0400 | [diff] [blame] | 230 | |
| 231 | /** |
| 232 | * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced |
| 233 | * @sdp: the filesystem |
| 234 | * @ai: the AIL entry |
| 235 | * |
| 236 | */ |
| 237 | |
| 238 | static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai) |
| 239 | { |
| 240 | struct list_head *head = &ai->ai_ail2_list; |
| 241 | struct gfs2_bufdata *bd; |
| 242 | |
| 243 | while (!list_empty(head)) { |
| 244 | bd = list_entry(head->prev, struct gfs2_bufdata, |
| 245 | bd_ail_st_list); |
| 246 | gfs2_assert(sdp, bd->bd_ail == ai); |
Steven Whitehouse | 1e1a3d0 | 2007-08-27 09:45:26 +0100 | [diff] [blame] | 247 | gfs2_remove_from_ail(bd->bd_bh->b_page->mapping, bd); |
Steven Whitehouse | ddacfaf | 2006-10-03 11:10:41 -0400 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 251 | static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail) |
| 252 | { |
| 253 | struct gfs2_ail *ai, *safe; |
| 254 | unsigned int old_tail = sdp->sd_log_tail; |
| 255 | int wrap = (new_tail < old_tail); |
| 256 | int a, b, rm; |
| 257 | |
| 258 | gfs2_log_lock(sdp); |
| 259 | |
| 260 | list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) { |
| 261 | a = (old_tail <= ai->ai_first); |
| 262 | b = (ai->ai_first < new_tail); |
| 263 | rm = (wrap) ? (a || b) : (a && b); |
| 264 | if (!rm) |
| 265 | continue; |
| 266 | |
| 267 | gfs2_ail2_empty_one(sdp, ai); |
| 268 | list_del(&ai->ai_list); |
| 269 | gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list)); |
| 270 | gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list)); |
| 271 | kfree(ai); |
| 272 | } |
| 273 | |
| 274 | gfs2_log_unlock(sdp); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * gfs2_log_reserve - Make a log reservation |
| 279 | * @sdp: The GFS2 superblock |
| 280 | * @blks: The number of blocks to reserve |
| 281 | * |
Steven Whitehouse | 8991864 | 2007-06-01 15:19:33 +0100 | [diff] [blame] | 282 | * Note that we never give out the last few blocks of the journal. Thats |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 283 | * due to the fact that there is a small number of header blocks |
Steven Whitehouse | b004157 | 2006-11-23 10:51:34 -0500 | [diff] [blame] | 284 | * associated with each log flush. The exact number can't be known until |
| 285 | * flush time, so we ensure that we have just enough free blocks at all |
| 286 | * times to avoid running out during a log flush. |
| 287 | * |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 288 | * Returns: errno |
| 289 | */ |
| 290 | |
| 291 | int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks) |
| 292 | { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 293 | unsigned int try = 0; |
Steven Whitehouse | 8991864 | 2007-06-01 15:19:33 +0100 | [diff] [blame] | 294 | unsigned reserved_blks = 6 * (4096 / sdp->sd_vfs->s_blocksize); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 295 | |
| 296 | if (gfs2_assert_warn(sdp, blks) || |
| 297 | gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks)) |
| 298 | return -EINVAL; |
| 299 | |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 300 | mutex_lock(&sdp->sd_log_reserve_mutex); |
Steven Whitehouse | 484adff | 2006-03-29 09:12:12 -0500 | [diff] [blame] | 301 | gfs2_log_lock(sdp); |
Steven Whitehouse | 8991864 | 2007-06-01 15:19:33 +0100 | [diff] [blame] | 302 | while(sdp->sd_log_blks_free <= (blks + reserved_blks)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 303 | gfs2_log_unlock(sdp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 304 | gfs2_ail1_empty(sdp, 0); |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 305 | gfs2_log_flush(sdp, NULL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 306 | |
| 307 | if (try++) |
| 308 | gfs2_ail1_start(sdp, 0); |
Steven Whitehouse | 484adff | 2006-03-29 09:12:12 -0500 | [diff] [blame] | 309 | gfs2_log_lock(sdp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 310 | } |
Steven Whitehouse | 484adff | 2006-03-29 09:12:12 -0500 | [diff] [blame] | 311 | sdp->sd_log_blks_free -= blks; |
| 312 | gfs2_log_unlock(sdp); |
| 313 | mutex_unlock(&sdp->sd_log_reserve_mutex); |
| 314 | |
| 315 | down_read(&sdp->sd_log_flush_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * gfs2_log_release - Release a given number of log blocks |
| 322 | * @sdp: The GFS2 superblock |
| 323 | * @blks: The number of blocks |
| 324 | * |
| 325 | */ |
| 326 | |
| 327 | void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks) |
| 328 | { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 329 | |
| 330 | gfs2_log_lock(sdp); |
| 331 | sdp->sd_log_blks_free += blks; |
| 332 | gfs2_assert_withdraw(sdp, |
| 333 | sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks); |
| 334 | gfs2_log_unlock(sdp); |
Steven Whitehouse | ed38650 | 2006-04-07 16:28:07 -0400 | [diff] [blame] | 335 | up_read(&sdp->sd_log_flush_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 338 | static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 339 | { |
Steven Whitehouse | 2359125 | 2006-10-13 17:25:45 -0400 | [diff] [blame] | 340 | struct inode *inode = sdp->sd_jdesc->jd_inode; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 341 | int error; |
Steven Whitehouse | 2359125 | 2006-10-13 17:25:45 -0400 | [diff] [blame] | 342 | struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 }; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 343 | |
Steven Whitehouse | 2359125 | 2006-10-13 17:25:45 -0400 | [diff] [blame] | 344 | bh_map.b_size = 1 << inode->i_blkbits; |
| 345 | error = gfs2_block_map(inode, lbn, 0, &bh_map); |
Steven Whitehouse | 7a6bbac | 2006-09-18 17:18:23 -0400 | [diff] [blame] | 346 | if (error || !bh_map.b_blocknr) |
Ryusuke Konishi | aed3255 | 2006-11-28 02:53:22 +0900 | [diff] [blame] | 347 | printk(KERN_INFO "error=%d, dbn=%llu lbn=%u", error, |
| 348 | (unsigned long long)bh_map.b_blocknr, lbn); |
Steven Whitehouse | 7a6bbac | 2006-09-18 17:18:23 -0400 | [diff] [blame] | 349 | gfs2_assert_withdraw(sdp, !error && bh_map.b_blocknr); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 350 | |
Steven Whitehouse | 7a6bbac | 2006-09-18 17:18:23 -0400 | [diff] [blame] | 351 | return bh_map.b_blocknr; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /** |
| 355 | * log_distance - Compute distance between two journal blocks |
| 356 | * @sdp: The GFS2 superblock |
| 357 | * @newer: The most recent journal block of the pair |
| 358 | * @older: The older journal block of the pair |
| 359 | * |
| 360 | * Compute the distance (in the journal direction) between two |
| 361 | * blocks in the journal |
| 362 | * |
| 363 | * Returns: the distance in blocks |
| 364 | */ |
| 365 | |
Steven Whitehouse | faa31ce | 2006-09-13 11:13:27 -0400 | [diff] [blame] | 366 | 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] | 367 | unsigned int older) |
| 368 | { |
| 369 | int dist; |
| 370 | |
| 371 | dist = newer - older; |
| 372 | if (dist < 0) |
| 373 | dist += sdp->sd_jdesc->jd_blocks; |
| 374 | |
| 375 | return dist; |
| 376 | } |
| 377 | |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 378 | /** |
| 379 | * calc_reserved - Calculate the number of blocks to reserve when |
| 380 | * refunding a transaction's unused buffers. |
| 381 | * @sdp: The GFS2 superblock |
| 382 | * |
| 383 | * This is complex. We need to reserve room for all our currently used |
| 384 | * metadata buffers (e.g. normal file I/O rewriting file time stamps) and |
| 385 | * all our journaled data buffers for journaled files (e.g. files in the |
| 386 | * meta_fs like rindex, or files for which chattr +j was done.) |
| 387 | * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush |
| 388 | * will count it as free space (sd_log_blks_free) and corruption will follow. |
| 389 | * |
| 390 | * We can have metadata bufs and jdata bufs in the same journal. So each |
| 391 | * type gets its own log header, for which we need to reserve a block. |
| 392 | * In fact, each type has the potential for needing more than one header |
| 393 | * in cases where we have more buffers than will fit on a journal page. |
| 394 | * Metadata journal entries take up half the space of journaled buffer entries. |
| 395 | * Thus, metadata entries have buf_limit (502) and journaled buffers have |
| 396 | * databuf_limit (251) before they cause a wrap around. |
| 397 | * |
| 398 | * Also, we need to reserve blocks for revoke journal entries and one for an |
| 399 | * overall header for the lot. |
| 400 | * |
| 401 | * Returns: the number of blocks reserved |
| 402 | */ |
| 403 | static unsigned int calc_reserved(struct gfs2_sbd *sdp) |
| 404 | { |
| 405 | unsigned int reserved = 0; |
| 406 | unsigned int mbuf_limit, metabufhdrs_needed; |
| 407 | unsigned int dbuf_limit, databufhdrs_needed; |
| 408 | unsigned int revokes = 0; |
| 409 | |
| 410 | mbuf_limit = buf_limit(sdp); |
| 411 | metabufhdrs_needed = (sdp->sd_log_commited_buf + |
| 412 | (mbuf_limit - 1)) / mbuf_limit; |
| 413 | dbuf_limit = databuf_limit(sdp); |
| 414 | databufhdrs_needed = (sdp->sd_log_commited_databuf + |
| 415 | (dbuf_limit - 1)) / dbuf_limit; |
| 416 | |
| 417 | if (sdp->sd_log_commited_revoke) |
| 418 | revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke, |
| 419 | sizeof(u64)); |
| 420 | |
| 421 | reserved = sdp->sd_log_commited_buf + metabufhdrs_needed + |
| 422 | sdp->sd_log_commited_databuf + databufhdrs_needed + |
| 423 | revokes; |
| 424 | /* One for the overall header */ |
| 425 | if (reserved) |
| 426 | reserved++; |
| 427 | return reserved; |
| 428 | } |
| 429 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 430 | static unsigned int current_tail(struct gfs2_sbd *sdp) |
| 431 | { |
| 432 | struct gfs2_ail *ai; |
| 433 | unsigned int tail; |
| 434 | |
| 435 | gfs2_log_lock(sdp); |
| 436 | |
Steven Whitehouse | faa31ce | 2006-09-13 11:13:27 -0400 | [diff] [blame] | 437 | if (list_empty(&sdp->sd_ail1_list)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 438 | tail = sdp->sd_log_head; |
Steven Whitehouse | faa31ce | 2006-09-13 11:13:27 -0400 | [diff] [blame] | 439 | } else { |
| 440 | 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] | 441 | tail = ai->ai_first; |
| 442 | } |
| 443 | |
| 444 | gfs2_log_unlock(sdp); |
| 445 | |
| 446 | return tail; |
| 447 | } |
| 448 | |
| 449 | static inline void log_incr_head(struct gfs2_sbd *sdp) |
| 450 | { |
| 451 | if (sdp->sd_log_flush_head == sdp->sd_log_tail) |
Steven Whitehouse | faa31ce | 2006-09-13 11:13:27 -0400 | [diff] [blame] | 452 | 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] | 453 | |
| 454 | if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) { |
| 455 | sdp->sd_log_flush_head = 0; |
| 456 | sdp->sd_log_flush_wrapped = 1; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * gfs2_log_get_buf - Get and initialize a buffer to use for log control data |
| 462 | * @sdp: The GFS2 superblock |
| 463 | * |
| 464 | * Returns: the buffer_head |
| 465 | */ |
| 466 | |
| 467 | struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp) |
| 468 | { |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 469 | u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 470 | struct gfs2_log_buf *lb; |
| 471 | struct buffer_head *bh; |
| 472 | |
Steven Whitehouse | 4f3df04 | 2006-01-18 13:20:16 +0000 | [diff] [blame] | 473 | lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 474 | list_add(&lb->lb_list, &sdp->sd_log_flush_list); |
| 475 | |
| 476 | bh = lb->lb_bh = sb_getblk(sdp->sd_vfs, blkno); |
| 477 | lock_buffer(bh); |
| 478 | memset(bh->b_data, 0, bh->b_size); |
| 479 | set_buffer_uptodate(bh); |
| 480 | clear_buffer_dirty(bh); |
| 481 | unlock_buffer(bh); |
| 482 | |
| 483 | log_incr_head(sdp); |
| 484 | |
| 485 | return bh; |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log |
| 490 | * @sdp: the filesystem |
| 491 | * @data: the data the buffer_head should point to |
| 492 | * |
| 493 | * Returns: the log buffer descriptor |
| 494 | */ |
| 495 | |
| 496 | struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp, |
| 497 | struct buffer_head *real) |
| 498 | { |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 499 | u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 500 | struct gfs2_log_buf *lb; |
| 501 | struct buffer_head *bh; |
| 502 | |
Steven Whitehouse | 4f3df04 | 2006-01-18 13:20:16 +0000 | [diff] [blame] | 503 | lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 504 | list_add(&lb->lb_list, &sdp->sd_log_flush_list); |
| 505 | lb->lb_real = real; |
| 506 | |
| 507 | bh = lb->lb_bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL); |
| 508 | atomic_set(&bh->b_count, 1); |
| 509 | bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 510 | set_bh_page(bh, real->b_page, bh_offset(real)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 511 | bh->b_blocknr = blkno; |
| 512 | bh->b_size = sdp->sd_sb.sb_bsize; |
| 513 | bh->b_bdev = sdp->sd_vfs->s_bdev; |
| 514 | |
| 515 | log_incr_head(sdp); |
| 516 | |
| 517 | return bh; |
| 518 | } |
| 519 | |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 520 | static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 521 | { |
| 522 | unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail); |
| 523 | |
| 524 | ail2_empty(sdp, new_tail); |
| 525 | |
| 526 | gfs2_log_lock(sdp); |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 527 | sdp->sd_log_blks_free += dist; |
Steven Whitehouse | faa31ce | 2006-09-13 11:13:27 -0400 | [diff] [blame] | 528 | 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] | 529 | gfs2_log_unlock(sdp); |
| 530 | |
| 531 | sdp->sd_log_tail = new_tail; |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * log_write_header - Get and initialize a journal header buffer |
| 536 | * @sdp: The GFS2 superblock |
| 537 | * |
| 538 | * Returns: the initialized log buffer descriptor |
| 539 | */ |
| 540 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 541 | 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] | 542 | { |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 543 | u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 544 | struct buffer_head *bh; |
| 545 | struct gfs2_log_header *lh; |
| 546 | unsigned int tail; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 547 | u32 hash; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 548 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 549 | bh = sb_getblk(sdp->sd_vfs, blkno); |
| 550 | lock_buffer(bh); |
| 551 | memset(bh->b_data, 0, bh->b_size); |
| 552 | set_buffer_uptodate(bh); |
| 553 | clear_buffer_dirty(bh); |
| 554 | unlock_buffer(bh); |
| 555 | |
| 556 | gfs2_ail1_empty(sdp, 0); |
| 557 | tail = current_tail(sdp); |
| 558 | |
| 559 | lh = (struct gfs2_log_header *)bh->b_data; |
| 560 | memset(lh, 0, sizeof(struct gfs2_log_header)); |
| 561 | lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC); |
Steven Whitehouse | e3167de | 2006-03-30 15:46:23 -0500 | [diff] [blame] | 562 | lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH); |
| 563 | lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH); |
Steven Whitehouse | e0f2bf7 | 2006-07-17 09:36:28 -0400 | [diff] [blame] | 564 | lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++); |
| 565 | lh->lh_flags = cpu_to_be32(flags); |
| 566 | lh->lh_tail = cpu_to_be32(tail); |
| 567 | lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 568 | hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header)); |
| 569 | lh->lh_hash = cpu_to_be32(hash); |
| 570 | |
| 571 | set_buffer_dirty(bh); |
| 572 | if (sync_dirty_buffer(bh)) |
| 573 | gfs2_io_error_bh(sdp, bh); |
| 574 | brelse(bh); |
| 575 | |
| 576 | if (sdp->sd_log_tail != tail) |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 577 | log_pull_tail(sdp, tail); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 578 | else |
| 579 | gfs2_assert_withdraw(sdp, !pull); |
| 580 | |
| 581 | sdp->sd_log_idle = (tail == sdp->sd_log_flush_head); |
| 582 | log_incr_head(sdp); |
| 583 | } |
| 584 | |
| 585 | static void log_flush_commit(struct gfs2_sbd *sdp) |
| 586 | { |
| 587 | struct list_head *head = &sdp->sd_log_flush_list; |
| 588 | struct gfs2_log_buf *lb; |
| 589 | struct buffer_head *bh; |
Steven Whitehouse | a0a2474 | 2007-07-09 15:43:07 +0100 | [diff] [blame] | 590 | int flushcount = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 591 | |
| 592 | while (!list_empty(head)) { |
| 593 | lb = list_entry(head->next, struct gfs2_log_buf, lb_list); |
| 594 | list_del(&lb->lb_list); |
| 595 | bh = lb->lb_bh; |
| 596 | |
| 597 | wait_on_buffer(bh); |
| 598 | if (!buffer_uptodate(bh)) |
| 599 | gfs2_io_error_bh(sdp, bh); |
| 600 | if (lb->lb_real) { |
| 601 | while (atomic_read(&bh->b_count) != 1) /* Grrrr... */ |
| 602 | schedule(); |
| 603 | free_buffer_head(bh); |
| 604 | } else |
| 605 | brelse(bh); |
| 606 | kfree(lb); |
Steven Whitehouse | a0a2474 | 2007-07-09 15:43:07 +0100 | [diff] [blame] | 607 | flushcount++; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Steven Whitehouse | a0a2474 | 2007-07-09 15:43:07 +0100 | [diff] [blame] | 610 | /* If nothing was journaled, the header is unplanned and unwanted. */ |
| 611 | if (flushcount) { |
| 612 | log_write_header(sdp, 0, 0); |
| 613 | } else { |
| 614 | unsigned int tail; |
| 615 | tail = current_tail(sdp); |
| 616 | |
| 617 | gfs2_ail1_empty(sdp, 0); |
| 618 | if (sdp->sd_log_tail != tail) |
| 619 | log_pull_tail(sdp, tail); |
| 620 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Steven Whitehouse | d7b616e | 2007-09-02 10:48:13 +0100 | [diff] [blame] | 623 | static void gfs2_ordered_write(struct gfs2_sbd *sdp) |
| 624 | { |
| 625 | struct gfs2_bufdata *bd; |
| 626 | struct buffer_head *bh; |
| 627 | LIST_HEAD(written); |
| 628 | |
| 629 | gfs2_log_lock(sdp); |
| 630 | while (!list_empty(&sdp->sd_log_le_ordered)) { |
| 631 | bd = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_bufdata, bd_le.le_list); |
| 632 | list_move(&bd->bd_le.le_list, &written); |
| 633 | bh = bd->bd_bh; |
| 634 | if (!buffer_dirty(bh)) |
| 635 | continue; |
| 636 | get_bh(bh); |
| 637 | gfs2_log_unlock(sdp); |
| 638 | lock_buffer(bh); |
| 639 | if (test_clear_buffer_dirty(bh)) { |
| 640 | bh->b_end_io = end_buffer_write_sync; |
| 641 | submit_bh(WRITE, bh); |
| 642 | } else { |
| 643 | unlock_buffer(bh); |
| 644 | brelse(bh); |
| 645 | } |
| 646 | gfs2_log_lock(sdp); |
| 647 | } |
| 648 | list_splice(&written, &sdp->sd_log_le_ordered); |
| 649 | gfs2_log_unlock(sdp); |
| 650 | } |
| 651 | |
| 652 | static void gfs2_ordered_wait(struct gfs2_sbd *sdp) |
| 653 | { |
| 654 | struct gfs2_bufdata *bd; |
| 655 | struct buffer_head *bh; |
| 656 | |
| 657 | gfs2_log_lock(sdp); |
| 658 | while (!list_empty(&sdp->sd_log_le_ordered)) { |
| 659 | bd = list_entry(sdp->sd_log_le_ordered.prev, struct gfs2_bufdata, bd_le.le_list); |
| 660 | bh = bd->bd_bh; |
| 661 | if (buffer_locked(bh)) { |
| 662 | get_bh(bh); |
| 663 | gfs2_log_unlock(sdp); |
| 664 | wait_on_buffer(bh); |
| 665 | brelse(bh); |
| 666 | gfs2_log_lock(sdp); |
| 667 | continue; |
| 668 | } |
| 669 | list_del_init(&bd->bd_le.le_list); |
| 670 | } |
| 671 | gfs2_log_unlock(sdp); |
| 672 | } |
| 673 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 674 | /** |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 675 | * gfs2_log_flush - flush incore transaction(s) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 676 | * @sdp: the filesystem |
| 677 | * @gl: The glock structure to flush. If NULL, flush the whole incore log |
| 678 | * |
| 679 | */ |
| 680 | |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 681 | void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 682 | { |
| 683 | struct gfs2_ail *ai; |
| 684 | |
Steven Whitehouse | 484adff | 2006-03-29 09:12:12 -0500 | [diff] [blame] | 685 | down_write(&sdp->sd_log_flush_lock); |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 686 | |
| 687 | if (gl) { |
| 688 | gfs2_log_lock(sdp); |
| 689 | if (list_empty(&gl->gl_le.le_list)) { |
| 690 | gfs2_log_unlock(sdp); |
Steven Whitehouse | 484adff | 2006-03-29 09:12:12 -0500 | [diff] [blame] | 691 | up_write(&sdp->sd_log_flush_lock); |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 692 | return; |
| 693 | } |
| 694 | gfs2_log_unlock(sdp); |
| 695 | } |
| 696 | |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 697 | ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL); |
| 698 | INIT_LIST_HEAD(&ai->ai_ail1_list); |
| 699 | INIT_LIST_HEAD(&ai->ai_ail2_list); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 700 | |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 701 | gfs2_assert_withdraw(sdp, |
Steven Whitehouse | d7b616e | 2007-09-02 10:48:13 +0100 | [diff] [blame] | 702 | sdp->sd_log_num_buf + sdp->sd_log_num_databuf == |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 703 | sdp->sd_log_commited_buf + |
| 704 | sdp->sd_log_commited_databuf); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 705 | gfs2_assert_withdraw(sdp, |
| 706 | sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke); |
| 707 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 708 | sdp->sd_log_flush_head = sdp->sd_log_head; |
| 709 | sdp->sd_log_flush_wrapped = 0; |
| 710 | ai->ai_first = sdp->sd_log_flush_head; |
| 711 | |
Steven Whitehouse | d7b616e | 2007-09-02 10:48:13 +0100 | [diff] [blame] | 712 | gfs2_ordered_write(sdp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 713 | lops_before_commit(sdp); |
Steven Whitehouse | d7b616e | 2007-09-02 10:48:13 +0100 | [diff] [blame] | 714 | gfs2_ordered_wait(sdp); |
| 715 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 716 | if (!list_empty(&sdp->sd_log_flush_list)) |
| 717 | log_flush_commit(sdp); |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 718 | else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){ |
| 719 | gfs2_log_lock(sdp); |
| 720 | sdp->sd_log_blks_free--; /* Adjust for unreserved buffer */ |
| 721 | gfs2_log_unlock(sdp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 722 | log_write_header(sdp, 0, PULL); |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 723 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 724 | lops_after_commit(sdp, ai); |
Steven Whitehouse | fe1a698 | 2006-10-11 13:34:59 -0400 | [diff] [blame] | 725 | |
| 726 | gfs2_log_lock(sdp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 727 | sdp->sd_log_head = sdp->sd_log_flush_head; |
Steven Whitehouse | faa31ce | 2006-09-13 11:13:27 -0400 | [diff] [blame] | 728 | sdp->sd_log_blks_reserved = 0; |
| 729 | sdp->sd_log_commited_buf = 0; |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 730 | sdp->sd_log_commited_databuf = 0; |
Steven Whitehouse | faa31ce | 2006-09-13 11:13:27 -0400 | [diff] [blame] | 731 | sdp->sd_log_commited_revoke = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 732 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 733 | if (!list_empty(&ai->ai_ail1_list)) { |
| 734 | list_add(&ai->ai_list, &sdp->sd_ail1_list); |
| 735 | ai = NULL; |
| 736 | } |
| 737 | gfs2_log_unlock(sdp); |
| 738 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 739 | sdp->sd_vfs->s_dirt = 0; |
Steven Whitehouse | 484adff | 2006-03-29 09:12:12 -0500 | [diff] [blame] | 740 | up_write(&sdp->sd_log_flush_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 741 | |
| 742 | kfree(ai); |
| 743 | } |
| 744 | |
| 745 | static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr) |
| 746 | { |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 747 | unsigned int reserved; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 748 | unsigned int old; |
| 749 | |
| 750 | gfs2_log_lock(sdp); |
| 751 | |
| 752 | sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm; |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 753 | sdp->sd_log_commited_databuf += tr->tr_num_databuf_new - |
| 754 | tr->tr_num_databuf_rm; |
| 755 | gfs2_assert_withdraw(sdp, (((int)sdp->sd_log_commited_buf) >= 0) || |
| 756 | (((int)sdp->sd_log_commited_databuf) >= 0)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 757 | sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm; |
| 758 | gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0); |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 759 | reserved = calc_reserved(sdp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 760 | old = sdp->sd_log_blks_free; |
| 761 | sdp->sd_log_blks_free += tr->tr_reserved - |
| 762 | (reserved - sdp->sd_log_blks_reserved); |
| 763 | |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 764 | gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free >= old); |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 765 | gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free <= |
| 766 | sdp->sd_jdesc->jd_blocks); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 767 | |
| 768 | sdp->sd_log_blks_reserved = reserved; |
| 769 | |
| 770 | gfs2_log_unlock(sdp); |
| 771 | } |
| 772 | |
| 773 | /** |
| 774 | * gfs2_log_commit - Commit a transaction to the log |
| 775 | * @sdp: the filesystem |
| 776 | * @tr: the transaction |
| 777 | * |
| 778 | * Returns: errno |
| 779 | */ |
| 780 | |
| 781 | void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr) |
| 782 | { |
| 783 | log_refund(sdp, tr); |
| 784 | lops_incore_commit(sdp, tr); |
| 785 | |
| 786 | sdp->sd_vfs->s_dirt = 1; |
Steven Whitehouse | 484adff | 2006-03-29 09:12:12 -0500 | [diff] [blame] | 787 | up_read(&sdp->sd_log_flush_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 788 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 789 | gfs2_log_lock(sdp); |
Steven Whitehouse | b004157 | 2006-11-23 10:51:34 -0500 | [diff] [blame] | 790 | if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks)) |
| 791 | wake_up_process(sdp->sd_logd_process); |
| 792 | gfs2_log_unlock(sdp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | /** |
| 796 | * gfs2_log_shutdown - write a shutdown header into a journal |
| 797 | * @sdp: the filesystem |
| 798 | * |
| 799 | */ |
| 800 | |
| 801 | void gfs2_log_shutdown(struct gfs2_sbd *sdp) |
| 802 | { |
Steven Whitehouse | 484adff | 2006-03-29 09:12:12 -0500 | [diff] [blame] | 803 | down_write(&sdp->sd_log_flush_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 804 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 805 | gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved); |
| 806 | gfs2_assert_withdraw(sdp, !sdp->sd_log_num_gl); |
| 807 | gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 808 | gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke); |
| 809 | gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg); |
| 810 | gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf); |
| 811 | gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list)); |
| 812 | |
| 813 | sdp->sd_log_flush_head = sdp->sd_log_head; |
| 814 | sdp->sd_log_flush_wrapped = 0; |
| 815 | |
Robert Peterson | 2332c44 | 2007-06-18 14:50:20 -0500 | [diff] [blame] | 816 | log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT, |
| 817 | (sdp->sd_log_tail == current_tail(sdp)) ? 0 : PULL); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 818 | |
Steven Whitehouse | a74604b | 2006-04-21 15:10:46 -0400 | [diff] [blame] | 819 | gfs2_assert_warn(sdp, sdp->sd_log_blks_free == sdp->sd_jdesc->jd_blocks); |
| 820 | gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail); |
| 821 | gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 822 | |
| 823 | sdp->sd_log_head = sdp->sd_log_flush_head; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 824 | sdp->sd_log_tail = sdp->sd_log_head; |
| 825 | |
Steven Whitehouse | 484adff | 2006-03-29 09:12:12 -0500 | [diff] [blame] | 826 | up_write(&sdp->sd_log_flush_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Steven Whitehouse | a25311c | 2006-11-23 11:06:35 -0500 | [diff] [blame] | 829 | |
| 830 | /** |
| 831 | * gfs2_meta_syncfs - sync all the buffers in a filesystem |
| 832 | * @sdp: the filesystem |
| 833 | * |
| 834 | */ |
| 835 | |
| 836 | void gfs2_meta_syncfs(struct gfs2_sbd *sdp) |
| 837 | { |
| 838 | gfs2_log_flush(sdp, NULL); |
| 839 | for (;;) { |
| 840 | gfs2_ail1_start(sdp, DIO_ALL); |
| 841 | if (gfs2_ail1_empty(sdp, DIO_ALL)) |
| 842 | break; |
| 843 | msleep(10); |
| 844 | } |
| 845 | } |
| 846 | |