aboutsummaryrefslogtreecommitdiffstats
path: root/dbfs.c
blob: bcdfa7f099f3351d58a92ae09bc078a3340d414d (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
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
/*
 * 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.
 *
 */

#define FUSE_USE_VERSION 26

#define _BSD_SOURCE

#include "dbfs-config.h"
#include <fuse_lowlevel.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <sys/vfs.h>
#include <glib.h>
#include <db.h>
#include "dbfs.h"

int debugging = 1;

static void dbfs_fill_attr(const struct dbfs_inode *ino, struct stat *st)
{
	memset(st, 0, sizeof(*st));
	st->st_dev	= 1;
	st->st_ino	= GUINT64_FROM_LE(ino->raw_inode->ino);
	st->st_mode	= GUINT32_FROM_LE(ino->raw_inode->mode);
	st->st_nlink	= GUINT32_FROM_LE(ino->raw_inode->nlink);
	st->st_uid	= GUINT32_FROM_LE(ino->raw_inode->uid);
	st->st_gid	= GUINT32_FROM_LE(ino->raw_inode->gid);
	st->st_rdev	= GUINT64_FROM_LE(ino->raw_inode->rdev);
	st->st_size	= GUINT64_FROM_LE(ino->raw_inode->size);
	st->st_blksize	= 512;
	st->st_blocks	= GUINT64_FROM_LE(ino->raw_inode->size) / 512ULL;
	st->st_atime	= GUINT64_FROM_LE(ino->raw_inode->atime);
	st->st_mtime	= GUINT64_FROM_LE(ino->raw_inode->mtime);
	st->st_ctime	= GUINT64_FROM_LE(ino->raw_inode->ctime);

	if (debugging)
		syslog(LOG_DEBUG, "fill_attr: ino %lu, mode %u, uid %u, "
			"gid %u, size %lu, mtime %lu",
		       st->st_ino,
		       st->st_mode,
		       st->st_uid,
		       st->st_gid,
		       st->st_size,
		       st->st_mtime);

}

static void dbfs_fill_ent(const struct dbfs_inode *ino,
			  struct fuse_entry_param *ent)
{
	memset(ent, 0, sizeof(*ent));

	ent->ino = GUINT64_FROM_LE(ino->raw_inode->ino);
	ent->generation = GUINT64_FROM_LE(ino->raw_inode->version);

	dbfs_fill_attr(ino, &ent->attr);

	/* these timeouts are just a guess */
	ent->attr_timeout = 2.0;
	ent->entry_timeout = 2.0;
}

static void dbfs_reply_ino(fuse_req_t req, struct dbfs_inode *ino)
{
	struct fuse_entry_param ent;

	dbfs_fill_ent(ino, &ent);

	fuse_reply_entry(req, &ent);

	dbfs_inode_free(ino);
}

static void dbfs_op_init(void *userdata, struct fuse_conn_info *conn)
{
	struct dbfs *fs;
	int rc;

	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_init");

	fs = dbfs_new();

	rc = dbfs_open(fs, DB_RECOVER | DB_CREATE, DB_CREATE, "dbfs", TRUE);
	if (rc) {
		syslog(LOG_ERR, "dbfs_open failed");
		abort();			/* TODO: improve */
	}

	gfs = fs;

	syslog(LOG_INFO, PACKAGE_STRING " initialized");
}

static void dbfs_op_destroy(void *userdata)
{
	struct dbfs *fs = gfs;

	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_destroy");

	dbfs_close(fs);
	dbfs_free(fs);

	gfs = NULL;

	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_destroy");
}

static void dbfs_op_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
{
	guint64 ino_n;
	struct dbfs_inode *ino;
	int rc;
	DB_TXN *txn;
	unsigned long long ino_pr;

	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_lookup, name=='%s'", name);

	rc = gfs->env->txn_begin(gfs->env, NULL, &txn, 0);
	if (rc) {
		fuse_reply_err(req, rc);
		return;
	}

	/* lookup inode in parent directory */
	rc = dbfs_dir_lookup(txn, parent, name, &ino_n);
	if (rc)
		goto err_out;

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

	rc = txn->commit(txn, 0);
	if (rc) {
		dbfs_inode_free(ino);
		fuse_reply_err(req, rc);
		return;
	}

	/* send reply */
	ino_pr = GUINT64_FROM_LE(ino->raw_inode->ino);
	dbfs_reply_ino(req, ino);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_lookup, ino==%Lu", ino_pr);
	return;

err_out:
	txn->abort(txn);
	fuse_reply_err(req, -rc);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_lookup, rc==%d", -rc);
}

static void dbfs_op_getattr(fuse_req_t req, fuse_ino_t ino_n,
			     struct fuse_file_info *fi)
{
	struct dbfs_inode *ino;
	struct stat st;
	int rc;
	DB_TXN *txn;

	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_getattr, ino==%lu", ino_n);

	rc = gfs->env->txn_begin(gfs->env, NULL, &txn, 0);
	if (rc) {
		fuse_reply_err(req, rc);
		return;
	}

	/* read inode from database */
	rc = dbfs_inode_read(txn, ino_n, &ino);
	if (rc) {
		rc = ENOENT;
		goto err_out_txn;
	}

	rc = txn->commit(txn, 0);
	if (rc) {
		rc = -rc;
		goto err_out;
	}

	/* fill in stat buf, taking care to convert from
	 * little endian to native endian
	 */
	dbfs_fill_attr(ino, &st);

	/* send result back to FUSE */
	fuse_reply_attr(req, &st, 2.0);

	dbfs_inode_free(ino);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_getattr, ino==%Lu",
			(unsigned long long) st.st_ino);
	return;

err_out_txn:
	txn->abort(txn);
err_out:
	fuse_reply_err(req, rc);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_getattr, rc==%d", rc);
}

