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