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