static void dbfs_op_setattr(fuse_req_t req, fuse_ino_t ino_n,
			    struct stat *attr, int to_set,
			    struct fuse_file_info *fi)
{
	struct dbfs_inode *ino;
	struct stat st;
	int rc, dirty = 0;
	DB_TXN *txn;

	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_setattr, ino==%lu", ino_n);

	rc = gfs->env->txn_begin(gfs->env, NULL, &txn, 0);
	if (rc) {
		rc = -rc;
		goto err_out;
	}

	/* read inode from database */
	rc = dbfs_inode_read(txn, ino_n, &ino);
	if (rc)
		goto err_out_txn;

	if (to_set & FUSE_SET_ATTR_MODE) {
		ino->raw_inode->mode = GUINT32_TO_LE(attr->st_mode);
		dirty = 1;
	}
	if (to_set & FUSE_SET_ATTR_UID) {
		ino->raw_inode->uid = GUINT32_TO_LE(attr->st_uid);
		dirty = 1;
	}
	if (to_set & FUSE_SET_ATTR_GID) {
		ino->raw_inode->gid = GUINT32_TO_LE(attr->st_gid);
		dirty = 1;
	}
	if (to_set & FUSE_SET_ATTR_SIZE) {
		rc = dbfs_inode_resize(txn, ino, attr->st_size);
		if (rc)
			goto err_out_free;
		ino->raw_inode->size = GUINT64_TO_LE(attr->st_size);
		dirty = 1;
	}
	if (to_set & FUSE_SET_ATTR_ATIME) {
		ino->raw_inode->atime = GUINT64_TO_LE(attr->st_atime);
		dirty = 1;
	}
	if (to_set & FUSE_SET_ATTR_MTIME) {
		ino->raw_inode->mtime = GUINT64_TO_LE(attr->st_mtime);
		dirty = 1;
	}

	if (dirty) {
		rc = dbfs_inode_write(txn, ino);
		if (rc)
			goto err_out_free;
	}

	rc = txn->commit(txn, 0);
	if (rc) {
		rc = -rc;
		txn = NULL;
		goto err_out_free;
	}

	dbfs_fill_attr(ino, &st);
	dbfs_inode_free(ino);
	fuse_reply_attr(req, &st, 2.0);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_setattr, ino==%Lu",
			(unsigned long long) st.st_ino);
	return;

err_out_free:
	dbfs_inode_free(ino);
err_out_txn:
	if (txn)
		txn->abort(txn);
err_out:
	fuse_reply_err(req, -rc);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_setattr, rc==%d", -rc);
}

