Commit 817c28201e2a9564b38a624be491245aa025c77f

Vicent Marti 2011-02-21T17:05:16

Rewrite all file IO for more performance The new `git_filebuf` structure provides atomic high-performance writes to disk by using a write cache, and optionally a double-buffered scheme through a worker thread (not enabled yet). Writes can be done 3-layered, like in git.git (user code -> write cache -> disk), or 2-layered, by writing directly on the cache. This makes index writing considerably faster. The `git_filebuf` structure contains all the old functionality of `git_filelock` for atomic file writes and reads. The `git_filelock` structure has been removed. Additionally, the `git_filebuf` API allows to automatically hash (SHA1) all the data as it is written to disk (hashing is done smartly on big chunks to improve performance). Signed-off-by: Vicent Marti <tanoku@gmail.com>

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
diff --git a/src/filebuf.c b/src/filebuf.c
new file mode 100644
index 0000000..58ff0b6
--- /dev/null
+++ b/src/filebuf.c
@@ -0,0 +1,261 @@
+/*
+ * This file is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2,
+ * as published by the Free Software Foundation.
+ *
+ * In addition to the permissions in the GNU General Public License,
+ * the authors give you unlimited permission to link the compiled
+ * version of this file into combinations with other programs,
+ * and to distribute those combinations without any restriction
+ * coming from the use of this file.  (The General Public License
+ * restrictions do apply in other respects; for example, they cover
+ * modification of the file, and distribution when not linked into
+ * a combined executable.)
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING.  If not, write to
+ * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include "common.h"
+#include "filebuf.h"
+#include "fileops.h"
+
+static const char *GIT_FILELOCK_EXTENSION = ".lock\0";
+static const size_t GIT_FILELOCK_EXTLENGTH = 6;
+
+static const size_t WRITE_BUFFER_SIZE = (4096 * 2);
+
+static int lock_file(git_filebuf *file, int flags)
+{
+	if (gitfo_exists(file->path_lock) == 0) {
+		if (flags & GIT_FILEBUF_FORCE)
+			gitfo_unlink(file->path_lock);
+		else
+			return GIT_EOSERR;
+	}
+
+	file->fd = gitfo_creat(file->path_lock, 0644);
+
+	if (file->fd < 0)
+		return GIT_EOSERR;
+
+	/* TODO: do a flock() in the descriptor file_lock */
+
+	if ((flags & GIT_FILEBUF_APPEND) && gitfo_exists(file->path_original) == 0) {
+		git_file source;
+		char buffer[2048];
+		size_t read_bytes;
+
+		source = gitfo_open(file->path_original, O_RDONLY);
+		if (source < 0)
+			return GIT_EOSERR;
+
+		while ((read_bytes = gitfo_read(source, buffer, 2048)) > 0) {
+			gitfo_write(file->fd, buffer, read_bytes);
+			if (file->digest)
+				git_hash_update(file->digest, buffer, read_bytes);
+		}
+
+		gitfo_close(source);
+	}
+
+	return GIT_SUCCESS;
+}
+
+void git_filebuf_cleanup(git_filebuf *file)
+{
+	if (file->fd >= 0) {
+		gitfo_close(file->fd);
+		gitfo_unlink(file->path_lock);
+	}
+
+	if (file->digest)
+		git_hash_free_ctx(file->digest);
+
+	free(file->buffer);
+
+#ifdef GIT_FILEBUF_THREADS
+	free(file->buffer_back);
+#endif
+
+	free(file->path_original);
+	free(file->path_lock);
+}
+
+static int flush_buffer(git_filebuf *file)
+{
+	int result = GIT_SUCCESS;
+
+	if (file->buf_pos > 0) {
+		result = gitfo_write(file->fd, file->buffer, file->buf_pos);
+		if (file->digest)
+			git_hash_update(file->digest, file->buffer, file->buf_pos);
+
+		file->buf_pos = 0;
+	}
+
+	return result;
+}
+
+int git_filebuf_open(git_filebuf *file, const char *path, int flags)
+{
+	int error;
+	size_t path_len;
+
+	if (file == NULL || path == NULL)
+		return GIT_ERROR;
+
+	memset(file, 0x0, sizeof(git_filebuf));
+
+	file->buf_size = WRITE_BUFFER_SIZE;
+	file->buf_pos = 0;
+	file->fd = -1;
+
+	path_len = strlen(path);
+
+	file->path_original = git__strdup(path);
+	if (file->path_original == NULL) {
+		error = GIT_ENOMEM;
+		goto cleanup;
+	}
+
+	file->path_lock = git__malloc(path_len + GIT_FILELOCK_EXTLENGTH);
+	if (file->path_lock == NULL) {
+		error = GIT_ENOMEM;
+		goto cleanup;
+	}
+
+	memcpy(file->path_lock, file->path_original, path_len);
+	memcpy(file->path_lock + path_len, GIT_FILELOCK_EXTENSION, GIT_FILELOCK_EXTLENGTH);
+
+	file->buffer = git__malloc(file->buf_size);
+	if (file->buffer == NULL){
+		error = GIT_ENOMEM;
+		goto cleanup;
+	}
+
+#ifdef GIT_FILEBUF_THREADS
+	file->buffer_back = git__malloc(file->buf_size);
+	if (file->buffer_back == NULL){
+		error = GIT_ENOMEM;
+		goto cleanup;
+	}
+#endif
+
+	if (flags & GIT_FILEBUF_HASH_CONTENTS) {
+		if ((file->digest = git_hash_new_ctx()) == NULL) {
+			error = GIT_ENOMEM;
+			goto cleanup;
+		}
+	}
+
+	if ((error = lock_file(file, flags)) < GIT_SUCCESS)
+		goto cleanup;
+
+	return GIT_SUCCESS;
+
+cleanup:
+	git_filebuf_cleanup(file);
+	return error;
+}
+
+int git_filebuf_hash(git_oid *oid, git_filebuf *file)
+{
+	int error;
+
+	if (file->digest == NULL)
+		return GIT_ERROR;
+
+	if ((error = flush_buffer(file)) < GIT_SUCCESS)
+		return error;
+
+	git_hash_final(oid, file->digest);
+	git_hash_free_ctx(file->digest);
+	file->digest = NULL;
+
+	return GIT_SUCCESS;
+}
+
+int git_filebuf_commit(git_filebuf *file)
+{
+	int error;
+
+	if ((error = flush_buffer(file)) < GIT_SUCCESS)
+		goto cleanup;
+
+	gitfo_close(file->fd);
+	error = gitfo_move_file(file->path_lock, file->path_original);
+
+cleanup:
+	git_filebuf_cleanup(file);
+	return error;
+}
+
+GIT_INLINE(void) add_to_cache(git_filebuf *file, void *buf, size_t len)
+{
+	memcpy(file->buffer + file->buf_pos, buf, len);
+	file->buf_pos += len;
+}
+
+int git_filebuf_write(git_filebuf *file, void *buff, size_t len)
+{
+	int error;
+	unsigned char *buf = buff;
+
+	for (;;) {
+		size_t space_left = file->buf_size - file->buf_pos;
+
+		/* cache if it's small */
+		if (space_left > len) {
+			add_to_cache(file, buf, len);
+			return GIT_SUCCESS;
+		}
+
+		/* flush the cache if it doesn't fit */
+		if (file->buf_pos > 0) {
+			add_to_cache(file, buf, space_left);
+
+			if ((error = flush_buffer(file)) < GIT_SUCCESS)
+				return error;
+
+			len -= space_left;
+			buf += space_left;
+		}
+
+		/* write too-large chunks immediately */
+		if (len > file->buf_size) {
+			error = gitfo_write(file->fd, buf, len);
+			if (file->digest)
+				git_hash_update(file->digest, buf, len);
+		}
+	}
+}
+
+int git_filebuf_reserve(git_filebuf *file, void **buffer, size_t len)
+{
+	int error;
+	size_t space_left = file->buf_size - file->buf_pos;
+
+	*buffer = NULL;
+
+	if (len > file->buf_size)
+		return GIT_ENOMEM;
+
+	if (space_left <= len) {
+		if ((error = flush_buffer(file)) < GIT_SUCCESS)
+			return error;
+	}
+
+	*buffer = (file->buffer + file->buf_pos);
+	file->buf_pos += len;
+
+	return GIT_SUCCESS;
+}
+
diff --git a/src/filebuf.h b/src/filebuf.h
new file mode 100644
index 0000000..f2e4dba
--- /dev/null
+++ b/src/filebuf.h
@@ -0,0 +1,40 @@
+#ifndef INCLUDE_filebuf_h__
+#define INCLUDE_filebuf_h__
+
+#include "fileops.h"
+#include "hash.h"
+
+#ifdef GIT_THREADS
+#	define GIT_FILEBUF_THREADS
+#endif
+
+#define GIT_FILEBUF_HASH_CONTENTS 0x1
+#define GIT_FILEBUF_APPEND 0x2
+#define GIT_FILEBUF_FORCE 0x4
+
+struct git_filebuf {
+	char *path_original;
+	char *path_lock;
+
+	git_hash_ctx *digest;
+
+	unsigned char *buffer;
+#ifdef GIT_FILEBUF_THREADS
+	unsigned char *buffer_back;
+#endif
+
+	size_t buf_size, buf_pos;
+	git_file fd;
+};
+
+typedef struct git_filebuf git_filebuf;
+
+int git_filebuf_write(git_filebuf *lock, void *buff, size_t len);
+int git_filebuf_reserve(git_filebuf *file, void **buff, size_t len);
+
+int git_filebuf_open(git_filebuf *lock, const char *path, int flags);
+int git_filebuf_commit(git_filebuf *lock);
+void git_filebuf_cleanup(git_filebuf *lock);
+int git_filebuf_hash(git_oid *oid, git_filebuf *file);
+
+#endif
diff --git a/src/filelock.c b/src/filelock.c
deleted file mode 100644
index 9db1cd7..0000000
--- a/src/filelock.c
+++ /dev/null
@@ -1,132 +0,0 @@
-/*
- * This file is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License, version 2,
- * as published by the Free Software Foundation.
- *
- * In addition to the permissions in the GNU General Public License,
- * the authors give you unlimited permission to link the compiled
- * version of this file into combinations with other programs,
- * and to distribute those combinations without any restriction
- * coming from the use of this file.  (The General Public License
- * restrictions do apply in other respects; for example, they cover
- * modification of the file, and distribution when not linked into
- * a combined executable.)
- *
- * This file is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; see the file COPYING.  If not, write to
- * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301, USA.
- */
-
-#include "common.h"
-#include "filelock.h"
-#include "fileops.h"
-
-static const char *GIT_FILELOCK_EXTENSION = ".lock\0";
-static const size_t GIT_FILELOCK_EXTLENGTH = 6;
-
-#define BUILD_PATH_LOCK(_lock, _path) { \
-	memcpy(_path, _lock->path, _lock->path_length); \
-	memcpy(_path + _lock->path_length, GIT_FILELOCK_EXTENSION,\
-			GIT_FILELOCK_EXTLENGTH);\
-}
-
-int git_filelock_init(git_filelock *lock, const char *path)
-{
-	if (lock == NULL || path == NULL)
-		return GIT_ERROR;
-
-	memset(lock, 0x0, sizeof(git_filelock));
-
-	lock->path_length = strlen(path);
-
-	if (lock->path_length + GIT_FILELOCK_EXTLENGTH >= GIT_PATH_MAX)
-		return GIT_ERROR;
-
-	memcpy(lock->path, path, lock->path_length);
-	return GIT_SUCCESS;
-}
-
-int git_filelock_lock(git_filelock *lock, int append)
-{
-	char path_lock[GIT_PATH_MAX];
-	BUILD_PATH_LOCK(lock, path_lock);
-
-	/* If file already exists, we cannot create a lock */
-	if (gitfo_exists(path_lock) == 0)
-		return GIT_EOSERR;
-
-	lock->file_lock = gitfo_creat(path_lock, 0666);
-
-	if (lock->file_lock < 0)
-		return GIT_EOSERR;
-
-	lock->is_locked = 1;
-
-	/* TODO: do a flock() in the descriptor file_lock */
-
-	if (append && gitfo_exists(lock->path) == 0) {
-		git_file source;
-		char buffer[2048];
-		size_t read_bytes;
-
-		source = gitfo_open(lock->path, O_RDONLY);
-		if (source < 0)
-			return GIT_EOSERR;
-
-		while ((read_bytes = gitfo_read(source, buffer, 2048)) > 0)
-			gitfo_write(lock->file_lock, buffer, read_bytes);
-
-		gitfo_close(source);
-	}
-
-	return GIT_SUCCESS;
-}
-
-void git_filelock_unlock(git_filelock *lock)
-{
-	char path_lock[GIT_PATH_MAX];
-	BUILD_PATH_LOCK(lock, path_lock);
-
-	if (lock->is_locked) {
-		/* The flock() in lock->file_lock is removed
-		 * automatically when closing the descriptor */
-		gitfo_close(lock->file_lock);
-		gitfo_unlink(path_lock);
-		lock->is_locked = 0;
-	}
-}
-
-int git_filelock_commit(git_filelock *lock)
-{
-	int error;
-	char path_lock[GIT_PATH_MAX];
-	BUILD_PATH_LOCK(lock, path_lock);
-
-	if (!lock->is_locked || lock->file_lock < 0)
-		return GIT_ERROR;
-
-	/* FIXME: flush the descriptor? */
-	gitfo_close(lock->file_lock);
-
-	error = gitfo_move_file(path_lock, lock->path);
-
-	if (error < GIT_SUCCESS)
-		gitfo_unlink(path_lock);
-
-	lock->is_locked = 0;
-	return error;
-}
-
-int git_filelock_write(git_filelock *lock, const void *buffer, size_t length)
-{
-	if (!lock->is_locked || lock->file_lock < 0)
-		return GIT_ERROR;
-
-	return gitfo_write(lock->file_lock, (void *)buffer, length);
-}
diff --git a/src/filelock.h b/src/filelock.h
deleted file mode 100644
index bff8618..0000000
--- a/src/filelock.h
+++ /dev/null
@@ -1,23 +0,0 @@
-#ifndef INCLUDE_filelock_h__
-#define INCLUDE_filelock_h__
-
-#include "fileops.h"
-
-struct git_filelock {
-
-	char path[GIT_PATH_MAX];
-	size_t path_length;
-
-	git_file file_lock;
-	int is_locked;
-};
-
-typedef struct git_filelock git_filelock;
-
-int git_filelock_init(git_filelock *lock, const char *path);
-int git_filelock_lock(git_filelock *lock, int append);
-void git_filelock_unlock(git_filelock *lock);
-int git_filelock_commit(git_filelock *lock);
-int git_filelock_write(git_filelock *lock, const void *buffer, size_t length);
-
-#endif
diff --git a/src/index.c b/src/index.c
index 297836f..e83d115 100644
--- a/src/index.c
+++ b/src/index.c
@@ -102,7 +102,7 @@ static git_index_tree *read_tree_internal(const char **, const char *, git_index
 
 static int parse_index(git_index *index, const char *buffer, size_t buffer_size);
 static void sort_index(git_index *index);
-static int write_index(git_index *index, git_filelock *file);
+static int write_index(git_index *index, git_filebuf *file);
 
 int index_srch(const void *key, const void *array_member)
 {
@@ -255,25 +255,22 @@ int git_index_read(git_index *index)
 
 int git_index_write(git_index *index)
 {
-	git_filelock file;
+	git_filebuf file;
 	struct stat indexst;
 	int error;
 
 	if (!index->sorted)
 		sort_index(index);
 
-	if ((error = git_filelock_init(&file, index->index_file_path)) < GIT_SUCCESS)
-		return error;
-
-	if ((error = git_filelock_lock(&file, 0)) < GIT_SUCCESS)
+	if ((error = git_filebuf_open(&file, index->index_file_path, GIT_FILEBUF_HASH_CONTENTS)) < GIT_SUCCESS)
 		return error;
 
 	if ((error = write_index(index, &file)) < GIT_SUCCESS) {
-		git_filelock_unlock(&file);
+		git_filebuf_cleanup(&file);
 		return error;
 	}
 
-	if ((error = git_filelock_commit(&file)) < GIT_SUCCESS)
+	if ((error = git_filebuf_commit(&file)) < GIT_SUCCESS)
 		return error;
 
 	if (gitfo_stat(index->index_file_path, &indexst) == 0) {
@@ -684,22 +681,23 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
 	return GIT_SUCCESS;
 }
 
-static void *create_disk_entry(size_t *disk_size, git_index_entry *entry)
+static int write_disk_entry(git_filebuf *file, git_index_entry *entry)
 {
 	struct entry_short *ondisk;
-	size_t path_len;
+	size_t path_len, disk_size;
 	char *path;
 
 	path_len = strlen(entry->path);
 
 	if (entry->flags & GIT_IDXENTRY_EXTENDED)
-		*disk_size = long_entry_size(path_len);
+		disk_size = long_entry_size(path_len);
 	else
-		*disk_size = short_entry_size(path_len);
+		disk_size = short_entry_size(path_len);
 
-	ondisk = git__calloc(1, *disk_size);
-	if (ondisk == NULL)
-		return NULL;
+	if (git_filebuf_reserve(file, (void **)&ondisk, disk_size) < GIT_SUCCESS)
+		return GIT_ENOMEM;
+
+	memset(ondisk, 0x0, disk_size);
 
 	ondisk->ctime.seconds = htonl((unsigned long)entry->ctime.seconds);
 	ondisk->mtime.seconds = htonl((unsigned long)entry->mtime.seconds);
@@ -727,265 +725,51 @@ static void *create_disk_entry(size_t *disk_size, git_index_entry *entry)
 
 	memcpy(path, entry->path, path_len);
 
-	return ondisk;
-}
-
-#if defined(GIT_THREADS) && defined(GIT_INDEX_THREADED)
-
-#define THREAD_QUEUE_SIZE 64
-
-typedef struct {
-	void *data;
-	size_t size;
-	git_refcnt refcount;
-} index_thread_entry;
-
-typedef struct {
-	void *extra_data;
-	void (*process_entry)(void *extra_data, index_thread_entry *entry);
-
-	index_thread_entry *buffer[THREAD_QUEUE_SIZE];
-	int count, read_pos, write_pos;
-
-	git_lck mutex;
-	git_cnd entry_available, space_available;
-} index_thread_queue;
-
-void index_thread_enqueue(index_thread_queue *queue, index_thread_entry *entry)
-{
-	gitlck_lock(&queue->mutex);
-	if (queue->count == THREAD_QUEUE_SIZE)
-		gitcnd_wait(&queue->space_available, &queue->mutex);
-
-	queue->buffer[queue->write_pos++ % THREAD_QUEUE_SIZE] = entry;
-	queue->count++;
-
-	gitcnd_signal(&queue->entry_available);
-	gitlck_unlock(&queue->mutex);
-}
-
-void thread_hash_entry(void *digest, index_thread_entry *entry)
-{
-	git_hash_update((git_hash_ctx *)digest, entry->data, entry->size);
-}
-
-void thread_write_entry(void *file, index_thread_entry *entry)
-{
-	git_filelock_write((git_filelock *)file, entry->data, entry->size);
-}
-
-void *index_thread(void *attr)
-{
-	index_thread_queue *queue = (index_thread_queue *)attr;
-
-	for (;;) {
-		index_thread_entry *entry;
-
-		gitlck_lock(&queue->mutex);
-		if (queue->count == 0)
-			gitcnd_wait(&queue->entry_available, &queue->mutex);
-
-		entry = queue->buffer[queue->read_pos++ % THREAD_QUEUE_SIZE];
-		queue->count--;
-
-		gitcnd_signal(&queue->space_available);
-		gitlck_unlock(&queue->mutex);
-
-		if (entry == NULL)
-			break;
-
-		queue->process_entry(queue->extra_data, entry);
-
-		if (gitrc_dec(&entry->refcount)) {
-			gitrc_free(&entry->refcount);
-			free(entry->data);
-			free(entry);
-		}
-	}
-
-	git_thread_exit(NULL);
-}
-
-static int write_entries(git_index *index, git_filelock *file, git_hash_ctx *digest)
-{
-	git_thread write_thread, hash_thread;
-	index_thread_queue *write_queue, *hash_queue;
-	int error = GIT_SUCCESS;
-	unsigned int i;
-
-	write_queue = git__malloc(sizeof(index_thread_queue));
-	hash_queue = git__malloc(sizeof(index_thread_queue));
-
-	if (write_queue == NULL || hash_queue == NULL)
-		return GIT_ENOMEM;
-
-	/*
-	 * Init the writer thread.
-	 * This thread takes care of all the blocking I/O: reads
-	 * the produced index entries and writes them back to disk
-	 * via the filelock API.
-	 */
-	{
-		write_queue->extra_data = (void *)file;
-		write_queue->process_entry = thread_write_entry;
-
-		write_queue->count = 0;
-		write_queue->read_pos = 0;
-		write_queue->write_pos = 0;
-
-		gitlck_init(&write_queue->mutex);
-		gitcnd_init(&write_queue->space_available, NULL);
-		gitcnd_init(&write_queue->entry_available, NULL);
-
-		if (git_thread_create(&write_thread, NULL, index_thread, (void *)write_queue) < 0) {
-			error = GIT_EOSERR;
-			goto thread_error;
-		}
-	}
-
-	/*
-	 * Init the hasher thread.
-	 * This thread takes care of doing an incremental
-	 * SHA1 hash on all the written data; the final value
-	 * of this hash must be appended at the end of the
-	 * written index file.
-	 */
-	{
-		hash_queue->extra_data = (void *)digest;
-		hash_queue->process_entry = thread_hash_entry;
-
-		hash_queue->count = 0;
-		hash_queue->read_pos = 0;
-		hash_queue->write_pos = 0;
-
-		gitlck_init(&hash_queue->mutex);
-		gitcnd_init(&hash_queue->space_available, NULL);
-		gitcnd_init(&hash_queue->entry_available, NULL);
-
-		if (git_thread_create(&hash_thread, NULL, index_thread, (void *)hash_queue) < 0) {
-			error = GIT_EOSERR;
-			goto thread_error;
-		}
-	}
-
-	/*
-	 * Do the processing.
-	 * This is the main thread. Takes care of preparing all
-	 * the entries that will be written to disk
-	 */
-	for (i = 0; i < index->entries.length; ++i) {
-		git_index_entry *entry;
-		index_thread_entry *thread_entry;
-
-		entry = git_vector_get(&index->entries, i);
-
-		thread_entry = git__malloc(sizeof(index_thread_entry));
-		if (thread_entry == NULL) {
-			error = GIT_ENOMEM;
-			goto thread_error;
-		}
-
-		thread_entry->data = create_disk_entry(&thread_entry->size, entry);
-		if (thread_entry->data == NULL) {
-			error = GIT_ENOMEM;
-			goto thread_error;
-		}
-
-		/* queue in both queues */
-		gitrc_init(&thread_entry->refcount, 2);
-
-		index_thread_enqueue(write_queue, thread_entry);
-		index_thread_enqueue(hash_queue, thread_entry);
-	}
-
-	/* kill the two threads by queuing a NULL item */
-	{
-		index_thread_enqueue(write_queue, NULL);
-		index_thread_enqueue(hash_queue, NULL);
-	}
-
-	/* wait for them to terminate */
-	git_thread_join(write_thread, NULL);
-	git_thread_join(hash_thread, NULL);
-
-	free(write_queue);
-	free(hash_queue);
-
 	return GIT_SUCCESS;
-
-thread_error:
-	git_thread_kill(write_thread);
-	git_thread_kill(hash_thread);
-
-	free(write_queue);
-	free(hash_queue);
-
-	return error;
 }
 
-#else
-
-static int write_entries(git_index *index, git_filelock *file, git_hash_ctx *digest)
+static int write_entries(git_index *index, git_filebuf *file)
 {
 	unsigned int i;
 
 	for (i = 0; i < index->entries.length; ++i) {
 		git_index_entry *entry;
-		void *disk_entry;
-		size_t disk_size;
-
 		entry = git_vector_get(&index->entries, i);
-		disk_entry = create_disk_entry(&disk_size, entry);
-
-		if (disk_entry == NULL)
+		if (write_disk_entry(file, entry) < GIT_SUCCESS)
 			return GIT_ENOMEM;
-
-		if (git_filelock_write(file, disk_entry, disk_size) < GIT_SUCCESS)
-			return GIT_EOSERR;
-
-		git_hash_update(digest, disk_entry, disk_size);
-
-		free(disk_entry);
 	}
 
 	return GIT_SUCCESS;
 }
 
-#endif
-
-static int write_index(git_index *index, git_filelock *file)
+static int write_index(git_index *index, git_filebuf *file)
 {
 	int error = GIT_SUCCESS;
-
-	git_hash_ctx *digest;
 	git_oid hash_final;
 
 	struct index_header header;
 
 	int is_extended = 1;
 
-	assert(index && file && file->is_locked);
-
-	if ((digest = git_hash_new_ctx()) == NULL)
-		return GIT_ENOMEM;
+	assert(index && file);
 
 	header.signature = htonl(INDEX_HEADER_SIG);
 	header.version = htonl(is_extended ? INDEX_VERSION_NUMBER : INDEX_VERSION_NUMBER_EXT);
 	header.entry_count = htonl(index->entries.length);
 
-	git_filelock_write(file, &header, sizeof(struct index_header));
-	git_hash_update(digest, &header, sizeof(struct index_header));
+	git_filebuf_write(file, &header, sizeof(struct index_header));
 
-	error = write_entries(index, file, digest);
+	error = write_entries(index, file);
 	if (error < GIT_SUCCESS)
-		goto cleanup;
+		return error;
 
 	/* TODO: write extensions (tree cache) */
 
-	git_hash_final(&hash_final, digest);
-	git_filelock_write(file, hash_final.id, GIT_OID_RAWSZ);
+	/* get out the hash for all the contents we've appended to the file */
+	git_filebuf_hash(&hash_final, file);
+
+	/* write it at the end of the file */
+	git_filebuf_write(file, hash_final.id, GIT_OID_RAWSZ);
 
-cleanup:
-	git_hash_free_ctx(digest);
 	return error;
 }
diff --git a/src/index.h b/src/index.h
index 747ced0..43aadff 100644
--- a/src/index.h
+++ b/src/index.h
@@ -2,7 +2,7 @@
 #define INCLUDE_index_h__
 
 #include "fileops.h"
-#include "filelock.h"
+#include "filebuf.h"
 #include "vector.h"
 #include "git2/odb.h"
 #include "git2/index.h"
diff --git a/src/refs.c b/src/refs.c
index a305565..1f434ea 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -511,7 +511,7 @@ int git_reference_resolve(git_reference **resolved_ref, git_reference *ref)
 
 int git_reference_write(git_reference *ref)
 {
-	git_filelock lock;
+	git_filebuf file;
 	char ref_path[GIT_PATH_MAX];
 	int error, contents_size;
 	char *ref_contents = NULL;
@@ -528,10 +528,7 @@ int git_reference_write(git_reference *ref)
 
 	git__joinpath(ref_path, ref->owner->path_repository, ref->name);
 
-	if ((error = git_filelock_init(&lock, ref_path)) < GIT_SUCCESS)
-		goto error_cleanup;
-
-	if ((error = git_filelock_lock(&lock, 0)) < GIT_SUCCESS)
+	if ((error = git_filebuf_open(&file, ref_path, 0)) < GIT_SUCCESS)
 		goto error_cleanup;
 
 	if (ref->type == GIT_REF_OID) {
@@ -560,20 +557,21 @@ int git_reference_write(git_reference *ref)
 		ref_contents[contents_size - 1] = '\n';
 	}
 
-	if ((error = git_filelock_write(&lock, ref_contents, contents_size)) < GIT_SUCCESS)
+	if ((error = git_filebuf_write(&file, ref_contents, contents_size)) < GIT_SUCCESS)
 		goto error_cleanup;
 
-	if ((error = git_filelock_commit(&lock)) < GIT_SUCCESS)
-		goto error_cleanup;
+	free(ref_contents);
 
-	ref->modified = 0;
+	error = git_filebuf_commit(&file);
 
-	free(ref_contents);
-	return GIT_SUCCESS;
+	if (error == GIT_SUCCESS)
+		ref->modified = 0;
+
+	return error;
 
 error_cleanup:
 	free(ref_contents);
-	git_filelock_unlock(&lock);
+	git_filebuf_cleanup(&file);
 	return error;
 }