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