-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathnfs.cl
1626 lines (1453 loc) · 53.8 KB
/
nfs.cl
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
;; -*- mode: common-lisp -*-
;; See the file LICENSE for the full license governing this code.
(in-package :user)
(eval-when (compile) (declaim (optimize (speed 3))))
(eval-when (compile load eval)
(use-package :gen-nfs)
(use-package :xdr)
(require :pe)
(require :autozoom))
(defparameter *socketbuffersize* (* 128 1024))
(defparameter *nfsd-start-time* nil)
;; Called by startem
(defun nfsd (start-gate)
(setf *nfsd-start-time* (get-universal-time))
(sunrpc:with-rpc-sockets ("NFS" usock tsock :port #.*nfs-port*)
(sunrpc:with-portmapper-mappings ("NFS" #.*nfs-program*
'(2 3)
#.*nfs-port*
#.*nfs-port*)
(initialize-reaper-process)
(mp:process-run-function "attr cache reaper" #'attr-cache-reaper)
(let ((server (sunrpc:make-rpc-server :udpsock usock :tcpsock tsock)))
(dolist (sock (list usock tsock))
(socket:set-socket-options sock
:receive-buffer-size *socketbuffersize*
:send-buffer-size *socketbuffersize*))
(announce "started")
(if* *nfs-gc-debug*
then (logit-stamp "~&Turning on memory management debugging.~%")
(setf (sys:gsgc-switch :print) t)
(setf (sys:gsgc-switch :stats) t)
;; This is the default setting.
(setf *global-gc-behavior* :auto-and-warn)
else (setf (sys:gsgc-switch :print) nil))
;; Notify caller that we've started
(mp:open-gate start-gate)
(loop
(sunrpc:rpc-receive-and-handle-message server
#'nfsd-message-handler))))))
(defun nfsd-message-handler (xdr peer)
(declare (optimize (speed 3)))
(let ((msg (sunrpc:xdr-rpc-msg xdr)))
(sunrpc:with-valid-call (msg peer cbody)
(let ((xid (sunrpc:rpc-msg-xid msg))
(vers (sunrpc:call-body-vers cbody))
(proc (sunrpc:call-body-proc cbody)))
;; sanity checks first
(when (not (eq (sunrpc:call-body-prog cbody) #.*nfs-program*))
(if *nfs-debug*
(logit-stamp "~
NFS: ~a: Sending program unavailable response for prog=~D~%"
(sunrpc:peer-dotted peer)
(sunrpc:call-body-prog cbody)))
(sunrpc:send-prog-unavail-reply peer xid sunrpc:*nullverf*)
(return-from nfsd-message-handler))
(case vers
(2
(case proc
(0 (nfsd-null peer xid xdr cbody))
(1 (nfsd-getattr peer xid xdr cbody))
(2 (nfsd-setattr peer xid xdr cbody))
;; 3 = ROOT
(4 (nfsd-lookup peer xid xdr cbody))
(5 (nfsd-readlink peer xid xdr cbody))
(6 (nfsd-read peer xid xdr cbody))
;; 7 = WRITECACHE
(8 (nfsd-write peer xid xdr cbody))
(9 (nfsd-create peer xid xdr cbody))
(10 (nfsd-remove peer xid xdr cbody))
(11 (nfsd-rename peer xid xdr cbody))
(12 (nfsd-link peer xid xdr cbody))
(13 (nfsd-symlink peer xid xdr cbody))
(14 (nfsd-mkdir peer xid xdr cbody))
(15 (nfsd-rmdir peer xid xdr cbody))
(16 (nfsd-readdir peer xid xdr cbody))
(17 (nfsd-statfs peer xid xdr cbody))
;; secret functions used by the configuration program
(100 (nfsd-get-config-file-path peer xid xdr cbody))
(101 (nfsd-reload-configuration peer xid xdr cbody))
;; secret function used by the console program
(102 (nfsd-get-console-port peer xid xdr cbody))
(t
(sunrpc:send-proc-unavail-reply peer xid sunrpc:*nullverf*)
(logit-stamp "NFSv2: ~a: unhandled procedure ~D~%"
(sunrpc:peer-dotted peer) proc))))
(3
(case proc
(0 (nfsd-null peer xid xdr cbody))
(1 (nfsd-getattr peer xid xdr cbody))
(2 (nfsd-setattr3 peer xid xdr cbody))
(3 (nfsd-lookup peer xid xdr cbody))
(4 (nfsd-access peer xid xdr cbody))
(5 (nfsd-readlink peer xid xdr cbody))
(6 (nfsd-read3 peer xid xdr cbody))
(7 (nfsd-write3 peer xid xdr cbody))
(8 (nfsd-create3 peer xid xdr cbody))
(9 (nfsd-mkdir peer xid xdr cbody))
(10 (nfsd-symlink3 peer xid xdr cbody))
(11 (nfsd-mknod peer xid xdr cbody))
(12 (nfsd-remove peer xid xdr cbody))
(13 (nfsd-rmdir peer xid xdr cbody))
(14 (nfsd-rename peer xid xdr cbody))
(15 (nfsd-link peer xid xdr cbody))
(16 (nfsd-readdir3 peer xid xdr cbody))
;;;;TODO: how to handle stream errors here? like
;;;; "Connection reset by peer" (errno 10054)
(17 (nfsd-readdirplus peer xid xdr cbody))
(18 (nfsd-fsstat peer xid xdr cbody))
(19 (nfsd-fsinfo peer xid xdr cbody))
(20 (nfsd-pathconf peer xid xdr cbody))
(21 (nfsd-commit peer xid xdr cbody))
(t
(sunrpc:send-proc-unavail-reply peer xid sunrpc:*nullverf*)
(logit-stamp "NFSv3: ~a: unhandled procedure ~D~%"
(sunrpc:peer-dotted peer)
proc))))
(t
(if *nfs-debug*
(logit-stamp "NFS: ~a: Sending program version mismatch response~%"
(sunrpc:peer-dotted peer)))
(sunrpc:send-prog-mismatch-reply peer xid sunrpc:*nullverf* 2 3)
(return-from nfsd-message-handler)))))))
(defun log-backtrace-to-nfs-errlog (c)
(with-open-file (f "sys:nfs-errlog.txt"
:direction :output
:if-does-not-exist :create
:if-exists :append)
(format f "~a: Allegro NFS version ~a (commit ~a)~%"
(excl.osi:ctime)
*nfsd-version*
*nfsd-commit-id*)
(format f "Unexpected error: ~a~%" c)
(top-level.debug:zoom f :count t)))
(defun get-error-code-from-condition (c)
"Handles syscall-error and errno-stream-error"
(typecase c
(syscall-error
(excl::syscall-error-errno c))
(errno-stream-error
(slot-value c 'excl::code))))
(defun syscall-error-handler (c xdr savedpos vers debug-this-procedure)
"Handles syscall-error and errno-stream-error"
(let ((errno (get-error-code-from-condition c)))
(setf (xdr:xdr-pos xdr) savedpos)
;; Make some errors look less alarming.
(when debug-this-procedure
(case errno
(#.*enoent*
(logit " => [Not found]~%"))
(#.*enotempty*
(logit " => [Not empty]~%"))
(#.*eexist*
(logit " => [Already exists]~%"))
(#.*enospc*
(logit " => [No space]~%"))
(t
(logit "Handling file error: ~A~%" c))))
(xdr-int xdr (map-errno-to-nfs-error-code errno))
(if (= vers 3)
(nfs-xdr-wcc-data xdr nil nil))))
(defmacro with-nfs-err-handler ((xdr vers) &body body)
(let ((savepossym (gensym))
(c (gensym))
(out (gensym)))
`(let ((,savepossym (xdr:xdr-pos ,xdr)))
(tagbody
(handler-bind
((illegal-filename-error
(lambda (,c)
(setf (xdr:xdr-pos ,xdr) ,savepossym)
(when debug-this-procedure (logit "Illegal filename~%"))
(ecase (mode ,c)
(:lookup
;; Filename is illegal so it will definitely not exist
(xdr-int ,xdr #.*nfserr-noent*))
(:create
(xdr-int ,xdr #.*nfserr-acces*))) ;; rfc1813 says to use this
(if (= ,vers 3)
(nfs-xdr-wcc-data ,xdr nil nil))
(go ,out)))
(syscall-error
(lambda (,c)
(syscall-error-handler ,c ,xdr ,savepossym ,vers debug-this-procedure)
(go ,out)))
(errno-stream-error
(lambda (,c)
(syscall-error-handler ,c ,xdr ,savepossym ,vers debug-this-procedure)
(go ,out)))
(error
(lambda (,c)
(ignore-errors (log-backtrace-to-nfs-errlog ,c))
(setf (xdr:xdr-pos ,xdr) ,savepossym)
(logit-stamp "Handling unexpected error: ~a~%" ,c)
(xdr-int ,xdr #.*nfserr-io*)
(if (= ,vers 3)
(nfs-xdr-wcc-data ,xdr nil nil))
(go ,out))))
(progn ,@body))
,out))))
(defmacro with-valid-fh ((xdr vers fhs) &body body)
(let ((block (gensym))
(fh (gensym)))
`(block ,block
(dolist (,fh (list ,@fhs))
(case ,fh
(:inval
(when debug-this-procedure (logit " Invalid file handle~%"))
(ecase ,vers
(2
(xdr-int ,xdr #.*nfserr-stale*))
(3
(xdr-int ,xdr #.*nfs3err-badhandle*)
(nfs-xdr-wcc-data ,xdr nil nil)))
(return-from ,block))
(:stale
(when debug-this-procedure (logit " Stale file handle~%"))
(xdr-int ,xdr #.*nfserr-stale*)
(if (= ,vers 3)
(nfs-xdr-wcc-data ,xdr nil nil))
(return-from ,block))))
,@body)))
(defmacro with-allowed-host-access ((vers xdr fh addr) &body body)
`(if* (export-host-access-allowed-p (fh-export ,fh) ,addr)
then
,@body
else
(when debug-this-procedure (logit " Host access denied~%"))
(xdr-int ,xdr #.*nfserr-acces*)
(if (= ,vers 3)
(nfs-xdr-wcc-data ,xdr nil nil))))
;; Returns a timestamp as a float. Note that this value may very well
;; be negative! This is only intended to be used to compare relative
;; times.
(defun get-high-res-time ()
(multiple-value-bind (secs usecs)
(excl::acl-internal-real-time)
(+ secs (* usecs #.(coerce (expt 10.0 -6) 'double-float)))))
(eval-when (compile load eval)
(if (/= 1000 internal-time-units-per-second)
(error "internal-time-units-per-second is not 1000. define-nfs-proc macro will need to be adjusted since it assumes millisecond resolution")))
(defmacro define-nfs-proc (name arglist &body body)
(let ((funcname (intern (format nil "~A-~A" 'nfsd name)))
(first t)
argdefs debugs fhsyms host-access-check-fh debugtype)
(setf debugtype
(let* ((x (symbol-name name))
(y (1- (length x))))
(if (char= (schar x y) #\3)
(setf x (subseq x 0 y)))
(intern x)))
(macrolet ((add-debug (expr)
`(progn
(if (not first)
(push '(logit ", ") debugs))
(push ,expr debugs))))
(dolist (pair arglist)
(ecase (second pair)
(fhandle
(push (first pair) fhsyms)
(push `(,(first pair) (xdr-fhandle params vers)) argdefs)
(add-debug `(logit "~A" (if (fh-p ,(first pair))
(fh-pathname ,(first pair))
,(first pair)))))
(string
(push `(,(first pair) (xdr-string params)) argdefs)
(add-debug `(logit "~A" ,(first pair))))
(filename
(push `(,(first pair) (xdr-string-utf8 params)) argdefs)
(add-debug `(logit "~A" ,(first pair))))
(unsigned
(push `(,(first pair) (xdr-unsigned-int params)) argdefs)
(add-debug `(logit "~D" ,(first pair))))
(access-mask
(push `(,(first pair) (xdr-unsigned-int params)) argdefs)
(add-debug `(logit "~a" (access-mask-to-string ,(first pair)))))
(uint64
(push `(,(first pair) (xdr-unsigned-hyper params)) argdefs)
(add-debug `(logit "~D" ,(first pair))))
(sattr
(push `(,(first pair) (xdr-tweaked-sattr params vers))
argdefs)
(add-debug `(logit "~A" (tweaked-sattr-to-string ,(first pair)))))
(sattrguard3
(push `(,(first pair)
(if (xdr-bool params)
(prog1 (unix-to-universal-time (xdr-unsigned-int params))
(xdr-unsigned-int params))))
argdefs)
(add-debug `(logit "~A" ,(first pair))))
(createhow3
(push `(,(first pair)
(ecase (xdr-unsigned-int params)
(0 ;; unchecked
(list :unchecked (xdr-tweaked-sattr params 3)))
(1 ;; guarded
(list :guarded (xdr-tweaked-sattr params 3)))
(2 ;; exclusive
(list :exclusive (xdr-unsigned-hyper params)))))
argdefs)
(add-debug `(logit "~a, ~a" (first ,(first pair))
(if* (eq (first ,(first pair)) :exclusive)
then (second ,(first pair))
else (tweaked-sattr-to-string
(second ,(first pair)))))))
(data
(push `(,(first pair) (xdr-opaque-variable params)) argdefs)
(add-debug `(logit "<data>"))))
(setf first nil)))
(setf argdefs (reverse argdefs))
(push '(logit ")") debugs)
(setf debugs (reverse debugs))
(setf host-access-check-fh (first fhsyms))
(if (null host-access-check-fh)
(error
"define-nfs-proc: There must be at least one fhandle variable"))
`(defun ,funcname (peer xid params cbody)
(let* ((vers (sunrpc:call-body-vers cbody))
procedure-start-time
debug-this-procedure
,@argdefs)
(when (nfs-debug-filter-on ,debugtype)
(setf debug-this-procedure t)
(logit-stamp "NFSv~d: ~a: ~a("
vers
(sunrpc:peer-dotted peer)
(quote ,name))
,@debugs
(if *nfs-debug-timings*
(setf procedure-start-time (get-high-res-time))))
(sunrpc:with-successful-reply (*nfsdxdr* peer xid sunrpc:*nullverf*)
(with-nfs-err-handler (*nfsdxdr* vers)
(with-valid-fh (*nfsdxdr* vers ,fhsyms)
(with-allowed-host-access (vers *nfsdxdr*
,host-access-check-fh
(sunrpc:rpc-peer-addr peer))
#+nfs-debug
(handler-bind
((error
(lambda (c)
(when *nfs-debug*
(with-standard-io-syntax
(let ((*print-readably* nil)
(*print-miser-width* 40)
(*print-pretty* t)
(*print-structure* nil)
(*print-array* nil)
;; nil makes for better backtraces
(tpl:*zoom-print-circle* nil)
(tpl:*zoom-print-level* nil)
(tpl:*zoom-print-length* nil))
(ignore-errors
(logit "Error: ~a.~%" c))
(ignore-errors ;prevent recursion
(let* ((s *log-stream*)
(*terminal-io* s)
(*standard-output* s))
(tpl:do-command "zoom"
:brief t
:from-read-eval-print-loop nil
:count t :all t)))))))))
,@body)
#-nfs-debug ,@body
(when debug-this-procedure
(when *nfs-debug-timings*
(let* ((end-time (get-high-res-time))
(elapsed-time (- end-time procedure-start-time)))
(logit " ==> ~,3f secs" elapsed-time)))
;; Terminate general debug output
(logit "~%"))))))))))
(defmacro with-permission ((fh type &key op) &body body)
(let ((func (ecase type
(:read 'nfs-okay-to-read)
(:write 'nfs-okay-to-write)))
(failres3 (if* (eq op :link)
then `(progn
(nfs-xdr-post-op-attr *nfsdxdr* nil)
(nfs-xdr-wcc-data *nfsdxdr* nil nil))
else `(nfs-xdr-wcc-data *nfsdxdr* nil nil))))
`(if* (not (,func ,fh (sunrpc:call-body-cred cbody)))
then (if debug-this-procedure (logit " permission denied~%") )
(xdr-int *nfsdxdr* #.*nfserr-acces*)
(if (= vers 3)
,failres3)
else ,@body)))
;; fh must be a non-stale file handle
(defmacro with-dirfh ((fh &key op) &body body)
(let ((attr (gensym "attr"))
(failres3 (if* (eq op :link)
then `(progn
(nfs-xdr-post-op-attr *nfsdxdr* nil)
(nfs-xdr-wcc-data *nfsdxdr* nil nil))
else `(nfs-xdr-wcc-data *nfsdxdr* nil nil))))
`(let ((,attr (lookup-attr ,fh)))
(if* (= (nfs-attr-type ,attr) #.*nfdir*)
then ,@body
else (when debug-this-procedure (logit " Not a directory~%"))
(xdr-int *nfsdxdr* #.*nfserr-notdir*)
(if (= vers 3)
,failres3)))))
;; fh must be a non-stale file handle
(defmacro with-non-dir-fh ((fh &key op) &body body)
(let ((attr (gensym))
(failres3 (if* (eq op :link)
then `(progn
(nfs-xdr-post-op-attr *nfsdxdr* nil)
(nfs-xdr-wcc-data *nfsdxdr* nil nil))
else `(nfs-xdr-wcc-data *nfsdxdr* nil nil))))
`(let ((,attr (lookup-attr ,fh)))
(if* (/= (nfs-attr-type ,attr) #.*nfdir*)
then ,@body
else (when debug-this-procedure (logit " Not allowed to be a directory~%"))
(xdr-int *nfsdxdr* #.*nfserr-perm*)
(if (= vers 3)
,failres3)))))
(defmacro with-same-export ((fh1 fh2 function) &body body)
(let ((failres3
(ecase function
(:rename
`(progn
(nfs-xdr-wcc-data *nfsdxdr* nil nil) ;; from
(nfs-xdr-wcc-data *nfsdxdr* nil nil))) ;; to
(:link
`(progn
(nfs-xdr-post-op-attr *nfsdxdr* nil)
(nfs-xdr-wcc-data *nfsdxdr* nil nil))))))
`(if* (eq (fh-export ,fh1) (fh-export ,fh2))
then ,@body
else(when debug-this-procedure (logit " Not same export~%"))
(ecase vers
(2
(xdr-int *nfsdxdr* #.*nfserr-acces*))
(3
(xdr-int *nfsdxdr* #.*nfserr-xdev*)
,failres3)))))
(defmacro nfs-unsupported ()
`(progn
(when debug-this-procedure (logit " Unsupported~%"))
(ecase vers
(2
(xdr-int *nfsdxdr* #.*nfserr-io*))
(3
(xdr-int *nfsdxdr* #.*nfs3err-notsupp*)
(nfs-xdr-wcc-data *nfsdxdr* nil nil)))))
;; This is good for nfstime and nfstime2 (where the second slot
;; is nanoseconds, not microseconds) because we don't use the
;; second slot.
(defmacro nfstime-to-universal-time (nfstime)
;; We ignore the microseconds slot since universal time only has
;; 1-second resolution.
`(unix-to-universal-time (nfstime-seconds ,nfstime)))
(defun tweak-sattr (s vers)
(ecase vers
(2
(macrolet ((tweak (slot)
`(if (= (,slot s) #xffffffff)
(setf (,slot s) nil)))
(tweak-time (slot)
`(let ((x (,slot s)))
(if* (= (nfstime-seconds x) #xffffffff)
then (setf (,slot s) nil)
else (setf (,slot s)
(nfstime-to-universal-time x))))))
(tweak sattr-mode)
(tweak sattr-uid)
(tweak sattr-gid)
(tweak sattr-size)
(tweak-time sattr-atime)
(tweak-time sattr-mtime)
s))
(3
(macrolet ((getslot (slot)
(let ((reader
(intern (format nil "sattr3-~a" slot)
:gen-nfs))
(set-it
(intern (format nil "set-~a3-set-it" slot)
:gen-nfs))
(reader2
(intern (format nil "set-~a3-~a" slot slot)
:gen-nfs)))
`(let ((x (,reader s)))
(if (,set-it x)
(,reader2 x)))))
(get-time-slot (slot)
(let ((reader
(intern (format nil "sattr3-~a" slot)))
(set-it
(intern (format nil "set-~a-set-it" slot)))
(reader2
(intern (format nil "set-~a-~a" slot slot))))
`(let ((x (,reader s)))
(case (,set-it x)
(#.*set-to-client-time*
(nfstime-to-universal-time (,reader2 x)))
(#.*set-to-server-time*
(get-universal-time))
(t nil))))))
(make-sattr :mode (getslot mode)
:uid (getslot uid)
:gid (getslot gid)
:size (getslot size)
:atime (get-time-slot atime)
:mtime (get-time-slot mtime))))))
(defun xdr-tweaked-sattr (xdr vers)
(ecase vers
(2 (tweak-sattr (xdr-sattr xdr) 2))
(3 (tweak-sattr (xdr-sattr3 xdr) 3))))
(defun tweaked-sattr-to-string (s)
(format nil "[Mode: ~o, uid: ~a, gid: ~a, size: ~a, atime: ~a, mtime: ~a]"
(sattr-mode s) (sattr-uid s) (sattr-gid s) (sattr-size s)
(sattr-atime s) (sattr-mtime s)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; PROCEDURES
(defun nfsd-null (peer xid params cbody)
(declare (ignore params))
(if (nfs-debug-filter-on null)
(logit-stamp "NFSv~d: ~a: NULL~%"
(sunrpc:call-body-vers cbody)
(sunrpc:peer-dotted peer)))
(sunrpc:with-successful-reply (xdr peer xid sunrpc:*nullverf*)
))
(define-nfs-proc getattr ((fh fhandle))
(xdr-int *nfsdxdr* #.*nfs-ok*)
(nfs-xdr-fattr *nfsdxdr* fh vers))
#|
struct fattr {
ftype type; (int)
unsigned int mode;
unsigned int nlink;
unsigned int uid;
unsigned int gid;
unsigned int size;
unsigned int blocksize;
unsigned int rdev;
unsigned int blocks;
unsigned int fsid;
unsigned int fileid;
timeval atime; (uint32 seconds, uint32 microseconds)
timeval mtime;
timeval ctime;
};
|#
#|
struct fattr3 {
ftype3 type; (int)
mode3 mode; (unsigned int)
uint32 nlink; (unsigned int)
uid3 uid; (unsigned int)
gid3 gid; (unsigned int)
size3 size; (uint64)
size3 used; (uint64)
specdata3 rdev; (uint32 specdata1, uint32 specdata2)
uint64 fsid; (uint64)
fileid3 fileid; (uint64)
nfstime3 atime; (uint32 seconds, uint32 nanoseconds)
nfstime3 mtime; (uint32 seconds, uint32 nanoseconds)
nfstime3 ctime; (uint32 seconds, uint32 nanoseconds)
};
|#
(defun nfs-xdr-fattr (xdr fh vers)
(let ((attr (lookup-attr fh))
(exp (fh-export fh)))
(xdr-int xdr (nfs-attr-type attr)) ;; type
(xdr-unsigned-int xdr (logior (logand (nfs-attr-mode attr)
(lognot (nfs-export-umask exp)))
(nfs-export-set-mode-bits exp))) ;; mode
(xdr-unsigned-int xdr (nfs-attr-nlinks attr)) ;;nlinks
(xdr-unsigned-int xdr (nfs-export-uid exp)) ;; uid
(xdr-unsigned-int xdr (nfs-export-gid exp)) ;; gid
(ecase vers
(2 (xdr-unsigned-int xdr (nfs-attr-size attr)) ;; size
(xdr-unsigned-int xdr (nfs-attr-blocksize attr)) ;; blocksize
(xdr-unsigned-int xdr 0) ;; rdev
(xdr-unsigned-int xdr (nfs-attr-blocks attr)) ;;blocks
(xdr-unsigned-int xdr (nfs-attr-fsid attr)) ;;fsid
(xdr-unsigned-int xdr (nfs-attr-fileid attr))) ;;fileid
(3 (xdr-unsigned-hyper xdr (nfs-attr-size attr)) ;; size
(xdr-unsigned-hyper xdr (nfs-attr-used attr)) ;; used
(xdr-unsigned-int xdr 0) (xdr-unsigned-int xdr 0) ;; rdev
(xdr-unsigned-hyper xdr (nfs-attr-fsid attr)) ;; fsid
(xdr-unsigned-hyper xdr (nfs-attr-fileid attr)))) ;; fileid
;; timeval and nfstime3 types are the same in their first part
;; (uint32 seconds), but different in their second part
;; (v2: uint32 microseconds, v3: uint32 nanoseconds). However,
;; we don't have precision better than one second, so, as far
;; as we're concerned, the types are the same since we always
;; put 0 in the second part.
(xdr-unsigned-int xdr (universal-to-unix-time (nfs-attr-atime attr)))
(xdr-int xdr 0)
(xdr-unsigned-int xdr (universal-to-unix-time (nfs-attr-mtime attr)))
(xdr-int xdr 0)
(xdr-unsigned-int xdr (universal-to-unix-time (nfs-attr-ctime attr)))
(xdr-int xdr 0)))
(define-nfs-proc statfs ((fh fhandle))
(with-permission (fh :read)
(multiple-value-bind (non-priv-free priv-free total)
(ignore-errors (unicode-get-filesystem-free-space (fh-pathname fh)))
(if* (null non-priv-free)
then
(when debug-this-procedure (logit " I/O error~%"))
(xdr-int *nfsdxdr* #.*nfserr-io*)
else
;; Convert to "blocks"
(setf non-priv-free (howmany non-priv-free *blocksize*))
(setf priv-free (howmany priv-free *blocksize*))
(setf total (howmany total *blocksize*))
(xdr-int *nfsdxdr* #.*nfs-ok*)
(xdr-unsigned-int *nfsdxdr* 8192)
(xdr-unsigned-int *nfsdxdr* *blocksize*)
(xdr-unsigned-int *nfsdxdr* total)
(xdr-unsigned-int *nfsdxdr* priv-free)
(xdr-unsigned-int *nfsdxdr* non-priv-free)))))
;; v3 only.
;; Allowable errors:
;; NFS3ERR_IO
;; NFS3ERR_STALE
;; NFS3ERR_BADHANDLE
;; NFS3ERR_SERVERFAULT
#|
struct FSSTAT3resok {
post_op_attr obj_attributes;
size3 tbytes;
size3 fbytes;
size3 abytes;
size3 tfiles;
size3 ffiles;
size3 afiles;
uint32 invarsec;
};
|#
(define-nfs-proc fsstat ((fh fhandle))
(multiple-value-bind (non-priv-free priv-free total)
(ignore-errors (unicode-get-filesystem-free-space (fh-pathname fh)))
(if* (null non-priv-free)
then
(when debug-this-procedure (logit " I/O error~%"))
(xdr-int *nfsdxdr* #.*nfserr-io*)
(nfs-xdr-wcc-data *nfsdxdr* nil nil)
else
(xdr-int *nfsdxdr* #.*nfs-ok*)
(nfs-xdr-post-op-attr *nfsdxdr* fh)
(xdr-unsigned-hyper *nfsdxdr* total)
(xdr-unsigned-hyper *nfsdxdr* priv-free)
(xdr-unsigned-hyper *nfsdxdr* non-priv-free)
(xdr-unsigned-hyper *nfsdxdr* -1) ;; tfiles
(xdr-unsigned-hyper *nfsdxdr* -1) ;; ffiles
(xdr-unsigned-hyper *nfsdxdr* -1) ;; afiles
(xdr-unsigned-int *nfsdxdr* 0)))) ;; invarsec
;; v3 only
;; allowable errors:
;; NFS3ERR_STALE
;; NFS3ERR_BADHANDLE
;; NFS3ERR_SERVERFAULT
#|
struct FSINFO3resok {
post_op_attr obj_attributes;
uint32 rtmax;
uint32 rtpref;
uint32 rtmult;
uint32 wtmax;
uint32 wtpref;
uint32 wtmult;
uint32 dtpref;
size3 maxfilesize;
nfstime3 time_delta;
uint32 properties;
};
|#
(define-nfs-proc fsinfo ((fh fhandle))
(xdr-int *nfsdxdr* #.*nfs-ok*)
(nfs-xdr-post-op-attr *nfsdxdr* fh)
(xdr-unsigned-int *nfsdxdr* 65536) ;; rtmax
(xdr-unsigned-int *nfsdxdr* 65536) ;; rtpref
(xdr-unsigned-int *nfsdxdr* 512) ;; rtmult
(xdr-unsigned-int *nfsdxdr* 65536) ;; wtmax
(xdr-unsigned-int *nfsdxdr* 65536) ;; wtpref
(xdr-unsigned-int *nfsdxdr* 512) ;; wtmult
(xdr-unsigned-int *nfsdxdr* 65536) ;; dtpref
(xdr-unsigned-hyper *nfsdxdr* #.(1- (ash 1 63))) ;; maxfilesize
;; time_delta. Indicate that we only keep time to the nearest
;; two seconds which is the case for FAT filesystems. NTFS
;; is 10 milliseconds.
(xdr-unsigned-int *nfsdxdr* 2) (xdr-unsigned-int *nfsdxdr* 0)
;; Can set time on files
;; homogeneous (all files have same pathconf information)
;; hard links supported
;; symbolink links supported
(xdr-unsigned-int *nfsdxdr*
#.(logior *fsf3-link*
*fsf3-symlink*
*fsf3-homogeneous*
*fsf3-cansettime*)))
;;; readdirargs: fhandle dir, cookie, count
(define-nfs-proc readdir ((fh fhandle) (cookie unsigned) (count unsigned))
(with-dirfh (fh)
(with-permission (fh :read)
(add-direntries *nfsdxdr* fh count count cookie 2 nil nil)
(update-attr-atime fh))))
;; cookieverf is 8 bytes of data. We use those 8 bytes as a uint64.
;; Allowable errors:
;; NFS3ERR_IO
;; NFS3ERR_ACCES
;; NFS3ERR_NOTDIR
;; NFS3ERR_BAD_COOKIE
;; NFS3ERR_TOOSMALL
;; NFS3ERR_STALE
;; NFS3ERR_BADHANDLE
;; NFS3ERR_SERVERFAULT
;; RFC183: "count" is the maximum size of the
;; READDIR3resok structure, including all XDR overhead.
(define-nfs-proc readdir3 ((dirfh fhandle)
(cookie uint64)
(cookieverf uint64)
(count unsigned))
(with-dirfh (dirfh)
(with-permission (dirfh :read)
(add-direntries *nfsdxdr* dirfh count count cookie 3 cookieverf nil)
(update-attr-atime dirfh))))
;;; RFC1813:
;;; dircount
;;; The maximum number of bytes of directory information
;;; returned. This number should not include the size of
;;; the attributes and file handle portions of the result.
;;;
;;; maxcount
;;; The maximum size of the READDIRPLUS3resok structure, in
;;; bytes. The size must include all XDR overhead.
(define-nfs-proc readdirplus ((dirfh fhandle)
(cookie uint64)
(cookieverf uint64)
(dircount unsigned)
(maxcount unsigned))
(with-dirfh (dirfh)
(with-permission (dirfh :read)
(add-direntries *nfsdxdr* dirfh dircount maxcount cookie 3 cookieverf t)
(update-attr-atime dirfh))))
;; totalbytesadded starts at 8 because in the minimal case, we need to
;; be able to add the final 0 discriminant.. and the eof indicator
;; DIRMAX is the maximum number of bytes of directory information
;; to return (not including attributes and file handle info).
;; MAXCOUNT is the maximum size of the READDIRPLUS3resok structure, including
;; XDR overhead.
(defun add-direntries (xdr dirfh dirmax maxcount startindex vers verf plus)
(declare (optimize (speed 3)))
;; rfe15117: Some NFS clients using UDP may request a result size
;; that is larger than the max UDP packet size, so trim MAXCOUNT to
;; avoid issues.
(let ((udp-limit (- *max-udp-datagram-size* (sunrpc:get-successful-reply-overhead))))
(setf maxcount (min maxcount udp-limit)))
(multiple-value-bind (dirvec dc)
(nfs-lookup-dir dirfh t)
(declare (simple-vector dirvec))
(let ((index startindex)
(totalbytesadded 8)
(dirbytesadded 0)
(complete t)
(entries 0)
bytesadded
innerbytesadded
endindex
p
debug)
(declare (fixnum entries totalbytesadded dirbytesadded index
bytesadded innerbytesadded))
(when (nfs-debug-filter-on readdir)
(setf debug *nfs-debug*)
(if (eq debug :verbose)
(logit "~%")))
;; HP-UX doesn't do the cookie verifier properly so don't
;; complain if we get a verifier of 0.
(when (and (= vers 3) (/= startindex 0) (/= verf 0)
(/= verf (dircache-id dc))
;; [rfe15123] Handle reversed readdir cookie verifier
(/= (bswap64 verf) (dircache-id dc)))
(when debug (logit " Bad cookie~%"))
(xdr-int xdr #.*nfs3err-bad-cookie*)
(nfs-xdr-post-op-attr xdr dirfh)
(return-from add-direntries))
(xdr-int xdr #.*nfs-ok*)
;; readdirresok begins here. This is what is subject to the
;; 'count' (v2) or 'maxcount' (v3) limit.
(when (= vers 3)
(incf totalbytesadded
(with-xdr-compute-bytes-added (xdr)
(nfs-xdr-post-op-attr xdr dirfh)
(xdr-unsigned-hyper xdr (dircache-id dc)))))
(setf endindex (length dirvec))
(while (< index endindex)
(setf p (aref dirvec index))
(when p ;; some entries can be nil (due to removal by client)
(multiple-value-setq (bytesadded innerbytesadded)
(make-direntry-xdr xdr dirfh p (1+ index) vers plus))
(incf totalbytesadded bytesadded)
(incf dirbytesadded innerbytesadded)
(when (or (> totalbytesadded maxcount)
(> dirbytesadded dirmax))
(if (eq debug :verbose)
(logit " [not added due to size overflow]~%"))
(xdr:xdr-backspace xdr bytesadded)
(setf complete nil)
(return)) ;; break from loop
(incf entries)
(if (eq debug :verbose) (logit " [added]~%")))
(incf index))
;; end while
(xdr-int xdr 0) ;; no more entries
(xdr-bool xdr complete)
(when debug
(logit " ~d entries" entries)
(if complete
(logit " EOF"))))))
#|
struct entry {
unsigned fileid;
filename name;
nfscookie cookie;
entry *nextentry;
};
struct entry3 {
fileid3 fileid; (uint64)
filename3 name;
cookie3 cookie; (uint64)
entry3 *nextentry;
};
struct entryplus3 {
fileid3 fileid;
filename3 name;
cookie3 cookie;
/* Same as entry3 up to this point */
post_op_attr name_attributes;
post_op_fh3 name_handle;
entryplus3 *nextentry;
};
|#
;; returns
;; 1) The number of bytes added to xdr
;; 2) The number of bytes of directory information added.
;; This includes the "entry follows" indicator,
;; the fileid, the filename, and the cookie.
(defun make-direntry-xdr (xdr dirfh filename cookie vers plus)
(let* ((fh (lookup-fh-in-dir dirfh filename
:allow-dotnames t))
(fileid (fh-file-id fh))
inner-bytes-added)
(when (and (nfs-debug-filter-on readdir) (eq *nfs-debug* :verbose))
(logit "~D: ~A fileid=~A next=~A"
(1- cookie) filename fileid cookie))
(values
;; First return value is the result of xdr:with-xdr-compute-bytes-added.
(xdr:with-xdr-compute-bytes-added (xdr)
(setf inner-bytes-added
(xdr:with-xdr-compute-bytes-added (xdr)
(xdr-int xdr 1) ;; indicate that data follows
(ecase vers
(2 (xdr-unsigned-int xdr fileid))
(3 (xdr-unsigned-hyper xdr fileid)))
(xdr-string-utf8 xdr filename)
(ecase vers
(2 (xdr-int xdr cookie))
(3 (xdr-unsigned-hyper xdr cookie)))))
(when plus
(nfs-xdr-post-op-attr xdr fh)
(nfs-xdr-post-op-fh xdr fh)))
;; Second return value
inner-bytes-added)))
;; Like lookup-fh-in-dir but returns nil if no such file or directory.
(defun nfs-probe-file (dirfh filename &key allow-dotnames)
(handler-bind
((syscall-error
(lambda (e)
(when (eq (syscall-error-errno e) *enoent*)
(return-from nfs-probe-file nil)))))
(lookup-fh-in-dir dirfh filename :allow-dotnames allow-dotnames)))
;; v3 allowable errors:
;; NFS3ERR_IO
;; NFS3ERR_NOENT
;; NFS3ERR_ACCES
;; NFS3ERR_NOTDIR
;; NFS3ERR_NAMETOOLONG
;; NFS3ERR_STALE
;; NFS3ERR_BADHANDLE
;; NFS3ERR_SERVERFAULT
;;; v2 returns: status fhandle attributes (fattr)
;; v3 returns: status fhandle, obj post-op attributes, dir post-op attributes
(define-nfs-proc lookup ((dirfh fhandle) (filename filename))
(with-dirfh (dirfh)
(with-permission (dirfh :read)
(let ((fh (lookup-fh-in-dir dirfh filename :allow-dotnames t)))
(xdr-int *nfsdxdr* #.*nfs-ok*)
(xdr-fhandle *nfsdxdr* vers fh)
(ecase vers
(2
(nfs-xdr-fattr *nfsdxdr* fh 2))
(3
(nfs-xdr-post-op-attr *nfsdxdr* fh)
(nfs-xdr-post-op-attr *nfsdxdr* dirfh)))))))
;; v3 only
;; const ACCESS3_READ = 0x0001;
;; const ACCESS3_LOOKUP = 0x0002;
;; const ACCESS3_MODIFY = 0x0004;
;; const ACCESS3_EXTEND = 0x0008;