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