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