static void dbfs_op_readlink(fuse_req_t req, fuse_ino_t ino)
{
	int rc;
	DBT val;
	char *s;
	DB_TXN *txn;

	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_readlink, ino==%lu", ino);

	rc = gfs->env->txn_begin(gfs->env, NULL, &txn, 0);
	if (rc) {
		rc = -rc;
		goto err_out;
	}

	/* read link from database */
	rc = dbfs_symlink_read(txn, ino, &val);
	if (rc)
		goto err_out_txn;

	rc = txn->commit(txn, 0);
	if (rc) {
		rc = -rc;
		free(val.data);
		goto err_out;
	}

	/* send reply; use g_strndup to append a trailing null */
	s = g_strndup(val.data, val.size);
	fuse_reply_readlink(req, s);

	free(val.data);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_readlink, linktext=='%s'", s);
	g_free(s);
	return;

err_out_txn:
	txn->abort(txn);
err_out:
	fuse_reply_err(req, -rc);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_readlink, rc==%d", -rc);
}

static int dbfs_mode_validate(mode_t mode)
{
	unsigned int ifmt = mode & S_IFMT;
	int rc = 0;

	if (S_ISREG(mode)) {
		if (ifmt & ~S_IFREG)
			rc = -EINVAL;
	}
	else if (S_ISDIR(mode)) {
		if (ifmt & ~S_IFDIR)
			rc = -EINVAL;
	}
	else if (S_ISCHR(mode)) {
		if (ifmt & ~S_IFCHR)
			rc = -EINVAL;
	}
	else if (S_ISBLK(mode)) {
		if (ifmt & ~S_IFBLK)
			rc = -EINVAL;
	}
	else if (S_ISFIFO(mode)) {
		if (ifmt & ~S_IFIFO)
			rc = -EINVAL;
	}
	else if (S_ISLNK(mode))
		rc = -EINVAL;
	else if (S_ISSOCK(mode)) {
		if (ifmt & ~S_IFSOCK)
			rc = -EINVAL;
	}
	else
		rc = -EINVAL;

	return rc;
}

static void dbfs_op_mknod(fuse_req_t req, fuse_ino_t parent, const char *name,
			  mode_t mode, dev_t rdev)
{
	struct dbfs_inode *ino;
	int rc;
	DB_TXN *txn;
	unsigned long long ino_pr;

	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_mknod, name='%s'", name);

	rc = gfs->env->txn_begin(gfs->env, NULL, &txn, 0);
	if (rc) {
		rc = -rc;
		goto err_out;
	}

	rc = dbfs_mode_validate(mode);
	if (rc)
		goto err_out_txn;

	/* these have separate inode-creation hooks */
	if (S_ISDIR(mode) || S_ISLNK(mode)) {
		rc = -EINVAL;
		goto err_out_txn;
	}

	rc = dbfs_mknod(txn, parent, name, mode, rdev, &ino);
	if (rc)
		goto err_out_txn;

	rc = txn->commit(txn, 0);
	if (rc) {
		dbfs_inode_free(ino);
		rc = -rc;
		goto err_out;
	}

	ino_pr = GUINT64_FROM_LE(ino->raw_inode->ino);
	dbfs_reply_ino(req, ino);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_mknod, ino==%Lu", ino_pr);
	return;

err_out_txn:
	txn->abort(txn);
err_out:
	fuse_reply_err(req, -rc);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_mknod, rc==%d", -rc);
}

static void dbfs_op_mkdir(fuse_req_t req, fuse_ino_t parent, const char *name,
			  mode_t mode)
{
	struct dbfs_inode *ino;
	int rc;
	DB_TXN *txn;
	unsigned long long ino_pr;

	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_mkdir, name=='%s'", name);

	rc = gfs->env->txn_begin(gfs->env, NULL, &txn, 0);
	if (rc) {
		rc = -rc;
		goto err_out;
	}

	mode &= ALLPERMS;
	mode |= S_IFDIR;

	rc = dbfs_mknod(txn, parent, name, mode, 0, &ino);
	if (rc)
		goto err_out_txn;

	rc = txn->commit(txn, 0);
	if (rc) {
		dbfs_inode_free(ino);
		rc = -rc;
		goto err_out;
	}

	ino_pr = GUINT64_FROM_LE(ino->raw_inode->ino);
	dbfs_reply_ino(req, ino);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_mkdir, ino==%Lu", ino_pr);
	return;

err_out_txn:
	txn->abort(txn);
err_out:
	fuse_reply_err(req, -rc);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_mkdir, rc==%d", -rc);
}

