-
-
Notifications
You must be signed in to change notification settings - Fork 338
/
nfs_add_on.c
2551 lines (1982 loc) · 54.5 KB
/
nfs_add_on.c
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
#include <posix/stdlib.h>
#include "nfs_add_on.h"
#include <sys/socket.h>
#include "rpc.h"
#include "pmap.h"
#include "nfs.h"
#include "mount.h"
#include <errno.h>
#include <string.h>
#include <KernelExport.h>
#include <driver_settings.h>
#include <sys/stat.h>
#include <dirent.h>
#include <SupportDefs.h>
#include <ByteOrder.h>
#include <netinet/udp.h>
#ifndef UDP_SIZE_MAX
#define UDP_SIZE_MAX 65515
#endif
#define B_UDP_MAX_SIZE UDP_SIZE_MAX
static status_t fs_rmdir(fs_volume *_volume, fs_vnode *_dir, const char *name);
/* *** configuration *** */
//#define NFS_FS_FLAGS B_FS_IS_SHARED
#define NFS_FS_FLAGS B_FS_IS_SHARED|B_FS_IS_PERSISTENT
/* port numbers: most NFS servers insist on the client port to be < 1024 (secure option) */
/* ports to bind() to; we start at conf_high_port, then go down */
static int16 conf_high_port = 1023;
static int16 conf_low_port = 900;
/* Allow open() to open directories too */
static bool conf_allow_dir_open = true;
/* Do we list ".." in readdir(rootid) ? (the VFS corrects the dirents anyway) */
/* this seems to be mandatory for Dano... BEntry::GetPath() needs that */
static bool conf_ls_root_parent = true;
/* timeout when waiting for an answer to a call */
static bigtime_t conf_call_timeout = 2000000;
/* number of retries when waiting for an anwser to a call */
static unsigned long conf_call_tries = 3;
/* don't check who the answers come from for requests */
bool conf_no_check_ip_xid = false;
static vint32 refcount = 0; /* we only want to read the config once ? */
static status_t
read_config(void)
{
void *handle;
const char *str, *endptr;
handle = load_driver_settings("nfs");
if (handle == NULL)
return ENOENT;
str = get_driver_parameter(handle, "high_port", NULL, NULL);
if (str) {
endptr = str + strlen(str);
conf_high_port = (int16)strtoul(str, (char **)&endptr, 10);
}
str = get_driver_parameter(handle, "low_port", NULL, NULL);
if (str) {
endptr = str + strlen(str);
conf_low_port = (int16)strtoul(str, (char **)&endptr, 10);
}
conf_allow_dir_open = get_driver_boolean_parameter(handle, "allow_dir_open", conf_allow_dir_open, true);
conf_ls_root_parent = get_driver_boolean_parameter(handle, "ls_root_parent", conf_ls_root_parent, true);
conf_no_check_ip_xid = get_driver_boolean_parameter(handle, "no_check_ip_xid", conf_no_check_ip_xid, true);
str = get_driver_parameter(handle, "call_timeout", NULL, NULL);
if (str) {
endptr = str + strlen(str);
conf_call_timeout = (bigtime_t)1000 * strtoul(str, (char **)&endptr, 10);
if (conf_call_timeout < 1000)
conf_call_timeout = 1000;
}
str = get_driver_parameter(handle, "call_tries", NULL, NULL);
if (str) {
endptr = str + strlen(str);
conf_call_tries = strtoul(str, (char **)&endptr, 10);
}
unload_driver_settings(handle);
return B_OK;
}
status_t
create_socket(fs_nspace *ns)
{
struct sockaddr_in addr;
uint16 port=conf_high_port;
ns->s=socket(AF_INET,SOCK_DGRAM,0);
if (ns->s<0)
return errno;
do
{
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=htonl(INADDR_ANY);
//addr.sin_addr.s_addr=htonl(INADDR_LOOPBACK);
addr.sin_port=htons(port);
memset (addr.sin_zero,0,sizeof(addr.sin_zero));
if (bind(ns->s,(const struct sockaddr *)&addr,sizeof(addr))<0)
{
if (errno!=EADDRINUSE)
{
int result=errno;
close(ns->s);
return result;
}
port--;
if (port==conf_low_port)
{
close(ns->s);
return B_ERROR;
}
}
else
break;//return B_OK;
}
while (true);
// doesn't seem to help with autoincrementing port on source address...
addr.sin_addr = ns->mountAddr.sin_addr;
addr.sin_port = htons(111);
//kconnect(ns->s,(const struct sockaddr *)&addr,sizeof(addr));
return B_OK;
}
#if 0
static status_t
connect_socket(fs_nspace *ns)
{
uint16 port = conf_high_port;
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
//addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
addr.sin_port = htons(port);
memset(addr.sin_zero,0,sizeof(addr.sin_zero));
if (kconnect(ns->s,(const struct sockaddr *)&ns->nfsAddr,sizeof(ns->nfsAddr))<0)
{
return -1;
}
return B_OK;
}
#endif
status_t
init_postoffice(fs_nspace *ns)
{
status_t result;
ns->tid=spawn_kernel_thread ((thread_func)postoffice_func,"NFSv2 Postoffice",B_NORMAL_PRIORITY,ns);
if (ns->tid<B_OK)
return ns->tid;
ns->quit=false;
result=resume_thread (ns->tid);
if (result<B_OK)
{
kill_thread (ns->tid);
return result;
}
return B_OK;
}
void
shutdown_postoffice(fs_nspace *ns)
{
status_t result;
ns->quit=true;
close(ns->s);
wait_for_thread (ns->tid,&result);
}
status_t
postoffice_func(fs_nspace *ns)
{
uint8 *buffer=(uint8 *)malloc(B_UDP_MAX_SIZE);
while (!ns->quit) {
struct sockaddr_in from;
socklen_t fromLen=sizeof(from);
ssize_t bytes = recvfrom(ns->s, buffer, B_UDP_MAX_SIZE, 0,
(struct sockaddr *)&from, &fromLen);
if (bytes >= 4) {
struct PendingCall *call;
int32 xid=B_BENDIAN_TO_HOST_INT32(*((int32 *)buffer));
call=RPCPendingCallsFindAndRemovePendingCall(&ns->pendingCalls, xid,
&from);
if (call) {
call->buffer=(uint8 *)malloc(bytes);
memcpy(call->buffer, buffer, bytes);
while (release_sem (call->sem) == B_INTERRUPTED);
} else {
dprintf("nfs: postoffice: can't find pending call to remove "
"for xid %" B_PRId32 "\n", xid);
}
}
}
free (buffer);
return B_OK;
}
uint8 *
send_rpc_call(fs_nspace *ns, const struct sockaddr_in *addr, int32 prog,
int32 vers, int32 proc, const struct XDROutPacket *packet)
{
int32 xid;
size_t authSize;
struct PendingCall *pending;
int32 retries=conf_call_tries;
status_t result;
struct PendingCall *call;
struct XDROutPacket rpc_call;
XDROutPacketInit(&rpc_call);
xid=atomic_add(&ns->xid, 1);
#ifdef DEBUG_XID
//dbgprintxid(logfd1, xid);
#endif
XDROutPacketAddInt32(&rpc_call, xid);
XDROutPacketAddInt32(&rpc_call, RPC_CALL);
XDROutPacketAddInt32(&rpc_call, RPC_VERSION);
XDROutPacketAddInt32(&rpc_call, prog);
XDROutPacketAddInt32(&rpc_call, vers);
XDROutPacketAddInt32(&rpc_call, proc);
#if !defined(USE_SYSTEM_AUTHENTICATION)
XDROutPacketAddInt32(&rpc_call, RPC_AUTH_NONE);
XDROutPacketAddDynamic (&rpc_call, NULL, 0);
#else
XDROutPacketAddInt32(&rpc_call, RPC_AUTH_SYS);
authSize = 4 + 4 + ((strlen(ns->params.server) + 3) &~3) + 4 + 4 + 4;
XDROutPacketAddInt32(&rpc_call, authSize);
XDROutPacketAddInt32(&rpc_call, 0);
XDROutPacketAddString(&rpc_call, ns->params.server);
XDROutPacketAddInt32(&rpc_call, ns->params.uid);
XDROutPacketAddInt32(&rpc_call, ns->params.gid);
XDROutPacketAddInt32(&rpc_call, 0);
#endif
XDROutPacketAddInt32(&rpc_call, RPC_AUTH_NONE);
XDROutPacketAddDynamic (&rpc_call, NULL, 0);
XDROutPacketAppend (&rpc_call, packet);
pending = RPCPendingCallsAddPendingCall(&ns->pendingCalls, xid, addr);
#ifdef DEBUG_XID
checksemstate(xid, pending->sem, 0);
#endif
do {
ssize_t bytes;
do {
bytes = sendto(ns->s,(const void *)XDROutPacketBuffer(&rpc_call),
XDROutPacketLength(&rpc_call), 0,
(const struct sockaddr *)addr, sizeof(*addr));
}
while (bytes < 0 && errno == EINTR);
do {
result = acquire_sem_etc (pending->sem, 1, B_TIMEOUT,
(retries) ? (conf_call_timeout) : (2*conf_call_timeout));
}
while (result == B_INTERRUPTED);
retries--;
} while (result == B_TIMED_OUT && retries >= 0);
if (result >= B_OK) {
uint8 *buffer = pending->buffer;
pending->buffer = NULL;
SemaphorePoolPut(&ns->pendingCalls.fPool, pending->sem);
PendingCallDestroy(pending);
free(pending);
XDROutPacketDestroy(&rpc_call);
return buffer;
}
// we timed out
call = RPCPendingCallsFindAndRemovePendingCall(&ns->pendingCalls, xid, addr);
dprintf("nfs: xid %" B_PRId32 " timed out, removing from queue", xid);
#if 0
if (call==NULL)
{
#if 1
//XXX:mmu_man:???
while (acquire_sem(pending->sem)==B_INTERRUPTED);
#else
status_t err;
/* NOTE(mmu_man): there can be a race condition here where the sem is returned
* to the pool without the correct value, compromising the next call using it.
* however it seems waiting forever can lead to lockups...
*/
while ((err = acquire_sem_etc(pending->sem,1,B_TIMEOUT,5000000))==B_INTERRUPTED);
dprintf("nfs: acquire(pending->sem) = 0x%08lx\n", err);
if (err == B_TIMED_OUT)
dprintf("nfs: timed out waiting on sem\n");
#endif
}
#endif
/* mmu_man */
if (call) /* if the call has been found and removed (atomic op), the sem hasn't been released */
SemaphorePoolPut(&ns->pendingCalls.fPool, pending->sem);
else
delete_sem(pending->sem); /* else it's in an unknown state, forget it */
PendingCallDestroy(pending);
free(pending);
XDROutPacketDestroy (&rpc_call);
return NULL;
}
bool
is_successful_reply(struct XDRInPacket *reply)
{
bool success = false;
int32 xid = XDRInPacketGetInt32(reply);
rpc_msg_type mtype=(rpc_msg_type)XDRInPacketGetInt32(reply);
rpc_reply_stat replyStat=(rpc_reply_stat)XDRInPacketGetInt32(reply);
(void)xid;
(void)mtype;
if (replyStat == RPC_MSG_DENIED) {
rpc_reject_stat rejectStat = (rpc_reject_stat)XDRInPacketGetInt32(reply);
if (rejectStat == RPC_RPC_MISMATCH) {
int32 low = XDRInPacketGetInt32(reply);
int32 high = XDRInPacketGetInt32(reply);
dprintf("nfs: RPC_MISMATCH (%" B_PRId32 ",%" B_PRId32 ")", low,
high);
} else {
rpc_auth_stat authStat = (rpc_auth_stat)XDRInPacketGetInt32(reply);
dprintf("nfs: RPC_AUTH_ERROR (%d)", authStat);
}
} else {
rpc_auth_flavor flavor = (rpc_auth_flavor)XDRInPacketGetInt32(reply);
char body[400];
size_t bodyLength = XDRInPacketGetDynamic(reply, body);
rpc_accept_stat acceptStat = (rpc_accept_stat)XDRInPacketGetInt32(reply);
(void)flavor;
(void)bodyLength;
if (acceptStat == RPC_PROG_MISMATCH) {
int32 low = XDRInPacketGetInt32(reply);
int32 high = XDRInPacketGetInt32(reply);
dprintf("nfs: RPC_PROG_MISMATCH (%" B_PRId32 ",%" B_PRId32 ")",
low, high);
} else if (acceptStat != RPC_SUCCESS)
dprintf("nfs: Accepted but failed (%d)", acceptStat);
else
success = true;
}
return success;
}
status_t
get_remote_address(fs_nspace *ns, int32 prog, int32 vers, int32 prot,
struct sockaddr_in *addr)
{
struct XDROutPacket call;
uint8 *replyBuf;
XDROutPacketInit(&call);
addr->sin_port = htons(PMAP_PORT);
XDROutPacketAddInt32(&call, prog);
XDROutPacketAddInt32(&call, vers);
XDROutPacketAddInt32(&call, prot);
XDROutPacketAddInt32(&call, 0);
replyBuf = send_rpc_call(ns, addr, PMAP_PROGRAM, PMAP_VERSION,
PMAPPROC_GETPORT, &call);
if (replyBuf) {
struct XDRInPacket reply;
XDRInPacketInit(&reply);
XDRInPacketSetTo(&reply,replyBuf,0);
if (is_successful_reply(&reply)) {
addr->sin_port = htons(XDRInPacketGetInt32(&reply));
memset(addr->sin_zero, 0, sizeof(addr->sin_zero));
XDRInPacketDestroy(&reply);
XDROutPacketDestroy(&call);
return B_OK;
}
XDRInPacketDestroy(&reply);
}
XDROutPacketDestroy (&call);
return EHOSTUNREACH;
}
status_t
nfs_mount(fs_nspace *ns, const char *path, nfs_fhandle *fhandle)
{
struct XDROutPacket call;
struct XDRInPacket reply;
uint8 *replyBuf;
int32 fhstatus;
XDROutPacketInit(&call);
XDRInPacketInit(&reply);
XDROutPacketAddString(&call,path);
replyBuf = send_rpc_call(ns, &ns->mountAddr, MOUNT_PROGRAM, MOUNT_VERSION,
MOUNTPROC_MNT, &call);
if (!replyBuf) {
XDRInPacketDestroy(&reply);
XDROutPacketDestroy(&call);
return EHOSTUNREACH;
}
XDRInPacketSetTo(&reply, replyBuf, 0);
if (!is_successful_reply(&reply)) {
XDRInPacketDestroy(&reply);
XDROutPacketDestroy(&call);
return B_ERROR;
}
fhstatus = XDRInPacketGetInt32(&reply);
if (fhstatus != 0) {
XDRInPacketDestroy(&reply);
XDROutPacketDestroy(&call);
return map_nfs_to_system_error(fhstatus);
}
XDRInPacketGetFixed(&reply, fhandle->opaque, NFS_FHSIZE);
XDRInPacketDestroy(&reply);
XDROutPacketDestroy(&call);
return B_OK;
}
status_t
nfs_lookup (fs_nspace *ns, const nfs_fhandle *dir, const char *filename,
nfs_fhandle *fhandle, struct stat *st)
{
struct XDROutPacket call;
struct XDRInPacket reply;
int32 status;
uint8 *replyBuf;
XDROutPacketInit(&call);
XDRInPacketInit(&reply);
XDROutPacketAddFixed(&call, dir->opaque, NFS_FHSIZE);
XDROutPacketAddString(&call, filename);
replyBuf = send_rpc_call(ns, &ns->nfsAddr, NFS_PROGRAM, NFS_VERSION,
NFSPROC_LOOKUP, &call);
if (!replyBuf) {
XDRInPacketDestroy(&reply);
XDROutPacketDestroy(&call);
return EHOSTUNREACH;
}
XDRInPacketSetTo(&reply, replyBuf, 0);
if (!is_successful_reply(&reply)) {
XDRInPacketDestroy(&reply);
XDROutPacketDestroy(&call);
return B_ERROR;
}
status = XDRInPacketGetInt32(&reply);
if (status != NFS_OK) {
XDRInPacketDestroy(&reply);
XDROutPacketDestroy(&call);
return map_nfs_to_system_error(status);
}
XDRInPacketGetFixed(&reply, fhandle->opaque, NFS_FHSIZE);
if (st)
get_nfs_attr(&reply, st);
XDRInPacketDestroy(&reply);
XDROutPacketDestroy(&call);
return B_OK;
}
status_t
nfs_getattr(fs_nspace *ns, const nfs_fhandle *fhandle, struct stat *st)
{
struct XDROutPacket call;
struct XDRInPacket reply;
uint8 *replyBuf;
int32 status;
XDROutPacketInit(&call);
XDRInPacketInit(&reply);
XDROutPacketAddFixed(&call, fhandle->opaque, NFS_FHSIZE);
replyBuf = send_rpc_call(ns, &ns->nfsAddr, NFS_PROGRAM, NFS_VERSION,
NFSPROC_GETATTR, &call);
if (replyBuf == NULL) {
XDRInPacketDestroy(&reply);
XDROutPacketDestroy(&call);
return EHOSTUNREACH;
}
XDRInPacketSetTo(&reply, replyBuf, 0);
if (!is_successful_reply(&reply)) {
XDRInPacketDestroy(&reply);
XDROutPacketDestroy(&call);
return B_ERROR;
}
status = XDRInPacketGetInt32(&reply);
if (status != NFS_OK) {
XDRInPacketDestroy(&reply);
XDROutPacketDestroy(&call);
return map_nfs_to_system_error(status);
}
get_nfs_attr(&reply, st);
XDRInPacketDestroy(&reply);
XDROutPacketDestroy(&call);
return B_OK;
}
status_t
nfs_truncate_file(fs_nspace *ns, const nfs_fhandle *fhandle, struct stat *st)
{
struct XDROutPacket call;
struct XDRInPacket reply;
uint8 *replyBuf;
int32 status;
XDROutPacketInit(&call);
XDRInPacketInit(&reply);
XDROutPacketAddFixed(&call, fhandle->opaque, NFS_FHSIZE);
XDROutPacketAddInt32(&call, -1);
XDROutPacketAddInt32(&call, -1);
XDROutPacketAddInt32(&call, -1);
XDROutPacketAddInt32(&call, 0);
XDROutPacketAddInt32(&call, time(NULL));
XDROutPacketAddInt32(&call, 0);
XDROutPacketAddInt32(&call, time(NULL));
XDROutPacketAddInt32(&call, 0);
replyBuf = send_rpc_call(ns, &ns->nfsAddr, NFS_PROGRAM, NFS_VERSION,
NFSPROC_SETATTR, &call);
if (replyBuf == NULL) {
XDRInPacketDestroy(&reply);
XDROutPacketDestroy(&call);
return EHOSTUNREACH;
}
XDRInPacketSetTo(&reply, replyBuf, 0);
if (!is_successful_reply(&reply)) {
XDRInPacketDestroy(&reply);
XDROutPacketDestroy(&call);
return B_ERROR;
}
status = XDRInPacketGetInt32(&reply);
if (status != NFS_OK) {
XDRInPacketDestroy(&reply);
XDROutPacketDestroy(&call);
return map_nfs_to_system_error(status);
}
if (st)
get_nfs_attr(&reply,st);
XDRInPacketDestroy(&reply);
XDROutPacketDestroy(&call);
return B_OK;
}
void
get_nfs_attr(struct XDRInPacket *reply, struct stat *st)
{
nfs_ftype ftype=(nfs_ftype)XDRInPacketGetInt32(reply);
(void) ftype;
st->st_mode=XDRInPacketGetInt32(reply);
st->st_dev=0; // just to be sure
st->st_nlink=XDRInPacketGetInt32(reply);
st->st_uid=XDRInPacketGetInt32(reply);
st->st_gid=XDRInPacketGetInt32(reply);
st->st_size=XDRInPacketGetInt32(reply);
st->st_blksize=XDRInPacketGetInt32(reply);
st->st_rdev=XDRInPacketGetInt32(reply);
st->st_blocks=XDRInPacketGetInt32(reply);
XDRInPacketGetInt32(reply); // fsid
st->st_ino=XDRInPacketGetInt32(reply);
st->st_atime=XDRInPacketGetInt32(reply);
XDRInPacketGetInt32(reply); // usecs
st->st_mtime=st->st_crtime=XDRInPacketGetInt32(reply);
XDRInPacketGetInt32(reply); // usecs
st->st_ctime=XDRInPacketGetInt32(reply);
XDRInPacketGetInt32(reply); // usecs
}
status_t
map_nfs_to_system_error(status_t nfsstatus)
{
switch (nfsstatus) {
case NFS_OK:
return B_OK;
case NFSERR_PERM:
return EPERM;
case NFSERR_NOENT:
return ENOENT;
case NFSERR_IO:
return EIO;
case NFSERR_NXIO:
return ENXIO;
case NFSERR_ACCES:
return EACCES;
case NFSERR_EXIST:
return EEXIST;
case NFSERR_NODEV:
return ENODEV;
case NFSERR_NOTDIR:
return ENOTDIR;
case NFSERR_ISDIR:
return EISDIR;
case NFSERR_FBIG:
return EFBIG;
case NFSERR_NOSPC:
return ENOSPC;
case NFSERR_ROFS:
return EROFS;
case NFSERR_NAMETOOLONG:
return ENAMETOOLONG;
case NFSERR_NOTEMPTY:
return ENOTEMPTY;
case NFSERR_STALE:
return C_ERROR_STALE;
default:
return B_ERROR;
}
}
nfs_fhandle
handle_from_vnid(fs_nspace *ns, ino_t vnid)
{
fs_node *current;
while (acquire_sem(ns->sem) == B_INTERRUPTED);
current = ns->first;
while (current != NULL && current->vnid != vnid)
current = current->next;
while (release_sem(ns->sem) == B_INTERRUPTED);
return current->fhandle;
}
void
insert_node(fs_nspace *ns, fs_node *node)
{
fs_node *current;
while (acquire_sem(ns->sem) == B_INTERRUPTED);
current = ns->first;
while (current != NULL && current->vnid != node->vnid)
current = current->next;
if (current) {
free(node);
while (release_sem(ns->sem) == B_INTERRUPTED);
return;
}
node->next = ns->first;
ns->first = node;
while (release_sem (ns->sem) == B_INTERRUPTED);
}
void
remove_node(fs_nspace *ns, ino_t vnid)
{
fs_node *current;
fs_node *previous;
while (acquire_sem(ns->sem) == B_INTERRUPTED);
current = ns->first;
previous = NULL;
while (current != NULL && current->vnid != vnid) {
previous = current;
current = current->next;
}
if (current) {
if (previous)
previous->next = current->next;
else
ns->first = current->next;
free(current);
}
while (release_sem(ns->sem) == B_INTERRUPTED);
}
// #pragma mark -
static status_t
fs_read_vnode(fs_volume *_volume, ino_t vnid, fs_vnode *_node, int *_type,
uint32 *_flags, bool r)
{
fs_nspace *ns;
fs_node *current;
ns = _volume->private_volume;
if (!r) {
while (acquire_sem(ns->sem) == B_INTERRUPTED);
}
current = ns->first;
while (current != NULL && current->vnid != vnid)
current = current->next;
if (!current)
return EINVAL;
current->vnid = vnid;
_node->private_node = current;
_node->ops = &sNFSVnodeOps;
*_type = current->mode;
*_flags = 0;
if (!r) {
while (release_sem(ns->sem) == B_INTERRUPTED);
}
return B_OK;
}
static status_t
fs_release_vnode(fs_volume *_volume, fs_vnode *node, bool r)
{
(void) _volume;
(void) node;
(void) r;
return B_OK;
}
static status_t
fs_walk(fs_volume *_volume, fs_vnode *_base, const char *file, ino_t *vnid)
{
fs_node *dummy;
status_t result;
fs_nspace *ns;
fs_node *base;
//dprintf("nfs: walk(%s)\n", file);//XXX:mmu_man:debug
ns = _volume->private_volume;
base = _base->private_node;
if (!strcmp(".", file))
*vnid = base->vnid;
else {
fs_node *newNode = (fs_node *)malloc(sizeof(fs_node));
struct stat st;
if ((result = nfs_lookup(ns, &base->fhandle, file, &newNode->fhandle,
&st)) < B_OK) {
free(newNode);
return result;
}
newNode->vnid = st.st_ino;
newNode->mode = st.st_mode;
*vnid = newNode->vnid;
insert_node(ns, newNode);
}
if ((result = get_vnode (_volume, *vnid, (void **)&dummy)) < B_OK)
return result;
return B_OK;
}
static status_t
fs_opendir(fs_volume *_volume, fs_vnode *_node, void **_cookie)
{
fs_nspace *ns;
fs_node *node;
nfs_cookie **cookie;
struct stat st;
status_t result;
ns = _volume->private_volume;
node = _node->private_node;
cookie = (nfs_cookie **)_cookie;
if ((result = nfs_getattr(ns, &node->fhandle, &st)) < B_OK)
return result;
if (!S_ISDIR(st.st_mode))
return ENOTDIR;
*cookie = (nfs_cookie *)malloc(sizeof(nfs_cookie));
memset((*cookie)->opaque,0,NFS_COOKIESIZE);
return B_OK;
}
static status_t
fs_closedir(fs_volume *_volume, fs_vnode *_node, void *cookie)
{
(void) _volume;
(void) _node;
(void) cookie;
return B_OK;
}
static status_t
fs_rewinddir(fs_volume *_volume, fs_vnode *_node, void *_cookie)
{
nfs_cookie *cookie = (nfs_cookie *)_cookie;
(void) _volume;
(void) _node;
memset (cookie->opaque, 0, NFS_COOKIESIZE);
return B_OK;
}
static status_t
fs_readdir(fs_volume *_volume, fs_vnode *_node, void *_cookie,
struct dirent *buf, size_t bufsize, uint32 *num)
{
nfs_cookie *cookie = (nfs_cookie *)_cookie;
int32 max = *num;
int32 eof;
fs_nspace *ns;
fs_node *node;
size_t count = min_c(6000, max * 300);
*num = 0;
ns = _volume->private_volume;
node = _node->private_node;
do {
ino_t vnid;
char *filename;
uint8 *replyBuf;
struct XDROutPacket call;
struct XDRInPacket reply;
int32 status;
XDROutPacketInit(&call);
XDRInPacketInit(&reply);
XDROutPacketAddFixed(&call, node->fhandle.opaque, NFS_FHSIZE);
XDROutPacketAddFixed(&call, cookie->opaque, NFS_COOKIESIZE);
XDROutPacketAddInt32(&call, count);
replyBuf = send_rpc_call (ns, &ns->nfsAddr, NFS_PROGRAM, NFS_VERSION,
NFSPROC_READDIR, &call);
if (!replyBuf) {
XDRInPacketDestroy(&reply);
XDROutPacketDestroy(&call);
return B_ERROR;
}
XDRInPacketSetTo(&reply, replyBuf, 0);
if (!is_successful_reply(&reply)) {
XDRInPacketDestroy(&reply);
XDROutPacketDestroy(&call);
return B_ERROR;
}
status = XDRInPacketGetInt32(&reply);
if (status != NFS_OK) {
XDRInPacketDestroy(&reply);
XDROutPacketDestroy(&call);
return map_nfs_to_system_error(status);
}
while (XDRInPacketGetInt32(&reply) == 1) {
nfs_cookie newCookie;