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