DragonFlyBSD Kernel Audit
sys/vfs/hammer/hammer_flusher.c
← back
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
/*
 * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
 *
 * This code is derived from software contributed to The DragonFly Project
 * by Matthew Dillon <dillon@backplane.com>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name of The DragonFly Project nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific, prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $DragonFly: src/sys/vfs/hammer/hammer_flusher.c,v 1.45 2008/07/31 04:42:04 dillon Exp $
 */
/*
 * HAMMER dependancy flusher thread
 *
 * Meta data updates create buffer dependancies which are arranged as a
 * hierarchy of lists.
 */

#include "hammer.h"

static void hammer_flusher_master_thread(void *arg);
static void hammer_flusher_slave_thread(void *arg);
static int hammer_flusher_flush(hammer_mount_t hmp, int *nomorep);
static int hammer_flusher_flush_inode(hammer_inode_t ip, void *data);

RB_GENERATE(hammer_fls_rb_tree, hammer_inode, rb_flsnode,
              hammer_ino_rb_compare);

/*
 * Support structures for the flusher threads.
 */
typedef struct hammer_flusher_info {
	TAILQ_ENTRY(hammer_flusher_info) entry;
	hammer_mount_t	hmp;
	thread_t	td;
	int		runstate;
	hammer_flush_group_t flg;
	struct hammer_transaction trans;        /* per-slave transaction */
} *hammer_flusher_info_t;

/*
 * Sync all inodes pending on the flusher.
 *
 * All flush groups will be flushed.  This does not queue dirty inodes
 * to the flush groups, it just flushes out what has already been queued!
 */
void
hammer_flusher_sync(hammer_mount_t hmp)
{
	int seq;

	seq = hammer_flusher_async(hmp, NULL);
	hammer_flusher_wait(hmp, seq);
}

/*
 * Sync all flush groups through to close_flg - return immediately.
 * If close_flg is NULL all flush groups are synced.
 *
 * Returns the sequence number of the last closed flush group,
 * which may be close_flg.  When syncing to the end if there
 * are no flush groups pending we still cycle the flusher, and
 * must allocate a sequence number to placemark the spot even
 * though no flush group will ever be associated with it.
 */
int
hammer_flusher_async(hammer_mount_t hmp, hammer_flush_group_t close_flg)
{
	hammer_flush_group_t flg;
	int seq;

	/*
	 * Already closed
	 */
	if (close_flg && close_flg->closed)
		return(close_flg->seq);

	/*
	 * Close flush groups until we hit the end of the list
	 * or close_flg.
	 */
	while ((flg = hmp->next_flush_group) != NULL) {
		KKASSERT(flg->closed == 0 && flg->running == 0);
		flg->closed = 1;
		hmp->next_flush_group = TAILQ_NEXT(flg, flush_entry);
		if (flg == close_flg)
			break;
	}

	if (hmp->flusher.td) {
		if (hmp->flusher.signal++ == 0)
			wakeup(&hmp->flusher.signal);
		if (flg) {
			seq = flg->seq;
		} else {
			seq = hmp->flusher.next;
			++hmp->flusher.next;
		}
	} else {
		seq = hmp->flusher.done;
	}
	return(seq);
}

/*
 * Flush the current/next flushable flg.  This function is typically called
 * in a loop along with hammer_flusher_wait(hmp, returned_seq) to iterate
 * flush groups until specific conditions are met.
 *
 * If a flush is currently in progress its seq is returned.
 *
 * If no flush is currently in progress the next available flush group
 * will be flushed and its seq returned.
 *
 * If no flush groups are present a dummy seq will be allocated and
 * returned and the flusher will be activated (e.g. to flush the
 * undo/redo and the volume header).
 */
int
hammer_flusher_async_one(hammer_mount_t hmp)
{
	hammer_flush_group_t flg;
	int seq;

	if (hmp->flusher.td) {
		flg = TAILQ_FIRST(&hmp->flush_group_list);
		seq = hammer_flusher_async(hmp, flg);
	} else {
		seq = hmp->flusher.done;
	}
	return(seq);
}

