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