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