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