/*
 * Wait for the flusher to finish flushing the specified sequence
 * number.  The flush is already running and will signal us on
 * each completion.
 */
void
hammer_flusher_wait(hammer_mount_t hmp, int seq)
{
	while (seq - hmp->flusher.done > 0)
		tsleep(&hmp->flusher.done, 0, "hmrfls", 0);
}

/*
 * Returns non-zero if the flusher is currently running.  Used for
 * time-domain multiplexing of frontend operations in order to avoid
 * starving the backend flusher.
 */
int
hammer_flusher_running(hammer_mount_t hmp)
{
	int seq = hmp->flusher.next - 1;
	if (seq - hmp->flusher.done > 0)
		return(1);
	return (0);
}

void
hammer_flusher_wait_next(hammer_mount_t hmp)
{
	int seq;

	seq = hammer_flusher_async_one(hmp);
	hammer_flusher_wait(hmp, seq);
}

void
hammer_flusher_create(hammer_mount_t hmp)
{
	hammer_flusher_info_t info;
	int i;

	hmp->flusher.signal = 0;
	hmp->flusher.done = 0;
	hmp->flusher.next = 1;
	hammer_ref(&hmp->flusher.finalize_lock);
	TAILQ_INIT(&hmp->flusher.run_list);
	TAILQ_INIT(&hmp->flusher.ready_list);

	lwkt_create(hammer_flusher_master_thread, hmp,
		    &hmp->flusher.td, NULL, 0, -1, "hammer-M");
	for (i = 0; i < HAMMER_MAX_FLUSHERS; ++i) {
		info = kmalloc(sizeof(*info), hmp->m_misc, M_WAITOK|M_ZERO);
		info->hmp = hmp;
		TAILQ_INSERT_TAIL(&hmp->flusher.ready_list, info, entry);
		lwkt_create(hammer_flusher_slave_thread, info,
			    &info->td, NULL, 0, -1, "hammer-S%d", i);
	}
}

void
hammer_flusher_destroy(hammer_mount_t hmp)
{
	hammer_flusher_info_t info;

	/*
	 * Kill the master
	 */
	hmp->flusher.exiting = 1;
	while (hmp->flusher.td) {
		++hmp->flusher.signal;
		wakeup(&hmp->flusher.signal);
		tsleep(&hmp->flusher.exiting, 0, "hmrwex", hz);
	}

	/*
	 * Kill the slaves
	 */
	while ((info = TAILQ_FIRST(&hmp->flusher.ready_list)) != NULL) {
		KKASSERT(info->runstate == 0);
		TAILQ_REMOVE(&hmp->flusher.ready_list, info, entry);
		info->runstate = -1;
		wakeup(&info->runstate);
		while (info->td)
			tsleep(&info->td, 0, "hmrwwc", 0);
		kfree(info, hmp->m_misc);
	}
}

/*
 * The master flusher thread manages the flusher sequence id and
 * synchronization with the slave work threads.
 */
static void
hammer_flusher_master_thread(void *arg)
{
	hammer_mount_t hmp;
	int seq;
	int nomore;

	hmp = arg;

	lwkt_gettoken(&hmp->fs_token);

	for (;;) {
		/*
		 * Flush all sequence numbers up to but not including .next,
		 * or until an open flush group is encountered.
		 */
		for (;;) {
			while (hmp->flusher.group_lock)
				tsleep(&hmp->flusher.group_lock, 0, "hmrhld",0);
			hammer_flusher_clean_loose_ios(hmp);

			seq = hammer_flusher_flush(hmp, &nomore);
			hmp->flusher.done = seq;
			wakeup(&hmp->flusher.done);

			if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR)
				break;
			if (nomore)
				break;
		}

		/*
		 * Wait for activity.
		 */
		if (hmp->flusher.exiting && TAILQ_EMPTY(&hmp->flush_group_list))
			break;
		while (hmp->flusher.signal == 0)
			tsleep(&hmp->flusher.signal, 0, "hmrwwa", 0);
		hmp->flusher.signal = 0;
	}

	/*
	 * And we are done.
	 */
	hmp->flusher.td = NULL;
	wakeup(&hmp->flusher.exiting);
	lwkt_reltoken(&hmp->fs_token);
	lwkt_exit();
}

