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