aboutsummaryrefslogtreecommitdiffstats
path: root/dbfs-backend.c
blob: cdbd38ed2cf9a783d296eaf7f68ac51ab45254ad (plain)
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
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
/*
 * Maintained by Jeff Garzik <jgarzik@pobox.com>
 *
 * Copyright 2006-2007 Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * This program 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, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include <syslog.h>
#include <glib.h>
#include <db.h>
#include <openssl/sha.h>
#include "dbfs.h"

struct dbfs_lookup_info {
	const char	*name;
	size_t		namelen;
	guint64		*ino;
};

struct dbfs_dirscan_info {
	const char		*name;
	size_t			namelen;
	void			*ent;
};

static int dbfs_data_unref(DB_TXN *txn, const dbfs_blk_id_t *id);

int dbmeta_del(DB_TXN *txn, const char *key_str)
{
	DBT key;
	int rc;

	key.data = (void *) key_str;
	key.size = strlen(key_str);

	/* delete key 'key_str' from metadata database */
	rc = gfs->meta->del(gfs->meta, txn, &key, 0);
	if (rc == DB_NOTFOUND)
		return -ENOENT;
	if (rc)
		return -EIO;
	return 0;
}

static int dbfs_mode_type(guint32 mode, enum dbfs_inode_type *itype)
{
	if (S_ISDIR(mode))
		*itype = IT_DIR;
	else if (S_ISCHR(mode) || S_ISBLK(mode))
		*itype = IT_DEV;
	else if (S_ISFIFO(mode))
		*itype = IT_FIFO;
	else if (S_ISLNK(mode))
		*itype = IT_SYMLINK;
	else if (S_ISSOCK(mode))
		*itype = IT_SOCKET;
	else if (S_ISREG(mode))
		*itype = IT_REG;
	else
		return -EINVAL;

	return 0;
}

static int dbfs_inode_del_data(DB_TXN *txn, const struct dbfs_inode *ino)
{
	int i, rc = 0;

	for (i = 0; i < ino->n_extents; i++) {
		rc = dbfs_data_unref(txn, &ino->raw_inode->blocks[i].id);
		if (rc)
			return rc;
	}

	return 0;
}

int dbfs_inode_del(DB_TXN *txn, const struct dbfs_inode *ino)
{
	guint64 ino_n = GUINT64_FROM_LE(ino->raw_inode->ino);
	char key[32];
	int rc = 0;

	switch (ino->type) {
	case IT_REG:
		rc = dbfs_inode_del_data(txn, ino);
		break;

	case IT_DIR:
		sprintf(key, "/dir/%Lu", (unsigned long long) ino_n);
		rc = dbmeta_del(txn, key);
		break;

	case IT_SYMLINK:
		sprintf(key, "/symlink/%Lu", (unsigned long long) ino_n);
		rc = dbmeta_del(txn, key);
		break;

	case IT_DEV:
	case IT_FIFO:
	case IT_SOCKET:
		/* nothing additional to delete */
		break;
	}

	if (rc)
		goto out;

	sprintf(key, "/inode/%Lu", (unsigned long long) ino_n);

	rc = dbmeta_del(txn, key);

out:
	return rc;
}

int dbfs_inode_read(DB_TXN *txn, guint64 ino_n, struct dbfs_inode **ino_out)
{
	int rc;
	DBT key, val;
	char key_str[32];
	struct dbfs_inode *ino;
	size_t ex_sz;
	guint32 mode;

	sprintf(key_str, "/inode/%Lu", (unsigned long long) ino_n);

	memset(&key, 0, sizeof(key));
	key.data = key_str;
	key.size = strlen(key_str);

	memset(&val, 0, sizeof(val));
	val.flags = DB_DBT_MALLOC;

	rc = gfs->meta->get(gfs->meta, txn, &key, &val, 0);
	if (rc == DB_NOTFOUND)
		return -ENOENT;
	if (rc)
		return rc;

	/* calculate size of struct dbfs_extent area */
	ex_sz = val.size - sizeof(struct dbfs_raw_inode);

	/* initialize runtime information about the inode */
	ino = g_new(struct dbfs_inode, 1);
	ino->n_extents = ex_sz / sizeof(struct dbfs_extent);
	ino->raw_ino_size = val.size;
	ino->raw_inode = val.data;

	/* deduce inode type */
	mode = GUINT32_FROM_LE(ino->raw_inode->mode);
	rc = dbfs_mode_type(mode, &ino->type);
	g_assert(rc == 0);

	*ino_out = ino;

	return 0;
}

