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