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