static int dbfs_inode_next(DB_TXN *txn, struct dbfs_inode **ino_out)
{
	struct dbfs_inode *ino;
	int rc;
	guint64 curtime, start = gfs->next_inode;

	*ino_out = NULL;

	/* loop through inode numbers starting at gfs->next_inode,
	 * and stop on first error.  Error ENOENT means the
	 * inode number was not found, and can therefore be used
	 * as the next free inode number.
	 */
	while (1) {
		rc = dbfs_inode_read(txn, gfs->next_inode, &ino);
		if (rc)
			break;

		dbfs_inode_free(ino);
		gfs->next_inode++;
		if (gfs->next_inode < 2)
			gfs->next_inode = 2;
		if (gfs->next_inode == start) {	/* loop */
			rc = -EBUSY;
			break;
		}
	}

	if (rc != -ENOENT)
		return rc;

	/* allocate an empty inode */
	ino = g_new0(struct dbfs_inode, 1);
	ino->raw_ino_size = sizeof(struct dbfs_raw_inode);
	ino->raw_inode = malloc(ino->raw_ino_size);
	memset(ino->raw_inode, 0, ino->raw_ino_size);

	ino->raw_inode->ino = GUINT64_TO_LE(gfs->next_inode++);
	curtime = GUINT64_TO_LE(time(NULL));
	ino->raw_inode->ctime = curtime;
	ino->raw_inode->atime = curtime;
	ino->raw_inode->mtime = curtime;

	*ino_out = ino;

	return 0;
}

int dbfs_dir_read(DB_TXN *txn, guint64 ino, DBT *val)
{
	DBT key;
	char key_str[32];
	int rc;

	sprintf(key_str, "/dir/%Lu", (unsigned long long) ino);

	memset(&key, 0, sizeof(key));
	key.data = key_str;
	key.size = strlen(key_str);

	memset(val, 0, sizeof(*val));
	val->flags = DB_DBT_MALLOC;

	rc = gfs->meta->get(gfs->meta, txn, &key, val, 0);
	if (rc == DB_NOTFOUND)
		return -ENOTDIR;
	return rc ? -EIO : 0;
}

int dbfs_dir_foreach(void *dir, dbfs_dir_actor_t func, void *userdata)
{
	struct dbfs_dirent *de;
	void *p;
	int rc = 0;
	unsigned int namelen;

	p = dir;
	while (1) {
		de = p;

		g_assert (de->magic == DBFS_DE_MAGIC);
		if (!de->namelen)
			break;

		/* send dirent to callback function */
		rc = func(de, userdata);
		if (rc)
			break;

		/* align things so that compiler structures
		 * do not wind up misaligned.
		 */
		namelen = GUINT16_FROM_LE(de->namelen);
		p += dbfs_dirent_next(namelen);
	}

	return rc;
}

static int dbfs_dir_scan1(struct dbfs_dirent *de, void *userdata)
{
	struct dbfs_dirscan_info *di = userdata;

	if ((GUINT16_FROM_LE(de->namelen) == di->namelen) &&
	    (!memcmp(de->name, di->name, di->namelen))) {
		di->ent = de;
		return 1;
	}

	return 0;
}