/*
 * Flush the next sequence number until an open flush group is encountered
 * or we reach (next).  Not all sequence numbers will have flush groups
 * associated with them.  These require that the UNDO/REDO FIFO still be
 * flushed since it can take at least one additional run to synchronize
 * the FIFO, and more to also synchronize the reserve structures.
 */
static int
hammer_flusher_flush(hammer_mount_t hmp, int *nomorep)
{
	hammer_flusher_info_t info;
	hammer_flush_group_t flg;
	hammer_reserve_t resv;
	int count;
	int seq;

	/*
	 * Just in-case there's a flush race on mount.  Seq number
	 * does not change.
	 */
	if (TAILQ_FIRST(&hmp->flusher.ready_list) == NULL) {
		*nomorep = 1;
		return (hmp->flusher.done);
	}
	*nomorep = 0;

	/*
	 * Flush the next sequence number.  Sequence numbers can exist
	 * without an assigned flush group, indicating that just a FIFO flush
	 * should occur.
	 */
	seq = hmp->flusher.done + 1;
	flg = TAILQ_FIRST(&hmp->flush_group_list);
	if (flg == NULL) {
		if (seq == hmp->flusher.next) {
			*nomorep = 1;
			return (hmp->flusher.done);
		}
	} else if (seq == flg->seq) {
		if (flg->closed) {
			KKASSERT(flg->running == 0);
			flg->running = 1;
			if (hmp->fill_flush_group == flg) {
				hmp->fill_flush_group =
					TAILQ_NEXT(flg, flush_entry);
			}
		} else {
			*nomorep = 1;
			return (hmp->flusher.done);
		}
	} else {
		/*
		 * Sequence number problems can only happen if a critical
		 * filesystem error occurred which forced the filesystem into
		 * read-only mode.
		 */
		KKASSERT(flg->seq - seq > 0 || hmp->ronly >= 2);
		flg = NULL;
	}

	/*
	 * We only do one flg but we may have to loop/retry.
	 *
	 * Due to various races it is possible to come across a flush
	 * group which as not yet been closed.
	 */
	count = 0;
	while (flg && flg->running) {
		++count;
		if (hammer_debug_general & 0x0001) {
			hdkprintf("%d ttl=%d recs=%d\n",
				flg->seq, flg->total_count, flg->refs);
		}
		if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR)
			break;
		hammer_start_transaction_fls(&hmp->flusher.trans, hmp);

		/*
		 * If the previous flush cycle just about exhausted our
		 * UNDO space we may have to do a dummy cycle to move the
		 * first_offset up before actually digging into a new cycle,
		 * or the new cycle will not have sufficient undo space.
		 */
		if (hammer_flusher_undo_exhausted(&hmp->flusher.trans, 3))
			hammer_flusher_finalize(&hmp->flusher.trans, 0);

		KKASSERT(hmp->next_flush_group != flg);

		/*
		 * Place the flg in the flusher structure and start the
		 * slaves running.  The slaves will compete for inodes
		 * to flush.
		 *
		 * Make a per-thread copy of the transaction.
		 */
		while ((info = TAILQ_FIRST(&hmp->flusher.ready_list)) != NULL) {
			TAILQ_REMOVE(&hmp->flusher.ready_list, info, entry);
			info->flg = flg;
			info->runstate = 1;
			info->trans = hmp->flusher.trans;
			TAILQ_INSERT_TAIL(&hmp->flusher.run_list, info, entry);
			wakeup(&info->runstate);
		}

		/*
		 * Wait for all slaves to finish running
		 */
		while (TAILQ_FIRST(&hmp->flusher.run_list) != NULL)
			tsleep(&hmp->flusher.ready_list, 0, "hmrfcc", 0);

		/*
		 * Do the final finalization, clean up
		 */
		hammer_flusher_finalize(&hmp->flusher.trans, 1);
		hmp->flusher.tid = hmp->flusher.trans.tid;

		hammer_done_transaction(&hmp->flusher.trans);

		/*
		 * Loop up on the same flg.  If the flg is done clean it up
		 * and break out.  We only flush one flg.
		 */
		if (RB_EMPTY(&flg->flush_tree)) {
			KKASSERT(flg->refs == 0);
			TAILQ_REMOVE(&hmp->flush_group_list, flg, flush_entry);
			kfree(flg, hmp->m_misc);
			break;
		}
		KKASSERT(TAILQ_FIRST(&hmp->flush_group_list) == flg);
	}

	/*
	 * We may have pure meta-data to flush, or we may have to finish
	 * cycling the UNDO FIFO, even if there were no flush groups.
	 */
	if (count == 0 && hammer_flusher_haswork(hmp)) {
		hammer_start_transaction_fls(&hmp->flusher.trans, hmp);
		hammer_flusher_finalize(&hmp->flusher.trans, 1);
		hammer_done_transaction(&hmp->flusher.trans);
	}

	/*
	 * Clean up any freed big-blocks (typically zone-2).
	 * resv->flush_group is typically set several flush groups ahead
	 * of the free to ensure that the freed block is not reused until
	 * it can no longer be reused.
	 */
	while ((resv = TAILQ_FIRST(&hmp->delay_list)) != NULL) {
		if (resv->flg_no - seq > 0)
			break;
		hammer_reserve_clrdelay(hmp, resv);
	}
	return (seq);
}


