summaryrefslogtreecommitdiff
path: root/src/netfs.c
blob: a8718369caed078c4664eaa1db2a9f20f0fbc110 (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
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
/**********************************************************
 * netfs.c
 *
 * Copyright(C) 2004,2005,2006 by Stefan Siegl <stesie@brokenpipe.de>, Germany
 * 
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Publice License,
 * version 2 or any later. The license is contained in the COPYING
 * file that comes with the libfuse distribution.
 *
 * callback functions for libnetfs
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stddef.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>

#include <hurd/netfs.h>

#include <stdio.h>

#include "fuse_i.h"
#include "fuse.h"





/* fuse_dirhandle, passed to ops->getdir to store our information */
struct fuse_dirhandle {
  int first_entry;         /* index of first entry to return (counted down
			    * in helper function) */
  int num_entries;         /* number of further entries we may write out to the
			    * buffer, counted down in callback function*/
  int count;               /* number of elements, we already wrote to buffer */
  size_t size;             /* number of further bytes we may write to buffer */
  struct dirent *hdrpos;   /* where to write next dirent structure */
  size_t maxlen;           /* (allocated) length of filename field, def 256 */
  struct netnode *parent;  /* netnode of the dir which's content we list */

  char *abspath;
  char *filename;
};


static inline void
refresh_context_struct(struct iouser *cred)
{
  update_context_struct(cred, libfuse_fuse);
}

void
update_context_struct(struct iouser *cred, struct fuse *fuse)
{
  FUNC_PROLOGUE("refresh_context_struct");
  struct fuse_context *ctx = libfuse_ctx;
  
  if(! ctx) 
    {
      ctx = malloc(sizeof(struct fuse_context));
      if(! ctx) 
	{
	  perror(PACKAGE_NAME);
	  return;
	}

      libfuse_ctx = ctx;

      /* FIXME, how to figure out the pid of the program asking for the
       * filesystem operation? */
      ctx->pid = 0;
    }

  ctx->fuse = fuse;
  ctx->private_data = ctx->fuse ? ctx->fuse->private_data : NULL;

  if(cred)
    {
      ctx->uid = cred->uids->num ? cred->uids->ids[0] : 
	(libfuse_params.force_uid ? libfuse_params.uid : geteuid());
      ctx->gid = cred->gids->num ? cred->gids->ids[0] : 
	(libfuse_params.force_gid ? libfuse_params.gid : getegid());
    }
  else
    {
      ctx->uid = libfuse_params.force_uid ? libfuse_params.uid : geteuid();
      ctx->gid = libfuse_params.force_gid ? libfuse_params.gid : getegid();
    }      

  FUNC_EPILOGUE_NORET();
}

/* Check whether to allow access to a node, testing the allow_root and
 * allow_other flag.  This does not check whether default permissions
 * are okay to allow access.
 *
 * Return: 0 if access is to be granted, EPERM otherwise
 *
 * Sidenote: This function is mainly called from netfs_validate_stat which in
 *           turn is called by any other of the netfs-operation-functions. This
 *           is, we don't have to call this from all of the operation-functions
 *           since netfs_validate_stat is called anyways!
 */
static error_t
test_allow_root_or_other (struct iouser *cred)
{
  FUNC_PROLOGUE("test_allow_root_or_other");

  assert(cred);

  /* if allow_other is set, access is okay in any case */
  if(libfuse_params.allow_other) 
    FUNC_RETURN_FMT(0, "allow_other is set");

  unsigned int i;
  uid_t proc_uid = getuid();
  for(i = 0; i < cred->uids->num; i ++) 
    {
      DEBUG("test_allow", "testing for uid=%d\n", cred->uids->ids[i]);

      if(cred->uids->ids[i] == 0 && libfuse_params.allow_root)
	FUNC_RETURN_FMT(0, "allowing access for root");

      if(cred->uids->ids[i] == proc_uid)
	FUNC_RETURN_FMT(0, "allowing access for owner");
    }

  /* iouser is not the "filesystem-owner" and allow_other is not set,
   * or iouser is root but allow_root is not set either */
  FUNC_EPILOGUE(EPERM);
}



/* Make sure that NP->nn_stat is filled with current information.  CRED
   identifies the user responsible for the operation. 

   If the user `CRED' is not allowed to perform any operation (considering
   the allow_root and allow_other flags), return EPERM. */
error_t
netfs_validate_stat (struct node *node, struct iouser *cred)
{
  FUNC_PROLOGUE_NODE("netfs_validate_stat", node);
  error_t err = EOPNOTSUPP;

  if(test_allow_root_or_other(cred))
    FUNC_RETURN(EPERM);

  if(FUSE_OP_HAVE(getattr))
    {
      refresh_context_struct(cred);
      err = -FUSE_OP_CALL(getattr, node->nn->path, &node->nn_stat);
    }

  if(! err)
    {
      if(! libfuse_params.use_ino)
	node->nn_stat.st_ino = node->nn->inode;

      node->nn_stat.st_dev = getpid();
      node->nn_stat.st_blksize = 1 << 12; /* there's probably no sane default,
					   * use 4 kB for the moment */

      if(libfuse_params.force_uid)
	node->nn_stat.st_uid = libfuse_params.uid;

      if(libfuse_params.force_gid)
	node->nn_stat.st_gid = libfuse_params.gid;

      if(libfuse_params.force_umask)
	node->nn_stat.st_mode &= ~libfuse_params.umask;
    }

  FUNC_EPILOGUE(err);
}



/* Read the contents of NODE (a symlink), for USER, into BUF. */
error_t netfs_attempt_readlink (struct iouser *user, struct node *node,
				char *buf)
{
  FUNC_PROLOGUE_NODE("netfs_attempt_readlink", node);
  error_t err;

  if((err = netfs_validate_stat(node, user))
     || (libfuse_params.deflt_perms
	 && (err = fshelp_access(&node->nn_stat, S_IREAD, user))))
    goto out;

  if(FUSE_OP_HAVE(readlink))
    err = -FUSE_OP_CALL(readlink, node->nn->path, buf, node->nn_stat.st_size + 1);
  else
    err = EOPNOTSUPP;

 out:
  FUNC_EPILOGUE(err);
}



/* Attempt to create a file named NAME in DIR for USER with MODE.  Set *NODE
   to the new node upon return.  On any error, clear *NODE.  *NODE should be
   locked on success; no matter what, unlock DIR before returning.  */
error_t
netfs_attempt_create_file (struct iouser *user, struct node *dir,
			   char *name, mode_t mode, struct node **node)
{
  FUNC_PROLOGUE("netfs_attempt_create_file");
  error_t err;
  char *path = NULL;

  if((err = netfs_validate_stat(dir, user))
     || (libfuse_params.deflt_perms
	 && (err = fshelp_checkdirmod(&dir->nn_stat, NULL, user))))
    goto out;

  if(! FUSE_OP_HAVE(mknod))
    {
      err = EOPNOTSUPP;
      goto out;
    }

  if(! (path = malloc(strlen(dir->nn->path) + strlen(name) + 2)))
    {
      err = ENOMEM;
      goto out;
    }

  sprintf(path, "%s/%s", dir->nn->path, name);

  /* FUSE expects us to use mknod function to create files. This is allowed
   * on Linux, however neither the Hurd nor the POSIX standard consider that
   */
  err = -FUSE_OP_CALL(mknod, path, (mode & ALLPERMS) | S_IFREG, 0);

 out:
  if(err)
    *node = NULL;
  else
    {
      /* create a new (net-)node for this file */
      struct netnode *nn = fuse_make_netnode(dir->nn, path);

      if(nn)
	{
	  nn->may_need_sync = 1;
	  *node = fuse_make_node(nn);
	}
      else
	{
	  *node = NULL;
	  err = ENOMEM;
	}
    }

  free(path); /* fuse_make_netnode strdup'ed it. */

  if(*node)
    pthread_mutex_lock(&(*node)->lock);

  pthread_mutex_unlock (&dir->lock);

  FUNC_EPILOGUE(err);
}



/* This should attempt a chmod call for the user specified by CRED on node
   NODE, to change the owner to UID and the group to GID. */
error_t netfs_attempt_chown (struct iouser *cred, struct node *node,
			     uid_t uid, uid_t gid)
{
  FUNC_PROLOGUE_NODE("netfs_attempt_chown", node);
  error_t err;

  if((err = netfs_validate_stat(node, cred))
     || (libfuse_params.deflt_perms
	 && (err = fshelp_isowner(&node->nn_stat, cred))))
    goto out;

  if(! FUSE_OP_HAVE(chown))
    {
      err = EOPNOTSUPP;
      goto out;
    }

  /* FIXME, make sure, that user CRED is not able to change permissions
   * to somebody who is not. That is, don't allow $unpriv_user to change
   * owner to e.g. root.
   */

  err = -FUSE_OP_CALL(chown, node->nn->path, uid, gid);
  node->nn->may_need_sync = 1;
  
 out:
  FUNC_EPILOGUE(err);
}



/* This should attempt to fetch filesystem status information for the remote
   filesystem, for the user CRED. */
error_t
netfs_attempt_statfs (struct iouser *cred, struct node *node,
		      fsys_statfsbuf_t *st)
{
  FUNC_PROLOGUE_NODE("netfs_attempt_statfs", node);
  error_t err;

  if(test_allow_root_or_other(cred))
    err = EPERM;

  else if(FUSE_OP_HAVE(statfs))
    {
      struct statvfs stvfs;

      refresh_context_struct(cred);
      err = -FUSE_OP_CALL(statfs, node->nn->path, &stvfs);

      if(! err)
	{
	  st->f_type = stvfs.__f_type;
	  st->f_bsize = stvfs.f_bsize;
	  st->f_blocks = stvfs.f_blocks;
	  st->f_bfree = stvfs.f_bfree;
	  st->f_bavail = stvfs.f_bavail;
	  st->f_files = stvfs.f_files;
	  st->f_ffree = stvfs.f_ffree;
	  st->f_fsid = stvfs.f_fsid;
	  st->f_namelen = stvfs.f_namemax;
	  st->f_favail = stvfs.f_favail;
	  st->f_frsize = stvfs.f_frsize;
	  st->f_flag = stvfs.f_flag;
	}
    }

  else
    err = EOPNOTSUPP;

  FUNC_EPILOGUE(err);
}



/* Attempt to create a new directory named NAME in DIR for USER with mode
   MODE.  */
error_t netfs_attempt_mkdir (struct iouser *user, struct node *dir,
			     char *name, mode_t mode)
{
  FUNC_PROLOGUE("netfs_attempt_mkdir");
  error_t err;
  char *path = NULL;

  if((err = netfs_validate_stat(dir, user))
     || (libfuse_params.deflt_perms
	 && (err = fshelp_checkdirmod(&dir->nn_stat, NULL, user))))
    goto out;

  if(! FUSE_OP_HAVE(mkdir))
    {
      err = EOPNOTSUPP;
      goto out;
    }

  if(! (path = malloc(strlen(dir->nn->path) + strlen(name) + 2)))
    {
      err = ENOMEM;
      goto out;
    }

  sprintf(path, "%s/%s", dir->nn->path, name);

  err = -FUSE_OP_CALL(mkdir, path, mode & ALLPERMS);

 out:
  /* we don't need to make a netnode already, lookup will be called and do
   * that for us.
   *
   * FIXME we must make sure, that the netnode will have may_need_sync is set
   */
  free(path);
  FUNC_EPILOGUE(err);
}



/* This should attempt a chflags call for the user specified by CRED on node
   NODE, to change the flags to FLAGS. */
error_t netfs_attempt_chflags (struct iouser *cred, struct node *node,
			       int flags)
{
  (void) cred;
  (void) flags;

  FUNC_PROLOGUE_NODE("netfs_attempt_chflags", node);
  NOT_IMPLEMENTED();
  FUNC_EPILOGUE(EOPNOTSUPP);
}



/* Node NODE is being opened by USER, with FLAGS.  NEWNODE is nonzero if we
   just created this node.  Return an error if we should not permit the open
   to complete because of a permission restriction. */
error_t
netfs_check_open_permissions (struct iouser *user, struct node *node,
			      int flags, int newnode)
{
  (void) newnode;

  FUNC_PROLOGUE_FMT("netfs_check_open_permissions", "node=%s, flags=%d", 
		    node->nn->path, flags);
  error_t err = 0;

  if((err = netfs_validate_stat(node, user)))
    goto out;

  if(libfuse_params.deflt_perms)
    {
      if (flags & O_READ)
	err = fshelp_access (&node->nn_stat, S_IREAD, user);
      
      if (!err && (flags & O_WRITE))
	err = fshelp_access (&node->nn_stat, S_IWRITE, user);
      
      if (!err && (flags & O_EXEC))
	err = fshelp_access (&node->nn_stat, S_IEXEC, user);
    }

  /* store provided flags for later open/read/write/release operation call.
   *
   * If O_EXEC is set, make sure O_RDONLY is set, this is, if you want to 
   * execute a binary, only O_EXEC is set, but we want to read the binary
   * into memory. */
  if(! err) 
    {
      NN_INFO_APPLY(node, flags = flags);
      if(flags & O_EXEC) NN_INFO_APPLY(node, flags |= O_RDONLY);
    }

 out:
  FUNC_EPILOGUE(err);
}



/* This should attempt a chmod call for the user specified by CRED on node
   NODE, to change the mode to MODE.  Unlike the normal Unix and Hurd meaning
   of chmod, this function is also used to attempt to change files into other
   types.  If such a transition is attempted which is impossible, then return
   EOPNOTSUPP.  */
error_t netfs_attempt_chmod (struct iouser *cred, struct node *node,
			     mode_t mode)
{
  FUNC_PROLOGUE_NODE("netfs_attempt_chmod", node);
  error_t err;

  if((err = netfs_validate_stat(node, cred))
     || (libfuse_params.deflt_perms
	 && (err = fshelp_isowner(&node->nn_stat, cred))))
    goto out;

  if(! FUSE_OP_HAVE(chmod))
    {
      err = EOPNOTSUPP;
      goto out;
    }

  err = -FUSE_OP_CALL(chmod, node->nn->path, mode);
  node->nn->may_need_sync = 1;
  
 out:
  FUNC_EPILOGUE(err);
}



/* Attempt to create an anonymous file related to DIR for USER with MODE.
   Set *NODE to the returned file upon success.  No matter what, unlock DIR. */
error_t netfs_attempt_mkfile (struct iouser *user, struct node *dir,
			      mode_t mode, struct node **node)
{
  FUNC_PROLOGUE("netfs_attempt_mkfile");
  error_t err;
  char name[20];
  static int num = 0;

  if(! (err = netfs_validate_stat(dir, user))
     && libfuse_params.deflt_perms)
    err = fshelp_checkdirmod(&dir->nn_stat, NULL, user);

  if(! err && ! FUSE_OP_HAVE(mknod))
    err = EOPNOTSUPP;

  if(err)
    {
      /* dir has to be unlocked no matter what ... */
      pthread_mutex_unlock (&dir->lock);
      goto out;
    }

  /* call netfs_attempt_create_file with O_EXCL and O_CREAT bits set */
  mode |= O_EXCL | O_CREAT;

  do 
    {
      snprintf(name, sizeof(name), ".libfuse-%06d", num ++);
      err = netfs_attempt_create_file(user, dir, name, mode, node);

      if(err == EEXIST)
	pthread_mutex_lock(&dir->lock); /* netfs_attempt_create_file just unlocked
				 * it for us, however we need to call it once
				 * more ...
				 */
    } 
  while(err == EEXIST);

 out:
  if(err) 
    *node = 0;
  else
    (*node)->nn->anonymous = 1; /* mark netnode of created file as anonymous,
				 * i.e. mark it as to delete in noref routine
				 */

  /* we don't have to mark the netnode as may_need_sync, as
   * netfs_attempt_create_file has already done that for us
   */

  /* pthread_mutex_unlock (&dir->lock);
   * netfs_attempt_create_file already unlocked the node for us.
   */
  FUNC_EPILOGUE(err);
}



/* This should sync the entire remote filesystem.  If WAIT is set, return
   only after sync is completely finished.  */
error_t netfs_attempt_syncfs (struct iouser *cred, int wait)
{
  (void) wait;   /* there's no such flag in libfuse */

  FUNC_PROLOGUE("netfs_attempt_syncfs");
  error_t err;

  if(cred && test_allow_root_or_other(cred))
    err = EPERM;
  else
    {
      refresh_context_struct(cred);
      err = fuse_sync_filesystem();
    }

  FUNC_EPILOGUE(err);
}



/* This should sync the file NODE completely to disk, for the user CRED.  If
   WAIT is set, return only after sync is completely finished.  */
error_t
netfs_attempt_sync (struct iouser *cred, struct node *node, int wait)
{
  (void) wait;   /* there's no such flag in libfuse */

  FUNC_PROLOGUE_NODE("netfs_attempt_sync", node);
  error_t err;

  if((err = netfs_validate_stat(node, cred))
     || (libfuse_params.deflt_perms
	 && (err = fshelp_access(&node->nn_stat, S_IWRITE, cred))))
    goto out;

  if(! FUSE_OP_HAVE(fsync))
    {
      err = EOPNOTSUPP;
      goto out;
    }
  
  err = -FUSE_OP_CALL(fsync, node->nn->path, 0, NN_INFO(node));

  if(! err)
    node->nn->may_need_sync = 0; 

 out:
  FUNC_EPILOGUE(err);
}



/* Delete NAME in DIR for USER.
 * The node DIR is locked, and shall stay locked
 */
error_t netfs_attempt_unlink (struct iouser *user, struct node *dir,
			      char *name)
{
  FUNC_PROLOGUE("netfs_attempt_unlink");
  error_t err;
  struct node *node = NULL;

  err = netfs_attempt_lookup(user, dir, name, &node);
  assert(dir != node);
  pthread_mutex_lock(&dir->lock); /* re-lock directory, since netfs_attempt_lookup
			   * unlocked it for us
			   */  
  if(err)
    goto out;

  pthread_mutex_unlock(&node->lock);

  if((err = netfs_validate_stat(dir, user))
     || (err = netfs_validate_stat(node, user))
     || (libfuse_params.deflt_perms 
	 && (err = fshelp_checkdirmod(&dir->nn_stat, &node->nn_stat, user))))
    goto out;

  if(FUSE_OP_HAVE(unlink))
    err = -FUSE_OP_CALL(unlink, node->nn->path);
  else
    err = EOPNOTSUPP;

  /* TODO free associated netnode. really? 
   * FIXME, make sure nn->may_need_sync is set */
 out:
  if(node)
    netfs_nrele(node);

  FUNC_EPILOGUE(err);
}



/* This should attempt to set the size of the file NODE (for user CRED) to
   SIZE bytes long. */
error_t netfs_attempt_set_size (struct iouser *cred, struct node *node,
				loff_t size)
{
  FUNC_PROLOGUE_NODE("netfs_attempt_set_size", node);
  error_t err;

  if((err = netfs_validate_stat(node, cred))
     || (libfuse_params.deflt_perms
	 && (err = fshelp_access(&node->nn_stat, S_IWRITE, cred))))
    goto out;

  if(FUSE_OP_HAVE(truncate))
    err = -FUSE_OP_CALL(truncate, node->nn->path, size);
  else
    err = EOPNOTSUPP;

 out:
  FUNC_EPILOGUE(err);
}



/* Attempt to turn NODE (user CRED) into a device.  TYPE is either S_IFBLK or
   S_IFCHR. */
error_t netfs_attempt_mkdev (struct iouser *cred, struct node *node,
			     mode_t type, dev_t indexes)
{
  FUNC_PROLOGUE_NODE("netfs_attempt_mkdev", node);
  error_t err;

  /* check permissions
   * XXX, shall we check permissions of the parent directory as well,
   * since we're going to unlink files?
   */
  if((err = netfs_validate_stat(node, cred))
     || (libfuse_params.deflt_perms
	 && (err = fshelp_access(&node->nn_stat, S_IWRITE, cred))))
    goto out;

  /* check whether the operations are available at all */
  if(! FUSE_OP_HAVE(mknod))
    {
      err = EOPNOTSUPP;
      goto out;
    }

  /* we need to unlink the existing node, therefore, if unlink is not
   * available, we cannot turn *node into a device.
   */
  if(! FUSE_OP_HAVE(unlink))
    {
      err = EOPNOTSUPP;
      goto out;
    }

  /* unlink the already existing node, to be able to create the (new)
   * device file
   */
  if((err = -FUSE_OP_CALL(unlink, node->nn->path)))
    goto out;

  err = -FUSE_OP_CALL(mknod, node->nn->path,
		      type & (ALLPERMS | S_IFBLK | S_IFCHR), indexes);

  node->nn->may_need_sync = 1;

 out:
  FUNC_EPILOGUE(err);
}



/* Return the valid access types (bitwise OR of O_READ, O_WRITE, and O_EXEC)
   in *TYPES for file NODE and user CRED.  */
error_t
netfs_report_access (struct iouser *cred, struct node *node, int *types)
{
  FUNC_PROLOGUE_NODE("netfs_report_access", node);
  error_t err;
  *types = 0;

  if((err = netfs_validate_stat(node, cred)))
    goto out;
  
  if(libfuse_params.deflt_perms) 
    {
      if(fshelp_access (&node->nn_stat, S_IREAD, cred) == 0)
	*types |= O_READ;
      
      if(fshelp_access (&node->nn_stat, S_IWRITE, cred) == 0)
	*types |= O_WRITE;

      if(fshelp_access (&node->nn_stat, S_IEXEC, cred) == 0)
	*types |= O_EXEC;
    }
  else
    /* check_allow_root_or_other allowed to get here, this is, we're
     * allowed to access the file. Default permissions checking is disabled,
     * thus, grant access
     */
    *types |= O_READ | O_WRITE | O_EXEC;

 out:
  FUNC_EPILOGUE(0);
}


/* Lookup NAME in DIR for USER; set *NODE to the found name upon return.  If
   the name was not found, then return ENOENT.  On any error, clear *NODE.
   (*NODE, if found, should be locked, this call should unlock DIR no matter
   what.) */
error_t netfs_attempt_lookup (struct iouser *user, struct node *dir,
			      char *name, struct node **node)
{
  FUNC_PROLOGUE_FMT("netfs_attempt_lookup", "name=%s, dir=%s",
		    name, dir->nn->path);

  error_t err;

  if((err = netfs_validate_stat(dir, user))
     || (libfuse_params.deflt_perms
	 && ((err = fshelp_access(&dir->nn_stat, S_IREAD, user))
	     || (err = fshelp_access(&dir->nn_stat, S_IEXEC, user)))))
    goto out;
  else
    err = ENOENT; /* default to return ENOENT */

  if(! strcmp(name, "."))
    {
      /* lookup for current directory, return another refernce to it */
      netfs_nref(dir);
      *node = dir;
      err = 0; /* it's alright ... */
    }

  else if(! strcmp(name, ".."))
    {
      if(dir->nn->parent)
	{
	  /* okay, there is a parent directory, return a reference to that */
	  *node = fuse_make_node(dir->nn->parent);
	  err = 0;
	}
      else
	/* cannot go up from top directory */
	err = EAGAIN;
    }

  else if(FUSE_OP_HAVE(getattr))
    {
      /* lookup for common file */
      struct netnode *nn;
      char *path;
      struct stat stbuf;

      if(asprintf(&path, "%s/%s",
		  dir->nn->parent ? dir->nn->path : "", name) < 0)
	{
	  err = ENOMEM;
	  goto out;
	}

      if(! (err = -FUSE_OP_CALL(getattr, path, &stbuf)))
	if(! (nn = fuse_make_netnode(dir->nn, path)) ||
	   ! (*node = fuse_make_node(nn)))
	  err = ENOMEM;

      free(path); /* fuse_make_netnode strdup()s the pathname */
    }

out:
  pthread_mutex_unlock(&dir->lock);

  if(err)
    *node = NULL;
  else
    pthread_mutex_lock(&(*node)->lock);

  FUNC_EPILOGUE(err);
}



/* Create a link in DIR with name NAME to FILE for USER.  Note that neither
   DIR nor FILE are locked.  If EXCL is set, do not delete the target, but
   return EEXIST if NAME is already found in DIR.  */
error_t netfs_attempt_link (struct iouser *user, struct node *dir,
			    struct node *file, char *name, int excl)
{
  FUNC_PROLOGUE_FMT("netfs_attempt_link", "link=%s/%s, to=%s",
		    dir->nn->path, name, file->nn->path);
  error_t err;
  struct node *node = NULL;

  pthread_mutex_lock(&dir->lock);

  if((err = netfs_attempt_lookup(user, dir, name, &node)))
    goto out_nounlock; /* netfs_attempt_lookup unlocked dir */

  assert(dir != node);
  pthread_mutex_lock(&dir->lock);

  if((err = netfs_validate_stat(node, user))
     || (libfuse_params.deflt_perms
	 && (err = fshelp_checkdirmod(&dir->nn_stat, &node->nn_stat, user))))
    goto out;

  if(! FUSE_OP_HAVE(link)) {
    err = EOPNOTSUPP;
    goto out;
  }

  if(! excl && FUSE_OP_HAVE(unlink))
    /* EXCL is not set, therefore we may remove the target, i.e. call
     * unlink on it.  Ignoring return value, as it's mostly not interesting,
     * since the file does not exist in most case
     */
    (void) FUSE_OP_CALL(unlink, node->nn->path);

  err = -FUSE_OP_CALL(link, file->nn->path, node->nn->path);

  /* TODO
   * create a netnode with the may_need_sync flag set!!   */

 out:
  pthread_mutex_unlock(&dir->lock);

  pthread_mutex_unlock(&node->lock);
  netfs_nrele(node);

 out_nounlock:
  FUNC_EPILOGUE(err);
}



/* Attempt to remove directory named NAME in DIR for USER.
 * directory DIR is locked, and shall stay locked. */
error_t netfs_attempt_rmdir (struct iouser *user,
			     struct node *dir, char *name)
{
  FUNC_PROLOGUE("netfs_attempt_rmdir");
  error_t err;
  struct node *node = NULL;

  err = netfs_attempt_lookup(user, dir, name, &node);
  assert(dir != node);
  pthread_mutex_lock(&dir->lock); /* netfs_attempt_lookup unlocked dir */

  if(err)
    goto out_nounlock; 

  if((err = netfs_validate_stat(node, user))
     || (libfuse_params.deflt_perms
	 && (err = fshelp_checkdirmod(&dir->nn_stat, &node->nn_stat, user))))
    goto out;

  if(FUSE_OP_HAVE(rmdir))
    err = -FUSE_OP_CALL(rmdir, node->nn->path);
  else
    err = EOPNOTSUPP;


  /* TODO free associated netnode. really? 
   * FIXME, make sure nn->may_need_sync is set */

 out:
  pthread_mutex_unlock(&node->lock);
  netfs_nrele(node);

 out_nounlock:
  FUNC_EPILOGUE(err);
}



/* This should attempt a chauthor call for the user specified by CRED on node
   NODE, to change the author to AUTHOR. */
error_t netfs_attempt_chauthor (struct iouser *cred, struct node *node,
				uid_t author)
{
  (void) cred;
  (void) author;

  FUNC_PROLOGUE_NODE("netfs_attempt_chauthor", node);
  NOT_IMPLEMENTED();
  FUNC_EPILOGUE(EROFS);
}



/* Attempt to turn NODE (user CRED) into a symlink with target NAME. */
error_t netfs_attempt_mksymlink (struct iouser *cred, struct node *node,
				 char *name)
{
  FUNC_PROLOGUE_NODE("netfs_attempt_mksymlink", node);
  error_t err;

  /* check permissions
   * XXX, shall we check permissions of the parent directory as well,
   * since we're going to unlink files?
   */
  if((err = netfs_validate_stat(node, cred))
     || (libfuse_params.deflt_perms
	 && (err = fshelp_access(&node->nn_stat, S_IWRITE, cred))))
    goto out;

  /* we need to unlink the existing node, therefore, if unlink is not
   * available, we cannot create symlinks
   */
  if(! FUSE_OP_HAVE(unlink))
    { 
      err = EOPNOTSUPP;
      goto out;
    }

  /* symlink function available? if not, fail. */
  if(! FUSE_OP_HAVE(symlink))
    {
      err = EOPNOTSUPP;
      goto out;
    }

  /* try to remove the existing node (probably an anonymous file) */
  if((err = -FUSE_OP_CALL(unlink, node->nn->path)))
    goto out;

  err = -FUSE_OP_CALL(symlink, name, node->nn->path);

  /* we don't have to adjust nodes/netnodes, as these are already existing.
   * netfs_attempt_mkfile did that for us.
   */
  if(! err)
    node->nn->may_need_sync = 1;

 out:
  FUNC_EPILOGUE(err);
}



/* Note that in this one call, neither of the specific nodes are locked. */
error_t netfs_attempt_rename (struct iouser *user, struct node *fromdir,
			      char *fromname, struct node *todir,
			      char *toname, int excl)
{
  FUNC_PROLOGUE("netfs_attempt_rename");
  error_t err;
  struct node *fromnode;
  char *topath = NULL;

  if(! (topath = malloc(strlen(toname) + strlen(todir->nn->path) + 2)))
    {
      err = ENOMEM;
      goto out_nounlock;
    }

  pthread_mutex_lock(&fromdir->lock);

  if(netfs_attempt_lookup(user, fromdir, fromname, &fromnode))
    {
      err = ENOENT;
      goto out_nounlock; /* netfs_attempt_lookup unlocked fromdir and locked
			  * fromnode for us. 
			  */
    }

  pthread_mutex_lock(&todir->lock);

  if((err = netfs_validate_stat(fromdir, user))
     || (libfuse_params.deflt_perms
	 && (err = fshelp_checkdirmod(&fromdir->nn_stat,
				      &fromnode->nn_stat, user))))
    goto out;

  if((err = netfs_validate_stat(todir, user))
     || (libfuse_params.deflt_perms
	 && (err = fshelp_checkdirmod(&todir->nn_stat, NULL, user))))
    goto out;

  if(! FUSE_OP_HAVE(rename))
    {
      err = EOPNOTSUPP;
      goto out; 
    }

  sprintf(topath, "%s/%s", todir->nn->path, toname);

  if(! excl && FUSE_OP_HAVE(unlink))
    /* EXCL is not set, therefore we may remove the target, i.e. call
     * unlink on it.  Ignoring return value, as it's mostly not interesting,
     * since the file does not exist in most cases
     */
    (void) FUSE_OP_CALL(unlink, topath);

  err = -FUSE_OP_CALL(rename, fromnode->nn->path, topath);

  if(! err) 
    {
      struct netnode *nn;

      if(! (nn = fuse_make_netnode(todir->nn, topath)))
	goto out;

      nn->may_need_sync = 1;
      /* FIXME mark fromnode to destroy it's associated netnode */
    }

 out:
  free(topath);
  pthread_mutex_unlock(&todir->lock);

  pthread_mutex_unlock(&fromnode->lock);
  netfs_nrele(fromnode);

 out_nounlock:
  FUNC_EPILOGUE(err);
}



/* Write to the file NODE for user CRED starting at OFSET and continuing for up
   to *LEN bytes from DATA.  Set *LEN to the amount seccessfully written upon
   return. */
error_t netfs_attempt_write (struct iouser *cred, struct node *node,
			     loff_t offset, size_t *len, void *data)
{
  FUNC_PROLOGUE_NODE("netfs_attempt_write", node);
  error_t err;

  if((err = netfs_validate_stat(node, cred))
     || (libfuse_params.deflt_perms
	 && (err = fshelp_access(&node->nn_stat, S_IWRITE, cred))))
    goto out;

  if(! FUSE_OP_HAVE(write)) 
    {
      err = EOPNOTSUPP;
      goto out;
    }

  NN_INFO_APPLY(node, writepage = 0); /* cannot distinct on the Hurd :( */

  if(FUSE_OP_HAVE(open))
    {
      err = -FUSE_OP_CALL(open, node->nn->path, NN_INFO(node));

      if(err) goto out;
    }

  int sz = FUSE_OP_CALL(write, node->nn->path, data, *len, offset,
			NN_INFO(node));

  /* FIXME: open, flush and release handling probably should be changed
   * completely, I mean, we probably should do fuse_ops->open in 
   * the netfs_check_open_permissions function and leave it open
   * until the node is destroyed.
   *
   * This way we wouldn't be able to report any errors back.
   */
  if(sz >= 0 && FUSE_OP_HAVE(flush))
    err = -FUSE_OP_CALL(flush, node->nn->path, NN_INFO(node));

  if(FUSE_OP_HAVE(open) && FUSE_OP_HAVE(release))
    FUSE_OP_CALL(release, node->nn->path, NN_INFO(node));
  
  if(sz < 0)
    err = -sz;
  else
    *len = sz;

 out:
  FUNC_EPILOGUE(err);
}



/* This should attempt a utimes call for the user specified by CRED on node
   NODE, to change the atime to ATIME and the mtime to MTIME. */
error_t
netfs_attempt_utimes (struct iouser *cred, struct node *node,
		      struct timespec *atime, struct timespec *mtime)
{
  FUNC_PROLOGUE_NODE("netfs_attempt_utimes", node);
  error_t err;

  /* test whether operation is supported and permission are sufficient */
  if((err = netfs_validate_stat(node, cred))
     || (libfuse_params.deflt_perms
	 && (err = fshelp_isowner(&node->nn_stat, cred))))
    goto out;

  if(FUSE_OP_HAVE(utimens))
    {
      /* Prepare TS for FUSE_OP_HAVE(utimens) call.  */
      struct timespec ts[2];
      ts[0] = atime ? *atime : node->nn_stat.st_atim;
      ts[1] = mtime ? *mtime : node->nn_stat.st_mtim;

      err = -FUSE_OP_CALL(utimens, node->nn->path, ts);

      if (!err)
	{
	  node->nn_stat.st_mtim = ts[1];

	  node->nn_stat.st_atim = ts[0];

	  node->nn->may_need_sync = 1;
	}
    }
  else if(FUSE_OP_HAVE(utime))
    {
      /* Prepare UTB for FUSE_OP_HAVE(utime) call.  */
      struct utimbuf utb;
      utb.actime = atime ? atime->tv_sec : node->nn_stat.st_atime;
      utb.modtime = mtime ? mtime->tv_sec : node->nn_stat.st_mtime;

      err = -FUSE_OP_CALL(utime, node->nn->path, &utb);

      if (!err)
	{
	  node->nn_stat.st_mtim.tv_sec = utb.modtime;
	  node->nn_stat.st_mtim.tv_nsec = 0;

	  node->nn_stat.st_atim.tv_sec = utb.actime;
	  node->nn_stat.st_atim.tv_nsec = 0;

	  node->nn->may_need_sync = 1;
	}
    }
  else
    err = EOPNOTSUPP;

 out:
  FUNC_EPILOGUE(err);
}



/* Read from the file NODE for user CRED starting at OFFSET and continuing for
   up to *LEN bytes.  Put the data at DATA.  Set *LEN to the amount
   successfully read upon return.  */
error_t netfs_attempt_read (struct iouser *cred, struct node *node,
			    loff_t offset, size_t *len, void *data)
{
  FUNC_PROLOGUE_NODE("netfs_attempt_read", node);
  error_t err;

  if((err = netfs_validate_stat(node, cred))
     || (libfuse_params.deflt_perms
	 && (err = fshelp_access(&node->nn_stat, S_IREAD, cred))))
    goto out;

  if(! FUSE_OP_HAVE(read))
    {
      err = EOPNOTSUPP;
      goto out;
    }

  if(FUSE_OP_HAVE(open))
    {
      err = -FUSE_OP_CALL(open, node->nn->path, NN_INFO(node));

      if(err) goto out;
    }

  int sz = FUSE_OP_CALL(read, node->nn->path, data, *len, offset,
			NN_INFO(node));

  /* FIXME: open, flush and release handling probably should be changed
   * completely, I mean, we probably should do fuse_ops->open in 
   * the netfs_check_open_permissions function and leave it open
   * until the node is destroyed.
   *
   * This way we wouldn't be able to report any errors back.
   */
  if(sz >= 0 && FUSE_OP_HAVE(flush))
    err = -FUSE_OP_CALL(flush, node->nn->path, NN_INFO(node));

  if(FUSE_OP_HAVE(open) && FUSE_OP_HAVE(release))
    FUSE_OP_CALL(release, node->nn->path, NN_INFO(node));

  if(sz < 0)
    err = -sz;
  else
    {
      err = 0;
      *len = sz;
    }

 out:
  FUNC_EPILOGUE(err);
}



/* Returned directory entries are aligned to blocks this many bytes long.
   Must be a power of two.  */
#define DIRENT_ALIGN 4
#define DIRENT_NAME_OFFS offsetof (struct dirent, d_name)

/* Length is structure before the name + the name + '\0', all
   padded to a four-byte alignment.  */
#define DIRENT_LEN(name_len)						      \
  ((DIRENT_NAME_OFFS + (name_len) + 1 + (DIRENT_ALIGN - 1))		      \
   & ~(DIRENT_ALIGN - 1))

/* fuse_get_inode
 *
 * look up the inode of the named file (full path) in fuse_use_ino mode,
 * where we don't have the inode number of parent directories in our structs
 */
static ino_t 
fuse_get_inode(const char *name)
{
  struct stat stat;
  
  assert(FUSE_OP_HAVE(getattr));
  FUSE_OP_CALL(getattr, name, &stat);

  return stat.st_ino;
}

/* callback handler used by netfs_get_dirents to write our dirents
 * to the mmaped memory (getdir case)
 */
static int
get_dirents_getdir_helper(fuse_dirh_t handle, const char *name,
			  int type, ino_t ino)
{
  ino_t inode;

  if(handle->first_entry)
    {
      /* skip this entry, it's before the first one we got to write out ... */
      handle->first_entry --;
      return 0;
    }

  if(! (handle->num_entries --))
    return ENOMEM;

  size_t name_len = strlen(name);
  size_t dirent_len = DIRENT_LEN(name_len);

  if(dirent_len > handle->size)
    {
      handle->num_entries = 0; /* don't write any further data, e.g. in case */
      return ENOMEM;           /* next filename's shorter */
    }
  else
    handle->size -= dirent_len;

  /* look the inode of this element up ... */
  if(libfuse_params.use_ino)
    {
      /* using the inode based api */
      if(! strcmp(name, "."))
	inode = fuse_get_inode(handle->parent->path);

      else if(handle->parent->parent && ! strcmp(name, ".."))
	inode = fuse_get_inode(handle->parent->parent->path);

      else
	inode = ino;
    }
  else
    {
      /* using the old (lookup) method ... */

      if(! strcmp(name, "."))
	inode = handle->parent->inode;

      else if(handle->parent->parent && ! strcmp(name, ".."))
	inode = handle->parent->parent->inode;

      else
	{
	  if(name_len > handle->maxlen)
	    {
	      /* allocated field in handle structure is to small, enlarge it */
	      handle->maxlen = name_len << 1;
	      void *a = realloc(handle->abspath, handle->maxlen +
				(handle->filename - handle->abspath) + 1);
	      if(! a)
		return ENOMEM;

	      handle->filename = a + (handle->filename - handle->abspath);
	      handle->abspath = a;
	    }

	  strcpy(handle->filename, name);
	  struct netnode *nn = fuse_make_netnode(handle->parent,
						 handle->abspath);

	  if(! nn)
	    return ENOMEM;

	  inode = nn->inode;
	}
    }

  /* write out struct dirent ... */
  handle->hdrpos->d_fileno = inode;
  handle->hdrpos->d_reclen = dirent_len;
  handle->hdrpos->d_type = type;
  handle->hdrpos->d_namlen = name_len;

  /* copy file's name ... */
  memcpy(((void *) handle->hdrpos) + DIRENT_NAME_OFFS, name, name_len + 1);

  /* update hdrpos pointer */
  handle->hdrpos = ((void *) handle->hdrpos) + dirent_len;

  handle->count ++;
  return 0;
}


static error_t
get_dirents_getdir(struct node *dir, int first_entry, int num_entries, 
		   char **data, mach_msg_type_number_t *data_len,
		   int *data_entries)
{
  FUNC_PROLOGUE_NODE("get_dirents_getdir", dir);

  if(! FUSE_OP_HAVE(getdir))
    FUNC_RETURN(EOPNOTSUPP);

  fuse_dirh_t handle;
  if(! (handle = malloc(sizeof(struct fuse_dirhandle))))
    FUNC_RETURN(ENOMEM);
  
  handle->first_entry = first_entry;
  handle->num_entries = num_entries;
  handle->count = 0;
  handle->size = *data_len;

  /* allocate handle->abspath */
  size_t path_len = strlen(dir->nn->path);
  if(! (handle->abspath = malloc((handle->maxlen = 256) + path_len + 2)))
    {
      free(handle);
      FUNC_RETURN(ENOMEM);
    }

  memcpy(handle->abspath, dir->nn->path, path_len);
  handle->filename = handle->abspath + path_len;

  /* add a delimiting slash if there are parent directories */
  if(dir->nn->parent)
    *(handle->filename ++) = '/';

  handle->parent = dir->nn;
  handle->hdrpos = (struct dirent*) *data;

  FUSE_OP_CALL(getdir, dir->nn->path, handle, get_dirents_getdir_helper);

  *data_len -= handle->size; /* subtract number of bytes left in the
			      * buffer from the length of the buffer we
			      * got. */
  *data_entries = handle->count;

  free(handle->abspath);
  free(handle);

  FUNC_EPILOGUE(0);
}


/* callback handler used by netfs_get_dirents to write our dirents
 * to the mmaped memory (in readdir case)
 *
 * be careful, according to fuse.h `stat' may be NULL!
 */
static int
get_dirents_readdir_helper(void *buf, const char *name,
			   const struct stat *stat, off_t off)
{
  fuse_dirh_t handle = buf;
  ino_t inode;

  if(handle->first_entry && !off)
    {
      /* skip this entry, it's before the first one 
       * we got to write out ... */
      handle->first_entry --;
      return 0;
    }

  if(! (handle->num_entries --))
    return 1;

  size_t name_len = strlen(name);
  size_t dirent_len = DIRENT_LEN(name_len);

  if(dirent_len > handle->size)
    {
      handle->num_entries = 0; /* don't write any further data, e.g. in case */
      return ENOMEM;           /* next filename's shorter */
    }
  else
    handle->size -= dirent_len;

  /* look the inode of this element up ... */
  if(libfuse_params.use_ino)
    {
      /* using the inode based api */
      if(! strcmp(name, "."))
	inode = fuse_get_inode(handle->parent->path);

      else if(handle->parent->parent && ! strcmp(name, ".."))
	inode = fuse_get_inode(handle->parent->parent->path);

      if(! stat)
	{
	  DEBUG("critical", "use_ino flag set, but stat ptr not available.\n");
	  inode = 0;
	}

      else
	inode = stat->st_ino;
    }
  else
    {
      /* using the old (lookup) method ... */

      if(! strcmp(name, "."))
	inode = handle->parent->inode;

      else if(handle->parent->parent && ! strcmp(name, ".."))
	inode = handle->parent->parent->inode;

      else
	{
	  if(name_len > handle->maxlen)
	    {
	      /* allocated field in handle structure is to small, enlarge it */
	      handle->maxlen = name_len << 1;
	      void *a = realloc(handle->abspath, handle->maxlen +
				(handle->filename - handle->abspath) + 1);
	      if(! a)
		return ENOMEM;

	      handle->filename = a + (handle->filename - handle->abspath);
	      handle->abspath = a;
	    }

	  strcpy(handle->filename, name);
	  struct netnode *nn = fuse_make_netnode(handle->parent,
						 handle->abspath);

	  if(! nn) 
	    return ENOMEM;

	  inode = nn->inode;
	}
    }

  /* write out struct dirent ... */
  handle->hdrpos->d_fileno = inode;
  handle->hdrpos->d_reclen = dirent_len;
  handle->hdrpos->d_type = stat ? (stat->st_mode >> 12) : 0;
  handle->hdrpos->d_namlen = name_len;

  /* copy file's name ... */
  memcpy(((void *) handle->hdrpos) + DIRENT_NAME_OFFS, name, name_len + 1);

  /* update hdrpos pointer */
  handle->hdrpos = ((void *) handle->hdrpos) + dirent_len;

  handle->count ++;
  return 0;
}


static error_t
get_dirents_readdir(struct node *dir, int first_entry, int num_entries, 
		    char **data, mach_msg_type_number_t *data_len,
		    int *data_entries)
{
  error_t err;
  FUNC_PROLOGUE_NODE("get_dirents_readdir", dir);

  assert(FUSE_OP_HAVE(readdir));

  fuse_dirh_t handle;
  if(! (handle = malloc(sizeof(struct fuse_dirhandle))))
    FUNC_RETURN(ENOMEM);
  
  handle->first_entry = first_entry;
  handle->num_entries = num_entries;
  handle->count = 0;
  handle->size = *data_len;

  /* allocate handle->abspath */
  size_t path_len = strlen(dir->nn->path);
  if(! (handle->abspath = malloc((handle->maxlen = 256) + path_len + 2)))
    {
      err = ENOMEM;
      goto out;
    }

  memcpy(handle->abspath, dir->nn->path, path_len);
  handle->filename = handle->abspath + path_len;

  /* add a delimiting slash if there are parent directories */
  if(dir->nn->parent)
    *(handle->filename ++) = '/';

  handle->parent = dir->nn;
  handle->hdrpos = (struct dirent*) *data;

  if(FUSE_OP_HAVE(opendir)
     && (err = -FUSE_OP_CALL(opendir, dir->nn->path, NN_INFO(dir))))
    goto out;

  if((err = -FUSE_OP_CALL(readdir, dir->nn->path, handle,
			  get_dirents_readdir_helper,
			  first_entry, NN_INFO(dir))))
    {
      if(FUSE_OP_HAVE(releasedir))
	FUSE_OP_CALL(releasedir, dir->nn->path, NN_INFO(dir));
      goto out;
    }

  if(FUSE_OP_HAVE(releasedir)
     && (err = -FUSE_OP_CALL(releasedir, dir->nn->path, NN_INFO(dir))))
    goto out;

  *data_len -= handle->size; /* subtract number of bytes left in the
			      * buffer from the length of the buffer we
			      * got. */
  *data_entries = handle->count;

 out:
  free(handle->abspath);
  free(handle);

  FUNC_EPILOGUE(err);
}

error_t
netfs_get_dirents (struct iouser *cred, struct node *dir,
		   int first_entry, int num_entries, char **data,
		   mach_msg_type_number_t *data_len,
		   vm_size_t max_data_len, int *data_entries)
{
  (void) max_data_len; /* we live with the supplied mmap area in any case,
			* i.e. never allocate any further memory */

  FUNC_PROLOGUE_NODE("netfs_get_dirents", dir);
  error_t err;

  if((err = netfs_validate_stat(dir, cred))
     || (libfuse_params.deflt_perms
	 && ((err = fshelp_access(&dir->nn_stat, S_IREAD, cred))
	     || (err = fshelp_access(&dir->nn_stat, S_IEXEC, cred)))))
    goto out;


  if(FUSE_OP_HAVE(readdir))
    err = get_dirents_readdir(dir, first_entry, num_entries, data, data_len,
			      data_entries);

  else if(FUSE_OP_HAVE(getdir))
    err = get_dirents_getdir(dir, first_entry, num_entries, data, data_len,
			     data_entries);

  else
    err = EOPNOTSUPP;

  /* TODO: fshelp_touch ATIME here */

 out:
  FUNC_EPILOGUE_FMT(err, "%d entries.", *data_entries);
}



/* Node NP is all done; free all its associated storage. */
void
netfs_node_norefs (struct node *node)
{
  FUNC_PROLOGUE_NODE("netfs_node_norefs", node);
  assert(node);

  DEBUG("netnode-lock", "locking netnode, path=%s\n", node->nn->path);
  pthread_mutex_lock(&node->nn->lock);

  if(node->nn->anonymous && FUSE_OP_HAVE(unlink))
    {
      /* FIXME, need to lock parent directory structure */
      FUSE_OP_CALL(unlink, node->nn->path);

      /* FIXME, free associated netnode somehow. */
    }

  node->nn->node = NULL;

  pthread_mutex_unlock(&node->nn->lock);
  DEBUG("netnode-lock", "netnode unlocked.\n"); /* no ref to node->nn av. */

  FUNC_EPILOGUE_NORET();
}