int dbfs_dir_lookup(DB_TXN *txn, guint64 parent, const char *name, guint64 *ino)
{
	struct dbfs_dirscan_info di;
	struct dbfs_dirent *de;
	DBT val;
	int rc;

	*ino = 0;

	/* read directory from database */
	rc = dbfs_dir_read(txn, parent, &val);
	if (rc)
		return rc;

	memset(&di, 0, sizeof(di));
	di.name = name;
	di.namelen = strlen(name);

	/* query pointer to start of matching dirent */
	rc = dbfs_dir_foreach(val.data, dbfs_dir_scan1, &di);
	if (rc != 1) {
		if (rc == 0)
			rc = -ENOENT;
		goto out;
	}
	rc = 0;

	/* if match found, return inode number */
	de = di.ent;
	*ino = de->ino;

out:
	free(val.data);
	return rc;
}

static int dbfs_dirent_del(DB_TXN *txn, guint64 parent, const char *name)
{
	struct dbfs_dirscan_info ui;
	DBT dir_val;
	int rc, del_len, tail_len;
	void *end_ent;
	struct dbfs_dirent *de;

	rc = dbfs_dir_read(txn, parent, &dir_val);
	if (rc)
		return rc;

	memset(&ui, 0, sizeof(ui));
	ui.name = name;
	ui.namelen = strlen(name);

	/* query pointer to start of matching dirent */
	rc = dbfs_dir_foreach(dir_val.data, dbfs_dir_scan1, &ui);
	if (rc != 1) {
		free(dir_val.data);
		if (rc == 0)
			rc = -ENOENT;
		return rc;
	}

	de = ui.ent;
	end_ent = ui.ent + dbfs_dirent_next(GUINT16_FROM_LE(de->namelen));

	del_len = end_ent - ui.ent;
	tail_len = (dir_val.data + dir_val.size) - end_ent;

	memmove(ui.ent, end_ent, tail_len);
	dir_val.size -= del_len;

	rc = dbfs_dir_write(txn, parent, &dir_val);

	free(dir_val.data);

	return rc;
}

static int dbfs_dir_find_last(struct dbfs_dirent *de, void *userdata)
{
	struct dbfs_dirscan_info *di = userdata;

	di->ent = de;

	return 0;
}

static int dbfs_name_validate(const char *name)
{
	if (strchr(name, '/'))
		return -EINVAL;
	if (!g_utf8_validate(name, -1, NULL))
		return -EINVAL;
	if (!strcmp(name, "."))
		return -EINVAL;
	if (!strcmp(name, ".."))
		return -EINVAL;
	if (strlen(name) > DBFS_FILENAME_MAX)
		return -EINVAL;
	return 0;
}

static int dbfs_dir_append(DB_TXN *txn, guint64 parent, guint64 ino_n,
			   const char *name)
{
	struct dbfs_dirscan_info di;
	struct dbfs_dirent *de;
	DBT val;
	int rc;
	unsigned int dir_size, namelen;
	void *p;

	rc = dbfs_name_validate(name);
	if (rc)
		return rc;

	/* read parent directory from database */
	rc = dbfs_dir_read(txn, parent, &val);
	if (rc)
		return rc;

	memset(&di, 0, sizeof(di));
	di.name = name;
	di.namelen = strlen(name);

	/* scan for name in directory, abort if found */
	rc = dbfs_dir_foreach(val.data, dbfs_dir_scan1, &di);
	if (rc != 0) {
		if (rc > 0)
			rc = -EEXIST;
		goto out;
	}

	/* get pointer to last entry */
	rc = dbfs_dir_foreach(val.data, dbfs_dir_find_last, &di);
	if (rc)
		goto out;

	/* adjust pointer 'p' to point to terminator entry */
	de = p = di.ent;
	namelen = GUINT16_FROM_LE(de->namelen);
	p += dbfs_dirent_next(namelen);

	/* increase directory data area size */
	dir_size = p - val.data;
	val.size = dir_size + (3 * sizeof(struct dbfs_dirent)) + di.namelen;
	val.data = realloc(val.data, val.size);
	p = val.data + dir_size;

	/* append directory entry */
	de = p;
	de->magic = GUINT32_TO_LE(DBFS_DE_MAGIC);
	de->namelen = GUINT16_TO_LE(di.namelen);
	de->ino = GUINT64_TO_LE(ino_n);
	memcpy(de->name, name, di.namelen);

	namelen = di.namelen;
	p += dbfs_dirent_next(namelen);

	/* append terminator entry */
	de = p;
	memset(de, 0, sizeof(*de));
	de->magic = GUINT32_TO_LE(DBFS_DE_MAGIC);

	namelen = 0;
	p += dbfs_dirent_next(namelen);

	val.size = p - val.data;

	rc = dbfs_dir_write(txn, parent, &val);

out:
	free(val.data);
	return rc;
}