/*
 * The slave flusher thread pulls work off the master flush list until no
 * work is left.
 */
static void
hammer_flusher_slave_thread(void *arg)
{
	hammer_flush_group_t flg;
	hammer_flusher_info_t info;
	hammer_mount_t hmp;

	info = arg;
	hmp = info->hmp;
	lwkt_gettoken(&hmp->fs_token);

	for (;;) {
		while (info->runstate == 0)
			tsleep(&info->runstate, 0, "hmrssw", 0);
		if (info->runstate < 0)
			break;
		flg = info->flg;

		RB_SCAN(hammer_fls_rb_tree, &flg->flush_tree, NULL,
			hammer_flusher_flush_inode, info);

		info->runstate = 0;
		info->flg = NULL;
		TAILQ_REMOVE(&hmp->flusher.run_list, info, entry);
		TAILQ_INSERT_TAIL(&hmp->flusher.ready_list, info, entry);
		wakeup(&hmp->flusher.ready_list);
	}
	info->td = NULL;
	wakeup(&info->td);
	lwkt_reltoken(&hmp->fs_token);
	lwkt_exit();
}

void
hammer_flusher_clean_loose_ios(hammer_mount_t hmp)
{
	hammer_buffer_t buffer;
	hammer_io_t io;

	/*
	 * loose ends - buffers without bp's aren't tracked by the kernel
	 * and can build up, so clean them out.  This can occur when an
	 * IO completes on a buffer with no references left.
	 *
	 * The io_token is needed to protect the list.
	 */
	if ((io = RB_ROOT(&hmp->lose_root)) != NULL) {
		lwkt_gettoken(&hmp->io_token);
		while ((io = RB_ROOT(&hmp->lose_root)) != NULL) {
			KKASSERT(io->mod_root == &hmp->lose_root);
			RB_REMOVE(hammer_mod_rb_tree, io->mod_root, io);
			io->mod_root = NULL;
			hammer_ref(&io->lock);
			buffer = (void *)io;
			hammer_rel_buffer(buffer, 0);
		}
		lwkt_reltoken(&hmp->io_token);
	}
}

/*
 * Flush a single inode that is part of a flush group.
 *
 * Flusher errors are extremely serious, even ENOSPC shouldn't occur because
 * the front-end should have reserved sufficient space on the media.  Any
 * error other then EWOULDBLOCK will force the mount to be read-only.
 */
