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