int dbfs_symlink_read(DB_TXN *txn, guint64 ino, DBT *val)
{
	DBT key;
	char key_str[32];
	int rc;

	sprintf(key_str, "/symlink/%Lu", (unsigned long long) ino);

	memset(&key, 0, sizeof(key));
	key.data = key_str;
	key.size = strlen(key_str);

	memset(val, 0, sizeof(*val));
	val->flags = DB_DBT_MALLOC;

	rc = gfs->meta->get(gfs->meta, txn, &key, val, 0);
	if (rc == DB_NOTFOUND)
		return -EINVAL;
	return rc ? -EIO : 0;
}

int dbfs_symlink_write(DB_TXN *txn, guint64 ino, const char *link)
{
	DBT key, val;
	char key_str[32];

	sprintf(key_str, "/symlink/%Lu", (unsigned long long) ino);

	memset(&key, 0, sizeof(key));
	key.data = key_str;
	key.size = strlen(key_str);

	memset(&val, 0, sizeof(val));
	val.data = (void *) link;
	val.size = strlen(link);

	return gfs->meta->put(gfs->meta, txn, &key, &val, 0) ? -EIO : 0;
}

int dbfs_link(DB_TXN *txn, struct dbfs_inode *ino, guint64 ino_n,
	      guint64 parent, const char *name)
{
	guint32 nlink;
	int rc;

	/* make sure it doesn't exist yet */
	rc = dbfs_dir_append(txn, parent, ino_n, name);
	if (rc)
		return rc;

	/* increment link count */
	nlink = GUINT32_FROM_LE(ino->raw_inode->nlink);
	nlink++;
	ino->raw_inode->nlink = GUINT32_TO_LE(nlink);

	/* write inode; if fails, DB TXN undoes directory modification */
	rc = dbfs_inode_write(txn, ino);

	return rc;
}

int dbfs_unlink(DB_TXN *txn, guint64 parent, const char *name,
		unsigned long flags)
{
	struct dbfs_inode *ino;
	guint64 ino_n;
	int rc, is_dir;
	guint32 nlink;

	rc = dbfs_dir_lookup(txn, parent, name, &ino_n);
	if (rc)
		goto out;

	if (ino_n == DBFS_ROOT_INO) {
		rc = -EINVAL;
		goto out;
	}

	rc = dbfs_inode_read(txn, ino_n, &ino);
	if (rc)
		goto out;

	is_dir = S_ISDIR(GUINT32_FROM_LE(ino->raw_inode->mode));
	if (is_dir && (!(flags & DBFS_UNLINK_DIR))) {
		rc = -EISDIR;
		goto out_ino;
	}

	rc = dbfs_dirent_del(txn, parent, name);
	if (rc)
		goto out_ino;

	nlink = GUINT32_FROM_LE(ino->raw_inode->nlink);
	if (is_dir && (nlink <= 2))
		nlink = 0;
	else
		nlink--;
	ino->raw_inode->nlink = GUINT32_TO_LE(nlink);

	if (!nlink)
		rc = dbfs_inode_del(txn, ino);
	else
		rc = dbfs_inode_write(txn, ino);

out_ino:
	dbfs_inode_free(ino);
out:
	return rc;
}