static void dbfs_op_unlink(fuse_req_t req, fuse_ino_t parent, const char *name)
{
	int rc;
	DB_TXN *txn;

	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_unlink, name=='%s'", name);

	rc = gfs->env->txn_begin(gfs->env, NULL, &txn, 0);
	if (rc) {
		rc = -rc;
		goto out;
	}

	rc = dbfs_unlink(txn, parent, name, 0);
	if (rc)
		goto err_out;

	rc = txn->commit(txn, 0);
	if (rc) {
		rc = -rc;
		goto out;
	}

out:
	fuse_reply_err(req, -rc);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_unlink, rc==%d", -rc);
	return;

err_out:
	txn->abort(txn);
	goto out;
}

static void dbfs_op_link(fuse_req_t req, fuse_ino_t ino_n, fuse_ino_t parent,
			 const char *newname)
{
	struct dbfs_inode *ino;
	int rc;
	DB_TXN *txn;
	unsigned long long ino_pr;

	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_link, ino==%lu, newname=='%s'",
			ino_n, newname);

	rc = gfs->env->txn_begin(gfs->env, NULL, &txn, 0);
	if (rc) {
		rc = -rc;
		goto err_out;
	}

	/* read inode from database */
	rc = dbfs_inode_read(txn, ino_n, &ino);
	if (rc) {
		rc = -ENOENT;
		goto err_out_txn;
	}

	/* attempt to create hard link */
	rc = dbfs_link(txn, ino, ino_n, parent, newname);
	if (rc)
		goto err_out_ino;

	rc = txn->commit(txn, 0);
	if (rc) {
		dbfs_inode_free(ino);
		rc = -rc;
		goto err_out;
	}

	ino_pr = GUINT64_FROM_LE(ino->raw_inode->ino);
	dbfs_reply_ino(req, ino);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_link, ino==%Lu", ino_pr);
	return;

err_out_ino:
	dbfs_inode_free(ino);
err_out_txn:
	txn->abort(txn);
err_out:
	fuse_reply_err(req, -rc);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_link, rc==%d", -rc);
}

static void dbfs_op_open(fuse_req_t req, fuse_ino_t ino,
			 struct fuse_file_info *fi)
{
	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_open, ino==%lu", ino);

	fi->direct_io = 0;
	fi->keep_cache = 1;
	fuse_reply_open(req, fi);
}

static void dbfs_op_read(fuse_req_t req, fuse_ino_t ino, size_t size,
			 off_t off, struct fuse_file_info *fi)
{
	void *buf = NULL;
	int rc, rc2;
	DB_TXN *txn;

	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_read, ino==%lu", ino);

	rc = gfs->env->txn_begin(gfs->env, NULL, &txn, 0);
	if (rc) {
		rc = -rc;
		goto err_out;
	}

	rc = dbfs_read(txn, ino, off, size, &buf);
	if (rc < 0)
		goto err_out_txn;

	rc2 = txn->commit(txn, 0);
	if (rc2) {
		rc = -rc2;
		goto err_out;
	}

	fuse_reply_buf(req, buf, rc);
	free(buf);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_read, rc==%d", rc);
	return;

err_out_txn:
	txn->abort(txn);
err_out:
	fuse_reply_err(req, -rc);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_read, rc==%d", -rc);
}

static void dbfs_op_write(fuse_req_t req, fuse_ino_t ino, const char *buf,
			  size_t size, off_t off, struct fuse_file_info *fi)
{
	int rc, rc2;
	DB_TXN *txn;

	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_write, ino==%lu", ino);

	rc = gfs->env->txn_begin(gfs->env, NULL, &txn, 0);
	if (rc) {
		rc = -rc;
		goto err_out;
	}

	rc = dbfs_write(txn, ino, off, buf, size);
	if (rc < 0)
		goto err_out_txn;

	rc2 = txn->commit(txn, 0);
	if (rc2) {
		rc = -rc2;
		goto err_out;
	}

	fuse_reply_write(req, rc);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_write, rc==%d", rc);
	return;

err_out_txn:
	txn->abort(txn);
err_out:
	fuse_reply_err(req, -rc);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_write, error, rc==%d", -rc);
}

