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