static
int
hammer_flusher_flush_inode(hammer_inode_t ip, void *data)
{
	hammer_flusher_info_t info = data;
	hammer_mount_t hmp = info->hmp;
	hammer_transaction_t trans = &info->trans;
	int error;

	/*
	 * Several slaves are operating on the same flush group concurrently.
	 * The SLAVEFLUSH flag prevents them from tripping over each other.
	 *
	 * NOTE: It is possible for a EWOULDBLOCK'd ip returned by one slave
	 *	 to be resynced by another, but normally such inodes are not
	 *	 revisited until the master loop gets to them.
	 */
	if (ip->flags & HAMMER_INODE_SLAVEFLUSH)
		return(0);
	ip->flags |= HAMMER_INODE_SLAVEFLUSH;
	++hammer_stats_inode_flushes;

	hammer_flusher_clean_loose_ios(hmp);
	vm_wait_nominal();
	error = hammer_sync_inode(trans, ip);

	/*
	 * EWOULDBLOCK can happen under normal operation, all other errors
	 * are considered extremely serious.  We must set WOULDBLOCK
	 * mechanics to deal with the mess left over from the abort of the
	 * previous flush.
	 */
	if (error) {
		ip->flags |= HAMMER_INODE_WOULDBLOCK;
		if (error == EWOULDBLOCK)
			error = 0;
	}
	hammer_sync_inode_done(ip, error);
	/* ip invalid */

	while (hmp->flusher.finalize_want)
		tsleep(&hmp->flusher.finalize_want, 0, "hmrsxx", 0);
	if (hammer_flusher_undo_exhausted(trans, 1)) {
		hkprintf("Warning: UNDO area too small!\n");
		hammer_flusher_finalize(trans, 1);
	} else if (hammer_flusher_meta_limit(trans->hmp)) {
		hammer_flusher_finalize(trans, 0);
	}
	return (0);
}

/*
 * Return non-zero if the UNDO area has less then (QUARTER / 4) of its
 * space left.
 *
 * 1/4 - Emergency free undo space level.  Below this point the flusher
 *	 will finalize even if directory dependancies have not been resolved.
 *
 * 2/4 - Used by the pruning and reblocking code.  These functions may be
 *	 running in parallel with a flush and cannot be allowed to drop
 *	 available undo space to emergency levels.
 *
 * 3/4 - Used at the beginning of a flush to force-sync the volume header
 *	 to give the flush plenty of runway to work in.
 */
int
hammer_flusher_undo_exhausted(hammer_transaction_t trans, int quarter)
{
	if (hammer_undo_space(trans) <
	    hammer_undo_max(trans->hmp) * quarter / 4) {
		return(1);
	} else {
		return(0);
	}
}

/*
 * Flush all pending UNDOs, wait for write completion, update the volume
 * header with the new UNDO end position, and flush it.  Then
 * asynchronously flush the meta-data.
 *
 * If this is the last finalization in a flush group we also synchronize
 * our cached blockmap and set hmp->flusher_undo_start and our cached undo
 * fifo first_offset so the next flush resets the FIFO pointers.
 *
 * If this is not final it is being called because too many dirty meta-data
 * buffers have built up and must be flushed with UNDO synchronization to
 * avoid a buffer cache deadlock.
 */