int dbfs_mknod(DB_TXN *txn, guint64 parent, const char *name, guint32 mode,
	       guint64 rdev, struct dbfs_inode **ino_out)
{
	struct dbfs_inode *ino;
	int rc, is_dir;
	unsigned int nlink = 1;
	guint64 ino_n;

	*ino_out = NULL;

	rc = dbfs_inode_next(txn, &ino);
	if (rc)
		return rc;

	is_dir = S_ISDIR(mode);
	if (is_dir)
		nlink++;
	ino_n = GUINT64_FROM_LE(ino->raw_inode->ino);
	ino->raw_inode->mode = GUINT32_TO_LE(mode);
	ino->raw_inode->nlink = GUINT32_TO_LE(nlink);
	ino->raw_inode->rdev = GUINT64_TO_LE(rdev);

	rc = dbfs_inode_write(txn, ino);
	if (rc)
		goto err_out;

	if (is_dir) {
		rc = dbfs_dir_new(txn, parent, ino_n, ino);
		if (rc)
			goto err_out_del;
	}

	rc = dbfs_dir_append(txn, parent, ino_n, name);
	if (rc)
		goto err_out_del;

	*ino_out = ino;
	return 0;

err_out_del:
	/* txn abort will delete inode */
err_out:
	dbfs_inode_free(ino);
	return rc;
}

int dbfs_rename(DB_TXN *txn, guint64 parent, const char *name,
		guint64 new_parent, const char *new_name)
{
	int rc;
	guint64 ino_n;

	if ((parent == new_parent) && (!strcmp(name, new_name)))
		return -EINVAL;

	rc = dbfs_dir_lookup(txn, parent, name, &ino_n);
	if (rc)
		return rc;

	rc = dbfs_dirent_del(txn, parent, name);
	if (rc)
		return rc;

	rc = dbfs_unlink(txn, new_parent, new_name, 0);
	if (rc && (rc != -ENOENT))
		return rc;

	return dbfs_dir_append(txn, new_parent, ino_n, new_name);
}

static void ext_list_free(GList *ext_list)
{
	GList *tmp;

	tmp = ext_list;
	while (tmp) {
		g_free(tmp->data);
		tmp = tmp->next;
	}
	g_list_free(ext_list);
}

static int dbfs_ext_match(const struct dbfs_inode *ino, guint64 off,
			  guint32 rd_size, GList **ext_list_out)
{
	struct dbfs_extent *ext;
	guint64 pos;
	guint32 ext_len, ext_off, ext_list_want, bytes = 0;
	GList *ext_list = *ext_list_out;
	gboolean in_frag;
	unsigned int i;
	int rc;

	pos = 0;
	in_frag = FALSE;
	ext_list_want = rd_size;
	for (i = 0; i < ino->n_extents; i++) {
		ext_len = GUINT32_FROM_LE(ino->raw_inode->blocks[i].len);
		ext_off = GUINT32_FROM_LE(ino->raw_inode->blocks[i].off);

		if ((!in_frag) && ((pos + ext_len) > off)) {
			guint32 skip;

			ext = g_new(struct dbfs_extent, 1);
			if (!ext) {
				rc = -ENOMEM;
				goto err_out;
			}

			memcpy(&ext->id, &ino->raw_inode->blocks[i].id,
			       sizeof(dbfs_blk_id_t));
			skip = off - pos;
			ext->off = skip + ext_off;
			ext->len = MIN(ext_len - skip, ext_list_want);

			ext_list = g_list_append(ext_list, ext);
			ext_list_want -= ext->len;
			bytes += ext->len;
			in_frag = TRUE;
		}

		else if (in_frag) {
			ext = g_new(struct dbfs_extent, 1);
			if (!ext) {
				rc = -ENOMEM;
				goto err_out;
			}

			memcpy(&ext->id, &ino->raw_inode->blocks[i].id,
			       sizeof(dbfs_blk_id_t));
			ext->off = ext_off;
			ext->len = MIN(ext_len, ext_list_want);

			ext_list = g_list_append(ext_list, ext);
			ext_list_want -= ext->len;
			bytes += ext->len;

			if (ext_list_want == 0)
				break;
		}

		pos += ext_len;
	}

	*ext_list_out = ext_list;
	return (int) bytes;

err_out:
	ext_list_free(ext_list);
	*ext_list_out = NULL;
	return rc;
}