static int dbfs_chk_empty(struct dbfs_dirent *de, void *userdata)
{
	if ((GUINT16_FROM_LE(de->namelen) == 1) && (!memcmp(de->name, ".", 1)))
		return 0;
	if ((GUINT16_FROM_LE(de->namelen) == 2) && (!memcmp(de->name, "..", 2)))
		return 0;
	return ENOTEMPTY;
}

static void dbfs_op_rmdir(fuse_req_t req, fuse_ino_t parent, const char *name)
{
	guint64 ino_n;
	int rc;
	DBT val;
	DB_TXN *txn;

	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_rmdir, name=='%s'", name);

	rc = gfs->env->txn_begin(gfs->env, NULL, &txn, 0);
	if (rc) {
		rc = -rc;
		goto out;
	}

	/* get inode number associated with name */
	rc = dbfs_dir_lookup(txn, parent, name, &ino_n);
	if (rc)
		goto out_txn;

	/* read dir associated with name */
	rc = dbfs_dir_read(txn, ino_n, &val);
	if (rc)
		goto out_txn;

	/* make sure dir only contains "." and ".." */
	rc = dbfs_dir_foreach(val.data, dbfs_chk_empty, NULL);
	free(val.data);

	/* if dbfs_chk_empty() returns non-zero, dir is not empty */
	if (rc)
		goto out_txn;

	/* dir is empty, go ahead and unlink */
	rc = dbfs_unlink(txn, parent, name, DBFS_UNLINK_DIR);
	if (rc)
		goto out_txn;

	rc = txn->commit(txn, 0);
	if (rc) {
		rc = -rc;
		goto out;
	}

	fuse_reply_err(req, 0);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_rmdir, rc==0");
	return;

out_txn:
	txn->abort(txn);
out:
	fuse_reply_err(req, -rc);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_rmdir, rc==%d", -rc);
}

static void dbfs_op_symlink(fuse_req_t req, const char *link,
			    fuse_ino_t parent, const char *name)
{
	struct dbfs_inode *ino;
	int rc;
	DB_TXN *txn;
	unsigned long long ino_pr;

	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_symlink, name=='%s'", name);

	if (!g_utf8_validate(link, -1, NULL)) {
		rc = -EINVAL;
		goto err_out;
	}

	rc = gfs->env->txn_begin(gfs->env, NULL, &txn, 0);
	if (rc) {
		rc = -rc;
		goto err_out;
	}

	rc = dbfs_mknod(txn, parent, name,
			S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO, 0, &ino);
	if (rc)
		goto err_out_txn;

	rc = dbfs_symlink_write(txn, GUINT64_FROM_LE(ino->raw_inode->ino), link);
	if (rc)
		goto err_out_ino;

	rc = txn->commit(txn, 0);
	if (rc) {
		dbfs_inode_free(ino);
		rc = -rc;
		goto err_out;
	}

	ino_pr = GUINT64_FROM_LE(ino->raw_inode->ino);
	dbfs_reply_ino(req, ino);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_symlink, ino==%Lu", ino_pr);
	return;

err_out_ino:
	dbfs_inode_free(ino);
err_out_txn:
	txn->abort(txn);
err_out:
	fuse_reply_err(req, -rc);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_symlink, rc==%d", -rc);
}

static void dbfs_op_rename(fuse_req_t req, fuse_ino_t parent,
			   const char *name, fuse_ino_t newparent,
			   const char *newname)
{
	int rc;
	DB_TXN *txn;

	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_rename, name=='%s', newname=='%s'",
			name, newname);

	rc = gfs->env->txn_begin(gfs->env, NULL, &txn, 0);
	if (rc) {
		rc = -rc;
		goto out;
	}

	rc = dbfs_rename(txn, parent, name, newparent, newname);
	if (rc)
		goto out_txn;

	rc = txn->commit(txn, 0);
	if (rc) {
		rc = -rc;
		goto out;
	}

	fuse_reply_err(req, 0);
	return;

out_txn:
	txn->abort(txn);
out:
	fuse_reply_err(req, -rc);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_rename, rc==%d", -rc);
}

