Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs_types.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 21 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "xfs_log.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 23 | #include "xfs_inum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_trans.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 25 | #include "xfs_sb.h" |
| 26 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "xfs_mount.h" |
| 28 | #include "xfs_error.h" |
| 29 | #include "xfs_log_priv.h" |
| 30 | #include "xfs_buf_item.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 31 | #include "xfs_bmap_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "xfs_alloc_btree.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 33 | #include "xfs_ialloc_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_log_recover.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "xfs_trans_priv.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 36 | #include "xfs_dinode.h" |
| 37 | #include "xfs_inode.h" |
| 38 | #include "xfs_rw.h" |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 39 | #include "xfs_trace.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
David Chinner | eb01c9c | 2008-04-10 12:18:46 +1000 | [diff] [blame] | 41 | kmem_zone_t *xfs_log_ticket_zone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | /* Local miscellaneous function prototypes */ |
Dave Chinner | 55b6633 | 2010-03-23 11:43:17 +1100 | [diff] [blame] | 44 | STATIC int xlog_commit_record(struct log *log, struct xlog_ticket *ticket, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | xlog_in_core_t **, xfs_lsn_t *); |
| 46 | STATIC xlog_t * xlog_alloc_log(xfs_mount_t *mp, |
| 47 | xfs_buftarg_t *log_target, |
| 48 | xfs_daddr_t blk_offset, |
| 49 | int num_bblks); |
Dave Chinner | c8a09ff | 2010-12-04 00:02:40 +1100 | [diff] [blame] | 50 | STATIC int xlog_space_left(struct log *log, atomic64_t *head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | STATIC int xlog_sync(xlog_t *log, xlog_in_core_t *iclog); |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 52 | STATIC void xlog_dealloc_log(xlog_t *log); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | /* local state machine functions */ |
| 55 | STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int); |
| 56 | STATIC void xlog_state_do_callback(xlog_t *log,int aborted, xlog_in_core_t *iclog); |
| 57 | STATIC int xlog_state_get_iclog_space(xlog_t *log, |
| 58 | int len, |
| 59 | xlog_in_core_t **iclog, |
| 60 | xlog_ticket_t *ticket, |
| 61 | int *continued_write, |
| 62 | int *logoffsetp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | STATIC int xlog_state_release_iclog(xlog_t *log, |
| 64 | xlog_in_core_t *iclog); |
| 65 | STATIC void xlog_state_switch_iclogs(xlog_t *log, |
| 66 | xlog_in_core_t *iclog, |
| 67 | int eventual_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog); |
| 69 | |
| 70 | /* local functions to manipulate grant head */ |
| 71 | STATIC int xlog_grant_log_space(xlog_t *log, |
| 72 | xlog_ticket_t *xtic); |
Dave Chinner | 2ced19c | 2010-12-21 12:09:20 +1100 | [diff] [blame] | 73 | STATIC void xlog_grant_push_ail(struct log *log, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | int need_bytes); |
| 75 | STATIC void xlog_regrant_reserve_log_space(xlog_t *log, |
| 76 | xlog_ticket_t *ticket); |
| 77 | STATIC int xlog_regrant_write_log_space(xlog_t *log, |
| 78 | xlog_ticket_t *ticket); |
| 79 | STATIC void xlog_ungrant_log_space(xlog_t *log, |
| 80 | xlog_ticket_t *ticket); |
| 81 | |
Nathan Scott | cfcbbbd | 2005-11-02 15:12:04 +1100 | [diff] [blame] | 82 | #if defined(DEBUG) |
Christoph Hellwig | e6b1f27 | 2010-03-23 11:47:38 +1100 | [diff] [blame] | 83 | STATIC void xlog_verify_dest_ptr(xlog_t *log, char *ptr); |
Dave Chinner | 3f336c6 | 2010-12-21 12:02:52 +1100 | [diff] [blame] | 84 | STATIC void xlog_verify_grant_tail(struct log *log); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog, |
| 86 | int count, boolean_t syncing); |
| 87 | STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog, |
| 88 | xfs_lsn_t tail_lsn); |
| 89 | #else |
| 90 | #define xlog_verify_dest_ptr(a,b) |
Dave Chinner | 3f336c6 | 2010-12-21 12:02:52 +1100 | [diff] [blame] | 91 | #define xlog_verify_grant_tail(a) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | #define xlog_verify_iclog(a,b,c,d) |
| 93 | #define xlog_verify_tail_lsn(a,b,c) |
| 94 | #endif |
| 95 | |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 96 | STATIC int xlog_iclogs_empty(xlog_t *log); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Christoph Hellwig | dd954c6 | 2006-01-11 15:34:50 +1100 | [diff] [blame] | 98 | static void |
Dave Chinner | 663e496 | 2010-12-21 12:06:05 +1100 | [diff] [blame] | 99 | xlog_grant_sub_space( |
| 100 | struct log *log, |
Dave Chinner | c8a09ff | 2010-12-04 00:02:40 +1100 | [diff] [blame] | 101 | atomic64_t *head, |
Dave Chinner | 663e496 | 2010-12-21 12:06:05 +1100 | [diff] [blame] | 102 | int bytes) |
Christoph Hellwig | dd954c6 | 2006-01-11 15:34:50 +1100 | [diff] [blame] | 103 | { |
Dave Chinner | d0eb2f3 | 2010-12-21 12:29:14 +1100 | [diff] [blame] | 104 | int64_t head_val = atomic64_read(head); |
| 105 | int64_t new, old; |
Dave Chinner | a69ed03 | 2010-12-21 12:08:20 +1100 | [diff] [blame] | 106 | |
Dave Chinner | d0eb2f3 | 2010-12-21 12:29:14 +1100 | [diff] [blame] | 107 | do { |
| 108 | int cycle, space; |
Dave Chinner | a69ed03 | 2010-12-21 12:08:20 +1100 | [diff] [blame] | 109 | |
Dave Chinner | d0eb2f3 | 2010-12-21 12:29:14 +1100 | [diff] [blame] | 110 | xlog_crack_grant_head_val(head_val, &cycle, &space); |
Dave Chinner | a69ed03 | 2010-12-21 12:08:20 +1100 | [diff] [blame] | 111 | |
Dave Chinner | d0eb2f3 | 2010-12-21 12:29:14 +1100 | [diff] [blame] | 112 | space -= bytes; |
| 113 | if (space < 0) { |
| 114 | space += log->l_logsize; |
| 115 | cycle--; |
| 116 | } |
| 117 | |
| 118 | old = head_val; |
| 119 | new = xlog_assign_grant_head_val(cycle, space); |
| 120 | head_val = atomic64_cmpxchg(head, old, new); |
| 121 | } while (head_val != old); |
Christoph Hellwig | dd954c6 | 2006-01-11 15:34:50 +1100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static void |
Dave Chinner | 663e496 | 2010-12-21 12:06:05 +1100 | [diff] [blame] | 125 | xlog_grant_add_space( |
| 126 | struct log *log, |
Dave Chinner | c8a09ff | 2010-12-04 00:02:40 +1100 | [diff] [blame] | 127 | atomic64_t *head, |
Dave Chinner | 663e496 | 2010-12-21 12:06:05 +1100 | [diff] [blame] | 128 | int bytes) |
Christoph Hellwig | dd954c6 | 2006-01-11 15:34:50 +1100 | [diff] [blame] | 129 | { |
Dave Chinner | d0eb2f3 | 2010-12-21 12:29:14 +1100 | [diff] [blame] | 130 | int64_t head_val = atomic64_read(head); |
| 131 | int64_t new, old; |
Dave Chinner | a69ed03 | 2010-12-21 12:08:20 +1100 | [diff] [blame] | 132 | |
Dave Chinner | d0eb2f3 | 2010-12-21 12:29:14 +1100 | [diff] [blame] | 133 | do { |
| 134 | int tmp; |
| 135 | int cycle, space; |
Dave Chinner | a69ed03 | 2010-12-21 12:08:20 +1100 | [diff] [blame] | 136 | |
Dave Chinner | d0eb2f3 | 2010-12-21 12:29:14 +1100 | [diff] [blame] | 137 | xlog_crack_grant_head_val(head_val, &cycle, &space); |
Dave Chinner | a69ed03 | 2010-12-21 12:08:20 +1100 | [diff] [blame] | 138 | |
Dave Chinner | d0eb2f3 | 2010-12-21 12:29:14 +1100 | [diff] [blame] | 139 | tmp = log->l_logsize - space; |
| 140 | if (tmp > bytes) |
| 141 | space += bytes; |
| 142 | else { |
| 143 | space = bytes - tmp; |
| 144 | cycle++; |
| 145 | } |
| 146 | |
| 147 | old = head_val; |
| 148 | new = xlog_assign_grant_head_val(cycle, space); |
| 149 | head_val = atomic64_cmpxchg(head, old, new); |
| 150 | } while (head_val != old); |
Christoph Hellwig | dd954c6 | 2006-01-11 15:34:50 +1100 | [diff] [blame] | 151 | } |
Dave Chinner | a69ed03 | 2010-12-21 12:08:20 +1100 | [diff] [blame] | 152 | |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 153 | STATIC bool |
| 154 | xlog_reserveq_wake( |
| 155 | struct log *log, |
| 156 | int *free_bytes) |
| 157 | { |
| 158 | struct xlog_ticket *tic; |
| 159 | int need_bytes; |
| 160 | |
| 161 | list_for_each_entry(tic, &log->l_reserveq, t_queue) { |
| 162 | if (tic->t_flags & XLOG_TIC_PERM_RESERV) |
| 163 | need_bytes = tic->t_unit_res * tic->t_cnt; |
| 164 | else |
| 165 | need_bytes = tic->t_unit_res; |
| 166 | |
| 167 | if (*free_bytes < need_bytes) |
| 168 | return false; |
| 169 | *free_bytes -= need_bytes; |
| 170 | |
| 171 | trace_xfs_log_grant_wake_up(log, tic); |
Christoph Hellwig | 14a7235f | 2012-02-20 02:31:24 +0000 | [diff] [blame^] | 172 | wake_up_process(tic->t_task); |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | STATIC bool |
| 179 | xlog_writeq_wake( |
| 180 | struct log *log, |
| 181 | int *free_bytes) |
| 182 | { |
| 183 | struct xlog_ticket *tic; |
| 184 | int need_bytes; |
| 185 | |
| 186 | list_for_each_entry(tic, &log->l_writeq, t_queue) { |
| 187 | ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV); |
| 188 | |
| 189 | need_bytes = tic->t_unit_res; |
| 190 | |
| 191 | if (*free_bytes < need_bytes) |
| 192 | return false; |
| 193 | *free_bytes -= need_bytes; |
| 194 | |
| 195 | trace_xfs_log_regrant_write_wake_up(log, tic); |
Christoph Hellwig | 14a7235f | 2012-02-20 02:31:24 +0000 | [diff] [blame^] | 196 | wake_up_process(tic->t_task); |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | return true; |
| 200 | } |
| 201 | |
| 202 | STATIC int |
| 203 | xlog_reserveq_wait( |
| 204 | struct log *log, |
| 205 | struct xlog_ticket *tic, |
| 206 | int need_bytes) |
| 207 | { |
| 208 | list_add_tail(&tic->t_queue, &log->l_reserveq); |
| 209 | |
| 210 | do { |
| 211 | if (XLOG_FORCED_SHUTDOWN(log)) |
| 212 | goto shutdown; |
| 213 | xlog_grant_push_ail(log, need_bytes); |
| 214 | |
Christoph Hellwig | 14a7235f | 2012-02-20 02:31:24 +0000 | [diff] [blame^] | 215 | __set_current_state(TASK_UNINTERRUPTIBLE); |
| 216 | spin_unlock(&log->l_grant_reserve_lock); |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 217 | |
Christoph Hellwig | 14a7235f | 2012-02-20 02:31:24 +0000 | [diff] [blame^] | 218 | XFS_STATS_INC(xs_sleep_logspace); |
| 219 | |
| 220 | trace_xfs_log_grant_sleep(log, tic); |
| 221 | schedule(); |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 222 | trace_xfs_log_grant_wake(log, tic); |
| 223 | |
| 224 | spin_lock(&log->l_grant_reserve_lock); |
| 225 | if (XLOG_FORCED_SHUTDOWN(log)) |
| 226 | goto shutdown; |
| 227 | } while (xlog_space_left(log, &log->l_grant_reserve_head) < need_bytes); |
| 228 | |
| 229 | list_del_init(&tic->t_queue); |
| 230 | return 0; |
| 231 | shutdown: |
| 232 | list_del_init(&tic->t_queue); |
| 233 | return XFS_ERROR(EIO); |
| 234 | } |
| 235 | |
| 236 | STATIC int |
| 237 | xlog_writeq_wait( |
| 238 | struct log *log, |
| 239 | struct xlog_ticket *tic, |
| 240 | int need_bytes) |
| 241 | { |
| 242 | list_add_tail(&tic->t_queue, &log->l_writeq); |
| 243 | |
| 244 | do { |
| 245 | if (XLOG_FORCED_SHUTDOWN(log)) |
| 246 | goto shutdown; |
| 247 | xlog_grant_push_ail(log, need_bytes); |
| 248 | |
Christoph Hellwig | 14a7235f | 2012-02-20 02:31:24 +0000 | [diff] [blame^] | 249 | __set_current_state(TASK_UNINTERRUPTIBLE); |
| 250 | spin_unlock(&log->l_grant_write_lock); |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 251 | |
Christoph Hellwig | 14a7235f | 2012-02-20 02:31:24 +0000 | [diff] [blame^] | 252 | XFS_STATS_INC(xs_sleep_logspace); |
| 253 | |
| 254 | trace_xfs_log_regrant_write_sleep(log, tic); |
| 255 | schedule(); |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 256 | trace_xfs_log_regrant_write_wake(log, tic); |
| 257 | |
| 258 | spin_lock(&log->l_grant_write_lock); |
| 259 | if (XLOG_FORCED_SHUTDOWN(log)) |
| 260 | goto shutdown; |
| 261 | } while (xlog_space_left(log, &log->l_grant_write_head) < need_bytes); |
| 262 | |
| 263 | list_del_init(&tic->t_queue); |
| 264 | return 0; |
| 265 | shutdown: |
| 266 | list_del_init(&tic->t_queue); |
| 267 | return XFS_ERROR(EIO); |
| 268 | } |
| 269 | |
Christoph Hellwig | 0adba53 | 2007-08-30 17:21:46 +1000 | [diff] [blame] | 270 | static void |
| 271 | xlog_tic_reset_res(xlog_ticket_t *tic) |
| 272 | { |
| 273 | tic->t_res_num = 0; |
| 274 | tic->t_res_arr_sum = 0; |
| 275 | tic->t_res_num_ophdrs = 0; |
| 276 | } |
| 277 | |
| 278 | static void |
| 279 | xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type) |
| 280 | { |
| 281 | if (tic->t_res_num == XLOG_TIC_LEN_MAX) { |
| 282 | /* add to overflow and start again */ |
| 283 | tic->t_res_o_flow += tic->t_res_arr_sum; |
| 284 | tic->t_res_num = 0; |
| 285 | tic->t_res_arr_sum = 0; |
| 286 | } |
| 287 | |
| 288 | tic->t_res_arr[tic->t_res_num].r_len = len; |
| 289 | tic->t_res_arr[tic->t_res_num].r_type = type; |
| 290 | tic->t_res_arr_sum += len; |
| 291 | tic->t_res_num++; |
| 292 | } |
Christoph Hellwig | dd954c6 | 2006-01-11 15:34:50 +1100 | [diff] [blame] | 293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | /* |
| 295 | * NOTES: |
| 296 | * |
| 297 | * 1. currblock field gets updated at startup and after in-core logs |
| 298 | * marked as with WANT_SYNC. |
| 299 | */ |
| 300 | |
| 301 | /* |
| 302 | * This routine is called when a user of a log manager ticket is done with |
| 303 | * the reservation. If the ticket was ever used, then a commit record for |
| 304 | * the associated transaction is written out as a log operation header with |
| 305 | * no data. The flag XLOG_TIC_INITED is set when the first write occurs with |
| 306 | * a given ticket. If the ticket was one with a permanent reservation, then |
| 307 | * a few operations are done differently. Permanent reservation tickets by |
| 308 | * default don't release the reservation. They just commit the current |
| 309 | * transaction with the belief that the reservation is still needed. A flag |
| 310 | * must be passed in before permanent reservations are actually released. |
| 311 | * When these type of tickets are not released, they need to be set into |
| 312 | * the inited state again. By doing this, a start record will be written |
| 313 | * out when the next write occurs. |
| 314 | */ |
| 315 | xfs_lsn_t |
Christoph Hellwig | 35a8a72 | 2010-02-15 23:34:54 +0000 | [diff] [blame] | 316 | xfs_log_done( |
| 317 | struct xfs_mount *mp, |
| 318 | struct xlog_ticket *ticket, |
| 319 | struct xlog_in_core **iclog, |
| 320 | uint flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | { |
Christoph Hellwig | 35a8a72 | 2010-02-15 23:34:54 +0000 | [diff] [blame] | 322 | struct log *log = mp->m_log; |
| 323 | xfs_lsn_t lsn = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | if (XLOG_FORCED_SHUTDOWN(log) || |
| 326 | /* |
| 327 | * If nothing was ever written, don't write out commit record. |
| 328 | * If we get an error, just continue and give back the log ticket. |
| 329 | */ |
| 330 | (((ticket->t_flags & XLOG_TIC_INITED) == 0) && |
Dave Chinner | 55b6633 | 2010-03-23 11:43:17 +1100 | [diff] [blame] | 331 | (xlog_commit_record(log, ticket, iclog, &lsn)))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | lsn = (xfs_lsn_t) -1; |
| 333 | if (ticket->t_flags & XLOG_TIC_PERM_RESERV) { |
| 334 | flags |= XFS_LOG_REL_PERM_RESERV; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | |
| 339 | if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 || |
| 340 | (flags & XFS_LOG_REL_PERM_RESERV)) { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 341 | trace_xfs_log_done_nonperm(log, ticket); |
| 342 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | /* |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 344 | * Release ticket if not permanent reservation or a specific |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | * request has been made to release a permanent reservation. |
| 346 | */ |
| 347 | xlog_ungrant_log_space(log, ticket); |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 348 | xfs_log_ticket_put(ticket); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } else { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 350 | trace_xfs_log_done_perm(log, ticket); |
| 351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | xlog_regrant_reserve_log_space(log, ticket); |
Lachlan McIlroy | c6a7b0f | 2008-08-13 16:52:50 +1000 | [diff] [blame] | 353 | /* If this ticket was a permanent reservation and we aren't |
| 354 | * trying to release it, reset the inited flags; so next time |
| 355 | * we write, a start record will be written out. |
| 356 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | ticket->t_flags |= XLOG_TIC_INITED; |
Lachlan McIlroy | c6a7b0f | 2008-08-13 16:52:50 +1000 | [diff] [blame] | 358 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
| 360 | return lsn; |
Christoph Hellwig | 35a8a72 | 2010-02-15 23:34:54 +0000 | [diff] [blame] | 361 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | /* |
| 364 | * Attaches a new iclog I/O completion callback routine during |
| 365 | * transaction commit. If the log is in error state, a non-zero |
| 366 | * return code is handed back and the caller is responsible for |
| 367 | * executing the callback at an appropriate time. |
| 368 | */ |
| 369 | int |
Christoph Hellwig | 35a8a72 | 2010-02-15 23:34:54 +0000 | [diff] [blame] | 370 | xfs_log_notify( |
| 371 | struct xfs_mount *mp, |
| 372 | struct xlog_in_core *iclog, |
| 373 | xfs_log_callback_t *cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | { |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 375 | int abortflg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
David Chinner | 114d23a | 2008-04-10 12:18:39 +1000 | [diff] [blame] | 377 | spin_lock(&iclog->ic_callback_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | abortflg = (iclog->ic_state & XLOG_STATE_IOERROR); |
| 379 | if (!abortflg) { |
| 380 | ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) || |
| 381 | (iclog->ic_state == XLOG_STATE_WANT_SYNC)); |
| 382 | cb->cb_next = NULL; |
| 383 | *(iclog->ic_callback_tail) = cb; |
| 384 | iclog->ic_callback_tail = &(cb->cb_next); |
| 385 | } |
David Chinner | 114d23a | 2008-04-10 12:18:39 +1000 | [diff] [blame] | 386 | spin_unlock(&iclog->ic_callback_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | return abortflg; |
Christoph Hellwig | 35a8a72 | 2010-02-15 23:34:54 +0000 | [diff] [blame] | 388 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
| 390 | int |
Christoph Hellwig | 35a8a72 | 2010-02-15 23:34:54 +0000 | [diff] [blame] | 391 | xfs_log_release_iclog( |
| 392 | struct xfs_mount *mp, |
| 393 | struct xlog_in_core *iclog) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | { |
Christoph Hellwig | 35a8a72 | 2010-02-15 23:34:54 +0000 | [diff] [blame] | 395 | if (xlog_state_release_iclog(mp->m_log, iclog)) { |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 396 | xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 397 | return EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | * 1. Reserve an amount of on-disk log space and return a ticket corresponding |
| 405 | * to the reservation. |
| 406 | * 2. Potentially, push buffers at tail of log to disk. |
| 407 | * |
| 408 | * Each reservation is going to reserve extra space for a log record header. |
| 409 | * When writes happen to the on-disk log, we don't subtract the length of the |
| 410 | * log record header from any reservation. By wasting space in each |
| 411 | * reservation, we prevent over allocation problems. |
| 412 | */ |
| 413 | int |
Christoph Hellwig | 35a8a72 | 2010-02-15 23:34:54 +0000 | [diff] [blame] | 414 | xfs_log_reserve( |
| 415 | struct xfs_mount *mp, |
| 416 | int unit_bytes, |
| 417 | int cnt, |
| 418 | struct xlog_ticket **ticket, |
| 419 | __uint8_t client, |
| 420 | uint flags, |
| 421 | uint t_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | { |
Christoph Hellwig | 35a8a72 | 2010-02-15 23:34:54 +0000 | [diff] [blame] | 423 | struct log *log = mp->m_log; |
| 424 | struct xlog_ticket *internal_ticket; |
| 425 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | ASSERT(client == XFS_TRANSACTION || client == XFS_LOG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
| 429 | if (XLOG_FORCED_SHUTDOWN(log)) |
| 430 | return XFS_ERROR(EIO); |
| 431 | |
| 432 | XFS_STATS_INC(xs_try_logspace); |
| 433 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | if (*ticket != NULL) { |
| 436 | ASSERT(flags & XFS_LOG_PERM_RESERV); |
Christoph Hellwig | 35a8a72 | 2010-02-15 23:34:54 +0000 | [diff] [blame] | 437 | internal_ticket = *ticket; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 438 | |
Dave Chinner | 524ee36 | 2010-05-07 11:05:05 +1000 | [diff] [blame] | 439 | /* |
| 440 | * this is a new transaction on the ticket, so we need to |
| 441 | * change the transaction ID so that the next transaction has a |
| 442 | * different TID in the log. Just add one to the existing tid |
| 443 | * so that we can see chains of rolling transactions in the log |
| 444 | * easily. |
| 445 | */ |
| 446 | internal_ticket->t_tid++; |
| 447 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 448 | trace_xfs_log_reserve(log, internal_ticket); |
| 449 | |
Dave Chinner | 2ced19c | 2010-12-21 12:09:20 +1100 | [diff] [blame] | 450 | xlog_grant_push_ail(log, internal_ticket->t_unit_res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | retval = xlog_regrant_write_log_space(log, internal_ticket); |
| 452 | } else { |
| 453 | /* may sleep if need to allocate more tickets */ |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 454 | internal_ticket = xlog_ticket_alloc(log, unit_bytes, cnt, |
Dave Chinner | 3383ca5 | 2010-05-07 11:04:17 +1000 | [diff] [blame] | 455 | client, flags, |
| 456 | KM_SLEEP|KM_MAYFAIL); |
David Chinner | eb01c9c | 2008-04-10 12:18:46 +1000 | [diff] [blame] | 457 | if (!internal_ticket) |
| 458 | return XFS_ERROR(ENOMEM); |
Tim Shimmin | 7e9c639 | 2005-09-02 16:42:05 +1000 | [diff] [blame] | 459 | internal_ticket->t_trans_type = t_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | *ticket = internal_ticket; |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 461 | |
| 462 | trace_xfs_log_reserve(log, internal_ticket); |
| 463 | |
Dave Chinner | 2ced19c | 2010-12-21 12:09:20 +1100 | [diff] [blame] | 464 | xlog_grant_push_ail(log, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | (internal_ticket->t_unit_res * |
| 466 | internal_ticket->t_cnt)); |
| 467 | retval = xlog_grant_log_space(log, internal_ticket); |
| 468 | } |
| 469 | |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 470 | if (unlikely(retval)) { |
| 471 | /* |
| 472 | * If we are failing, make sure the ticket doesn't have any |
| 473 | * current reservations. We don't want to add this back |
| 474 | * when the ticket/ transaction gets cancelled. |
| 475 | */ |
| 476 | internal_ticket->t_curr_res = 0; |
| 477 | /* ungrant will give back unit_res * t_cnt. */ |
| 478 | internal_ticket->t_cnt = 0; |
| 479 | } |
| 480 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | return retval; |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 482 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
| 484 | |
| 485 | /* |
| 486 | * Mount a log filesystem |
| 487 | * |
| 488 | * mp - ubiquitous xfs mount point structure |
| 489 | * log_target - buftarg of on-disk log device |
| 490 | * blk_offset - Start block # where block size is 512 bytes (BBSIZE) |
| 491 | * num_bblocks - Number of BBSIZE blocks in on-disk log |
| 492 | * |
| 493 | * Return error or zero. |
| 494 | */ |
| 495 | int |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 496 | xfs_log_mount( |
| 497 | xfs_mount_t *mp, |
| 498 | xfs_buftarg_t *log_target, |
| 499 | xfs_daddr_t blk_offset, |
| 500 | int num_bblks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | { |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 502 | int error; |
| 503 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 505 | xfs_notice(mp, "Mounting Filesystem"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | else { |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 507 | xfs_notice(mp, |
| 508 | "Mounting filesystem in no-recovery mode. Filesystem will be inconsistent."); |
Christoph Hellwig | bd186aa | 2007-08-30 17:21:12 +1000 | [diff] [blame] | 509 | ASSERT(mp->m_flags & XFS_MOUNT_RDONLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks); |
Dave Chinner | a6cb767 | 2009-04-06 18:39:27 +0200 | [diff] [blame] | 513 | if (IS_ERR(mp->m_log)) { |
| 514 | error = -PTR_ERR(mp->m_log); |
Dave Chinner | 644c356 | 2008-11-10 16:50:24 +1100 | [diff] [blame] | 515 | goto out; |
| 516 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | /* |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 519 | * Initialize the AIL now we have a log. |
| 520 | */ |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 521 | error = xfs_trans_ail_init(mp); |
| 522 | if (error) { |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 523 | xfs_warn(mp, "AIL initialisation failed: error %d", error); |
Christoph Hellwig | 2643075 | 2009-02-12 19:55:48 +0100 | [diff] [blame] | 524 | goto out_free_log; |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 525 | } |
David Chinner | a9c21c1 | 2008-10-30 17:39:35 +1100 | [diff] [blame] | 526 | mp->m_log->l_ailp = mp->m_ail; |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 527 | |
| 528 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | * skip log recovery on a norecovery mount. pretend it all |
| 530 | * just worked. |
| 531 | */ |
| 532 | if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) { |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 533 | int readonly = (mp->m_flags & XFS_MOUNT_RDONLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | |
| 535 | if (readonly) |
Christoph Hellwig | bd186aa | 2007-08-30 17:21:12 +1000 | [diff] [blame] | 536 | mp->m_flags &= ~XFS_MOUNT_RDONLY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
Eric Sandeen | 65be605 | 2006-01-11 15:34:19 +1100 | [diff] [blame] | 538 | error = xlog_recover(mp->m_log); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | |
| 540 | if (readonly) |
Christoph Hellwig | bd186aa | 2007-08-30 17:21:12 +1000 | [diff] [blame] | 541 | mp->m_flags |= XFS_MOUNT_RDONLY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | if (error) { |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 543 | xfs_warn(mp, "log mount/recovery failed: error %d", |
| 544 | error); |
Christoph Hellwig | 2643075 | 2009-02-12 19:55:48 +0100 | [diff] [blame] | 545 | goto out_destroy_ail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
| 549 | /* Normal transactions can now occur */ |
| 550 | mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY; |
| 551 | |
Dave Chinner | 71e330b | 2010-05-21 14:37:18 +1000 | [diff] [blame] | 552 | /* |
| 553 | * Now the log has been fully initialised and we know were our |
| 554 | * space grant counters are, we can initialise the permanent ticket |
| 555 | * needed for delayed logging to work. |
| 556 | */ |
| 557 | xlog_cil_init_post_recovery(mp->m_log); |
| 558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | return 0; |
Christoph Hellwig | 2643075 | 2009-02-12 19:55:48 +0100 | [diff] [blame] | 560 | |
| 561 | out_destroy_ail: |
| 562 | xfs_trans_ail_destroy(mp); |
| 563 | out_free_log: |
| 564 | xlog_dealloc_log(mp->m_log); |
Dave Chinner | 644c356 | 2008-11-10 16:50:24 +1100 | [diff] [blame] | 565 | out: |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 566 | return error; |
Christoph Hellwig | 2643075 | 2009-02-12 19:55:48 +0100 | [diff] [blame] | 567 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | |
| 569 | /* |
| 570 | * Finish the recovery of the file system. This is separate from |
| 571 | * the xfs_log_mount() call, because it depends on the code in |
| 572 | * xfs_mountfs() to read in the root and real-time bitmap inodes |
| 573 | * between calling xfs_log_mount() and here. |
| 574 | * |
| 575 | * mp - ubiquitous xfs mount point structure |
| 576 | */ |
| 577 | int |
Christoph Hellwig | 4249023 | 2008-08-13 16:49:32 +1000 | [diff] [blame] | 578 | xfs_log_mount_finish(xfs_mount_t *mp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | { |
| 580 | int error; |
| 581 | |
| 582 | if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) |
Christoph Hellwig | 4249023 | 2008-08-13 16:49:32 +1000 | [diff] [blame] | 583 | error = xlog_recover_finish(mp->m_log); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | else { |
| 585 | error = 0; |
Christoph Hellwig | bd186aa | 2007-08-30 17:21:12 +1000 | [diff] [blame] | 586 | ASSERT(mp->m_flags & XFS_MOUNT_RDONLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | return error; |
| 590 | } |
| 591 | |
| 592 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | * Final log writes as part of unmount. |
| 594 | * |
| 595 | * Mark the filesystem clean as unmount happens. Note that during relocation |
| 596 | * this routine needs to be executed as part of source-bag while the |
| 597 | * deallocation must not be done until source-end. |
| 598 | */ |
| 599 | |
| 600 | /* |
| 601 | * Unmount record used to have a string "Unmount filesystem--" in the |
| 602 | * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE). |
| 603 | * We just write the magic number now since that particular field isn't |
| 604 | * currently architecture converted and "nUmount" is a bit foo. |
| 605 | * As far as I know, there weren't any dependencies on the old behaviour. |
| 606 | */ |
| 607 | |
| 608 | int |
| 609 | xfs_log_unmount_write(xfs_mount_t *mp) |
| 610 | { |
| 611 | xlog_t *log = mp->m_log; |
| 612 | xlog_in_core_t *iclog; |
| 613 | #ifdef DEBUG |
| 614 | xlog_in_core_t *first_iclog; |
| 615 | #endif |
Christoph Hellwig | 35a8a72 | 2010-02-15 23:34:54 +0000 | [diff] [blame] | 616 | xlog_ticket_t *tic = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | xfs_lsn_t lsn; |
| 618 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | /* |
| 621 | * Don't write out unmount record on read-only mounts. |
| 622 | * Or, if we are doing a forced umount (typically because of IO errors). |
| 623 | */ |
Christoph Hellwig | bd186aa | 2007-08-30 17:21:12 +1000 | [diff] [blame] | 624 | if (mp->m_flags & XFS_MOUNT_RDONLY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | return 0; |
| 626 | |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 627 | error = _xfs_log_force(mp, XFS_LOG_SYNC, NULL); |
David Chinner | b911ca0 | 2008-04-10 12:24:30 +1000 | [diff] [blame] | 628 | ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | |
| 630 | #ifdef DEBUG |
| 631 | first_iclog = iclog = log->l_iclog; |
| 632 | do { |
| 633 | if (!(iclog->ic_state & XLOG_STATE_IOERROR)) { |
| 634 | ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE); |
| 635 | ASSERT(iclog->ic_offset == 0); |
| 636 | } |
| 637 | iclog = iclog->ic_next; |
| 638 | } while (iclog != first_iclog); |
| 639 | #endif |
| 640 | if (! (XLOG_FORCED_SHUTDOWN(log))) { |
Tim Shimmin | 955e47a | 2006-09-28 11:04:16 +1000 | [diff] [blame] | 641 | error = xfs_log_reserve(mp, 600, 1, &tic, |
| 642 | XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | if (!error) { |
Dave Chinner | 55b6633 | 2010-03-23 11:43:17 +1100 | [diff] [blame] | 644 | /* the data section must be 32 bit size aligned */ |
| 645 | struct { |
| 646 | __uint16_t magic; |
| 647 | __uint16_t pad1; |
| 648 | __uint32_t pad2; /* may as well make it 64 bits */ |
| 649 | } magic = { |
| 650 | .magic = XLOG_UNMOUNT_TYPE, |
| 651 | }; |
| 652 | struct xfs_log_iovec reg = { |
Christoph Hellwig | 4e0d5f9 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 653 | .i_addr = &magic, |
Dave Chinner | 55b6633 | 2010-03-23 11:43:17 +1100 | [diff] [blame] | 654 | .i_len = sizeof(magic), |
| 655 | .i_type = XLOG_REG_TYPE_UNMOUNT, |
| 656 | }; |
| 657 | struct xfs_log_vec vec = { |
| 658 | .lv_niovecs = 1, |
| 659 | .lv_iovecp = ®, |
| 660 | }; |
| 661 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | /* remove inited flag */ |
Dave Chinner | 55b6633 | 2010-03-23 11:43:17 +1100 | [diff] [blame] | 663 | tic->t_flags = 0; |
| 664 | error = xlog_write(log, &vec, tic, &lsn, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | NULL, XLOG_UNMOUNT_TRANS); |
| 666 | /* |
| 667 | * At this point, we're umounting anyway, |
| 668 | * so there's no point in transitioning log state |
| 669 | * to IOERROR. Just continue... |
| 670 | */ |
| 671 | } |
| 672 | |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 673 | if (error) |
| 674 | xfs_alert(mp, "%s: unmount record failed", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | |
| 676 | |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 677 | spin_lock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | iclog = log->l_iclog; |
David Chinner | 155cc6b | 2008-03-06 13:44:14 +1100 | [diff] [blame] | 679 | atomic_inc(&iclog->ic_refcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | xlog_state_want_sync(log, iclog); |
Christoph Hellwig | 39e2def | 2008-12-03 12:20:28 +0100 | [diff] [blame] | 681 | spin_unlock(&log->l_icloglock); |
David Chinner | 1bb7d6b | 2008-04-10 12:24:38 +1000 | [diff] [blame] | 682 | error = xlog_state_release_iclog(log, iclog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 684 | spin_lock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | if (!(iclog->ic_state == XLOG_STATE_ACTIVE || |
| 686 | iclog->ic_state == XLOG_STATE_DIRTY)) { |
| 687 | if (!XLOG_FORCED_SHUTDOWN(log)) { |
Dave Chinner | eb40a87 | 2010-12-21 12:09:01 +1100 | [diff] [blame] | 688 | xlog_wait(&iclog->ic_force_wait, |
| 689 | &log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | } else { |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 691 | spin_unlock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | } |
| 693 | } else { |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 694 | spin_unlock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | } |
Tim Shimmin | 955e47a | 2006-09-28 11:04:16 +1000 | [diff] [blame] | 696 | if (tic) { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 697 | trace_xfs_log_umount_write(log, tic); |
Tim Shimmin | 955e47a | 2006-09-28 11:04:16 +1000 | [diff] [blame] | 698 | xlog_ungrant_log_space(log, tic); |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 699 | xfs_log_ticket_put(tic); |
Tim Shimmin | 955e47a | 2006-09-28 11:04:16 +1000 | [diff] [blame] | 700 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | } else { |
| 702 | /* |
| 703 | * We're already in forced_shutdown mode, couldn't |
| 704 | * even attempt to write out the unmount transaction. |
| 705 | * |
| 706 | * Go through the motions of sync'ing and releasing |
| 707 | * the iclog, even though no I/O will actually happen, |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 708 | * we need to wait for other log I/Os that may already |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | * be in progress. Do this as a separate section of |
| 710 | * code so we'll know if we ever get stuck here that |
| 711 | * we're in this odd situation of trying to unmount |
| 712 | * a file system that went into forced_shutdown as |
| 713 | * the result of an unmount.. |
| 714 | */ |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 715 | spin_lock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | iclog = log->l_iclog; |
David Chinner | 155cc6b | 2008-03-06 13:44:14 +1100 | [diff] [blame] | 717 | atomic_inc(&iclog->ic_refcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | |
| 719 | xlog_state_want_sync(log, iclog); |
Christoph Hellwig | 39e2def | 2008-12-03 12:20:28 +0100 | [diff] [blame] | 720 | spin_unlock(&log->l_icloglock); |
David Chinner | 1bb7d6b | 2008-04-10 12:24:38 +1000 | [diff] [blame] | 721 | error = xlog_state_release_iclog(log, iclog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 723 | spin_lock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | |
| 725 | if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE |
| 726 | || iclog->ic_state == XLOG_STATE_DIRTY |
| 727 | || iclog->ic_state == XLOG_STATE_IOERROR) ) { |
| 728 | |
Dave Chinner | eb40a87 | 2010-12-21 12:09:01 +1100 | [diff] [blame] | 729 | xlog_wait(&iclog->ic_force_wait, |
| 730 | &log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | } else { |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 732 | spin_unlock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | } |
| 734 | } |
| 735 | |
David Chinner | 1bb7d6b | 2008-04-10 12:24:38 +1000 | [diff] [blame] | 736 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | } /* xfs_log_unmount_write */ |
| 738 | |
| 739 | /* |
| 740 | * Deallocate log structures for unmount/relocation. |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 741 | * |
| 742 | * We need to stop the aild from running before we destroy |
| 743 | * and deallocate the log as the aild references the log. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | */ |
| 745 | void |
Christoph Hellwig | 21b699c | 2009-03-16 08:19:29 +0100 | [diff] [blame] | 746 | xfs_log_unmount(xfs_mount_t *mp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | { |
David Chinner | 249a8c1 | 2008-02-05 12:13:32 +1100 | [diff] [blame] | 748 | xfs_trans_ail_destroy(mp); |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 749 | xlog_dealloc_log(mp->m_log); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | } |
| 751 | |
Dave Chinner | 43f5efc | 2010-03-23 10:10:00 +1100 | [diff] [blame] | 752 | void |
| 753 | xfs_log_item_init( |
| 754 | struct xfs_mount *mp, |
| 755 | struct xfs_log_item *item, |
| 756 | int type, |
Christoph Hellwig | 272e42b | 2011-10-28 09:54:24 +0000 | [diff] [blame] | 757 | const struct xfs_item_ops *ops) |
Dave Chinner | 43f5efc | 2010-03-23 10:10:00 +1100 | [diff] [blame] | 758 | { |
| 759 | item->li_mountp = mp; |
| 760 | item->li_ailp = mp->m_ail; |
| 761 | item->li_type = type; |
| 762 | item->li_ops = ops; |
Dave Chinner | 71e330b | 2010-05-21 14:37:18 +1000 | [diff] [blame] | 763 | item->li_lv = NULL; |
| 764 | |
| 765 | INIT_LIST_HEAD(&item->li_ail); |
| 766 | INIT_LIST_HEAD(&item->li_cil); |
Dave Chinner | 43f5efc | 2010-03-23 10:10:00 +1100 | [diff] [blame] | 767 | } |
| 768 | |
Christoph Hellwig | 09a423a | 2012-02-20 02:31:20 +0000 | [diff] [blame] | 769 | /* |
| 770 | * Wake up processes waiting for log space after we have moved the log tail. |
Christoph Hellwig | 09a423a | 2012-02-20 02:31:20 +0000 | [diff] [blame] | 771 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | void |
Christoph Hellwig | 09a423a | 2012-02-20 02:31:20 +0000 | [diff] [blame] | 773 | xfs_log_space_wake( |
Christoph Hellwig | cfb7cdc | 2012-02-20 02:31:23 +0000 | [diff] [blame] | 774 | struct xfs_mount *mp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | { |
Christoph Hellwig | 09a423a | 2012-02-20 02:31:20 +0000 | [diff] [blame] | 776 | struct log *log = mp->m_log; |
Christoph Hellwig | cfb7cdc | 2012-02-20 02:31:23 +0000 | [diff] [blame] | 777 | int free_bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | if (XLOG_FORCED_SHUTDOWN(log)) |
| 780 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | |
Dave Chinner | 3f16b98 | 2010-12-21 12:29:01 +1100 | [diff] [blame] | 782 | if (!list_empty_careful(&log->l_writeq)) { |
Christoph Hellwig | 09a423a | 2012-02-20 02:31:20 +0000 | [diff] [blame] | 783 | ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY)); |
| 784 | |
Dave Chinner | 3f16b98 | 2010-12-21 12:29:01 +1100 | [diff] [blame] | 785 | spin_lock(&log->l_grant_write_lock); |
Dave Chinner | a69ed03 | 2010-12-21 12:08:20 +1100 | [diff] [blame] | 786 | free_bytes = xlog_space_left(log, &log->l_grant_write_head); |
Christoph Hellwig | cfb7cdc | 2012-02-20 02:31:23 +0000 | [diff] [blame] | 787 | xlog_writeq_wake(log, &free_bytes); |
Dave Chinner | 3f16b98 | 2010-12-21 12:29:01 +1100 | [diff] [blame] | 788 | spin_unlock(&log->l_grant_write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | } |
Dave Chinner | 1054794 | 2010-12-21 12:02:25 +1100 | [diff] [blame] | 790 | |
Dave Chinner | 3f16b98 | 2010-12-21 12:29:01 +1100 | [diff] [blame] | 791 | if (!list_empty_careful(&log->l_reserveq)) { |
Christoph Hellwig | 09a423a | 2012-02-20 02:31:20 +0000 | [diff] [blame] | 792 | ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY)); |
| 793 | |
Dave Chinner | 3f16b98 | 2010-12-21 12:29:01 +1100 | [diff] [blame] | 794 | spin_lock(&log->l_grant_reserve_lock); |
Dave Chinner | a69ed03 | 2010-12-21 12:08:20 +1100 | [diff] [blame] | 795 | free_bytes = xlog_space_left(log, &log->l_grant_reserve_head); |
Christoph Hellwig | cfb7cdc | 2012-02-20 02:31:23 +0000 | [diff] [blame] | 796 | xlog_reserveq_wake(log, &free_bytes); |
Dave Chinner | 3f16b98 | 2010-12-21 12:29:01 +1100 | [diff] [blame] | 797 | spin_unlock(&log->l_grant_reserve_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | } |
Dave Chinner | 3f16b98 | 2010-12-21 12:29:01 +1100 | [diff] [blame] | 799 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | |
| 801 | /* |
| 802 | * Determine if we have a transaction that has gone to disk |
Dave Chinner | b6f8dd4 | 2010-04-13 15:06:44 +1000 | [diff] [blame] | 803 | * that needs to be covered. To begin the transition to the idle state |
| 804 | * firstly the log needs to be idle (no AIL and nothing in the iclogs). |
| 805 | * If we are then in a state where covering is needed, the caller is informed |
| 806 | * that dummy transactions are required to move the log into the idle state. |
| 807 | * |
| 808 | * Because this is called as part of the sync process, we should also indicate |
| 809 | * that dummy transactions should be issued in anything but the covered or |
| 810 | * idle states. This ensures that the log tail is accurately reflected in |
| 811 | * the log at the end of the sync, hence if a crash occurrs avoids replay |
| 812 | * of transactions where the metadata is already on disk. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | */ |
| 814 | int |
| 815 | xfs_log_need_covered(xfs_mount_t *mp) |
| 816 | { |
David Chinner | 27d8d5f | 2008-10-30 17:38:39 +1100 | [diff] [blame] | 817 | int needed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | xlog_t *log = mp->m_log; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 820 | if (!xfs_fs_writable(mp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | return 0; |
| 822 | |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 823 | spin_lock(&log->l_icloglock); |
Dave Chinner | b6f8dd4 | 2010-04-13 15:06:44 +1000 | [diff] [blame] | 824 | switch (log->l_covered_state) { |
| 825 | case XLOG_STATE_COVER_DONE: |
| 826 | case XLOG_STATE_COVER_DONE2: |
| 827 | case XLOG_STATE_COVER_IDLE: |
| 828 | break; |
| 829 | case XLOG_STATE_COVER_NEED: |
| 830 | case XLOG_STATE_COVER_NEED2: |
Dave Chinner | fd07484 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 831 | if (!xfs_ail_min_lsn(log->l_ailp) && |
Dave Chinner | b6f8dd4 | 2010-04-13 15:06:44 +1000 | [diff] [blame] | 832 | xlog_iclogs_empty(log)) { |
| 833 | if (log->l_covered_state == XLOG_STATE_COVER_NEED) |
| 834 | log->l_covered_state = XLOG_STATE_COVER_DONE; |
| 835 | else |
| 836 | log->l_covered_state = XLOG_STATE_COVER_DONE2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | } |
Dave Chinner | b6f8dd4 | 2010-04-13 15:06:44 +1000 | [diff] [blame] | 838 | /* FALLTHRU */ |
| 839 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | needed = 1; |
Dave Chinner | b6f8dd4 | 2010-04-13 15:06:44 +1000 | [diff] [blame] | 841 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | } |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 843 | spin_unlock(&log->l_icloglock); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 844 | return needed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | } |
| 846 | |
Christoph Hellwig | 09a423a | 2012-02-20 02:31:20 +0000 | [diff] [blame] | 847 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | * We may be holding the log iclog lock upon entering this routine. |
| 849 | */ |
| 850 | xfs_lsn_t |
Dave Chinner | 1c3cb9e | 2010-12-21 12:28:39 +1100 | [diff] [blame] | 851 | xlog_assign_tail_lsn( |
| 852 | struct xfs_mount *mp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | { |
Dave Chinner | 1c3cb9e | 2010-12-21 12:28:39 +1100 | [diff] [blame] | 854 | xfs_lsn_t tail_lsn; |
| 855 | struct log *log = mp->m_log; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | |
Christoph Hellwig | 09a423a | 2012-02-20 02:31:20 +0000 | [diff] [blame] | 857 | /* |
| 858 | * To make sure we always have a valid LSN for the log tail we keep |
| 859 | * track of the last LSN which was committed in log->l_last_sync_lsn, |
| 860 | * and use that when the AIL was empty and xfs_ail_min_lsn returns 0. |
| 861 | * |
| 862 | * If the AIL has been emptied we also need to wake any process |
| 863 | * waiting for this condition. |
| 864 | */ |
Dave Chinner | fd07484 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 865 | tail_lsn = xfs_ail_min_lsn(mp->m_ail); |
Dave Chinner | 84f3c68 | 2010-12-03 22:11:29 +1100 | [diff] [blame] | 866 | if (!tail_lsn) |
| 867 | tail_lsn = atomic64_read(&log->l_last_sync_lsn); |
Dave Chinner | 1c3cb9e | 2010-12-21 12:28:39 +1100 | [diff] [blame] | 868 | atomic64_set(&log->l_tail_lsn, tail_lsn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | return tail_lsn; |
Dave Chinner | 1c3cb9e | 2010-12-21 12:28:39 +1100 | [diff] [blame] | 870 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | |
| 872 | /* |
| 873 | * Return the space in the log between the tail and the head. The head |
| 874 | * is passed in the cycle/bytes formal parms. In the special case where |
| 875 | * the reserve head has wrapped passed the tail, this calculation is no |
| 876 | * longer valid. In this case, just return 0 which means there is no space |
| 877 | * in the log. This works for all places where this function is called |
| 878 | * with the reserve head. Of course, if the write head were to ever |
| 879 | * wrap the tail, we should blow up. Rather than catch this case here, |
| 880 | * we depend on other ASSERTions in other parts of the code. XXXmiken |
| 881 | * |
| 882 | * This code also handles the case where the reservation head is behind |
| 883 | * the tail. The details of this case are described below, but the end |
| 884 | * result is that we return the size of the log as the amount of space left. |
| 885 | */ |
David Chinner | a8272ce | 2007-11-23 16:28:09 +1100 | [diff] [blame] | 886 | STATIC int |
Dave Chinner | a69ed03 | 2010-12-21 12:08:20 +1100 | [diff] [blame] | 887 | xlog_space_left( |
| 888 | struct log *log, |
Dave Chinner | c8a09ff | 2010-12-04 00:02:40 +1100 | [diff] [blame] | 889 | atomic64_t *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | { |
Dave Chinner | a69ed03 | 2010-12-21 12:08:20 +1100 | [diff] [blame] | 891 | int free_bytes; |
| 892 | int tail_bytes; |
| 893 | int tail_cycle; |
| 894 | int head_cycle; |
| 895 | int head_bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | |
Dave Chinner | a69ed03 | 2010-12-21 12:08:20 +1100 | [diff] [blame] | 897 | xlog_crack_grant_head(head, &head_cycle, &head_bytes); |
Dave Chinner | 1c3cb9e | 2010-12-21 12:28:39 +1100 | [diff] [blame] | 898 | xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_bytes); |
| 899 | tail_bytes = BBTOB(tail_bytes); |
Dave Chinner | a69ed03 | 2010-12-21 12:08:20 +1100 | [diff] [blame] | 900 | if (tail_cycle == head_cycle && head_bytes >= tail_bytes) |
| 901 | free_bytes = log->l_logsize - (head_bytes - tail_bytes); |
| 902 | else if (tail_cycle + 1 < head_cycle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | return 0; |
Dave Chinner | a69ed03 | 2010-12-21 12:08:20 +1100 | [diff] [blame] | 904 | else if (tail_cycle < head_cycle) { |
| 905 | ASSERT(tail_cycle == (head_cycle - 1)); |
| 906 | free_bytes = tail_bytes - head_bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | } else { |
| 908 | /* |
| 909 | * The reservation head is behind the tail. |
| 910 | * In this case we just want to return the size of the |
| 911 | * log as the amount of space left. |
| 912 | */ |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 913 | xfs_alert(log->l_mp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | "xlog_space_left: head behind tail\n" |
| 915 | " tail_cycle = %d, tail_bytes = %d\n" |
| 916 | " GH cycle = %d, GH bytes = %d", |
Dave Chinner | a69ed03 | 2010-12-21 12:08:20 +1100 | [diff] [blame] | 917 | tail_cycle, tail_bytes, head_cycle, head_bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | ASSERT(0); |
| 919 | free_bytes = log->l_logsize; |
| 920 | } |
| 921 | return free_bytes; |
Dave Chinner | a69ed03 | 2010-12-21 12:08:20 +1100 | [diff] [blame] | 922 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | |
| 924 | |
| 925 | /* |
| 926 | * Log function which is called when an io completes. |
| 927 | * |
| 928 | * The log manager needs its own routine, in order to control what |
| 929 | * happens with the buffer after the write completes. |
| 930 | */ |
| 931 | void |
| 932 | xlog_iodone(xfs_buf_t *bp) |
| 933 | { |
Christoph Hellwig | adadbee | 2011-07-13 13:43:49 +0200 | [diff] [blame] | 934 | xlog_in_core_t *iclog = bp->b_fspriv; |
| 935 | xlog_t *l = iclog->ic_log; |
| 936 | int aborted = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | |
| 938 | /* |
| 939 | * Race to shutdown the filesystem if we see an error. |
| 940 | */ |
Chandra Seetharaman | 5a52c2a58 | 2011-07-22 23:39:51 +0000 | [diff] [blame] | 941 | if (XFS_TEST_ERROR((xfs_buf_geterror(bp)), l->l_mp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) { |
Christoph Hellwig | 901796a | 2011-10-10 16:52:49 +0000 | [diff] [blame] | 943 | xfs_buf_ioerror_alert(bp, __func__); |
Christoph Hellwig | c867cb6 | 2011-10-10 16:52:46 +0000 | [diff] [blame] | 944 | xfs_buf_stale(bp); |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 945 | xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | /* |
| 947 | * This flag will be propagated to the trans-committed |
| 948 | * callback routines to let them know that the log-commit |
| 949 | * didn't succeed. |
| 950 | */ |
| 951 | aborted = XFS_LI_ABORTED; |
| 952 | } else if (iclog->ic_state & XLOG_STATE_IOERROR) { |
| 953 | aborted = XFS_LI_ABORTED; |
| 954 | } |
David Chinner | 3db296f | 2007-05-14 18:24:16 +1000 | [diff] [blame] | 955 | |
| 956 | /* log I/O is always issued ASYNC */ |
| 957 | ASSERT(XFS_BUF_ISASYNC(bp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | xlog_state_done_syncing(iclog, aborted); |
David Chinner | 3db296f | 2007-05-14 18:24:16 +1000 | [diff] [blame] | 959 | /* |
| 960 | * do not reference the buffer (bp) here as we could race |
| 961 | * with it being freed after writing the unmount record to the |
| 962 | * log. |
| 963 | */ |
| 964 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | } /* xlog_iodone */ |
| 966 | |
| 967 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | * Return size of each in-core log record buffer. |
| 969 | * |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 970 | * All machines get 8 x 32kB buffers by default, unless tuned otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | * |
| 972 | * If the filesystem blocksize is too large, we may need to choose a |
| 973 | * larger size since the directory code currently logs entire blocks. |
| 974 | */ |
| 975 | |
| 976 | STATIC void |
| 977 | xlog_get_iclog_buffer_size(xfs_mount_t *mp, |
| 978 | xlog_t *log) |
| 979 | { |
| 980 | int size; |
| 981 | int xhdrs; |
| 982 | |
Eric Sandeen | 1cb5125 | 2007-08-16 16:24:43 +1000 | [diff] [blame] | 983 | if (mp->m_logbufs <= 0) |
| 984 | log->l_iclog_bufs = XLOG_MAX_ICLOGS; |
| 985 | else |
Nathan Scott | cfcbbbd | 2005-11-02 15:12:04 +1100 | [diff] [blame] | 986 | log->l_iclog_bufs = mp->m_logbufs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | |
| 988 | /* |
| 989 | * Buffer size passed in from mount system call. |
| 990 | */ |
Nathan Scott | cfcbbbd | 2005-11-02 15:12:04 +1100 | [diff] [blame] | 991 | if (mp->m_logbsize > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | size = log->l_iclog_size = mp->m_logbsize; |
| 993 | log->l_iclog_size_log = 0; |
| 994 | while (size != 1) { |
| 995 | log->l_iclog_size_log++; |
| 996 | size >>= 1; |
| 997 | } |
| 998 | |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 999 | if (xfs_sb_version_haslogv2(&mp->m_sb)) { |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 1000 | /* # headers = size / 32k |
| 1001 | * one header holds cycles from 32k of data |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | */ |
| 1003 | |
| 1004 | xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE; |
| 1005 | if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE) |
| 1006 | xhdrs++; |
| 1007 | log->l_iclog_hsize = xhdrs << BBSHIFT; |
| 1008 | log->l_iclog_heads = xhdrs; |
| 1009 | } else { |
| 1010 | ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE); |
| 1011 | log->l_iclog_hsize = BBSIZE; |
| 1012 | log->l_iclog_heads = 1; |
| 1013 | } |
Nathan Scott | cfcbbbd | 2005-11-02 15:12:04 +1100 | [diff] [blame] | 1014 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | } |
| 1016 | |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 1017 | /* All machines use 32kB buffers by default. */ |
Eric Sandeen | 1cb5125 | 2007-08-16 16:24:43 +1000 | [diff] [blame] | 1018 | log->l_iclog_size = XLOG_BIG_RECORD_BSIZE; |
| 1019 | log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | |
| 1021 | /* the default log size is 16k or 32k which is one header sector */ |
| 1022 | log->l_iclog_hsize = BBSIZE; |
| 1023 | log->l_iclog_heads = 1; |
| 1024 | |
Christoph Hellwig | 7153f8b | 2009-02-09 08:36:46 +0100 | [diff] [blame] | 1025 | done: |
| 1026 | /* are we being asked to make the sizes selected above visible? */ |
Nathan Scott | cfcbbbd | 2005-11-02 15:12:04 +1100 | [diff] [blame] | 1027 | if (mp->m_logbufs == 0) |
| 1028 | mp->m_logbufs = log->l_iclog_bufs; |
| 1029 | if (mp->m_logbsize == 0) |
| 1030 | mp->m_logbsize = log->l_iclog_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | } /* xlog_get_iclog_buffer_size */ |
| 1032 | |
| 1033 | |
| 1034 | /* |
| 1035 | * This routine initializes some of the log structure for a given mount point. |
| 1036 | * Its primary purpose is to fill in enough, so recovery can occur. However, |
| 1037 | * some other stuff may be filled in too. |
| 1038 | */ |
| 1039 | STATIC xlog_t * |
| 1040 | xlog_alloc_log(xfs_mount_t *mp, |
| 1041 | xfs_buftarg_t *log_target, |
| 1042 | xfs_daddr_t blk_offset, |
| 1043 | int num_bblks) |
| 1044 | { |
| 1045 | xlog_t *log; |
| 1046 | xlog_rec_header_t *head; |
| 1047 | xlog_in_core_t **iclogp; |
| 1048 | xlog_in_core_t *iclog, *prev_iclog=NULL; |
| 1049 | xfs_buf_t *bp; |
| 1050 | int i; |
Dave Chinner | a6cb767 | 2009-04-06 18:39:27 +0200 | [diff] [blame] | 1051 | int error = ENOMEM; |
Alex Elder | 69ce58f | 2010-04-20 17:09:59 +1000 | [diff] [blame] | 1052 | uint log2_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | |
Dave Chinner | 644c356 | 2008-11-10 16:50:24 +1100 | [diff] [blame] | 1054 | log = kmem_zalloc(sizeof(xlog_t), KM_MAYFAIL); |
Dave Chinner | a6cb767 | 2009-04-06 18:39:27 +0200 | [diff] [blame] | 1055 | if (!log) { |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 1056 | xfs_warn(mp, "Log allocation failed: No memory!"); |
Dave Chinner | a6cb767 | 2009-04-06 18:39:27 +0200 | [diff] [blame] | 1057 | goto out; |
| 1058 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | |
| 1060 | log->l_mp = mp; |
| 1061 | log->l_targ = log_target; |
| 1062 | log->l_logsize = BBTOB(num_bblks); |
| 1063 | log->l_logBBstart = blk_offset; |
| 1064 | log->l_logBBsize = num_bblks; |
| 1065 | log->l_covered_state = XLOG_STATE_COVER_IDLE; |
| 1066 | log->l_flags |= XLOG_ACTIVE_RECOVERY; |
| 1067 | |
| 1068 | log->l_prev_block = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */ |
Dave Chinner | 1c3cb9e | 2010-12-21 12:28:39 +1100 | [diff] [blame] | 1070 | xlog_assign_atomic_lsn(&log->l_tail_lsn, 1, 0); |
| 1071 | xlog_assign_atomic_lsn(&log->l_last_sync_lsn, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | log->l_curr_cycle = 1; /* 0 is bad since this is initial value */ |
Dave Chinner | a69ed03 | 2010-12-21 12:08:20 +1100 | [diff] [blame] | 1073 | xlog_assign_grant_head(&log->l_grant_reserve_head, 1, 0); |
| 1074 | xlog_assign_grant_head(&log->l_grant_write_head, 1, 0); |
Dave Chinner | 1054794 | 2010-12-21 12:02:25 +1100 | [diff] [blame] | 1075 | INIT_LIST_HEAD(&log->l_reserveq); |
| 1076 | INIT_LIST_HEAD(&log->l_writeq); |
Dave Chinner | 3f16b98 | 2010-12-21 12:29:01 +1100 | [diff] [blame] | 1077 | spin_lock_init(&log->l_grant_reserve_lock); |
| 1078 | spin_lock_init(&log->l_grant_write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | |
Dave Chinner | a6cb767 | 2009-04-06 18:39:27 +0200 | [diff] [blame] | 1080 | error = EFSCORRUPTED; |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 1081 | if (xfs_sb_version_hassector(&mp->m_sb)) { |
Alex Elder | 69ce58f | 2010-04-20 17:09:59 +1000 | [diff] [blame] | 1082 | log2_size = mp->m_sb.sb_logsectlog; |
| 1083 | if (log2_size < BBSHIFT) { |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 1084 | xfs_warn(mp, "Log sector size too small (0x%x < 0x%x)", |
| 1085 | log2_size, BBSHIFT); |
Alex Elder | 69ce58f | 2010-04-20 17:09:59 +1000 | [diff] [blame] | 1086 | goto out_free_log; |
| 1087 | } |
| 1088 | |
| 1089 | log2_size -= BBSHIFT; |
| 1090 | if (log2_size > mp->m_sectbb_log) { |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 1091 | xfs_warn(mp, "Log sector size too large (0x%x > 0x%x)", |
| 1092 | log2_size, mp->m_sectbb_log); |
Dave Chinner | a6cb767 | 2009-04-06 18:39:27 +0200 | [diff] [blame] | 1093 | goto out_free_log; |
| 1094 | } |
| 1095 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | /* for larger sector sizes, must have v2 or external log */ |
Alex Elder | 69ce58f | 2010-04-20 17:09:59 +1000 | [diff] [blame] | 1097 | if (log2_size && log->l_logBBstart > 0 && |
| 1098 | !xfs_sb_version_haslogv2(&mp->m_sb)) { |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 1099 | xfs_warn(mp, |
| 1100 | "log sector size (0x%x) invalid for configuration.", |
| 1101 | log2_size); |
Dave Chinner | a6cb767 | 2009-04-06 18:39:27 +0200 | [diff] [blame] | 1102 | goto out_free_log; |
| 1103 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | } |
Alex Elder | 69ce58f | 2010-04-20 17:09:59 +1000 | [diff] [blame] | 1105 | log->l_sectBBsize = 1 << log2_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | |
| 1107 | xlog_get_iclog_buffer_size(mp, log); |
| 1108 | |
Dave Chinner | a6cb767 | 2009-04-06 18:39:27 +0200 | [diff] [blame] | 1109 | error = ENOMEM; |
Christoph Hellwig | 4347b9d | 2011-10-10 16:52:48 +0000 | [diff] [blame] | 1110 | bp = xfs_buf_alloc(mp->m_logdev_targp, 0, log->l_iclog_size, 0); |
Dave Chinner | 644c356 | 2008-11-10 16:50:24 +1100 | [diff] [blame] | 1111 | if (!bp) |
| 1112 | goto out_free_log; |
Christoph Hellwig | cb669ca | 2011-07-13 13:43:49 +0200 | [diff] [blame] | 1113 | bp->b_iodone = xlog_iodone; |
Christoph Hellwig | 0c842ad | 2011-07-08 14:36:19 +0200 | [diff] [blame] | 1114 | ASSERT(xfs_buf_islocked(bp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | log->l_xbuf = bp; |
| 1116 | |
Eric Sandeen | 007c61c | 2007-10-11 17:43:56 +1000 | [diff] [blame] | 1117 | spin_lock_init(&log->l_icloglock); |
Dave Chinner | eb40a87 | 2010-12-21 12:09:01 +1100 | [diff] [blame] | 1118 | init_waitqueue_head(&log->l_flush_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | |
| 1120 | /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */ |
| 1121 | ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0); |
| 1122 | |
| 1123 | iclogp = &log->l_iclog; |
| 1124 | /* |
| 1125 | * The amount of memory to allocate for the iclog structure is |
| 1126 | * rather funky due to the way the structure is defined. It is |
| 1127 | * done this way so that we can use different sizes for machines |
| 1128 | * with different amounts of memory. See the definition of |
| 1129 | * xlog_in_core_t in xfs_log_priv.h for details. |
| 1130 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | ASSERT(log->l_iclog_size >= 4096); |
| 1132 | for (i=0; i < log->l_iclog_bufs; i++) { |
Dave Chinner | 644c356 | 2008-11-10 16:50:24 +1100 | [diff] [blame] | 1133 | *iclogp = kmem_zalloc(sizeof(xlog_in_core_t), KM_MAYFAIL); |
| 1134 | if (!*iclogp) |
| 1135 | goto out_free_iclog; |
| 1136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | iclog = *iclogp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | iclog->ic_prev = prev_iclog; |
| 1139 | prev_iclog = iclog; |
Christoph Hellwig | 1fa40b0 | 2007-05-14 18:23:50 +1000 | [diff] [blame] | 1140 | |
Dave Chinner | 686865f | 2010-09-24 20:07:47 +1000 | [diff] [blame] | 1141 | bp = xfs_buf_get_uncached(mp->m_logdev_targp, |
| 1142 | log->l_iclog_size, 0); |
Dave Chinner | 644c356 | 2008-11-10 16:50:24 +1100 | [diff] [blame] | 1143 | if (!bp) |
| 1144 | goto out_free_iclog; |
Christoph Hellwig | c8da0fa | 2011-07-08 14:36:25 +0200 | [diff] [blame] | 1145 | |
Christoph Hellwig | cb669ca | 2011-07-13 13:43:49 +0200 | [diff] [blame] | 1146 | bp->b_iodone = xlog_iodone; |
Christoph Hellwig | 1fa40b0 | 2007-05-14 18:23:50 +1000 | [diff] [blame] | 1147 | iclog->ic_bp = bp; |
Christoph Hellwig | b28708d | 2008-11-28 14:23:38 +1100 | [diff] [blame] | 1148 | iclog->ic_data = bp->b_addr; |
David Chinner | 4679b2d | 2008-04-10 12:18:54 +1000 | [diff] [blame] | 1149 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header); |
David Chinner | 4679b2d | 2008-04-10 12:18:54 +1000 | [diff] [blame] | 1151 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | head = &iclog->ic_header; |
| 1153 | memset(head, 0, sizeof(xlog_rec_header_t)); |
Christoph Hellwig | b53e675 | 2007-10-12 10:59:34 +1000 | [diff] [blame] | 1154 | head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM); |
| 1155 | head->h_version = cpu_to_be32( |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 1156 | xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1); |
Christoph Hellwig | b53e675 | 2007-10-12 10:59:34 +1000 | [diff] [blame] | 1157 | head->h_size = cpu_to_be32(log->l_iclog_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | /* new fields */ |
Christoph Hellwig | b53e675 | 2007-10-12 10:59:34 +1000 | [diff] [blame] | 1159 | head->h_fmt = cpu_to_be32(XLOG_FMT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t)); |
| 1161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize; |
| 1163 | iclog->ic_state = XLOG_STATE_ACTIVE; |
| 1164 | iclog->ic_log = log; |
David Chinner | 114d23a | 2008-04-10 12:18:39 +1000 | [diff] [blame] | 1165 | atomic_set(&iclog->ic_refcnt, 0); |
| 1166 | spin_lock_init(&iclog->ic_callback_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | iclog->ic_callback_tail = &(iclog->ic_callback); |
Christoph Hellwig | b28708d | 2008-11-28 14:23:38 +1100 | [diff] [blame] | 1168 | iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | |
Christoph Hellwig | 0c842ad | 2011-07-08 14:36:19 +0200 | [diff] [blame] | 1170 | ASSERT(xfs_buf_islocked(iclog->ic_bp)); |
Dave Chinner | eb40a87 | 2010-12-21 12:09:01 +1100 | [diff] [blame] | 1171 | init_waitqueue_head(&iclog->ic_force_wait); |
| 1172 | init_waitqueue_head(&iclog->ic_write_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | |
| 1174 | iclogp = &iclog->ic_next; |
| 1175 | } |
| 1176 | *iclogp = log->l_iclog; /* complete ring */ |
| 1177 | log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */ |
| 1178 | |
Dave Chinner | 71e330b | 2010-05-21 14:37:18 +1000 | [diff] [blame] | 1179 | error = xlog_cil_init(log); |
| 1180 | if (error) |
| 1181 | goto out_free_iclog; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | return log; |
Dave Chinner | 644c356 | 2008-11-10 16:50:24 +1100 | [diff] [blame] | 1183 | |
| 1184 | out_free_iclog: |
| 1185 | for (iclog = log->l_iclog; iclog; iclog = prev_iclog) { |
| 1186 | prev_iclog = iclog->ic_next; |
Dave Chinner | eb40a87 | 2010-12-21 12:09:01 +1100 | [diff] [blame] | 1187 | if (iclog->ic_bp) |
Dave Chinner | 644c356 | 2008-11-10 16:50:24 +1100 | [diff] [blame] | 1188 | xfs_buf_free(iclog->ic_bp); |
Dave Chinner | 644c356 | 2008-11-10 16:50:24 +1100 | [diff] [blame] | 1189 | kmem_free(iclog); |
| 1190 | } |
| 1191 | spinlock_destroy(&log->l_icloglock); |
Dave Chinner | 644c356 | 2008-11-10 16:50:24 +1100 | [diff] [blame] | 1192 | xfs_buf_free(log->l_xbuf); |
| 1193 | out_free_log: |
| 1194 | kmem_free(log); |
Dave Chinner | a6cb767 | 2009-04-06 18:39:27 +0200 | [diff] [blame] | 1195 | out: |
| 1196 | return ERR_PTR(-error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | } /* xlog_alloc_log */ |
| 1198 | |
| 1199 | |
| 1200 | /* |
| 1201 | * Write out the commit record of a transaction associated with the given |
| 1202 | * ticket. Return the lsn of the commit record. |
| 1203 | */ |
| 1204 | STATIC int |
Dave Chinner | 55b6633 | 2010-03-23 11:43:17 +1100 | [diff] [blame] | 1205 | xlog_commit_record( |
| 1206 | struct log *log, |
| 1207 | struct xlog_ticket *ticket, |
| 1208 | struct xlog_in_core **iclog, |
| 1209 | xfs_lsn_t *commitlsnp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | { |
Dave Chinner | 55b6633 | 2010-03-23 11:43:17 +1100 | [diff] [blame] | 1211 | struct xfs_mount *mp = log->l_mp; |
| 1212 | int error; |
| 1213 | struct xfs_log_iovec reg = { |
| 1214 | .i_addr = NULL, |
| 1215 | .i_len = 0, |
| 1216 | .i_type = XLOG_REG_TYPE_COMMIT, |
| 1217 | }; |
| 1218 | struct xfs_log_vec vec = { |
| 1219 | .lv_niovecs = 1, |
| 1220 | .lv_iovecp = ®, |
| 1221 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | |
| 1223 | ASSERT_ALWAYS(iclog); |
Dave Chinner | 55b6633 | 2010-03-23 11:43:17 +1100 | [diff] [blame] | 1224 | error = xlog_write(log, &vec, ticket, commitlsnp, iclog, |
| 1225 | XLOG_COMMIT_TRANS); |
| 1226 | if (error) |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 1227 | xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1228 | return error; |
Dave Chinner | 55b6633 | 2010-03-23 11:43:17 +1100 | [diff] [blame] | 1229 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | |
| 1231 | /* |
| 1232 | * Push on the buffer cache code if we ever use more than 75% of the on-disk |
| 1233 | * log space. This code pushes on the lsn which would supposedly free up |
| 1234 | * the 25% which we want to leave free. We may need to adopt a policy which |
| 1235 | * pushes on an lsn which is further along in the log once we reach the high |
| 1236 | * water mark. In this manner, we would be creating a low water mark. |
| 1237 | */ |
David Chinner | a8272ce | 2007-11-23 16:28:09 +1100 | [diff] [blame] | 1238 | STATIC void |
Dave Chinner | 2ced19c | 2010-12-21 12:09:20 +1100 | [diff] [blame] | 1239 | xlog_grant_push_ail( |
| 1240 | struct log *log, |
| 1241 | int need_bytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | { |
Dave Chinner | 2ced19c | 2010-12-21 12:09:20 +1100 | [diff] [blame] | 1243 | xfs_lsn_t threshold_lsn = 0; |
Dave Chinner | 84f3c68 | 2010-12-03 22:11:29 +1100 | [diff] [blame] | 1244 | xfs_lsn_t last_sync_lsn; |
Dave Chinner | 2ced19c | 2010-12-21 12:09:20 +1100 | [diff] [blame] | 1245 | int free_blocks; |
| 1246 | int free_bytes; |
| 1247 | int threshold_block; |
| 1248 | int threshold_cycle; |
| 1249 | int free_threshold; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | |
Dave Chinner | 2ced19c | 2010-12-21 12:09:20 +1100 | [diff] [blame] | 1251 | ASSERT(BTOBB(need_bytes) < log->l_logBBsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | |
Dave Chinner | 2ced19c | 2010-12-21 12:09:20 +1100 | [diff] [blame] | 1253 | free_bytes = xlog_space_left(log, &log->l_grant_reserve_head); |
| 1254 | free_blocks = BTOBBT(free_bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | |
Dave Chinner | 2ced19c | 2010-12-21 12:09:20 +1100 | [diff] [blame] | 1256 | /* |
| 1257 | * Set the threshold for the minimum number of free blocks in the |
| 1258 | * log to the maximum of what the caller needs, one quarter of the |
| 1259 | * log, and 256 blocks. |
| 1260 | */ |
| 1261 | free_threshold = BTOBB(need_bytes); |
| 1262 | free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2)); |
| 1263 | free_threshold = MAX(free_threshold, 256); |
| 1264 | if (free_blocks >= free_threshold) |
| 1265 | return; |
| 1266 | |
Dave Chinner | 1c3cb9e | 2010-12-21 12:28:39 +1100 | [diff] [blame] | 1267 | xlog_crack_atomic_lsn(&log->l_tail_lsn, &threshold_cycle, |
| 1268 | &threshold_block); |
| 1269 | threshold_block += free_threshold; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | if (threshold_block >= log->l_logBBsize) { |
Dave Chinner | 2ced19c | 2010-12-21 12:09:20 +1100 | [diff] [blame] | 1271 | threshold_block -= log->l_logBBsize; |
| 1272 | threshold_cycle += 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | } |
Dave Chinner | 2ced19c | 2010-12-21 12:09:20 +1100 | [diff] [blame] | 1274 | threshold_lsn = xlog_assign_lsn(threshold_cycle, |
| 1275 | threshold_block); |
| 1276 | /* |
| 1277 | * Don't pass in an lsn greater than the lsn of the last |
Dave Chinner | 84f3c68 | 2010-12-03 22:11:29 +1100 | [diff] [blame] | 1278 | * log record known to be on disk. Use a snapshot of the last sync lsn |
| 1279 | * so that it doesn't change between the compare and the set. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | */ |
Dave Chinner | 84f3c68 | 2010-12-03 22:11:29 +1100 | [diff] [blame] | 1281 | last_sync_lsn = atomic64_read(&log->l_last_sync_lsn); |
| 1282 | if (XFS_LSN_CMP(threshold_lsn, last_sync_lsn) > 0) |
| 1283 | threshold_lsn = last_sync_lsn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | |
Dave Chinner | 2ced19c | 2010-12-21 12:09:20 +1100 | [diff] [blame] | 1285 | /* |
| 1286 | * Get the transaction layer to kick the dirty buffers out to |
| 1287 | * disk asynchronously. No point in trying to do this if |
| 1288 | * the filesystem is shutting down. |
| 1289 | */ |
| 1290 | if (!XLOG_FORCED_SHUTDOWN(log)) |
Dave Chinner | fd07484 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 1291 | xfs_ail_push(log->l_ailp, threshold_lsn); |
Dave Chinner | 2ced19c | 2010-12-21 12:09:20 +1100 | [diff] [blame] | 1292 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | |
Christoph Hellwig | 873ff55 | 2010-01-13 22:17:57 +0000 | [diff] [blame] | 1294 | /* |
| 1295 | * The bdstrat callback function for log bufs. This gives us a central |
| 1296 | * place to trap bufs in case we get hit by a log I/O error and need to |
| 1297 | * shutdown. Actually, in practice, even when we didn't get a log error, |
| 1298 | * we transition the iclogs to IOERROR state *after* flushing all existing |
| 1299 | * iclogs to disk. This is because we don't want anymore new transactions to be |
| 1300 | * started or completed afterwards. |
| 1301 | */ |
| 1302 | STATIC int |
| 1303 | xlog_bdstrat( |
| 1304 | struct xfs_buf *bp) |
| 1305 | { |
Christoph Hellwig | adadbee | 2011-07-13 13:43:49 +0200 | [diff] [blame] | 1306 | struct xlog_in_core *iclog = bp->b_fspriv; |
Christoph Hellwig | 873ff55 | 2010-01-13 22:17:57 +0000 | [diff] [blame] | 1307 | |
Christoph Hellwig | 873ff55 | 2010-01-13 22:17:57 +0000 | [diff] [blame] | 1308 | if (iclog->ic_state & XLOG_STATE_IOERROR) { |
Chandra Seetharaman | 5a52c2a58 | 2011-07-22 23:39:51 +0000 | [diff] [blame] | 1309 | xfs_buf_ioerror(bp, EIO); |
Christoph Hellwig | c867cb6 | 2011-10-10 16:52:46 +0000 | [diff] [blame] | 1310 | xfs_buf_stale(bp); |
Christoph Hellwig | 1a1a3e9 | 2010-10-06 18:41:18 +0000 | [diff] [blame] | 1311 | xfs_buf_ioend(bp, 0); |
Christoph Hellwig | 873ff55 | 2010-01-13 22:17:57 +0000 | [diff] [blame] | 1312 | /* |
| 1313 | * It would seem logical to return EIO here, but we rely on |
| 1314 | * the log state machine to propagate I/O errors instead of |
| 1315 | * doing it here. |
| 1316 | */ |
| 1317 | return 0; |
| 1318 | } |
| 1319 | |
Christoph Hellwig | 873ff55 | 2010-01-13 22:17:57 +0000 | [diff] [blame] | 1320 | xfs_buf_iorequest(bp); |
| 1321 | return 0; |
| 1322 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | |
| 1324 | /* |
| 1325 | * Flush out the in-core log (iclog) to the on-disk log in an asynchronous |
| 1326 | * fashion. Previously, we should have moved the current iclog |
| 1327 | * ptr in the log to point to the next available iclog. This allows further |
| 1328 | * write to continue while this code syncs out an iclog ready to go. |
| 1329 | * Before an in-core log can be written out, the data section must be scanned |
| 1330 | * to save away the 1st word of each BBSIZE block into the header. We replace |
| 1331 | * it with the current cycle count. Each BBSIZE block is tagged with the |
| 1332 | * cycle count because there in an implicit assumption that drives will |
| 1333 | * guarantee that entire 512 byte blocks get written at once. In other words, |
| 1334 | * we can't have part of a 512 byte block written and part not written. By |
| 1335 | * tagging each block, we will know which blocks are valid when recovering |
| 1336 | * after an unclean shutdown. |
| 1337 | * |
| 1338 | * This routine is single threaded on the iclog. No other thread can be in |
| 1339 | * this routine with the same iclog. Changing contents of iclog can there- |
| 1340 | * fore be done without grabbing the state machine lock. Updating the global |
| 1341 | * log will require grabbing the lock though. |
| 1342 | * |
| 1343 | * The entire log manager uses a logical block numbering scheme. Only |
| 1344 | * log_sync (and then only bwrite()) know about the fact that the log may |
| 1345 | * not start with block zero on a given device. The log block start offset |
| 1346 | * is added immediately before calling bwrite(). |
| 1347 | */ |
| 1348 | |
David Chinner | a8272ce | 2007-11-23 16:28:09 +1100 | [diff] [blame] | 1349 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | xlog_sync(xlog_t *log, |
| 1351 | xlog_in_core_t *iclog) |
| 1352 | { |
| 1353 | xfs_caddr_t dptr; /* pointer to byte sized element */ |
| 1354 | xfs_buf_t *bp; |
Christoph Hellwig | b53e675 | 2007-10-12 10:59:34 +1000 | [diff] [blame] | 1355 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | uint count; /* byte count of bwrite */ |
| 1357 | uint count_init; /* initial count before roundup */ |
| 1358 | int roundoff; /* roundoff to BB or stripe */ |
| 1359 | int split = 0; /* split write into two regions */ |
| 1360 | int error; |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 1361 | int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | |
| 1363 | XFS_STATS_INC(xs_log_writes); |
David Chinner | 155cc6b | 2008-03-06 13:44:14 +1100 | [diff] [blame] | 1364 | ASSERT(atomic_read(&iclog->ic_refcnt) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | |
| 1366 | /* Add for LR header */ |
| 1367 | count_init = log->l_iclog_hsize + iclog->ic_offset; |
| 1368 | |
| 1369 | /* Round out the log write size */ |
| 1370 | if (v2 && log->l_mp->m_sb.sb_logsunit > 1) { |
| 1371 | /* we have a v2 stripe unit to use */ |
| 1372 | count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init)); |
| 1373 | } else { |
| 1374 | count = BBTOB(BTOBB(count_init)); |
| 1375 | } |
| 1376 | roundoff = count - count_init; |
| 1377 | ASSERT(roundoff >= 0); |
| 1378 | ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 && |
| 1379 | roundoff < log->l_mp->m_sb.sb_logsunit) |
| 1380 | || |
| 1381 | (log->l_mp->m_sb.sb_logsunit <= 1 && |
| 1382 | roundoff < BBTOB(1))); |
| 1383 | |
| 1384 | /* move grant heads by roundoff in sync */ |
Dave Chinner | a69ed03 | 2010-12-21 12:08:20 +1100 | [diff] [blame] | 1385 | xlog_grant_add_space(log, &log->l_grant_reserve_head, roundoff); |
| 1386 | xlog_grant_add_space(log, &log->l_grant_write_head, roundoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | |
| 1388 | /* put cycle number in every block */ |
| 1389 | xlog_pack_data(log, iclog, roundoff); |
| 1390 | |
| 1391 | /* real byte length */ |
| 1392 | if (v2) { |
Christoph Hellwig | b53e675 | 2007-10-12 10:59:34 +1000 | [diff] [blame] | 1393 | iclog->ic_header.h_len = |
| 1394 | cpu_to_be32(iclog->ic_offset + roundoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | } else { |
Christoph Hellwig | b53e675 | 2007-10-12 10:59:34 +1000 | [diff] [blame] | 1396 | iclog->ic_header.h_len = |
| 1397 | cpu_to_be32(iclog->ic_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
Nathan Scott | f5faad7 | 2006-07-28 17:04:44 +1000 | [diff] [blame] | 1400 | bp = iclog->ic_bp; |
Christoph Hellwig | b53e675 | 2007-10-12 10:59:34 +1000 | [diff] [blame] | 1401 | XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | |
| 1403 | XFS_STATS_ADD(xs_log_blocks, BTOBB(count)); |
| 1404 | |
| 1405 | /* Do we need to split this write into 2 parts? */ |
| 1406 | if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) { |
| 1407 | split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp))); |
| 1408 | count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)); |
| 1409 | iclog->ic_bwritecnt = 2; /* split into 2 writes */ |
| 1410 | } else { |
| 1411 | iclog->ic_bwritecnt = 1; |
| 1412 | } |
David Chinner | 511105b | 2007-05-24 15:21:57 +1000 | [diff] [blame] | 1413 | XFS_BUF_SET_COUNT(bp, count); |
Christoph Hellwig | adadbee | 2011-07-13 13:43:49 +0200 | [diff] [blame] | 1414 | bp->b_fspriv = iclog; |
Nathan Scott | f5faad7 | 2006-07-28 17:04:44 +1000 | [diff] [blame] | 1415 | XFS_BUF_ZEROFLAGS(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | XFS_BUF_ASYNC(bp); |
Christoph Hellwig | 1d5ae5d | 2011-07-08 14:36:32 +0200 | [diff] [blame] | 1417 | bp->b_flags |= XBF_SYNCIO; |
Christoph Hellwig | 651701d | 2010-06-28 10:34:34 -0400 | [diff] [blame] | 1418 | |
Christoph Hellwig | a27a263 | 2011-06-16 12:02:23 +0000 | [diff] [blame] | 1419 | if (log->l_mp->m_flags & XFS_MOUNT_BARRIER) { |
Christoph Hellwig | e163cbd | 2011-07-08 14:36:36 +0200 | [diff] [blame] | 1420 | bp->b_flags |= XBF_FUA; |
| 1421 | |
Christoph Hellwig | a27a263 | 2011-06-16 12:02:23 +0000 | [diff] [blame] | 1422 | /* |
Christoph Hellwig | e163cbd | 2011-07-08 14:36:36 +0200 | [diff] [blame] | 1423 | * Flush the data device before flushing the log to make |
| 1424 | * sure all meta data written back from the AIL actually made |
| 1425 | * it to disk before stamping the new log tail LSN into the |
| 1426 | * log buffer. For an external log we need to issue the |
| 1427 | * flush explicitly, and unfortunately synchronously here; |
| 1428 | * for an internal log we can simply use the block layer |
| 1429 | * state machine for preflushes. |
Christoph Hellwig | a27a263 | 2011-06-16 12:02:23 +0000 | [diff] [blame] | 1430 | */ |
| 1431 | if (log->l_mp->m_logdev_targp != log->l_mp->m_ddev_targp) |
| 1432 | xfs_blkdev_issue_flush(log->l_mp->m_ddev_targp); |
Christoph Hellwig | e163cbd | 2011-07-08 14:36:36 +0200 | [diff] [blame] | 1433 | else |
| 1434 | bp->b_flags |= XBF_FLUSH; |
Christoph Hellwig | a27a263 | 2011-06-16 12:02:23 +0000 | [diff] [blame] | 1435 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | |
| 1437 | ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1); |
| 1438 | ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize); |
| 1439 | |
| 1440 | xlog_verify_iclog(log, iclog, count, B_TRUE); |
| 1441 | |
| 1442 | /* account for log which doesn't start at block #0 */ |
| 1443 | XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart); |
| 1444 | /* |
| 1445 | * Don't call xfs_bwrite here. We do log-syncs even when the filesystem |
| 1446 | * is shutting down. |
| 1447 | */ |
| 1448 | XFS_BUF_WRITE(bp); |
| 1449 | |
Christoph Hellwig | 901796a | 2011-10-10 16:52:49 +0000 | [diff] [blame] | 1450 | error = xlog_bdstrat(bp); |
| 1451 | if (error) { |
| 1452 | xfs_buf_ioerror_alert(bp, "xlog_sync"); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1453 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | } |
| 1455 | if (split) { |
Nathan Scott | f5faad7 | 2006-07-28 17:04:44 +1000 | [diff] [blame] | 1456 | bp = iclog->ic_log->l_xbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */ |
Chandra Seetharaman | 02fe03d | 2011-07-22 23:40:22 +0000 | [diff] [blame] | 1458 | xfs_buf_associate_memory(bp, |
| 1459 | (char *)&iclog->ic_header + count, split); |
Christoph Hellwig | adadbee | 2011-07-13 13:43:49 +0200 | [diff] [blame] | 1460 | bp->b_fspriv = iclog; |
Nathan Scott | f5faad7 | 2006-07-28 17:04:44 +1000 | [diff] [blame] | 1461 | XFS_BUF_ZEROFLAGS(bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | XFS_BUF_ASYNC(bp); |
Christoph Hellwig | 1d5ae5d | 2011-07-08 14:36:32 +0200 | [diff] [blame] | 1463 | bp->b_flags |= XBF_SYNCIO; |
Christoph Hellwig | f538d4d | 2005-11-02 10:26:59 +1100 | [diff] [blame] | 1464 | if (log->l_mp->m_flags & XFS_MOUNT_BARRIER) |
Christoph Hellwig | e163cbd | 2011-07-08 14:36:36 +0200 | [diff] [blame] | 1465 | bp->b_flags |= XBF_FUA; |
Chandra Seetharaman | 6292604 | 2011-07-22 23:40:15 +0000 | [diff] [blame] | 1466 | dptr = bp->b_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | /* |
| 1468 | * Bump the cycle numbers at the start of each block |
| 1469 | * since this part of the buffer is at the start of |
| 1470 | * a new cycle. Watch out for the header magic number |
| 1471 | * case, though. |
| 1472 | */ |
Christoph Hellwig | b53e675 | 2007-10-12 10:59:34 +1000 | [diff] [blame] | 1473 | for (i = 0; i < split; i += BBSIZE) { |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 1474 | be32_add_cpu((__be32 *)dptr, 1); |
Christoph Hellwig | b53e675 | 2007-10-12 10:59:34 +1000 | [diff] [blame] | 1475 | if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM) |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 1476 | be32_add_cpu((__be32 *)dptr, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | dptr += BBSIZE; |
| 1478 | } |
| 1479 | |
| 1480 | ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1); |
| 1481 | ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize); |
| 1482 | |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1483 | /* account for internal log which doesn't start at block #0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart); |
| 1485 | XFS_BUF_WRITE(bp); |
Christoph Hellwig | 901796a | 2011-10-10 16:52:49 +0000 | [diff] [blame] | 1486 | error = xlog_bdstrat(bp); |
| 1487 | if (error) { |
| 1488 | xfs_buf_ioerror_alert(bp, "xlog_sync (split)"); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1489 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | } |
| 1491 | } |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 1492 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | } /* xlog_sync */ |
| 1494 | |
| 1495 | |
| 1496 | /* |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1497 | * Deallocate a log structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | */ |
David Chinner | a8272ce | 2007-11-23 16:28:09 +1100 | [diff] [blame] | 1499 | STATIC void |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1500 | xlog_dealloc_log(xlog_t *log) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | { |
| 1502 | xlog_in_core_t *iclog, *next_iclog; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | int i; |
| 1504 | |
Dave Chinner | 71e330b | 2010-05-21 14:37:18 +1000 | [diff] [blame] | 1505 | xlog_cil_destroy(log); |
| 1506 | |
Dave Chinner | 4439647 | 2011-04-21 09:34:27 +0000 | [diff] [blame] | 1507 | /* |
| 1508 | * always need to ensure that the extra buffer does not point to memory |
| 1509 | * owned by another log buffer before we free it. |
| 1510 | */ |
| 1511 | xfs_buf_set_empty(log->l_xbuf, log->l_iclog_size); |
| 1512 | xfs_buf_free(log->l_xbuf); |
| 1513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | iclog = log->l_iclog; |
| 1515 | for (i=0; i<log->l_iclog_bufs; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | xfs_buf_free(iclog->ic_bp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | next_iclog = iclog->ic_next; |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1518 | kmem_free(iclog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | iclog = next_iclog; |
| 1520 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | spinlock_destroy(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | log->l_mp->m_log = NULL; |
Denys Vlasenko | f0e2d93 | 2008-05-19 16:31:57 +1000 | [diff] [blame] | 1524 | kmem_free(log); |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 1525 | } /* xlog_dealloc_log */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | |
| 1527 | /* |
| 1528 | * Update counters atomically now that memcpy is done. |
| 1529 | */ |
| 1530 | /* ARGSUSED */ |
| 1531 | static inline void |
| 1532 | xlog_state_finish_copy(xlog_t *log, |
| 1533 | xlog_in_core_t *iclog, |
| 1534 | int record_cnt, |
| 1535 | int copy_bytes) |
| 1536 | { |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 1537 | spin_lock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 1539 | be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | iclog->ic_offset += copy_bytes; |
| 1541 | |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 1542 | spin_unlock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | } /* xlog_state_finish_copy */ |
| 1544 | |
| 1545 | |
| 1546 | |
| 1547 | |
| 1548 | /* |
Tim Shimmin | 7e9c639 | 2005-09-02 16:42:05 +1000 | [diff] [blame] | 1549 | * print out info relating to regions written which consume |
| 1550 | * the reservation |
| 1551 | */ |
Dave Chinner | 71e330b | 2010-05-21 14:37:18 +1000 | [diff] [blame] | 1552 | void |
| 1553 | xlog_print_tic_res( |
| 1554 | struct xfs_mount *mp, |
| 1555 | struct xlog_ticket *ticket) |
Tim Shimmin | 7e9c639 | 2005-09-02 16:42:05 +1000 | [diff] [blame] | 1556 | { |
| 1557 | uint i; |
| 1558 | uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t); |
| 1559 | |
| 1560 | /* match with XLOG_REG_TYPE_* in xfs_log.h */ |
| 1561 | static char *res_type_str[XLOG_REG_TYPE_MAX] = { |
| 1562 | "bformat", |
| 1563 | "bchunk", |
| 1564 | "efi_format", |
| 1565 | "efd_format", |
| 1566 | "iformat", |
| 1567 | "icore", |
| 1568 | "iext", |
| 1569 | "ibroot", |
| 1570 | "ilocal", |
| 1571 | "iattr_ext", |
| 1572 | "iattr_broot", |
| 1573 | "iattr_local", |
| 1574 | "qformat", |
| 1575 | "dquot", |
| 1576 | "quotaoff", |
| 1577 | "LR header", |
| 1578 | "unmount", |
| 1579 | "commit", |
| 1580 | "trans header" |
| 1581 | }; |
| 1582 | static char *trans_type_str[XFS_TRANS_TYPE_MAX] = { |
| 1583 | "SETATTR_NOT_SIZE", |
| 1584 | "SETATTR_SIZE", |
| 1585 | "INACTIVE", |
| 1586 | "CREATE", |
| 1587 | "CREATE_TRUNC", |
| 1588 | "TRUNCATE_FILE", |
| 1589 | "REMOVE", |
| 1590 | "LINK", |
| 1591 | "RENAME", |
| 1592 | "MKDIR", |
| 1593 | "RMDIR", |
| 1594 | "SYMLINK", |
| 1595 | "SET_DMATTRS", |
| 1596 | "GROWFS", |
| 1597 | "STRAT_WRITE", |
| 1598 | "DIOSTRAT", |
| 1599 | "WRITE_SYNC", |
| 1600 | "WRITEID", |
| 1601 | "ADDAFORK", |
| 1602 | "ATTRINVAL", |
| 1603 | "ATRUNCATE", |
| 1604 | "ATTR_SET", |
| 1605 | "ATTR_RM", |
| 1606 | "ATTR_FLAG", |
| 1607 | "CLEAR_AGI_BUCKET", |
| 1608 | "QM_SBCHANGE", |
| 1609 | "DUMMY1", |
| 1610 | "DUMMY2", |
| 1611 | "QM_QUOTAOFF", |
| 1612 | "QM_DQALLOC", |
| 1613 | "QM_SETQLIM", |
| 1614 | "QM_DQCLUSTER", |
| 1615 | "QM_QINOCREATE", |
| 1616 | "QM_QUOTAOFF_END", |
| 1617 | "SB_UNIT", |
| 1618 | "FSYNC_TS", |
| 1619 | "GROWFSRT_ALLOC", |
| 1620 | "GROWFSRT_ZERO", |
| 1621 | "GROWFSRT_FREE", |
| 1622 | "SWAPEXT" |
| 1623 | }; |
| 1624 | |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 1625 | xfs_warn(mp, |
Christoph Hellwig | 93b8a58 | 2011-12-06 21:58:07 +0000 | [diff] [blame] | 1626 | "xlog_write: reservation summary:\n" |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 1627 | " trans type = %s (%u)\n" |
| 1628 | " unit res = %d bytes\n" |
| 1629 | " current res = %d bytes\n" |
| 1630 | " total reg = %u bytes (o/flow = %u bytes)\n" |
| 1631 | " ophdrs = %u (ophdr space = %u bytes)\n" |
| 1632 | " ophdr + reg = %u bytes\n" |
| 1633 | " num regions = %u\n", |
| 1634 | ((ticket->t_trans_type <= 0 || |
| 1635 | ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ? |
| 1636 | "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]), |
| 1637 | ticket->t_trans_type, |
| 1638 | ticket->t_unit_res, |
| 1639 | ticket->t_curr_res, |
| 1640 | ticket->t_res_arr_sum, ticket->t_res_o_flow, |
| 1641 | ticket->t_res_num_ophdrs, ophdr_spc, |
| 1642 | ticket->t_res_arr_sum + |
| 1643 | ticket->t_res_o_flow + ophdr_spc, |
| 1644 | ticket->t_res_num); |
Tim Shimmin | 7e9c639 | 2005-09-02 16:42:05 +1000 | [diff] [blame] | 1645 | |
| 1646 | for (i = 0; i < ticket->t_res_num; i++) { |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 1647 | uint r_type = ticket->t_res_arr[i].r_type; |
| 1648 | xfs_warn(mp, "region[%u]: %s - %u bytes\n", i, |
Tim Shimmin | 7e9c639 | 2005-09-02 16:42:05 +1000 | [diff] [blame] | 1649 | ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ? |
| 1650 | "bad-rtype" : res_type_str[r_type-1]), |
| 1651 | ticket->t_res_arr[i].r_len); |
| 1652 | } |
Dave Chinner | 169a7b0 | 2010-05-07 11:05:31 +1000 | [diff] [blame] | 1653 | |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 1654 | xfs_alert_tag(mp, XFS_PTAG_LOGRES, |
Christoph Hellwig | 93b8a58 | 2011-12-06 21:58:07 +0000 | [diff] [blame] | 1655 | "xlog_write: reservation ran out. Need to up reservation"); |
Dave Chinner | 169a7b0 | 2010-05-07 11:05:31 +1000 | [diff] [blame] | 1656 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
Tim Shimmin | 7e9c639 | 2005-09-02 16:42:05 +1000 | [diff] [blame] | 1657 | } |
Tim Shimmin | 7e9c639 | 2005-09-02 16:42:05 +1000 | [diff] [blame] | 1658 | |
| 1659 | /* |
Dave Chinner | b5203cd | 2010-03-23 11:29:44 +1100 | [diff] [blame] | 1660 | * Calculate the potential space needed by the log vector. Each region gets |
| 1661 | * its own xlog_op_header_t and may need to be double word aligned. |
| 1662 | */ |
| 1663 | static int |
| 1664 | xlog_write_calc_vec_length( |
| 1665 | struct xlog_ticket *ticket, |
Dave Chinner | 55b6633 | 2010-03-23 11:43:17 +1100 | [diff] [blame] | 1666 | struct xfs_log_vec *log_vector) |
Dave Chinner | b5203cd | 2010-03-23 11:29:44 +1100 | [diff] [blame] | 1667 | { |
Dave Chinner | 55b6633 | 2010-03-23 11:43:17 +1100 | [diff] [blame] | 1668 | struct xfs_log_vec *lv; |
Dave Chinner | b5203cd | 2010-03-23 11:29:44 +1100 | [diff] [blame] | 1669 | int headers = 0; |
| 1670 | int len = 0; |
| 1671 | int i; |
| 1672 | |
| 1673 | /* acct for start rec of xact */ |
| 1674 | if (ticket->t_flags & XLOG_TIC_INITED) |
| 1675 | headers++; |
| 1676 | |
Dave Chinner | 55b6633 | 2010-03-23 11:43:17 +1100 | [diff] [blame] | 1677 | for (lv = log_vector; lv; lv = lv->lv_next) { |
| 1678 | headers += lv->lv_niovecs; |
Dave Chinner | b5203cd | 2010-03-23 11:29:44 +1100 | [diff] [blame] | 1679 | |
Dave Chinner | 55b6633 | 2010-03-23 11:43:17 +1100 | [diff] [blame] | 1680 | for (i = 0; i < lv->lv_niovecs; i++) { |
| 1681 | struct xfs_log_iovec *vecp = &lv->lv_iovecp[i]; |
| 1682 | |
| 1683 | len += vecp->i_len; |
| 1684 | xlog_tic_add_region(ticket, vecp->i_len, vecp->i_type); |
| 1685 | } |
Dave Chinner | b5203cd | 2010-03-23 11:29:44 +1100 | [diff] [blame] | 1686 | } |
| 1687 | |
| 1688 | ticket->t_res_num_ophdrs += headers; |
| 1689 | len += headers * sizeof(struct xlog_op_header); |
| 1690 | |
| 1691 | return len; |
| 1692 | } |
| 1693 | |
| 1694 | /* |
| 1695 | * If first write for transaction, insert start record We can't be trying to |
| 1696 | * commit if we are inited. We can't have any "partial_copy" if we are inited. |
| 1697 | */ |
| 1698 | static int |
| 1699 | xlog_write_start_rec( |
Christoph Hellwig | e6b1f27 | 2010-03-23 11:47:38 +1100 | [diff] [blame] | 1700 | struct xlog_op_header *ophdr, |
Dave Chinner | b5203cd | 2010-03-23 11:29:44 +1100 | [diff] [blame] | 1701 | struct xlog_ticket *ticket) |
| 1702 | { |
Dave Chinner | b5203cd | 2010-03-23 11:29:44 +1100 | [diff] [blame] | 1703 | if (!(ticket->t_flags & XLOG_TIC_INITED)) |
| 1704 | return 0; |
| 1705 | |
| 1706 | ophdr->oh_tid = cpu_to_be32(ticket->t_tid); |
| 1707 | ophdr->oh_clientid = ticket->t_clientid; |
| 1708 | ophdr->oh_len = 0; |
| 1709 | ophdr->oh_flags = XLOG_START_TRANS; |
| 1710 | ophdr->oh_res2 = 0; |
| 1711 | |
| 1712 | ticket->t_flags &= ~XLOG_TIC_INITED; |
| 1713 | |
| 1714 | return sizeof(struct xlog_op_header); |
| 1715 | } |
| 1716 | |
| 1717 | static xlog_op_header_t * |
| 1718 | xlog_write_setup_ophdr( |
| 1719 | struct log *log, |
Christoph Hellwig | e6b1f27 | 2010-03-23 11:47:38 +1100 | [diff] [blame] | 1720 | struct xlog_op_header *ophdr, |
Dave Chinner | b5203cd | 2010-03-23 11:29:44 +1100 | [diff] [blame] | 1721 | struct xlog_ticket *ticket, |
| 1722 | uint flags) |
| 1723 | { |
Dave Chinner | b5203cd | 2010-03-23 11:29:44 +1100 | [diff] [blame] | 1724 | ophdr->oh_tid = cpu_to_be32(ticket->t_tid); |
| 1725 | ophdr->oh_clientid = ticket->t_clientid; |
| 1726 | ophdr->oh_res2 = 0; |
| 1727 | |
| 1728 | /* are we copying a commit or unmount record? */ |
| 1729 | ophdr->oh_flags = flags; |
| 1730 | |
| 1731 | /* |
| 1732 | * We've seen logs corrupted with bad transaction client ids. This |
| 1733 | * makes sure that XFS doesn't generate them on. Turn this into an EIO |
| 1734 | * and shut down the filesystem. |
| 1735 | */ |
| 1736 | switch (ophdr->oh_clientid) { |
| 1737 | case XFS_TRANSACTION: |
| 1738 | case XFS_VOLUME: |
| 1739 | case XFS_LOG: |
| 1740 | break; |
| 1741 | default: |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 1742 | xfs_warn(log->l_mp, |
Dave Chinner | b5203cd | 2010-03-23 11:29:44 +1100 | [diff] [blame] | 1743 | "Bad XFS transaction clientid 0x%x in ticket 0x%p", |
| 1744 | ophdr->oh_clientid, ticket); |
| 1745 | return NULL; |
| 1746 | } |
| 1747 | |
| 1748 | return ophdr; |
| 1749 | } |
| 1750 | |
| 1751 | /* |
| 1752 | * Set up the parameters of the region copy into the log. This has |
| 1753 | * to handle region write split across multiple log buffers - this |
| 1754 | * state is kept external to this function so that this code can |
| 1755 | * can be written in an obvious, self documenting manner. |
| 1756 | */ |
| 1757 | static int |
| 1758 | xlog_write_setup_copy( |
| 1759 | struct xlog_ticket *ticket, |
| 1760 | struct xlog_op_header *ophdr, |
| 1761 | int space_available, |
| 1762 | int space_required, |
| 1763 | int *copy_off, |
| 1764 | int *copy_len, |
| 1765 | int *last_was_partial_copy, |
| 1766 | int *bytes_consumed) |
| 1767 | { |
| 1768 | int still_to_copy; |
| 1769 | |
| 1770 | still_to_copy = space_required - *bytes_consumed; |
| 1771 | *copy_off = *bytes_consumed; |
| 1772 | |
| 1773 | if (still_to_copy <= space_available) { |
| 1774 | /* write of region completes here */ |
| 1775 | *copy_len = still_to_copy; |
| 1776 | ophdr->oh_len = cpu_to_be32(*copy_len); |
| 1777 | if (*last_was_partial_copy) |
| 1778 | ophdr->oh_flags |= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS); |
| 1779 | *last_was_partial_copy = 0; |
| 1780 | *bytes_consumed = 0; |
| 1781 | return 0; |
| 1782 | } |
| 1783 | |
| 1784 | /* partial write of region, needs extra log op header reservation */ |
| 1785 | *copy_len = space_available; |
| 1786 | ophdr->oh_len = cpu_to_be32(*copy_len); |
| 1787 | ophdr->oh_flags |= XLOG_CONTINUE_TRANS; |
| 1788 | if (*last_was_partial_copy) |
| 1789 | ophdr->oh_flags |= XLOG_WAS_CONT_TRANS; |
| 1790 | *bytes_consumed += *copy_len; |
| 1791 | (*last_was_partial_copy)++; |
| 1792 | |
| 1793 | /* account for new log op header */ |
| 1794 | ticket->t_curr_res -= sizeof(struct xlog_op_header); |
| 1795 | ticket->t_res_num_ophdrs++; |
| 1796 | |
| 1797 | return sizeof(struct xlog_op_header); |
| 1798 | } |
| 1799 | |
| 1800 | static int |
| 1801 | xlog_write_copy_finish( |
| 1802 | struct log *log, |
| 1803 | struct xlog_in_core *iclog, |
| 1804 | uint flags, |
| 1805 | int *record_cnt, |
| 1806 | int *data_cnt, |
| 1807 | int *partial_copy, |
| 1808 | int *partial_copy_len, |
| 1809 | int log_offset, |
| 1810 | struct xlog_in_core **commit_iclog) |
| 1811 | { |
| 1812 | if (*partial_copy) { |
| 1813 | /* |
| 1814 | * This iclog has already been marked WANT_SYNC by |
| 1815 | * xlog_state_get_iclog_space. |
| 1816 | */ |
| 1817 | xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt); |
| 1818 | *record_cnt = 0; |
| 1819 | *data_cnt = 0; |
| 1820 | return xlog_state_release_iclog(log, iclog); |
| 1821 | } |
| 1822 | |
| 1823 | *partial_copy = 0; |
| 1824 | *partial_copy_len = 0; |
| 1825 | |
| 1826 | if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) { |
| 1827 | /* no more space in this iclog - push it. */ |
| 1828 | xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt); |
| 1829 | *record_cnt = 0; |
| 1830 | *data_cnt = 0; |
| 1831 | |
| 1832 | spin_lock(&log->l_icloglock); |
| 1833 | xlog_state_want_sync(log, iclog); |
| 1834 | spin_unlock(&log->l_icloglock); |
| 1835 | |
| 1836 | if (!commit_iclog) |
| 1837 | return xlog_state_release_iclog(log, iclog); |
| 1838 | ASSERT(flags & XLOG_COMMIT_TRANS); |
| 1839 | *commit_iclog = iclog; |
| 1840 | } |
| 1841 | |
| 1842 | return 0; |
| 1843 | } |
| 1844 | |
| 1845 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | * Write some region out to in-core log |
| 1847 | * |
| 1848 | * This will be called when writing externally provided regions or when |
| 1849 | * writing out a commit record for a given transaction. |
| 1850 | * |
| 1851 | * General algorithm: |
| 1852 | * 1. Find total length of this write. This may include adding to the |
| 1853 | * lengths passed in. |
| 1854 | * 2. Check whether we violate the tickets reservation. |
| 1855 | * 3. While writing to this iclog |
| 1856 | * A. Reserve as much space in this iclog as can get |
| 1857 | * B. If this is first write, save away start lsn |
| 1858 | * C. While writing this region: |
| 1859 | * 1. If first write of transaction, write start record |
| 1860 | * 2. Write log operation header (header per region) |
| 1861 | * 3. Find out if we can fit entire region into this iclog |
| 1862 | * 4. Potentially, verify destination memcpy ptr |
| 1863 | * 5. Memcpy (partial) region |
| 1864 | * 6. If partial copy, release iclog; otherwise, continue |
| 1865 | * copying more regions into current iclog |
| 1866 | * 4. Mark want sync bit (in simulation mode) |
| 1867 | * 5. Release iclog for potential flush to on-disk log. |
| 1868 | * |
| 1869 | * ERRORS: |
| 1870 | * 1. Panic if reservation is overrun. This should never happen since |
| 1871 | * reservation amounts are generated internal to the filesystem. |
| 1872 | * NOTES: |
| 1873 | * 1. Tickets are single threaded data structures. |
| 1874 | * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the |
| 1875 | * syncing routine. When a single log_write region needs to span |
| 1876 | * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set |
| 1877 | * on all log operation writes which don't contain the end of the |
| 1878 | * region. The XLOG_END_TRANS bit is used for the in-core log |
| 1879 | * operation which contains the end of the continued log_write region. |
| 1880 | * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog, |
| 1881 | * we don't really know exactly how much space will be used. As a result, |
| 1882 | * we don't update ic_offset until the end when we know exactly how many |
| 1883 | * bytes have been written out. |
| 1884 | */ |
Dave Chinner | 71e330b | 2010-05-21 14:37:18 +1000 | [diff] [blame] | 1885 | int |
Christoph Hellwig | 35a8a72 | 2010-02-15 23:34:54 +0000 | [diff] [blame] | 1886 | xlog_write( |
Dave Chinner | 55b6633 | 2010-03-23 11:43:17 +1100 | [diff] [blame] | 1887 | struct log *log, |
| 1888 | struct xfs_log_vec *log_vector, |
Christoph Hellwig | 35a8a72 | 2010-02-15 23:34:54 +0000 | [diff] [blame] | 1889 | struct xlog_ticket *ticket, |
| 1890 | xfs_lsn_t *start_lsn, |
| 1891 | struct xlog_in_core **commit_iclog, |
| 1892 | uint flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | { |
Christoph Hellwig | 99428ad | 2010-03-23 11:35:45 +1100 | [diff] [blame] | 1894 | struct xlog_in_core *iclog = NULL; |
Dave Chinner | 55b6633 | 2010-03-23 11:43:17 +1100 | [diff] [blame] | 1895 | struct xfs_log_iovec *vecp; |
| 1896 | struct xfs_log_vec *lv; |
Christoph Hellwig | 99428ad | 2010-03-23 11:35:45 +1100 | [diff] [blame] | 1897 | int len; |
| 1898 | int index; |
| 1899 | int partial_copy = 0; |
| 1900 | int partial_copy_len = 0; |
| 1901 | int contwr = 0; |
| 1902 | int record_cnt = 0; |
| 1903 | int data_cnt = 0; |
| 1904 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | |
Christoph Hellwig | 99428ad | 2010-03-23 11:35:45 +1100 | [diff] [blame] | 1906 | *start_lsn = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1907 | |
Dave Chinner | 55b6633 | 2010-03-23 11:43:17 +1100 | [diff] [blame] | 1908 | len = xlog_write_calc_vec_length(ticket, log_vector); |
Dave Chinner | 71e330b | 2010-05-21 14:37:18 +1000 | [diff] [blame] | 1909 | |
Christoph Hellwig | 93b8a58 | 2011-12-06 21:58:07 +0000 | [diff] [blame] | 1910 | /* |
| 1911 | * Region headers and bytes are already accounted for. |
| 1912 | * We only need to take into account start records and |
| 1913 | * split regions in this function. |
| 1914 | */ |
| 1915 | if (ticket->t_flags & XLOG_TIC_INITED) |
| 1916 | ticket->t_curr_res -= sizeof(xlog_op_header_t); |
| 1917 | |
| 1918 | /* |
| 1919 | * Commit record headers need to be accounted for. These |
| 1920 | * come in as separate writes so are easy to detect. |
| 1921 | */ |
| 1922 | if (flags & (XLOG_COMMIT_TRANS | XLOG_UNMOUNT_TRANS)) |
| 1923 | ticket->t_curr_res -= sizeof(xlog_op_header_t); |
Dave Chinner | 71e330b | 2010-05-21 14:37:18 +1000 | [diff] [blame] | 1924 | |
| 1925 | if (ticket->t_curr_res < 0) |
Dave Chinner | 55b6633 | 2010-03-23 11:43:17 +1100 | [diff] [blame] | 1926 | xlog_print_tic_res(log->l_mp, ticket); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | |
Dave Chinner | 55b6633 | 2010-03-23 11:43:17 +1100 | [diff] [blame] | 1928 | index = 0; |
| 1929 | lv = log_vector; |
| 1930 | vecp = lv->lv_iovecp; |
| 1931 | while (lv && index < lv->lv_niovecs) { |
Christoph Hellwig | e6b1f27 | 2010-03-23 11:47:38 +1100 | [diff] [blame] | 1932 | void *ptr; |
Christoph Hellwig | 99428ad | 2010-03-23 11:35:45 +1100 | [diff] [blame] | 1933 | int log_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | |
Christoph Hellwig | 99428ad | 2010-03-23 11:35:45 +1100 | [diff] [blame] | 1935 | error = xlog_state_get_iclog_space(log, len, &iclog, ticket, |
| 1936 | &contwr, &log_offset); |
| 1937 | if (error) |
| 1938 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | |
Christoph Hellwig | 99428ad | 2010-03-23 11:35:45 +1100 | [diff] [blame] | 1940 | ASSERT(log_offset <= iclog->ic_size - 1); |
Christoph Hellwig | e6b1f27 | 2010-03-23 11:47:38 +1100 | [diff] [blame] | 1941 | ptr = iclog->ic_datap + log_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | |
Christoph Hellwig | 99428ad | 2010-03-23 11:35:45 +1100 | [diff] [blame] | 1943 | /* start_lsn is the first lsn written to. That's all we need. */ |
| 1944 | if (!*start_lsn) |
| 1945 | *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1946 | |
Christoph Hellwig | 99428ad | 2010-03-23 11:35:45 +1100 | [diff] [blame] | 1947 | /* |
| 1948 | * This loop writes out as many regions as can fit in the amount |
| 1949 | * of space which was allocated by xlog_state_get_iclog_space(). |
| 1950 | */ |
Dave Chinner | 55b6633 | 2010-03-23 11:43:17 +1100 | [diff] [blame] | 1951 | while (lv && index < lv->lv_niovecs) { |
| 1952 | struct xfs_log_iovec *reg = &vecp[index]; |
Christoph Hellwig | 99428ad | 2010-03-23 11:35:45 +1100 | [diff] [blame] | 1953 | struct xlog_op_header *ophdr; |
| 1954 | int start_rec_copy; |
| 1955 | int copy_len; |
| 1956 | int copy_off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1957 | |
Dave Chinner | 55b6633 | 2010-03-23 11:43:17 +1100 | [diff] [blame] | 1958 | ASSERT(reg->i_len % sizeof(__int32_t) == 0); |
Christoph Hellwig | e6b1f27 | 2010-03-23 11:47:38 +1100 | [diff] [blame] | 1959 | ASSERT((unsigned long)ptr % sizeof(__int32_t) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | |
Christoph Hellwig | 99428ad | 2010-03-23 11:35:45 +1100 | [diff] [blame] | 1961 | start_rec_copy = xlog_write_start_rec(ptr, ticket); |
| 1962 | if (start_rec_copy) { |
| 1963 | record_cnt++; |
Christoph Hellwig | e6b1f27 | 2010-03-23 11:47:38 +1100 | [diff] [blame] | 1964 | xlog_write_adv_cnt(&ptr, &len, &log_offset, |
Christoph Hellwig | 99428ad | 2010-03-23 11:35:45 +1100 | [diff] [blame] | 1965 | start_rec_copy); |
| 1966 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | |
Christoph Hellwig | 99428ad | 2010-03-23 11:35:45 +1100 | [diff] [blame] | 1968 | ophdr = xlog_write_setup_ophdr(log, ptr, ticket, flags); |
| 1969 | if (!ophdr) |
| 1970 | return XFS_ERROR(EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | |
Christoph Hellwig | e6b1f27 | 2010-03-23 11:47:38 +1100 | [diff] [blame] | 1972 | xlog_write_adv_cnt(&ptr, &len, &log_offset, |
Christoph Hellwig | 99428ad | 2010-03-23 11:35:45 +1100 | [diff] [blame] | 1973 | sizeof(struct xlog_op_header)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | |
Christoph Hellwig | 99428ad | 2010-03-23 11:35:45 +1100 | [diff] [blame] | 1975 | len += xlog_write_setup_copy(ticket, ophdr, |
| 1976 | iclog->ic_size-log_offset, |
Dave Chinner | 55b6633 | 2010-03-23 11:43:17 +1100 | [diff] [blame] | 1977 | reg->i_len, |
Christoph Hellwig | 99428ad | 2010-03-23 11:35:45 +1100 | [diff] [blame] | 1978 | ©_off, ©_len, |
| 1979 | &partial_copy, |
| 1980 | &partial_copy_len); |
| 1981 | xlog_verify_dest_ptr(log, ptr); |
Dave Chinner | b5203cd | 2010-03-23 11:29:44 +1100 | [diff] [blame] | 1982 | |
Christoph Hellwig | 99428ad | 2010-03-23 11:35:45 +1100 | [diff] [blame] | 1983 | /* copy region */ |
| 1984 | ASSERT(copy_len >= 0); |
Christoph Hellwig | e6b1f27 | 2010-03-23 11:47:38 +1100 | [diff] [blame] | 1985 | memcpy(ptr, reg->i_addr + copy_off, copy_len); |
| 1986 | xlog_write_adv_cnt(&ptr, &len, &log_offset, copy_len); |
Dave Chinner | b5203cd | 2010-03-23 11:29:44 +1100 | [diff] [blame] | 1987 | |
Christoph Hellwig | 99428ad | 2010-03-23 11:35:45 +1100 | [diff] [blame] | 1988 | copy_len += start_rec_copy + sizeof(xlog_op_header_t); |
| 1989 | record_cnt++; |
| 1990 | data_cnt += contwr ? copy_len : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | |
Christoph Hellwig | 99428ad | 2010-03-23 11:35:45 +1100 | [diff] [blame] | 1992 | error = xlog_write_copy_finish(log, iclog, flags, |
| 1993 | &record_cnt, &data_cnt, |
| 1994 | &partial_copy, |
| 1995 | &partial_copy_len, |
| 1996 | log_offset, |
| 1997 | commit_iclog); |
| 1998 | if (error) |
| 1999 | return error; |
| 2000 | |
| 2001 | /* |
| 2002 | * if we had a partial copy, we need to get more iclog |
| 2003 | * space but we don't want to increment the region |
| 2004 | * index because there is still more is this region to |
| 2005 | * write. |
| 2006 | * |
| 2007 | * If we completed writing this region, and we flushed |
| 2008 | * the iclog (indicated by resetting of the record |
| 2009 | * count), then we also need to get more log space. If |
| 2010 | * this was the last record, though, we are done and |
| 2011 | * can just return. |
| 2012 | */ |
| 2013 | if (partial_copy) |
| 2014 | break; |
| 2015 | |
Dave Chinner | 55b6633 | 2010-03-23 11:43:17 +1100 | [diff] [blame] | 2016 | if (++index == lv->lv_niovecs) { |
| 2017 | lv = lv->lv_next; |
| 2018 | index = 0; |
| 2019 | if (lv) |
| 2020 | vecp = lv->lv_iovecp; |
| 2021 | } |
Christoph Hellwig | 99428ad | 2010-03-23 11:35:45 +1100 | [diff] [blame] | 2022 | if (record_cnt == 0) { |
Dave Chinner | 55b6633 | 2010-03-23 11:43:17 +1100 | [diff] [blame] | 2023 | if (!lv) |
Christoph Hellwig | 99428ad | 2010-03-23 11:35:45 +1100 | [diff] [blame] | 2024 | return 0; |
| 2025 | break; |
| 2026 | } |
| 2027 | } |
| 2028 | } |
| 2029 | |
| 2030 | ASSERT(len == 0); |
| 2031 | |
| 2032 | xlog_state_finish_copy(log, iclog, record_cnt, data_cnt); |
| 2033 | if (!commit_iclog) |
| 2034 | return xlog_state_release_iclog(log, iclog); |
| 2035 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | ASSERT(flags & XLOG_COMMIT_TRANS); |
| 2037 | *commit_iclog = iclog; |
| 2038 | return 0; |
Christoph Hellwig | 99428ad | 2010-03-23 11:35:45 +1100 | [diff] [blame] | 2039 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | |
| 2041 | |
| 2042 | /***************************************************************************** |
| 2043 | * |
| 2044 | * State Machine functions |
| 2045 | * |
| 2046 | ***************************************************************************** |
| 2047 | */ |
| 2048 | |
| 2049 | /* Clean iclogs starting from the head. This ordering must be |
| 2050 | * maintained, so an iclog doesn't become ACTIVE beyond one that |
| 2051 | * is SYNCING. This is also required to maintain the notion that we use |
David Chinner | 12017fa | 2008-08-13 16:34:31 +1000 | [diff] [blame] | 2052 | * a ordered wait queue to hold off would be writers to the log when every |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | * iclog is trying to sync to disk. |
| 2054 | * |
| 2055 | * State Change: DIRTY -> ACTIVE |
| 2056 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 2057 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | xlog_state_clean_log(xlog_t *log) |
| 2059 | { |
| 2060 | xlog_in_core_t *iclog; |
| 2061 | int changed = 0; |
| 2062 | |
| 2063 | iclog = log->l_iclog; |
| 2064 | do { |
| 2065 | if (iclog->ic_state == XLOG_STATE_DIRTY) { |
| 2066 | iclog->ic_state = XLOG_STATE_ACTIVE; |
| 2067 | iclog->ic_offset = 0; |
David Chinner | 114d23a | 2008-04-10 12:18:39 +1000 | [diff] [blame] | 2068 | ASSERT(iclog->ic_callback == NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | /* |
| 2070 | * If the number of ops in this iclog indicate it just |
| 2071 | * contains the dummy transaction, we can |
| 2072 | * change state into IDLE (the second time around). |
| 2073 | * Otherwise we should change the state into |
| 2074 | * NEED a dummy. |
| 2075 | * We don't need to cover the dummy. |
| 2076 | */ |
| 2077 | if (!changed && |
Christoph Hellwig | b53e675 | 2007-10-12 10:59:34 +1000 | [diff] [blame] | 2078 | (be32_to_cpu(iclog->ic_header.h_num_logops) == |
| 2079 | XLOG_COVER_OPS)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | changed = 1; |
| 2081 | } else { |
| 2082 | /* |
| 2083 | * We have two dirty iclogs so start over |
| 2084 | * This could also be num of ops indicates |
| 2085 | * this is not the dummy going out. |
| 2086 | */ |
| 2087 | changed = 2; |
| 2088 | } |
| 2089 | iclog->ic_header.h_num_logops = 0; |
| 2090 | memset(iclog->ic_header.h_cycle_data, 0, |
| 2091 | sizeof(iclog->ic_header.h_cycle_data)); |
| 2092 | iclog->ic_header.h_lsn = 0; |
| 2093 | } else if (iclog->ic_state == XLOG_STATE_ACTIVE) |
| 2094 | /* do nothing */; |
| 2095 | else |
| 2096 | break; /* stop cleaning */ |
| 2097 | iclog = iclog->ic_next; |
| 2098 | } while (iclog != log->l_iclog); |
| 2099 | |
| 2100 | /* log is locked when we are called */ |
| 2101 | /* |
| 2102 | * Change state for the dummy log recording. |
| 2103 | * We usually go to NEED. But we go to NEED2 if the changed indicates |
| 2104 | * we are done writing the dummy record. |
| 2105 | * If we are done with the second dummy recored (DONE2), then |
| 2106 | * we go to IDLE. |
| 2107 | */ |
| 2108 | if (changed) { |
| 2109 | switch (log->l_covered_state) { |
| 2110 | case XLOG_STATE_COVER_IDLE: |
| 2111 | case XLOG_STATE_COVER_NEED: |
| 2112 | case XLOG_STATE_COVER_NEED2: |
| 2113 | log->l_covered_state = XLOG_STATE_COVER_NEED; |
| 2114 | break; |
| 2115 | |
| 2116 | case XLOG_STATE_COVER_DONE: |
| 2117 | if (changed == 1) |
| 2118 | log->l_covered_state = XLOG_STATE_COVER_NEED2; |
| 2119 | else |
| 2120 | log->l_covered_state = XLOG_STATE_COVER_NEED; |
| 2121 | break; |
| 2122 | |
| 2123 | case XLOG_STATE_COVER_DONE2: |
| 2124 | if (changed == 1) |
| 2125 | log->l_covered_state = XLOG_STATE_COVER_IDLE; |
| 2126 | else |
| 2127 | log->l_covered_state = XLOG_STATE_COVER_NEED; |
| 2128 | break; |
| 2129 | |
| 2130 | default: |
| 2131 | ASSERT(0); |
| 2132 | } |
| 2133 | } |
| 2134 | } /* xlog_state_clean_log */ |
| 2135 | |
| 2136 | STATIC xfs_lsn_t |
| 2137 | xlog_get_lowest_lsn( |
| 2138 | xlog_t *log) |
| 2139 | { |
| 2140 | xlog_in_core_t *lsn_log; |
| 2141 | xfs_lsn_t lowest_lsn, lsn; |
| 2142 | |
| 2143 | lsn_log = log->l_iclog; |
| 2144 | lowest_lsn = 0; |
| 2145 | do { |
| 2146 | if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) { |
Christoph Hellwig | b53e675 | 2007-10-12 10:59:34 +1000 | [diff] [blame] | 2147 | lsn = be64_to_cpu(lsn_log->ic_header.h_lsn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | if ((lsn && !lowest_lsn) || |
| 2149 | (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) { |
| 2150 | lowest_lsn = lsn; |
| 2151 | } |
| 2152 | } |
| 2153 | lsn_log = lsn_log->ic_next; |
| 2154 | } while (lsn_log != log->l_iclog); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2155 | return lowest_lsn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | } |
| 2157 | |
| 2158 | |
| 2159 | STATIC void |
| 2160 | xlog_state_do_callback( |
| 2161 | xlog_t *log, |
| 2162 | int aborted, |
| 2163 | xlog_in_core_t *ciclog) |
| 2164 | { |
| 2165 | xlog_in_core_t *iclog; |
| 2166 | xlog_in_core_t *first_iclog; /* used to know when we've |
| 2167 | * processed all iclogs once */ |
| 2168 | xfs_log_callback_t *cb, *cb_next; |
| 2169 | int flushcnt = 0; |
| 2170 | xfs_lsn_t lowest_lsn; |
| 2171 | int ioerrors; /* counter: iclogs with errors */ |
| 2172 | int loopdidcallbacks; /* flag: inner loop did callbacks*/ |
| 2173 | int funcdidcallbacks; /* flag: function did callbacks */ |
| 2174 | int repeats; /* for issuing console warnings if |
| 2175 | * looping too many times */ |
Matthew Wilcox | d748c62 | 2008-05-19 16:34:27 +1000 | [diff] [blame] | 2176 | int wake = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 2178 | spin_lock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | first_iclog = iclog = log->l_iclog; |
| 2180 | ioerrors = 0; |
| 2181 | funcdidcallbacks = 0; |
| 2182 | repeats = 0; |
| 2183 | |
| 2184 | do { |
| 2185 | /* |
| 2186 | * Scan all iclogs starting with the one pointed to by the |
| 2187 | * log. Reset this starting point each time the log is |
| 2188 | * unlocked (during callbacks). |
| 2189 | * |
| 2190 | * Keep looping through iclogs until one full pass is made |
| 2191 | * without running any callbacks. |
| 2192 | */ |
| 2193 | first_iclog = log->l_iclog; |
| 2194 | iclog = log->l_iclog; |
| 2195 | loopdidcallbacks = 0; |
| 2196 | repeats++; |
| 2197 | |
| 2198 | do { |
| 2199 | |
| 2200 | /* skip all iclogs in the ACTIVE & DIRTY states */ |
| 2201 | if (iclog->ic_state & |
| 2202 | (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) { |
| 2203 | iclog = iclog->ic_next; |
| 2204 | continue; |
| 2205 | } |
| 2206 | |
| 2207 | /* |
| 2208 | * Between marking a filesystem SHUTDOWN and stopping |
| 2209 | * the log, we do flush all iclogs to disk (if there |
| 2210 | * wasn't a log I/O error). So, we do want things to |
| 2211 | * go smoothly in case of just a SHUTDOWN w/o a |
| 2212 | * LOG_IO_ERROR. |
| 2213 | */ |
| 2214 | if (!(iclog->ic_state & XLOG_STATE_IOERROR)) { |
| 2215 | /* |
| 2216 | * Can only perform callbacks in order. Since |
| 2217 | * this iclog is not in the DONE_SYNC/ |
| 2218 | * DO_CALLBACK state, we skip the rest and |
| 2219 | * just try to clean up. If we set our iclog |
| 2220 | * to DO_CALLBACK, we will not process it when |
| 2221 | * we retry since a previous iclog is in the |
| 2222 | * CALLBACK and the state cannot change since |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 2223 | * we are holding the l_icloglock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2224 | */ |
| 2225 | if (!(iclog->ic_state & |
| 2226 | (XLOG_STATE_DONE_SYNC | |
| 2227 | XLOG_STATE_DO_CALLBACK))) { |
| 2228 | if (ciclog && (ciclog->ic_state == |
| 2229 | XLOG_STATE_DONE_SYNC)) { |
| 2230 | ciclog->ic_state = XLOG_STATE_DO_CALLBACK; |
| 2231 | } |
| 2232 | break; |
| 2233 | } |
| 2234 | /* |
| 2235 | * We now have an iclog that is in either the |
| 2236 | * DO_CALLBACK or DONE_SYNC states. The other |
| 2237 | * states (WANT_SYNC, SYNCING, or CALLBACK were |
| 2238 | * caught by the above if and are going to |
| 2239 | * clean (i.e. we aren't doing their callbacks) |
| 2240 | * see the above if. |
| 2241 | */ |
| 2242 | |
| 2243 | /* |
| 2244 | * We will do one more check here to see if we |
| 2245 | * have chased our tail around. |
| 2246 | */ |
| 2247 | |
| 2248 | lowest_lsn = xlog_get_lowest_lsn(log); |
Christoph Hellwig | b53e675 | 2007-10-12 10:59:34 +1000 | [diff] [blame] | 2249 | if (lowest_lsn && |
| 2250 | XFS_LSN_CMP(lowest_lsn, |
Dave Chinner | 84f3c68 | 2010-12-03 22:11:29 +1100 | [diff] [blame] | 2251 | be64_to_cpu(iclog->ic_header.h_lsn)) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | iclog = iclog->ic_next; |
| 2253 | continue; /* Leave this iclog for |
| 2254 | * another thread */ |
| 2255 | } |
| 2256 | |
| 2257 | iclog->ic_state = XLOG_STATE_CALLBACK; |
| 2258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | |
Dave Chinner | 84f3c68 | 2010-12-03 22:11:29 +1100 | [diff] [blame] | 2260 | /* |
| 2261 | * update the last_sync_lsn before we drop the |
| 2262 | * icloglock to ensure we are the only one that |
| 2263 | * can update it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2264 | */ |
Dave Chinner | 84f3c68 | 2010-12-03 22:11:29 +1100 | [diff] [blame] | 2265 | ASSERT(XFS_LSN_CMP(atomic64_read(&log->l_last_sync_lsn), |
| 2266 | be64_to_cpu(iclog->ic_header.h_lsn)) <= 0); |
| 2267 | atomic64_set(&log->l_last_sync_lsn, |
| 2268 | be64_to_cpu(iclog->ic_header.h_lsn)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | |
Dave Chinner | 84f3c68 | 2010-12-03 22:11:29 +1100 | [diff] [blame] | 2270 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2271 | ioerrors++; |
Dave Chinner | 84f3c68 | 2010-12-03 22:11:29 +1100 | [diff] [blame] | 2272 | |
| 2273 | spin_unlock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | |
David Chinner | 114d23a | 2008-04-10 12:18:39 +1000 | [diff] [blame] | 2275 | /* |
| 2276 | * Keep processing entries in the callback list until |
| 2277 | * we come around and it is empty. We need to |
| 2278 | * atomically see that the list is empty and change the |
| 2279 | * state to DIRTY so that we don't miss any more |
| 2280 | * callbacks being added. |
| 2281 | */ |
| 2282 | spin_lock(&iclog->ic_callback_lock); |
| 2283 | cb = iclog->ic_callback; |
Christoph Hellwig | 4b80916 | 2007-08-16 15:37:36 +1000 | [diff] [blame] | 2284 | while (cb) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | iclog->ic_callback_tail = &(iclog->ic_callback); |
| 2286 | iclog->ic_callback = NULL; |
David Chinner | 114d23a | 2008-04-10 12:18:39 +1000 | [diff] [blame] | 2287 | spin_unlock(&iclog->ic_callback_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | |
| 2289 | /* perform callbacks in the order given */ |
Christoph Hellwig | 4b80916 | 2007-08-16 15:37:36 +1000 | [diff] [blame] | 2290 | for (; cb; cb = cb_next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | cb_next = cb->cb_next; |
| 2292 | cb->cb_func(cb->cb_arg, aborted); |
| 2293 | } |
David Chinner | 114d23a | 2008-04-10 12:18:39 +1000 | [diff] [blame] | 2294 | spin_lock(&iclog->ic_callback_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | cb = iclog->ic_callback; |
| 2296 | } |
| 2297 | |
| 2298 | loopdidcallbacks++; |
| 2299 | funcdidcallbacks++; |
| 2300 | |
David Chinner | 114d23a | 2008-04-10 12:18:39 +1000 | [diff] [blame] | 2301 | spin_lock(&log->l_icloglock); |
Christoph Hellwig | 4b80916 | 2007-08-16 15:37:36 +1000 | [diff] [blame] | 2302 | ASSERT(iclog->ic_callback == NULL); |
David Chinner | 114d23a | 2008-04-10 12:18:39 +1000 | [diff] [blame] | 2303 | spin_unlock(&iclog->ic_callback_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | if (!(iclog->ic_state & XLOG_STATE_IOERROR)) |
| 2305 | iclog->ic_state = XLOG_STATE_DIRTY; |
| 2306 | |
| 2307 | /* |
| 2308 | * Transition from DIRTY to ACTIVE if applicable. |
| 2309 | * NOP if STATE_IOERROR. |
| 2310 | */ |
| 2311 | xlog_state_clean_log(log); |
| 2312 | |
| 2313 | /* wake up threads waiting in xfs_log_force() */ |
Dave Chinner | eb40a87 | 2010-12-21 12:09:01 +1100 | [diff] [blame] | 2314 | wake_up_all(&iclog->ic_force_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | |
| 2316 | iclog = iclog->ic_next; |
| 2317 | } while (first_iclog != iclog); |
Nathan Scott | a3c6685e | 2006-09-28 11:02:14 +1000 | [diff] [blame] | 2318 | |
| 2319 | if (repeats > 5000) { |
| 2320 | flushcnt += repeats; |
| 2321 | repeats = 0; |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 2322 | xfs_warn(log->l_mp, |
Nathan Scott | a3c6685e | 2006-09-28 11:02:14 +1000 | [diff] [blame] | 2323 | "%s: possible infinite loop (%d iterations)", |
Harvey Harrison | 34a622b | 2008-04-10 12:19:21 +1000 | [diff] [blame] | 2324 | __func__, flushcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | } |
| 2326 | } while (!ioerrors && loopdidcallbacks); |
| 2327 | |
| 2328 | /* |
| 2329 | * make one last gasp attempt to see if iclogs are being left in |
| 2330 | * limbo.. |
| 2331 | */ |
| 2332 | #ifdef DEBUG |
| 2333 | if (funcdidcallbacks) { |
| 2334 | first_iclog = iclog = log->l_iclog; |
| 2335 | do { |
| 2336 | ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK); |
| 2337 | /* |
| 2338 | * Terminate the loop if iclogs are found in states |
| 2339 | * which will cause other threads to clean up iclogs. |
| 2340 | * |
| 2341 | * SYNCING - i/o completion will go through logs |
| 2342 | * DONE_SYNC - interrupt thread should be waiting for |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 2343 | * l_icloglock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2344 | * IOERROR - give up hope all ye who enter here |
| 2345 | */ |
| 2346 | if (iclog->ic_state == XLOG_STATE_WANT_SYNC || |
| 2347 | iclog->ic_state == XLOG_STATE_SYNCING || |
| 2348 | iclog->ic_state == XLOG_STATE_DONE_SYNC || |
| 2349 | iclog->ic_state == XLOG_STATE_IOERROR ) |
| 2350 | break; |
| 2351 | iclog = iclog->ic_next; |
| 2352 | } while (first_iclog != iclog); |
| 2353 | } |
| 2354 | #endif |
| 2355 | |
Matthew Wilcox | d748c62 | 2008-05-19 16:34:27 +1000 | [diff] [blame] | 2356 | if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR)) |
| 2357 | wake = 1; |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 2358 | spin_unlock(&log->l_icloglock); |
Matthew Wilcox | d748c62 | 2008-05-19 16:34:27 +1000 | [diff] [blame] | 2359 | |
| 2360 | if (wake) |
Dave Chinner | eb40a87 | 2010-12-21 12:09:01 +1100 | [diff] [blame] | 2361 | wake_up_all(&log->l_flush_wait); |
Matthew Wilcox | d748c62 | 2008-05-19 16:34:27 +1000 | [diff] [blame] | 2362 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | |
| 2364 | |
| 2365 | /* |
| 2366 | * Finish transitioning this iclog to the dirty state. |
| 2367 | * |
| 2368 | * Make sure that we completely execute this routine only when this is |
| 2369 | * the last call to the iclog. There is a good chance that iclog flushes, |
| 2370 | * when we reach the end of the physical log, get turned into 2 separate |
| 2371 | * calls to bwrite. Hence, one iclog flush could generate two calls to this |
| 2372 | * routine. By using the reference count bwritecnt, we guarantee that only |
| 2373 | * the second completion goes through. |
| 2374 | * |
| 2375 | * Callbacks could take time, so they are done outside the scope of the |
David Chinner | 12017fa | 2008-08-13 16:34:31 +1000 | [diff] [blame] | 2376 | * global state machine log lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | */ |
David Chinner | a8272ce | 2007-11-23 16:28:09 +1100 | [diff] [blame] | 2378 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2379 | xlog_state_done_syncing( |
| 2380 | xlog_in_core_t *iclog, |
| 2381 | int aborted) |
| 2382 | { |
| 2383 | xlog_t *log = iclog->ic_log; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2384 | |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 2385 | spin_lock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | |
| 2387 | ASSERT(iclog->ic_state == XLOG_STATE_SYNCING || |
| 2388 | iclog->ic_state == XLOG_STATE_IOERROR); |
David Chinner | 155cc6b | 2008-03-06 13:44:14 +1100 | [diff] [blame] | 2389 | ASSERT(atomic_read(&iclog->ic_refcnt) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2); |
| 2391 | |
| 2392 | |
| 2393 | /* |
| 2394 | * If we got an error, either on the first buffer, or in the case of |
| 2395 | * split log writes, on the second, we mark ALL iclogs STATE_IOERROR, |
| 2396 | * and none should ever be attempted to be written to disk |
| 2397 | * again. |
| 2398 | */ |
| 2399 | if (iclog->ic_state != XLOG_STATE_IOERROR) { |
| 2400 | if (--iclog->ic_bwritecnt == 1) { |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 2401 | spin_unlock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | return; |
| 2403 | } |
| 2404 | iclog->ic_state = XLOG_STATE_DONE_SYNC; |
| 2405 | } |
| 2406 | |
| 2407 | /* |
| 2408 | * Someone could be sleeping prior to writing out the next |
| 2409 | * iclog buffer, we wake them all, one will get to do the |
| 2410 | * I/O, the others get to wait for the result. |
| 2411 | */ |
Dave Chinner | eb40a87 | 2010-12-21 12:09:01 +1100 | [diff] [blame] | 2412 | wake_up_all(&iclog->ic_write_wait); |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 2413 | spin_unlock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | xlog_state_do_callback(log, aborted, iclog); /* also cleans log */ |
| 2415 | } /* xlog_state_done_syncing */ |
| 2416 | |
| 2417 | |
| 2418 | /* |
| 2419 | * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must |
David Chinner | 12017fa | 2008-08-13 16:34:31 +1000 | [diff] [blame] | 2420 | * sleep. We wait on the flush queue on the head iclog as that should be |
| 2421 | * the first iclog to complete flushing. Hence if all iclogs are syncing, |
| 2422 | * we will wait here and all new writes will sleep until a sync completes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2423 | * |
| 2424 | * The in-core logs are used in a circular fashion. They are not used |
| 2425 | * out-of-order even when an iclog past the head is free. |
| 2426 | * |
| 2427 | * return: |
| 2428 | * * log_offset where xlog_write() can start writing into the in-core |
| 2429 | * log's data space. |
| 2430 | * * in-core log pointer to which xlog_write() should write. |
| 2431 | * * boolean indicating this is a continued write to an in-core log. |
| 2432 | * If this is the last write, then the in-core log's offset field |
| 2433 | * needs to be incremented, depending on the amount of data which |
| 2434 | * is copied. |
| 2435 | */ |
David Chinner | a8272ce | 2007-11-23 16:28:09 +1100 | [diff] [blame] | 2436 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2437 | xlog_state_get_iclog_space(xlog_t *log, |
| 2438 | int len, |
| 2439 | xlog_in_core_t **iclogp, |
| 2440 | xlog_ticket_t *ticket, |
| 2441 | int *continued_write, |
| 2442 | int *logoffsetp) |
| 2443 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2444 | int log_offset; |
| 2445 | xlog_rec_header_t *head; |
| 2446 | xlog_in_core_t *iclog; |
| 2447 | int error; |
| 2448 | |
| 2449 | restart: |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 2450 | spin_lock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | if (XLOG_FORCED_SHUTDOWN(log)) { |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 2452 | spin_unlock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2453 | return XFS_ERROR(EIO); |
| 2454 | } |
| 2455 | |
| 2456 | iclog = log->l_iclog; |
Matthew Wilcox | d748c62 | 2008-05-19 16:34:27 +1000 | [diff] [blame] | 2457 | if (iclog->ic_state != XLOG_STATE_ACTIVE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2458 | XFS_STATS_INC(xs_log_noiclogs); |
Matthew Wilcox | d748c62 | 2008-05-19 16:34:27 +1000 | [diff] [blame] | 2459 | |
| 2460 | /* Wait for log writes to have flushed */ |
Dave Chinner | eb40a87 | 2010-12-21 12:09:01 +1100 | [diff] [blame] | 2461 | xlog_wait(&log->l_flush_wait, &log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2462 | goto restart; |
| 2463 | } |
Matthew Wilcox | d748c62 | 2008-05-19 16:34:27 +1000 | [diff] [blame] | 2464 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2465 | head = &iclog->ic_header; |
| 2466 | |
David Chinner | 155cc6b | 2008-03-06 13:44:14 +1100 | [diff] [blame] | 2467 | atomic_inc(&iclog->ic_refcnt); /* prevents sync */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2468 | log_offset = iclog->ic_offset; |
| 2469 | |
| 2470 | /* On the 1st write to an iclog, figure out lsn. This works |
| 2471 | * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are |
| 2472 | * committing to. If the offset is set, that's how many blocks |
| 2473 | * must be written. |
| 2474 | */ |
| 2475 | if (log_offset == 0) { |
| 2476 | ticket->t_curr_res -= log->l_iclog_hsize; |
Christoph Hellwig | 0adba53 | 2007-08-30 17:21:46 +1000 | [diff] [blame] | 2477 | xlog_tic_add_region(ticket, |
Tim Shimmin | 7e9c639 | 2005-09-02 16:42:05 +1000 | [diff] [blame] | 2478 | log->l_iclog_hsize, |
| 2479 | XLOG_REG_TYPE_LRHEADER); |
Christoph Hellwig | b53e675 | 2007-10-12 10:59:34 +1000 | [diff] [blame] | 2480 | head->h_cycle = cpu_to_be32(log->l_curr_cycle); |
| 2481 | head->h_lsn = cpu_to_be64( |
Christoph Hellwig | 03bea6f | 2007-10-12 10:58:05 +1000 | [diff] [blame] | 2482 | xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | ASSERT(log->l_curr_block >= 0); |
| 2484 | } |
| 2485 | |
| 2486 | /* If there is enough room to write everything, then do it. Otherwise, |
| 2487 | * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC |
| 2488 | * bit is on, so this will get flushed out. Don't update ic_offset |
| 2489 | * until you know exactly how many bytes get copied. Therefore, wait |
| 2490 | * until later to update ic_offset. |
| 2491 | * |
| 2492 | * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's |
| 2493 | * can fit into remaining data section. |
| 2494 | */ |
| 2495 | if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) { |
| 2496 | xlog_state_switch_iclogs(log, iclog, iclog->ic_size); |
| 2497 | |
Dave Chinner | 49641f1 | 2008-07-11 17:43:55 +1000 | [diff] [blame] | 2498 | /* |
| 2499 | * If I'm the only one writing to this iclog, sync it to disk. |
| 2500 | * We need to do an atomic compare and decrement here to avoid |
| 2501 | * racing with concurrent atomic_dec_and_lock() calls in |
| 2502 | * xlog_state_release_iclog() when there is more than one |
| 2503 | * reference to the iclog. |
| 2504 | */ |
| 2505 | if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) { |
| 2506 | /* we are the only one */ |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 2507 | spin_unlock(&log->l_icloglock); |
Dave Chinner | 49641f1 | 2008-07-11 17:43:55 +1000 | [diff] [blame] | 2508 | error = xlog_state_release_iclog(log, iclog); |
| 2509 | if (error) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2510 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2511 | } else { |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 2512 | spin_unlock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | } |
| 2514 | goto restart; |
| 2515 | } |
| 2516 | |
| 2517 | /* Do we have enough room to write the full amount in the remainder |
| 2518 | * of this iclog? Or must we continue a write on the next iclog and |
| 2519 | * mark this iclog as completely taken? In the case where we switch |
| 2520 | * iclogs (to mark it taken), this particular iclog will release/sync |
| 2521 | * to disk in xlog_write(). |
| 2522 | */ |
| 2523 | if (len <= iclog->ic_size - iclog->ic_offset) { |
| 2524 | *continued_write = 0; |
| 2525 | iclog->ic_offset += len; |
| 2526 | } else { |
| 2527 | *continued_write = 1; |
| 2528 | xlog_state_switch_iclogs(log, iclog, iclog->ic_size); |
| 2529 | } |
| 2530 | *iclogp = iclog; |
| 2531 | |
| 2532 | ASSERT(iclog->ic_offset <= iclog->ic_size); |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 2533 | spin_unlock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2534 | |
| 2535 | *logoffsetp = log_offset; |
| 2536 | return 0; |
| 2537 | } /* xlog_state_get_iclog_space */ |
| 2538 | |
| 2539 | /* |
| 2540 | * Atomically get the log space required for a log ticket. |
| 2541 | * |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 2542 | * Once a ticket gets put onto the reserveq, it will only return after the |
| 2543 | * needed reservation is satisfied. |
Dave Chinner | 3f16b98 | 2010-12-21 12:29:01 +1100 | [diff] [blame] | 2544 | * |
| 2545 | * This function is structured so that it has a lock free fast path. This is |
| 2546 | * necessary because every new transaction reservation will come through this |
| 2547 | * path. Hence any lock will be globally hot if we take it unconditionally on |
| 2548 | * every pass. |
| 2549 | * |
| 2550 | * As tickets are only ever moved on and off the reserveq under the |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 2551 | * l_grant_reserve_lock, we only need to take that lock if we are going to add |
| 2552 | * the ticket to the queue and sleep. We can avoid taking the lock if the ticket |
| 2553 | * was never added to the reserveq because the t_queue list head will be empty |
| 2554 | * and we hold the only reference to it so it can safely be checked unlocked. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2555 | */ |
| 2556 | STATIC int |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 2557 | xlog_grant_log_space( |
| 2558 | struct log *log, |
| 2559 | struct xlog_ticket *tic) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2560 | { |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 2561 | int free_bytes, need_bytes; |
| 2562 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2563 | |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 2564 | ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2565 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2566 | trace_xfs_log_grant_enter(log, tic); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2567 | |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 2568 | /* |
| 2569 | * If there are other waiters on the queue then give them a chance at |
| 2570 | * logspace before us. Wake up the first waiters, if we do not wake |
| 2571 | * up all the waiters then go to sleep waiting for more free space, |
| 2572 | * otherwise try to get some space for this transaction. |
| 2573 | */ |
Dave Chinner | 3f16b98 | 2010-12-21 12:29:01 +1100 | [diff] [blame] | 2574 | need_bytes = tic->t_unit_res; |
| 2575 | if (tic->t_flags & XFS_LOG_PERM_RESERV) |
| 2576 | need_bytes *= tic->t_ocnt; |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 2577 | free_bytes = xlog_space_left(log, &log->l_grant_reserve_head); |
Dave Chinner | 3f16b98 | 2010-12-21 12:29:01 +1100 | [diff] [blame] | 2578 | if (!list_empty_careful(&log->l_reserveq)) { |
| 2579 | spin_lock(&log->l_grant_reserve_lock); |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 2580 | if (!xlog_reserveq_wake(log, &free_bytes) || |
| 2581 | free_bytes < need_bytes) |
| 2582 | error = xlog_reserveq_wait(log, tic, need_bytes); |
| 2583 | spin_unlock(&log->l_grant_reserve_lock); |
| 2584 | } else if (free_bytes < need_bytes) { |
Dave Chinner | 3f16b98 | 2010-12-21 12:29:01 +1100 | [diff] [blame] | 2585 | spin_lock(&log->l_grant_reserve_lock); |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 2586 | error = xlog_reserveq_wait(log, tic, need_bytes); |
Dave Chinner | 3f16b98 | 2010-12-21 12:29:01 +1100 | [diff] [blame] | 2587 | spin_unlock(&log->l_grant_reserve_lock); |
| 2588 | } |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 2589 | if (error) |
| 2590 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2591 | |
Dave Chinner | a69ed03 | 2010-12-21 12:08:20 +1100 | [diff] [blame] | 2592 | xlog_grant_add_space(log, &log->l_grant_reserve_head, need_bytes); |
| 2593 | xlog_grant_add_space(log, &log->l_grant_write_head, need_bytes); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2594 | trace_xfs_log_grant_exit(log, tic); |
Dave Chinner | 3f336c6 | 2010-12-21 12:02:52 +1100 | [diff] [blame] | 2595 | xlog_verify_grant_tail(log); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2596 | return 0; |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 2597 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | |
| 2599 | /* |
| 2600 | * Replenish the byte reservation required by moving the grant write head. |
| 2601 | * |
Dave Chinner | 3f16b98 | 2010-12-21 12:29:01 +1100 | [diff] [blame] | 2602 | * Similar to xlog_grant_log_space, the function is structured to have a lock |
| 2603 | * free fast path. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2604 | */ |
| 2605 | STATIC int |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 2606 | xlog_regrant_write_log_space( |
| 2607 | struct log *log, |
| 2608 | struct xlog_ticket *tic) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2609 | { |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 2610 | int free_bytes, need_bytes; |
| 2611 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2612 | |
| 2613 | tic->t_curr_res = tic->t_unit_res; |
Christoph Hellwig | 0adba53 | 2007-08-30 17:21:46 +1000 | [diff] [blame] | 2614 | xlog_tic_reset_res(tic); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2615 | |
| 2616 | if (tic->t_cnt > 0) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2617 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2618 | |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 2619 | ASSERT(!(log->l_flags & XLOG_ACTIVE_RECOVERY)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2620 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2621 | trace_xfs_log_regrant_write_enter(log, tic); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2622 | |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 2623 | /* |
| 2624 | * If there are other waiters on the queue then give them a chance at |
| 2625 | * logspace before us. Wake up the first waiters, if we do not wake |
| 2626 | * up all the waiters then go to sleep waiting for more free space, |
| 2627 | * otherwise try to get some space for this transaction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2628 | */ |
Dave Chinner | 9d7fef7 | 2009-04-06 18:42:59 +0200 | [diff] [blame] | 2629 | need_bytes = tic->t_unit_res; |
Dave Chinner | a69ed03 | 2010-12-21 12:08:20 +1100 | [diff] [blame] | 2630 | free_bytes = xlog_space_left(log, &log->l_grant_write_head); |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 2631 | if (!list_empty_careful(&log->l_writeq)) { |
Dave Chinner | 3f16b98 | 2010-12-21 12:29:01 +1100 | [diff] [blame] | 2632 | spin_lock(&log->l_grant_write_lock); |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 2633 | if (!xlog_writeq_wake(log, &free_bytes) || |
| 2634 | free_bytes < need_bytes) |
| 2635 | error = xlog_writeq_wait(log, tic, need_bytes); |
| 2636 | spin_unlock(&log->l_grant_write_lock); |
| 2637 | } else if (free_bytes < need_bytes) { |
Dave Chinner | 3f16b98 | 2010-12-21 12:29:01 +1100 | [diff] [blame] | 2638 | spin_lock(&log->l_grant_write_lock); |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 2639 | error = xlog_writeq_wait(log, tic, need_bytes); |
Dave Chinner | 3f16b98 | 2010-12-21 12:29:01 +1100 | [diff] [blame] | 2640 | spin_unlock(&log->l_grant_write_lock); |
| 2641 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2642 | |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 2643 | if (error) |
| 2644 | return error; |
| 2645 | |
Dave Chinner | a69ed03 | 2010-12-21 12:08:20 +1100 | [diff] [blame] | 2646 | xlog_grant_add_space(log, &log->l_grant_write_head, need_bytes); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2647 | trace_xfs_log_regrant_write_exit(log, tic); |
Dave Chinner | 3f336c6 | 2010-12-21 12:02:52 +1100 | [diff] [blame] | 2648 | xlog_verify_grant_tail(log); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2649 | return 0; |
Christoph Hellwig | 9f9c19e | 2011-11-28 08:17:36 +0000 | [diff] [blame] | 2650 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2651 | |
| 2652 | /* The first cnt-1 times through here we don't need to |
| 2653 | * move the grant write head because the permanent |
| 2654 | * reservation has reserved cnt times the unit amount. |
| 2655 | * Release part of current permanent unit reservation and |
| 2656 | * reset current reservation to be one units worth. Also |
| 2657 | * move grant reservation head forward. |
| 2658 | */ |
| 2659 | STATIC void |
| 2660 | xlog_regrant_reserve_log_space(xlog_t *log, |
| 2661 | xlog_ticket_t *ticket) |
| 2662 | { |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2663 | trace_xfs_log_regrant_reserve_enter(log, ticket); |
| 2664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2665 | if (ticket->t_cnt > 0) |
| 2666 | ticket->t_cnt--; |
| 2667 | |
Dave Chinner | a69ed03 | 2010-12-21 12:08:20 +1100 | [diff] [blame] | 2668 | xlog_grant_sub_space(log, &log->l_grant_reserve_head, |
| 2669 | ticket->t_curr_res); |
| 2670 | xlog_grant_sub_space(log, &log->l_grant_write_head, |
| 2671 | ticket->t_curr_res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2672 | ticket->t_curr_res = ticket->t_unit_res; |
Christoph Hellwig | 0adba53 | 2007-08-30 17:21:46 +1000 | [diff] [blame] | 2673 | xlog_tic_reset_res(ticket); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2674 | |
| 2675 | trace_xfs_log_regrant_reserve_sub(log, ticket); |
| 2676 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2677 | /* just return if we still have some of the pre-reserved space */ |
Dave Chinner | d0eb2f3 | 2010-12-21 12:29:14 +1100 | [diff] [blame] | 2678 | if (ticket->t_cnt > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2679 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2680 | |
Dave Chinner | a69ed03 | 2010-12-21 12:08:20 +1100 | [diff] [blame] | 2681 | xlog_grant_add_space(log, &log->l_grant_reserve_head, |
| 2682 | ticket->t_unit_res); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2683 | |
| 2684 | trace_xfs_log_regrant_reserve_exit(log, ticket); |
| 2685 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2686 | ticket->t_curr_res = ticket->t_unit_res; |
Christoph Hellwig | 0adba53 | 2007-08-30 17:21:46 +1000 | [diff] [blame] | 2687 | xlog_tic_reset_res(ticket); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2688 | } /* xlog_regrant_reserve_log_space */ |
| 2689 | |
| 2690 | |
| 2691 | /* |
| 2692 | * Give back the space left from a reservation. |
| 2693 | * |
| 2694 | * All the information we need to make a correct determination of space left |
| 2695 | * is present. For non-permanent reservations, things are quite easy. The |
| 2696 | * count should have been decremented to zero. We only need to deal with the |
| 2697 | * space remaining in the current reservation part of the ticket. If the |
| 2698 | * ticket contains a permanent reservation, there may be left over space which |
| 2699 | * needs to be released. A count of N means that N-1 refills of the current |
| 2700 | * reservation can be done before we need to ask for more space. The first |
| 2701 | * one goes to fill up the first current reservation. Once we run out of |
| 2702 | * space, the count will stay at zero and the only space remaining will be |
| 2703 | * in the current reservation field. |
| 2704 | */ |
| 2705 | STATIC void |
| 2706 | xlog_ungrant_log_space(xlog_t *log, |
| 2707 | xlog_ticket_t *ticket) |
| 2708 | { |
Dave Chinner | 663e496 | 2010-12-21 12:06:05 +1100 | [diff] [blame] | 2709 | int bytes; |
| 2710 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2711 | if (ticket->t_cnt > 0) |
| 2712 | ticket->t_cnt--; |
| 2713 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2714 | trace_xfs_log_ungrant_enter(log, ticket); |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2715 | trace_xfs_log_ungrant_sub(log, ticket); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2716 | |
Dave Chinner | 663e496 | 2010-12-21 12:06:05 +1100 | [diff] [blame] | 2717 | /* |
| 2718 | * If this is a permanent reservation ticket, we may be able to free |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2719 | * up more space based on the remaining count. |
| 2720 | */ |
Dave Chinner | 663e496 | 2010-12-21 12:06:05 +1100 | [diff] [blame] | 2721 | bytes = ticket->t_curr_res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2722 | if (ticket->t_cnt > 0) { |
| 2723 | ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV); |
Dave Chinner | 663e496 | 2010-12-21 12:06:05 +1100 | [diff] [blame] | 2724 | bytes += ticket->t_unit_res*ticket->t_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2725 | } |
| 2726 | |
Dave Chinner | a69ed03 | 2010-12-21 12:08:20 +1100 | [diff] [blame] | 2727 | xlog_grant_sub_space(log, &log->l_grant_reserve_head, bytes); |
| 2728 | xlog_grant_sub_space(log, &log->l_grant_write_head, bytes); |
Dave Chinner | 663e496 | 2010-12-21 12:06:05 +1100 | [diff] [blame] | 2729 | |
Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 2730 | trace_xfs_log_ungrant_exit(log, ticket); |
| 2731 | |
Christoph Hellwig | cfb7cdc | 2012-02-20 02:31:23 +0000 | [diff] [blame] | 2732 | xfs_log_space_wake(log->l_mp); |
Christoph Hellwig | 09a423a | 2012-02-20 02:31:20 +0000 | [diff] [blame] | 2733 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2734 | |
| 2735 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2736 | * Flush iclog to disk if this is the last reference to the given iclog and |
| 2737 | * the WANT_SYNC bit is set. |
| 2738 | * |
| 2739 | * When this function is entered, the iclog is not necessarily in the |
| 2740 | * WANT_SYNC state. It may be sitting around waiting to get filled. |
| 2741 | * |
| 2742 | * |
| 2743 | */ |
David Chinner | a8272ce | 2007-11-23 16:28:09 +1100 | [diff] [blame] | 2744 | STATIC int |
David Chinner | b589334 | 2008-03-06 13:44:06 +1100 | [diff] [blame] | 2745 | xlog_state_release_iclog( |
| 2746 | xlog_t *log, |
| 2747 | xlog_in_core_t *iclog) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2748 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2749 | int sync = 0; /* do we sync? */ |
| 2750 | |
David Chinner | 155cc6b | 2008-03-06 13:44:14 +1100 | [diff] [blame] | 2751 | if (iclog->ic_state & XLOG_STATE_IOERROR) |
| 2752 | return XFS_ERROR(EIO); |
| 2753 | |
| 2754 | ASSERT(atomic_read(&iclog->ic_refcnt) > 0); |
| 2755 | if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock)) |
| 2756 | return 0; |
| 2757 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2758 | if (iclog->ic_state & XLOG_STATE_IOERROR) { |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 2759 | spin_unlock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2760 | return XFS_ERROR(EIO); |
| 2761 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2762 | ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE || |
| 2763 | iclog->ic_state == XLOG_STATE_WANT_SYNC); |
| 2764 | |
David Chinner | 155cc6b | 2008-03-06 13:44:14 +1100 | [diff] [blame] | 2765 | if (iclog->ic_state == XLOG_STATE_WANT_SYNC) { |
David Chinner | b589334 | 2008-03-06 13:44:06 +1100 | [diff] [blame] | 2766 | /* update tail before writing to iclog */ |
Dave Chinner | 1c3cb9e | 2010-12-21 12:28:39 +1100 | [diff] [blame] | 2767 | xfs_lsn_t tail_lsn = xlog_assign_tail_lsn(log->l_mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2768 | sync++; |
| 2769 | iclog->ic_state = XLOG_STATE_SYNCING; |
Dave Chinner | 1c3cb9e | 2010-12-21 12:28:39 +1100 | [diff] [blame] | 2770 | iclog->ic_header.h_tail_lsn = cpu_to_be64(tail_lsn); |
| 2771 | xlog_verify_tail_lsn(log, iclog, tail_lsn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2772 | /* cycle incremented when incrementing curr_block */ |
| 2773 | } |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 2774 | spin_unlock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | |
| 2776 | /* |
| 2777 | * We let the log lock go, so it's possible that we hit a log I/O |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 2778 | * error or some other SHUTDOWN condition that marks the iclog |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2779 | * as XLOG_STATE_IOERROR before the bwrite. However, we know that |
| 2780 | * this iclog has consistent data, so we ignore IOERROR |
| 2781 | * flags after this point. |
| 2782 | */ |
David Chinner | b589334 | 2008-03-06 13:44:06 +1100 | [diff] [blame] | 2783 | if (sync) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | return xlog_sync(log, iclog); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 2785 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2786 | } /* xlog_state_release_iclog */ |
| 2787 | |
| 2788 | |
| 2789 | /* |
| 2790 | * This routine will mark the current iclog in the ring as WANT_SYNC |
| 2791 | * and move the current iclog pointer to the next iclog in the ring. |
| 2792 | * When this routine is called from xlog_state_get_iclog_space(), the |
| 2793 | * exact size of the iclog has not yet been determined. All we know is |
| 2794 | * that every data block. We have run out of space in this log record. |
| 2795 | */ |
| 2796 | STATIC void |
| 2797 | xlog_state_switch_iclogs(xlog_t *log, |
| 2798 | xlog_in_core_t *iclog, |
| 2799 | int eventual_size) |
| 2800 | { |
| 2801 | ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE); |
| 2802 | if (!eventual_size) |
| 2803 | eventual_size = iclog->ic_offset; |
| 2804 | iclog->ic_state = XLOG_STATE_WANT_SYNC; |
Christoph Hellwig | b53e675 | 2007-10-12 10:59:34 +1000 | [diff] [blame] | 2805 | iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2806 | log->l_prev_block = log->l_curr_block; |
| 2807 | log->l_prev_cycle = log->l_curr_cycle; |
| 2808 | |
| 2809 | /* roll log?: ic_offset changed later */ |
| 2810 | log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize); |
| 2811 | |
| 2812 | /* Round up to next log-sunit */ |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 2813 | if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2814 | log->l_mp->m_sb.sb_logsunit > 1) { |
| 2815 | __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit); |
| 2816 | log->l_curr_block = roundup(log->l_curr_block, sunit_bb); |
| 2817 | } |
| 2818 | |
| 2819 | if (log->l_curr_block >= log->l_logBBsize) { |
| 2820 | log->l_curr_cycle++; |
| 2821 | if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM) |
| 2822 | log->l_curr_cycle++; |
| 2823 | log->l_curr_block -= log->l_logBBsize; |
| 2824 | ASSERT(log->l_curr_block >= 0); |
| 2825 | } |
| 2826 | ASSERT(iclog == log->l_iclog); |
| 2827 | log->l_iclog = iclog->ic_next; |
| 2828 | } /* xlog_state_switch_iclogs */ |
| 2829 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2830 | /* |
| 2831 | * Write out all data in the in-core log as of this exact moment in time. |
| 2832 | * |
| 2833 | * Data may be written to the in-core log during this call. However, |
| 2834 | * we don't guarantee this data will be written out. A change from past |
| 2835 | * implementation means this routine will *not* write out zero length LRs. |
| 2836 | * |
| 2837 | * Basically, we try and perform an intelligent scan of the in-core logs. |
| 2838 | * If we determine there is no flushable data, we just return. There is no |
| 2839 | * flushable data if: |
| 2840 | * |
| 2841 | * 1. the current iclog is active and has no data; the previous iclog |
| 2842 | * is in the active or dirty state. |
| 2843 | * 2. the current iclog is drity, and the previous iclog is in the |
| 2844 | * active or dirty state. |
| 2845 | * |
David Chinner | 12017fa | 2008-08-13 16:34:31 +1000 | [diff] [blame] | 2846 | * We may sleep if: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2847 | * |
| 2848 | * 1. the current iclog is not in the active nor dirty state. |
| 2849 | * 2. the current iclog dirty, and the previous iclog is not in the |
| 2850 | * active nor dirty state. |
| 2851 | * 3. the current iclog is active, and there is another thread writing |
| 2852 | * to this particular iclog. |
| 2853 | * 4. a) the current iclog is active and has no other writers |
| 2854 | * b) when we return from flushing out this iclog, it is still |
| 2855 | * not in the active nor dirty state. |
| 2856 | */ |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 2857 | int |
| 2858 | _xfs_log_force( |
| 2859 | struct xfs_mount *mp, |
| 2860 | uint flags, |
| 2861 | int *log_flushed) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2862 | { |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 2863 | struct log *log = mp->m_log; |
| 2864 | struct xlog_in_core *iclog; |
| 2865 | xfs_lsn_t lsn; |
| 2866 | |
| 2867 | XFS_STATS_INC(xs_log_force); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2868 | |
Christoph Hellwig | 93b8a58 | 2011-12-06 21:58:07 +0000 | [diff] [blame] | 2869 | xlog_cil_force(log); |
Dave Chinner | 71e330b | 2010-05-21 14:37:18 +1000 | [diff] [blame] | 2870 | |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 2871 | spin_lock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2872 | |
| 2873 | iclog = log->l_iclog; |
| 2874 | if (iclog->ic_state & XLOG_STATE_IOERROR) { |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 2875 | spin_unlock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2876 | return XFS_ERROR(EIO); |
| 2877 | } |
| 2878 | |
| 2879 | /* If the head iclog is not active nor dirty, we just attach |
| 2880 | * ourselves to the head and go to sleep. |
| 2881 | */ |
| 2882 | if (iclog->ic_state == XLOG_STATE_ACTIVE || |
| 2883 | iclog->ic_state == XLOG_STATE_DIRTY) { |
| 2884 | /* |
| 2885 | * If the head is dirty or (active and empty), then |
| 2886 | * we need to look at the previous iclog. If the previous |
| 2887 | * iclog is active or dirty we are done. There is nothing |
| 2888 | * to sync out. Otherwise, we attach ourselves to the |
| 2889 | * previous iclog and go to sleep. |
| 2890 | */ |
| 2891 | if (iclog->ic_state == XLOG_STATE_DIRTY || |
David Chinner | 155cc6b | 2008-03-06 13:44:14 +1100 | [diff] [blame] | 2892 | (atomic_read(&iclog->ic_refcnt) == 0 |
| 2893 | && iclog->ic_offset == 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2894 | iclog = iclog->ic_prev; |
| 2895 | if (iclog->ic_state == XLOG_STATE_ACTIVE || |
| 2896 | iclog->ic_state == XLOG_STATE_DIRTY) |
| 2897 | goto no_sleep; |
| 2898 | else |
| 2899 | goto maybe_sleep; |
| 2900 | } else { |
David Chinner | 155cc6b | 2008-03-06 13:44:14 +1100 | [diff] [blame] | 2901 | if (atomic_read(&iclog->ic_refcnt) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2902 | /* We are the only one with access to this |
| 2903 | * iclog. Flush it out now. There should |
| 2904 | * be a roundoff of zero to show that someone |
| 2905 | * has already taken care of the roundoff from |
| 2906 | * the previous sync. |
| 2907 | */ |
David Chinner | 155cc6b | 2008-03-06 13:44:14 +1100 | [diff] [blame] | 2908 | atomic_inc(&iclog->ic_refcnt); |
Christoph Hellwig | b53e675 | 2007-10-12 10:59:34 +1000 | [diff] [blame] | 2909 | lsn = be64_to_cpu(iclog->ic_header.h_lsn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2910 | xlog_state_switch_iclogs(log, iclog, 0); |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 2911 | spin_unlock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2912 | |
| 2913 | if (xlog_state_release_iclog(log, iclog)) |
| 2914 | return XFS_ERROR(EIO); |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 2915 | |
| 2916 | if (log_flushed) |
| 2917 | *log_flushed = 1; |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 2918 | spin_lock(&log->l_icloglock); |
Christoph Hellwig | b53e675 | 2007-10-12 10:59:34 +1000 | [diff] [blame] | 2919 | if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2920 | iclog->ic_state != XLOG_STATE_DIRTY) |
| 2921 | goto maybe_sleep; |
| 2922 | else |
| 2923 | goto no_sleep; |
| 2924 | } else { |
| 2925 | /* Someone else is writing to this iclog. |
| 2926 | * Use its call to flush out the data. However, |
| 2927 | * the other thread may not force out this LR, |
| 2928 | * so we mark it WANT_SYNC. |
| 2929 | */ |
| 2930 | xlog_state_switch_iclogs(log, iclog, 0); |
| 2931 | goto maybe_sleep; |
| 2932 | } |
| 2933 | } |
| 2934 | } |
| 2935 | |
| 2936 | /* By the time we come around again, the iclog could've been filled |
| 2937 | * which would give it another lsn. If we have a new lsn, just |
| 2938 | * return because the relevant data has been flushed. |
| 2939 | */ |
| 2940 | maybe_sleep: |
| 2941 | if (flags & XFS_LOG_SYNC) { |
| 2942 | /* |
| 2943 | * We must check if we're shutting down here, before |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 2944 | * we wait, while we're holding the l_icloglock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2945 | * Then we check again after waking up, in case our |
| 2946 | * sleep was disturbed by a bad news. |
| 2947 | */ |
| 2948 | if (iclog->ic_state & XLOG_STATE_IOERROR) { |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 2949 | spin_unlock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2950 | return XFS_ERROR(EIO); |
| 2951 | } |
| 2952 | XFS_STATS_INC(xs_log_force_sleep); |
Dave Chinner | eb40a87 | 2010-12-21 12:09:01 +1100 | [diff] [blame] | 2953 | xlog_wait(&iclog->ic_force_wait, &log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2954 | /* |
| 2955 | * No need to grab the log lock here since we're |
| 2956 | * only deciding whether or not to return EIO |
| 2957 | * and the memory read should be atomic. |
| 2958 | */ |
| 2959 | if (iclog->ic_state & XLOG_STATE_IOERROR) |
| 2960 | return XFS_ERROR(EIO); |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 2961 | if (log_flushed) |
| 2962 | *log_flushed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2963 | } else { |
| 2964 | |
| 2965 | no_sleep: |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 2966 | spin_unlock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2967 | } |
| 2968 | return 0; |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 2969 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2970 | |
| 2971 | /* |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 2972 | * Wrapper for _xfs_log_force(), to be used when caller doesn't care |
| 2973 | * about errors or whether the log was flushed or not. This is the normal |
| 2974 | * interface to use when trying to unpin items or move the log forward. |
| 2975 | */ |
| 2976 | void |
| 2977 | xfs_log_force( |
| 2978 | xfs_mount_t *mp, |
| 2979 | uint flags) |
| 2980 | { |
| 2981 | int error; |
| 2982 | |
| 2983 | error = _xfs_log_force(mp, flags, NULL); |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 2984 | if (error) |
| 2985 | xfs_warn(mp, "%s: error %d returned.", __func__, error); |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 2986 | } |
| 2987 | |
| 2988 | /* |
| 2989 | * Force the in-core log to disk for a specific LSN. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2990 | * |
| 2991 | * Find in-core log with lsn. |
| 2992 | * If it is in the DIRTY state, just return. |
| 2993 | * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC |
| 2994 | * state and go to sleep or return. |
| 2995 | * If it is in any other state, go to sleep or return. |
| 2996 | * |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 2997 | * Synchronous forces are implemented with a signal variable. All callers |
| 2998 | * to force a given lsn to disk will wait on a the sv attached to the |
| 2999 | * specific in-core log. When given in-core log finally completes its |
| 3000 | * write to disk, that thread will wake up all threads waiting on the |
| 3001 | * sv. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3002 | */ |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 3003 | int |
| 3004 | _xfs_log_force_lsn( |
| 3005 | struct xfs_mount *mp, |
| 3006 | xfs_lsn_t lsn, |
| 3007 | uint flags, |
| 3008 | int *log_flushed) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3009 | { |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 3010 | struct log *log = mp->m_log; |
| 3011 | struct xlog_in_core *iclog; |
| 3012 | int already_slept = 0; |
| 3013 | |
| 3014 | ASSERT(lsn != 0); |
| 3015 | |
| 3016 | XFS_STATS_INC(xs_log_force); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3017 | |
Christoph Hellwig | 93b8a58 | 2011-12-06 21:58:07 +0000 | [diff] [blame] | 3018 | lsn = xlog_cil_force_lsn(log, lsn); |
| 3019 | if (lsn == NULLCOMMITLSN) |
| 3020 | return 0; |
Dave Chinner | 71e330b | 2010-05-21 14:37:18 +1000 | [diff] [blame] | 3021 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3022 | try_again: |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 3023 | spin_lock(&log->l_icloglock); |
| 3024 | iclog = log->l_iclog; |
| 3025 | if (iclog->ic_state & XLOG_STATE_IOERROR) { |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 3026 | spin_unlock(&log->l_icloglock); |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 3027 | return XFS_ERROR(EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3028 | } |
| 3029 | |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 3030 | do { |
| 3031 | if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) { |
| 3032 | iclog = iclog->ic_next; |
| 3033 | continue; |
| 3034 | } |
| 3035 | |
| 3036 | if (iclog->ic_state == XLOG_STATE_DIRTY) { |
| 3037 | spin_unlock(&log->l_icloglock); |
| 3038 | return 0; |
| 3039 | } |
| 3040 | |
| 3041 | if (iclog->ic_state == XLOG_STATE_ACTIVE) { |
| 3042 | /* |
| 3043 | * We sleep here if we haven't already slept (e.g. |
| 3044 | * this is the first time we've looked at the correct |
| 3045 | * iclog buf) and the buffer before us is going to |
| 3046 | * be sync'ed. The reason for this is that if we |
| 3047 | * are doing sync transactions here, by waiting for |
| 3048 | * the previous I/O to complete, we can allow a few |
| 3049 | * more transactions into this iclog before we close |
| 3050 | * it down. |
| 3051 | * |
| 3052 | * Otherwise, we mark the buffer WANT_SYNC, and bump |
| 3053 | * up the refcnt so we can release the log (which |
| 3054 | * drops the ref count). The state switch keeps new |
| 3055 | * transaction commits from using this buffer. When |
| 3056 | * the current commits finish writing into the buffer, |
| 3057 | * the refcount will drop to zero and the buffer will |
| 3058 | * go out then. |
| 3059 | */ |
| 3060 | if (!already_slept && |
| 3061 | (iclog->ic_prev->ic_state & |
| 3062 | (XLOG_STATE_WANT_SYNC | XLOG_STATE_SYNCING))) { |
| 3063 | ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR)); |
| 3064 | |
| 3065 | XFS_STATS_INC(xs_log_force_sleep); |
| 3066 | |
Dave Chinner | eb40a87 | 2010-12-21 12:09:01 +1100 | [diff] [blame] | 3067 | xlog_wait(&iclog->ic_prev->ic_write_wait, |
| 3068 | &log->l_icloglock); |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 3069 | if (log_flushed) |
| 3070 | *log_flushed = 1; |
| 3071 | already_slept = 1; |
| 3072 | goto try_again; |
| 3073 | } |
David Chinner | 155cc6b | 2008-03-06 13:44:14 +1100 | [diff] [blame] | 3074 | atomic_inc(&iclog->ic_refcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3075 | xlog_state_switch_iclogs(log, iclog, 0); |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 3076 | spin_unlock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3077 | if (xlog_state_release_iclog(log, iclog)) |
| 3078 | return XFS_ERROR(EIO); |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 3079 | if (log_flushed) |
| 3080 | *log_flushed = 1; |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 3081 | spin_lock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3082 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3083 | |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 3084 | if ((flags & XFS_LOG_SYNC) && /* sleep */ |
| 3085 | !(iclog->ic_state & |
| 3086 | (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) { |
| 3087 | /* |
| 3088 | * Don't wait on completion if we know that we've |
| 3089 | * gotten a log write error. |
| 3090 | */ |
| 3091 | if (iclog->ic_state & XLOG_STATE_IOERROR) { |
| 3092 | spin_unlock(&log->l_icloglock); |
| 3093 | return XFS_ERROR(EIO); |
| 3094 | } |
| 3095 | XFS_STATS_INC(xs_log_force_sleep); |
Dave Chinner | eb40a87 | 2010-12-21 12:09:01 +1100 | [diff] [blame] | 3096 | xlog_wait(&iclog->ic_force_wait, &log->l_icloglock); |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 3097 | /* |
| 3098 | * No need to grab the log lock here since we're |
| 3099 | * only deciding whether or not to return EIO |
| 3100 | * and the memory read should be atomic. |
| 3101 | */ |
| 3102 | if (iclog->ic_state & XLOG_STATE_IOERROR) |
| 3103 | return XFS_ERROR(EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3104 | |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 3105 | if (log_flushed) |
| 3106 | *log_flushed = 1; |
| 3107 | } else { /* just return */ |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 3108 | spin_unlock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3109 | } |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 3110 | |
| 3111 | return 0; |
| 3112 | } while (iclog != log->l_iclog); |
| 3113 | |
| 3114 | spin_unlock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3115 | return 0; |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 3116 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3117 | |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 3118 | /* |
| 3119 | * Wrapper for _xfs_log_force_lsn(), to be used when caller doesn't care |
| 3120 | * about errors or whether the log was flushed or not. This is the normal |
| 3121 | * interface to use when trying to unpin items or move the log forward. |
| 3122 | */ |
| 3123 | void |
| 3124 | xfs_log_force_lsn( |
| 3125 | xfs_mount_t *mp, |
| 3126 | xfs_lsn_t lsn, |
| 3127 | uint flags) |
| 3128 | { |
| 3129 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3130 | |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 3131 | error = _xfs_log_force_lsn(mp, lsn, flags, NULL); |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 3132 | if (error) |
| 3133 | xfs_warn(mp, "%s: error %d returned.", __func__, error); |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 3134 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3135 | |
| 3136 | /* |
| 3137 | * Called when we want to mark the current iclog as being ready to sync to |
| 3138 | * disk. |
| 3139 | */ |
David Chinner | a8272ce | 2007-11-23 16:28:09 +1100 | [diff] [blame] | 3140 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3141 | xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog) |
| 3142 | { |
Christoph Hellwig | a8914f3 | 2009-08-10 11:32:44 -0300 | [diff] [blame] | 3143 | assert_spin_locked(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3144 | |
| 3145 | if (iclog->ic_state == XLOG_STATE_ACTIVE) { |
| 3146 | xlog_state_switch_iclogs(log, iclog, 0); |
| 3147 | } else { |
| 3148 | ASSERT(iclog->ic_state & |
| 3149 | (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR)); |
| 3150 | } |
Christoph Hellwig | 39e2def | 2008-12-03 12:20:28 +0100 | [diff] [blame] | 3151 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3152 | |
| 3153 | |
| 3154 | /***************************************************************************** |
| 3155 | * |
| 3156 | * TICKET functions |
| 3157 | * |
| 3158 | ***************************************************************************** |
| 3159 | */ |
| 3160 | |
| 3161 | /* |
Malcolm Parsons | 9da096f | 2009-03-29 09:55:42 +0200 | [diff] [blame] | 3162 | * Free a used ticket when its refcount falls to zero. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3163 | */ |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 3164 | void |
| 3165 | xfs_log_ticket_put( |
| 3166 | xlog_ticket_t *ticket) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3167 | { |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 3168 | ASSERT(atomic_read(&ticket->t_ref) > 0); |
Dave Chinner | eb40a87 | 2010-12-21 12:09:01 +1100 | [diff] [blame] | 3169 | if (atomic_dec_and_test(&ticket->t_ref)) |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 3170 | kmem_zone_free(xfs_log_ticket_zone, ticket); |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 3171 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3172 | |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 3173 | xlog_ticket_t * |
| 3174 | xfs_log_ticket_get( |
| 3175 | xlog_ticket_t *ticket) |
| 3176 | { |
| 3177 | ASSERT(atomic_read(&ticket->t_ref) > 0); |
| 3178 | atomic_inc(&ticket->t_ref); |
| 3179 | return ticket; |
| 3180 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3181 | |
| 3182 | /* |
David Chinner | eb01c9c | 2008-04-10 12:18:46 +1000 | [diff] [blame] | 3183 | * Allocate and initialise a new log ticket. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3184 | */ |
Dave Chinner | 71e330b | 2010-05-21 14:37:18 +1000 | [diff] [blame] | 3185 | xlog_ticket_t * |
Dave Chinner | 9b9fc2b7 | 2010-03-23 11:21:11 +1100 | [diff] [blame] | 3186 | xlog_ticket_alloc( |
| 3187 | struct log *log, |
| 3188 | int unit_bytes, |
| 3189 | int cnt, |
| 3190 | char client, |
Dave Chinner | 3383ca5 | 2010-05-07 11:04:17 +1000 | [diff] [blame] | 3191 | uint xflags, |
| 3192 | int alloc_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3193 | { |
Dave Chinner | 9b9fc2b7 | 2010-03-23 11:21:11 +1100 | [diff] [blame] | 3194 | struct xlog_ticket *tic; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3195 | uint num_headers; |
Dave Chinner | 9b9fc2b7 | 2010-03-23 11:21:11 +1100 | [diff] [blame] | 3196 | int iclog_space; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3197 | |
Dave Chinner | 3383ca5 | 2010-05-07 11:04:17 +1000 | [diff] [blame] | 3198 | tic = kmem_zone_zalloc(xfs_log_ticket_zone, alloc_flags); |
David Chinner | eb01c9c | 2008-04-10 12:18:46 +1000 | [diff] [blame] | 3199 | if (!tic) |
| 3200 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3201 | |
| 3202 | /* |
| 3203 | * Permanent reservations have up to 'cnt'-1 active log operations |
| 3204 | * in the log. A unit in this case is the amount of space for one |
| 3205 | * of these log operations. Normal reservations have a cnt of 1 |
| 3206 | * and their unit amount is the total amount of space required. |
| 3207 | * |
| 3208 | * The following lines of code account for non-transaction data |
Tim Shimmin | 32fb9b5 | 2005-09-02 16:41:43 +1000 | [diff] [blame] | 3209 | * which occupy space in the on-disk log. |
| 3210 | * |
| 3211 | * Normal form of a transaction is: |
| 3212 | * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph> |
| 3213 | * and then there are LR hdrs, split-recs and roundoff at end of syncs. |
| 3214 | * |
| 3215 | * We need to account for all the leadup data and trailer data |
| 3216 | * around the transaction data. |
| 3217 | * And then we need to account for the worst case in terms of using |
| 3218 | * more space. |
| 3219 | * The worst case will happen if: |
| 3220 | * - the placement of the transaction happens to be such that the |
| 3221 | * roundoff is at its maximum |
| 3222 | * - the transaction data is synced before the commit record is synced |
| 3223 | * i.e. <transaction-data><roundoff> | <commit-rec><roundoff> |
| 3224 | * Therefore the commit record is in its own Log Record. |
| 3225 | * This can happen as the commit record is called with its |
| 3226 | * own region to xlog_write(). |
| 3227 | * This then means that in the worst case, roundoff can happen for |
| 3228 | * the commit-rec as well. |
| 3229 | * The commit-rec is smaller than padding in this scenario and so it is |
| 3230 | * not added separately. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3231 | */ |
| 3232 | |
Tim Shimmin | 32fb9b5 | 2005-09-02 16:41:43 +1000 | [diff] [blame] | 3233 | /* for trans header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3234 | unit_bytes += sizeof(xlog_op_header_t); |
Tim Shimmin | 32fb9b5 | 2005-09-02 16:41:43 +1000 | [diff] [blame] | 3235 | unit_bytes += sizeof(xfs_trans_header_t); |
| 3236 | |
| 3237 | /* for start-rec */ |
| 3238 | unit_bytes += sizeof(xlog_op_header_t); |
| 3239 | |
Dave Chinner | 9b9fc2b7 | 2010-03-23 11:21:11 +1100 | [diff] [blame] | 3240 | /* |
| 3241 | * for LR headers - the space for data in an iclog is the size minus |
| 3242 | * the space used for the headers. If we use the iclog size, then we |
| 3243 | * undercalculate the number of headers required. |
| 3244 | * |
| 3245 | * Furthermore - the addition of op headers for split-recs might |
| 3246 | * increase the space required enough to require more log and op |
| 3247 | * headers, so take that into account too. |
| 3248 | * |
| 3249 | * IMPORTANT: This reservation makes the assumption that if this |
| 3250 | * transaction is the first in an iclog and hence has the LR headers |
| 3251 | * accounted to it, then the remaining space in the iclog is |
| 3252 | * exclusively for this transaction. i.e. if the transaction is larger |
| 3253 | * than the iclog, it will be the only thing in that iclog. |
| 3254 | * Fundamentally, this means we must pass the entire log vector to |
| 3255 | * xlog_write to guarantee this. |
| 3256 | */ |
| 3257 | iclog_space = log->l_iclog_size - log->l_iclog_hsize; |
| 3258 | num_headers = howmany(unit_bytes, iclog_space); |
| 3259 | |
| 3260 | /* for split-recs - ophdrs added when data split over LRs */ |
| 3261 | unit_bytes += sizeof(xlog_op_header_t) * num_headers; |
| 3262 | |
| 3263 | /* add extra header reservations if we overrun */ |
| 3264 | while (!num_headers || |
| 3265 | howmany(unit_bytes, iclog_space) > num_headers) { |
| 3266 | unit_bytes += sizeof(xlog_op_header_t); |
| 3267 | num_headers++; |
| 3268 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3269 | unit_bytes += log->l_iclog_hsize * num_headers; |
| 3270 | |
Tim Shimmin | 32fb9b5 | 2005-09-02 16:41:43 +1000 | [diff] [blame] | 3271 | /* for commit-rec LR header - note: padding will subsume the ophdr */ |
| 3272 | unit_bytes += log->l_iclog_hsize; |
| 3273 | |
Tim Shimmin | 32fb9b5 | 2005-09-02 16:41:43 +1000 | [diff] [blame] | 3274 | /* for roundoff padding for transaction data and one for commit record */ |
Eric Sandeen | 6211870 | 2008-03-06 13:44:28 +1100 | [diff] [blame] | 3275 | if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) && |
Tim Shimmin | 32fb9b5 | 2005-09-02 16:41:43 +1000 | [diff] [blame] | 3276 | log->l_mp->m_sb.sb_logsunit > 1) { |
| 3277 | /* log su roundoff */ |
| 3278 | unit_bytes += 2*log->l_mp->m_sb.sb_logsunit; |
| 3279 | } else { |
| 3280 | /* BB roundoff */ |
| 3281 | unit_bytes += 2*BBSIZE; |
| 3282 | } |
| 3283 | |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 3284 | atomic_set(&tic->t_ref, 1); |
Christoph Hellwig | 14a7235f | 2012-02-20 02:31:24 +0000 | [diff] [blame^] | 3285 | tic->t_task = current; |
Dave Chinner | 1054794 | 2010-12-21 12:02:25 +1100 | [diff] [blame] | 3286 | INIT_LIST_HEAD(&tic->t_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3287 | tic->t_unit_res = unit_bytes; |
| 3288 | tic->t_curr_res = unit_bytes; |
| 3289 | tic->t_cnt = cnt; |
| 3290 | tic->t_ocnt = cnt; |
Dave Chinner | f983710 | 2010-04-14 15:47:55 +1000 | [diff] [blame] | 3291 | tic->t_tid = random32(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3292 | tic->t_clientid = client; |
| 3293 | tic->t_flags = XLOG_TIC_INITED; |
Tim Shimmin | 7e9c639 | 2005-09-02 16:42:05 +1000 | [diff] [blame] | 3294 | tic->t_trans_type = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3295 | if (xflags & XFS_LOG_PERM_RESERV) |
| 3296 | tic->t_flags |= XLOG_TIC_PERM_RESERV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3297 | |
Christoph Hellwig | 0adba53 | 2007-08-30 17:21:46 +1000 | [diff] [blame] | 3298 | xlog_tic_reset_res(tic); |
Tim Shimmin | 7e9c639 | 2005-09-02 16:42:05 +1000 | [diff] [blame] | 3299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3300 | return tic; |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 3301 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3302 | |
| 3303 | |
| 3304 | /****************************************************************************** |
| 3305 | * |
| 3306 | * Log debug routines |
| 3307 | * |
| 3308 | ****************************************************************************** |
| 3309 | */ |
Nathan Scott | cfcbbbd | 2005-11-02 15:12:04 +1100 | [diff] [blame] | 3310 | #if defined(DEBUG) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3311 | /* |
| 3312 | * Make sure that the destination ptr is within the valid data region of |
| 3313 | * one of the iclogs. This uses backup pointers stored in a different |
| 3314 | * part of the log in case we trash the log structure. |
| 3315 | */ |
| 3316 | void |
Christoph Hellwig | e6b1f27 | 2010-03-23 11:47:38 +1100 | [diff] [blame] | 3317 | xlog_verify_dest_ptr( |
| 3318 | struct log *log, |
| 3319 | char *ptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3320 | { |
| 3321 | int i; |
| 3322 | int good_ptr = 0; |
| 3323 | |
Christoph Hellwig | e6b1f27 | 2010-03-23 11:47:38 +1100 | [diff] [blame] | 3324 | for (i = 0; i < log->l_iclog_bufs; i++) { |
| 3325 | if (ptr >= log->l_iclog_bak[i] && |
| 3326 | ptr <= log->l_iclog_bak[i] + log->l_iclog_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3327 | good_ptr++; |
| 3328 | } |
Christoph Hellwig | e6b1f27 | 2010-03-23 11:47:38 +1100 | [diff] [blame] | 3329 | |
| 3330 | if (!good_ptr) |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 3331 | xfs_emerg(log->l_mp, "%s: invalid ptr", __func__); |
Christoph Hellwig | e6b1f27 | 2010-03-23 11:47:38 +1100 | [diff] [blame] | 3332 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3333 | |
Dave Chinner | da8a1a4 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 3334 | /* |
| 3335 | * Check to make sure the grant write head didn't just over lap the tail. If |
| 3336 | * the cycles are the same, we can't be overlapping. Otherwise, make sure that |
| 3337 | * the cycles differ by exactly one and check the byte count. |
| 3338 | * |
| 3339 | * This check is run unlocked, so can give false positives. Rather than assert |
| 3340 | * on failures, use a warn-once flag and a panic tag to allow the admin to |
| 3341 | * determine if they want to panic the machine when such an error occurs. For |
| 3342 | * debug kernels this will have the same effect as using an assert but, unlinke |
| 3343 | * an assert, it can be turned off at runtime. |
| 3344 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3345 | STATIC void |
Dave Chinner | 3f336c6 | 2010-12-21 12:02:52 +1100 | [diff] [blame] | 3346 | xlog_verify_grant_tail( |
| 3347 | struct log *log) |
| 3348 | { |
Dave Chinner | 1c3cb9e | 2010-12-21 12:28:39 +1100 | [diff] [blame] | 3349 | int tail_cycle, tail_blocks; |
Dave Chinner | a69ed03 | 2010-12-21 12:08:20 +1100 | [diff] [blame] | 3350 | int cycle, space; |
Dave Chinner | 3f336c6 | 2010-12-21 12:02:52 +1100 | [diff] [blame] | 3351 | |
Dave Chinner | a69ed03 | 2010-12-21 12:08:20 +1100 | [diff] [blame] | 3352 | xlog_crack_grant_head(&log->l_grant_write_head, &cycle, &space); |
Dave Chinner | 1c3cb9e | 2010-12-21 12:28:39 +1100 | [diff] [blame] | 3353 | xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_blocks); |
| 3354 | if (tail_cycle != cycle) { |
Dave Chinner | da8a1a4 | 2011-04-08 12:45:07 +1000 | [diff] [blame] | 3355 | if (cycle - 1 != tail_cycle && |
| 3356 | !(log->l_flags & XLOG_TAIL_WARN)) { |
| 3357 | xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES, |
| 3358 | "%s: cycle - 1 != tail_cycle", __func__); |
| 3359 | log->l_flags |= XLOG_TAIL_WARN; |
| 3360 | } |
| 3361 | |
| 3362 | if (space > BBTOB(tail_blocks) && |
| 3363 | !(log->l_flags & XLOG_TAIL_WARN)) { |
| 3364 | xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES, |
| 3365 | "%s: space > BBTOB(tail_blocks)", __func__); |
| 3366 | log->l_flags |= XLOG_TAIL_WARN; |
| 3367 | } |
Dave Chinner | 3f336c6 | 2010-12-21 12:02:52 +1100 | [diff] [blame] | 3368 | } |
| 3369 | } |
| 3370 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3371 | /* check if it will fit */ |
| 3372 | STATIC void |
| 3373 | xlog_verify_tail_lsn(xlog_t *log, |
| 3374 | xlog_in_core_t *iclog, |
| 3375 | xfs_lsn_t tail_lsn) |
| 3376 | { |
| 3377 | int blocks; |
| 3378 | |
| 3379 | if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) { |
| 3380 | blocks = |
| 3381 | log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn)); |
| 3382 | if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize)) |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 3383 | xfs_emerg(log->l_mp, "%s: ran out of log space", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3384 | } else { |
| 3385 | ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle); |
| 3386 | |
| 3387 | if (BLOCK_LSN(tail_lsn) == log->l_prev_block) |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 3388 | xfs_emerg(log->l_mp, "%s: tail wrapped", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3389 | |
| 3390 | blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block; |
| 3391 | if (blocks < BTOBB(iclog->ic_offset) + 1) |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 3392 | xfs_emerg(log->l_mp, "%s: ran out of log space", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3393 | } |
| 3394 | } /* xlog_verify_tail_lsn */ |
| 3395 | |
| 3396 | /* |
| 3397 | * Perform a number of checks on the iclog before writing to disk. |
| 3398 | * |
| 3399 | * 1. Make sure the iclogs are still circular |
| 3400 | * 2. Make sure we have a good magic number |
| 3401 | * 3. Make sure we don't have magic numbers in the data |
| 3402 | * 4. Check fields of each log operation header for: |
| 3403 | * A. Valid client identifier |
| 3404 | * B. tid ptr value falls in valid ptr space (user space code) |
| 3405 | * C. Length in log record header is correct according to the |
| 3406 | * individual operation headers within record. |
| 3407 | * 5. When a bwrite will occur within 5 blocks of the front of the physical |
| 3408 | * log, check the preceding blocks of the physical log to make sure all |
| 3409 | * the cycle numbers agree with the current cycle number. |
| 3410 | */ |
| 3411 | STATIC void |
| 3412 | xlog_verify_iclog(xlog_t *log, |
| 3413 | xlog_in_core_t *iclog, |
| 3414 | int count, |
| 3415 | boolean_t syncing) |
| 3416 | { |
| 3417 | xlog_op_header_t *ophead; |
| 3418 | xlog_in_core_t *icptr; |
| 3419 | xlog_in_core_2_t *xhdr; |
| 3420 | xfs_caddr_t ptr; |
| 3421 | xfs_caddr_t base_ptr; |
| 3422 | __psint_t field_offset; |
| 3423 | __uint8_t clientid; |
| 3424 | int len, i, j, k, op_len; |
| 3425 | int idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3426 | |
| 3427 | /* check validity of iclog pointers */ |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 3428 | spin_lock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3429 | icptr = log->l_iclog; |
| 3430 | for (i=0; i < log->l_iclog_bufs; i++) { |
Christoph Hellwig | 4b80916 | 2007-08-16 15:37:36 +1000 | [diff] [blame] | 3431 | if (icptr == NULL) |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 3432 | xfs_emerg(log->l_mp, "%s: invalid ptr", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3433 | icptr = icptr->ic_next; |
| 3434 | } |
| 3435 | if (icptr != log->l_iclog) |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 3436 | xfs_emerg(log->l_mp, "%s: corrupt iclog ring", __func__); |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 3437 | spin_unlock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3438 | |
| 3439 | /* check log magic numbers */ |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 3440 | if (iclog->ic_header.h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 3441 | xfs_emerg(log->l_mp, "%s: invalid magic num", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3442 | |
Christoph Hellwig | b53e675 | 2007-10-12 10:59:34 +1000 | [diff] [blame] | 3443 | ptr = (xfs_caddr_t) &iclog->ic_header; |
| 3444 | for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3445 | ptr += BBSIZE) { |
Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 3446 | if (*(__be32 *)ptr == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 3447 | xfs_emerg(log->l_mp, "%s: unexpected magic num", |
| 3448 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3449 | } |
| 3450 | |
| 3451 | /* check fields */ |
Christoph Hellwig | b53e675 | 2007-10-12 10:59:34 +1000 | [diff] [blame] | 3452 | len = be32_to_cpu(iclog->ic_header.h_num_logops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3453 | ptr = iclog->ic_datap; |
| 3454 | base_ptr = ptr; |
| 3455 | ophead = (xlog_op_header_t *)ptr; |
Christoph Hellwig | b28708d | 2008-11-28 14:23:38 +1100 | [diff] [blame] | 3456 | xhdr = iclog->ic_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3457 | for (i = 0; i < len; i++) { |
| 3458 | ophead = (xlog_op_header_t *)ptr; |
| 3459 | |
| 3460 | /* clientid is only 1 byte */ |
| 3461 | field_offset = (__psint_t) |
| 3462 | ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr); |
| 3463 | if (syncing == B_FALSE || (field_offset & 0x1ff)) { |
| 3464 | clientid = ophead->oh_clientid; |
| 3465 | } else { |
| 3466 | idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap); |
| 3467 | if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) { |
| 3468 | j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE); |
| 3469 | k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE); |
Christoph Hellwig | 03bea6f | 2007-10-12 10:58:05 +1000 | [diff] [blame] | 3470 | clientid = xlog_get_client_id( |
| 3471 | xhdr[j].hic_xheader.xh_cycle_data[k]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3472 | } else { |
Christoph Hellwig | 03bea6f | 2007-10-12 10:58:05 +1000 | [diff] [blame] | 3473 | clientid = xlog_get_client_id( |
| 3474 | iclog->ic_header.h_cycle_data[idx]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3475 | } |
| 3476 | } |
| 3477 | if (clientid != XFS_TRANSACTION && clientid != XFS_LOG) |
Dave Chinner | a0fa2b6 | 2011-03-07 10:01:35 +1100 | [diff] [blame] | 3478 | xfs_warn(log->l_mp, |
| 3479 | "%s: invalid clientid %d op 0x%p offset 0x%lx", |
| 3480 | __func__, clientid, ophead, |
| 3481 | (unsigned long)field_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3482 | |
| 3483 | /* check length */ |
| 3484 | field_offset = (__psint_t) |
| 3485 | ((xfs_caddr_t)&(ophead->oh_len) - base_ptr); |
| 3486 | if (syncing == B_FALSE || (field_offset & 0x1ff)) { |
Christoph Hellwig | 67fcb7b | 2007-10-12 10:58:59 +1000 | [diff] [blame] | 3487 | op_len = be32_to_cpu(ophead->oh_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3488 | } else { |
| 3489 | idx = BTOBBT((__psint_t)&ophead->oh_len - |
| 3490 | (__psint_t)iclog->ic_datap); |
| 3491 | if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) { |
| 3492 | j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE); |
| 3493 | k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE); |
Christoph Hellwig | b53e675 | 2007-10-12 10:59:34 +1000 | [diff] [blame] | 3494 | op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3495 | } else { |
Christoph Hellwig | b53e675 | 2007-10-12 10:59:34 +1000 | [diff] [blame] | 3496 | op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3497 | } |
| 3498 | } |
| 3499 | ptr += sizeof(xlog_op_header_t) + op_len; |
| 3500 | } |
| 3501 | } /* xlog_verify_iclog */ |
Nathan Scott | cfcbbbd | 2005-11-02 15:12:04 +1100 | [diff] [blame] | 3502 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3503 | |
| 3504 | /* |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 3505 | * Mark all iclogs IOERROR. l_icloglock is held by the caller. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3506 | */ |
| 3507 | STATIC int |
| 3508 | xlog_state_ioerror( |
| 3509 | xlog_t *log) |
| 3510 | { |
| 3511 | xlog_in_core_t *iclog, *ic; |
| 3512 | |
| 3513 | iclog = log->l_iclog; |
| 3514 | if (! (iclog->ic_state & XLOG_STATE_IOERROR)) { |
| 3515 | /* |
| 3516 | * Mark all the incore logs IOERROR. |
| 3517 | * From now on, no log flushes will result. |
| 3518 | */ |
| 3519 | ic = iclog; |
| 3520 | do { |
| 3521 | ic->ic_state = XLOG_STATE_IOERROR; |
| 3522 | ic = ic->ic_next; |
| 3523 | } while (ic != iclog); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 3524 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3525 | } |
| 3526 | /* |
| 3527 | * Return non-zero, if state transition has already happened. |
| 3528 | */ |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 3529 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3530 | } |
| 3531 | |
| 3532 | /* |
| 3533 | * This is called from xfs_force_shutdown, when we're forcibly |
| 3534 | * shutting down the filesystem, typically because of an IO error. |
| 3535 | * Our main objectives here are to make sure that: |
| 3536 | * a. the filesystem gets marked 'SHUTDOWN' for all interested |
| 3537 | * parties to find out, 'atomically'. |
| 3538 | * b. those who're sleeping on log reservations, pinned objects and |
| 3539 | * other resources get woken up, and be told the bad news. |
| 3540 | * c. nothing new gets queued up after (a) and (b) are done. |
| 3541 | * d. if !logerror, flush the iclogs to disk, then seal them off |
| 3542 | * for business. |
Dave Chinner | 9da1ab1 | 2010-05-17 15:51:59 +1000 | [diff] [blame] | 3543 | * |
| 3544 | * Note: for delayed logging the !logerror case needs to flush the regions |
| 3545 | * held in memory out to the iclogs before flushing them to disk. This needs |
| 3546 | * to be done before the log is marked as shutdown, otherwise the flush to the |
| 3547 | * iclogs will fail. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3548 | */ |
| 3549 | int |
| 3550 | xfs_log_force_umount( |
| 3551 | struct xfs_mount *mp, |
| 3552 | int logerror) |
| 3553 | { |
| 3554 | xlog_ticket_t *tic; |
| 3555 | xlog_t *log; |
| 3556 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3557 | |
| 3558 | log = mp->m_log; |
| 3559 | |
| 3560 | /* |
| 3561 | * If this happens during log recovery, don't worry about |
| 3562 | * locking; the log isn't open for business yet. |
| 3563 | */ |
| 3564 | if (!log || |
| 3565 | log->l_flags & XLOG_ACTIVE_RECOVERY) { |
| 3566 | mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN; |
Christoph Hellwig | bac8dca | 2008-11-28 14:23:31 +1100 | [diff] [blame] | 3567 | if (mp->m_sb_bp) |
| 3568 | XFS_BUF_DONE(mp->m_sb_bp); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 3569 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3570 | } |
| 3571 | |
| 3572 | /* |
| 3573 | * Somebody could've already done the hard work for us. |
| 3574 | * No need to get locks for this. |
| 3575 | */ |
| 3576 | if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) { |
| 3577 | ASSERT(XLOG_FORCED_SHUTDOWN(log)); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 3578 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3579 | } |
| 3580 | retval = 0; |
Dave Chinner | 9da1ab1 | 2010-05-17 15:51:59 +1000 | [diff] [blame] | 3581 | |
| 3582 | /* |
| 3583 | * Flush the in memory commit item list before marking the log as |
| 3584 | * being shut down. We need to do it in this order to ensure all the |
| 3585 | * completed transactions are flushed to disk with the xfs_log_force() |
| 3586 | * call below. |
| 3587 | */ |
Christoph Hellwig | 93b8a58 | 2011-12-06 21:58:07 +0000 | [diff] [blame] | 3588 | if (!logerror) |
Dave Chinner | a44f13e | 2010-08-24 11:40:03 +1000 | [diff] [blame] | 3589 | xlog_cil_force(log); |
Dave Chinner | 9da1ab1 | 2010-05-17 15:51:59 +1000 | [diff] [blame] | 3590 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3591 | /* |
Dave Chinner | 3f16b98 | 2010-12-21 12:29:01 +1100 | [diff] [blame] | 3592 | * mark the filesystem and the as in a shutdown state and wake |
| 3593 | * everybody up to tell them the bad news. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3594 | */ |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 3595 | spin_lock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3596 | mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN; |
Christoph Hellwig | bac8dca | 2008-11-28 14:23:31 +1100 | [diff] [blame] | 3597 | if (mp->m_sb_bp) |
| 3598 | XFS_BUF_DONE(mp->m_sb_bp); |
| 3599 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3600 | /* |
| 3601 | * This flag is sort of redundant because of the mount flag, but |
| 3602 | * it's good to maintain the separation between the log and the rest |
| 3603 | * of XFS. |
| 3604 | */ |
| 3605 | log->l_flags |= XLOG_IO_ERROR; |
| 3606 | |
| 3607 | /* |
| 3608 | * If we hit a log error, we want to mark all the iclogs IOERROR |
| 3609 | * while we're still holding the loglock. |
| 3610 | */ |
| 3611 | if (logerror) |
| 3612 | retval = xlog_state_ioerror(log); |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 3613 | spin_unlock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3614 | |
| 3615 | /* |
Dave Chinner | 1054794 | 2010-12-21 12:02:25 +1100 | [diff] [blame] | 3616 | * We don't want anybody waiting for log reservations after this. That |
| 3617 | * means we have to wake up everybody queued up on reserveq as well as |
| 3618 | * writeq. In addition, we make sure in xlog_{re}grant_log_space that |
| 3619 | * we don't enqueue anything once the SHUTDOWN flag is set, and this |
Dave Chinner | 3f16b98 | 2010-12-21 12:29:01 +1100 | [diff] [blame] | 3620 | * action is protected by the grant locks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3621 | */ |
Dave Chinner | 3f16b98 | 2010-12-21 12:29:01 +1100 | [diff] [blame] | 3622 | spin_lock(&log->l_grant_reserve_lock); |
Dave Chinner | 1054794 | 2010-12-21 12:02:25 +1100 | [diff] [blame] | 3623 | list_for_each_entry(tic, &log->l_reserveq, t_queue) |
Christoph Hellwig | 14a7235f | 2012-02-20 02:31:24 +0000 | [diff] [blame^] | 3624 | wake_up_process(tic->t_task); |
Dave Chinner | 3f16b98 | 2010-12-21 12:29:01 +1100 | [diff] [blame] | 3625 | spin_unlock(&log->l_grant_reserve_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3626 | |
Dave Chinner | 3f16b98 | 2010-12-21 12:29:01 +1100 | [diff] [blame] | 3627 | spin_lock(&log->l_grant_write_lock); |
Dave Chinner | 1054794 | 2010-12-21 12:02:25 +1100 | [diff] [blame] | 3628 | list_for_each_entry(tic, &log->l_writeq, t_queue) |
Christoph Hellwig | 14a7235f | 2012-02-20 02:31:24 +0000 | [diff] [blame^] | 3629 | wake_up_process(tic->t_task); |
Dave Chinner | 3f16b98 | 2010-12-21 12:29:01 +1100 | [diff] [blame] | 3630 | spin_unlock(&log->l_grant_write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3631 | |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 3632 | if (!(log->l_iclog->ic_state & XLOG_STATE_IOERROR)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3633 | ASSERT(!logerror); |
| 3634 | /* |
| 3635 | * Force the incore logs to disk before shutting the |
| 3636 | * log down completely. |
| 3637 | */ |
Christoph Hellwig | a14a348 | 2010-01-19 09:56:46 +0000 | [diff] [blame] | 3638 | _xfs_log_force(mp, XFS_LOG_SYNC, NULL); |
| 3639 | |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 3640 | spin_lock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3641 | retval = xlog_state_ioerror(log); |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 3642 | spin_unlock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3643 | } |
| 3644 | /* |
| 3645 | * Wake up everybody waiting on xfs_log_force. |
| 3646 | * Callback all log item committed functions as if the |
| 3647 | * log writes were completed. |
| 3648 | */ |
| 3649 | xlog_state_do_callback(log, XFS_LI_ABORTED, NULL); |
| 3650 | |
| 3651 | #ifdef XFSERRORDEBUG |
| 3652 | { |
| 3653 | xlog_in_core_t *iclog; |
| 3654 | |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 3655 | spin_lock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3656 | iclog = log->l_iclog; |
| 3657 | do { |
| 3658 | ASSERT(iclog->ic_callback == 0); |
| 3659 | iclog = iclog->ic_next; |
| 3660 | } while (iclog != log->l_iclog); |
Eric Sandeen | b22cd72c | 2007-10-11 17:37:10 +1000 | [diff] [blame] | 3661 | spin_unlock(&log->l_icloglock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3662 | } |
| 3663 | #endif |
| 3664 | /* return non-zero if log IOERROR transition had already happened */ |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 3665 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3666 | } |
| 3667 | |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 3668 | STATIC int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3669 | xlog_iclogs_empty(xlog_t *log) |
| 3670 | { |
| 3671 | xlog_in_core_t *iclog; |
| 3672 | |
| 3673 | iclog = log->l_iclog; |
| 3674 | do { |
| 3675 | /* endianness does not matter here, zero is zero in |
| 3676 | * any language. |
| 3677 | */ |
| 3678 | if (iclog->ic_header.h_num_logops) |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 3679 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3680 | iclog = iclog->ic_next; |
| 3681 | } while (iclog != log->l_iclog); |
Jesper Juhl | 014c254 | 2006-01-15 02:37:08 +0100 | [diff] [blame] | 3682 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3683 | } |