void
hammer_flusher_finalize(hammer_transaction_t trans, int final)
{
	hammer_volume_t root_volume;
	hammer_blockmap_t cundomap, dundomap;
	hammer_mount_t hmp;
	hammer_io_t io;
	hammer_off_t save_undo_next_offset;
	int count;
	int i;

	hmp = trans->hmp;
	root_volume = trans->rootvol;

	/*
	 * Exclusively lock the flusher.  This guarantees that all dirty
	 * buffers will be idled (have a mod-count of 0).
	 */
	++hmp->flusher.finalize_want;
	hammer_lock_ex(&hmp->flusher.finalize_lock);

	/*
	 * If this isn't the final sync several threads may have hit the
	 * meta-limit at the same time and raced.  Only sync if we really
	 * have to, after acquiring the lock.
	 */
	if (final == 0 && !hammer_flusher_meta_limit(hmp))
		goto done;

	if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR)
		goto done;

	/*
	 * Flush data buffers.  This can occur asynchronously and at any
	 * time.  We must interlock against the frontend direct-data write
	 * but do not have to acquire the sync-lock yet.
	 *
	 * These data buffers have already been collected prior to the
	 * related inode(s) getting queued to the flush group.
	 */
	count = 0;
	while ((io = RB_FIRST(hammer_mod_rb_tree, &hmp->data_root)) != NULL) {
		if (io->ioerror)
			break;
		hammer_ref(&io->lock);
		hammer_io_write_interlock(io);
		KKASSERT(io->type != HAMMER_IOTYPE_VOLUME);
		hammer_io_flush(io, 0);
		hammer_io_done_interlock(io);
		hammer_rel_buffer(HAMMER_ITOB(io), 0);
		hammer_io_limit_backlog(hmp);
		++count;
	}

	/*
	 * The sync-lock is required for the remaining sequence.  This lock
	 * prevents meta-data from being modified.
	 */
	hammer_sync_lock_ex(trans);

	/*
	 * If we have been asked to finalize the volume header sync the
	 * cached blockmap to the on-disk blockmap.  Generate an UNDO
	 * record for the update.
	 */
	if (final) {
		cundomap = &hmp->blockmap[0];
		dundomap = &root_volume->ondisk->vol0_blockmap[0];
		if (root_volume->io.modified) {
			hammer_modify_volume(trans, root_volume,
					     dundomap, sizeof(hmp->blockmap));
			for (i = 0; i < HAMMER_MAX_ZONES; ++i) {
				hammer_crc_set_blockmap(hmp->version,
							&cundomap[i]);
			}
			bcopy(cundomap, dundomap, sizeof(hmp->blockmap));
			hammer_modify_volume_done(root_volume);
		}
	}

	/*
	 * Flush UNDOs.  This can occur concurrently with the data flush
	 * because data writes never overwrite.
	 *
	 * This also waits for I/Os to complete and flushes the cache on
	 * the target disk.
	 *
	 * Record the UNDO append point as this can continue to change
	 * after we have flushed the UNDOs.
	 */
	cundomap = &hmp->blockmap[HAMMER_ZONE_UNDO_INDEX];
	hammer_lock_ex(&hmp->undo_lock);
	save_undo_next_offset = cundomap->next_offset;
	hammer_unlock(&hmp->undo_lock);
	hammer_flusher_flush_undos(hmp, HAMMER_FLUSH_UNDOS_FORCED);

	if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR)
		goto failed;

	/*
	 * HAMMER VERSION < 4:
	 *	Update the on-disk volume header with new UNDO FIFO end
	 *	position (do not generate new UNDO records for this change).
	 *	We have to do this for the UNDO FIFO whether (final) is
	 *	set or not in order for the UNDOs to be recognized on
	 *	recovery.
	 *
	 * HAMMER VERSION >= 4:
	 *	The UNDO FIFO data written above will be recognized on
	 *	recovery without us having to sync the volume header.
	 *
	 * Also update the on-disk next_tid field.  This does not require
	 * an UNDO.  However, because our TID is generated before we get
	 * the sync lock another sync may have beat us to the punch.
	 *
	 * This also has the side effect of updating first_offset based on
	 * a prior finalization when the first finalization of the next flush
	 * cycle occurs, removing any undo info from the prior finalization
	 * from consideration.
	 *
	 * The volume header will be flushed out synchronously.
	 */
	dundomap = &root_volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
	cundomap = &hmp->blockmap[HAMMER_ZONE_UNDO_INDEX];

	if (dundomap->first_offset != cundomap->first_offset ||
		   dundomap->next_offset != save_undo_next_offset) {
		hammer_modify_volume_noundo(NULL, root_volume);
		dundomap->first_offset = cundomap->first_offset;
		dundomap->next_offset = save_undo_next_offset;
		hammer_crc_set_blockmap(hmp->version, dundomap);
		hammer_modify_volume_done(root_volume);
	}

	/*
	 * vol0_next_tid is used for TID selection and is updated without
	 * an UNDO so we do not reuse a TID that may have been rolled-back.
	 *
	 * vol0_last_tid is the highest fully-synchronized TID.  It is
	 * set-up when the UNDO fifo is fully synced, later on (not here).
	 *
	 * The root volume can be open for modification by other threads
	 * generating UNDO or REDO records.  For example, reblocking,
	 * pruning, REDO mode fast-fsyncs, so the write interlock is
	 * mandatory.
	 */
	if (root_volume->io.modified) {
		hammer_modify_volume_noundo(NULL, root_volume);
		if (root_volume->ondisk->vol0_next_tid < trans->tid)
			root_volume->ondisk->vol0_next_tid = trans->tid;
		hammer_crc_set_volume(hmp->version, root_volume->ondisk);
		hammer_modify_volume_done(root_volume);
		hammer_io_write_interlock(&root_volume->io);
		hammer_io_flush(&root_volume->io, 0);
		hammer_io_done_interlock(&root_volume->io);
	}

	/*
	 * Wait for I/Os to complete.
	 *
	 * For HAMMER VERSION 4+ filesystems we do not have to wait for
	 * the I/O to complete as the new UNDO FIFO entries are recognized
	 * even without the volume header update.  This allows the volume
	 * header to flushed along with meta-data, significantly reducing
	 * flush overheads.
	 */
	hammer_flusher_clean_loose_ios(hmp);
	if (hmp->version < HAMMER_VOL_VERSION_FOUR)
		hammer_io_wait_all(hmp, "hmrfl3", 1);

	if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR)
		goto failed;

	/*
	 * Flush meta-data.  The meta-data will be undone if we crash
	 * so we can safely flush it asynchronously.  There is no need
	 * to wait for I/O to complete (or issue a synchronous disk flush).
	 *
	 * In fact, even if we did wait the meta-data will still be undone
	 * by a crash up until the next flush cycle due to the first_offset
	 * in the volume header for the UNDO FIFO not being adjusted until
	 * the following flush cycle.
	 *
	 * No io interlock is needed, bioops callbacks will not mess with
	 * meta data buffers.
	 */
	count = 0;
	while ((io = RB_FIRST(hammer_mod_rb_tree, &hmp->meta_root)) != NULL) {
		if (io->ioerror)
			break;
		KKASSERT(io->modify_refs == 0);
		hammer_ref(&io->lock);
		KKASSERT(io->type != HAMMER_IOTYPE_VOLUME);
		hammer_io_flush(io, 0);
		hammer_rel_buffer(HAMMER_ITOB(io), 0);
		hammer_io_limit_backlog(hmp);
		++count;
	}

	/*
	 * If this is the final finalization for the flush group set
	 * up for the next sequence by setting a new first_offset in
	 * our cached blockmap and clearing the undo history.
	 *
	 * Even though we have updated our cached first_offset, the on-disk
	 * first_offset still governs available-undo-space calculations.
	 *
	 * We synchronize to save_undo_next_offset rather than
	 * cundomap->next_offset because that is what we flushed out
	 * above.
	 *
	 * NOTE! UNDOs can only be added with the sync_lock held
	 *	 so we can clear the undo history without racing.
	 *	 REDOs can be added at any time which is why we
	 *	 have to be careful and use save_undo_next_offset
	 *	 when setting the new first_offset.
	 */
	if (final) {
		cundomap = &hmp->blockmap[HAMMER_ZONE_UNDO_INDEX];
		if (cundomap->first_offset != save_undo_next_offset) {
			cundomap->first_offset = save_undo_next_offset;
			hmp->hflags |= HMNT_UNDO_DIRTY;
		} else if (cundomap->first_offset != cundomap->next_offset) {
			hmp->hflags |= HMNT_UNDO_DIRTY;
		} else {
			hmp->hflags &= ~HMNT_UNDO_DIRTY;
		}
		hammer_clear_undo_history(hmp);

		/*
		 * Flush tid sequencing.  flush_tid1 is fully synchronized,
		 * meaning a crash will not roll it back.  flush_tid2 has
		 * been written out asynchronously and a crash will roll
		 * it back.  flush_tid1 is used for all mirroring masters.
		 */
		if (hmp->flush_tid1 != hmp->flush_tid2) {
			hmp->flush_tid1 = hmp->flush_tid2;
			wakeup(&hmp->flush_tid1);
		}
		hmp->flush_tid2 = trans->tid;

		/*
		 * Clear the REDO SYNC flag.  This flag is used to ensure
		 * that the recovery span in the UNDO/REDO FIFO contains
		 * at least one REDO SYNC record.
		 */
		hmp->flags &= ~HAMMER_MOUNT_REDO_SYNC;
	}

	/*
	 * Cleanup.  Report any critical errors.
	 */