static void dbfs_op_fsync (fuse_req_t req, fuse_ino_t ino,
			   int datasync, struct fuse_file_info *fi)
{
	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_fsync, ino==%lu", ino);

	/* DB should have already sync'd our data for us */
	fuse_reply_err(req, 0);
}

static void dbfs_op_opendir(fuse_req_t req, fuse_ino_t ino,
			    struct fuse_file_info *fi)
{
	DBT val;
	int rc;
	DB_TXN *txn;

	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_opendir, ino==%lu", ino);

	rc = gfs->env->txn_begin(gfs->env, NULL, &txn, 0);
	if (rc) {
		rc = -rc;
		goto err_out;
	}

	/* read directory from database */
	rc = dbfs_dir_read(txn, ino, &val);
	if (rc)
		goto err_out_txn;

	rc = txn->commit(txn, 0);
	if (rc) {
		rc = -rc;
		goto err_out;
	}

	/* save for later use */
	fi->fh = (uint64_t) (unsigned long) val.data;

	/* send reply */
	fuse_reply_open(req, fi);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_opendir");
	return;

err_out_txn:
	txn->abort(txn);
err_out:
	fuse_reply_err(req, -rc);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_opendir, rc==%d", -rc);
}

struct dirbuf {
	char		*p;
	size_t		size;
};

struct dirbuf_iter {
	struct dirbuf	db;
	fuse_req_t	req;
};

/* stock function copied from FUSE template (hello_ll.c) */
static void dirbuf_add(fuse_req_t req, struct dirbuf *b, const char *name,
		       fuse_ino_t ino)
{
	struct stat stbuf;
	size_t oldsize = b->size;
	b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0);
	b->p = (char *) realloc(b->p, b->size);
	memset(&stbuf, 0, sizeof(stbuf));
	stbuf.st_ino = ino;
	fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf,
			  b->size);
}

#ifndef min
#define min(x, y) ((x) < (y) ? (x) : (y))
#endif

/* stock function copied from FUSE template */
static int reply_buf_limited(fuse_req_t req, const void *buf, size_t bufsize,
			     off_t off, size_t maxsize)
{
	if (off < bufsize)
		return fuse_reply_buf(req, buf + off,
				      min(bufsize - off, maxsize));
	else
		return fuse_reply_buf(req, NULL, 0);
}

static int dbfs_fill_dirbuf(struct dbfs_dirent *de, void *userdata)
{
	struct dirbuf_iter *di = userdata;
	struct dirbuf *b = &di->db;
	char *s;

	/* add dirent to buffer; use g_strndup solely to append nul */
	s = g_strndup(de->name, GUINT16_FROM_LE(de->namelen));
	dirbuf_add(di->req, b, s, GUINT64_FROM_LE(de->ino));
	free(s);
	return 0;
}

static void dbfs_op_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
			     off_t off, struct fuse_file_info *fi)
{
	struct dirbuf_iter di;
	void *p;

	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_readdir, ino==%lu", ino);

	/* grab directory contents stored by opendir */
	p = (void *) (unsigned long) fi->fh;

	/* iterate through each dirent, filling dirbuf */
	memset(&di, 0, sizeof(di));
	dbfs_dir_foreach(p, dbfs_fill_dirbuf, &di);

	/* send reply */
	reply_buf_limited(req, di.db.p, di.db.size, off, size);
	free(di.db.p);

	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_readdir");
}

static void dbfs_op_releasedir(fuse_req_t req, fuse_ino_t ino,
			       struct fuse_file_info *fi)
{
	void *p = (void *) (unsigned long) fi->fh;

	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_releasedir, ino==%lu", ino);

	/* release directory contents */
	free(p);

	fuse_reply_err(req, 0);
}

static void dbfs_op_fsyncdir (fuse_req_t req, fuse_ino_t ino,
			      int datasync, struct fuse_file_info *fi)
{
	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_fsyncdir, ino==%lu", ino);

	/* DB should have already sync'd our data for us */
	fuse_reply_err(req, 0);
}

