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