failed:
	hammer_sync_unlock(trans);

	if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR) {
		hvkprintf(root_volume,
			"Critical write error during flush, "
			"refusing to sync UNDO FIFO\n");
	}

done:
	hammer_unlock(&hmp->flusher.finalize_lock);

	if (--hmp->flusher.finalize_want == 0)
		wakeup(&hmp->flusher.finalize_want);
	hammer_stats_commits += final;
}

/*
 * Flush UNDOs.
 */
void
hammer_flusher_flush_undos(hammer_mount_t hmp, int mode)
{
	hammer_io_t io;
	int count;

	count = 0;
	while ((io = RB_FIRST(hammer_mod_rb_tree, &hmp->undo_root)) != NULL) {
		if (io->ioerror)
			break;
		hammer_ref(&io->lock);
		KKASSERT(io->type != HAMMER_IOTYPE_VOLUME);
		hammer_io_write_interlock(io);
		hammer_io_flush(io, hammer_undo_reclaim(io));
		hammer_io_done_interlock(io);
		hammer_rel_buffer(HAMMER_ITOB(io), 0);
		hammer_io_limit_backlog(hmp);
		++count;
	}
	hammer_flusher_clean_loose_ios(hmp);
	if (mode == HAMMER_FLUSH_UNDOS_FORCED ||
	    (mode == HAMMER_FLUSH_UNDOS_AUTO && count)) {
		hammer_io_wait_all(hmp, "hmrfl1", 1);
	} else {
		hammer_io_wait_all(hmp, "hmrfl2", 0);
	}
}