#define COPY(x) f.f_##x = st.f_##x
static void dbfs_op_statfs(fuse_req_t req, fuse_ino_t ino)
{
	struct statvfs f;
	struct statfs st;

	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_statfs");

	if (statfs(gfs->home, &st) < 0) {
		fuse_reply_err(req, errno);
		return;
	}

	memset(&f, 0, sizeof(f));
	COPY(bsize);
	f.f_frsize = 512;
	COPY(blocks);
	COPY(bfree);
	COPY(bavail);
	f.f_files = 0xfffffff;
	f.f_ffree = 0xffffff;
	f.f_favail = 0xffffff;
	f.f_fsid = 0xdeadbeef;
	f.f_flag = 0;
	f.f_namemax = DBFS_FILENAME_MAX;

	fuse_reply_statfs(req, &f);
}
#undef COPY

static void dbfs_op_setxattr(fuse_req_t req, fuse_ino_t ino,
			     const char *name, const char *value,
			     size_t size, int flags)
{
	int rc;
	DB_TXN *txn;

	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_setxattr, name=='%s'", name);

	rc = gfs->env->txn_begin(gfs->env, NULL, &txn, 0);
	if (rc) {
		rc = -rc;
		goto err_out;
	}

	rc = dbfs_xattr_set(txn, ino, name, value, size, flags);
	if (rc)
		goto err_out_txn;

	rc = txn->commit(txn, 0);
	if (rc) {
		rc = -rc;
		goto err_out;
	}

	fuse_reply_err(req, 0);
	return;

err_out_txn:
	txn->abort(txn);
err_out:
	fuse_reply_err(req, -rc);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_setxattr, rc==%d", -rc);
}

static void dbfs_op_getxattr(fuse_req_t req, fuse_ino_t ino,
			     const char *name, size_t size)
{
	void *buf = NULL;
	size_t buflen = 0;
	int rc;
	DB_TXN *txn;

	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_getxattr, name=='%s'", name);

	rc = gfs->env->txn_begin(gfs->env, NULL, &txn, 0);
	if (rc) {
		rc = -rc;
		goto err_out;
	}

	rc = dbfs_xattr_get(txn, ino, name, &buf, &buflen);
	if (rc)
		goto err_out_txn;

	rc = txn->commit(txn, 0);
	if (rc) {
		rc = -rc;
		goto err_out;
	}

	if (size == 0)
		fuse_reply_xattr(req, buflen);
	else if (buflen <= size)
		fuse_reply_buf(req, buf, buflen);
	else {
		rc = -ERANGE;
		goto err_out;
	}

	free(buf);
	return;

err_out_txn:
	txn->abort(txn);
err_out:
	fuse_reply_err(req, -rc);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_getxattr, rc==%d", -rc);
}

static void dbfs_op_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
{
	int rc;
	void *buf;
	size_t buflen;
	DB_TXN *txn;

	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_listxattr, ino==%lu", ino);

	rc = gfs->env->txn_begin(gfs->env, NULL, &txn, 0);
	if (rc) {
		rc = -rc;
		goto err_out;
	}

	rc = dbfs_xattr_list(txn, ino, &buf, &buflen);
	if (rc < 0)
		goto err_out_txn;

	rc = txn->commit(txn, 0);
	if (rc) {
		rc = -rc;
		goto err_out;
	}

	if (size == 0)
		fuse_reply_xattr(req, buflen);
	else if (size < buflen)
		fuse_reply_err(req, ERANGE);
	else
		fuse_reply_buf(req, buf, buflen);

	free(buf);
	return;

err_out_txn:
	txn->abort(txn);
err_out:
	fuse_reply_err(req, -rc);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_listxattr, rc==%d", -rc);
}

static void dbfs_op_removexattr(fuse_req_t req, fuse_ino_t ino,
				const char *name)
{
	int rc;
	DB_TXN *txn;

	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_removexattr, ino==%lu, name=='%s'",
			ino, name);

	rc = gfs->env->txn_begin(gfs->env, NULL, &txn, 0);
	if (rc) {
		rc = -rc;
		goto err_out;
	}

	rc = dbfs_xattr_remove(txn, ino, name, TRUE);
	if (rc)
		goto err_out_txn;

	rc = txn->commit(txn, 0);
	if (rc) {
		rc = -rc;
		goto err_out;
	}

	fuse_reply_err(req, 0);
	return;

err_out_txn:
	txn->abort(txn);