static const char zero_block[DBFS_ZERO_CMP_BLK_SZ];

static gboolean is_zero_buf(const void *buf, size_t buflen)
{
	while (buflen > 0) {
		size_t cmp_size;
		int rc;

		cmp_size = MIN(buflen, DBFS_ZERO_CMP_BLK_SZ);
		rc = memcmp(buf, zero_block, cmp_size);
		if (rc)
			return FALSE;

		buflen -= cmp_size;
	}

	return TRUE;
}

static gboolean is_null_id(const dbfs_blk_id_t *id)
{
	return is_zero_buf(id, DBFS_BLK_ID_LEN);
}

static int dbfs_ext_read(DB_TXN *txn, const dbfs_blk_id_t *id, void *buf,
			 unsigned int offset, size_t *buflen)
{
	DBT key, val;
	int rc;

	memset(&key, 0, sizeof(key));
	key.data = (void *) id;
	key.size = DBFS_BLK_ID_LEN;

	memset(&val, 0, sizeof(val));
	val.data = buf;
	val.ulen = *buflen;
	val.dlen = *buflen;
	val.doff = offset;
	val.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;

	rc = gfs->data->get(gfs->data, txn, &key, &val, 0);
	if (rc == DB_NOTFOUND)
		return -ENOENT;
	if (rc)
		return rc;

	*buflen = val.size;
	return 0;
}

int dbfs_read(DB_TXN *txn, guint64 ino_n, guint64 off, size_t read_req_size,
	      void **buf_out)
{
	struct dbfs_inode *ino;
	GList *tmp, *ext_list = NULL;
	void *buf = NULL;
	size_t buflen = 0;
	unsigned int pos = 0;
	int rc;

	rc = dbfs_inode_read(txn, ino_n, &ino);
	if (rc)
		goto out;

	rc = dbfs_ext_match(ino, off, read_req_size, &ext_list);
	if (rc <= 0)
		goto out_ino;
	buflen = rc;

	buf = malloc(buflen);
	if (!buf) {
		rc = -ENOMEM;
		goto out_list;
	}

	tmp = ext_list;
	while (tmp) {
		struct dbfs_extent *ext;

		ext = tmp->data;
		if (is_null_id(&ext->id)) {
			memset(buf + pos, 0, ext->len);
		} else {
			size_t fraglen = ext->len;

			rc = dbfs_ext_read(txn, &ext->id, buf + pos,
					   ext->off, &fraglen);
			if (rc) {
				free(buf);
				buf = NULL;
				goto out_list;
			}
			if (fraglen != ext->len) {
				free(buf);
				buf = NULL;
				rc = -EINVAL;
				goto out_list;
			}
		}

		pos += ext->len;

		tmp = tmp->next;
	}

out_list:
	ext_list_free(ext_list);
out_ino:
	dbfs_inode_free(ino);
out:
	*buf_out = buf;
	return rc < 0 ? rc : buflen;
}

static int dbfs_write_unique_buf(DB_TXN *txn, const DBT *key, const void *buf,
				 size_t buflen)
{
	struct dbfs_hashref ref;
	DBT val;
	int rc;

	ref.refs = GUINT32_TO_LE(1);

	memset(&val, 0, sizeof(val));
	val.data = &ref;
	val.size = sizeof(ref);

	rc = gfs->hashref->put(gfs->hashref, txn, (DBT *) key, &val, 0);
	if (rc)
		return -EIO;

	memset(&val, 0, sizeof(val));
	val.data = (void *) buf;
	val.size = buflen;

	rc = gfs->data->put(gfs->data, txn, (DBT *) key, &val, DB_NOOVERWRITE);
	if (rc)
		return -EIO;

	return 0;
}

