Commit | Line | Data |
---|---|---|
0b61f8a4 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
7b718769 NS |
3 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
4 | * All Rights Reserved. | |
1da177e4 | 5 | */ |
1da177e4 | 6 | #include "xfs.h" |
a844f451 | 7 | #include "xfs_fs.h" |
70a9883c | 8 | #include "xfs_shared.h" |
a4fbe6ab | 9 | #include "xfs_format.h" |
239880ef DC |
10 | #include "xfs_log_format.h" |
11 | #include "xfs_trans_resv.h" | |
1da177e4 | 12 | #include "xfs_mount.h" |
e9e899a2 | 13 | #include "xfs_errortag.h" |
1da177e4 | 14 | #include "xfs_error.h" |
239880ef DC |
15 | #include "xfs_trans.h" |
16 | #include "xfs_trans_priv.h" | |
17 | #include "xfs_log.h" | |
1da177e4 | 18 | #include "xfs_log_priv.h" |
0b1b213f | 19 | #include "xfs_trace.h" |
baff4e44 | 20 | #include "xfs_sysfs.h" |
61e63ecb | 21 | #include "xfs_sb.h" |
39353ff6 | 22 | #include "xfs_health.h" |
1da177e4 | 23 | |
eb01c9cd | 24 | kmem_zone_t *xfs_log_ticket_zone; |
1da177e4 | 25 | |
1da177e4 | 26 | /* Local miscellaneous function prototypes */ |
9a8d2fdb MT |
27 | STATIC struct xlog * |
28 | xlog_alloc_log( | |
29 | struct xfs_mount *mp, | |
30 | struct xfs_buftarg *log_target, | |
31 | xfs_daddr_t blk_offset, | |
32 | int num_bblks); | |
ad223e60 MT |
33 | STATIC int |
34 | xlog_space_left( | |
35 | struct xlog *log, | |
36 | atomic64_t *head); | |
9a8d2fdb MT |
37 | STATIC void |
38 | xlog_dealloc_log( | |
39 | struct xlog *log); | |
1da177e4 LT |
40 | |
41 | /* local state machine functions */ | |
d15cbf2f | 42 | STATIC void xlog_state_done_syncing( |
12e6a0f4 | 43 | struct xlog_in_core *iclog); |
502a01fa DC |
44 | STATIC void xlog_state_do_callback( |
45 | struct xlog *log); | |
9a8d2fdb MT |
46 | STATIC int |
47 | xlog_state_get_iclog_space( | |
48 | struct xlog *log, | |
49 | int len, | |
50 | struct xlog_in_core **iclog, | |
51 | struct xlog_ticket *ticket, | |
52 | int *continued_write, | |
53 | int *logoffsetp); | |
9a8d2fdb MT |
54 | STATIC void |
55 | xlog_state_switch_iclogs( | |
56 | struct xlog *log, | |
57 | struct xlog_in_core *iclog, | |
58 | int eventual_size); | |
59 | STATIC void | |
ad223e60 | 60 | xlog_grant_push_ail( |
9a8d2fdb MT |
61 | struct xlog *log, |
62 | int need_bytes); | |
63 | STATIC void | |
df732b29 CH |
64 | xlog_sync( |
65 | struct xlog *log, | |
66 | struct xlog_in_core *iclog); | |
cfcbbbd0 | 67 | #if defined(DEBUG) |
9a8d2fdb MT |
68 | STATIC void |
69 | xlog_verify_dest_ptr( | |
70 | struct xlog *log, | |
5809d5e0 | 71 | void *ptr); |
ad223e60 MT |
72 | STATIC void |
73 | xlog_verify_grant_tail( | |
9a8d2fdb MT |
74 | struct xlog *log); |
75 | STATIC void | |
76 | xlog_verify_iclog( | |
77 | struct xlog *log, | |
78 | struct xlog_in_core *iclog, | |
abca1f33 | 79 | int count); |
9a8d2fdb MT |
80 | STATIC void |
81 | xlog_verify_tail_lsn( | |
82 | struct xlog *log, | |
9d110014 | 83 | struct xlog_in_core *iclog); |
1da177e4 LT |
84 | #else |
85 | #define xlog_verify_dest_ptr(a,b) | |
3f336c6f | 86 | #define xlog_verify_grant_tail(a) |
abca1f33 | 87 | #define xlog_verify_iclog(a,b,c) |
9d110014 | 88 | #define xlog_verify_tail_lsn(a,b) |
1da177e4 LT |
89 | #endif |
90 | ||
9a8d2fdb MT |
91 | STATIC int |
92 | xlog_iclogs_empty( | |
93 | struct xlog *log); | |
1da177e4 | 94 | |
303591a0 BF |
95 | static int |
96 | xfs_log_cover(struct xfs_mount *); | |
97 | ||
dd954c69 | 98 | static void |
663e496a | 99 | xlog_grant_sub_space( |
ad223e60 MT |
100 | struct xlog *log, |
101 | atomic64_t *head, | |
102 | int bytes) | |
dd954c69 | 103 | { |
d0eb2f38 DC |
104 | int64_t head_val = atomic64_read(head); |
105 | int64_t new, old; | |
a69ed03c | 106 | |
d0eb2f38 DC |
107 | do { |
108 | int cycle, space; | |
a69ed03c | 109 | |
d0eb2f38 | 110 | xlog_crack_grant_head_val(head_val, &cycle, &space); |
a69ed03c | 111 | |
d0eb2f38 DC |
112 | space -= bytes; |
113 | if (space < 0) { | |
114 | space += log->l_logsize; | |
115 | cycle--; | |
116 | } | |
117 | ||
118 | old = head_val; | |
119 | new = xlog_assign_grant_head_val(cycle, space); | |
120 | head_val = atomic64_cmpxchg(head, old, new); | |
121 | } while (head_val != old); | |
dd954c69 CH |
122 | } |
123 | ||
124 | static void | |
663e496a | 125 | xlog_grant_add_space( |
ad223e60 MT |
126 | struct xlog *log, |
127 | atomic64_t *head, | |
128 | int bytes) | |
dd954c69 | 129 | { |
d0eb2f38 DC |
130 | int64_t head_val = atomic64_read(head); |
131 | int64_t new, old; | |
a69ed03c | 132 | |
d0eb2f38 DC |
133 | do { |
134 | int tmp; | |
135 | int cycle, space; | |
a69ed03c | 136 | |
d0eb2f38 | 137 | xlog_crack_grant_head_val(head_val, &cycle, &space); |
a69ed03c | 138 | |
d0eb2f38 DC |
139 | tmp = log->l_logsize - space; |
140 | if (tmp > bytes) | |
141 | space += bytes; | |
142 | else { | |
143 | space = bytes - tmp; | |
144 | cycle++; | |
145 | } | |
146 | ||
147 | old = head_val; | |
148 | new = xlog_assign_grant_head_val(cycle, space); | |
149 | head_val = atomic64_cmpxchg(head, old, new); | |
150 | } while (head_val != old); | |
dd954c69 | 151 | } |
a69ed03c | 152 | |
c303c5b8 CH |
153 | STATIC void |
154 | xlog_grant_head_init( | |
155 | struct xlog_grant_head *head) | |
156 | { | |
157 | xlog_assign_grant_head(&head->grant, 1, 0); | |
158 | INIT_LIST_HEAD(&head->waiters); | |
159 | spin_lock_init(&head->lock); | |
160 | } | |
161 | ||
a79bf2d7 CH |
162 | STATIC void |
163 | xlog_grant_head_wake_all( | |
164 | struct xlog_grant_head *head) | |
165 | { | |
166 | struct xlog_ticket *tic; | |
167 | ||
168 | spin_lock(&head->lock); | |
169 | list_for_each_entry(tic, &head->waiters, t_queue) | |
170 | wake_up_process(tic->t_task); | |
171 | spin_unlock(&head->lock); | |
172 | } | |
173 | ||
e179840d CH |
174 | static inline int |
175 | xlog_ticket_reservation( | |
ad223e60 | 176 | struct xlog *log, |
e179840d CH |
177 | struct xlog_grant_head *head, |
178 | struct xlog_ticket *tic) | |
9f9c19ec | 179 | { |
e179840d CH |
180 | if (head == &log->l_write_head) { |
181 | ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV); | |
182 | return tic->t_unit_res; | |
183 | } else { | |
9f9c19ec | 184 | if (tic->t_flags & XLOG_TIC_PERM_RESERV) |
e179840d | 185 | return tic->t_unit_res * tic->t_cnt; |
9f9c19ec | 186 | else |
e179840d | 187 | return tic->t_unit_res; |
9f9c19ec | 188 | } |
9f9c19ec CH |
189 | } |
190 | ||
191 | STATIC bool | |
e179840d | 192 | xlog_grant_head_wake( |
ad223e60 | 193 | struct xlog *log, |
e179840d | 194 | struct xlog_grant_head *head, |
9f9c19ec CH |
195 | int *free_bytes) |
196 | { | |
197 | struct xlog_ticket *tic; | |
198 | int need_bytes; | |
7c107afb | 199 | bool woken_task = false; |
9f9c19ec | 200 | |
e179840d | 201 | list_for_each_entry(tic, &head->waiters, t_queue) { |
7c107afb DC |
202 | |
203 | /* | |
204 | * There is a chance that the size of the CIL checkpoints in | |
205 | * progress at the last AIL push target calculation resulted in | |
206 | * limiting the target to the log head (l_last_sync_lsn) at the | |
207 | * time. This may not reflect where the log head is now as the | |
208 | * CIL checkpoints may have completed. | |
209 | * | |
210 | * Hence when we are woken here, it may be that the head of the | |
211 | * log that has moved rather than the tail. As the tail didn't | |
212 | * move, there still won't be space available for the | |
213 | * reservation we require. However, if the AIL has already | |
214 | * pushed to the target defined by the old log head location, we | |
215 | * will hang here waiting for something else to update the AIL | |
216 | * push target. | |
217 | * | |
218 | * Therefore, if there isn't space to wake the first waiter on | |
219 | * the grant head, we need to push the AIL again to ensure the | |
220 | * target reflects both the current log tail and log head | |
221 | * position before we wait for the tail to move again. | |
222 | */ | |
223 | ||
e179840d | 224 | need_bytes = xlog_ticket_reservation(log, head, tic); |
7c107afb DC |
225 | if (*free_bytes < need_bytes) { |
226 | if (!woken_task) | |
227 | xlog_grant_push_ail(log, need_bytes); | |
9f9c19ec | 228 | return false; |
7c107afb | 229 | } |
9f9c19ec | 230 | |
e179840d CH |
231 | *free_bytes -= need_bytes; |
232 | trace_xfs_log_grant_wake_up(log, tic); | |
14a7235f | 233 | wake_up_process(tic->t_task); |
7c107afb | 234 | woken_task = true; |
9f9c19ec CH |
235 | } |
236 | ||
237 | return true; | |
238 | } | |
239 | ||
240 | STATIC int | |
23ee3df3 | 241 | xlog_grant_head_wait( |
ad223e60 | 242 | struct xlog *log, |
23ee3df3 | 243 | struct xlog_grant_head *head, |
9f9c19ec | 244 | struct xlog_ticket *tic, |
a30b0367 DC |
245 | int need_bytes) __releases(&head->lock) |
246 | __acquires(&head->lock) | |
9f9c19ec | 247 | { |
23ee3df3 | 248 | list_add_tail(&tic->t_queue, &head->waiters); |
9f9c19ec CH |
249 | |
250 | do { | |
2039a272 | 251 | if (xlog_is_shutdown(log)) |
9f9c19ec CH |
252 | goto shutdown; |
253 | xlog_grant_push_ail(log, need_bytes); | |
254 | ||
14a7235f | 255 | __set_current_state(TASK_UNINTERRUPTIBLE); |
23ee3df3 | 256 | spin_unlock(&head->lock); |
14a7235f | 257 | |
ff6d6af2 | 258 | XFS_STATS_INC(log->l_mp, xs_sleep_logspace); |
9f9c19ec | 259 | |
14a7235f CH |
260 | trace_xfs_log_grant_sleep(log, tic); |
261 | schedule(); | |
9f9c19ec CH |
262 | trace_xfs_log_grant_wake(log, tic); |
263 | ||
23ee3df3 | 264 | spin_lock(&head->lock); |
2039a272 | 265 | if (xlog_is_shutdown(log)) |
9f9c19ec | 266 | goto shutdown; |
23ee3df3 | 267 | } while (xlog_space_left(log, &head->grant) < need_bytes); |
9f9c19ec CH |
268 | |
269 | list_del_init(&tic->t_queue); | |
270 | return 0; | |
271 | shutdown: | |
272 | list_del_init(&tic->t_queue); | |
2451337d | 273 | return -EIO; |
9f9c19ec CH |
274 | } |
275 | ||
42ceedb3 CH |
276 | /* |
277 | * Atomically get the log space required for a log ticket. | |
278 | * | |
279 | * Once a ticket gets put onto head->waiters, it will only return after the | |
280 | * needed reservation is satisfied. | |
281 | * | |
282 | * This function is structured so that it has a lock free fast path. This is | |
283 | * necessary because every new transaction reservation will come through this | |
284 | * path. Hence any lock will be globally hot if we take it unconditionally on | |
285 | * every pass. | |
286 | * | |
287 | * As tickets are only ever moved on and off head->waiters under head->lock, we | |
288 | * only need to take that lock if we are going to add the ticket to the queue | |
289 | * and sleep. We can avoid taking the lock if the ticket was never added to | |
290 | * head->waiters because the t_queue list head will be empty and we hold the | |
291 | * only reference to it so it can safely be checked unlocked. | |
292 | */ | |
293 | STATIC int | |
294 | xlog_grant_head_check( | |
ad223e60 | 295 | struct xlog *log, |
42ceedb3 CH |
296 | struct xlog_grant_head *head, |
297 | struct xlog_ticket *tic, | |
298 | int *need_bytes) | |
299 | { | |
300 | int free_bytes; | |
301 | int error = 0; | |
302 | ||
e1d06e5f | 303 | ASSERT(!xlog_in_recovery(log)); |
42ceedb3 CH |
304 | |
305 | /* | |
306 | * If there are other waiters on the queue then give them a chance at | |
307 | * logspace before us. Wake up the first waiters, if we do not wake | |
308 | * up all the waiters then go to sleep waiting for more free space, | |
309 | * otherwise try to get some space for this transaction. | |
310 | */ | |
311 | *need_bytes = xlog_ticket_reservation(log, head, tic); | |
312 | free_bytes = xlog_space_left(log, &head->grant); | |
313 | if (!list_empty_careful(&head->waiters)) { | |
314 | spin_lock(&head->lock); | |
315 | if (!xlog_grant_head_wake(log, head, &free_bytes) || | |
316 | free_bytes < *need_bytes) { | |
317 | error = xlog_grant_head_wait(log, head, tic, | |
318 | *need_bytes); | |
319 | } | |
320 | spin_unlock(&head->lock); | |
321 | } else if (free_bytes < *need_bytes) { | |
322 | spin_lock(&head->lock); | |
323 | error = xlog_grant_head_wait(log, head, tic, *need_bytes); | |
324 | spin_unlock(&head->lock); | |
325 | } | |
326 | ||
327 | return error; | |
328 | } | |
329 | ||
0adba536 CH |
330 | static void |
331 | xlog_tic_reset_res(xlog_ticket_t *tic) | |
332 | { | |
333 | tic->t_res_num = 0; | |
334 | tic->t_res_arr_sum = 0; | |
335 | tic->t_res_num_ophdrs = 0; | |
336 | } | |
337 | ||
338 | static void | |
339 | xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type) | |
340 | { | |
341 | if (tic->t_res_num == XLOG_TIC_LEN_MAX) { | |
342 | /* add to overflow and start again */ | |
343 | tic->t_res_o_flow += tic->t_res_arr_sum; | |
344 | tic->t_res_num = 0; | |
345 | tic->t_res_arr_sum = 0; | |
346 | } | |
347 | ||
348 | tic->t_res_arr[tic->t_res_num].r_len = len; | |
349 | tic->t_res_arr[tic->t_res_num].r_type = type; | |
350 | tic->t_res_arr_sum += len; | |
351 | tic->t_res_num++; | |
352 | } | |
dd954c69 | 353 | |
50d25484 BF |
354 | bool |
355 | xfs_log_writable( | |
356 | struct xfs_mount *mp) | |
357 | { | |
358 | /* | |
8e9800f9 DW |
359 | * Do not write to the log on norecovery mounts, if the data or log |
360 | * devices are read-only, or if the filesystem is shutdown. Read-only | |
361 | * mounts allow internal writes for log recovery and unmount purposes, | |
362 | * so don't restrict that case. | |
50d25484 BF |
363 | */ |
364 | if (mp->m_flags & XFS_MOUNT_NORECOVERY) | |
365 | return false; | |
8e9800f9 DW |
366 | if (xfs_readonly_buftarg(mp->m_ddev_targp)) |
367 | return false; | |
50d25484 BF |
368 | if (xfs_readonly_buftarg(mp->m_log->l_targ)) |
369 | return false; | |
2039a272 | 370 | if (xlog_is_shutdown(mp->m_log)) |
50d25484 BF |
371 | return false; |
372 | return true; | |
373 | } | |
374 | ||
9006fb91 CH |
375 | /* |
376 | * Replenish the byte reservation required by moving the grant write head. | |
377 | */ | |
378 | int | |
379 | xfs_log_regrant( | |
380 | struct xfs_mount *mp, | |
381 | struct xlog_ticket *tic) | |
382 | { | |
ad223e60 | 383 | struct xlog *log = mp->m_log; |
9006fb91 CH |
384 | int need_bytes; |
385 | int error = 0; | |
386 | ||
2039a272 | 387 | if (xlog_is_shutdown(log)) |
2451337d | 388 | return -EIO; |
9006fb91 | 389 | |
ff6d6af2 | 390 | XFS_STATS_INC(mp, xs_try_logspace); |
9006fb91 CH |
391 | |
392 | /* | |
393 | * This is a new transaction on the ticket, so we need to change the | |
394 | * transaction ID so that the next transaction has a different TID in | |
395 | * the log. Just add one to the existing tid so that we can see chains | |
396 | * of rolling transactions in the log easily. | |
397 | */ | |
398 | tic->t_tid++; | |
399 | ||
400 | xlog_grant_push_ail(log, tic->t_unit_res); | |
401 | ||
402 | tic->t_curr_res = tic->t_unit_res; | |
403 | xlog_tic_reset_res(tic); | |
404 | ||
405 | if (tic->t_cnt > 0) | |
406 | return 0; | |
407 | ||
408 | trace_xfs_log_regrant(log, tic); | |
409 | ||
410 | error = xlog_grant_head_check(log, &log->l_write_head, tic, | |
411 | &need_bytes); | |
412 | if (error) | |
413 | goto out_error; | |
414 | ||
415 | xlog_grant_add_space(log, &log->l_write_head.grant, need_bytes); | |
416 | trace_xfs_log_regrant_exit(log, tic); | |
417 | xlog_verify_grant_tail(log); | |
418 | return 0; | |
419 | ||
420 | out_error: | |
421 | /* | |
422 | * If we are failing, make sure the ticket doesn't have any current | |
423 | * reservations. We don't want to add this back when the ticket/ | |
424 | * transaction gets cancelled. | |
425 | */ | |
426 | tic->t_curr_res = 0; | |
427 | tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */ | |
428 | return error; | |
429 | } | |
430 | ||
431 | /* | |
a0e336ba | 432 | * Reserve log space and return a ticket corresponding to the reservation. |
9006fb91 CH |
433 | * |
434 | * Each reservation is going to reserve extra space for a log record header. | |
435 | * When writes happen to the on-disk log, we don't subtract the length of the | |
436 | * log record header from any reservation. By wasting space in each | |
437 | * reservation, we prevent over allocation problems. | |
438 | */ | |
439 | int | |
440 | xfs_log_reserve( | |
441 | struct xfs_mount *mp, | |
442 | int unit_bytes, | |
443 | int cnt, | |
444 | struct xlog_ticket **ticp, | |
c8ce540d | 445 | uint8_t client, |
710b1e2c | 446 | bool permanent) |
9006fb91 | 447 | { |
ad223e60 | 448 | struct xlog *log = mp->m_log; |
9006fb91 CH |
449 | struct xlog_ticket *tic; |
450 | int need_bytes; | |
451 | int error = 0; | |
452 | ||
453 | ASSERT(client == XFS_TRANSACTION || client == XFS_LOG); | |
454 | ||
2039a272 | 455 | if (xlog_is_shutdown(log)) |
2451337d | 456 | return -EIO; |
9006fb91 | 457 | |
ff6d6af2 | 458 | XFS_STATS_INC(mp, xs_try_logspace); |
9006fb91 CH |
459 | |
460 | ASSERT(*ticp == NULL); | |
ca4f2589 | 461 | tic = xlog_ticket_alloc(log, unit_bytes, cnt, client, permanent); |
9006fb91 CH |
462 | *ticp = tic; |
463 | ||
437a255a DC |
464 | xlog_grant_push_ail(log, tic->t_cnt ? tic->t_unit_res * tic->t_cnt |
465 | : tic->t_unit_res); | |
9006fb91 CH |
466 | |
467 | trace_xfs_log_reserve(log, tic); | |
468 | ||
469 | error = xlog_grant_head_check(log, &log->l_reserve_head, tic, | |
470 | &need_bytes); | |
471 | if (error) | |
472 | goto out_error; | |
473 | ||
474 | xlog_grant_add_space(log, &log->l_reserve_head.grant, need_bytes); | |
475 | xlog_grant_add_space(log, &log->l_write_head.grant, need_bytes); | |
476 | trace_xfs_log_reserve_exit(log, tic); | |
477 | xlog_verify_grant_tail(log); | |
478 | return 0; | |
479 | ||
480 | out_error: | |
481 | /* | |
482 | * If we are failing, make sure the ticket doesn't have any current | |
483 | * reservations. We don't want to add this back when the ticket/ | |
484 | * transaction gets cancelled. | |
485 | */ | |
486 | tic->t_curr_res = 0; | |
487 | tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */ | |
488 | return error; | |
489 | } | |
490 | ||
aad7272a DC |
491 | /* |
492 | * Run all the pending iclog callbacks and wake log force waiters and iclog | |
493 | * space waiters so they can process the newly set shutdown state. We really | |
494 | * don't care what order we process callbacks here because the log is shut down | |
495 | * and so state cannot change on disk anymore. | |
502a01fa DC |
496 | * |
497 | * We avoid processing actively referenced iclogs so that we don't run callbacks | |
498 | * while the iclog owner might still be preparing the iclog for IO submssion. | |
499 | * These will be caught by xlog_state_iclog_release() and call this function | |
500 | * again to process any callbacks that may have been added to that iclog. | |
aad7272a DC |
501 | */ |
502 | static void | |
503 | xlog_state_shutdown_callbacks( | |
504 | struct xlog *log) | |
505 | { | |
506 | struct xlog_in_core *iclog; | |
507 | LIST_HEAD(cb_list); | |
508 | ||
509 | spin_lock(&log->l_icloglock); | |
510 | iclog = log->l_iclog; | |
511 | do { | |
502a01fa DC |
512 | if (atomic_read(&iclog->ic_refcnt)) { |
513 | /* Reference holder will re-run iclog callbacks. */ | |
514 | continue; | |
515 | } | |
aad7272a | 516 | list_splice_init(&iclog->ic_callbacks, &cb_list); |
502a01fa | 517 | wake_up_all(&iclog->ic_write_wait); |
aad7272a DC |
518 | wake_up_all(&iclog->ic_force_wait); |
519 | } while ((iclog = iclog->ic_next) != log->l_iclog); | |
520 | ||
521 | wake_up_all(&log->l_flush_wait); | |
522 | spin_unlock(&log->l_icloglock); | |
523 | ||
524 | xlog_cil_process_committed(&cb_list); | |
525 | } | |
526 | ||
df732b29 CH |
527 | /* |
528 | * Flush iclog to disk if this is the last reference to the given iclog and the | |
9d110014 DC |
529 | * it is in the WANT_SYNC state. |
530 | * | |
531 | * If the caller passes in a non-zero @old_tail_lsn and the current log tail | |
532 | * does not match, there may be metadata on disk that must be persisted before | |
533 | * this iclog is written. To satisfy that requirement, set the | |
534 | * XLOG_ICL_NEED_FLUSH flag as a condition for writing this iclog with the new | |
535 | * log tail value. | |
536 | * | |
537 | * If XLOG_ICL_NEED_FUA is already set on the iclog, we need to ensure that the | |
538 | * log tail is updated correctly. NEED_FUA indicates that the iclog will be | |
539 | * written to stable storage, and implies that a commit record is contained | |
540 | * within the iclog. We need to ensure that the log tail does not move beyond | |
541 | * the tail that the first commit record in the iclog ordered against, otherwise | |
542 | * correct recovery of that checkpoint becomes dependent on future operations | |
543 | * performed on this iclog. | |
544 | * | |
545 | * Hence if NEED_FUA is set and the current iclog tail lsn is empty, write the | |
546 | * current tail into iclog. Once the iclog tail is set, future operations must | |
547 | * not modify it, otherwise they potentially violate ordering constraints for | |
548 | * the checkpoint commit that wrote the initial tail lsn value. The tail lsn in | |
549 | * the iclog will get zeroed on activation of the iclog after sync, so we | |
550 | * always capture the tail lsn on the iclog on the first NEED_FUA release | |
551 | * regardless of the number of active reference counts on this iclog. | |
df732b29 | 552 | */ |
9d110014 | 553 | |
eef983ff | 554 | int |
df732b29 CH |
555 | xlog_state_release_iclog( |
556 | struct xlog *log, | |
0dc8f7f1 DC |
557 | struct xlog_in_core *iclog, |
558 | xfs_lsn_t old_tail_lsn) | |
df732b29 | 559 | { |
9d392064 | 560 | xfs_lsn_t tail_lsn; |
502a01fa DC |
561 | bool last_ref; |
562 | ||
df732b29 CH |
563 | lockdep_assert_held(&log->l_icloglock); |
564 | ||
956f6daa | 565 | trace_xlog_iclog_release(iclog, _RET_IP_); |
0dc8f7f1 DC |
566 | /* |
567 | * Grabbing the current log tail needs to be atomic w.r.t. the writing | |
568 | * of the tail LSN into the iclog so we guarantee that the log tail does | |
569 | * not move between deciding if a cache flush is required and writing | |
570 | * the LSN into the iclog below. | |
571 | */ | |
572 | if (old_tail_lsn || iclog->ic_state == XLOG_STATE_WANT_SYNC) { | |
573 | tail_lsn = xlog_assign_tail_lsn(log->l_mp); | |
574 | ||
575 | if (old_tail_lsn && tail_lsn != old_tail_lsn) | |
576 | iclog->ic_flags |= XLOG_ICL_NEED_FLUSH; | |
9d110014 DC |
577 | |
578 | if ((iclog->ic_flags & XLOG_ICL_NEED_FUA) && | |
579 | !iclog->ic_header.h_tail_lsn) | |
580 | iclog->ic_header.h_tail_lsn = cpu_to_be64(tail_lsn); | |
0dc8f7f1 DC |
581 | } |
582 | ||
502a01fa DC |
583 | last_ref = atomic_dec_and_test(&iclog->ic_refcnt); |
584 | ||
585 | if (xlog_is_shutdown(log)) { | |
586 | /* | |
587 | * If there are no more references to this iclog, process the | |
588 | * pending iclog callbacks that were waiting on the release of | |
589 | * this iclog. | |
590 | */ | |
591 | if (last_ref) { | |
592 | spin_unlock(&log->l_icloglock); | |
593 | xlog_state_shutdown_callbacks(log); | |
594 | spin_lock(&log->l_icloglock); | |
595 | } | |
596 | return -EIO; | |
597 | } | |
598 | ||
599 | if (!last_ref) | |
9d392064 DC |
600 | return 0; |
601 | ||
602 | if (iclog->ic_state != XLOG_STATE_WANT_SYNC) { | |
603 | ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE); | |
604 | return 0; | |
df732b29 CH |
605 | } |
606 | ||
9d392064 | 607 | iclog->ic_state = XLOG_STATE_SYNCING; |
9d110014 DC |
608 | if (!iclog->ic_header.h_tail_lsn) |
609 | iclog->ic_header.h_tail_lsn = cpu_to_be64(tail_lsn); | |
610 | xlog_verify_tail_lsn(log, iclog); | |
9d392064 DC |
611 | trace_xlog_iclog_syncing(iclog, _RET_IP_); |
612 | ||
613 | spin_unlock(&log->l_icloglock); | |
614 | xlog_sync(log, iclog); | |
615 | spin_lock(&log->l_icloglock); | |
df732b29 CH |
616 | return 0; |
617 | } | |
618 | ||
1da177e4 LT |
619 | /* |
620 | * Mount a log filesystem | |
621 | * | |
622 | * mp - ubiquitous xfs mount point structure | |
623 | * log_target - buftarg of on-disk log device | |
624 | * blk_offset - Start block # where block size is 512 bytes (BBSIZE) | |
625 | * num_bblocks - Number of BBSIZE blocks in on-disk log | |
626 | * | |
627 | * Return error or zero. | |
628 | */ | |
629 | int | |
249a8c11 DC |
630 | xfs_log_mount( |
631 | xfs_mount_t *mp, | |
632 | xfs_buftarg_t *log_target, | |
633 | xfs_daddr_t blk_offset, | |
634 | int num_bblks) | |
1da177e4 | 635 | { |
e1d06e5f | 636 | struct xlog *log; |
9c92ee20 | 637 | bool fatal = xfs_sb_version_hascrc(&mp->m_sb); |
3e7b91cf JL |
638 | int error = 0; |
639 | int min_logfsbs; | |
249a8c11 | 640 | |
c99d609a DC |
641 | if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) { |
642 | xfs_notice(mp, "Mounting V%d Filesystem", | |
643 | XFS_SB_VERSION_NUM(&mp->m_sb)); | |
644 | } else { | |
a0fa2b67 | 645 | xfs_notice(mp, |
c99d609a DC |
646 | "Mounting V%d filesystem in no-recovery mode. Filesystem will be inconsistent.", |
647 | XFS_SB_VERSION_NUM(&mp->m_sb)); | |
bd186aa9 | 648 | ASSERT(mp->m_flags & XFS_MOUNT_RDONLY); |
1da177e4 LT |
649 | } |
650 | ||
e1d06e5f DC |
651 | log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks); |
652 | if (IS_ERR(log)) { | |
653 | error = PTR_ERR(log); | |
644c3567 DC |
654 | goto out; |
655 | } | |
e1d06e5f | 656 | mp->m_log = log; |
1da177e4 | 657 | |
3e7b91cf JL |
658 | /* |
659 | * Validate the given log space and drop a critical message via syslog | |
660 | * if the log size is too small that would lead to some unexpected | |
661 | * situations in transaction log space reservation stage. | |
662 | * | |
663 | * Note: we can't just reject the mount if the validation fails. This | |
664 | * would mean that people would have to downgrade their kernel just to | |
665 | * remedy the situation as there is no way to grow the log (short of | |
666 | * black magic surgery with xfs_db). | |
667 | * | |
668 | * We can, however, reject mounts for CRC format filesystems, as the | |
669 | * mkfs binary being used to make the filesystem should never create a | |
670 | * filesystem with a log that is too small. | |
671 | */ | |
672 | min_logfsbs = xfs_log_calc_minimum_size(mp); | |
673 | ||
674 | if (mp->m_sb.sb_logblocks < min_logfsbs) { | |
675 | xfs_warn(mp, | |
676 | "Log size %d blocks too small, minimum size is %d blocks", | |
677 | mp->m_sb.sb_logblocks, min_logfsbs); | |
2451337d | 678 | error = -EINVAL; |
3e7b91cf JL |
679 | } else if (mp->m_sb.sb_logblocks > XFS_MAX_LOG_BLOCKS) { |
680 | xfs_warn(mp, | |
681 | "Log size %d blocks too large, maximum size is %lld blocks", | |
682 | mp->m_sb.sb_logblocks, XFS_MAX_LOG_BLOCKS); | |
2451337d | 683 | error = -EINVAL; |
3e7b91cf JL |
684 | } else if (XFS_FSB_TO_B(mp, mp->m_sb.sb_logblocks) > XFS_MAX_LOG_BYTES) { |
685 | xfs_warn(mp, | |
686 | "log size %lld bytes too large, maximum size is %lld bytes", | |
687 | XFS_FSB_TO_B(mp, mp->m_sb.sb_logblocks), | |
688 | XFS_MAX_LOG_BYTES); | |
2451337d | 689 | error = -EINVAL; |
9c92ee20 DW |
690 | } else if (mp->m_sb.sb_logsunit > 1 && |
691 | mp->m_sb.sb_logsunit % mp->m_sb.sb_blocksize) { | |
692 | xfs_warn(mp, | |
693 | "log stripe unit %u bytes must be a multiple of block size", | |
694 | mp->m_sb.sb_logsunit); | |
695 | error = -EINVAL; | |
696 | fatal = true; | |
3e7b91cf JL |
697 | } |
698 | if (error) { | |
9c92ee20 DW |
699 | /* |
700 | * Log check errors are always fatal on v5; or whenever bad | |
701 | * metadata leads to a crash. | |
702 | */ | |
703 | if (fatal) { | |
3e7b91cf JL |
704 | xfs_crit(mp, "AAIEEE! Log failed size checks. Abort!"); |
705 | ASSERT(0); | |
706 | goto out_free_log; | |
707 | } | |
f41febd2 | 708 | xfs_crit(mp, "Log size out of supported range."); |
3e7b91cf | 709 | xfs_crit(mp, |
f41febd2 | 710 | "Continuing onwards, but if log hangs are experienced then please report this message in the bug report."); |
3e7b91cf JL |
711 | } |
712 | ||
249a8c11 DC |
713 | /* |
714 | * Initialize the AIL now we have a log. | |
715 | */ | |
249a8c11 DC |
716 | error = xfs_trans_ail_init(mp); |
717 | if (error) { | |
a0fa2b67 | 718 | xfs_warn(mp, "AIL initialisation failed: error %d", error); |
26430752 | 719 | goto out_free_log; |
249a8c11 | 720 | } |
e1d06e5f | 721 | log->l_ailp = mp->m_ail; |
249a8c11 | 722 | |
1da177e4 LT |
723 | /* |
724 | * skip log recovery on a norecovery mount. pretend it all | |
725 | * just worked. | |
726 | */ | |
727 | if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) { | |
249a8c11 | 728 | int readonly = (mp->m_flags & XFS_MOUNT_RDONLY); |
1da177e4 LT |
729 | |
730 | if (readonly) | |
bd186aa9 | 731 | mp->m_flags &= ~XFS_MOUNT_RDONLY; |
1da177e4 | 732 | |
e1d06e5f | 733 | error = xlog_recover(log); |
1da177e4 LT |
734 | |
735 | if (readonly) | |
bd186aa9 | 736 | mp->m_flags |= XFS_MOUNT_RDONLY; |
1da177e4 | 737 | if (error) { |
a0fa2b67 DC |
738 | xfs_warn(mp, "log mount/recovery failed: error %d", |
739 | error); | |
e1d06e5f | 740 | xlog_recover_cancel(log); |
26430752 | 741 | goto out_destroy_ail; |
1da177e4 LT |
742 | } |
743 | } | |
744 | ||
e1d06e5f | 745 | error = xfs_sysfs_init(&log->l_kobj, &xfs_log_ktype, &mp->m_kobj, |
baff4e44 BF |
746 | "log"); |
747 | if (error) | |
748 | goto out_destroy_ail; | |
749 | ||
1da177e4 | 750 | /* Normal transactions can now occur */ |
e1d06e5f | 751 | clear_bit(XLOG_ACTIVE_RECOVERY, &log->l_opstate); |
1da177e4 | 752 | |
71e330b5 DC |
753 | /* |
754 | * Now the log has been fully initialised and we know were our | |
755 | * space grant counters are, we can initialise the permanent ticket | |
756 | * needed for delayed logging to work. | |
757 | */ | |
e1d06e5f | 758 | xlog_cil_init_post_recovery(log); |
71e330b5 | 759 | |
1da177e4 | 760 | return 0; |
26430752 CH |
761 | |
762 | out_destroy_ail: | |
763 | xfs_trans_ail_destroy(mp); | |
764 | out_free_log: | |
e1d06e5f | 765 | xlog_dealloc_log(log); |
644c3567 | 766 | out: |
249a8c11 | 767 | return error; |
26430752 | 768 | } |
1da177e4 LT |
769 | |
770 | /* | |
f661f1e0 DC |
771 | * Finish the recovery of the file system. This is separate from the |
772 | * xfs_log_mount() call, because it depends on the code in xfs_mountfs() to read | |
773 | * in the root and real-time bitmap inodes between calling xfs_log_mount() and | |
774 | * here. | |
1da177e4 | 775 | * |
f661f1e0 DC |
776 | * If we finish recovery successfully, start the background log work. If we are |
777 | * not doing recovery, then we have a RO filesystem and we don't need to start | |
778 | * it. | |
1da177e4 LT |
779 | */ |
780 | int | |
f0b2efad BF |
781 | xfs_log_mount_finish( |
782 | struct xfs_mount *mp) | |
1da177e4 | 783 | { |
fd67d8a0 DC |
784 | struct xlog *log = mp->m_log; |
785 | bool readonly = (mp->m_flags & XFS_MOUNT_RDONLY); | |
786 | int error = 0; | |
1da177e4 | 787 | |
f0b2efad | 788 | if (mp->m_flags & XFS_MOUNT_NORECOVERY) { |
bd186aa9 | 789 | ASSERT(mp->m_flags & XFS_MOUNT_RDONLY); |
f0b2efad | 790 | return 0; |
6f4a1eef ES |
791 | } else if (readonly) { |
792 | /* Allow unlinked processing to proceed */ | |
793 | mp->m_flags &= ~XFS_MOUNT_RDONLY; | |
1da177e4 LT |
794 | } |
795 | ||
8204f8dd DW |
796 | /* |
797 | * During the second phase of log recovery, we need iget and | |
798 | * iput to behave like they do for an active filesystem. | |
799 | * xfs_fs_drop_inode needs to be able to prevent the deletion | |
800 | * of inodes before we're done replaying log items on those | |
801 | * inodes. Turn it off immediately after recovery finishes | |
802 | * so that we don't leak the quota inodes if subsequent mount | |
803 | * activities fail. | |
799ea9e9 DW |
804 | * |
805 | * We let all inodes involved in redo item processing end up on | |
806 | * the LRU instead of being evicted immediately so that if we do | |
807 | * something to an unlinked inode, the irele won't cause | |
808 | * premature truncation and freeing of the inode, which results | |
809 | * in log recovery failure. We have to evict the unreferenced | |
1751e8a6 | 810 | * lru inodes after clearing SB_ACTIVE because we don't |
799ea9e9 DW |
811 | * otherwise clean up the lru if there's a subsequent failure in |
812 | * xfs_mountfs, which leads to us leaking the inodes if nothing | |
813 | * else (e.g. quotacheck) references the inodes before the | |
814 | * mount failure occurs. | |
8204f8dd | 815 | */ |
1751e8a6 | 816 | mp->m_super->s_flags |= SB_ACTIVE; |
e1d06e5f | 817 | if (xlog_recovery_needed(log)) |
fd67d8a0 | 818 | error = xlog_recover_finish(log); |
f0b2efad BF |
819 | if (!error) |
820 | xfs_log_work_queue(mp); | |
1751e8a6 | 821 | mp->m_super->s_flags &= ~SB_ACTIVE; |
799ea9e9 | 822 | evict_inodes(mp->m_super); |
f0b2efad | 823 | |
f1b92bbc BF |
824 | /* |
825 | * Drain the buffer LRU after log recovery. This is required for v4 | |
826 | * filesystems to avoid leaving around buffers with NULL verifier ops, | |
827 | * but we do it unconditionally to make sure we're always in a clean | |
828 | * cache state after mount. | |
829 | * | |
830 | * Don't push in the error case because the AIL may have pending intents | |
831 | * that aren't removed until recovery is cancelled. | |
832 | */ | |
e1d06e5f | 833 | if (xlog_recovery_needed(log)) { |
fd67d8a0 DC |
834 | if (!error) { |
835 | xfs_log_force(mp, XFS_LOG_SYNC); | |
836 | xfs_ail_push_all_sync(mp->m_ail); | |
837 | } | |
838 | xfs_notice(mp, "Ending recovery (logdev: %s)", | |
839 | mp->m_logname ? mp->m_logname : "internal"); | |
840 | } else { | |
841 | xfs_info(mp, "Ending clean mount"); | |
f1b92bbc | 842 | } |
10fb9ac1 | 843 | xfs_buftarg_drain(mp->m_ddev_targp); |
f1b92bbc | 844 | |
e1d06e5f | 845 | clear_bit(XLOG_RECOVERY_NEEDED, &log->l_opstate); |
6f4a1eef ES |
846 | if (readonly) |
847 | mp->m_flags |= XFS_MOUNT_RDONLY; | |
848 | ||
4e6b8270 | 849 | /* Make sure the log is dead if we're returning failure. */ |
fd67d8a0 | 850 | ASSERT(!error || xlog_is_shutdown(log)); |
4e6b8270 | 851 | |
f0b2efad BF |
852 | return error; |
853 | } | |
854 | ||
855 | /* | |
856 | * The mount has failed. Cancel the recovery if it hasn't completed and destroy | |
857 | * the log. | |
858 | */ | |
a7a9250e | 859 | void |
f0b2efad BF |
860 | xfs_log_mount_cancel( |
861 | struct xfs_mount *mp) | |
862 | { | |
a7a9250e | 863 | xlog_recover_cancel(mp->m_log); |
f0b2efad | 864 | xfs_log_unmount(mp); |
1da177e4 LT |
865 | } |
866 | ||
45eddb41 DC |
867 | /* |
868 | * Flush out the iclog to disk ensuring that device caches are flushed and | |
869 | * the iclog hits stable storage before any completion waiters are woken. | |
870 | */ | |
871 | static inline int | |
872 | xlog_force_iclog( | |
873 | struct xlog_in_core *iclog) | |
874 | { | |
875 | atomic_inc(&iclog->ic_refcnt); | |
2bf1ec0f | 876 | iclog->ic_flags |= XLOG_ICL_NEED_FLUSH | XLOG_ICL_NEED_FUA; |
45eddb41 DC |
877 | if (iclog->ic_state == XLOG_STATE_ACTIVE) |
878 | xlog_state_switch_iclogs(iclog->ic_log, iclog, 0); | |
879 | return xlog_state_release_iclog(iclog->ic_log, iclog, 0); | |
880 | } | |
881 | ||
81e5b50a | 882 | /* |
a79b28c2 DC |
883 | * Wait for the iclog and all prior iclogs to be written disk as required by the |
884 | * log force state machine. Waiting on ic_force_wait ensures iclog completions | |
885 | * have been ordered and callbacks run before we are woken here, hence | |
886 | * guaranteeing that all the iclogs up to this one are on stable storage. | |
81e5b50a | 887 | */ |
a79b28c2 | 888 | int |
81e5b50a CH |
889 | xlog_wait_on_iclog( |
890 | struct xlog_in_core *iclog) | |
891 | __releases(iclog->ic_log->l_icloglock) | |
892 | { | |
893 | struct xlog *log = iclog->ic_log; | |
894 | ||
956f6daa | 895 | trace_xlog_iclog_wait_on(iclog, _RET_IP_); |
2039a272 | 896 | if (!xlog_is_shutdown(log) && |
81e5b50a CH |
897 | iclog->ic_state != XLOG_STATE_ACTIVE && |
898 | iclog->ic_state != XLOG_STATE_DIRTY) { | |
899 | XFS_STATS_INC(log->l_mp, xs_log_force_sleep); | |
900 | xlog_wait(&iclog->ic_force_wait, &log->l_icloglock); | |
901 | } else { | |
902 | spin_unlock(&log->l_icloglock); | |
903 | } | |
904 | ||
2039a272 | 905 | if (xlog_is_shutdown(log)) |
81e5b50a CH |
906 | return -EIO; |
907 | return 0; | |
908 | } | |
909 | ||
1da177e4 | 910 | /* |
3c702f95 DC |
911 | * Write out an unmount record using the ticket provided. We have to account for |
912 | * the data space used in the unmount ticket as this write is not done from a | |
913 | * transaction context that has already done the accounting for us. | |
1da177e4 | 914 | */ |
3c702f95 DC |
915 | static int |
916 | xlog_write_unmount_record( | |
917 | struct xlog *log, | |
3468bb1c | 918 | struct xlog_ticket *ticket) |
53235f22 | 919 | { |
3c702f95 | 920 | struct xfs_unmount_log_format ulf = { |
53235f22 DW |
921 | .magic = XLOG_UNMOUNT_TYPE, |
922 | }; | |
923 | struct xfs_log_iovec reg = { | |
3c702f95 DC |
924 | .i_addr = &ulf, |
925 | .i_len = sizeof(ulf), | |
53235f22 DW |
926 | .i_type = XLOG_REG_TYPE_UNMOUNT, |
927 | }; | |
928 | struct xfs_log_vec vec = { | |
929 | .lv_niovecs = 1, | |
930 | .lv_iovecp = ®, | |
931 | }; | |
3c702f95 DC |
932 | |
933 | /* account for space used by record data */ | |
934 | ticket->t_curr_res -= sizeof(ulf); | |
eef983ff | 935 | |
3468bb1c | 936 | return xlog_write(log, &vec, ticket, NULL, NULL, XLOG_UNMOUNT_TRANS); |
3c702f95 DC |
937 | } |
938 | ||
939 | /* | |
940 | * Mark the filesystem clean by writing an unmount record to the head of the | |
941 | * log. | |
942 | */ | |
943 | static void | |
944 | xlog_unmount_write( | |
945 | struct xlog *log) | |
946 | { | |
947 | struct xfs_mount *mp = log->l_mp; | |
53235f22 DW |
948 | struct xlog_in_core *iclog; |
949 | struct xlog_ticket *tic = NULL; | |
53235f22 DW |
950 | int error; |
951 | ||
952 | error = xfs_log_reserve(mp, 600, 1, &tic, XFS_LOG, 0); | |
953 | if (error) | |
954 | goto out_err; | |
955 | ||
3468bb1c | 956 | error = xlog_write_unmount_record(log, tic); |
53235f22 DW |
957 | /* |
958 | * At this point, we're umounting anyway, so there's no point in | |
5112e206 | 959 | * transitioning log state to shutdown. Just continue... |
53235f22 DW |
960 | */ |
961 | out_err: | |
962 | if (error) | |
963 | xfs_alert(mp, "%s: unmount record failed", __func__); | |
964 | ||
965 | spin_lock(&log->l_icloglock); | |
966 | iclog = log->l_iclog; | |
45eddb41 | 967 | error = xlog_force_iclog(iclog); |
81e5b50a | 968 | xlog_wait_on_iclog(iclog); |
53235f22 DW |
969 | |
970 | if (tic) { | |
971 | trace_xfs_log_umount_write(log, tic); | |
8b41e3f9 | 972 | xfs_log_ticket_ungrant(log, tic); |
53235f22 DW |
973 | } |
974 | } | |
975 | ||
13859c98 CH |
976 | static void |
977 | xfs_log_unmount_verify_iclog( | |
978 | struct xlog *log) | |
979 | { | |
980 | struct xlog_in_core *iclog = log->l_iclog; | |
981 | ||
982 | do { | |
983 | ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE); | |
984 | ASSERT(iclog->ic_offset == 0); | |
985 | } while ((iclog = iclog->ic_next) != log->l_iclog); | |
986 | } | |
987 | ||
1da177e4 LT |
988 | /* |
989 | * Unmount record used to have a string "Unmount filesystem--" in the | |
990 | * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE). | |
991 | * We just write the magic number now since that particular field isn't | |
8e159e72 | 992 | * currently architecture converted and "Unmount" is a bit foo. |
1da177e4 LT |
993 | * As far as I know, there weren't any dependencies on the old behaviour. |
994 | */ | |
550319e9 | 995 | static void |
13859c98 CH |
996 | xfs_log_unmount_write( |
997 | struct xfs_mount *mp) | |
1da177e4 | 998 | { |
13859c98 | 999 | struct xlog *log = mp->m_log; |
1da177e4 | 1000 | |
50d25484 | 1001 | if (!xfs_log_writable(mp)) |
550319e9 | 1002 | return; |
1da177e4 | 1003 | |
550319e9 | 1004 | xfs_log_force(mp, XFS_LOG_SYNC); |
1da177e4 | 1005 | |
2039a272 | 1006 | if (xlog_is_shutdown(log)) |
6178d104 | 1007 | return; |
5cc3c006 DW |
1008 | |
1009 | /* | |
1010 | * If we think the summary counters are bad, avoid writing the unmount | |
1011 | * record to force log recovery at next mount, after which the summary | |
1012 | * counters will be recalculated. Refer to xlog_check_unmount_rec for | |
1013 | * more details. | |
1014 | */ | |
1015 | if (XFS_TEST_ERROR(xfs_fs_has_sickness(mp, XFS_SICK_FS_COUNTERS), mp, | |
1016 | XFS_ERRTAG_FORCE_SUMMARY_RECALC)) { | |
1017 | xfs_alert(mp, "%s: will fix summary counters at next mount", | |
1018 | __func__); | |
1019 | return; | |
1020 | } | |
1021 | ||
13859c98 | 1022 | xfs_log_unmount_verify_iclog(log); |
3c702f95 | 1023 | xlog_unmount_write(log); |
550319e9 | 1024 | } |
1da177e4 LT |
1025 | |
1026 | /* | |
c75921a7 | 1027 | * Empty the log for unmount/freeze. |
cf2931db DC |
1028 | * |
1029 | * To do this, we first need to shut down the background log work so it is not | |
1030 | * trying to cover the log as we clean up. We then need to unpin all objects in | |
1031 | * the log so we can then flush them out. Once they have completed their IO and | |
303591a0 | 1032 | * run the callbacks removing themselves from the AIL, we can cover the log. |
1da177e4 | 1033 | */ |
303591a0 | 1034 | int |
c75921a7 DC |
1035 | xfs_log_quiesce( |
1036 | struct xfs_mount *mp) | |
1da177e4 | 1037 | { |
908ce71e DW |
1038 | /* |
1039 | * Clear log incompat features since we're quiescing the log. Report | |
1040 | * failures, though it's not fatal to have a higher log feature | |
1041 | * protection level than the log contents actually require. | |
1042 | */ | |
1043 | if (xfs_clear_incompat_log_features(mp)) { | |
1044 | int error; | |
1045 | ||
1046 | error = xfs_sync_sb(mp, false); | |
1047 | if (error) | |
1048 | xfs_warn(mp, | |
1049 | "Failed to clear log incompat features on quiesce"); | |
1050 | } | |
1051 | ||
f661f1e0 | 1052 | cancel_delayed_work_sync(&mp->m_log->l_work); |
cf2931db DC |
1053 | xfs_log_force(mp, XFS_LOG_SYNC); |
1054 | ||
1055 | /* | |
1056 | * The superblock buffer is uncached and while xfs_ail_push_all_sync() | |
8321ddb2 | 1057 | * will push it, xfs_buftarg_wait() will not wait for it. Further, |
cf2931db DC |
1058 | * xfs_buf_iowait() cannot be used because it was pushed with the |
1059 | * XBF_ASYNC flag set, so we need to use a lock/unlock pair to wait for | |
1060 | * the IO to complete. | |
1061 | */ | |
1062 | xfs_ail_push_all_sync(mp->m_ail); | |
8321ddb2 | 1063 | xfs_buftarg_wait(mp->m_ddev_targp); |
cf2931db DC |
1064 | xfs_buf_lock(mp->m_sb_bp); |
1065 | xfs_buf_unlock(mp->m_sb_bp); | |
303591a0 BF |
1066 | |
1067 | return xfs_log_cover(mp); | |
9e54ee0f | 1068 | } |
cf2931db | 1069 | |
9e54ee0f BF |
1070 | void |
1071 | xfs_log_clean( | |
1072 | struct xfs_mount *mp) | |
1073 | { | |
1074 | xfs_log_quiesce(mp); | |
cf2931db | 1075 | xfs_log_unmount_write(mp); |
c75921a7 DC |
1076 | } |
1077 | ||
1078 | /* | |
1079 | * Shut down and release the AIL and Log. | |
1080 | * | |
1081 | * During unmount, we need to ensure we flush all the dirty metadata objects | |
1082 | * from the AIL so that the log is empty before we write the unmount record to | |
1083 | * the log. Once this is done, we can tear down the AIL and the log. | |
1084 | */ | |
1085 | void | |
1086 | xfs_log_unmount( | |
1087 | struct xfs_mount *mp) | |
1088 | { | |
9e54ee0f | 1089 | xfs_log_clean(mp); |
cf2931db | 1090 | |
8321ddb2 BF |
1091 | xfs_buftarg_drain(mp->m_ddev_targp); |
1092 | ||
249a8c11 | 1093 | xfs_trans_ail_destroy(mp); |
baff4e44 BF |
1094 | |
1095 | xfs_sysfs_del(&mp->m_log->l_kobj); | |
1096 | ||
c41564b5 | 1097 | xlog_dealloc_log(mp->m_log); |
1da177e4 LT |
1098 | } |
1099 | ||
43f5efc5 DC |
1100 | void |
1101 | xfs_log_item_init( | |
1102 | struct xfs_mount *mp, | |
1103 | struct xfs_log_item *item, | |
1104 | int type, | |
272e42b2 | 1105 | const struct xfs_item_ops *ops) |
43f5efc5 DC |
1106 | { |
1107 | item->li_mountp = mp; | |
1108 | item->li_ailp = mp->m_ail; | |
1109 | item->li_type = type; | |
1110 | item->li_ops = ops; | |
71e330b5 DC |
1111 | item->li_lv = NULL; |
1112 | ||
1113 | INIT_LIST_HEAD(&item->li_ail); | |
1114 | INIT_LIST_HEAD(&item->li_cil); | |
643c8c05 | 1115 | INIT_LIST_HEAD(&item->li_bio_list); |
e6631f85 | 1116 | INIT_LIST_HEAD(&item->li_trans); |
43f5efc5 DC |
1117 | } |
1118 | ||
09a423a3 CH |
1119 | /* |
1120 | * Wake up processes waiting for log space after we have moved the log tail. | |
09a423a3 | 1121 | */ |
1da177e4 | 1122 | void |
09a423a3 | 1123 | xfs_log_space_wake( |
cfb7cdca | 1124 | struct xfs_mount *mp) |
1da177e4 | 1125 | { |
ad223e60 | 1126 | struct xlog *log = mp->m_log; |
cfb7cdca | 1127 | int free_bytes; |
1da177e4 | 1128 | |
2039a272 | 1129 | if (xlog_is_shutdown(log)) |
1da177e4 | 1130 | return; |
1da177e4 | 1131 | |
28496968 | 1132 | if (!list_empty_careful(&log->l_write_head.waiters)) { |
e1d06e5f | 1133 | ASSERT(!xlog_in_recovery(log)); |
09a423a3 | 1134 | |
28496968 CH |
1135 | spin_lock(&log->l_write_head.lock); |
1136 | free_bytes = xlog_space_left(log, &log->l_write_head.grant); | |
e179840d | 1137 | xlog_grant_head_wake(log, &log->l_write_head, &free_bytes); |
28496968 | 1138 | spin_unlock(&log->l_write_head.lock); |
1da177e4 | 1139 | } |
10547941 | 1140 | |
28496968 | 1141 | if (!list_empty_careful(&log->l_reserve_head.waiters)) { |
e1d06e5f | 1142 | ASSERT(!xlog_in_recovery(log)); |
09a423a3 | 1143 | |
28496968 CH |
1144 | spin_lock(&log->l_reserve_head.lock); |
1145 | free_bytes = xlog_space_left(log, &log->l_reserve_head.grant); | |
e179840d | 1146 | xlog_grant_head_wake(log, &log->l_reserve_head, &free_bytes); |
28496968 | 1147 | spin_unlock(&log->l_reserve_head.lock); |
1da177e4 | 1148 | } |
3f16b985 | 1149 | } |
1da177e4 LT |
1150 | |
1151 | /* | |
2c6e24ce DC |
1152 | * Determine if we have a transaction that has gone to disk that needs to be |
1153 | * covered. To begin the transition to the idle state firstly the log needs to | |
1154 | * be idle. That means the CIL, the AIL and the iclogs needs to be empty before | |
1155 | * we start attempting to cover the log. | |
b6f8dd49 | 1156 | * |
2c6e24ce DC |
1157 | * Only if we are then in a state where covering is needed, the caller is |
1158 | * informed that dummy transactions are required to move the log into the idle | |
1159 | * state. | |
1160 | * | |
1161 | * If there are any items in the AIl or CIL, then we do not want to attempt to | |
1162 | * cover the log as we may be in a situation where there isn't log space | |
1163 | * available to run a dummy transaction and this can lead to deadlocks when the | |
1164 | * tail of the log is pinned by an item that is modified in the CIL. Hence | |
1165 | * there's no point in running a dummy transaction at this point because we | |
1166 | * can't start trying to idle the log until both the CIL and AIL are empty. | |
1da177e4 | 1167 | */ |
37444fc4 BF |
1168 | static bool |
1169 | xfs_log_need_covered( | |
1170 | struct xfs_mount *mp) | |
1da177e4 | 1171 | { |
37444fc4 BF |
1172 | struct xlog *log = mp->m_log; |
1173 | bool needed = false; | |
1da177e4 | 1174 | |
2c6e24ce | 1175 | if (!xlog_cil_empty(log)) |
8646b982 | 1176 | return false; |
2c6e24ce | 1177 | |
b22cd72c | 1178 | spin_lock(&log->l_icloglock); |
b6f8dd49 DC |
1179 | switch (log->l_covered_state) { |
1180 | case XLOG_STATE_COVER_DONE: | |
1181 | case XLOG_STATE_COVER_DONE2: | |
1182 | case XLOG_STATE_COVER_IDLE: | |
1183 | break; | |
1184 | case XLOG_STATE_COVER_NEED: | |
1185 | case XLOG_STATE_COVER_NEED2: | |
2c6e24ce DC |
1186 | if (xfs_ail_min_lsn(log->l_ailp)) |
1187 | break; | |
1188 | if (!xlog_iclogs_empty(log)) | |
1189 | break; | |
1190 | ||
37444fc4 | 1191 | needed = true; |
2c6e24ce DC |
1192 | if (log->l_covered_state == XLOG_STATE_COVER_NEED) |
1193 | log->l_covered_state = XLOG_STATE_COVER_DONE; | |
1194 | else | |
1195 | log->l_covered_state = XLOG_STATE_COVER_DONE2; | |
1196 | break; | |
b6f8dd49 | 1197 | default: |
37444fc4 | 1198 | needed = true; |
b6f8dd49 | 1199 | break; |
1da177e4 | 1200 | } |
b22cd72c | 1201 | spin_unlock(&log->l_icloglock); |
014c2544 | 1202 | return needed; |
1da177e4 LT |
1203 | } |
1204 | ||
303591a0 BF |
1205 | /* |
1206 | * Explicitly cover the log. This is similar to background log covering but | |
1207 | * intended for usage in quiesce codepaths. The caller is responsible to ensure | |
1208 | * the log is idle and suitable for covering. The CIL, iclog buffers and AIL | |
1209 | * must all be empty. | |
1210 | */ | |
1211 | static int | |
1212 | xfs_log_cover( | |
1213 | struct xfs_mount *mp) | |
1214 | { | |
303591a0 | 1215 | int error = 0; |
f46e5a17 | 1216 | bool need_covered; |
303591a0 | 1217 | |
4533fc63 BF |
1218 | ASSERT((xlog_cil_empty(mp->m_log) && xlog_iclogs_empty(mp->m_log) && |
1219 | !xfs_ail_min_lsn(mp->m_log->l_ailp)) || | |
2039a272 | 1220 | xlog_is_shutdown(mp->m_log)); |
303591a0 BF |
1221 | |
1222 | if (!xfs_log_writable(mp)) | |
1223 | return 0; | |
1224 | ||
f46e5a17 BF |
1225 | /* |
1226 | * xfs_log_need_covered() is not idempotent because it progresses the | |
1227 | * state machine if the log requires covering. Therefore, we must call | |
1228 | * this function once and use the result until we've issued an sb sync. | |
1229 | * Do so first to make that abundantly clear. | |
1230 | * | |
1231 | * Fall into the covering sequence if the log needs covering or the | |
1232 | * mount has lazy superblock accounting to sync to disk. The sb sync | |
1233 | * used for covering accumulates the in-core counters, so covering | |
1234 | * handles this for us. | |
1235 | */ | |
1236 | need_covered = xfs_log_need_covered(mp); | |
1237 | if (!need_covered && !xfs_sb_version_haslazysbcount(&mp->m_sb)) | |
1238 | return 0; | |
1239 | ||
303591a0 BF |
1240 | /* |
1241 | * To cover the log, commit the superblock twice (at most) in | |
1242 | * independent checkpoints. The first serves as a reference for the | |
1243 | * tail pointer. The sync transaction and AIL push empties the AIL and | |
1244 | * updates the in-core tail to the LSN of the first checkpoint. The | |
1245 | * second commit updates the on-disk tail with the in-core LSN, | |
1246 | * covering the log. Push the AIL one more time to leave it empty, as | |
1247 | * we found it. | |
1248 | */ | |
f46e5a17 | 1249 | do { |
303591a0 BF |
1250 | error = xfs_sync_sb(mp, true); |
1251 | if (error) | |
1252 | break; | |
1253 | xfs_ail_push_all_sync(mp->m_ail); | |
f46e5a17 | 1254 | } while (xfs_log_need_covered(mp)); |
303591a0 BF |
1255 | |
1256 | return error; | |
1257 | } | |
1258 | ||
09a423a3 | 1259 | /* |
1da177e4 LT |
1260 | * We may be holding the log iclog lock upon entering this routine. |
1261 | */ | |
1262 | xfs_lsn_t | |
1c304625 | 1263 | xlog_assign_tail_lsn_locked( |
1c3cb9ec | 1264 | struct xfs_mount *mp) |
1da177e4 | 1265 | { |
ad223e60 | 1266 | struct xlog *log = mp->m_log; |
1c304625 CH |
1267 | struct xfs_log_item *lip; |
1268 | xfs_lsn_t tail_lsn; | |
1269 | ||
57e80956 | 1270 | assert_spin_locked(&mp->m_ail->ail_lock); |
1da177e4 | 1271 | |
09a423a3 CH |
1272 | /* |
1273 | * To make sure we always have a valid LSN for the log tail we keep | |
1274 | * track of the last LSN which was committed in log->l_last_sync_lsn, | |
1c304625 | 1275 | * and use that when the AIL was empty. |
09a423a3 | 1276 | */ |
1c304625 CH |
1277 | lip = xfs_ail_min(mp->m_ail); |
1278 | if (lip) | |
1279 | tail_lsn = lip->li_lsn; | |
1280 | else | |
84f3c683 | 1281 | tail_lsn = atomic64_read(&log->l_last_sync_lsn); |
750b9c90 | 1282 | trace_xfs_log_assign_tail_lsn(log, tail_lsn); |
1c3cb9ec | 1283 | atomic64_set(&log->l_tail_lsn, tail_lsn); |
1da177e4 | 1284 | return tail_lsn; |
1c3cb9ec | 1285 | } |
1da177e4 | 1286 | |
1c304625 CH |
1287 | xfs_lsn_t |
1288 | xlog_assign_tail_lsn( | |
1289 | struct xfs_mount *mp) | |
1290 | { | |
1291 | xfs_lsn_t tail_lsn; | |
1292 | ||
57e80956 | 1293 | spin_lock(&mp->m_ail->ail_lock); |
1c304625 | 1294 | tail_lsn = xlog_assign_tail_lsn_locked(mp); |
57e80956 | 1295 | spin_unlock(&mp->m_ail->ail_lock); |
1c304625 CH |
1296 | |
1297 | return tail_lsn; | |
1298 | } | |
1299 | ||
1da177e4 LT |
1300 | /* |
1301 | * Return the space in the log between the tail and the head. The head | |
1302 | * is passed in the cycle/bytes formal parms. In the special case where | |
1303 | * the reserve head has wrapped passed the tail, this calculation is no | |
1304 | * longer valid. In this case, just return 0 which means there is no space | |
1305 | * in the log. This works for all places where this function is called | |
1306 | * with the reserve head. Of course, if the write head were to ever | |
1307 | * wrap the tail, we should blow up. Rather than catch this case here, | |
1308 | * we depend on other ASSERTions in other parts of the code. XXXmiken | |
1309 | * | |
1310 | * This code also handles the case where the reservation head is behind | |
1311 | * the tail. The details of this case are described below, but the end | |
1312 | * result is that we return the size of the log as the amount of space left. | |
1313 | */ | |
a8272ce0 | 1314 | STATIC int |
a69ed03c | 1315 | xlog_space_left( |
ad223e60 | 1316 | struct xlog *log, |
c8a09ff8 | 1317 | atomic64_t *head) |
1da177e4 | 1318 | { |
a69ed03c DC |
1319 | int free_bytes; |
1320 | int tail_bytes; | |
1321 | int tail_cycle; | |
1322 | int head_cycle; | |
1323 | int head_bytes; | |
1da177e4 | 1324 | |
a69ed03c | 1325 | xlog_crack_grant_head(head, &head_cycle, &head_bytes); |
1c3cb9ec DC |
1326 | xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_bytes); |
1327 | tail_bytes = BBTOB(tail_bytes); | |
a69ed03c DC |
1328 | if (tail_cycle == head_cycle && head_bytes >= tail_bytes) |
1329 | free_bytes = log->l_logsize - (head_bytes - tail_bytes); | |
1330 | else if (tail_cycle + 1 < head_cycle) | |
1da177e4 | 1331 | return 0; |
a69ed03c DC |
1332 | else if (tail_cycle < head_cycle) { |
1333 | ASSERT(tail_cycle == (head_cycle - 1)); | |
1334 | free_bytes = tail_bytes - head_bytes; | |
1da177e4 LT |
1335 | } else { |
1336 | /* | |
1337 | * The reservation head is behind the tail. | |
1338 | * In this case we just want to return the size of the | |
1339 | * log as the amount of space left. | |
1340 | */ | |
f41febd2 | 1341 | xfs_alert(log->l_mp, "xlog_space_left: head behind tail"); |
a0fa2b67 | 1342 | xfs_alert(log->l_mp, |
f41febd2 JP |
1343 | " tail_cycle = %d, tail_bytes = %d", |
1344 | tail_cycle, tail_bytes); | |
1345 | xfs_alert(log->l_mp, | |
1346 | " GH cycle = %d, GH bytes = %d", | |
1347 | head_cycle, head_bytes); | |
1da177e4 LT |
1348 | ASSERT(0); |
1349 | free_bytes = log->l_logsize; | |
1350 | } | |
1351 | return free_bytes; | |
a69ed03c | 1352 | } |
1da177e4 LT |
1353 | |
1354 | ||
0d5a75e9 | 1355 | static void |
79b54d9b CH |
1356 | xlog_ioend_work( |
1357 | struct work_struct *work) | |
1da177e4 | 1358 | { |
79b54d9b CH |
1359 | struct xlog_in_core *iclog = |
1360 | container_of(work, struct xlog_in_core, ic_end_io_work); | |
1361 | struct xlog *log = iclog->ic_log; | |
79b54d9b | 1362 | int error; |
1da177e4 | 1363 | |
79b54d9b | 1364 | error = blk_status_to_errno(iclog->ic_bio.bi_status); |
366fc4b8 CH |
1365 | #ifdef DEBUG |
1366 | /* treat writes with injected CRC errors as failed */ | |
1367 | if (iclog->ic_fail_crc) | |
79b54d9b | 1368 | error = -EIO; |
366fc4b8 CH |
1369 | #endif |
1370 | ||
1da177e4 | 1371 | /* |
366fc4b8 | 1372 | * Race to shutdown the filesystem if we see an error. |
1da177e4 | 1373 | */ |
79b54d9b CH |
1374 | if (XFS_TEST_ERROR(error, log->l_mp, XFS_ERRTAG_IODONE_IOERR)) { |
1375 | xfs_alert(log->l_mp, "log I/O error %d", error); | |
1376 | xfs_force_shutdown(log->l_mp, SHUTDOWN_LOG_IO_ERROR); | |
1da177e4 | 1377 | } |
3db296f3 | 1378 | |
12e6a0f4 | 1379 | xlog_state_done_syncing(iclog); |
79b54d9b | 1380 | bio_uninit(&iclog->ic_bio); |
9c23eccc | 1381 | |
3db296f3 | 1382 | /* |
79b54d9b CH |
1383 | * Drop the lock to signal that we are done. Nothing references the |
1384 | * iclog after this, so an unmount waiting on this lock can now tear it | |
1385 | * down safely. As such, it is unsafe to reference the iclog after the | |
1386 | * unlock as we could race with it being freed. | |
3db296f3 | 1387 | */ |
79b54d9b | 1388 | up(&iclog->ic_sema); |
c3f8fc73 | 1389 | } |
1da177e4 | 1390 | |
1da177e4 LT |
1391 | /* |
1392 | * Return size of each in-core log record buffer. | |
1393 | * | |
9da096fd | 1394 | * All machines get 8 x 32kB buffers by default, unless tuned otherwise. |
1da177e4 LT |
1395 | * |
1396 | * If the filesystem blocksize is too large, we may need to choose a | |
1397 | * larger size since the directory code currently logs entire blocks. | |
1398 | */ | |
1da177e4 | 1399 | STATIC void |
9a8d2fdb MT |
1400 | xlog_get_iclog_buffer_size( |
1401 | struct xfs_mount *mp, | |
1402 | struct xlog *log) | |
1da177e4 | 1403 | { |
1cb51258 | 1404 | if (mp->m_logbufs <= 0) |
4f62282a CH |
1405 | mp->m_logbufs = XLOG_MAX_ICLOGS; |
1406 | if (mp->m_logbsize <= 0) | |
1407 | mp->m_logbsize = XLOG_BIG_RECORD_BSIZE; | |
1408 | ||
1409 | log->l_iclog_bufs = mp->m_logbufs; | |
1410 | log->l_iclog_size = mp->m_logbsize; | |
1da177e4 LT |
1411 | |
1412 | /* | |
4f62282a | 1413 | * # headers = size / 32k - one header holds cycles from 32k of data. |
1da177e4 | 1414 | */ |
4f62282a CH |
1415 | log->l_iclog_heads = |
1416 | DIV_ROUND_UP(mp->m_logbsize, XLOG_HEADER_CYCLE_SIZE); | |
1417 | log->l_iclog_hsize = log->l_iclog_heads << BBSHIFT; | |
1418 | } | |
1da177e4 | 1419 | |
f661f1e0 DC |
1420 | void |
1421 | xfs_log_work_queue( | |
1422 | struct xfs_mount *mp) | |
1423 | { | |
696a5620 | 1424 | queue_delayed_work(mp->m_sync_workqueue, &mp->m_log->l_work, |
f661f1e0 DC |
1425 | msecs_to_jiffies(xfs_syncd_centisecs * 10)); |
1426 | } | |
1427 | ||
2b73a2c8 DW |
1428 | /* |
1429 | * Clear the log incompat flags if we have the opportunity. | |
1430 | * | |
1431 | * This only happens if we're about to log the second dummy transaction as part | |
1432 | * of covering the log and we can get the log incompat feature usage lock. | |
1433 | */ | |
1434 | static inline void | |
1435 | xlog_clear_incompat( | |
1436 | struct xlog *log) | |
1437 | { | |
1438 | struct xfs_mount *mp = log->l_mp; | |
1439 | ||
1440 | if (!xfs_sb_has_incompat_log_feature(&mp->m_sb, | |
1441 | XFS_SB_FEAT_INCOMPAT_LOG_ALL)) | |
1442 | return; | |
1443 | ||
1444 | if (log->l_covered_state != XLOG_STATE_COVER_DONE2) | |
1445 | return; | |
1446 | ||
1447 | if (!down_write_trylock(&log->l_incompat_users)) | |
1448 | return; | |
1449 | ||
1450 | xfs_clear_incompat_log_features(mp); | |
1451 | up_write(&log->l_incompat_users); | |
1452 | } | |
1453 | ||
f661f1e0 DC |
1454 | /* |
1455 | * Every sync period we need to unpin all items in the AIL and push them to | |
1456 | * disk. If there is nothing dirty, then we might need to cover the log to | |
1457 | * indicate that the filesystem is idle. | |
1458 | */ | |
0d5a75e9 | 1459 | static void |
f661f1e0 DC |
1460 | xfs_log_worker( |
1461 | struct work_struct *work) | |
1462 | { | |
1463 | struct xlog *log = container_of(to_delayed_work(work), | |
1464 | struct xlog, l_work); | |
1465 | struct xfs_mount *mp = log->l_mp; | |
1466 | ||
1467 | /* dgc: errors ignored - not fatal and nowhere to report them */ | |
37444fc4 | 1468 | if (xfs_fs_writable(mp, SB_FREEZE_WRITE) && xfs_log_need_covered(mp)) { |
61e63ecb DC |
1469 | /* |
1470 | * Dump a transaction into the log that contains no real change. | |
1471 | * This is needed to stamp the current tail LSN into the log | |
1472 | * during the covering operation. | |
1473 | * | |
1474 | * We cannot use an inode here for this - that will push dirty | |
1475 | * state back up into the VFS and then periodic inode flushing | |
1476 | * will prevent log covering from making progress. Hence we | |
1477 | * synchronously log the superblock instead to ensure the | |
1478 | * superblock is immediately unpinned and can be written back. | |
1479 | */ | |
2b73a2c8 | 1480 | xlog_clear_incompat(log); |
61e63ecb DC |
1481 | xfs_sync_sb(mp, true); |
1482 | } else | |
f661f1e0 DC |
1483 | xfs_log_force(mp, 0); |
1484 | ||
1485 | /* start pushing all the metadata that is currently dirty */ | |
1486 | xfs_ail_push_all(mp->m_ail); | |
1487 | ||
1488 | /* queue us up again */ | |
1489 | xfs_log_work_queue(mp); | |
1490 | } | |
1491 | ||
1da177e4 LT |
1492 | /* |
1493 | * This routine initializes some of the log structure for a given mount point. | |
1494 | * Its primary purpose is to fill in enough, so recovery can occur. However, | |
1495 | * some other stuff may be filled in too. | |
1496 | */ | |
9a8d2fdb MT |
1497 | STATIC struct xlog * |
1498 | xlog_alloc_log( | |
1499 | struct xfs_mount *mp, | |
1500 | struct xfs_buftarg *log_target, | |
1501 | xfs_daddr_t blk_offset, | |
1502 | int num_bblks) | |
1da177e4 | 1503 | { |
9a8d2fdb | 1504 | struct xlog *log; |
1da177e4 LT |
1505 | xlog_rec_header_t *head; |
1506 | xlog_in_core_t **iclogp; | |
1507 | xlog_in_core_t *iclog, *prev_iclog=NULL; | |
1da177e4 | 1508 | int i; |
2451337d | 1509 | int error = -ENOMEM; |
69ce58f0 | 1510 | uint log2_size = 0; |
1da177e4 | 1511 | |
9a8d2fdb | 1512 | log = kmem_zalloc(sizeof(struct xlog), KM_MAYFAIL); |
a6cb767e | 1513 | if (!log) { |
a0fa2b67 | 1514 | xfs_warn(mp, "Log allocation failed: No memory!"); |
a6cb767e DC |
1515 | goto out; |
1516 | } | |
1da177e4 LT |
1517 | |
1518 | log->l_mp = mp; | |
1519 | log->l_targ = log_target; | |
1520 | log->l_logsize = BBTOB(num_bblks); | |
1521 | log->l_logBBstart = blk_offset; | |
1522 | log->l_logBBsize = num_bblks; | |
1523 | log->l_covered_state = XLOG_STATE_COVER_IDLE; | |
e1d06e5f | 1524 | set_bit(XLOG_ACTIVE_RECOVERY, &log->l_opstate); |
f661f1e0 | 1525 | INIT_DELAYED_WORK(&log->l_work, xfs_log_worker); |
1da177e4 LT |
1526 | |
1527 | log->l_prev_block = -1; | |
1da177e4 | 1528 | /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */ |
1c3cb9ec DC |
1529 | xlog_assign_atomic_lsn(&log->l_tail_lsn, 1, 0); |
1530 | xlog_assign_atomic_lsn(&log->l_last_sync_lsn, 1, 0); | |
1da177e4 | 1531 | log->l_curr_cycle = 1; /* 0 is bad since this is initial value */ |
c303c5b8 | 1532 | |
a6a65fef DC |
1533 | if (xfs_sb_version_haslogv2(&mp->m_sb) && mp->m_sb.sb_logsunit > 1) |
1534 | log->l_iclog_roundoff = mp->m_sb.sb_logsunit; | |
1535 | else | |
1536 | log->l_iclog_roundoff = BBSIZE; | |
1537 | ||
c303c5b8 CH |
1538 | xlog_grant_head_init(&log->l_reserve_head); |
1539 | xlog_grant_head_init(&log->l_write_head); | |
1da177e4 | 1540 | |
2451337d | 1541 | error = -EFSCORRUPTED; |
62118709 | 1542 | if (xfs_sb_version_hassector(&mp->m_sb)) { |
69ce58f0 AE |
1543 | log2_size = mp->m_sb.sb_logsectlog; |
1544 | if (log2_size < BBSHIFT) { | |
a0fa2b67 DC |
1545 | xfs_warn(mp, "Log sector size too small (0x%x < 0x%x)", |
1546 | log2_size, BBSHIFT); | |
a6cb767e DC |
1547 | goto out_free_log; |
1548 | } | |
1549 | ||
69ce58f0 AE |
1550 | log2_size -= BBSHIFT; |
1551 | if (log2_size > mp->m_sectbb_log) { | |
a0fa2b67 DC |
1552 | xfs_warn(mp, "Log sector size too large (0x%x > 0x%x)", |
1553 | log2_size, mp->m_sectbb_log); | |
a6cb767e DC |
1554 | goto out_free_log; |
1555 | } | |
69ce58f0 AE |
1556 | |
1557 | /* for larger sector sizes, must have v2 or external log */ | |
1558 | if (log2_size && log->l_logBBstart > 0 && | |
1559 | !xfs_sb_version_haslogv2(&mp->m_sb)) { | |
a0fa2b67 DC |
1560 | xfs_warn(mp, |
1561 | "log sector size (0x%x) invalid for configuration.", | |
1562 | log2_size); | |
a6cb767e DC |
1563 | goto out_free_log; |
1564 | } | |
1da177e4 | 1565 | } |
69ce58f0 | 1566 | log->l_sectBBsize = 1 << log2_size; |
1da177e4 | 1567 | |
2b73a2c8 DW |
1568 | init_rwsem(&log->l_incompat_users); |
1569 | ||
1da177e4 LT |
1570 | xlog_get_iclog_buffer_size(mp, log); |
1571 | ||
007c61c6 | 1572 | spin_lock_init(&log->l_icloglock); |
eb40a875 | 1573 | init_waitqueue_head(&log->l_flush_wait); |
1da177e4 | 1574 | |
1da177e4 LT |
1575 | iclogp = &log->l_iclog; |
1576 | /* | |
1577 | * The amount of memory to allocate for the iclog structure is | |
1578 | * rather funky due to the way the structure is defined. It is | |
1579 | * done this way so that we can use different sizes for machines | |
1580 | * with different amounts of memory. See the definition of | |
1581 | * xlog_in_core_t in xfs_log_priv.h for details. | |
1582 | */ | |
1da177e4 | 1583 | ASSERT(log->l_iclog_size >= 4096); |
79b54d9b | 1584 | for (i = 0; i < log->l_iclog_bufs; i++) { |
89b171ac CH |
1585 | size_t bvec_size = howmany(log->l_iclog_size, PAGE_SIZE) * |
1586 | sizeof(struct bio_vec); | |
79b54d9b CH |
1587 | |
1588 | iclog = kmem_zalloc(sizeof(*iclog) + bvec_size, KM_MAYFAIL); | |
1589 | if (!iclog) | |
644c3567 DC |
1590 | goto out_free_iclog; |
1591 | ||
79b54d9b | 1592 | *iclogp = iclog; |
1da177e4 LT |
1593 | iclog->ic_prev = prev_iclog; |
1594 | prev_iclog = iclog; | |
1fa40b01 | 1595 | |
d634525d DC |
1596 | iclog->ic_data = kvzalloc(log->l_iclog_size, |
1597 | GFP_KERNEL | __GFP_RETRY_MAYFAIL); | |
79b54d9b | 1598 | if (!iclog->ic_data) |
644c3567 | 1599 | goto out_free_iclog; |
4679b2d3 | 1600 | #ifdef DEBUG |
5809d5e0 | 1601 | log->l_iclog_bak[i] = &iclog->ic_header; |
4679b2d3 | 1602 | #endif |
1da177e4 LT |
1603 | head = &iclog->ic_header; |
1604 | memset(head, 0, sizeof(xlog_rec_header_t)); | |
b53e675d CH |
1605 | head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM); |
1606 | head->h_version = cpu_to_be32( | |
62118709 | 1607 | xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1); |
b53e675d | 1608 | head->h_size = cpu_to_be32(log->l_iclog_size); |
1da177e4 | 1609 | /* new fields */ |
b53e675d | 1610 | head->h_fmt = cpu_to_be32(XLOG_FMT); |
1da177e4 LT |
1611 | memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t)); |
1612 | ||
79b54d9b | 1613 | iclog->ic_size = log->l_iclog_size - log->l_iclog_hsize; |
1da177e4 LT |
1614 | iclog->ic_state = XLOG_STATE_ACTIVE; |
1615 | iclog->ic_log = log; | |
114d23aa | 1616 | atomic_set(&iclog->ic_refcnt, 0); |
89ae379d | 1617 | INIT_LIST_HEAD(&iclog->ic_callbacks); |
b28708d6 | 1618 | iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize; |
1da177e4 | 1619 | |
eb40a875 DC |
1620 | init_waitqueue_head(&iclog->ic_force_wait); |
1621 | init_waitqueue_head(&iclog->ic_write_wait); | |
79b54d9b CH |
1622 | INIT_WORK(&iclog->ic_end_io_work, xlog_ioend_work); |
1623 | sema_init(&iclog->ic_sema, 1); | |
1da177e4 LT |
1624 | |
1625 | iclogp = &iclog->ic_next; | |
1626 | } | |
1627 | *iclogp = log->l_iclog; /* complete ring */ | |
1628 | log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */ | |
1629 | ||
1058d0f5 | 1630 | log->l_ioend_workqueue = alloc_workqueue("xfs-log/%s", |
05a302a1 DW |
1631 | XFS_WQFLAGS(WQ_FREEZABLE | WQ_MEM_RECLAIM | |
1632 | WQ_HIGHPRI), | |
1633 | 0, mp->m_super->s_id); | |
1058d0f5 CH |
1634 | if (!log->l_ioend_workqueue) |
1635 | goto out_free_iclog; | |
1636 | ||
71e330b5 DC |
1637 | error = xlog_cil_init(log); |
1638 | if (error) | |
1058d0f5 | 1639 | goto out_destroy_workqueue; |
1da177e4 | 1640 | return log; |
644c3567 | 1641 | |
1058d0f5 CH |
1642 | out_destroy_workqueue: |
1643 | destroy_workqueue(log->l_ioend_workqueue); | |
644c3567 DC |
1644 | out_free_iclog: |
1645 | for (iclog = log->l_iclog; iclog; iclog = prev_iclog) { | |
1646 | prev_iclog = iclog->ic_next; | |
79b54d9b | 1647 | kmem_free(iclog->ic_data); |
644c3567 | 1648 | kmem_free(iclog); |
798a9cad BF |
1649 | if (prev_iclog == log->l_iclog) |
1650 | break; | |
644c3567 | 1651 | } |
644c3567 DC |
1652 | out_free_log: |
1653 | kmem_free(log); | |
a6cb767e | 1654 | out: |
2451337d | 1655 | return ERR_PTR(error); |
1da177e4 LT |
1656 | } /* xlog_alloc_log */ |
1657 | ||
1da177e4 LT |
1658 | /* |
1659 | * Write out the commit record of a transaction associated with the given | |
f10e925d | 1660 | * ticket to close off a running log write. Return the lsn of the commit record. |
1da177e4 | 1661 | */ |
f10e925d | 1662 | int |
55b66332 | 1663 | xlog_commit_record( |
ad223e60 | 1664 | struct xlog *log, |
55b66332 DC |
1665 | struct xlog_ticket *ticket, |
1666 | struct xlog_in_core **iclog, | |
f10e925d | 1667 | xfs_lsn_t *lsn) |
1da177e4 | 1668 | { |
55b66332 DC |
1669 | struct xfs_log_iovec reg = { |
1670 | .i_addr = NULL, | |
1671 | .i_len = 0, | |
1672 | .i_type = XLOG_REG_TYPE_COMMIT, | |
1673 | }; | |
1674 | struct xfs_log_vec vec = { | |
1675 | .lv_niovecs = 1, | |
1676 | .lv_iovecp = ®, | |
1677 | }; | |
f10e925d | 1678 | int error; |
1da177e4 | 1679 | |
2039a272 | 1680 | if (xlog_is_shutdown(log)) |
f10e925d DC |
1681 | return -EIO; |
1682 | ||
3468bb1c | 1683 | error = xlog_write(log, &vec, ticket, lsn, iclog, XLOG_COMMIT_TRANS); |
55b66332 | 1684 | if (error) |
f10e925d | 1685 | xfs_force_shutdown(log->l_mp, SHUTDOWN_LOG_IO_ERROR); |
014c2544 | 1686 | return error; |
55b66332 | 1687 | } |
1da177e4 LT |
1688 | |
1689 | /* | |
ed1575da DW |
1690 | * Compute the LSN that we'd need to push the log tail towards in order to have |
1691 | * (a) enough on-disk log space to log the number of bytes specified, (b) at | |
1692 | * least 25% of the log space free, and (c) at least 256 blocks free. If the | |
1693 | * log free space already meets all three thresholds, this function returns | |
1694 | * NULLCOMMITLSN. | |
1da177e4 | 1695 | */ |
ed1575da DW |
1696 | xfs_lsn_t |
1697 | xlog_grant_push_threshold( | |
ad223e60 | 1698 | struct xlog *log, |
2ced19cb | 1699 | int need_bytes) |
1da177e4 | 1700 | { |
2ced19cb | 1701 | xfs_lsn_t threshold_lsn = 0; |
84f3c683 | 1702 | xfs_lsn_t last_sync_lsn; |
2ced19cb DC |
1703 | int free_blocks; |
1704 | int free_bytes; | |
1705 | int threshold_block; | |
1706 | int threshold_cycle; | |
1707 | int free_threshold; | |
1708 | ||
1709 | ASSERT(BTOBB(need_bytes) < log->l_logBBsize); | |
1710 | ||
28496968 | 1711 | free_bytes = xlog_space_left(log, &log->l_reserve_head.grant); |
2ced19cb DC |
1712 | free_blocks = BTOBBT(free_bytes); |
1713 | ||
1714 | /* | |
1715 | * Set the threshold for the minimum number of free blocks in the | |
1716 | * log to the maximum of what the caller needs, one quarter of the | |
1717 | * log, and 256 blocks. | |
1718 | */ | |
1719 | free_threshold = BTOBB(need_bytes); | |
9bb54cb5 DC |
1720 | free_threshold = max(free_threshold, (log->l_logBBsize >> 2)); |
1721 | free_threshold = max(free_threshold, 256); | |
2ced19cb | 1722 | if (free_blocks >= free_threshold) |
ed1575da | 1723 | return NULLCOMMITLSN; |
2ced19cb | 1724 | |
1c3cb9ec DC |
1725 | xlog_crack_atomic_lsn(&log->l_tail_lsn, &threshold_cycle, |
1726 | &threshold_block); | |
1727 | threshold_block += free_threshold; | |
1da177e4 | 1728 | if (threshold_block >= log->l_logBBsize) { |
2ced19cb DC |
1729 | threshold_block -= log->l_logBBsize; |
1730 | threshold_cycle += 1; | |
1da177e4 | 1731 | } |
2ced19cb DC |
1732 | threshold_lsn = xlog_assign_lsn(threshold_cycle, |
1733 | threshold_block); | |
1734 | /* | |
1735 | * Don't pass in an lsn greater than the lsn of the last | |
84f3c683 DC |
1736 | * log record known to be on disk. Use a snapshot of the last sync lsn |
1737 | * so that it doesn't change between the compare and the set. | |
1da177e4 | 1738 | */ |
84f3c683 DC |
1739 | last_sync_lsn = atomic64_read(&log->l_last_sync_lsn); |
1740 | if (XFS_LSN_CMP(threshold_lsn, last_sync_lsn) > 0) | |
1741 | threshold_lsn = last_sync_lsn; | |
2ced19cb | 1742 | |
ed1575da DW |
1743 | return threshold_lsn; |
1744 | } | |
1745 | ||
1746 | /* | |
1747 | * Push the tail of the log if we need to do so to maintain the free log space | |
1748 | * thresholds set out by xlog_grant_push_threshold. We may need to adopt a | |
1749 | * policy which pushes on an lsn which is further along in the log once we | |
1750 | * reach the high water mark. In this manner, we would be creating a low water | |
1751 | * mark. | |
1752 | */ | |
1753 | STATIC void | |
1754 | xlog_grant_push_ail( | |
1755 | struct xlog *log, | |
1756 | int need_bytes) | |
1757 | { | |
1758 | xfs_lsn_t threshold_lsn; | |
1759 | ||
1760 | threshold_lsn = xlog_grant_push_threshold(log, need_bytes); | |
2039a272 | 1761 | if (threshold_lsn == NULLCOMMITLSN || xlog_is_shutdown(log)) |
ed1575da DW |
1762 | return; |
1763 | ||
2ced19cb DC |
1764 | /* |
1765 | * Get the transaction layer to kick the dirty buffers out to | |
1766 | * disk asynchronously. No point in trying to do this if | |
1767 | * the filesystem is shutting down. | |
1768 | */ | |
ed1575da | 1769 | xfs_ail_push(log->l_ailp, threshold_lsn); |
2ced19cb | 1770 | } |
1da177e4 | 1771 | |
0e446be4 CH |
1772 | /* |
1773 | * Stamp cycle number in every block | |
1774 | */ | |
1775 | STATIC void | |
1776 | xlog_pack_data( | |
1777 | struct xlog *log, | |
1778 | struct xlog_in_core *iclog, | |
1779 | int roundoff) | |
1780 | { | |
1781 | int i, j, k; | |
1782 | int size = iclog->ic_offset + roundoff; | |
1783 | __be32 cycle_lsn; | |
b2a922cd | 1784 | char *dp; |
0e446be4 CH |
1785 | |
1786 | cycle_lsn = CYCLE_LSN_DISK(iclog->ic_header.h_lsn); | |
1787 | ||
1788 | dp = iclog->ic_datap; | |
1789 | for (i = 0; i < BTOBB(size); i++) { | |
1790 | if (i >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) | |
1791 | break; | |
1792 | iclog->ic_header.h_cycle_data[i] = *(__be32 *)dp; | |
1793 | *(__be32 *)dp = cycle_lsn; | |
1794 | dp += BBSIZE; | |
1795 | } | |
1796 | ||
1797 | if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) { | |
1798 | xlog_in_core_2_t *xhdr = iclog->ic_data; | |
1799 | ||
1800 | for ( ; i < BTOBB(size); i++) { | |
1801 | j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE); | |
1802 | k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE); | |
1803 | xhdr[j].hic_xheader.xh_cycle_data[k] = *(__be32 *)dp; | |
1804 | *(__be32 *)dp = cycle_lsn; | |
1805 | dp += BBSIZE; | |
1806 | } | |
1807 | ||
1808 | for (i = 1; i < log->l_iclog_heads; i++) | |
1809 | xhdr[i].hic_xheader.xh_cycle = cycle_lsn; | |
1810 | } | |
1811 | } | |
1812 | ||
1813 | /* | |
1814 | * Calculate the checksum for a log buffer. | |
1815 | * | |
1816 | * This is a little more complicated than it should be because the various | |
1817 | * headers and the actual data are non-contiguous. | |
1818 | */ | |
f9668a09 | 1819 | __le32 |
0e446be4 CH |
1820 | xlog_cksum( |
1821 | struct xlog *log, | |
1822 | struct xlog_rec_header *rhead, | |
1823 | char *dp, | |
1824 | int size) | |
1825 | { | |
c8ce540d | 1826 | uint32_t crc; |
0e446be4 CH |
1827 | |
1828 | /* first generate the crc for the record header ... */ | |
cae028df | 1829 | crc = xfs_start_cksum_update((char *)rhead, |
0e446be4 CH |
1830 | sizeof(struct xlog_rec_header), |
1831 | offsetof(struct xlog_rec_header, h_crc)); | |
1832 | ||
1833 | /* ... then for additional cycle data for v2 logs ... */ | |
1834 | if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) { | |
1835 | union xlog_in_core2 *xhdr = (union xlog_in_core2 *)rhead; | |
1836 | int i; | |
a3f20014 | 1837 | int xheads; |
0e446be4 | 1838 | |
0c771b99 | 1839 | xheads = DIV_ROUND_UP(size, XLOG_HEADER_CYCLE_SIZE); |
0e446be4 | 1840 | |
a3f20014 | 1841 | for (i = 1; i < xheads; i++) { |
0e446be4 CH |
1842 | crc = crc32c(crc, &xhdr[i].hic_xheader, |
1843 | sizeof(struct xlog_rec_ext_header)); | |
1844 | } | |
1845 | } | |
1846 | ||
1847 | /* ... and finally for the payload */ | |
1848 | crc = crc32c(crc, dp, size); | |
1849 | ||
1850 | return xfs_end_cksum(crc); | |
1851 | } | |
1852 | ||
79b54d9b CH |
1853 | static void |
1854 | xlog_bio_end_io( | |
1855 | struct bio *bio) | |
1856 | { | |
1857 | struct xlog_in_core *iclog = bio->bi_private; | |
1858 | ||
1058d0f5 | 1859 | queue_work(iclog->ic_log->l_ioend_workqueue, |
79b54d9b CH |
1860 | &iclog->ic_end_io_work); |
1861 | } | |
1862 | ||
842a42d1 | 1863 | static int |
79b54d9b CH |
1864 | xlog_map_iclog_data( |
1865 | struct bio *bio, | |
1866 | void *data, | |
1867 | size_t count) | |
1868 | { | |
1869 | do { | |
1870 | struct page *page = kmem_to_page(data); | |
1871 | unsigned int off = offset_in_page(data); | |
1872 | size_t len = min_t(size_t, count, PAGE_SIZE - off); | |
1873 | ||
842a42d1 BF |
1874 | if (bio_add_page(bio, page, len, off) != len) |
1875 | return -EIO; | |
79b54d9b CH |
1876 | |
1877 | data += len; | |
1878 | count -= len; | |
1879 | } while (count); | |
842a42d1 BF |
1880 | |
1881 | return 0; | |
79b54d9b CH |
1882 | } |
1883 | ||
94860a30 CH |
1884 | STATIC void |
1885 | xlog_write_iclog( | |
1886 | struct xlog *log, | |
1887 | struct xlog_in_core *iclog, | |
94860a30 | 1888 | uint64_t bno, |
eef983ff | 1889 | unsigned int count) |
873ff550 | 1890 | { |
94860a30 | 1891 | ASSERT(bno < log->l_logBBsize); |
956f6daa | 1892 | trace_xlog_iclog_write(iclog, _RET_IP_); |
94860a30 CH |
1893 | |
1894 | /* | |
1895 | * We lock the iclogbufs here so that we can serialise against I/O | |
1896 | * completion during unmount. We might be processing a shutdown | |
1897 | * triggered during unmount, and that can occur asynchronously to the | |
1898 | * unmount thread, and hence we need to ensure that completes before | |
1899 | * tearing down the iclogbufs. Hence we need to hold the buffer lock | |
1900 | * across the log IO to archieve that. | |
1901 | */ | |
79b54d9b | 1902 | down(&iclog->ic_sema); |
5112e206 | 1903 | if (xlog_is_shutdown(log)) { |
873ff550 CH |
1904 | /* |
1905 | * It would seem logical to return EIO here, but we rely on | |
1906 | * the log state machine to propagate I/O errors instead of | |
79b54d9b CH |
1907 | * doing it here. We kick of the state machine and unlock |
1908 | * the buffer manually, the code needs to be kept in sync | |
1909 | * with the I/O completion path. | |
873ff550 | 1910 | */ |
12e6a0f4 | 1911 | xlog_state_done_syncing(iclog); |
79b54d9b | 1912 | up(&iclog->ic_sema); |
94860a30 | 1913 | return; |
873ff550 CH |
1914 | } |
1915 | ||
79b54d9b CH |
1916 | bio_init(&iclog->ic_bio, iclog->ic_bvec, howmany(count, PAGE_SIZE)); |
1917 | bio_set_dev(&iclog->ic_bio, log->l_targ->bt_bdev); | |
1918 | iclog->ic_bio.bi_iter.bi_sector = log->l_logBBstart + bno; | |
1919 | iclog->ic_bio.bi_end_io = xlog_bio_end_io; | |
1920 | iclog->ic_bio.bi_private = iclog; | |
2def2845 DC |
1921 | |
1922 | /* | |
1923 | * We use REQ_SYNC | REQ_IDLE here to tell the block layer the are more | |
1924 | * IOs coming immediately after this one. This prevents the block layer | |
1925 | * writeback throttle from throttling log writes behind background | |
1926 | * metadata writeback and causing priority inversions. | |
1927 | */ | |
eef983ff | 1928 | iclog->ic_bio.bi_opf = REQ_OP_WRITE | REQ_META | REQ_SYNC | REQ_IDLE; |
b5d721ea | 1929 | if (iclog->ic_flags & XLOG_ICL_NEED_FLUSH) { |
79b54d9b | 1930 | iclog->ic_bio.bi_opf |= REQ_PREFLUSH; |
b5d721ea DC |
1931 | /* |
1932 | * For external log devices, we also need to flush the data | |
1933 | * device cache first to ensure all metadata writeback covered | |
1934 | * by the LSN in this iclog is on stable storage. This is slow, | |
1935 | * but it *must* complete before we issue the external log IO. | |
1936 | */ | |
1937 | if (log->l_targ != log->l_mp->m_ddev_targp) | |
1938 | blkdev_issue_flush(log->l_mp->m_ddev_targp->bt_bdev); | |
1939 | } | |
eef983ff DC |
1940 | if (iclog->ic_flags & XLOG_ICL_NEED_FUA) |
1941 | iclog->ic_bio.bi_opf |= REQ_FUA; | |
b5d721ea | 1942 | |
eef983ff | 1943 | iclog->ic_flags &= ~(XLOG_ICL_NEED_FLUSH | XLOG_ICL_NEED_FUA); |
79b54d9b | 1944 | |
842a42d1 BF |
1945 | if (xlog_map_iclog_data(&iclog->ic_bio, iclog->ic_data, count)) { |
1946 | xfs_force_shutdown(log->l_mp, SHUTDOWN_LOG_IO_ERROR); | |
1947 | return; | |
1948 | } | |
79b54d9b | 1949 | if (is_vmalloc_addr(iclog->ic_data)) |
2c68a1df | 1950 | flush_kernel_vmap_range(iclog->ic_data, count); |
79b54d9b CH |
1951 | |
1952 | /* | |
1953 | * If this log buffer would straddle the end of the log we will have | |
1954 | * to split it up into two bios, so that we can continue at the start. | |
1955 | */ | |
1956 | if (bno + BTOBB(count) > log->l_logBBsize) { | |
1957 | struct bio *split; | |
1958 | ||
1959 | split = bio_split(&iclog->ic_bio, log->l_logBBsize - bno, | |
1960 | GFP_NOIO, &fs_bio_set); | |
1961 | bio_chain(split, &iclog->ic_bio); | |
1962 | submit_bio(split); | |
1963 | ||
1964 | /* restart at logical offset zero for the remainder */ | |
1965 | iclog->ic_bio.bi_iter.bi_sector = log->l_logBBstart; | |
1966 | } | |
1967 | ||
1968 | submit_bio(&iclog->ic_bio); | |
873ff550 | 1969 | } |
1da177e4 | 1970 | |
56933848 CH |
1971 | /* |
1972 | * We need to bump cycle number for the part of the iclog that is | |
1973 | * written to the start of the log. Watch out for the header magic | |
1974 | * number case, though. | |
1975 | */ | |
79b54d9b | 1976 | static void |
56933848 CH |
1977 | xlog_split_iclog( |
1978 | struct xlog *log, | |
1979 | void *data, | |
1980 | uint64_t bno, | |
1981 | unsigned int count) | |
1982 | { | |
1983 | unsigned int split_offset = BBTOB(log->l_logBBsize - bno); | |
1984 | unsigned int i; | |
1985 | ||
1986 | for (i = split_offset; i < count; i += BBSIZE) { | |
1987 | uint32_t cycle = get_unaligned_be32(data + i); | |
1988 | ||
1989 | if (++cycle == XLOG_HEADER_MAGIC_NUM) | |
1990 | cycle++; | |
1991 | put_unaligned_be32(cycle, data + i); | |
1992 | } | |
56933848 CH |
1993 | } |
1994 | ||
db0a6faf CH |
1995 | static int |
1996 | xlog_calc_iclog_size( | |
1997 | struct xlog *log, | |
1998 | struct xlog_in_core *iclog, | |
1999 | uint32_t *roundoff) | |
2000 | { | |
2001 | uint32_t count_init, count; | |
db0a6faf CH |
2002 | |
2003 | /* Add for LR header */ | |
2004 | count_init = log->l_iclog_hsize + iclog->ic_offset; | |
a6a65fef | 2005 | count = roundup(count_init, log->l_iclog_roundoff); |
db0a6faf | 2006 | |
db0a6faf CH |
2007 | *roundoff = count - count_init; |
2008 | ||
a6a65fef DC |
2009 | ASSERT(count >= count_init); |
2010 | ASSERT(*roundoff < log->l_iclog_roundoff); | |
db0a6faf CH |
2011 | return count; |
2012 | } | |
2013 | ||
1da177e4 LT |
2014 | /* |
2015 | * Flush out the in-core log (iclog) to the on-disk log in an asynchronous | |
2016 | * fashion. Previously, we should have moved the current iclog | |
2017 | * ptr in the log to point to the next available iclog. This allows further | |
2018 | * write to continue while this code syncs out an iclog ready to go. | |
2019 | * Before an in-core log can be written out, the data section must be scanned | |
2020 | * to save away the 1st word of each BBSIZE block into the header. We replace | |
2021 | * it with the current cycle count. Each BBSIZE block is tagged with the | |
2022 | * cycle count because there in an implicit assumption that drives will | |
2023 | * guarantee that entire 512 byte blocks get written at once. In other words, | |
2024 | * we can't have part of a 512 byte block written and part not written. By | |
2025 | * tagging each block, we will know which blocks are valid when recovering | |
2026 | * after an unclean shutdown. | |
2027 | * | |
2028 | * This routine is single threaded on the iclog. No other thread can be in | |
2029 | * this routine with the same iclog. Changing contents of iclog can there- | |
2030 | * fore be done without grabbing the state machine lock. Updating the global | |
2031 | * log will require grabbing the lock though. | |
2032 | * | |
2033 | * The entire log manager uses a logical block numbering scheme. Only | |
94860a30 CH |
2034 | * xlog_write_iclog knows about the fact that the log may not start with |
2035 | * block zero on a given device. | |
1da177e4 | 2036 | */ |
94860a30 | 2037 | STATIC void |
9a8d2fdb MT |
2038 | xlog_sync( |
2039 | struct xlog *log, | |
2040 | struct xlog_in_core *iclog) | |
1da177e4 | 2041 | { |
db0a6faf CH |
2042 | unsigned int count; /* byte count of bwrite */ |
2043 | unsigned int roundoff; /* roundoff to BB or stripe */ | |
2044 | uint64_t bno; | |
db0a6faf | 2045 | unsigned int size; |
1da177e4 | 2046 | |
155cc6b7 | 2047 | ASSERT(atomic_read(&iclog->ic_refcnt) == 0); |
956f6daa | 2048 | trace_xlog_iclog_sync(iclog, _RET_IP_); |
1da177e4 | 2049 | |
db0a6faf | 2050 | count = xlog_calc_iclog_size(log, iclog, &roundoff); |
1da177e4 LT |
2051 | |
2052 | /* move grant heads by roundoff in sync */ | |
28496968 CH |
2053 | xlog_grant_add_space(log, &log->l_reserve_head.grant, roundoff); |
2054 | xlog_grant_add_space(log, &log->l_write_head.grant, roundoff); | |
1da177e4 LT |
2055 | |
2056 | /* put cycle number in every block */ | |
2057 | xlog_pack_data(log, iclog, roundoff); | |
2058 | ||
2059 | /* real byte length */ | |
0e446be4 | 2060 | size = iclog->ic_offset; |
db0a6faf | 2061 | if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) |
0e446be4 CH |
2062 | size += roundoff; |
2063 | iclog->ic_header.h_len = cpu_to_be32(size); | |
1da177e4 | 2064 | |
9b0489c1 | 2065 | XFS_STATS_INC(log->l_mp, xs_log_writes); |
ff6d6af2 | 2066 | XFS_STATS_ADD(log->l_mp, xs_log_blocks, BTOBB(count)); |
1da177e4 | 2067 | |
94860a30 CH |
2068 | bno = BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn)); |
2069 | ||
1da177e4 | 2070 | /* Do we need to split this write into 2 parts? */ |
eef983ff | 2071 | if (bno + BTOBB(count) > log->l_logBBsize) |
79b54d9b | 2072 | xlog_split_iclog(log, &iclog->ic_header, bno, count); |
0e446be4 CH |
2073 | |
2074 | /* calculcate the checksum */ | |
2075 | iclog->ic_header.h_crc = xlog_cksum(log, &iclog->ic_header, | |
2076 | iclog->ic_datap, size); | |
609adfc2 BF |
2077 | /* |
2078 | * Intentionally corrupt the log record CRC based on the error injection | |
2079 | * frequency, if defined. This facilitates testing log recovery in the | |
2080 | * event of torn writes. Hence, set the IOABORT state to abort the log | |
2081 | * write on I/O completion and shutdown the fs. The subsequent mount | |
2082 | * detects the bad CRC and attempts to recover. | |
2083 | */ | |
366fc4b8 | 2084 | #ifdef DEBUG |
3e88a007 | 2085 | if (XFS_TEST_ERROR(false, log->l_mp, XFS_ERRTAG_LOG_BAD_CRC)) { |
e2a64192 | 2086 | iclog->ic_header.h_crc &= cpu_to_le32(0xAAAAAAAA); |
366fc4b8 | 2087 | iclog->ic_fail_crc = true; |
609adfc2 BF |
2088 | xfs_warn(log->l_mp, |
2089 | "Intentionally corrupted log record at LSN 0x%llx. Shutdown imminent.", | |
2090 | be64_to_cpu(iclog->ic_header.h_lsn)); | |
2091 | } | |
366fc4b8 | 2092 | #endif |
abca1f33 | 2093 | xlog_verify_iclog(log, iclog, count); |
eef983ff | 2094 | xlog_write_iclog(log, iclog, bno, count); |
94860a30 | 2095 | } |
1da177e4 | 2096 | |
1da177e4 | 2097 | /* |
c41564b5 | 2098 | * Deallocate a log structure |
1da177e4 | 2099 | */ |
a8272ce0 | 2100 | STATIC void |
9a8d2fdb MT |
2101 | xlog_dealloc_log( |
2102 | struct xlog *log) | |
1da177e4 LT |
2103 | { |
2104 | xlog_in_core_t *iclog, *next_iclog; | |
1da177e4 LT |
2105 | int i; |
2106 | ||
71e330b5 DC |
2107 | xlog_cil_destroy(log); |
2108 | ||
44396476 | 2109 | /* |
9c23eccc DC |
2110 | * Cycle all the iclogbuf locks to make sure all log IO completion |
2111 | * is done before we tear down these buffers. | |
2112 | */ | |
2113 | iclog = log->l_iclog; | |
2114 | for (i = 0; i < log->l_iclog_bufs; i++) { | |
79b54d9b CH |
2115 | down(&iclog->ic_sema); |
2116 | up(&iclog->ic_sema); | |
9c23eccc DC |
2117 | iclog = iclog->ic_next; |
2118 | } | |
2119 | ||
1da177e4 | 2120 | iclog = log->l_iclog; |
9c23eccc | 2121 | for (i = 0; i < log->l_iclog_bufs; i++) { |
1da177e4 | 2122 | next_iclog = iclog->ic_next; |
79b54d9b | 2123 | kmem_free(iclog->ic_data); |
f0e2d93c | 2124 | kmem_free(iclog); |
1da177e4 LT |
2125 | iclog = next_iclog; |
2126 | } | |
1da177e4 | 2127 | |
1da177e4 | 2128 | log->l_mp->m_log = NULL; |
1058d0f5 | 2129 | destroy_workqueue(log->l_ioend_workqueue); |
f0e2d93c | 2130 | kmem_free(log); |
b843299b | 2131 | } |
1da177e4 LT |
2132 | |
2133 | /* | |
2134 | * Update counters atomically now that memcpy is done. | |
2135 | */ | |
1da177e4 | 2136 | static inline void |
9a8d2fdb MT |
2137 | xlog_state_finish_copy( |
2138 | struct xlog *log, | |
2139 | struct xlog_in_core *iclog, | |
2140 | int record_cnt, | |
2141 | int copy_bytes) | |
1da177e4 | 2142 | { |
390aab0a | 2143 | lockdep_assert_held(&log->l_icloglock); |
1da177e4 | 2144 | |
413d57c9 | 2145 | be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt); |
1da177e4 | 2146 | iclog->ic_offset += copy_bytes; |
390aab0a | 2147 | } |
1da177e4 | 2148 | |
7e9c6396 TS |
2149 | /* |
2150 | * print out info relating to regions written which consume | |
2151 | * the reservation | |
2152 | */ | |
71e330b5 DC |
2153 | void |
2154 | xlog_print_tic_res( | |
2155 | struct xfs_mount *mp, | |
2156 | struct xlog_ticket *ticket) | |
7e9c6396 TS |
2157 | { |
2158 | uint i; | |
2159 | uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t); | |
2160 | ||
2161 | /* match with XLOG_REG_TYPE_* in xfs_log.h */ | |
5110cd82 | 2162 | #define REG_TYPE_STR(type, str) [XLOG_REG_TYPE_##type] = str |
d31d7185 | 2163 | static char *res_type_str[] = { |
5110cd82 DW |
2164 | REG_TYPE_STR(BFORMAT, "bformat"), |
2165 | REG_TYPE_STR(BCHUNK, "bchunk"), | |
2166 | REG_TYPE_STR(EFI_FORMAT, "efi_format"), | |
2167 | REG_TYPE_STR(EFD_FORMAT, "efd_format"), | |
2168 | REG_TYPE_STR(IFORMAT, "iformat"), | |
2169 | REG_TYPE_STR(ICORE, "icore"), | |
2170 | REG_TYPE_STR(IEXT, "iext"), | |
2171 | REG_TYPE_STR(IBROOT, "ibroot"), | |
2172 | REG_TYPE_STR(ILOCAL, "ilocal"), | |
2173 | REG_TYPE_STR(IATTR_EXT, "iattr_ext"), | |
2174 | REG_TYPE_STR(IATTR_BROOT, "iattr_broot"), | |
2175 | REG_TYPE_STR(IATTR_LOCAL, "iattr_local"), | |
2176 | REG_TYPE_STR(QFORMAT, "qformat"), | |
2177 | REG_TYPE_STR(DQUOT, "dquot"), | |
2178 | REG_TYPE_STR(QUOTAOFF, "quotaoff"), | |
2179 | REG_TYPE_STR(LRHEADER, "LR header"), | |
2180 | REG_TYPE_STR(UNMOUNT, "unmount"), | |
2181 | REG_TYPE_STR(COMMIT, "commit"), | |
2182 | REG_TYPE_STR(TRANSHDR, "trans header"), | |
d31d7185 DW |
2183 | REG_TYPE_STR(ICREATE, "inode create"), |
2184 | REG_TYPE_STR(RUI_FORMAT, "rui_format"), | |
2185 | REG_TYPE_STR(RUD_FORMAT, "rud_format"), | |
2186 | REG_TYPE_STR(CUI_FORMAT, "cui_format"), | |
2187 | REG_TYPE_STR(CUD_FORMAT, "cud_format"), | |
2188 | REG_TYPE_STR(BUI_FORMAT, "bui_format"), | |
2189 | REG_TYPE_STR(BUD_FORMAT, "bud_format"), | |
7e9c6396 | 2190 | }; |
d31d7185 | 2191 | BUILD_BUG_ON(ARRAY_SIZE(res_type_str) != XLOG_REG_TYPE_MAX + 1); |
5110cd82 | 2192 | #undef REG_TYPE_STR |
7e9c6396 | 2193 | |
7d2d5653 | 2194 | xfs_warn(mp, "ticket reservation summary:"); |
f41febd2 JP |
2195 | xfs_warn(mp, " unit res = %d bytes", |
2196 | ticket->t_unit_res); | |
2197 | xfs_warn(mp, " current res = %d bytes", | |
2198 | ticket->t_curr_res); | |
2199 | xfs_warn(mp, " total reg = %u bytes (o/flow = %u bytes)", | |
2200 | ticket->t_res_arr_sum, ticket->t_res_o_flow); | |
2201 | xfs_warn(mp, " ophdrs = %u (ophdr space = %u bytes)", | |
2202 | ticket->t_res_num_ophdrs, ophdr_spc); | |
2203 | xfs_warn(mp, " ophdr + reg = %u bytes", | |
2204 | ticket->t_res_arr_sum + ticket->t_res_o_flow + ophdr_spc); | |
2205 | xfs_warn(mp, " num regions = %u", | |
2206 | ticket->t_res_num); | |
7e9c6396 TS |
2207 | |
2208 | for (i = 0; i < ticket->t_res_num; i++) { | |
a0fa2b67 | 2209 | uint r_type = ticket->t_res_arr[i].r_type; |
08e96e1a | 2210 | xfs_warn(mp, "region[%u]: %s - %u bytes", i, |
7e9c6396 | 2211 | ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ? |
5110cd82 | 2212 | "bad-rtype" : res_type_str[r_type]), |
7e9c6396 TS |
2213 | ticket->t_res_arr[i].r_len); |
2214 | } | |
2215 | } | |
7e9c6396 | 2216 | |
d4ca1d55 BF |
2217 | /* |
2218 | * Print a summary of the transaction. | |
2219 | */ | |
2220 | void | |
2221 | xlog_print_trans( | |
e6631f85 | 2222 | struct xfs_trans *tp) |
d4ca1d55 | 2223 | { |
e6631f85 DC |
2224 | struct xfs_mount *mp = tp->t_mountp; |
2225 | struct xfs_log_item *lip; | |
d4ca1d55 BF |
2226 | |
2227 | /* dump core transaction and ticket info */ | |
2228 | xfs_warn(mp, "transaction summary:"); | |
2c8f6265 BF |
2229 | xfs_warn(mp, " log res = %d", tp->t_log_res); |
2230 | xfs_warn(mp, " log count = %d", tp->t_log_count); | |
2231 | xfs_warn(mp, " flags = 0x%x", tp->t_flags); | |
d4ca1d55 BF |
2232 | |
2233 | xlog_print_tic_res(mp, tp->t_ticket); | |
2234 | ||
2235 | /* dump each log item */ | |
e6631f85 | 2236 | list_for_each_entry(lip, &tp->t_items, li_trans) { |
d4ca1d55 BF |
2237 | struct xfs_log_vec *lv = lip->li_lv; |
2238 | struct xfs_log_iovec *vec; | |
2239 | int i; | |
2240 | ||
2241 | xfs_warn(mp, "log item: "); | |
2242 | xfs_warn(mp, " type = 0x%x", lip->li_type); | |
22525c17 | 2243 | xfs_warn(mp, " flags = 0x%lx", lip->li_flags); |
d4ca1d55 BF |
2244 | if (!lv) |
2245 | continue; | |
2246 | xfs_warn(mp, " niovecs = %d", lv->lv_niovecs); | |
2247 | xfs_warn(mp, " size = %d", lv->lv_size); | |
2248 | xfs_warn(mp, " bytes = %d", lv->lv_bytes); | |
2249 | xfs_warn(mp, " buf len = %d", lv->lv_buf_len); | |
2250 | ||
2251 | /* dump each iovec for the log item */ | |
2252 | vec = lv->lv_iovecp; | |
2253 | for (i = 0; i < lv->lv_niovecs; i++) { | |
2254 | int dumplen = min(vec->i_len, 32); | |
2255 | ||
2256 | xfs_warn(mp, " iovec[%d]", i); | |
2257 | xfs_warn(mp, " type = 0x%x", vec->i_type); | |
2258 | xfs_warn(mp, " len = %d", vec->i_len); | |
2259 | xfs_warn(mp, " first %d bytes of iovec[%d]:", dumplen, i); | |
244e3dea | 2260 | xfs_hex_dump(vec->i_addr, dumplen); |
d4ca1d55 BF |
2261 | |
2262 | vec++; | |
2263 | } | |
2264 | } | |
2265 | } | |
2266 | ||
b5203cd0 | 2267 | /* |
7ec94921 DC |
2268 | * Calculate the potential space needed by the log vector. We may need a start |
2269 | * record, and each region gets its own struct xlog_op_header and may need to be | |
2270 | * double word aligned. | |
b5203cd0 DC |
2271 | */ |
2272 | static int | |
2273 | xlog_write_calc_vec_length( | |
2274 | struct xlog_ticket *ticket, | |
7ec94921 | 2275 | struct xfs_log_vec *log_vector, |
3468bb1c | 2276 | uint optype) |
b5203cd0 | 2277 | { |
55b66332 | 2278 | struct xfs_log_vec *lv; |
3468bb1c | 2279 | int headers = 0; |
b5203cd0 DC |
2280 | int len = 0; |
2281 | int i; | |
2282 | ||
3468bb1c DC |
2283 | if (optype & XLOG_START_TRANS) |
2284 | headers++; | |
2285 | ||
55b66332 | 2286 | for (lv = log_vector; lv; lv = lv->lv_next) { |
fd63875c DC |
2287 | /* we don't write ordered log vectors */ |
2288 | if (lv->lv_buf_len == XFS_LOG_VEC_ORDERED) | |
2289 | continue; | |
2290 | ||
55b66332 DC |
2291 | headers += lv->lv_niovecs; |
2292 | ||
2293 | for (i = 0; i < lv->lv_niovecs; i++) { | |
2294 | struct xfs_log_iovec *vecp = &lv->lv_iovecp[i]; | |
b5203cd0 | 2295 | |
55b66332 DC |
2296 | len += vecp->i_len; |
2297 | xlog_tic_add_region(ticket, vecp->i_len, vecp->i_type); | |
2298 | } | |
b5203cd0 DC |
2299 | } |
2300 | ||
2301 | ticket->t_res_num_ophdrs += headers; | |
2302 | len += headers * sizeof(struct xlog_op_header); | |
2303 | ||
2304 | return len; | |
2305 | } | |
2306 | ||
7ec94921 | 2307 | static void |
b5203cd0 | 2308 | xlog_write_start_rec( |
e6b1f273 | 2309 | struct xlog_op_header *ophdr, |
b5203cd0 DC |
2310 | struct xlog_ticket *ticket) |
2311 | { | |
b5203cd0 DC |
2312 | ophdr->oh_tid = cpu_to_be32(ticket->t_tid); |
2313 | ophdr->oh_clientid = ticket->t_clientid; | |
2314 | ophdr->oh_len = 0; | |
2315 | ophdr->oh_flags = XLOG_START_TRANS; | |
2316 | ophdr->oh_res2 = 0; | |
b5203cd0 DC |
2317 | } |
2318 | ||
2319 | static xlog_op_header_t * | |
2320 | xlog_write_setup_ophdr( | |
ad223e60 | 2321 | struct xlog *log, |
e6b1f273 | 2322 | struct xlog_op_header *ophdr, |
b5203cd0 DC |
2323 | struct xlog_ticket *ticket, |
2324 | uint flags) | |
2325 | { | |
b5203cd0 DC |
2326 | ophdr->oh_tid = cpu_to_be32(ticket->t_tid); |
2327 | ophdr->oh_clientid = ticket->t_clientid; | |
2328 | ophdr->oh_res2 = 0; | |
2329 | ||
2330 | /* are we copying a commit or unmount record? */ | |
2331 | ophdr->oh_flags = flags; | |
2332 | ||
2333 | /* | |
2334 | * We've seen logs corrupted with bad transaction client ids. This | |
2335 | * makes sure that XFS doesn't generate them on. Turn this into an EIO | |
2336 | * and shut down the filesystem. | |
2337 | */ | |
2338 | switch (ophdr->oh_clientid) { | |
2339 | case XFS_TRANSACTION: | |
2340 | case XFS_VOLUME: | |
2341 | case XFS_LOG: | |
2342 | break; | |
2343 | default: | |
a0fa2b67 | 2344 | xfs_warn(log->l_mp, |
c9690043 | 2345 | "Bad XFS transaction clientid 0x%x in ticket "PTR_FMT, |
b5203cd0 DC |
2346 | ophdr->oh_clientid, ticket); |
2347 | return NULL; | |
2348 | } | |
2349 | ||
2350 | return ophdr; | |
2351 | } | |
2352 | ||
2353 | /* | |
2354 | * Set up the parameters of the region copy into the log. This has | |
2355 | * to handle region write split across multiple log buffers - this | |
2356 | * state is kept external to this function so that this code can | |
ac0e300f | 2357 | * be written in an obvious, self documenting manner. |
b5203cd0 DC |
2358 | */ |
2359 | static int | |
2360 | xlog_write_setup_copy( | |
2361 | struct xlog_ticket *ticket, | |
2362 | struct xlog_op_header *ophdr, | |
2363 | int space_available, | |
2364 | int space_required, | |
2365 | int *copy_off, | |
2366 | int *copy_len, | |
2367 | int *last_was_partial_copy, | |
2368 | int *bytes_consumed) | |
2369 | { | |
2370 | int still_to_copy; | |
2371 | ||
2372 | still_to_copy = space_required - *bytes_consumed; | |
2373 | *copy_off = *bytes_consumed; | |
2374 | ||
2375 | if (still_to_copy <= space_available) { | |
2376 | /* write of region completes here */ | |
2377 | *copy_len = still_to_copy; | |
2378 | ophdr->oh_len = cpu_to_be32(*copy_len); | |
2379 | if (*last_was_partial_copy) | |
2380 | ophdr->oh_flags |= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS); | |
2381 | *last_was_partial_copy = 0; | |
2382 | *bytes_consumed = 0; | |
2383 | return 0; | |
2384 | } | |
2385 | ||
2386 | /* partial write of region, needs extra log op header reservation */ | |
2387 | *copy_len = space_available; | |
2388 | ophdr->oh_len = cpu_to_be32(*copy_len); | |
2389 | ophdr->oh_flags |= XLOG_CONTINUE_TRANS; | |
2390 | if (*last_was_partial_copy) | |
2391 | ophdr->oh_flags |= XLOG_WAS_CONT_TRANS; | |
2392 | *bytes_consumed += *copy_len; | |
2393 | (*last_was_partial_copy)++; | |
2394 | ||
2395 | /* account for new log op header */ | |
2396 | ticket->t_curr_res -= sizeof(struct xlog_op_header); | |
2397 | ticket->t_res_num_ophdrs++; | |
2398 | ||
2399 | return sizeof(struct xlog_op_header); | |
2400 | } | |
2401 | ||
2402 | static int | |
2403 | xlog_write_copy_finish( | |
ad223e60 | 2404 | struct xlog *log, |
b5203cd0 DC |
2405 | struct xlog_in_core *iclog, |
2406 | uint flags, | |
2407 | int *record_cnt, | |
2408 | int *data_cnt, | |
2409 | int *partial_copy, | |
2410 | int *partial_copy_len, | |
2411 | int log_offset, | |
2412 | struct xlog_in_core **commit_iclog) | |
2413 | { | |
df732b29 CH |
2414 | int error; |
2415 | ||
b5203cd0 DC |
2416 | if (*partial_copy) { |
2417 | /* | |
2418 | * This iclog has already been marked WANT_SYNC by | |
2419 | * xlog_state_get_iclog_space. | |
2420 | */ | |
390aab0a | 2421 | spin_lock(&log->l_icloglock); |
b5203cd0 DC |
2422 | xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt); |
2423 | *record_cnt = 0; | |
2424 | *data_cnt = 0; | |
df732b29 | 2425 | goto release_iclog; |
b5203cd0 DC |
2426 | } |
2427 | ||
2428 | *partial_copy = 0; | |
2429 | *partial_copy_len = 0; | |
2430 | ||
2431 | if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) { | |
2432 | /* no more space in this iclog - push it. */ | |
390aab0a | 2433 | spin_lock(&log->l_icloglock); |
b5203cd0 DC |
2434 | xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt); |
2435 | *record_cnt = 0; | |
2436 | *data_cnt = 0; | |
2437 | ||
69363999 CH |
2438 | if (iclog->ic_state == XLOG_STATE_ACTIVE) |
2439 | xlog_state_switch_iclogs(log, iclog, 0); | |
2440 | else | |
2441 | ASSERT(iclog->ic_state == XLOG_STATE_WANT_SYNC || | |
5112e206 | 2442 | xlog_is_shutdown(log)); |
b5203cd0 | 2443 | if (!commit_iclog) |
df732b29 CH |
2444 | goto release_iclog; |
2445 | spin_unlock(&log->l_icloglock); | |
b5203cd0 DC |
2446 | ASSERT(flags & XLOG_COMMIT_TRANS); |
2447 | *commit_iclog = iclog; | |
2448 | } | |
2449 | ||
2450 | return 0; | |
df732b29 CH |
2451 | |
2452 | release_iclog: | |
0dc8f7f1 | 2453 | error = xlog_state_release_iclog(log, iclog, 0); |
df732b29 CH |
2454 | spin_unlock(&log->l_icloglock); |
2455 | return error; | |
b5203cd0 DC |
2456 | } |
2457 | ||
1da177e4 LT |
2458 | /* |
2459 | * Write some region out to in-core log | |
2460 | * | |
2461 | * This will be called when writing externally provided regions or when | |
2462 | * writing out a commit record for a given transaction. | |
2463 | * | |
2464 | * General algorithm: | |
2465 | * 1. Find total length of this write. This may include adding to the | |
2466 | * lengths passed in. | |
2467 | * 2. Check whether we violate the tickets reservation. | |
2468 | * 3. While writing to this iclog | |
2469 | * A. Reserve as much space in this iclog as can get | |
2470 | * B. If this is first write, save away start lsn | |
2471 | * C. While writing this region: | |
2472 | * 1. If first write of transaction, write start record | |
2473 | * 2. Write log operation header (header per region) | |
2474 | * 3. Find out if we can fit entire region into this iclog | |
2475 | * 4. Potentially, verify destination memcpy ptr | |
2476 | * 5. Memcpy (partial) region | |
2477 | * 6. If partial copy, release iclog; otherwise, continue | |
2478 | * copying more regions into current iclog | |
2479 | * 4. Mark want sync bit (in simulation mode) | |
2480 | * 5. Release iclog for potential flush to on-disk log. | |
2481 | * | |
2482 | * ERRORS: | |
2483 | * 1. Panic if reservation is overrun. This should never happen since | |
2484 | * reservation amounts are generated internal to the filesystem. | |
2485 | * NOTES: | |
2486 | * 1. Tickets are single threaded data structures. | |
2487 | * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the | |
2488 | * syncing routine. When a single log_write region needs to span | |
2489 | * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set | |
2490 | * on all log operation writes which don't contain the end of the | |
2491 | * region. The XLOG_END_TRANS bit is used for the in-core log | |
2492 | * operation which contains the end of the continued log_write region. | |
2493 | * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog, | |
2494 | * we don't really know exactly how much space will be used. As a result, | |
2495 | * we don't update ic_offset until the end when we know exactly how many | |
2496 | * bytes have been written out. | |
2497 | */ | |
71e330b5 | 2498 | int |
35a8a72f | 2499 | xlog_write( |
ad223e60 | 2500 | struct xlog *log, |
55b66332 | 2501 | struct xfs_log_vec *log_vector, |
35a8a72f CH |
2502 | struct xlog_ticket *ticket, |
2503 | xfs_lsn_t *start_lsn, | |
2504 | struct xlog_in_core **commit_iclog, | |
3468bb1c | 2505 | uint optype) |
1da177e4 | 2506 | { |
99428ad0 | 2507 | struct xlog_in_core *iclog = NULL; |
9590e9c6 DC |
2508 | struct xfs_log_vec *lv = log_vector; |
2509 | struct xfs_log_iovec *vecp = lv->lv_iovecp; | |
2510 | int index = 0; | |
99428ad0 | 2511 | int len; |
99428ad0 CH |
2512 | int partial_copy = 0; |
2513 | int partial_copy_len = 0; | |
2514 | int contwr = 0; | |
2515 | int record_cnt = 0; | |
2516 | int data_cnt = 0; | |
df732b29 | 2517 | int error = 0; |
99428ad0 | 2518 | |
93b8a585 | 2519 | /* |
9590e9c6 DC |
2520 | * If this is a commit or unmount transaction, we don't need a start |
2521 | * record to be written. We do, however, have to account for the | |
2522 | * commit or unmount header that gets written. Hence we always have | |
2523 | * to account for an extra xlog_op_header here. | |
93b8a585 | 2524 | */ |
9590e9c6 | 2525 | ticket->t_curr_res -= sizeof(struct xlog_op_header); |
7d2d5653 BF |
2526 | if (ticket->t_curr_res < 0) { |
2527 | xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES, | |
2528 | "ctx ticket reservation ran out. Need to up reservation"); | |
55b66332 | 2529 | xlog_print_tic_res(log->l_mp, ticket); |
7d2d5653 BF |
2530 | xfs_force_shutdown(log->l_mp, SHUTDOWN_LOG_IO_ERROR); |
2531 | } | |
1da177e4 | 2532 | |
3468bb1c DC |
2533 | len = xlog_write_calc_vec_length(ticket, log_vector, optype); |
2534 | if (start_lsn) | |
2535 | *start_lsn = 0; | |
fd63875c | 2536 | while (lv && (!lv->lv_niovecs || index < lv->lv_niovecs)) { |
e6b1f273 | 2537 | void *ptr; |
99428ad0 | 2538 | int log_offset; |
1da177e4 | 2539 | |
99428ad0 CH |
2540 | error = xlog_state_get_iclog_space(log, len, &iclog, ticket, |
2541 | &contwr, &log_offset); | |
2542 | if (error) | |
2543 | return error; | |
1da177e4 | 2544 | |
99428ad0 | 2545 | ASSERT(log_offset <= iclog->ic_size - 1); |
e6b1f273 | 2546 | ptr = iclog->ic_datap + log_offset; |
1da177e4 | 2547 | |
eef983ff | 2548 | /* Start_lsn is the first lsn written to. */ |
3468bb1c | 2549 | if (start_lsn && !*start_lsn) |
99428ad0 | 2550 | *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn); |
b5203cd0 | 2551 | |
99428ad0 CH |
2552 | /* |
2553 | * This loop writes out as many regions as can fit in the amount | |
2554 | * of space which was allocated by xlog_state_get_iclog_space(). | |
2555 | */ | |
fd63875c DC |
2556 | while (lv && (!lv->lv_niovecs || index < lv->lv_niovecs)) { |
2557 | struct xfs_log_iovec *reg; | |
99428ad0 | 2558 | struct xlog_op_header *ophdr; |
99428ad0 CH |
2559 | int copy_len; |
2560 | int copy_off; | |
fd63875c | 2561 | bool ordered = false; |
3468bb1c | 2562 | bool wrote_start_rec = false; |
fd63875c DC |
2563 | |
2564 | /* ordered log vectors have no regions to write */ | |
2565 | if (lv->lv_buf_len == XFS_LOG_VEC_ORDERED) { | |
2566 | ASSERT(lv->lv_niovecs == 0); | |
2567 | ordered = true; | |
2568 | goto next_lv; | |
2569 | } | |
99428ad0 | 2570 | |
fd63875c | 2571 | reg = &vecp[index]; |
c8ce540d DW |
2572 | ASSERT(reg->i_len % sizeof(int32_t) == 0); |
2573 | ASSERT((unsigned long)ptr % sizeof(int32_t) == 0); | |
99428ad0 | 2574 | |
7ec94921 DC |
2575 | /* |
2576 | * Before we start formatting log vectors, we need to | |
2577 | * write a start record. Only do this for the first | |
2578 | * iclog we write to. | |
2579 | */ | |
3468bb1c | 2580 | if (optype & XLOG_START_TRANS) { |
7ec94921 | 2581 | xlog_write_start_rec(ptr, ticket); |
e6b1f273 | 2582 | xlog_write_adv_cnt(&ptr, &len, &log_offset, |
7ec94921 | 2583 | sizeof(struct xlog_op_header)); |
3468bb1c DC |
2584 | optype &= ~XLOG_START_TRANS; |
2585 | wrote_start_rec = true; | |
99428ad0 | 2586 | } |
b5203cd0 | 2587 | |
3468bb1c | 2588 | ophdr = xlog_write_setup_ophdr(log, ptr, ticket, optype); |
99428ad0 | 2589 | if (!ophdr) |
2451337d | 2590 | return -EIO; |
99428ad0 | 2591 | |
e6b1f273 | 2592 | xlog_write_adv_cnt(&ptr, &len, &log_offset, |
99428ad0 CH |
2593 | sizeof(struct xlog_op_header)); |
2594 | ||
2595 | len += xlog_write_setup_copy(ticket, ophdr, | |
2596 | iclog->ic_size-log_offset, | |
55b66332 | 2597 | reg->i_len, |
99428ad0 CH |
2598 | ©_off, ©_len, |
2599 | &partial_copy, | |
2600 | &partial_copy_len); | |
2601 | xlog_verify_dest_ptr(log, ptr); | |
2602 | ||
91f9f5fe ES |
2603 | /* |
2604 | * Copy region. | |
2605 | * | |
2606 | * Unmount records just log an opheader, so can have | |
2607 | * empty payloads with no data region to copy. Hence we | |
2608 | * only copy the payload if the vector says it has data | |
2609 | * to copy. | |
2610 | */ | |
99428ad0 | 2611 | ASSERT(copy_len >= 0); |
91f9f5fe ES |
2612 | if (copy_len > 0) { |
2613 | memcpy(ptr, reg->i_addr + copy_off, copy_len); | |
2614 | xlog_write_adv_cnt(&ptr, &len, &log_offset, | |
2615 | copy_len); | |
2616 | } | |
7ec94921 | 2617 | copy_len += sizeof(struct xlog_op_header); |
99428ad0 | 2618 | record_cnt++; |
3468bb1c | 2619 | if (wrote_start_rec) { |
7ec94921 DC |
2620 | copy_len += sizeof(struct xlog_op_header); |
2621 | record_cnt++; | |
7ec94921 | 2622 | } |
99428ad0 CH |
2623 | data_cnt += contwr ? copy_len : 0; |
2624 | ||
3468bb1c | 2625 | error = xlog_write_copy_finish(log, iclog, optype, |
99428ad0 CH |
2626 | &record_cnt, &data_cnt, |
2627 | &partial_copy, | |
2628 | &partial_copy_len, | |
2629 | log_offset, | |
2630 | commit_iclog); | |
2631 | if (error) | |
2632 | return error; | |
2633 | ||
2634 | /* | |
2635 | * if we had a partial copy, we need to get more iclog | |
2636 | * space but we don't want to increment the region | |
2637 | * index because there is still more is this region to | |
2638 | * write. | |
2639 | * | |
2640 | * If we completed writing this region, and we flushed | |
2641 | * the iclog (indicated by resetting of the record | |
2642 | * count), then we also need to get more log space. If | |
2643 | * this was the last record, though, we are done and | |
2644 | * can just return. | |
2645 | */ | |
2646 | if (partial_copy) | |
2647 | break; | |
2648 | ||
55b66332 | 2649 | if (++index == lv->lv_niovecs) { |
fd63875c | 2650 | next_lv: |
55b66332 DC |
2651 | lv = lv->lv_next; |
2652 | index = 0; | |
2653 | if (lv) | |
2654 | vecp = lv->lv_iovecp; | |
2655 | } | |
749f24f3 | 2656 | if (record_cnt == 0 && !ordered) { |
55b66332 | 2657 | if (!lv) |
99428ad0 CH |
2658 | return 0; |
2659 | break; | |
2660 | } | |
2661 | } | |
2662 | } | |
2663 | ||
2664 | ASSERT(len == 0); | |
2665 | ||
390aab0a | 2666 | spin_lock(&log->l_icloglock); |
99428ad0 | 2667 | xlog_state_finish_copy(log, iclog, record_cnt, data_cnt); |
df732b29 | 2668 | if (commit_iclog) { |
3468bb1c | 2669 | ASSERT(optype & XLOG_COMMIT_TRANS); |
df732b29 CH |
2670 | *commit_iclog = iclog; |
2671 | } else { | |
0dc8f7f1 | 2672 | error = xlog_state_release_iclog(log, iclog, 0); |
df732b29 | 2673 | } |
390aab0a | 2674 | spin_unlock(&log->l_icloglock); |
1da177e4 | 2675 | |
df732b29 | 2676 | return error; |
99428ad0 | 2677 | } |
1da177e4 | 2678 | |
c814b4f2 CH |
2679 | static void |
2680 | xlog_state_activate_iclog( | |
2681 | struct xlog_in_core *iclog, | |
2682 | int *iclogs_changed) | |
2683 | { | |
2684 | ASSERT(list_empty_careful(&iclog->ic_callbacks)); | |
956f6daa | 2685 | trace_xlog_iclog_activate(iclog, _RET_IP_); |
c814b4f2 CH |
2686 | |
2687 | /* | |
2688 | * If the number of ops in this iclog indicate it just contains the | |
2689 | * dummy transaction, we can change state into IDLE (the second time | |
2690 | * around). Otherwise we should change the state into NEED a dummy. | |
2691 | * We don't need to cover the dummy. | |
2692 | */ | |
2693 | if (*iclogs_changed == 0 && | |
2694 | iclog->ic_header.h_num_logops == cpu_to_be32(XLOG_COVER_OPS)) { | |
2695 | *iclogs_changed = 1; | |
2696 | } else { | |
2697 | /* | |
2698 | * We have two dirty iclogs so start over. This could also be | |
2699 | * num of ops indicating this is not the dummy going out. | |
2700 | */ | |
2701 | *iclogs_changed = 2; | |
2702 | } | |
2703 | ||
2704 | iclog->ic_state = XLOG_STATE_ACTIVE; | |
2705 | iclog->ic_offset = 0; | |
2706 | iclog->ic_header.h_num_logops = 0; | |
2707 | memset(iclog->ic_header.h_cycle_data, 0, | |
2708 | sizeof(iclog->ic_header.h_cycle_data)); | |
2709 | iclog->ic_header.h_lsn = 0; | |
9d110014 | 2710 | iclog->ic_header.h_tail_lsn = 0; |
c814b4f2 CH |
2711 | } |
2712 | ||
0383f543 | 2713 | /* |
c814b4f2 CH |
2714 | * Loop through all iclogs and mark all iclogs currently marked DIRTY as |
2715 | * ACTIVE after iclog I/O has completed. | |
1da177e4 | 2716 | */ |
c814b4f2 CH |
2717 | static void |
2718 | xlog_state_activate_iclogs( | |
0383f543 | 2719 | struct xlog *log, |
c814b4f2 | 2720 | int *iclogs_changed) |
1da177e4 | 2721 | { |
c814b4f2 | 2722 | struct xlog_in_core *iclog = log->l_iclog; |
1da177e4 | 2723 | |
1da177e4 | 2724 | do { |
c814b4f2 CH |
2725 | if (iclog->ic_state == XLOG_STATE_DIRTY) |
2726 | xlog_state_activate_iclog(iclog, iclogs_changed); | |
2727 | /* | |
2728 | * The ordering of marking iclogs ACTIVE must be maintained, so | |
2729 | * an iclog doesn't become ACTIVE beyond one that is SYNCING. | |
2730 | */ | |
2731 | else if (iclog->ic_state != XLOG_STATE_ACTIVE) | |
2732 | break; | |
2733 | } while ((iclog = iclog->ic_next) != log->l_iclog); | |
2734 | } | |
0383f543 | 2735 | |
c814b4f2 CH |
2736 | static int |
2737 | xlog_covered_state( | |
2738 | int prev_state, | |
2739 | int iclogs_changed) | |
2740 | { | |
0383f543 | 2741 | /* |
b0eb9e11 BF |
2742 | * We go to NEED for any non-covering writes. We go to NEED2 if we just |
2743 | * wrote the first covering record (DONE). We go to IDLE if we just | |
2744 | * wrote the second covering record (DONE2) and remain in IDLE until a | |
2745 | * non-covering write occurs. | |
0383f543 | 2746 | */ |
c814b4f2 CH |
2747 | switch (prev_state) { |
2748 | case XLOG_STATE_COVER_IDLE: | |
b0eb9e11 BF |
2749 | if (iclogs_changed == 1) |
2750 | return XLOG_STATE_COVER_IDLE; | |
53004ee7 | 2751 | fallthrough; |
c814b4f2 CH |
2752 | case XLOG_STATE_COVER_NEED: |
2753 | case XLOG_STATE_COVER_NEED2: | |
2754 | break; | |
2755 | case XLOG_STATE_COVER_DONE: | |
2756 | if (iclogs_changed == 1) | |
2757 | return XLOG_STATE_COVER_NEED2; | |
2758 | break; | |
2759 | case XLOG_STATE_COVER_DONE2: | |
2760 | if (iclogs_changed == 1) | |
2761 | return XLOG_STATE_COVER_IDLE; | |
2762 | break; | |
2763 | default: | |
2764 | ASSERT(0); | |
2765 | } | |
0383f543 | 2766 | |
c814b4f2 CH |
2767 | return XLOG_STATE_COVER_NEED; |
2768 | } | |
1da177e4 | 2769 | |
c814b4f2 CH |
2770 | STATIC void |
2771 | xlog_state_clean_iclog( | |
2772 | struct xlog *log, | |
2773 | struct xlog_in_core *dirty_iclog) | |
2774 | { | |
2775 | int iclogs_changed = 0; | |
1da177e4 | 2776 | |
956f6daa DC |
2777 | trace_xlog_iclog_clean(dirty_iclog, _RET_IP_); |
2778 | ||
5781464b | 2779 | dirty_iclog->ic_state = XLOG_STATE_DIRTY; |
1da177e4 | 2780 | |
c814b4f2 CH |
2781 | xlog_state_activate_iclogs(log, &iclogs_changed); |
2782 | wake_up_all(&dirty_iclog->ic_force_wait); | |
2783 | ||
2784 | if (iclogs_changed) { | |
2785 | log->l_covered_state = xlog_covered_state(log->l_covered_state, | |
2786 | iclogs_changed); | |
1da177e4 | 2787 | } |
0383f543 | 2788 | } |
1da177e4 LT |
2789 | |
2790 | STATIC xfs_lsn_t | |
2791 | xlog_get_lowest_lsn( | |
9bff3132 | 2792 | struct xlog *log) |
1da177e4 | 2793 | { |
9bff3132 CH |
2794 | struct xlog_in_core *iclog = log->l_iclog; |
2795 | xfs_lsn_t lowest_lsn = 0, lsn; | |
1da177e4 | 2796 | |
1da177e4 | 2797 | do { |
1858bb0b CH |
2798 | if (iclog->ic_state == XLOG_STATE_ACTIVE || |
2799 | iclog->ic_state == XLOG_STATE_DIRTY) | |
9bff3132 CH |
2800 | continue; |
2801 | ||
2802 | lsn = be64_to_cpu(iclog->ic_header.h_lsn); | |
2803 | if ((lsn && !lowest_lsn) || XFS_LSN_CMP(lsn, lowest_lsn) < 0) | |
1da177e4 | 2804 | lowest_lsn = lsn; |
9bff3132 CH |
2805 | } while ((iclog = iclog->ic_next) != log->l_iclog); |
2806 | ||
014c2544 | 2807 | return lowest_lsn; |
1da177e4 LT |
2808 | } |
2809 | ||
14e15f1b DC |
2810 | /* |
2811 | * Completion of a iclog IO does not imply that a transaction has completed, as | |
2812 | * transactions can be large enough to span many iclogs. We cannot change the | |
2813 | * tail of the log half way through a transaction as this may be the only | |
2814 | * transaction in the log and moving the tail to point to the middle of it | |
2815 | * will prevent recovery from finding the start of the transaction. Hence we | |
2816 | * should only update the last_sync_lsn if this iclog contains transaction | |
2817 | * completion callbacks on it. | |
2818 | * | |
2819 | * We have to do this before we drop the icloglock to ensure we are the only one | |
2820 | * that can update it. | |
2821 | * | |
2822 | * If we are moving the last_sync_lsn forwards, we also need to ensure we kick | |
2823 | * the reservation grant head pushing. This is due to the fact that the push | |
2824 | * target is bound by the current last_sync_lsn value. Hence if we have a large | |
2825 | * amount of log space bound up in this committing transaction then the | |
2826 | * last_sync_lsn value may be the limiting factor preventing tail pushing from | |
2827 | * freeing space in the log. Hence once we've updated the last_sync_lsn we | |
2828 | * should push the AIL to ensure the push target (and hence the grant head) is | |
2829 | * no longer bound by the old log head location and can move forwards and make | |
2830 | * progress again. | |
2831 | */ | |
2832 | static void | |
2833 | xlog_state_set_callback( | |
2834 | struct xlog *log, | |
2835 | struct xlog_in_core *iclog, | |
2836 | xfs_lsn_t header_lsn) | |
2837 | { | |
956f6daa | 2838 | trace_xlog_iclog_callback(iclog, _RET_IP_); |
14e15f1b DC |
2839 | iclog->ic_state = XLOG_STATE_CALLBACK; |
2840 | ||
2841 | ASSERT(XFS_LSN_CMP(atomic64_read(&log->l_last_sync_lsn), | |
2842 | header_lsn) <= 0); | |
2843 | ||
2844 | if (list_empty_careful(&iclog->ic_callbacks)) | |
2845 | return; | |
2846 | ||
2847 | atomic64_set(&log->l_last_sync_lsn, header_lsn); | |
2848 | xlog_grant_push_ail(log, 0); | |
2849 | } | |
2850 | ||
5e96fa8d DC |
2851 | /* |
2852 | * Return true if we need to stop processing, false to continue to the next | |
2853 | * iclog. The caller will need to run callbacks if the iclog is returned in the | |
2854 | * XLOG_STATE_CALLBACK state. | |
2855 | */ | |
2856 | static bool | |
2857 | xlog_state_iodone_process_iclog( | |
2858 | struct xlog *log, | |
5112e206 | 2859 | struct xlog_in_core *iclog) |
5e96fa8d DC |
2860 | { |
2861 | xfs_lsn_t lowest_lsn; | |
14e15f1b | 2862 | xfs_lsn_t header_lsn; |
5e96fa8d | 2863 | |
1858bb0b CH |
2864 | switch (iclog->ic_state) { |
2865 | case XLOG_STATE_ACTIVE: | |
2866 | case XLOG_STATE_DIRTY: | |
2867 | /* | |
2868 | * Skip all iclogs in the ACTIVE & DIRTY states: | |
2869 | */ | |
5e96fa8d | 2870 | return false; |
1858bb0b | 2871 | case XLOG_STATE_DONE_SYNC: |
1858bb0b | 2872 | /* |
4b29ab04 CH |
2873 | * Now that we have an iclog that is in the DONE_SYNC state, do |
2874 | * one more check here to see if we have chased our tail around. | |
2875 | * If this is not the lowest lsn iclog, then we will leave it | |
2876 | * for another completion to process. | |
1858bb0b CH |
2877 | */ |
2878 | header_lsn = be64_to_cpu(iclog->ic_header.h_lsn); | |
2879 | lowest_lsn = xlog_get_lowest_lsn(log); | |
2880 | if (lowest_lsn && XFS_LSN_CMP(lowest_lsn, header_lsn) < 0) | |
2881 | return false; | |
2882 | xlog_state_set_callback(log, iclog, header_lsn); | |
2883 | return false; | |
2884 | default: | |
2885 | /* | |
2886 | * Can only perform callbacks in order. Since this iclog is not | |
4b29ab04 CH |
2887 | * in the DONE_SYNC state, we skip the rest and just try to |
2888 | * clean up. | |
1858bb0b | 2889 | */ |
5e96fa8d DC |
2890 | return true; |
2891 | } | |
5e96fa8d DC |
2892 | } |
2893 | ||
8bb92005 DC |
2894 | /* |
2895 | * Loop over all the iclogs, running attached callbacks on them. Return true if | |
aad7272a DC |
2896 | * we ran any callbacks, indicating that we dropped the icloglock. We don't need |
2897 | * to handle transient shutdown state here at all because | |
2898 | * xlog_state_shutdown_callbacks() will be run to do the necessary shutdown | |
2899 | * cleanup of the callbacks. | |
8bb92005 DC |
2900 | */ |
2901 | static bool | |
2902 | xlog_state_do_iclog_callbacks( | |
2903 | struct xlog *log) | |
2904 | __releases(&log->l_icloglock) | |
2905 | __acquires(&log->l_icloglock) | |
2906 | { | |
2907 | struct xlog_in_core *first_iclog = log->l_iclog; | |
2908 | struct xlog_in_core *iclog = first_iclog; | |
2909 | bool ran_callback = false; | |
2910 | ||
2911 | do { | |
2912 | LIST_HEAD(cb_list); | |
2913 | ||
aad7272a DC |
2914 | if (xlog_state_iodone_process_iclog(log, iclog)) |
2915 | break; | |
2916 | if (iclog->ic_state != XLOG_STATE_CALLBACK) { | |
2917 | iclog = iclog->ic_next; | |
2918 | continue; | |
8bb92005 DC |
2919 | } |
2920 | list_splice_init(&iclog->ic_callbacks, &cb_list); | |
2921 | spin_unlock(&log->l_icloglock); | |
2922 | ||
2923 | trace_xlog_iclog_callbacks_start(iclog, _RET_IP_); | |
2924 | xlog_cil_process_committed(&cb_list); | |
2925 | trace_xlog_iclog_callbacks_done(iclog, _RET_IP_); | |
2926 | ran_callback = true; | |
2927 | ||
2928 | spin_lock(&log->l_icloglock); | |
aad7272a | 2929 | xlog_state_clean_iclog(log, iclog); |
8bb92005 DC |
2930 | iclog = iclog->ic_next; |
2931 | } while (iclog != first_iclog); | |
2932 | ||
2933 | return ran_callback; | |
2934 | } | |
2935 | ||
2936 | ||
2937 | /* | |
2938 | * Loop running iclog completion callbacks until there are no more iclogs in a | |
2939 | * state that can run callbacks. | |
2940 | */ | |
1da177e4 LT |
2941 | STATIC void |
2942 | xlog_state_do_callback( | |
12e6a0f4 | 2943 | struct xlog *log) |
1da177e4 | 2944 | { |
5e96fa8d DC |
2945 | int flushcnt = 0; |
2946 | int repeats = 0; | |
1da177e4 | 2947 | |
b22cd72c | 2948 | spin_lock(&log->l_icloglock); |
8bb92005 DC |
2949 | while (xlog_state_do_iclog_callbacks(log)) { |
2950 | if (xlog_is_shutdown(log)) | |
2951 | break; | |
a3c6685e | 2952 | |
5112e206 | 2953 | if (++repeats > 5000) { |
a3c6685e NS |
2954 | flushcnt += repeats; |
2955 | repeats = 0; | |
a0fa2b67 | 2956 | xfs_warn(log->l_mp, |
a3c6685e | 2957 | "%s: possible infinite loop (%d iterations)", |
34a622b2 | 2958 | __func__, flushcnt); |
1da177e4 | 2959 | } |
8bb92005 | 2960 | } |
1da177e4 | 2961 | |
aad7272a | 2962 | if (log->l_iclog->ic_state == XLOG_STATE_ACTIVE) |
eb40a875 | 2963 | wake_up_all(&log->l_flush_wait); |
cdea5459 RR |
2964 | |
2965 | spin_unlock(&log->l_icloglock); | |
d748c623 | 2966 | } |
1da177e4 LT |
2967 | |
2968 | ||
2969 | /* | |
2970 | * Finish transitioning this iclog to the dirty state. | |
2971 | * | |
1da177e4 | 2972 | * Callbacks could take time, so they are done outside the scope of the |
12017faf | 2973 | * global state machine log lock. |
1da177e4 | 2974 | */ |
a8272ce0 | 2975 | STATIC void |
1da177e4 | 2976 | xlog_state_done_syncing( |
12e6a0f4 | 2977 | struct xlog_in_core *iclog) |
1da177e4 | 2978 | { |
d15cbf2f | 2979 | struct xlog *log = iclog->ic_log; |
1da177e4 | 2980 | |
b22cd72c | 2981 | spin_lock(&log->l_icloglock); |
155cc6b7 | 2982 | ASSERT(atomic_read(&iclog->ic_refcnt) == 0); |
956f6daa | 2983 | trace_xlog_iclog_sync_done(iclog, _RET_IP_); |
1da177e4 LT |
2984 | |
2985 | /* | |
2986 | * If we got an error, either on the first buffer, or in the case of | |
12e6a0f4 CH |
2987 | * split log writes, on the second, we shut down the file system and |
2988 | * no iclogs should ever be attempted to be written to disk again. | |
1da177e4 | 2989 | */ |
2039a272 | 2990 | if (!xlog_is_shutdown(log)) { |
12e6a0f4 | 2991 | ASSERT(iclog->ic_state == XLOG_STATE_SYNCING); |
1da177e4 | 2992 | iclog->ic_state = XLOG_STATE_DONE_SYNC; |
12e6a0f4 | 2993 | } |
1da177e4 LT |
2994 | |
2995 | /* | |
2996 | * Someone could be sleeping prior to writing out the next | |
2997 | * iclog buffer, we wake them all, one will get to do the | |
2998 | * I/O, the others get to wait for the result. | |
2999 | */ | |
eb40a875 | 3000 | wake_up_all(&iclog->ic_write_wait); |
b22cd72c | 3001 | spin_unlock(&log->l_icloglock); |
b843299b | 3002 | xlog_state_do_callback(log); |
12e6a0f4 | 3003 | } |
1da177e4 LT |
3004 | |
3005 | /* | |
3006 | * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must | |
12017faf DC |
3007 | * sleep. We wait on the flush queue on the head iclog as that should be |
3008 | * the first iclog to complete flushing. Hence if all iclogs are syncing, | |
3009 | * we will wait here and all new writes will sleep until a sync completes. | |
1da177e4 LT |
3010 | * |
3011 | * The in-core logs are used in a circular fashion. They are not used | |
3012 | * out-of-order even when an iclog past the head is free. | |
3013 | * | |
3014 | * return: | |
3015 | * * log_offset where xlog_write() can start writing into the in-core | |
3016 | * log's data space. | |
3017 | * * in-core log pointer to which xlog_write() should write. | |
3018 | * * boolean indicating this is a continued write to an in-core log. | |
3019 | * If this is the last write, then the in-core log's offset field | |
3020 | * needs to be incremented, depending on the amount of data which | |
3021 | * is copied. | |
3022 | */ | |
a8272ce0 | 3023 | STATIC int |
9a8d2fdb MT |
3024 | xlog_state_get_iclog_space( |
3025 | struct xlog *log, | |
3026 | int len, | |
3027 | struct xlog_in_core **iclogp, | |
3028 | struct xlog_ticket *ticket, | |
3029 | int *continued_write, | |
3030 | int *logoffsetp) | |
1da177e4 | 3031 | { |
1da177e4 LT |
3032 | int log_offset; |
3033 | xlog_rec_header_t *head; | |
3034 | xlog_in_core_t *iclog; | |
1da177e4 LT |
3035 | |
3036 | restart: | |
b22cd72c | 3037 | spin_lock(&log->l_icloglock); |
2039a272 | 3038 | if (xlog_is_shutdown(log)) { |
b22cd72c | 3039 | spin_unlock(&log->l_icloglock); |
2451337d | 3040 | return -EIO; |
1da177e4 LT |
3041 | } |
3042 | ||
3043 | iclog = log->l_iclog; | |
d748c623 | 3044 | if (iclog->ic_state != XLOG_STATE_ACTIVE) { |
ff6d6af2 | 3045 | XFS_STATS_INC(log->l_mp, xs_log_noiclogs); |
d748c623 MW |
3046 | |
3047 | /* Wait for log writes to have flushed */ | |
eb40a875 | 3048 | xlog_wait(&log->l_flush_wait, &log->l_icloglock); |
1da177e4 LT |
3049 | goto restart; |
3050 | } | |
d748c623 | 3051 | |
1da177e4 LT |
3052 | head = &iclog->ic_header; |
3053 | ||
155cc6b7 | 3054 | atomic_inc(&iclog->ic_refcnt); /* prevents sync */ |
1da177e4 LT |
3055 | log_offset = iclog->ic_offset; |
3056 | ||
956f6daa DC |
3057 | trace_xlog_iclog_get_space(iclog, _RET_IP_); |
3058 | ||
1da177e4 LT |
3059 | /* On the 1st write to an iclog, figure out lsn. This works |
3060 | * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are | |
3061 | * committing to. If the offset is set, that's how many blocks | |
3062 | * must be written. | |
3063 | */ | |
3064 | if (log_offset == 0) { | |
3065 | ticket->t_curr_res -= log->l_iclog_hsize; | |
0adba536 | 3066 | xlog_tic_add_region(ticket, |
7e9c6396 TS |
3067 | log->l_iclog_hsize, |
3068 | XLOG_REG_TYPE_LRHEADER); | |
b53e675d CH |
3069 | head->h_cycle = cpu_to_be32(log->l_curr_cycle); |
3070 | head->h_lsn = cpu_to_be64( | |
03bea6fe | 3071 | xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block)); |
1da177e4 LT |
3072 | ASSERT(log->l_curr_block >= 0); |
3073 | } | |
3074 | ||
3075 | /* If there is enough room to write everything, then do it. Otherwise, | |
3076 | * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC | |
3077 | * bit is on, so this will get flushed out. Don't update ic_offset | |
3078 | * until you know exactly how many bytes get copied. Therefore, wait | |
3079 | * until later to update ic_offset. | |
3080 | * | |
3081 | * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's | |
3082 | * can fit into remaining data section. | |
3083 | */ | |
3084 | if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) { | |
df732b29 CH |
3085 | int error = 0; |
3086 | ||
1da177e4 LT |
3087 | xlog_state_switch_iclogs(log, iclog, iclog->ic_size); |
3088 | ||
49641f1a | 3089 | /* |
df732b29 CH |
3090 | * If we are the only one writing to this iclog, sync it to |
3091 | * disk. We need to do an atomic compare and decrement here to | |
3092 | * avoid racing with concurrent atomic_dec_and_lock() calls in | |
49641f1a DC |
3093 | * xlog_state_release_iclog() when there is more than one |
3094 | * reference to the iclog. | |
3095 | */ | |
df732b29 | 3096 | if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) |
0dc8f7f1 | 3097 | error = xlog_state_release_iclog(log, iclog, 0); |
df732b29 CH |
3098 | spin_unlock(&log->l_icloglock); |
3099 | if (error) | |
3100 | return error; | |
1da177e4 LT |
3101 | goto restart; |
3102 | } | |
3103 | ||
3104 | /* Do we have enough room to write the full amount in the remainder | |
3105 | * of this iclog? Or must we continue a write on the next iclog and | |
3106 | * mark this iclog as completely taken? In the case where we switch | |
3107 | * iclogs (to mark it taken), this particular iclog will release/sync | |
3108 | * to disk in xlog_write(). | |
3109 | */ | |
3110 | if (len <= iclog->ic_size - iclog->ic_offset) { | |
3111 | *continued_write = 0; | |
3112 | iclog->ic_offset += len; | |
3113 | } else { | |
3114 | *continued_write = 1; | |
3115 | xlog_state_switch_iclogs(log, iclog, iclog->ic_size); | |
3116 | } | |
3117 | *iclogp = iclog; | |
3118 | ||
3119 | ASSERT(iclog->ic_offset <= iclog->ic_size); | |
b22cd72c | 3120 | spin_unlock(&log->l_icloglock); |
1da177e4 LT |
3121 | |
3122 | *logoffsetp = log_offset; | |
3123 | return 0; | |
b843299b | 3124 | } |
1da177e4 | 3125 | |
8b41e3f9 | 3126 | /* |
b843299b DC |
3127 | * The first cnt-1 times a ticket goes through here we don't need to move the |
3128 | * grant write head because the permanent reservation has reserved cnt times the | |
3129 | * unit amount. Release part of current permanent unit reservation and reset | |
3130 | * current reservation to be one units worth. Also move grant reservation head | |
3131 | * forward. | |
1da177e4 | 3132 | */ |
8b41e3f9 CH |
3133 | void |
3134 | xfs_log_ticket_regrant( | |
9a8d2fdb MT |
3135 | struct xlog *log, |
3136 | struct xlog_ticket *ticket) | |
1da177e4 | 3137 | { |
8b41e3f9 | 3138 | trace_xfs_log_ticket_regrant(log, ticket); |
0b1b213f | 3139 | |
1da177e4 LT |
3140 | if (ticket->t_cnt > 0) |
3141 | ticket->t_cnt--; | |
3142 | ||
28496968 | 3143 | xlog_grant_sub_space(log, &log->l_reserve_head.grant, |
a69ed03c | 3144 | ticket->t_curr_res); |
28496968 | 3145 | xlog_grant_sub_space(log, &log->l_write_head.grant, |
a69ed03c | 3146 | ticket->t_curr_res); |
1da177e4 | 3147 | ticket->t_curr_res = ticket->t_unit_res; |
0adba536 | 3148 | xlog_tic_reset_res(ticket); |
0b1b213f | 3149 | |
8b41e3f9 | 3150 | trace_xfs_log_ticket_regrant_sub(log, ticket); |
0b1b213f | 3151 | |
1da177e4 | 3152 | /* just return if we still have some of the pre-reserved space */ |
8b41e3f9 CH |
3153 | if (!ticket->t_cnt) { |
3154 | xlog_grant_add_space(log, &log->l_reserve_head.grant, | |
3155 | ticket->t_unit_res); | |
3156 | trace_xfs_log_ticket_regrant_exit(log, ticket); | |
1da177e4 | 3157 | |
8b41e3f9 CH |
3158 | ticket->t_curr_res = ticket->t_unit_res; |
3159 | xlog_tic_reset_res(ticket); | |
3160 | } | |
1da177e4 | 3161 | |
8b41e3f9 CH |
3162 | xfs_log_ticket_put(ticket); |
3163 | } | |
1da177e4 LT |
3164 | |
3165 | /* | |
3166 | * Give back the space left from a reservation. | |
3167 | * | |
3168 | * All the information we need to make a correct determination of space left | |
3169 | * is present. For non-permanent reservations, things are quite easy. The | |
3170 | * count should have been decremented to zero. We only need to deal with the | |
3171 | * space remaining in the current reservation part of the ticket. If the | |
3172 | * ticket contains a permanent reservation, there may be left over space which | |
3173 | * needs to be released. A count of N means that N-1 refills of the current | |
3174 | * reservation can be done before we need to ask for more space. The first | |
3175 | * one goes to fill up the first current reservation. Once we run out of | |
3176 | * space, the count will stay at zero and the only space remaining will be | |
3177 | * in the current reservation field. | |
3178 | */ | |
8b41e3f9 CH |
3179 | void |
3180 | xfs_log_ticket_ungrant( | |
9a8d2fdb MT |
3181 | struct xlog *log, |
3182 | struct xlog_ticket *ticket) | |
1da177e4 | 3183 | { |
8b41e3f9 CH |
3184 | int bytes; |
3185 | ||
3186 | trace_xfs_log_ticket_ungrant(log, ticket); | |
663e496a | 3187 | |
1da177e4 LT |
3188 | if (ticket->t_cnt > 0) |
3189 | ticket->t_cnt--; | |
3190 | ||
8b41e3f9 | 3191 | trace_xfs_log_ticket_ungrant_sub(log, ticket); |
1da177e4 | 3192 | |
663e496a DC |
3193 | /* |
3194 | * If this is a permanent reservation ticket, we may be able to free | |
1da177e4 LT |
3195 | * up more space based on the remaining count. |
3196 | */ | |
663e496a | 3197 | bytes = ticket->t_curr_res; |
1da177e4 LT |
3198 | if (ticket->t_cnt > 0) { |
3199 | ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV); | |
663e496a | 3200 | bytes += ticket->t_unit_res*ticket->t_cnt; |
1da177e4 LT |
3201 | } |
3202 | ||
28496968 CH |
3203 | xlog_grant_sub_space(log, &log->l_reserve_head.grant, bytes); |
3204 | xlog_grant_sub_space(log, &log->l_write_head.grant, bytes); | |
663e496a | 3205 | |
8b41e3f9 | 3206 | trace_xfs_log_ticket_ungrant_exit(log, ticket); |
0b1b213f | 3207 | |
cfb7cdca | 3208 | xfs_log_space_wake(log->l_mp); |
8b41e3f9 | 3209 | xfs_log_ticket_put(ticket); |
09a423a3 | 3210 | } |
1da177e4 | 3211 | |
1da177e4 | 3212 | /* |
b843299b DC |
3213 | * This routine will mark the current iclog in the ring as WANT_SYNC and move |
3214 | * the current iclog pointer to the next iclog in the ring. | |
1da177e4 LT |
3215 | */ |
3216 | STATIC void | |
9a8d2fdb MT |
3217 | xlog_state_switch_iclogs( |
3218 | struct xlog *log, | |
3219 | struct xlog_in_core *iclog, | |
3220 | int eventual_size) | |
1da177e4 LT |
3221 | { |
3222 | ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE); | |
69363999 | 3223 | assert_spin_locked(&log->l_icloglock); |
956f6daa | 3224 | trace_xlog_iclog_switch(iclog, _RET_IP_); |
69363999 | 3225 | |
1da177e4 LT |
3226 | if (!eventual_size) |
3227 | eventual_size = iclog->ic_offset; | |
3228 | iclog->ic_state = XLOG_STATE_WANT_SYNC; | |
b53e675d | 3229 | iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block); |
1da177e4 LT |
3230 | log->l_prev_block = log->l_curr_block; |
3231 | log->l_prev_cycle = log->l_curr_cycle; | |
3232 | ||
3233 | /* roll log?: ic_offset changed later */ | |
3234 | log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize); | |
3235 | ||
3236 | /* Round up to next log-sunit */ | |
a6a65fef | 3237 | if (log->l_iclog_roundoff > BBSIZE) { |
18842e0a | 3238 | uint32_t sunit_bb = BTOBB(log->l_iclog_roundoff); |
1da177e4 LT |
3239 | log->l_curr_block = roundup(log->l_curr_block, sunit_bb); |
3240 | } | |
3241 | ||
3242 | if (log->l_curr_block >= log->l_logBBsize) { | |
a45086e2 BF |
3243 | /* |
3244 | * Rewind the current block before the cycle is bumped to make | |
3245 | * sure that the combined LSN never transiently moves forward | |
3246 | * when the log wraps to the next cycle. This is to support the | |
3247 | * unlocked sample of these fields from xlog_valid_lsn(). Most | |
3248 | * other cases should acquire l_icloglock. | |
3249 | */ | |
3250 | log->l_curr_block -= log->l_logBBsize; | |
3251 | ASSERT(log->l_curr_block >= 0); | |
3252 | smp_wmb(); | |
1da177e4 LT |
3253 | log->l_curr_cycle++; |
3254 | if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM) | |
3255 | log->l_curr_cycle++; | |
1da177e4 LT |
3256 | } |
3257 | ASSERT(iclog == log->l_iclog); | |
3258 | log->l_iclog = iclog->ic_next; | |
b843299b | 3259 | } |
1da177e4 | 3260 | |
8191d822 DC |
3261 | /* |
3262 | * Force the iclog to disk and check if the iclog has been completed before | |
3263 | * xlog_force_iclog() returns. This can happen on synchronous (e.g. | |
3264 | * pmem) or fast async storage because we drop the icloglock to issue the IO. | |
3265 | * If completion has already occurred, tell the caller so that it can avoid an | |
3266 | * unnecessary wait on the iclog. | |
3267 | */ | |
3268 | static int | |
3269 | xlog_force_and_check_iclog( | |
3270 | struct xlog_in_core *iclog, | |
3271 | bool *completed) | |
3272 | { | |
3273 | xfs_lsn_t lsn = be64_to_cpu(iclog->ic_header.h_lsn); | |
3274 | int error; | |
3275 | ||
3276 | *completed = false; | |
3277 | error = xlog_force_iclog(iclog); | |
3278 | if (error) | |
3279 | return error; | |
3280 | ||
3281 | /* | |
3282 | * If the iclog has already been completed and reused the header LSN | |
3283 | * will have been rewritten by completion | |
3284 | */ | |
3285 | if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) | |
3286 | *completed = true; | |
3287 | return 0; | |
3288 | } | |
3289 | ||
1da177e4 LT |
3290 | /* |
3291 | * Write out all data in the in-core log as of this exact moment in time. | |
3292 | * | |
3293 | * Data may be written to the in-core log during this call. However, | |
3294 | * we don't guarantee this data will be written out. A change from past | |
3295 | * implementation means this routine will *not* write out zero length LRs. | |
3296 | * | |
3297 | * Basically, we try and perform an intelligent scan of the in-core logs. | |
3298 | * If we determine there is no flushable data, we just return. There is no | |
3299 | * flushable data if: | |
3300 | * | |
3301 | * 1. the current iclog is active and has no data; the previous iclog | |
3302 | * is in the active or dirty state. | |
3303 | * 2. the current iclog is drity, and the previous iclog is in the | |
3304 | * active or dirty state. | |
3305 | * | |
12017faf | 3306 | * We may sleep if: |
1da177e4 LT |
3307 | * |
3308 | * 1. the current iclog is not in the active nor dirty state. | |
3309 | * 2. the current iclog dirty, and the previous iclog is not in the | |
3310 | * active nor dirty state. | |
3311 | * 3. the current iclog is active, and there is another thread writing | |
3312 | * to this particular iclog. | |
3313 | * 4. a) the current iclog is active and has no other writers | |
3314 | * b) when we return from flushing out this iclog, it is still | |
3315 | * not in the active nor dirty state. | |
3316 | */ | |
a14a348b | 3317 | int |
60e5bb78 | 3318 | xfs_log_force( |
a14a348b | 3319 | struct xfs_mount *mp, |
60e5bb78 | 3320 | uint flags) |
1da177e4 | 3321 | { |
ad223e60 | 3322 | struct xlog *log = mp->m_log; |
a14a348b | 3323 | struct xlog_in_core *iclog; |
a14a348b | 3324 | |
ff6d6af2 | 3325 | XFS_STATS_INC(mp, xs_log_force); |
60e5bb78 | 3326 | trace_xfs_log_force(mp, 0, _RET_IP_); |
1da177e4 | 3327 | |
93b8a585 | 3328 | xlog_cil_force(log); |
71e330b5 | 3329 | |
b22cd72c | 3330 | spin_lock(&log->l_icloglock); |
5112e206 | 3331 | if (xlog_is_shutdown(log)) |
e6b96570 | 3332 | goto out_error; |
1da177e4 | 3333 | |
5112e206 | 3334 | iclog = log->l_iclog; |
956f6daa DC |
3335 | trace_xlog_iclog_force(iclog, _RET_IP_); |
3336 | ||
e6b96570 CH |
3337 | if (iclog->ic_state == XLOG_STATE_DIRTY || |
3338 | (iclog->ic_state == XLOG_STATE_ACTIVE && | |
3339 | atomic_read(&iclog->ic_refcnt) == 0 && iclog->ic_offset == 0)) { | |
1da177e4 | 3340 | /* |
e6b96570 CH |
3341 | * If the head is dirty or (active and empty), then we need to |
3342 | * look at the previous iclog. | |
3343 | * | |
3344 | * If the previous iclog is active or dirty we are done. There | |
3345 | * is nothing to sync out. Otherwise, we attach ourselves to the | |
1da177e4 LT |
3346 | * previous iclog and go to sleep. |
3347 | */ | |
e6b96570 | 3348 | iclog = iclog->ic_prev; |
e6b96570 CH |
3349 | } else if (iclog->ic_state == XLOG_STATE_ACTIVE) { |
3350 | if (atomic_read(&iclog->ic_refcnt) == 0) { | |
45eddb41 | 3351 | /* We have exclusive access to this iclog. */ |
8191d822 DC |
3352 | bool completed; |
3353 | ||
3354 | if (xlog_force_and_check_iclog(iclog, &completed)) | |
df732b29 | 3355 | goto out_error; |
1da177e4 | 3356 | |
8191d822 | 3357 | if (completed) |
e6b96570 CH |
3358 | goto out_unlock; |
3359 | } else { | |
3360 | /* | |
2bf1ec0f DC |
3361 | * Someone else is still writing to this iclog, so we |
3362 | * need to ensure that when they release the iclog it | |
3363 | * gets synced immediately as we may be waiting on it. | |
e6b96570 CH |
3364 | */ |
3365 | xlog_state_switch_iclogs(log, iclog, 0); | |
1da177e4 | 3366 | } |
1da177e4 | 3367 | } |
e6b96570 | 3368 | |
2bf1ec0f DC |
3369 | /* |
3370 | * The iclog we are about to wait on may contain the checkpoint pushed | |
3371 | * by the above xlog_cil_force() call, but it may not have been pushed | |
3372 | * to disk yet. Like the ACTIVE case above, we need to make sure caches | |
3373 | * are flushed when this iclog is written. | |
3374 | */ | |
3375 | if (iclog->ic_state == XLOG_STATE_WANT_SYNC) | |
3376 | iclog->ic_flags |= XLOG_ICL_NEED_FLUSH | XLOG_ICL_NEED_FUA; | |
3377 | ||
81e5b50a CH |
3378 | if (flags & XFS_LOG_SYNC) |
3379 | return xlog_wait_on_iclog(iclog); | |
e6b96570 CH |
3380 | out_unlock: |
3381 | spin_unlock(&log->l_icloglock); | |
3382 | return 0; | |
3383 | out_error: | |
3384 | spin_unlock(&log->l_icloglock); | |
3385 | return -EIO; | |
a14a348b | 3386 | } |
1da177e4 | 3387 | |
3e4da466 | 3388 | static int |
5f9b4b0d DC |
3389 | xlog_force_lsn( |
3390 | struct xlog *log, | |
a14a348b CH |
3391 | xfs_lsn_t lsn, |
3392 | uint flags, | |
3e4da466 CH |
3393 | int *log_flushed, |
3394 | bool already_slept) | |
1da177e4 | 3395 | { |
a14a348b | 3396 | struct xlog_in_core *iclog; |
8191d822 | 3397 | bool completed; |
71e330b5 | 3398 | |
a14a348b | 3399 | spin_lock(&log->l_icloglock); |
5112e206 | 3400 | if (xlog_is_shutdown(log)) |
93806299 | 3401 | goto out_error; |
1da177e4 | 3402 | |
5112e206 | 3403 | iclog = log->l_iclog; |
93806299 | 3404 | while (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) { |
956f6daa | 3405 | trace_xlog_iclog_force_lsn(iclog, _RET_IP_); |
93806299 CH |
3406 | iclog = iclog->ic_next; |
3407 | if (iclog == log->l_iclog) | |
3408 | goto out_unlock; | |
3409 | } | |
a14a348b | 3410 | |
2bf1ec0f DC |
3411 | switch (iclog->ic_state) { |
3412 | case XLOG_STATE_ACTIVE: | |
93806299 CH |
3413 | /* |
3414 | * We sleep here if we haven't already slept (e.g. this is the | |
3415 | * first time we've looked at the correct iclog buf) and the | |
3416 | * buffer before us is going to be sync'ed. The reason for this | |
3417 | * is that if we are doing sync transactions here, by waiting | |
3418 | * for the previous I/O to complete, we can allow a few more | |
3419 | * transactions into this iclog before we close it down. | |
3420 | * | |
3421 | * Otherwise, we mark the buffer WANT_SYNC, and bump up the | |
3422 | * refcnt so we can release the log (which drops the ref count). | |
3423 | * The state switch keeps new transaction commits from using | |
3424 | * this buffer. When the current commits finish writing into | |
3425 | * the buffer, the refcount will drop to zero and the buffer | |
3426 | * will go out then. | |
3427 | */ | |
3428 | if (!already_slept && | |
1858bb0b CH |
3429 | (iclog->ic_prev->ic_state == XLOG_STATE_WANT_SYNC || |
3430 | iclog->ic_prev->ic_state == XLOG_STATE_SYNCING)) { | |
93806299 CH |
3431 | xlog_wait(&iclog->ic_prev->ic_write_wait, |
3432 | &log->l_icloglock); | |
3e4da466 | 3433 | return -EAGAIN; |
1da177e4 | 3434 | } |
8191d822 | 3435 | if (xlog_force_and_check_iclog(iclog, &completed)) |
df732b29 | 3436 | goto out_error; |
93806299 CH |
3437 | if (log_flushed) |
3438 | *log_flushed = 1; | |
8191d822 DC |
3439 | if (completed) |
3440 | goto out_unlock; | |
2bf1ec0f DC |
3441 | break; |
3442 | case XLOG_STATE_WANT_SYNC: | |
3443 | /* | |
3444 | * This iclog may contain the checkpoint pushed by the | |
3445 | * xlog_cil_force_seq() call, but there are other writers still | |
3446 | * accessing it so it hasn't been pushed to disk yet. Like the | |
3447 | * ACTIVE case above, we need to make sure caches are flushed | |
3448 | * when this iclog is written. | |
3449 | */ | |
3450 | iclog->ic_flags |= XLOG_ICL_NEED_FLUSH | XLOG_ICL_NEED_FUA; | |
3451 | break; | |
3452 | default: | |
3453 | /* | |
3454 | * The entire checkpoint was written by the CIL force and is on | |
3455 | * its way to disk already. It will be stable when it | |
3456 | * completes, so we don't need to manipulate caches here at all. | |
3457 | * We just need to wait for completion if necessary. | |
3458 | */ | |
3459 | break; | |
93806299 | 3460 | } |
1da177e4 | 3461 | |
81e5b50a CH |
3462 | if (flags & XFS_LOG_SYNC) |
3463 | return xlog_wait_on_iclog(iclog); | |
93806299 | 3464 | out_unlock: |
a14a348b CH |
3465 | spin_unlock(&log->l_icloglock); |
3466 | return 0; | |
93806299 CH |
3467 | out_error: |
3468 | spin_unlock(&log->l_icloglock); | |
3469 | return -EIO; | |
a14a348b CH |
3470 | } |
3471 | ||
3e4da466 CH |
3472 | /* |
3473 | * Force the in-core log to disk for a specific LSN. | |
3474 | * | |
3475 | * Find in-core log with lsn. | |
3476 | * If it is in the DIRTY state, just return. | |
3477 | * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC | |
3478 | * state and go to sleep or return. | |
3479 | * If it is in any other state, go to sleep or return. | |
3480 | * | |
3481 | * Synchronous forces are implemented with a wait queue. All callers trying | |
3482 | * to force a given lsn to disk must wait on the queue attached to the | |
3483 | * specific in-core log. When given in-core log finally completes its write | |
3484 | * to disk, that thread will wake up all threads waiting on the queue. | |
3485 | */ | |
3486 | int | |
5f9b4b0d | 3487 | xfs_log_force_seq( |
3e4da466 | 3488 | struct xfs_mount *mp, |
5f9b4b0d | 3489 | xfs_csn_t seq, |
3e4da466 CH |
3490 | uint flags, |
3491 | int *log_flushed) | |
3492 | { | |
5f9b4b0d DC |
3493 | struct xlog *log = mp->m_log; |
3494 | xfs_lsn_t lsn; | |
3e4da466 | 3495 | int ret; |
5f9b4b0d | 3496 | ASSERT(seq != 0); |
3e4da466 CH |
3497 | |
3498 | XFS_STATS_INC(mp, xs_log_force); | |
5f9b4b0d | 3499 | trace_xfs_log_force(mp, seq, _RET_IP_); |
3e4da466 | 3500 | |
5f9b4b0d | 3501 | lsn = xlog_cil_force_seq(log, seq); |
3e4da466 CH |
3502 | if (lsn == NULLCOMMITLSN) |
3503 | return 0; | |
3504 | ||
5f9b4b0d DC |
3505 | ret = xlog_force_lsn(log, lsn, flags, log_flushed, false); |
3506 | if (ret == -EAGAIN) { | |
3507 | XFS_STATS_INC(mp, xs_log_force_sleep); | |
3508 | ret = xlog_force_lsn(log, lsn, flags, log_flushed, true); | |
3509 | } | |
3e4da466 CH |
3510 | return ret; |
3511 | } | |
3512 | ||
1da177e4 | 3513 | /* |
9da096fd | 3514 | * Free a used ticket when its refcount falls to zero. |
1da177e4 | 3515 | */ |
cc09c0dc DC |
3516 | void |
3517 | xfs_log_ticket_put( | |
3518 | xlog_ticket_t *ticket) | |
1da177e4 | 3519 | { |
cc09c0dc | 3520 | ASSERT(atomic_read(&ticket->t_ref) > 0); |
eb40a875 | 3521 | if (atomic_dec_and_test(&ticket->t_ref)) |
377bcd5f | 3522 | kmem_cache_free(xfs_log_ticket_zone, ticket); |
cc09c0dc | 3523 | } |
1da177e4 | 3524 | |
cc09c0dc DC |
3525 | xlog_ticket_t * |
3526 | xfs_log_ticket_get( | |
3527 | xlog_ticket_t *ticket) | |
3528 | { | |
3529 | ASSERT(atomic_read(&ticket->t_ref) > 0); | |
3530 | atomic_inc(&ticket->t_ref); | |
3531 | return ticket; | |
3532 | } | |
1da177e4 LT |
3533 | |
3534 | /* | |
e773fc93 JL |
3535 | * Figure out the total log space unit (in bytes) that would be |
3536 | * required for a log ticket. | |
1da177e4 | 3537 | */ |
a6a65fef DC |
3538 | static int |
3539 | xlog_calc_unit_res( | |
3540 | struct xlog *log, | |
e773fc93 | 3541 | int unit_bytes) |
1da177e4 | 3542 | { |
e773fc93 JL |
3543 | int iclog_space; |
3544 | uint num_headers; | |
1da177e4 LT |
3545 | |
3546 | /* | |
3547 | * Permanent reservations have up to 'cnt'-1 active log operations | |
3548 | * in the log. A unit in this case is the amount of space for one | |
3549 | * of these log operations. Normal reservations have a cnt of 1 | |
3550 | * and their unit amount is the total amount of space required. | |
3551 | * | |
3552 | * The following lines of code account for non-transaction data | |
32fb9b57 TS |
3553 | * which occupy space in the on-disk log. |
3554 | * | |
3555 | * Normal form of a transaction is: | |
3556 | * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph> | |
3557 | * and then there are LR hdrs, split-recs and roundoff at end of syncs. | |
3558 | * | |
3559 | * We need to account for all the leadup data and trailer data | |
3560 | * around the transaction data. | |
3561 | * And then we need to account for the worst case in terms of using | |
3562 | * more space. | |
3563 | * The worst case will happen if: | |
3564 | * - the placement of the transaction happens to be such that the | |
3565 | * roundoff is at its maximum | |
3566 | * - the transaction data is synced before the commit record is synced | |
3567 | * i.e. <transaction-data><roundoff> | <commit-rec><roundoff> | |
3568 | * Therefore the commit record is in its own Log Record. | |
3569 | * This can happen as the commit record is called with its | |
3570 | * own region to xlog_write(). | |
3571 | * This then means that in the worst case, roundoff can happen for | |
3572 | * the commit-rec as well. | |
3573 | * The commit-rec is smaller than padding in this scenario and so it is | |
3574 | * not added separately. | |
1da177e4 LT |
3575 | */ |
3576 | ||
32fb9b57 TS |
3577 | /* for trans header */ |
3578 | unit_bytes += sizeof(xlog_op_header_t); | |
3579 | unit_bytes += sizeof(xfs_trans_header_t); | |
3580 | ||
1da177e4 | 3581 | /* for start-rec */ |
32fb9b57 TS |
3582 | unit_bytes += sizeof(xlog_op_header_t); |
3583 | ||
9b9fc2b7 DC |
3584 | /* |
3585 | * for LR headers - the space for data in an iclog is the size minus | |
3586 | * the space used for the headers. If we use the iclog size, then we | |
3587 | * undercalculate the number of headers required. | |
3588 | * | |
3589 | * Furthermore - the addition of op headers for split-recs might | |
3590 | * increase the space required enough to require more log and op | |
3591 | * headers, so take that into account too. | |
3592 | * | |
3593 | * IMPORTANT: This reservation makes the assumption that if this | |
3594 | * transaction is the first in an iclog and hence has the LR headers | |
3595 | * accounted to it, then the remaining space in the iclog is | |
3596 | * exclusively for this transaction. i.e. if the transaction is larger | |
3597 | * than the iclog, it will be the only thing in that iclog. | |
3598 | * Fundamentally, this means we must pass the entire log vector to | |
3599 | * xlog_write to guarantee this. | |
3600 | */ | |
3601 | iclog_space = log->l_iclog_size - log->l_iclog_hsize; | |
3602 | num_headers = howmany(unit_bytes, iclog_space); | |
3603 | ||
3604 | /* for split-recs - ophdrs added when data split over LRs */ | |
3605 | unit_bytes += sizeof(xlog_op_header_t) * num_headers; | |
3606 | ||
3607 | /* add extra header reservations if we overrun */ | |
3608 | while (!num_headers || | |
3609 | howmany(unit_bytes, iclog_space) > num_headers) { | |
3610 | unit_bytes += sizeof(xlog_op_header_t); | |
3611 | num_headers++; | |
3612 | } | |
32fb9b57 | 3613 | unit_bytes += log->l_iclog_hsize * num_headers; |
1da177e4 | 3614 | |
32fb9b57 TS |
3615 | /* for commit-rec LR header - note: padding will subsume the ophdr */ |
3616 | unit_bytes += log->l_iclog_hsize; | |
3617 | ||
a6a65fef DC |
3618 | /* roundoff padding for transaction data and one for commit record */ |
3619 | unit_bytes += 2 * log->l_iclog_roundoff; | |
1da177e4 | 3620 | |
e773fc93 JL |
3621 | return unit_bytes; |
3622 | } | |
3623 | ||
a6a65fef DC |
3624 | int |
3625 | xfs_log_calc_unit_res( | |
3626 | struct xfs_mount *mp, | |
3627 | int unit_bytes) | |
3628 | { | |
3629 | return xlog_calc_unit_res(mp->m_log, unit_bytes); | |
3630 | } | |
3631 | ||
e773fc93 JL |
3632 | /* |
3633 | * Allocate and initialise a new log ticket. | |
3634 | */ | |
3635 | struct xlog_ticket * | |
3636 | xlog_ticket_alloc( | |
3637 | struct xlog *log, | |
3638 | int unit_bytes, | |
3639 | int cnt, | |
3640 | char client, | |
ca4f2589 | 3641 | bool permanent) |
e773fc93 JL |
3642 | { |
3643 | struct xlog_ticket *tic; | |
3644 | int unit_res; | |
3645 | ||
ca4f2589 | 3646 | tic = kmem_cache_zalloc(xfs_log_ticket_zone, GFP_NOFS | __GFP_NOFAIL); |
e773fc93 | 3647 | |
a6a65fef | 3648 | unit_res = xlog_calc_unit_res(log, unit_bytes); |
e773fc93 | 3649 | |
cc09c0dc | 3650 | atomic_set(&tic->t_ref, 1); |
14a7235f | 3651 | tic->t_task = current; |
10547941 | 3652 | INIT_LIST_HEAD(&tic->t_queue); |
e773fc93 JL |
3653 | tic->t_unit_res = unit_res; |
3654 | tic->t_curr_res = unit_res; | |
1da177e4 LT |
3655 | tic->t_cnt = cnt; |
3656 | tic->t_ocnt = cnt; | |
ecb3403d | 3657 | tic->t_tid = prandom_u32(); |
1da177e4 | 3658 | tic->t_clientid = client; |
9006fb91 | 3659 | if (permanent) |
1da177e4 | 3660 | tic->t_flags |= XLOG_TIC_PERM_RESERV; |
1da177e4 | 3661 | |
0adba536 | 3662 | xlog_tic_reset_res(tic); |
7e9c6396 | 3663 | |
1da177e4 | 3664 | return tic; |
cc09c0dc | 3665 | } |
1da177e4 | 3666 | |
cfcbbbd0 | 3667 | #if defined(DEBUG) |
1da177e4 LT |
3668 | /* |
3669 | * Make sure that the destination ptr is within the valid data region of | |
3670 | * one of the iclogs. This uses backup pointers stored in a different | |
3671 | * part of the log in case we trash the log structure. | |
3672 | */ | |
181fdfe6 | 3673 | STATIC void |
e6b1f273 | 3674 | xlog_verify_dest_ptr( |
ad223e60 | 3675 | struct xlog *log, |
5809d5e0 | 3676 | void *ptr) |
1da177e4 LT |
3677 | { |
3678 | int i; | |
3679 | int good_ptr = 0; | |
3680 | ||
e6b1f273 CH |
3681 | for (i = 0; i < log->l_iclog_bufs; i++) { |
3682 | if (ptr >= log->l_iclog_bak[i] && | |
3683 | ptr <= log->l_iclog_bak[i] + log->l_iclog_size) | |
1da177e4 LT |
3684 | good_ptr++; |
3685 | } | |
e6b1f273 CH |
3686 | |
3687 | if (!good_ptr) | |
a0fa2b67 | 3688 | xfs_emerg(log->l_mp, "%s: invalid ptr", __func__); |
e6b1f273 | 3689 | } |
1da177e4 | 3690 | |
da8a1a4a DC |
3691 | /* |
3692 | * Check to make sure the grant write head didn't just over lap the tail. If | |
3693 | * the cycles are the same, we can't be overlapping. Otherwise, make sure that | |
3694 | * the cycles differ by exactly one and check the byte count. | |
3695 | * | |
3696 | * This check is run unlocked, so can give false positives. Rather than assert | |
3697 | * on failures, use a warn-once flag and a panic tag to allow the admin to | |
3698 | * determine if they want to panic the machine when such an error occurs. For | |
3699 | * debug kernels this will have the same effect as using an assert but, unlinke | |
3700 | * an assert, it can be turned off at runtime. | |
3701 | */ | |
3f336c6f DC |
3702 | STATIC void |
3703 | xlog_verify_grant_tail( | |
ad223e60 | 3704 | struct xlog *log) |
3f336c6f | 3705 | { |
1c3cb9ec | 3706 | int tail_cycle, tail_blocks; |
a69ed03c | 3707 | int cycle, space; |
3f336c6f | 3708 | |
28496968 | 3709 | xlog_crack_grant_head(&log->l_write_head.grant, &cycle, &space); |
1c3cb9ec DC |
3710 | xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_blocks); |
3711 | if (tail_cycle != cycle) { | |
da8a1a4a | 3712 | if (cycle - 1 != tail_cycle && |
e1d06e5f | 3713 | !test_and_set_bit(XLOG_TAIL_WARN, &log->l_opstate)) { |
da8a1a4a DC |
3714 | xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES, |
3715 | "%s: cycle - 1 != tail_cycle", __func__); | |
da8a1a4a DC |
3716 | } |
3717 | ||
3718 | if (space > BBTOB(tail_blocks) && | |
e1d06e5f | 3719 | !test_and_set_bit(XLOG_TAIL_WARN, &log->l_opstate)) { |
da8a1a4a DC |
3720 | xfs_alert_tag(log->l_mp, XFS_PTAG_LOGRES, |
3721 | "%s: space > BBTOB(tail_blocks)", __func__); | |
da8a1a4a | 3722 | } |
3f336c6f DC |
3723 | } |
3724 | } | |
3725 | ||
1da177e4 LT |
3726 | /* check if it will fit */ |
3727 | STATIC void | |
9a8d2fdb MT |
3728 | xlog_verify_tail_lsn( |
3729 | struct xlog *log, | |
9d110014 | 3730 | struct xlog_in_core *iclog) |
1da177e4 | 3731 | { |
9d110014 DC |
3732 | xfs_lsn_t tail_lsn = be64_to_cpu(iclog->ic_header.h_tail_lsn); |
3733 | int blocks; | |
1da177e4 LT |
3734 | |
3735 | if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) { | |
3736 | blocks = | |
3737 | log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn)); | |
3738 | if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize)) | |
a0fa2b67 | 3739 | xfs_emerg(log->l_mp, "%s: ran out of log space", __func__); |
1da177e4 LT |
3740 | } else { |
3741 | ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle); | |
3742 | ||
3743 | if (BLOCK_LSN(tail_lsn) == log->l_prev_block) | |
a0fa2b67 | 3744 | xfs_emerg(log->l_mp, "%s: tail wrapped", __func__); |
1da177e4 LT |
3745 | |
3746 | blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block; | |
3747 | if (blocks < BTOBB(iclog->ic_offset) + 1) | |
a0fa2b67 | 3748 | xfs_emerg(log->l_mp, "%s: ran out of log space", __func__); |
1da177e4 | 3749 | } |
b843299b | 3750 | } |
1da177e4 LT |
3751 | |
3752 | /* | |
3753 | * Perform a number of checks on the iclog before writing to disk. | |
3754 | * | |
3755 | * 1. Make sure the iclogs are still circular | |
3756 | * 2. Make sure we have a good magic number | |
3757 | * 3. Make sure we don't have magic numbers in the data | |
3758 | * 4. Check fields of each log operation header for: | |
3759 | * A. Valid client identifier | |
3760 | * B. tid ptr value falls in valid ptr space (user space code) | |
3761 | * C. Length in log record header is correct according to the | |
3762 | * individual operation headers within record. | |
3763 | * 5. When a bwrite will occur within 5 blocks of the front of the physical | |
3764 | * log, check the preceding blocks of the physical log to make sure all | |
3765 | * the cycle numbers agree with the current cycle number. | |
3766 | */ | |
3767 | STATIC void | |
9a8d2fdb MT |
3768 | xlog_verify_iclog( |
3769 | struct xlog *log, | |
3770 | struct xlog_in_core *iclog, | |
abca1f33 | 3771 | int count) |
1da177e4 LT |
3772 | { |
3773 | xlog_op_header_t *ophead; | |
3774 | xlog_in_core_t *icptr; | |
3775 | xlog_in_core_2_t *xhdr; | |
5809d5e0 | 3776 | void *base_ptr, *ptr, *p; |
db9d67d6 | 3777 | ptrdiff_t field_offset; |
c8ce540d | 3778 | uint8_t clientid; |
1da177e4 LT |
3779 | int len, i, j, k, op_len; |
3780 | int idx; | |
1da177e4 LT |
3781 | |
3782 | /* check validity of iclog pointers */ | |
b22cd72c | 3783 | spin_lock(&log->l_icloglock); |
1da177e4 | 3784 | icptr = log->l_iclog; |
643f7c4e GB |
3785 | for (i = 0; i < log->l_iclog_bufs; i++, icptr = icptr->ic_next) |
3786 | ASSERT(icptr); | |
3787 | ||
1da177e4 | 3788 | if (icptr != log->l_iclog) |
a0fa2b67 | 3789 | xfs_emerg(log->l_mp, "%s: corrupt iclog ring", __func__); |
b22cd72c | 3790 | spin_unlock(&log->l_icloglock); |
1da177e4 LT |
3791 | |
3792 | /* check log magic numbers */ | |
69ef921b | 3793 | if (iclog->ic_header.h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) |
a0fa2b67 | 3794 | xfs_emerg(log->l_mp, "%s: invalid magic num", __func__); |
1da177e4 | 3795 | |
5809d5e0 CH |
3796 | base_ptr = ptr = &iclog->ic_header; |
3797 | p = &iclog->ic_header; | |
3798 | for (ptr += BBSIZE; ptr < base_ptr + count; ptr += BBSIZE) { | |
69ef921b | 3799 | if (*(__be32 *)ptr == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) |
a0fa2b67 DC |
3800 | xfs_emerg(log->l_mp, "%s: unexpected magic num", |
3801 | __func__); | |
1da177e4 LT |
3802 | } |
3803 | ||
3804 | /* check fields */ | |
b53e675d | 3805 | len = be32_to_cpu(iclog->ic_header.h_num_logops); |
5809d5e0 CH |
3806 | base_ptr = ptr = iclog->ic_datap; |
3807 | ophead = ptr; | |
b28708d6 | 3808 | xhdr = iclog->ic_data; |
1da177e4 | 3809 | for (i = 0; i < len; i++) { |
5809d5e0 | 3810 | ophead = ptr; |
1da177e4 LT |
3811 | |
3812 | /* clientid is only 1 byte */ | |
5809d5e0 CH |
3813 | p = &ophead->oh_clientid; |
3814 | field_offset = p - base_ptr; | |
abca1f33 | 3815 | if (field_offset & 0x1ff) { |
1da177e4 LT |
3816 | clientid = ophead->oh_clientid; |
3817 | } else { | |
b2a922cd | 3818 | idx = BTOBBT((char *)&ophead->oh_clientid - iclog->ic_datap); |
1da177e4 LT |
3819 | if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) { |
3820 | j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE); | |
3821 | k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE); | |
03bea6fe CH |
3822 | clientid = xlog_get_client_id( |
3823 | xhdr[j].hic_xheader.xh_cycle_data[k]); | |
1da177e4 | 3824 | } else { |
03bea6fe CH |
3825 | clientid = xlog_get_client_id( |
3826 | iclog->ic_header.h_cycle_data[idx]); | |
1da177e4 LT |
3827 | } |
3828 | } | |
3829 | if (clientid != XFS_TRANSACTION && clientid != XFS_LOG) | |
a0fa2b67 | 3830 | xfs_warn(log->l_mp, |
c9690043 | 3831 | "%s: invalid clientid %d op "PTR_FMT" offset 0x%lx", |
a0fa2b67 DC |
3832 | __func__, clientid, ophead, |
3833 | (unsigned long)field_offset); | |
1da177e4 LT |
3834 | |
3835 | /* check length */ | |
5809d5e0 CH |
3836 | p = &ophead->oh_len; |
3837 | field_offset = p - base_ptr; | |
abca1f33 | 3838 | if (field_offset & 0x1ff) { |
67fcb7bf | 3839 | op_len = be32_to_cpu(ophead->oh_len); |
1da177e4 | 3840 | } else { |
db9d67d6 CH |
3841 | idx = BTOBBT((uintptr_t)&ophead->oh_len - |
3842 | (uintptr_t)iclog->ic_datap); | |
1da177e4 LT |
3843 | if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) { |
3844 | j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE); | |
3845 | k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE); | |
b53e675d | 3846 | op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]); |
1da177e4 | 3847 | } else { |
b53e675d | 3848 | op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]); |
1da177e4 LT |
3849 | } |
3850 | } | |
3851 | ptr += sizeof(xlog_op_header_t) + op_len; | |
3852 | } | |
b843299b | 3853 | } |
cfcbbbd0 | 3854 | #endif |
1da177e4 | 3855 | |
1da177e4 | 3856 | /* |
b36d4651 DC |
3857 | * Perform a forced shutdown on the log. This should be called once and once |
3858 | * only by the high level filesystem shutdown code to shut the log subsystem | |
3859 | * down cleanly. | |
9da1ab18 | 3860 | * |
b36d4651 DC |
3861 | * Our main objectives here are to make sure that: |
3862 | * a. if the shutdown was not due to a log IO error, flush the logs to | |
3863 | * disk. Anything modified after this is ignored. | |
3864 | * b. the log gets atomically marked 'XLOG_IO_ERROR' for all interested | |
3865 | * parties to find out. Nothing new gets queued after this is done. | |
3866 | * c. Tasks sleeping on log reservations, pinned objects and | |
3867 | * other resources get woken up. | |
5112e206 | 3868 | * |
b36d4651 DC |
3869 | * Return true if the shutdown cause was a log IO error and we actually shut the |
3870 | * log down. | |
1da177e4 | 3871 | */ |
b36d4651 DC |
3872 | bool |
3873 | xlog_force_shutdown( | |
3874 | struct xlog *log, | |
3875 | int shutdown_flags) | |
1da177e4 | 3876 | { |
b36d4651 | 3877 | bool log_error = (shutdown_flags & SHUTDOWN_LOG_IO_ERROR); |
1da177e4 LT |
3878 | |
3879 | /* | |
b36d4651 DC |
3880 | * If this happens during log recovery then we aren't using the runtime |
3881 | * log mechanisms yet so there's nothing to shut down. | |
1da177e4 | 3882 | */ |
b36d4651 DC |
3883 | if (!log || xlog_in_recovery(log)) |
3884 | return false; | |
1da177e4 | 3885 | |
b36d4651 | 3886 | ASSERT(!xlog_is_shutdown(log)); |
9da1ab18 DC |
3887 | |
3888 | /* | |
a870fe6d | 3889 | * Flush all the completed transactions to disk before marking the log |
b36d4651 DC |
3890 | * being shut down. We need to do this first as shutting down the log |
3891 | * before the force will prevent the log force from flushing the iclogs | |
3892 | * to disk. | |
3893 | * | |
3894 | * Re-entry due to a log IO error shutdown during the log force is | |
3895 | * prevented by the atomicity of higher level shutdown code. | |
9da1ab18 | 3896 | */ |
b36d4651 DC |
3897 | if (!log_error) |
3898 | xfs_log_force(log->l_mp, XFS_LOG_SYNC); | |
9da1ab18 | 3899 | |
1da177e4 | 3900 | /* |
b36d4651 DC |
3901 | * Atomically set the shutdown state. If the shutdown state is already |
3902 | * set, there someone else is performing the shutdown and so we are done | |
3903 | * here. This should never happen because we should only ever get called | |
3904 | * once by the first shutdown caller. | |
3905 | * | |
3906 | * Much of the log state machine transitions assume that shutdown state | |
3907 | * cannot change once they hold the log->l_icloglock. Hence we need to | |
3908 | * hold that lock here, even though we use the atomic test_and_set_bit() | |
3909 | * operation to set the shutdown state. | |
1da177e4 | 3910 | */ |
b22cd72c | 3911 | spin_lock(&log->l_icloglock); |
b36d4651 DC |
3912 | if (test_and_set_bit(XLOG_IO_ERROR, &log->l_opstate)) { |
3913 | spin_unlock(&log->l_icloglock); | |
3914 | ASSERT(0); | |
3915 | return false; | |
3916 | } | |
b22cd72c | 3917 | spin_unlock(&log->l_icloglock); |
1da177e4 LT |
3918 | |
3919 | /* | |
10547941 DC |
3920 | * We don't want anybody waiting for log reservations after this. That |
3921 | * means we have to wake up everybody queued up on reserveq as well as | |
3922 | * writeq. In addition, we make sure in xlog_{re}grant_log_space that | |
3923 | * we don't enqueue anything once the SHUTDOWN flag is set, and this | |
3f16b985 | 3924 | * action is protected by the grant locks. |
1da177e4 | 3925 | */ |
a79bf2d7 CH |
3926 | xlog_grant_head_wake_all(&log->l_reserve_head); |
3927 | xlog_grant_head_wake_all(&log->l_write_head); | |
1da177e4 | 3928 | |
1da177e4 | 3929 | /* |
ac983517 DC |
3930 | * Wake up everybody waiting on xfs_log_force. Wake the CIL push first |
3931 | * as if the log writes were completed. The abort handling in the log | |
3932 | * item committed callback functions will do this again under lock to | |
3933 | * avoid races. | |
1da177e4 | 3934 | */ |
cdea5459 | 3935 | spin_lock(&log->l_cilp->xc_push_lock); |
ac983517 | 3936 | wake_up_all(&log->l_cilp->xc_commit_wait); |
cdea5459 | 3937 | spin_unlock(&log->l_cilp->xc_push_lock); |
aad7272a | 3938 | xlog_state_shutdown_callbacks(log); |
1da177e4 | 3939 | |
b36d4651 | 3940 | return log_error; |
1da177e4 LT |
3941 | } |
3942 | ||
ba0f32d4 | 3943 | STATIC int |
9a8d2fdb MT |
3944 | xlog_iclogs_empty( |
3945 | struct xlog *log) | |
1da177e4 LT |
3946 | { |
3947 | xlog_in_core_t *iclog; | |
3948 | ||
3949 | iclog = log->l_iclog; | |
3950 | do { | |
3951 | /* endianness does not matter here, zero is zero in | |
3952 | * any language. | |
3953 | */ | |
3954 | if (iclog->ic_header.h_num_logops) | |
014c2544 | 3955 | return 0; |
1da177e4 LT |
3956 | iclog = iclog->ic_next; |
3957 | } while (iclog != log->l_iclog); | |
014c2544 | 3958 | return 1; |
1da177e4 | 3959 | } |
f661f1e0 | 3960 | |
a45086e2 BF |
3961 | /* |
3962 | * Verify that an LSN stamped into a piece of metadata is valid. This is | |
3963 | * intended for use in read verifiers on v5 superblocks. | |
3964 | */ | |
3965 | bool | |
3966 | xfs_log_check_lsn( | |
3967 | struct xfs_mount *mp, | |
3968 | xfs_lsn_t lsn) | |
3969 | { | |
3970 | struct xlog *log = mp->m_log; | |
3971 | bool valid; | |
3972 | ||
3973 | /* | |
3974 | * norecovery mode skips mount-time log processing and unconditionally | |
3975 | * resets the in-core LSN. We can't validate in this mode, but | |
3976 | * modifications are not allowed anyways so just return true. | |
3977 | */ | |
3978 | if (mp->m_flags & XFS_MOUNT_NORECOVERY) | |
3979 | return true; | |
3980 | ||
3981 | /* | |
3982 | * Some metadata LSNs are initialized to NULL (e.g., the agfl). This is | |
3983 | * handled by recovery and thus safe to ignore here. | |
3984 | */ | |
3985 | if (lsn == NULLCOMMITLSN) | |
3986 | return true; | |
3987 | ||
3988 | valid = xlog_valid_lsn(mp->m_log, lsn); | |
3989 | ||
3990 | /* warn the user about what's gone wrong before verifier failure */ | |
3991 | if (!valid) { | |
3992 | spin_lock(&log->l_icloglock); | |
3993 | xfs_warn(mp, | |
3994 | "Corruption warning: Metadata has LSN (%d:%d) ahead of current LSN (%d:%d). " | |
3995 | "Please unmount and run xfs_repair (>= v4.3) to resolve.", | |
3996 | CYCLE_LSN(lsn), BLOCK_LSN(lsn), | |
3997 | log->l_curr_cycle, log->l_curr_block); | |
3998 | spin_unlock(&log->l_icloglock); | |
3999 | } | |
4000 | ||
4001 | return valid; | |
4002 | } | |
0c60d3aa | 4003 | |
2b73a2c8 DW |
4004 | /* |
4005 | * Notify the log that we're about to start using a feature that is protected | |
4006 | * by a log incompat feature flag. This will prevent log covering from | |
4007 | * clearing those flags. | |
4008 | */ | |
4009 | void | |
4010 | xlog_use_incompat_feat( | |
4011 | struct xlog *log) | |
4012 | { | |
4013 | down_read(&log->l_incompat_users); | |
4014 | } | |
4015 | ||
4016 | /* Notify the log that we've finished using log incompat features. */ | |
4017 | void | |
4018 | xlog_drop_incompat_feat( | |
4019 | struct xlog *log) | |
4020 | { | |
4021 | up_read(&log->l_incompat_users); | |
4022 | } |