summaryrefslogtreecommitdiff
path: root/nfs/ops.c
blob: 4520e51651fae70d13ac2ddfd0638385a64daddb (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
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
/* Libnetfs callbacks for node operations in NFS client
   Copyright (C) 1994, 1995, 1996, 1997 Free Software Foundation

   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; either version 2, or (at
   your option) any later version.

   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; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

#include "nfs.h"
#include <hurd/netfs.h>
#include <netinet/in.h>
#include <string.h>
#include <fcntl.h>
#include <stdio.h>
#include <dirent.h>
#include <unistd.h>

/* We have fresh stat information for NP; the fattr structure is at 
   P.  Update our entry.  Return the address of the next int after
   the fattr structure.  */
int *
register_fresh_stat (struct node *np, int *p)
{
  int *ret;
  
  ret = xdr_decode_fattr (p, &np->nn_stat);
  np->nn->stat_updated = mapped_time->seconds;

  switch (np->nn->dtrans)
    {
    case NOT_POSSIBLE:
    case POSSIBLE:
      break;
    
    case SYMLINK:
      np->nn_stat.st_size = strlen (np->nn->transarg.name);
      np->nn_stat.st_mode = ((np->nn_stat.st_mode & ~S_IFMT) | S_IFLNK);
      break;
      
    case CHRDEV:
      np->nn_stat.st_rdev = np->nn->transarg.indexes;
      np->nn_stat.st_mode = ((np->nn_stat.st_mode & ~S_IFMT) | S_IFCHR);
      break;

    case BLKDEV:
      np->nn_stat.st_rdev = np->nn->transarg.indexes;
      np->nn_stat.st_mode = ((np->nn_stat.st_mode & ~S_IFMT) | S_IFBLK);
      break;

    case FIFO:
      np->nn_stat.st_mode = ((np->nn_stat.st_mode & ~S_IFMT) | S_IFIFO);
      break;

    case SOCK:
      np->nn_stat.st_mode = ((np->nn_stat.st_mode & ~S_IFMT) | S_IFSOCK);
      break;
    }

  np->nn_stat.st_fsid = getpid ();
  np->nn_stat.st_fstype = FSTYPE_NFS;
  np->nn_stat.st_gen = 0;
  np->nn_stat.st_author = np->nn_stat.st_uid;
  np->nn_stat.st_flags = 0;
       
  return ret;
}

/* Handle returned wcc information for various calls.  In protocol
   version 2, this is just register_fresh_stat.  In version 3, it
   checks to see if stat information is present too.  If this follows
   an operation that we expect has modified the attributes, MOD should
   be set.  (This unpacks the post_op_attr XDR type.)  */
int *
process_returned_stat (struct node *np, int *p, int mod)
{
  int attrs_exist;

  if (protocol_version == 2)
    return register_fresh_stat (np, p);
  else
    {
      attrs_exist = ntohl (*p++);
      if (attrs_exist)
	p = register_fresh_stat (np, p);
      else if (mod)
	/* We know that our values are now wrong */
	np->nn->stat_updated = 0;
      return p;
    }
}


/* Handle returned wcc information for various calls.  In protocol
   version 2, this is just register_fresh_stat.  In version 3, it does
   the wcc_data interpretation too.  If this follows an operation that
   we expect has modified the attributes, MOD should be set. 
   (This unpacks the wcc_data XDR type.) */
int *
process_wcc_stat (struct node *np, int *p, int mod)
{
  if (protocol_version == 2)
    return register_fresh_stat (np, p);
  else
    {
      int attrs_exist;

      /* First the pre_op_attr */
      attrs_exist = ntohl (*p++);
      if (attrs_exist)
	{
	  /* Just skip them for now */
	  p += 2 * sizeof (int); /* size */
	  p += 2 * sizeof (int); /* mtime */
	  p += 2 * sizeof (int); /* atime */
	}
      
      /* Now the post_op_attr */
      return process_returned_stat (np, p, mod);
    }
}


/* Implement the netfs_validate_stat callback as described in
   <hurd/netfs.h>. */
error_t
netfs_validate_stat (struct node *np, struct iouser *cred)
{
  int *p;
  void *rpcbuf;
  error_t err;
  
  if (mapped_time->seconds - np->nn->stat_updated < stat_timeout)
    return 0;

  p = nfs_initialize_rpc (NFSPROC_GETATTR (protocol_version),
			  (struct iouser *) -1, 0, &rpcbuf, np, -1);
  p = xdr_encode_fhandle (p, &np->nn->handle);
  
  err = conduct_rpc (&rpcbuf, &p);
  if (!err)
    err = nfs_error_trans (ntohl (*p++));
  if (!err)
    register_fresh_stat (np, p);

  free (rpcbuf);
  return err;
}

/* Implement the netfs_attempt_chown callback as described in
   <hurd/netfs.h>. */
error_t
netfs_attempt_chown (struct iouser *cred, struct node *np,
		     uid_t uid, gid_t gid)
{
  int *p;
  void *rpcbuf;
  error_t err;
    
  p = nfs_initialize_rpc (NFSPROC_SETATTR (protocol_version),
			  cred, 0, &rpcbuf, np, gid);
  p = xdr_encode_fhandle (p, &np->nn->handle);
  p = xdr_encode_sattr_ids (p, uid, gid);
  if (protocol_version == 3)
    *p++ = 0;			/* guard_check == 0 */
  
  err = conduct_rpc (&rpcbuf, &p);
  if (!err)
    {
      err = nfs_error_trans (ntohl (*p++));
      if (!err || protocol_version == 3)
	p = process_wcc_stat (np, p, !err);
    }

  free (rpcbuf);
  
  return err;
}

/* Implement the netfs_attempt_chauthor callback as described in
   <hurd/netfs.h>.  */
error_t
netfs_attempt_chauthor (struct iouser *cred, struct node *rp,
			uid_t author)
{
  return EOPNOTSUPP;
}

/* Implement the netfs_attempt_chmod callback as described in
   <hurd/netfs.h>. */
error_t
netfs_attempt_chmod (struct iouser *cred, struct node *np,
		     mode_t mode)
{
  int *p;
  void *rpcbuf;
  error_t err;

  if ((mode & S_IFMT) != 0)
    {
      err = netfs_validate_stat (np, cred);
      if (err)
	return err;
      if ((mode & S_IFMT) != (np->nn_stat.st_mode & S_IFMT))
	{
	  char *f = 0;
	  
	  if (np->nn->dtrans == NOT_POSSIBLE)
	    return EOPNOTSUPP;
	  
	  if (np->nn->dtrans == SYMLINK)
	    f = np->nn->transarg.name;
	  
	  switch (mode & S_IFMT)
	    {
	    default:
	      return EOPNOTSUPP;
	    
	    case S_IFIFO:
	      np->nn->dtrans = FIFO;
	      np->nn->stat_updated = 0;
	      break;
	      
	    case S_IFSOCK:
	      np->nn->dtrans = SOCK;
	      np->nn->stat_updated = 0;
	    }
	  if (f)
	    free (f);
	  return 0;
	}
    }

  p = nfs_initialize_rpc (NFSPROC_SETATTR (protocol_version),
			  cred, 0, &rpcbuf, np, -1);
  p = xdr_encode_fhandle (p, &np->nn->handle);
  p = xdr_encode_sattr_mode (p, mode);
  if (protocol_version == 3)
    *p++ = 0;			/* guard check == 0 */
  
  err = conduct_rpc (&rpcbuf, &p);
  if (!err)
    {
      err = nfs_error_trans (ntohl (*p++));
      if (!err || protocol_version == 3)
	p = process_wcc_stat (np, p, !err);
    }
  
  free (rpcbuf);
  return err;
}

/* Implement the netfs_attempt_chflags callback as described in
   <hurd/netfs.h>. */
error_t 
netfs_attempt_chflags (struct iouser *cred, struct node *np,
		       int flags)
{
  return EOPNOTSUPP;
}

/* Implement the netfs_attempt_utimes callback as described in
   <hurd/netfs.h>.  */
error_t
netfs_attempt_utimes (struct iouser *cred, struct node *np,
		      struct timespec *atime, struct timespec *mtime)
{
  int *p;
  void *rpcbuf;
  error_t err;
    
  p = nfs_initialize_rpc (NFSPROC_SETATTR (protocol_version),
			  cred, 0, &rpcbuf, np, -1);
  p = xdr_encode_fhandle (p, &np->nn->handle);
  p = xdr_encode_sattr_times (p, atime, mtime);
  if (protocol_version == 3)
    *p++ = 0;			/* guard check == 0 */
  
  err = conduct_rpc (&rpcbuf, &p);
  if (!err)
    {
      err = nfs_error_trans (ntohl (*p++));
      if (!err || protocol_version == 3)
	p = process_wcc_stat (np, p, !err);
    }

  free (rpcbuf);
  return err;
}

/* Implement the netfs_attempt_set_size callback as described in
   <hurd/netfs.h>. */
error_t
netfs_attempt_set_size (struct iouser *cred, struct node *np,
			off_t size)
{
  int *p;
  void *rpcbuf;
  error_t err;
  
  p = nfs_initialize_rpc (NFSPROC_SETATTR (protocol_version),
			  cred, 0, &rpcbuf, np, -1);
  p = xdr_encode_fhandle (p, &np->nn->handle);
  p = xdr_encode_sattr_size (p, size);
  if (protocol_version == 3)
    *p++ = 0;			/* guard_check == 0 */
  
  err = conduct_rpc (&rpcbuf, &p);
  if (!err)
    {
      err = nfs_error_trans (ntohl (*p++));
      if (!err || protocol_version == 3)
	p = process_wcc_stat (np, p, !err);
    }
  
  /* If we got EACCES, but the user has the file open for writing,
     then the NFS protocol has screwed us.  There's nothing we can do,
     except in the important case of opens with
     O_TRUNC|O_CREAT|O_WRONLY|O_EXCL where the new mode does not allow
     writing.  RCS, for example, uses this to create lock files.  So permit
     cases where the O_TRUNC isn't doing anything to succeed if the user
     does have the file open for writing.  */
  if (err == EACCES)
    {
      err = netfs_validate_stat (np, cred);
      if (!err && np->nn_stat.st_size == size)
	err = 0;
      else
	/* Never mind, put the old error back */
	err = EACCES;
    }

  free (rpcbuf);
  return err;
}

/* Implement the netfs_attempt_statfs callback as described in
   <hurd/netfs.h>. */
error_t
netfs_attempt_statfs (struct iouser *cred, struct node *np,
		      struct statfs *st)
{
  int *p;
  void *rpcbuf;
  error_t err;
    
  p = nfs_initialize_rpc (NFS2PROC_STATFS, cred, 0, &rpcbuf, np, -1);
  p = xdr_encode_fhandle (p, &np->nn->handle);
  
  err = conduct_rpc (&rpcbuf, &p);
  if (!err)
    err = nfs_error_trans (ntohl (*p++));
  
  if (!err)
    {
      p++;			/* skip IOSIZE field */
      st->f_bsize = ntohl (*p++);
      st->f_blocks = ntohl (*p++);
      st->f_bfree = ntohl (*p++);
      st->f_bavail = ntohl (*p++);
      st->f_type = FSTYPE_NFS;
      st->f_files = 0;
      st->f_ffree = 0;
      st->f_fsid = getpid ();
      st->f_namelen = 0;
    }
  
  free (rpcbuf);
  return err;
}

/* Implement the netfs_attempt_sync callback as described in
   <hurd/netfs.h>. */
error_t
netfs_attempt_sync (struct iouser *cred, struct node *np, int wait)
{
  /* We are already completely synchronous. */
  return 0;
}

/* Implement the netfs_attempt_syncfs callback as described in
   <hurd/netfs.h>.  */
error_t
netfs_attempt_syncfs (struct iouser *cred, int wait)
{
  return 0;
}

/* Implement the netfs_attempt_read callback as described in
   <hurd/netfs.h>. */
error_t
netfs_attempt_read (struct iouser *cred, struct node *np,
		    off_t offset, size_t *len, void *data)
{
  int *p;
  void *rpcbuf;
  size_t trans_len;
  error_t err;
  size_t amt, thisamt;
  int eof;
  
  for (amt = *len; amt;)
    {
      thisamt = amt;
      if (thisamt > read_size)
	thisamt = read_size;

      p = nfs_initialize_rpc (NFSPROC_READ (protocol_version),
			      cred, 0, &rpcbuf, np, -1);
      p = xdr_encode_fhandle (p, &np->nn->handle);
      *p++ = htonl (offset);
      *p++ = htonl (thisamt);
      if (protocol_version == 2)
	*p++ = 0;
  
      err = conduct_rpc (&rpcbuf, &p);
      if (!err)
	{
	  err = nfs_error_trans (ntohl (*p++));

	  if (!err || protocol_version == 3)
	    p = process_returned_stat (np, p, !err);

	  if (err)
	    {
	      free (rpcbuf);
	      return err;
	    }
      
	  trans_len = ntohl (*p++);
	  if (trans_len > thisamt)
	    trans_len = thisamt;	/* ??? */

	  if (protocol_version == 3)
	    eof = ntohl (*p++);
	  else
	    eof = (trans_len < thisamt);
	  
	  bcopy (p, data, trans_len);
	  free (rpcbuf);

	  data += trans_len;
	  offset += trans_len;
	  amt -= trans_len;
      
	  if (eof)
	    {
	      *len -= amt;
	      return 0;
	    }
	}
    }
  return 0;
}
	  
/* Implement the netfs_attempt_write callback as described in
   <hurd/netfs.h>. */
error_t
netfs_attempt_write (struct iouser *cred, struct node *np,
		     off_t offset, size_t *len, void *data)
{
  int *p;
  void *rpcbuf;
  error_t err;
  size_t amt, thisamt;
  size_t count;
  
  for (amt = *len; amt;)
    {
      thisamt = amt;
      if (thisamt > write_size)
	thisamt = write_size;
      
      p = nfs_initialize_rpc (NFSPROC_WRITE (protocol_version),
			      cred, thisamt, &rpcbuf, np, -1);
      p = xdr_encode_fhandle (p, &np->nn->handle);
      if (protocol_version == 2)
	*p++ = 0;
      *p++ = htonl (offset);
      if (protocol_version == 2)
	*p++ = 0;
      if (protocol_version == 3)
	*p++ = htonl (FILE_SYNC);
      p = xdr_encode_data (p, data, thisamt);
  
      err = conduct_rpc (&rpcbuf, &p);
      if (!err)
	{
	  err = nfs_error_trans (ntohl (*p++));
	  if (!err || protocol_version == 3)
	    p = process_wcc_stat (np, p, !err);
	  if (!err)
	    {
	      if (protocol_version == 3)
		{
		  count = ntohl (*p++);
		  p++;		/* ignore COMMITTED */
		  /* ignore verf for now */
		  p += NFS3_WRITEVERFSIZE / sizeof (int);		  
		}
	      else
		/* assume it wrote the whole thing */
		count = thisamt;
	      
	      free (rpcbuf);
	      amt -= count;
	      data += count;
	      offset += count;
	    }
	}
      
      if (err == EINTR && amt != *len)
	{
	  *len -= amt;
	  free (rpcbuf);
	  return 0;
	}
      
      if (err)
	{
	  *len = 0;
	  free (rpcbuf);
	  return err;
	}
    }
  return 0;
}

/* See if NAME exists in DIR for CRED.  If so, return EEXIST.  */
error_t
verify_nonexistent (struct iouser *cred, struct node *dir,
		    char *name)
{
  int *p;
  void *rpcbuf;
  error_t err;
  
  /* Don't use the lookup cache for this; we want a full sync to
     get as close to real exclusive create behavior as possible. */

  assert (protocol_version == 2);

  p = nfs_initialize_rpc (NFSPROC_LOOKUP (protocol_version),
			  cred, 0, &rpcbuf, dir, -1);
  p = xdr_encode_fhandle (p, &dir->nn->handle);
  p = xdr_encode_string (p, name);

  err = conduct_rpc (&rpcbuf, &p);
  if (!err)
    err = nfs_error_trans (ntohl (*p++));
  
  if (!err)
    return EEXIST;
  else
    return 0;
}

/* Implement the netfs_attempt_lookup callback as described in
   <hurd/netfs.h>. */
error_t
netfs_attempt_lookup (struct iouser *cred, struct node *np,
		      char *name, struct node **newnp)
{
  int *p;
  void *rpcbuf;
  error_t err;
  char dirhandle[NFS3_FHSIZE];
  size_t dirlen;
  
  /* Check the cache first. */
  *newnp = check_lookup_cache (np, name);
  if (*newnp)
    {
      if (*newnp == (struct node *) -1)
	{
	  *newnp = 0;
	  return ENOENT;
	}
      else
	return 0;
    }
  
  p = nfs_initialize_rpc (NFSPROC_LOOKUP (protocol_version),
			  cred, 0, &rpcbuf, np, -1);
  p = xdr_encode_fhandle (p, &np->nn->handle);
  p = xdr_encode_string (p, name);
  
  /* Remember the directory handle for later cache use. */

  dirlen = np->nn->handle.size;
  bcopy (np->nn->handle.data, dirhandle, dirlen);

  mutex_unlock (&np->lock);

  err = conduct_rpc (&rpcbuf, &p);
  if (!err)
    {
      err = nfs_error_trans (ntohl (*p++));
      if (!err)
	{
	  p = xdr_decode_fhandle (p, newnp);
	  p = process_returned_stat (*newnp, p, 1);
	}
      if (err)
	*newnp = 0;
      if (protocol_version == 3)
	{
	  if (*newnp)
	    mutex_unlock (&(*newnp)->lock);
	  mutex_lock (&np->lock);
	  p = process_returned_stat (np, p, 0);
	  mutex_unlock (&np->lock);
	  if (*newnp)
	    mutex_lock (&(*newnp)->lock);
	}
    }
  else
    *newnp = 0;
      
  /* Notify the cache of the hit or miss. */
  enter_lookup_cache (dirhandle, dirlen, *newnp, name);

  free (rpcbuf);
  
  return err;
}

/* Implement the netfs_attempt_mkdir callback as described in
   <hurd/netfs.h>. */
error_t
netfs_attempt_mkdir (struct iouser *cred, struct node *np,
		     char *name, mode_t mode)
{
  int *p;
  void *rpcbuf;
  error_t err;
  uid_t owner;
  struct node *newnp;

  if (cred->uids->num)
    owner = cred->uids->ids[0];
  else
    {
      err = netfs_validate_stat (np, cred);
      owner = err ? 0 : np->nn_stat.st_uid;
      mode &= ~S_ISUID;
    }

  purge_lookup_cache (np, name, strlen (name));

  p = nfs_initialize_rpc (NFSPROC_MKDIR (protocol_version),
			  cred, 0, &rpcbuf, np, -1);
  p = xdr_encode_fhandle (p, &np->nn->handle);
  p = xdr_encode_string (p, name);
  p = xdr_encode_create_state (p, mode, owner);
  
  err = conduct_rpc (&rpcbuf, &p);
  if (!err)
    err = nfs_error_trans (ntohl (*p++));
  
  p = xdr_decode_fhandle (p, &newnp);
  p = process_returned_stat (newnp, p, 1);

  /* Did we set the owner correctly?  If not, try, but ignore failures. */
  if (!netfs_validate_stat (newnp, -1) && newnp->nn_stat.st_uid != owner)
    netfs_attempt_chown (-1, newnp, owner, newnp->nn_stat.st_gid);

  /* We don't actually return this. */
  netfs_nput (newnp);
  
  free (rpcbuf);
  return err;
}

/* Implement the netfs_attempt_rmdir callback as described in
   <hurd/netfs.h>. */
error_t
netfs_attempt_rmdir (struct iouser *cred, struct node *np,
		     char *name)
{
  int *p;
  void *rpcbuf;
  error_t err;
  
  /* Should we do the same sort of thing here as with attempt_unlink? */

  purge_lookup_cache (np, name, strlen (name));

  p = nfs_initialize_rpc (NFSPROC_RMDIR (protocol_version),
			  cred, 0, &rpcbuf, np, -1);
  p = xdr_encode_fhandle (p, &np->nn->handle);
  p = xdr_encode_string (p, name);
  
  err = conduct_rpc (&rpcbuf, &p);
  if (!err)
    {
      err = nfs_error_trans (ntohl (*p++));
      if (protocol_version == 3)
	p = process_wcc_stat (np, p, !err);
    }
  
  free (rpcbuf);
  return err;
}

/* Implement the netfs_attempt_link callback as described in
   <hurd/netfs.h>. */
error_t
netfs_attempt_link (struct iouser *cred, struct node *dir,
		    struct node *np, char *name, int excl)
{
  int *p;
  void *rpcbuf;
  error_t err = 0;
  
  if (!excl)
    return EOPNOTSUPP;		/* XXX */

  /* If we have postponed a translator setting on an unlinked node,
     then here's where we set it, by creating the new node instead of
     doing a normal link. */

  switch (np->nn->dtrans)
    {
    case POSSIBLE:
    case NOT_POSSIBLE:
      mutex_lock (&dir->lock);
      p = nfs_initialize_rpc (NFSPROC_LINK (protocol_version),
			      cred, 0, &rpcbuf, dir, -1);
      mutex_unlock (&dir->lock);
      
      mutex_lock (&np->lock);
      p = xdr_encode_fhandle (p, &np->nn->handle);
      mutex_unlock (&np->lock);
  
      mutex_lock (&dir->lock);
      purge_lookup_cache (dir, name, strlen (name));

      p = xdr_encode_fhandle (p, &dir->nn->handle);
      p = xdr_encode_string (p, name);
  
      err = conduct_rpc (&rpcbuf, &p);
      if (!err)
	err = nfs_error_trans (ntohl (*p++));
      mutex_unlock (&dir->lock);
      
      free (rpcbuf);

      break;

    case SYMLINK:
      mutex_lock (&dir->lock);
      p = nfs_initialize_rpc (NFSPROC_SYMLINK (protocol_version),
			      cred, 0, &rpcbuf, dir, -1);
      p = xdr_encode_fhandle (p, &dir->nn->handle);
      mutex_unlock (&dir->lock);
      
      p = xdr_encode_string (p, name);

      mutex_lock (&np->lock);
      err = netfs_validate_stat (np, cred);
      if (err)
	{
	  mutex_unlock (&np->lock);
	  free (rpcbuf);
	  return err;
	}
      
      if (protocol_version == 2)
	{
	  p = xdr_encode_string (p, np->nn->transarg.name);
	  p = xdr_encode_sattr_stat (p, &np->nn_stat);
	}
      else
	{
	  p = xdr_encode_sattr_stat (p, &np->nn_stat);
	  p = xdr_encode_string (p, np->nn->transarg.name);
	}	  
      mutex_unlock (&np->lock);

      mutex_lock (&dir->lock);

      purge_lookup_cache (dir, name, strlen (name));
      err = conduct_rpc (&rpcbuf, &p);
      if (!err)
	{
	  err = nfs_error_trans (ntohl (*p++));
      
	  if (protocol_version == 2 && !err)
	    {
	      free (rpcbuf);

	      /* NFSPROC_SYMLINK stupidly does not pass back an
		 fhandle, so we have to fetch one now. */
	      p = nfs_initialize_rpc (NFSPROC_LOOKUP (protocol_version),
				      cred, 0, &rpcbuf, dir, -1);
	      p = xdr_encode_fhandle (p, &dir->nn->handle);
	      p = xdr_encode_string (p, name);
	  
	      mutex_unlock (&dir->lock);
	  
	      err = conduct_rpc (&rpcbuf, &p);
	      if (!err)
		err = nfs_error_trans (ntohl (*p++));
	      if (!err)
		{
		  mutex_lock (&np->lock);
		  p = recache_handle (p, np);
		  p = process_returned_stat (np, p, 1);
		  mutex_unlock (&np->lock);
		}
	      if (err)
		err = EGRATUITOUS; /* damn */
	    }
	  else if (protocol_version == 3)
	    {
	      if (!err)
		{
		  mutex_unlock (&dir->lock);
		  mutex_lock (&np->lock);
		  p = recache_handle (p, np);
		  p = process_returned_stat (np, p, 1);
		  mutex_unlock (&np->lock);
		  mutex_lock (&dir->lock);
		}
	      p = process_wcc_stat (dir, p, !err);
	      mutex_unlock (&dir->lock);
	    }
	  else
	    mutex_unlock (&dir->lock);
	}
      else
	mutex_unlock (&dir->lock);

      free (rpcbuf);
      break;
      
    case CHRDEV:
    case BLKDEV:
    case FIFO:
    case SOCK:

      if (protocol_version == 2)
	{
	  mutex_lock (&dir->lock);
	  err = verify_nonexistent (cred, dir, name);
	  if (err)
	    return err;

	  p = nfs_initialize_rpc (NFSPROC_CREATE (protocol_version),
				  cred, 0, &rpcbuf, dir, -1);
	  p = xdr_encode_fhandle (p, &dir->nn->handle);
	  p = xdr_encode_string (p, name);
	  mutex_unlock (&dir->lock);
	  
	  mutex_lock (&np->lock);
	  err = netfs_validate_stat (np, cred);
	  if (err)
	    {
	      mutex_unlock (&np->lock);
	      free (rpcbuf);
	      return err;
	    }
	  
	  p = xdr_encode_sattr_stat (p, &np->nn_stat);
	  mutex_unlock (&np->lock);

	  mutex_lock (&dir->lock);
	  purge_lookup_cache (dir, name, strlen (name));
	  err = conduct_rpc (&rpcbuf, &p);
	  if (!err)
	    err = nfs_error_trans (ntohl (*p++));
	  mutex_unlock (&dir->lock);
      
	  mutex_lock (&np->lock);
	  p = recache_handle (p, np);
	  register_fresh_stat (np, p);
	  mutex_unlock (&np->lock);
	  free (rpcbuf);
	}
      else
	{
	  mutex_lock (&dir->lock);
	  p = nfs_initialize_rpc (NFS3PROC_MKNOD, cred, 0, &rpcbuf, dir, -1);
	  p = xdr_encode_fhandle (p, &dir->nn->handle);
	  p = xdr_encode_string (p, name);
	  mutex_unlock (&dir->lock);
	  
	  mutex_lock (&np->lock);
	  err = netfs_validate_stat (np, cred);
	  if (err)
	    {
	      mutex_unlock (&np->lock);
	      free (rpcbuf);
	      return err;
	    }
	  *p++ = htonl (hurd_mode_to_nfs_type (np->nn_stat.st_mode));
	  p = xdr_encode_sattr_stat (p, &np->nn_stat);
	  if (np->nn->dtrans == BLKDEV || np->nn->dtrans == CHRDEV)
	    {
#define major(D) (((D)>>8) & 0xff)
#define minor(D) ((D) & 0xff)
	      *p++ = htonl (major (np->nn_stat.st_rdev));
	      *p++ = htonl (minor (np->nn_stat.st_rdev));
	    }
	  mutex_unlock (&np->lock);
	  
	  purge_lookup_cache (dir, name, strlen (name));
	  err = conduct_rpc (&rpcbuf, &p);
	  if (!err)
	    {
	      err = nfs_error_trans (ntohl (*p++));
	      if (!err)
		{
		  mutex_lock (&np->lock);
		  p = recache_handle (p, np);
		  p = process_returned_stat (np, p, 1);
		  mutex_unlock (&np->lock);
		}
	      mutex_lock (&dir->lock);
	      p = process_wcc_stat (dir, p, !err);
	      mutex_unlock (&dir->lock);
	    }
	  free (rpcbuf);
	}
      break;
    }

  if (err)
    return err;
  
  mutex_lock (&np->lock);

  if (np->nn->dtrans == SYMLINK)
    free (np->nn->transarg.name);
  np->nn->dtrans = NOT_POSSIBLE;

  /* If there is a dead-dir tag lying around, it's time to delete it now. */
  if (np->nn->dead_dir)
    {
      struct node *dir = np->nn->dead_dir;
      char *name = np->nn->dead_name;
      np->nn->dead_dir = 0;
      np->nn->dead_name = 0;
      mutex_unlock (&np->lock);
      
      mutex_lock (&dir->lock);
      netfs_attempt_unlink ((struct iouser *)-1, dir, name);
      mutex_unlock (&dir->lock);
    }
  else
    mutex_unlock (&np->lock);
  
  return 0;
}

/* Implement the netfs_attempt_mkfile callback as described in
   <hurd/netfs.h>. */
error_t
netfs_attempt_mkfile (struct iouser *cred, struct node *dir,
		      mode_t mode, struct node **newnp)
{
  error_t err;
  char *name;
  static int n = 0;

  /* This is the best we can do. */

  name = malloc (50);

  do
    {
      sprintf (name, ".nfstmpgnu.%d", n++);
      err = netfs_attempt_create_file (cred, dir, name, mode, newnp);
      if (err == EEXIST)
	mutex_lock (&dir->lock);
    }
  while (err == EEXIST);
  
  if (err)
    {
      free (name);
      return err;
    }

  assert (!(*newnp)->nn->dead_dir);
  assert (!(*newnp)->nn->dead_name);
  netfs_nref (dir);
  (*newnp)->nn->dead_dir = dir;
  (*newnp)->nn->dead_name = name;
  if ((*newnp)->nn->dtrans == NOT_POSSIBLE)
    (*newnp)->nn->dtrans = POSSIBLE;
  return 0;
}

/* Implement the netfs_attempt_create_file callback as described in
   <hurd/netfs.h>. */
error_t
netfs_attempt_create_file (struct iouser *cred, struct node *np,
			   char *name, mode_t mode, struct node **newnp)
{
  int *p;
  void *rpcbuf;
  error_t err;
  uid_t owner;

  if (cred->uids->num)
    owner = cred->uids->ids[0];
  else
    {
      err = netfs_validate_stat (np, cred);
      owner = err ? 0 : np->nn_stat.st_uid;
      mode &= ~S_ISUID;
    }

  /* RFC 1094 says that create is always exclusive.  But Sun doesn't
     actually *implement* the spec.  No, of course not.  So we have to do
     it for them.  */
  if (protocol_version == 2)
    {
      err = verify_nonexistent (cred, np, name);
      if (err)
	{
	  mutex_unlock (&np->lock);
	  return err;
	}
    }

  purge_lookup_cache (np, name, strlen (name));

  p = nfs_initialize_rpc (NFSPROC_CREATE (protocol_version),
			  cred, 0, &rpcbuf, np, -1);
  p = xdr_encode_fhandle (p, &np->nn->handle);
  p = xdr_encode_string (p, name);
  if (protocol_version == 3)
    {
      /* We happen to know this is where the XID is. */
      int verf = *(int *)rpcbuf;
      
      *p++ = ntohl (EXCLUSIVE);
      /* 8 byte verf */
      *p++ = ntohl (verf);
      p++;
    }
  else
    p = xdr_encode_create_state (p, mode, owner);
  
  err = conduct_rpc (&rpcbuf, &p);

  mutex_unlock (&np->lock);

  if (!err)
    {
      err = nfs_error_trans (ntohl (*p++));
      if (!err)
	{
	  p = xdr_decode_fhandle (p, newnp);
	  p = process_returned_stat (*newnp, p, 1);
	}
      if (err)
	*newnp = 0;
      if (protocol_version == 3)
	{
	  if (*newnp)
	    mutex_unlock (&(*newnp)->lock);
	  mutex_lock (&np->lock);
	  p = process_wcc_stat (np, p, 1);
	  mutex_unlock (&np->lock);
	  if (*newnp)
	    mutex_lock (&(*newnp)->lock);
	}

      if (*newnp && !netfs_validate_stat (*newnp, -1)
	  && (*newnp)->nn_stat.st_uid != owner)
	netfs_attempt_chown (-1, *newnp, owner, (*newnp)->nn_stat.st_gid);
    }
  else
    *newnp = 0;
  
  free (rpcbuf);
  return err;
}

/* Implement the netfs_attempt_unlink callback as described in
   <hurd/netfs.h>. */
error_t
netfs_attempt_unlink (struct iouser *cred, struct node *dir,
		      char *name)
{
  int *p;
  void *rpcbuf;
  error_t err;
  struct node *np;

  /* First lookup the node being removed */
  err = netfs_attempt_lookup (cred, dir, name, &np);
  if (err)
    {
      mutex_lock (&dir->lock);
      return err;
    }

  /* Restore the locks to sanity. */
  mutex_unlock (&np->lock);
  mutex_lock (&dir->lock);

  /* Purge the cache of entries for this node, so that we don't
     regard cache-held references as live. */
  purge_lookup_cache_node (np);

  /* See if there are any other users of this node than the 
     one we just got; if so, we must give this file another link
     so that when we delete the one we are asked for it doesn't go
     away entirely. */
  if (np->references > 1)
    {
      char *newname;
      int n = 0;

      mutex_unlock (&dir->lock);

      newname = malloc (50);
      do
	{
	  sprintf (newname, ".nfs%xgnu.%d", (int) np, n++);
	  err = netfs_attempt_link (cred, dir, np, newname, 1);
	}
      while (err == EEXIST);

      if (err)
	{
	  free (newname);
	  mutex_lock (&dir->lock);
	  netfs_nrele (np);
	  return err;
	}

      /* Write down what name we gave it; we'll delete this when all
	 our uses vanish.  */
      mutex_lock (&np->lock);

      if (np->nn->dead_dir)
	netfs_nrele (np->nn->dead_dir);
      netfs_nref (dir);
      np->nn->dead_dir = dir;
      if (np->nn->dead_name)
	free (np->nn->dead_name);
      np->nn->dead_name = newname;
      if (np->nn->dtrans == NOT_POSSIBLE)
	np->nn->dtrans = POSSIBLE;

      netfs_nput (np);
      mutex_lock (&dir->lock);
    }
  else
    netfs_nrele (np);

  p = nfs_initialize_rpc (NFSPROC_REMOVE (protocol_version),
			  cred, 0, &rpcbuf, dir, -1);
  p = xdr_encode_fhandle (p, &dir->nn->handle);
  p = xdr_encode_string (p, name);
  
  err = conduct_rpc (&rpcbuf, &p);
  if (!err)
    {
      err = nfs_error_trans (ntohl (*p++));
      if (protocol_version == 3)
	p = process_wcc_stat (dir, p, !err);
    }
  
  free (rpcbuf);

  return err;
}

/* Implement the netfs_attempt_rename callback as described in
   <hurd/netfs.h>. */
error_t
netfs_attempt_rename (struct iouser *cred, struct node *fromdir,
		      char *fromname, struct node *todir, char *toname, 
		      int excl)
{
  int *p;
  void *rpcbuf;
  error_t err;
  
  if (excl)
    return EOPNOTSUPP;		/* XXX */

  mutex_lock (&fromdir->lock);
  purge_lookup_cache (fromdir, fromname, strlen (fromname));
  p = nfs_initialize_rpc (NFSPROC_RENAME (protocol_version),
			  cred, 0, &rpcbuf, fromdir, -1);
  p = xdr_encode_fhandle (p, &fromdir->nn->handle);
  p = xdr_encode_string (p, fromname);
  mutex_unlock (&fromdir->lock);
  
  mutex_lock (&todir->lock);
  purge_lookup_cache (todir, toname, strlen (toname));
  p = xdr_encode_fhandle (p, &todir->nn->handle);
  p = xdr_encode_string (p, toname);
  mutex_unlock (&todir->lock);
  
  err = conduct_rpc (&rpcbuf, &p);
  if (!err)
    {
      err = nfs_error_trans (ntohl (*p++));
      if (protocol_version == 3)
	{
	  mutex_lock (&fromdir->lock);
	  p = process_wcc_stat (fromdir, p, !err);
	  p = process_wcc_stat (todir, p, !err);
	}
    }
  
  free (rpcbuf);
  return err;
}

/* Implement the netfs_attempt_readlink callback as described in
   <hurd/netfs.h>. */
error_t
netfs_attempt_readlink (struct iouser *cred, struct node *np,
			char *buf)
{
  int *p;
  void *rpcbuf;
  error_t err;

  if (np->nn->dtrans == SYMLINK)
    {
      strcpy (buf, np->nn->transarg.name);
      return 0;
    }
  
  p = nfs_initialize_rpc (NFSPROC_READLINK (protocol_version),
			  cred, 0, &rpcbuf, np, -1);
  p = xdr_encode_fhandle (p, &np->nn->handle);
  
  err = conduct_rpc (&rpcbuf, &p);
  if (!err)
    {
      err = nfs_error_trans (ntohl (*p++));
      if (protocol_version == 3)
	p = process_returned_stat (np, p, 0);
      if (!err)
	p = xdr_decode_string (p, buf);
    }

  free (rpcbuf);
  return err;
}

/* Implement the netfs_check_open_permissions callback as described in
   <hurd/netfs.h>. */
error_t
netfs_check_open_permissions (struct iouser *cred, struct node *np,
			      int flags, int newnode)
{
  int modes;
  
  if (newnode || (flags & (O_READ|O_WRITE|O_EXEC)) == 0)
    return 0;
  
  netfs_report_access (cred, np, &modes);
  if ((flags & (O_READ|O_WRITE|O_EXEC)) == (flags & modes))
    return 0;
  else
    return EACCES;
}

/* Implement the netfs_report_access callback as described in
   <hurd/netfs.h>. */
error_t
netfs_report_access (struct iouser *cred,
		     struct node *np,
		     int *types)
{
  error_t err;
  
  err = netfs_validate_stat (np, cred);
  if (err)
    return err;
  
  if (protocol_version == 2)
    {
      /* Hope the server means the same thing be the bits as we do. */
      *types = 0;
      if (fshelp_access (&np->nn_stat, S_IREAD, cred) == 0)
	*types |= O_READ;
      if (fshelp_access (&np->nn_stat, S_IWRITE, cred) == 0)
	*types |= O_WRITE;
      if (fshelp_access (&np->nn_stat, S_IEXEC, cred) == 0)
	*types |= O_EXEC;
      return 0;
    }
  else
    {
      int *p;
      void *rpcbuf;
      error_t err;
      int ret;
      int write_check, execute_check;

      if (S_ISDIR (np->nn_stat.st_mode))
	{
	  write_check = ACCESS3_MODIFY | ACCESS3_DELETE | ACCESS3_EXTEND;
	  execute_check = ACCESS3_LOOKUP;
	}
      else
	{
	  write_check = ACCESS3_MODIFY;
	  execute_check = ACCESS3_EXECUTE;
	}

      p = nfs_initialize_rpc (NFS3PROC_ACCESS, cred, 0, &rpcbuf, np, -1);
      p = xdr_encode_fhandle (p, &np->nn->handle);
      *p++ = htonl (ACCESS3_READ | write_check | execute_check);
      
      err = conduct_rpc (&rpcbuf, &p);
      if (!err)
	{
	  err = nfs_error_trans (ntohl (*p++));
	  p = process_returned_stat (np, p, 0);
	  if (!err)
	    {
	      ret = ntohl (*p++);
	      *types = ((ret & ACCESS3_READ ? O_READ : 0)
			| (ret & write_check ? O_WRITE : 0)
			| (ret & execute_check ? O_EXEC : 0));
	    }
	}
      return err;
    }
}

/* These definitions have unfortunate side effects, don't use them,
   clever though they are. */
#if 0
/* Implement the netfs_check_open_permissions callback as described in
   <hurd/netfs.h>. */
error_t
netfs_check_open_permissions (struct iouser *cred, struct node *np,
			      int flags, int newnode)
{
  char byte;
  error_t err;
  size_t len;
 
  /* Sun derived nfs client implementations attempt to reproduce the
     server's permission restrictions by hoping they look like Unix,
     and using that to give errors at open time.  Sadly, that loses
     here.  So instead what we do is try and do what the user
     requested; if we can't, then we fail.  Otherwise, we allow the
     open, but acknowledge that the server might still give an error
     later.  (Even with our check, the server can revoke access, thus
     violiting Posix semantics; this means that erring on the side of
     permitting illegal opens won't harm otherwise correct programs,
     because they need to deal with revocation anyway.)  We thus here
     have the advantage of working correctly with servers that allow
     things Unix denies.  */

  if ((flags & O_READ) == 0
      && (flags & O_WRITE) == 0
      && (flags & O_EXEC) == 0)
    return 0;
    
  err = netfs_validate_stat (np, cred);
  if (err)
    return err;
  
  switch (np->nn_stat.st_mode & S_IFMT)
    {
      /* Don't know how to check, so return provisional success. */
    default:
      return 0;
     
    case S_IFREG:
      len = 1;
      err = netfs_attempt_read (cred, np, 0, &len, &byte);
      if (err)
	{
	  if ((flags & O_READ) || (flags & O_EXEC))
	    return err;
	  else
	    /* If we couldn't read a byte, but the user wasn't actually asking 
	       for read, then we shouldn't inhibit the open now.  */
	    return 0;
	}
  
      if (len != 1)
	/* The file is empty; reads are known to be OK, but writes can't be
	   tested, so no matter what, return success.  */
	return 0;
  
      if (flags & O_WRITE)
	{
	  err = netfs_attempt_write (cred, np, 0, &len, &byte);
	  return err;
	}

      /* Try as we might, we couldn't get the server to bump us, so
	 give (provisional) success. */
      return 0;

    case S_IFDIR:
      if (flags & O_READ)
	{
	  void *rpcbuf;
	  int *p;

	  /* Issue a readdir request; if it fails, then we can
	     return failure.  Otherwise, succeed. */
	  p = nfs_initialize_rpc (NFSPROC_READDIR, cred, 0, &rpcbuf, np, -1);
	  p = xdr_encode_fhandle (p, &np->nn->handle);
	  *p++ = 0;
	  *p++ = htonl (50);
	  err = conduct_rpc (&rpcbuf, &p);
	  if (!err)
	    err = nfs_error_trans (ntohl (*p++));
	  free (rpcbuf);
	  
	  if (err)
	    return err;
	}
      return 0;
    }
}
      
/* Implement the netfs_report_access callback as described in
   <hurd/netfs.h>. */
void
netfs_report_access (struct iouser *cred,
		     struct node *np,
		     int *types)
{
  char byte;
  error_t err;
  size_t len;
  
  /* Much the same logic as netfs_check_open_permissions, except that
     here we err on the side of denying access, and that we always
     have to check everything.  */
  
  *types = 0;

  len = 1;
  err = netfs_attempt_read (cred, np, 0, &len, &byte);
  if (err)
    return;
  assert (len == 1 || len == 0);

  *types |= O_READ | O_EXEC;

  if (len == 1)
    {
      err = netfs_attempt_write (cred, np, 0, &len, &byte);
      if (!err)
	*types |= O_WRITE;
    }
  else
    {
      /* Oh, ugh.  We have to try and write a byte and then truncate
	 back.  God help us if the file becomes unwritable in-between.
	 But because of the use of this function (by setuid programs
	 wanting to see if they should write user's files) we must
	 check this and not just return a presumptive error. */
      byte = 0;
      err = netfs_attempt_write (cred, np, 0, &len, &byte);
      if (!err)
	*types |= O_WRITE;
      netfs_attempt_set_size (cred, np, 0);
    }
}
#endif

/* Fetch the complete contents of DIR into a buffer of directs.  Set
   *BUFP to that buffer.  *BUFP must be freed by the caller when no
   longer needed.  If an error occurs, don't touch *BUFP and return
   the error code.  Set BUFSIZEP to the amount of data used inside
   *BUFP and TOTALENTRIES to the total number of entries copied. */
static error_t
fetch_directory (struct iouser *cred, struct node *dir,
		 void **bufp, size_t *bufsizep, int *totalentries)
{
  void *buf;
  int cookie;
  int *p;
  void *rpcbuf;
  struct dirent *entry;
  void *bp;
  int bufmalloced;
  int eof;
  error_t err;
  int isnext;

  bufmalloced = read_size;
  buf = malloc (bufmalloced);
  bp = buf;
  cookie = 0;
  eof = 0;
  *totalentries = 0;

  while (!eof)
    {
      /* Fetch new directory entries */
      p = nfs_initialize_rpc (NFSPROC_READDIR (protocol_version),
			      cred, 0, &rpcbuf, dir, -1);
      p = xdr_encode_fhandle (p, &dir->nn->handle);
      *p++ = cookie;
      *p++ = ntohl (read_size);
      err = conduct_rpc (&rpcbuf, &p);
      if (!err)
	err = nfs_error_trans (ntohl (*p++));
      if (err)
	{
	  free (buf);
	  return err;
	}

      isnext = ntohl (*p++);
      
      /* Now copy them one at a time. */
      while (isnext)
	{
	  ino_t fileno;
	  int namlen;
	  int reclen;
	  
	  fileno = ntohl (*p++);
	  namlen = ntohl (*p++);

	  /* There's a hidden +1 here for the null byte and -1 because d_name
	     has a size of one already in the sizeof.  */
	  reclen = sizeof (struct dirent) + namlen;
	  reclen = (reclen + 3) & ~3; /* make it a multiple of four */
	  
	  /* Expand buffer if necessary */
	  if (bp + reclen > buf + bufmalloced)
	    {
	      char *newbuf;
	      
	      newbuf = realloc (buf, bufmalloced *= 2);
	      if (newbuf != buf)
		bp = newbuf + (bp - buf);
	      buf = newbuf;
	    }
	  
	  /* Fill in new entry */
	  entry = (struct dirent *) bp;
	  entry->d_fileno = fileno;
	  entry->d_reclen = reclen;
	  entry->d_type = DT_UNKNOWN;
	  entry->d_namlen = namlen;
	  bcopy (p, entry->d_name, namlen);
	  entry->d_name[namlen] = '\0';

	  p += INTSIZE (namlen);
	  bp = bp + entry->d_reclen;

	  ++*totalentries;
	  
	  cookie = *p++;
	  isnext = ntohl (*p++);
	}

      eof = ntohl (*p++);
      free (rpcbuf);
    }

  /* Return it all to the user */
  *bufp = buf;
  *bufsizep = bufmalloced;
  return 0;
}

	 
/* Implement the netfs_get_directs callback as described in
   <hurd/netfs.h>. */
error_t
netfs_get_dirents (struct iouser *cred, struct node *np,
		   int entry, int nentries, char **data,
		   mach_msg_type_number_t *datacnt,
		   vm_size_t bufsiz, int *amt)
{
  void *buf;
  size_t our_bufsiz, allocsize;
  void *bp;
  char *userdp;
  error_t err;
  int totalentries;
  int thisentry;

  err = fetch_directory (cred, np, &buf, &our_bufsiz, &totalentries);
  if (err)
    return err;
  
  /* Allocate enough space to hold the maximum we might return. */
  if (!bufsiz || bufsiz > our_bufsiz)
    allocsize = round_page (our_bufsiz);
  else
    allocsize = round_page (bufsiz);
  if (allocsize > *datacnt)
    vm_allocate (mach_task_self (), (vm_address_t *)data, allocsize, 1);
 
  /* Skip ahead to the correct entry. */
  bp = buf;
  for (thisentry = 0; thisentry < entry;)
    {
      struct dirent *entry = (struct dirent *) bp;
      bp += entry->d_reclen;
      thisentry++;
    }
  
  /* Now copy them one at a time */
  {
    int entries_copied;
    
    for (entries_copied = 0, userdp = *data; 
	 (nentries == -1 || entries_copied < nentries)
	 && (!bufsiz || userdp - *data < bufsiz)
	 && thisentry < totalentries;)
      {
	struct dirent *entry = (struct dirent *) bp;
	bcopy (bp, userdp, entry->d_reclen);
	bp += entry->d_reclen;
	userdp += entry->d_reclen;
	entries_copied++;
	thisentry++;
      }
    *amt = entries_copied;
  }
  
  free (buf);

  /* If we allocated the buffer ourselves, but didn't use 
     all the pages, free the extra. */
  if (allocsize > *datacnt
      && round_page (userdp - *data) < round_page (allocsize))
    vm_deallocate (mach_task_self (), round_page (userdp),
		   round_page (allocsize) - round_page (userdp - *data));

  *datacnt = userdp - *data;
  return 0;
}

  
/* Implement the netfs_set_translator callback as described in
   <hurd/netfs.h>. */
error_t
netfs_set_translator (struct iouser *cred,
		      struct node *np,
		      char *argz,
		      size_t argzlen)
{
  return EOPNOTSUPP;
}

/* Implement the netfs_attempt_mksymlink callback as described in
   <hurd/netfs.h>. */
error_t
netfs_attempt_mksymlink (struct iouser *cred,
			 struct node *np,
			 char *arg)
{
  if (np->nn->dtrans == NOT_POSSIBLE)
    return EOPNOTSUPP;
  
  if (np->nn->dtrans == SYMLINK)
    free (np->nn->transarg.name);
  
  np->nn->transarg.name = malloc (strlen (arg) + 1);
  strcpy (np->nn->transarg.name, arg);
  np->nn->dtrans = SYMLINK;
  np->nn->stat_updated = 0;
  return 0;
}

/* Implement the netfs_attempt_mkdev callback as described in
   <hurd/netfs.h>. */
error_t
netfs_attempt_mkdev (struct iouser *cred,
		     struct node *np,
		     mode_t type,
		     dev_t indexes)
{
  if (np->nn->dtrans == NOT_POSSIBLE)
    return EOPNOTSUPP;
  
  if (np->nn->dtrans == SYMLINK)
    free (np->nn->transarg.name);
  
  np->nn->transarg.indexes = indexes;
  if (type == S_IFBLK)
    np->nn->dtrans = BLKDEV;
  else
    np->nn->dtrans = CHRDEV;
  np->nn->stat_updated = 0;
  return 0;
}