static int dbfs_write_buf(DB_TXN *txn, const void *buf, size_t buflen,
			  struct dbfs_extent *ext)
{
	struct dbfs_hashref *ref;
	DBT key, val;
	int rc;

	if (buflen == 0 || buflen > DBFS_MAX_EXT_LEN)
		return -EINVAL;

	memset(ext, 0, sizeof(*ext));
	ext->len = GUINT32_TO_LE(buflen);

	if (is_zero_buf(buf, buflen))
		return 0;

	SHA1(buf, buflen, (unsigned char *) &ext->id);

	memset(&key, 0, sizeof(key));
	key.data = &ext->id;
	key.size = DBFS_BLK_ID_LEN;

	memset(&val, 0, sizeof(val));
	val.flags = DB_DBT_MALLOC;

	rc = gfs->hashref->get(gfs->hashref, txn, &key, &val, 0);
	if (rc && (rc != DB_NOTFOUND))
		return -EIO;

	if (rc == DB_NOTFOUND)
		return dbfs_write_unique_buf(txn, &key, buf, buflen);

	ref = val.data;
	g_assert(val.size == sizeof(*ref));

	ref->refs = GUINT32_TO_LE(GUINT32_FROM_LE(ref->refs) + 1);

	rc = gfs->hashref->put(gfs->hashref, txn, &key, &val, 0);
	free(val.data);

	return rc ? -EIO : 0;
}

static int dbfs_data_unref(DB_TXN *txn, const dbfs_blk_id_t *id)
{
	struct dbfs_hashref *ref;
	guint32 refs;
	DBT key, val;
	int rc, rc2;

	if (is_null_id(id))
		return 0;

	memset(&key, 0, sizeof(key));
	key.data = (void *) id;
	key.size = DBFS_BLK_ID_LEN;

	memset(&val, 0, sizeof(val));
	val.flags = DB_DBT_MALLOC;

	rc = gfs->hashref->get(gfs->hashref, txn, &key, &val, 0);
	if (rc == DB_NOTFOUND)
		return -ENOENT;
	if (rc)
		return -EIO;

	ref = val.data;
	g_assert(val.size == sizeof(*ref));
	refs = GUINT32_FROM_LE(ref->refs);

	if (refs > 1) {
		refs--;
		ref->refs = GUINT32_TO_LE(refs);

		rc = gfs->hashref->put(gfs->hashref, txn, &key, &val, 0);
		free(val.data);

		return rc ? -EIO : 0;
	}

	free(val.data);

	rc = gfs->hashref->del(gfs->hashref, txn, &key, 0);
	rc2 = gfs->data->del(gfs->data, txn, &key, 0);

	return (rc || rc2) ? -EIO : 0;
}

static int dbfs_inode_realloc(struct dbfs_inode *ino,
			      unsigned int new_n_extents)
{
	struct dbfs_raw_inode *new_raw;
	size_t new_size = sizeof(struct dbfs_raw_inode) +
		(sizeof(struct dbfs_extent) * new_n_extents);

	new_raw = g_malloc0(new_size);
	if (!new_raw)
		return -ENOMEM;

	memcpy(new_raw, ino->raw_inode, ino->raw_ino_size);

	free(ino->raw_inode);
	ino->raw_inode = new_raw;
	ino->raw_ino_size = new_size;
	ino->n_extents = new_n_extents;

	return 0;
}