err_out:
	fuse_reply_err(req, -rc);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_removexattr, rc==%d", -rc);
}

static void dbfs_op_access(fuse_req_t req, fuse_ino_t ino_n, int mask)
{
	struct dbfs_inode *ino;
	const struct fuse_ctx *ctx;
	int rc;
	guint32 mode, uid, gid;
	DB_TXN *txn;

	if (debugging)
		syslog(LOG_DEBUG, "ENTER dbfs_op_access, ino==%lu", ino_n);

	ctx = fuse_req_ctx(req);
	g_assert(ctx != NULL);

	rc = gfs->env->txn_begin(gfs->env, NULL, &txn, 0);
	if (rc) {
		rc = -rc;
		goto out;
	}

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

	rc = txn->commit(txn, 0);
	if (rc) {
		dbfs_inode_free(ino);
		rc = -rc;
		goto out;
	}

	mode = GUINT32_FROM_LE(ino->raw_inode->mode);
	uid = GUINT32_FROM_LE(ino->raw_inode->uid);
	gid = GUINT32_FROM_LE(ino->raw_inode->gid);

	if (uid == ctx->uid)
		mode >>= 8;
	else if (gid == ctx->gid)
		mode >>= 4;

	rc = 0;
	if ((mask & R_OK) && (!(mode & S_IROTH)))
		rc = -EACCES;
	if ((mask & W_OK) && (!(mode & S_IWOTH)))
		rc = -EACCES;
	if ((mask & X_OK) && (!(mode & S_IXOTH)))
		rc = -EACCES;

	dbfs_inode_free(ino);

out:
	fuse_reply_err(req, -rc);
	if (debugging)
		syslog(LOG_DEBUG, "EXIT dbfs_op_access, rc==%d", -rc);
	return;

out_txn:
	txn->abort(txn);
	goto out;
}

static struct fuse_lowlevel_ops dbfs_ops = {
	.init		= dbfs_op_init,
	.destroy	= dbfs_op_destroy,
	.lookup		= dbfs_op_lookup,
	.forget		= NULL,
	.getattr	= dbfs_op_getattr,
	.setattr	= dbfs_op_setattr,
	.readlink	= dbfs_op_readlink,
	.mknod		= dbfs_op_mknod,
	.mkdir		= dbfs_op_mkdir,
	.unlink		= dbfs_op_unlink,
	.rmdir		= dbfs_op_rmdir,
	.symlink	= dbfs_op_symlink,
	.rename		= dbfs_op_rename,
	.link		= dbfs_op_link,
	.open		= dbfs_op_open,
	.read		= dbfs_op_read,
	.write		= dbfs_op_write,
	.flush		= NULL,
	.release	= NULL,
	.fsync		= dbfs_op_fsync,
	.opendir	= dbfs_op_opendir,
	.readdir	= dbfs_op_readdir,
	.releasedir	= dbfs_op_releasedir,
	.fsyncdir	= dbfs_op_fsyncdir,
	.statfs		= dbfs_op_statfs,
	.setxattr	= dbfs_op_setxattr,
	.getxattr	= dbfs_op_getxattr,
	.listxattr	= dbfs_op_listxattr,
	.removexattr	= dbfs_op_removexattr,
	.access		= dbfs_op_access,
	.create		= NULL,
};

/* stock main() from FUSE example */
int main(int argc, char *argv[])
{
	struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
	struct fuse_chan *ch;
	char *mountpoint;
	int err = -1;

	openlog("dbfs", LOG_PID, LOG_LOCAL4);

	if (fuse_parse_cmdline(&args, &mountpoint, NULL, NULL) != -1 &&
	    (ch = fuse_mount(mountpoint, &args)) != NULL) {
		struct fuse_session *se;

		se = fuse_lowlevel_new(&args, &dbfs_ops,
				       sizeof(dbfs_ops), NULL);
		if (se != NULL) {
			if (fuse_set_signal_handlers(se) != -1) {
				fuse_session_add_chan(se, ch);
				err = fuse_session_loop(se);
				fuse_remove_signal_handlers(se);
				fuse_session_remove_chan(ch);
			}
			fuse_session_destroy(se);
		}
		fuse_unmount(mountpoint, ch);
	}
	fuse_opt_free_args(&args);

	return err ? 1 : 0;
}