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