int dbfs_inode_resize(DB_TXN *txn, struct dbfs_inode *ino, guint64 new_size)
{
	guint64 old_size, diff, diff_ext;
	unsigned int grow, i, new_n_extents, tmp;
	int rc;

	old_size = GUINT64_FROM_LE(ino->raw_inode->size);
	grow = (old_size < new_size);
	diff = grow ? new_size - old_size : old_size - new_size;

	diff_ext = (diff / DBFS_MAX_EXT_LEN);
	if (diff % DBFS_MAX_EXT_LEN)
		diff_ext++;

	if (grow) {
		unsigned int old_n_extents = ino->n_extents;
		new_n_extents = ino->n_extents + diff_ext;

		rc = dbfs_inode_realloc(ino, new_n_extents);
		if (rc)
			return rc;

		for (i = old_n_extents; i < new_n_extents; i++) {
			g_assert(diff > 0);
			tmp = MIN(diff, DBFS_MAX_EXT_LEN);

			memset(&ino->raw_inode->blocks[i], 0,
				sizeof(struct dbfs_extent));
			ino->raw_inode->blocks[i].len =
				GUINT32_TO_LE(tmp);
			diff -= tmp;
		}

		g_assert(diff == 0);
	}

	else {
		struct dbfs_extent *ext;
		guint32 ext_len;

		new_n_extents = ino->n_extents;
		while (new_n_extents > 0) {
			ext = &ino->raw_inode->blocks[new_n_extents - 1];
			ext_len = GUINT32_FROM_LE(ext->len);
			if (ext_len > diff)
				break;

			rc = dbfs_data_unref(txn, &ext->id);
			if (rc)
				return rc;

			memset(ext, 0, sizeof(*ext));
			ino->n_extents--;

			new_n_extents--;
			diff -= ext_len;
		}

		if (diff > 0) {
			ext = &ino->raw_inode->blocks[new_n_extents - 1];
			ext_len = GUINT32_FROM_LE(ext->len);
			ext_len -= diff;
			ext->len = GUINT32_TO_LE(ext_len);

			g_assert(ext_len > 0);
		}

		rc = dbfs_inode_realloc(ino, new_n_extents);
		if (rc)
			return rc;
	}

	ino->raw_inode->size = GUINT64_TO_LE(new_size);
	return 0;
}

int dbfs_write(DB_TXN *txn, guint64 ino_n, guint64 off, const void *buf,
	       size_t buflen)
{
	struct dbfs_extent ext;
	struct dbfs_inode *ino;
	int rc;
	guint64 i_size, old_i_size;

	rc = dbfs_inode_read(txn, ino_n, &ino);
	if (rc)
		return rc;

	old_i_size = i_size = GUINT64_FROM_LE(ino->raw_inode->size);

	if (debugging)
		syslog(LOG_DEBUG, "dbfs_write: ino %Lu, off %Lu, len %zu, i_size %Lu",
		       (unsigned long long) ino_n,
		       (unsigned long long) off,
		       buflen,
		       (unsigned long long) i_size);

	if ((0xffffffffffffffffULL - off) < buflen)
		return -EINVAL;

	rc = dbfs_write_buf(txn, buf, buflen, &ext);
	if (rc)
		goto out;

	if (off > i_size) {
		rc = dbfs_inode_resize(txn, ino, off + 1);
		if (rc)
			goto err_out;
	}

	/* read again; it may have changed after calling dbfs_inode_resize() */
	i_size = GUINT64_FROM_LE(ino->raw_inode->size);

	if (debugging && (i_size != old_i_size))
		syslog(LOG_DEBUG, "dbfs_write: i_size updated to %Lu",
		       (unsigned long long) i_size);

	/* append */
	if (off == i_size) {
		unsigned int idx;

		rc = dbfs_inode_resize(txn, ino, i_size + buflen);
		if (rc)
			goto err_out;

		idx = ino->n_extents - 1;

		g_assert(is_null_id(&ino->raw_inode->blocks[idx].id));
		memcpy(&ino->raw_inode->blocks[idx], &ext, sizeof(ext));

		goto out_write;
	}

	/* FIXME: update data in middle of file */
	syslog(LOG_ERR, "dbfs_write: should be updating data in middle of file!");

out_write:
	rc = dbfs_inode_write(txn, ino);
	if (rc == 0)
		rc = buflen;
	goto out;

err_out:
out:
	dbfs_inode_free(ino);
	return rc;
}