/*
 * Return non-zero if too many dirty meta-data buffers have built up.
 *
 * Since we cannot allow such buffers to flush until we have dealt with
 * the UNDOs, we risk deadlocking the kernel's buffer cache.
 */
int
hammer_flusher_meta_limit(hammer_mount_t hmp)
{
	if (hmp->locked_dirty_space + hmp->io_running_space >
	    hammer_limit_dirtybufspace) {
		return(1);
	}
	return(0);
}

/*
 * Return non-zero if too many dirty meta-data buffers have built up.
 *
 * This version is used by background operations (mirror, prune, reblock)
 * to leave room for foreground operations.
 */
int
hammer_flusher_meta_halflimit(hammer_mount_t hmp)
{
	if (hmp->locked_dirty_space + hmp->io_running_space >
	    hammer_limit_dirtybufspace / 2) {
		return(1);
	}
	return(0);
}

/*
 * Return non-zero if the flusher still has something to flush.
 */
int
hammer_flusher_haswork(hammer_mount_t hmp)
{
	if (hmp->ronly)
		return(0);
	if (hmp->flags & HAMMER_MOUNT_CRITICAL_ERROR)
		return(0);
	if (TAILQ_FIRST(&hmp->flush_group_list) ||	/* dirty inodes */
	    RB_ROOT(&hmp->volu_root) ||			/* dirty buffers */
	    RB_ROOT(&hmp->undo_root) ||
	    RB_ROOT(&hmp->data_root) ||
	    RB_ROOT(&hmp->meta_root) ||
	    (hmp->hflags & HMNT_UNDO_DIRTY)) {		/* UNDO FIFO sync */
		return(1);
	}
	return(0);
}

int
hammer_flush_dirty(hammer_mount_t hmp, int max_count)
{
	int count = 0;
	int dummy;

	while (hammer_flusher_haswork(hmp)) {
		hammer_flusher_sync(hmp);
		++count;
		if (count >= 5) {
			if (count == 5)
				hkprintf("flushing.");
			else
				kprintf(".");
			tsleep(&dummy, 0, "hmrufl", hz);
		}
		if (max_count != -1 && count == max_count) {
			kprintf("giving up");
			break;
		}
	}
	if (count >= 5)
		kprintf("\n");

	if (count >= max_count)
		return(-1);
	return(0);
}