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