/
nfs_subs.c
995 lines (958 loc) · 22.1 KB
/
nfs_subs.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
/*
* Copyright (c) 1989 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Rick Macklem at The University of Guelph.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* from: @(#)nfs_subs.c 7.41 (Berkeley) 5/15/91
* $Id: nfs_subs.c,v 1.6 1993/07/13 10:40:07 cgd Exp $
*/
/*
* These functions support the macros and help fiddle mbuf chains for
* the nfs op functions. They do things like create the rpc header and
* copy data between mbuf chains and uio lists.
*/
#include "param.h"
#include "proc.h"
#include "filedesc.h"
#include "systm.h"
#include "kernel.h"
#include "mount.h"
#include "file.h"
#include "vnode.h"
#include "namei.h"
#include "mbuf.h"
#include "../ufs/quota.h"
#include "../ufs/inode.h"
#include "rpcv2.h"
#include "nfsv2.h"
#include "nfsnode.h"
#include "nfs.h"
#include "nfsiom.h"
#include "xdr_subs.h"
#include "nfsm_subs.h"
#include "nfscompress.h"
#define TRUE 1
#define FALSE 0
/*
* Data items converted to xdr at startup, since they are constant
* This is kinda hokey, but may save a little time doing byte swaps
*/
u_long nfs_procids[NFS_NPROCS];
u_long nfs_xdrneg1;
u_long rpc_call, rpc_vers, rpc_reply, rpc_msgdenied,
rpc_mismatch, rpc_auth_unix, rpc_msgaccepted;
u_long nfs_vers, nfs_prog, nfs_true, nfs_false;
/* And other global data */
static u_long *rpc_uidp = (u_long *)0;
static u_long nfs_xid = 1;
static char *rpc_unixauth;
extern long hostid;
extern struct proc *nfs_iodwant[NFS_MAXASYNCDAEMON];
extern struct nfsreq nfsreqh;
/* Function ret types */
static char *nfs_unixauth();
/*
* Maximum number of groups passed through to NFS server.
* According to RFC1057 it should be 16.
* For release 3.X systems, the maximum value is 8.
* For some other servers, the maximum value is 10.
*/
int numgrps = 8;
/*
* Create the header for an rpc request packet
* The function nfs_unixauth() creates a unix style authorization string
* and returns a ptr to it.
* The hsiz is the size of the rest of the nfs request header.
* (just used to decide if a cluster is a good idea)
* nb: Note that the prog, vers and procid args are already in xdr byte order
*/
struct mbuf *nfsm_reqh(prog, vers, procid, cred, hsiz, bpos, mb, retxid)
u_long prog;
u_long vers;
u_long procid;
struct ucred *cred;
int hsiz;
caddr_t **bpos;
struct mbuf **mb;
u_long *retxid;
{
register struct mbuf *mreq, *m;
register u_long *tl;
struct mbuf *m1;
char *ap;
int asiz, siz;
static char authnull[4*NFSX_UNSIGNED];
NFSMGETHDR(mreq);
if (cred != NOCRED) {
asiz = ((((cred->cr_ngroups - 1) > numgrps) ? numgrps :
(cred->cr_ngroups - 1)) << 2);
#ifdef FILLINHOST
asiz += nfsm_rndup(hostnamelen)+(9*NFSX_UNSIGNED);
#else
asiz += 9*NFSX_UNSIGNED;
#endif
} else
asiz = 4 * NFSX_UNSIGNED;
/* If we need a lot, alloc a cluster ?? */
if ((asiz+hsiz+RPC_SIZ) > MHLEN)
MCLGET(mreq, M_WAIT);
mreq->m_len = NFSMSIZ(mreq);
siz = mreq->m_len;
m1 = mreq;
/*
* Alloc enough mbufs
* We do it now to avoid all sleeps after the call to nfs_unixauth()
*/
while ((asiz+RPC_SIZ) > siz) {
MGET(m, M_WAIT, MT_DATA);
m1->m_next = m;
m->m_len = MLEN;
siz += MLEN;
m1 = m;
}
tl = mtod(mreq, u_long *);
*tl++ = *retxid = txdr_unsigned(++nfs_xid);
*tl++ = rpc_call;
*tl++ = rpc_vers;
*tl++ = prog;
*tl++ = vers;
*tl++ = procid;
/* Now we can call nfs_unixauth() and copy it in */
if (cred != NOCRED)
ap = nfs_unixauth(cred);
else
ap = authnull;
m = mreq;
siz = m->m_len-RPC_SIZ;
if (asiz <= siz) {
bcopy(ap, (caddr_t)tl, asiz);
m->m_len = asiz+RPC_SIZ;
} else {
bcopy(ap, (caddr_t)tl, siz);
ap += siz;
asiz -= siz;
while (asiz > 0) {
siz = (asiz > MLEN) ? MLEN : asiz;
m = m->m_next;
bcopy(ap, mtod(m, caddr_t), siz);
m->m_len = siz;
asiz -= siz;
ap += siz;
}
}
/* Finally, return values */
*mb = m;
*bpos = mtod(m, caddr_t)+m->m_len;
return (mreq);
}
/*
* copies mbuf chain to the uio scatter/gather list
*/
nfsm_mbuftouio(mrep, uiop, siz, dpos)
struct mbuf **mrep;
register struct uio *uiop;
int siz;
caddr_t *dpos;
{
register char *mbufcp, *uiocp;
register int xfer, left, len;
register struct mbuf *mp;
long uiosiz, rem;
int error = 0;
mp = *mrep;
mbufcp = *dpos;
len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
rem = nfsm_rndup(siz)-siz;
while (siz > 0) {
if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
return (EFBIG);
left = uiop->uio_iov->iov_len;
uiocp = uiop->uio_iov->iov_base;
if (left > siz)
left = siz;
uiosiz = left;
while (left > 0) {
while (len == 0) {
mp = mp->m_next;
if (mp == NULL)
return (EBADRPC);
mbufcp = mtod(mp, caddr_t);
len = mp->m_len;
}
xfer = (left > len) ? len : left;
#ifdef notdef
/* Not Yet.. */
if (uiop->uio_iov->iov_op != NULL)
(*(uiop->uio_iov->iov_op))
(mbufcp, uiocp, xfer);
else
#endif
if (uiop->uio_segflg == UIO_SYSSPACE)
bcopy(mbufcp, uiocp, xfer);
else
copyout(mbufcp, uiocp, xfer);
left -= xfer;
len -= xfer;
mbufcp += xfer;
uiocp += xfer;
uiop->uio_offset += xfer;
uiop->uio_resid -= xfer;
}
if (uiop->uio_iov->iov_len <= siz) {
uiop->uio_iovcnt--;
uiop->uio_iov++;
} else {
uiop->uio_iov->iov_base += uiosiz;
uiop->uio_iov->iov_len -= uiosiz;
}
siz -= uiosiz;
}
*dpos = mbufcp;
*mrep = mp;
if (rem > 0) {
if (len < rem)
error = nfs_adv(mrep, dpos, rem, len);
else
*dpos += rem;
}
return (error);
}
/*
* copies a uio scatter/gather list to an mbuf chain...
*/
nfsm_uiotombuf(uiop, mq, siz, bpos)
register struct uio *uiop;
struct mbuf **mq;
int siz;
caddr_t *bpos;
{
register char *uiocp;
register struct mbuf *mp, *mp2;
register int xfer, left, len;
int uiosiz, clflg, rem;
char *cp;
if (siz > MLEN) /* or should it >= MCLBYTES ?? */
clflg = 1;
else
clflg = 0;
rem = nfsm_rndup(siz)-siz;
mp2 = *mq;
while (siz > 0) {
if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
return (EINVAL);
left = uiop->uio_iov->iov_len;
uiocp = uiop->uio_iov->iov_base;
if (left > siz)
left = siz;
uiosiz = left;
while (left > 0) {
MGET(mp, M_WAIT, MT_DATA);
if (clflg)
MCLGET(mp, M_WAIT);
mp->m_len = NFSMSIZ(mp);
mp2->m_next = mp;
mp2 = mp;
xfer = (left > mp->m_len) ? mp->m_len : left;
#ifdef notdef
/* Not Yet.. */
if (uiop->uio_iov->iov_op != NULL)
(*(uiop->uio_iov->iov_op))
(uiocp, mtod(mp, caddr_t), xfer);
else
#endif
if (uiop->uio_segflg == UIO_SYSSPACE)
bcopy(uiocp, mtod(mp, caddr_t), xfer);
else
copyin(uiocp, mtod(mp, caddr_t), xfer);
len = mp->m_len;
mp->m_len = xfer;
left -= xfer;
uiocp += xfer;
uiop->uio_offset += xfer;
uiop->uio_resid -= xfer;
}
if (uiop->uio_iov->iov_len <= siz) {
uiop->uio_iovcnt--;
uiop->uio_iov++;
} else {
uiop->uio_iov->iov_base += uiosiz;
uiop->uio_iov->iov_len -= uiosiz;
}
siz -= uiosiz;
}
if (rem > 0) {
if (rem > (len-mp->m_len)) {
MGET(mp, M_WAIT, MT_DATA);
mp->m_len = 0;
mp2->m_next = mp;
}
cp = mtod(mp, caddr_t)+mp->m_len;
for (left = 0; left < rem; left++)
*cp++ = '\0';
mp->m_len += rem;
*bpos = cp;
} else
*bpos = mtod(mp, caddr_t)+mp->m_len;
*mq = mp;
return (0);
}
/*
* Help break down an mbuf chain by setting the first siz bytes contiguous
* pointed to by returned val.
* If Updateflg == True we can overwrite the first part of the mbuf data
* This is used by the macros nfsm_disect and nfsm_disecton for tough
* cases. (The macros use the vars. dpos and dpos2)
*/
nfsm_disct(mdp, dposp, siz, left, updateflg, cp2)
struct mbuf **mdp;
caddr_t *dposp;
int siz;
int left;
int updateflg;
caddr_t *cp2;
{
register struct mbuf *mp, *mp2;
register int siz2, xfer;
register caddr_t tl;
mp = *mdp;
while (left == 0) {
*mdp = mp = mp->m_next;
if (mp == NULL)
return (EBADRPC);
left = mp->m_len;
*dposp = mtod(mp, caddr_t);
}
if (left >= siz) {
*cp2 = *dposp;
*dposp += siz;
} else if (mp->m_next == NULL) {
return (EBADRPC);
} else if (siz > MHLEN) {
panic("nfs S too big");
} else {
/* Iff update, you can overwrite, else must alloc new mbuf */
if (updateflg) {
NFSMINOFF(mp);
} else {
MGET(mp2, M_WAIT, MT_DATA);
mp2->m_next = mp->m_next;
mp->m_next = mp2;
mp->m_len -= left;
mp = mp2;
}
*cp2 = tl = mtod(mp, caddr_t);
bcopy(*dposp, tl, left); /* Copy what was left */
siz2 = siz-left;
tl += left;
mp2 = mp->m_next;
/* Loop around copying up the siz2 bytes */
while (siz2 > 0) {
if (mp2 == NULL)
return (EBADRPC);
xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
if (xfer > 0) {
bcopy(mtod(mp2, caddr_t), tl, xfer);
NFSMADV(mp2, xfer);
mp2->m_len -= xfer;
tl += xfer;
siz2 -= xfer;
}
if (siz2 > 0)
mp2 = mp2->m_next;
}
mp->m_len = siz;
*mdp = mp2;
*dposp = mtod(mp2, caddr_t);
}
return (0);
}
/*
* Advance the position in the mbuf chain.
*/
nfs_adv(mdp, dposp, offs, left)
struct mbuf **mdp;
caddr_t *dposp;
int offs;
int left;
{
register struct mbuf *m;
register int s;
m = *mdp;
s = left;
while (s < offs) {
offs -= s;
m = m->m_next;
if (m == NULL)
return (EBADRPC);
s = m->m_len;
}
*mdp = m;
*dposp = mtod(m, caddr_t)+offs;
return (0);
}
/*
* Copy a string into mbufs for the hard cases...
*/
nfsm_strtmbuf(mb, bpos, cp, siz)
struct mbuf **mb;
char **bpos;
char *cp;
long siz;
{
register struct mbuf *m1, *m2;
long left, xfer, len, tlen;
u_long *tl;
int putsize;
putsize = 1;
m2 = *mb;
left = NFSMSIZ(m2)-m2->m_len;
if (left > 0) {
tl = ((u_long *)(*bpos));
*tl++ = txdr_unsigned(siz);
putsize = 0;
left -= NFSX_UNSIGNED;
m2->m_len += NFSX_UNSIGNED;
if (left > 0) {
bcopy(cp, (caddr_t) tl, left);
siz -= left;
cp += left;
m2->m_len += left;
left = 0;
}
}
/* Loop arround adding mbufs */
while (siz > 0) {
MGET(m1, M_WAIT, MT_DATA);
if (siz > MLEN)
MCLGET(m1, M_WAIT);
m1->m_len = NFSMSIZ(m1);
m2->m_next = m1;
m2 = m1;
tl = mtod(m1, u_long *);
tlen = 0;
if (putsize) {
*tl++ = txdr_unsigned(siz);
m1->m_len -= NFSX_UNSIGNED;
tlen = NFSX_UNSIGNED;
putsize = 0;
}
if (siz < m1->m_len) {
len = nfsm_rndup(siz);
xfer = siz;
if (xfer < len)
*(tl+(xfer>>2)) = 0;
} else {
xfer = len = m1->m_len;
}
bcopy(cp, (caddr_t) tl, xfer);
m1->m_len = len+tlen;
siz -= xfer;
cp += xfer;
}
*mb = m1;
*bpos = mtod(m1, caddr_t)+m1->m_len;
return (0);
}
/*
* Called once to initialize data structures...
*/
nfs_init()
{
register int i;
rpc_vers = txdr_unsigned(RPC_VER2);
rpc_call = txdr_unsigned(RPC_CALL);
rpc_reply = txdr_unsigned(RPC_REPLY);
rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
nfs_vers = txdr_unsigned(NFS_VER2);
nfs_prog = txdr_unsigned(NFS_PROG);
nfs_true = txdr_unsigned(TRUE);
nfs_false = txdr_unsigned(FALSE);
/* Loop thru nfs procids */
for (i = 0; i < NFS_NPROCS; i++)
nfs_procids[i] = txdr_unsigned(i);
/* Ensure async daemons disabled */
nfs_xdrneg1 = txdr_unsigned(-1);
#ifdef NFSCLIENT
for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
nfs_iodwant[i] = (struct proc *)0;
nfs_nhinit(); /* Init the nfsnode table */
#endif /* NFSCLIENT */
#ifdef NFSSERVER
nfsrv_initcache(); /* Init the server request cache */
#endif /*NFSSERVER */
/*
* Initialize reply list and start timer
*/
nfsreqh.r_prev = nfsreqh.r_next = &nfsreqh;
nfs_timer();
}
/*
* Fill in the rest of the rpc_unixauth and return it
*/
static char *nfs_unixauth(cr)
register struct ucred *cr;
{
register u_long *tl;
register int i;
int ngr;
/* Maybe someday there should be a cache of AUTH_SHORT's */
if ((tl = rpc_uidp) == NULL) {
#ifdef FILLINHOST
i = nfsm_rndup(hostnamelen)+(25*NFSX_UNSIGNED);
#else
i = 25*NFSX_UNSIGNED;
#endif
MALLOC(tl, u_long *, i, M_TEMP, M_WAITOK);
bzero((caddr_t)tl, i);
rpc_unixauth = (caddr_t)tl;
*tl++ = txdr_unsigned(RPCAUTH_UNIX);
tl++; /* Fill in size later */
*tl++ = hostid;
#ifdef FILLINHOST
*tl++ = txdr_unsigned(hostnamelen);
i = nfsm_rndup(hostnamelen);
bcopy(hostname, (caddr_t)tl, hostnamelen);
tl += (i>>2);
#else
*tl++ = 0;
#endif
rpc_uidp = tl;
}
*tl++ = txdr_unsigned(cr->cr_uid);
*tl++ = txdr_unsigned(cr->cr_groups[0]);
ngr = ((cr->cr_ngroups - 1) > numgrps) ? numgrps : (cr->cr_ngroups - 1);
*tl++ = txdr_unsigned(ngr);
for (i = 1; i <= ngr; i++)
*tl++ = txdr_unsigned(cr->cr_groups[i]);
/* And add the AUTH_NULL */
*tl++ = 0;
*tl = 0;
i = (((caddr_t)tl)-rpc_unixauth)-12;
tl = (u_long *)(rpc_unixauth+4);
*tl = txdr_unsigned(i);
return (rpc_unixauth);
}
/*
* Set up nameidata for a namei() call and do it
*/
nfs_namei(ndp, fhp, len, mdp, dposp, p)
register struct nameidata *ndp;
fhandle_t *fhp;
int len;
struct mbuf **mdp;
caddr_t *dposp;
struct proc *p;
{
register int i, rem;
register struct mbuf *md;
register char *fromcp, *tocp;
struct vnode *dp;
int flag;
int error;
flag = ndp->ni_nameiop & OPMASK;
MALLOC(ndp->ni_pnbuf, char *, len + 1, M_NAMEI, M_WAITOK);
/*
* Copy the name from the mbuf list to ndp->ni_pnbuf
* and set the various ndp fields appropriately.
*/
fromcp = *dposp;
tocp = ndp->ni_pnbuf;
md = *mdp;
rem = mtod(md, caddr_t) + md->m_len - fromcp;
ndp->ni_hash = 0;
for (i = 0; i < len; i++) {
while (rem == 0) {
md = md->m_next;
if (md == NULL) {
error = EBADRPC;
goto out;
}
fromcp = mtod(md, caddr_t);
rem = md->m_len;
}
if (*fromcp == '\0' || *fromcp == '/') {
error = EINVAL;
goto out;
}
if (*fromcp & 0200)
if ((*fromcp&0377) == ('/'|0200) || flag != DELETE) {
error = EINVAL;
goto out;
}
ndp->ni_hash += (unsigned char)*fromcp;
*tocp++ = *fromcp++;
rem--;
}
*tocp = '\0';
*mdp = md;
*dposp = fromcp;
len = nfsm_rndup(len)-len;
if (len > 0) {
if (rem >= len)
*dposp += len;
else if (error = nfs_adv(mdp, dposp, len, rem))
goto out;
}
ndp->ni_pathlen = tocp - ndp->ni_pnbuf;
ndp->ni_ptr = ndp->ni_pnbuf;
/*
* Extract and set starting directory.
*/
if (error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cred))
goto out;
if (dp->v_type != VDIR) {
vrele(dp);
error = ENOTDIR;
goto out;
}
ndp->ni_startdir = dp;
ndp->ni_nameiop |= (NOCROSSMOUNT | REMOTE);
/*
* And call lookup() to do the real work
*/
if (error = lookup(ndp, p))
goto out;
/*
* Check for encountering a symbolic link
*/
if (ndp->ni_more) {
if ((ndp->ni_nameiop & LOCKPARENT) && ndp->ni_pathlen == 1)
vput(ndp->ni_dvp);
else
vrele(ndp->ni_dvp);
vput(ndp->ni_vp);
ndp->ni_vp = NULL;
error = EINVAL;
goto out;
}
/*
* Check for saved name request
*/
if (ndp->ni_nameiop & (SAVENAME | SAVESTART)) {
ndp->ni_nameiop |= HASBUF;
return (0);
}
out:
FREE(ndp->ni_pnbuf, M_NAMEI);
return (error);
}
/*
* A fiddled version of m_adj() that ensures null fill to a long
* boundary and only trims off the back end
*/
nfsm_adj(mp, len, nul)
struct mbuf *mp;
register int len;
int nul;
{
register struct mbuf *m;
register int count, i;
register char *cp;
/*
* Trim from tail. Scan the mbuf chain,
* calculating its length and finding the last mbuf.
* If the adjustment only affects this mbuf, then just
* adjust and return. Otherwise, rescan and truncate
* after the remaining size.
*/
count = 0;
m = mp;
for (;;) {
count += m->m_len;
if (m->m_next == (struct mbuf *)0)
break;
m = m->m_next;
}
if (m->m_len > len) {
m->m_len -= len;
if (nul > 0) {
cp = mtod(m, caddr_t)+m->m_len-nul;
for (i = 0; i < nul; i++)
*cp++ = '\0';
}
return;
}
count -= len;
if (count < 0)
count = 0;
/*
* Correct length for chain is "count".
* Find the mbuf with last data, adjust its length,
* and toss data from remaining mbufs on chain.
*/
for (m = mp; m; m = m->m_next) {
if (m->m_len >= count) {
m->m_len = count;
if (nul > 0) {
cp = mtod(m, caddr_t)+m->m_len-nul;
for (i = 0; i < nul; i++)
*cp++ = '\0';
}
break;
}
count -= m->m_len;
}
while (m = m->m_next)
m->m_len = 0;
}
/*
* nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
* - look up fsid in mount list (if not found ret error)
* - check that it is exported
* - get vp by calling VFS_FHTOVP() macro
* - if not lockflag unlock it with VOP_UNLOCK()
* - if cred->cr_uid == 0 set it to m_exroot
*/
nfsrv_fhtovp(fhp, lockflag, vpp, cred)
fhandle_t *fhp;
int lockflag;
struct vnode **vpp;
struct ucred *cred;
{
register struct mount *mp;
if ((mp = getvfs(&fhp->fh_fsid)) == NULL)
return (ESTALE);
if ((mp->mnt_flag & MNT_EXPORTED) == 0)
return (EACCES);
if (VFS_FHTOVP(mp, &fhp->fh_fid, vpp))
return (ESTALE);
if (cred->cr_uid == 0)
cred->cr_uid = mp->mnt_exroot;
if (!lockflag)
VOP_UNLOCK(*vpp);
return (0);
}
/*
* These two functions implement nfs rpc compression.
* The algorithm is a trivial run length encoding of '\0' bytes. The high
* order nibble of hex "e" is or'd with the number of zeroes - 2 in four
* bits. (2 - 17 zeros) Any data byte with a high order nibble of hex "e"
* is byte stuffed.
* The compressed data is padded with 0x0 bytes to an even multiple of
* 4 bytes in length to avoid any weird long pointer alignments.
* If compression/uncompression is unsuccessful, the original mbuf list
* is returned.
* The first four bytes (the XID) are left uncompressed and the fifth
* byte is set to 0x1 for request and 0x2 for reply.
* An uncompressed RPC will always have the fifth byte == 0x0.
*/
struct mbuf *
nfs_compress(m0)
struct mbuf *m0;
{
register u_char ch, nextch;
register int i, rlelast;
register u_char *ip, *op;
register int ileft, oleft, noteof;
register struct mbuf *m, *om;
struct mbuf **mp, *retm;
int olen, clget;
i = rlelast = 0;
noteof = 1;
m = m0;
if (m->m_len < 12)
return (m0);
if (m->m_pkthdr.len >= MINCLSIZE)
clget = 1;
else
clget = 0;
ileft = m->m_len - 9;
ip = mtod(m, u_char *);
MGETHDR(om, M_WAIT, MT_DATA);
if (clget)
MCLGET(om, M_WAIT);
retm = om;
mp = &om->m_next;
olen = om->m_len = 5;
oleft = M_TRAILINGSPACE(om);
op = mtod(om, u_char *);
*((u_long *)op) = *((u_long *)ip);
ip += 7;
op += 4;
*op++ = *ip++ + 1;
nextch = *ip++;
while (noteof) {
ch = nextch;
if (ileft == 0) {
do {
m = m->m_next;
} while (m && m->m_len == 0);
if (m) {
ileft = m->m_len;
ip = mtod(m, u_char *);
} else {
noteof = 0;
nextch = 0x1;
goto doit;
}
}
nextch = *ip++;
ileft--;
doit:
if (ch == '\0') {
if (++i == NFSC_MAX || nextch != '\0') {
if (i < 2) {
nfscput('\0');
} else {
if (rlelast == i) {
nfscput('\0');
i--;
}
if (NFSCRLE(i) == (nextch & 0xff)) {
i--;
if (i < 2) {
nfscput('\0');
} else {
nfscput(NFSCRLE(i));
}
nfscput('\0');
rlelast = 0;
} else {
nfscput(NFSCRLE(i));
rlelast = i;
}
}
i = 0;
}
} else {
if ((ch & NFSCRL) == NFSCRL) {
nfscput(ch);
}
nfscput(ch);
i = rlelast = 0;
}
}
if (olen < m0->m_pkthdr.len) {
m_freem(m0);
if (i = (olen & 0x3)) {
i = 4 - i;
while (i-- > 0) {
nfscput('\0');
}
}
retm->m_pkthdr.len = olen;
retm->m_pkthdr.rcvif = (struct ifnet *)0;
return (retm);
} else {
m_freem(retm);
return (m0);
}
}
struct mbuf *
nfs_uncompress(m0)
struct mbuf *m0;
{
register u_char cp, nextcp, *ip, *op;
register struct mbuf *m, *om;
struct mbuf *retm, **mp;
int i, j, noteof, clget, ileft, oleft, olen;
m = m0;
i = 0;
while (m && i < MINCLSIZE) {
i += m->m_len;
m = m->m_next;
}
if (i < 6)
return (m0);
if (i >= MINCLSIZE)
clget = 1;
else
clget = 0;
m = m0;
MGET(om, M_WAIT, MT_DATA);
if (clget)
MCLGET(om, M_WAIT);
olen = om->m_len = 8;
oleft = M_TRAILINGSPACE(om);
op = mtod(om, u_char *);
retm = om;
mp = &om->m_next;
if (m->m_len >= 6) {
ileft = m->m_len - 6;
ip = mtod(m, u_char *);
*((u_long *)op) = *((u_long *)ip);
bzero(op + 4, 3);
ip += 4;
op += 7;
if (*ip == '\0') {
m_freem(om);
return (m0);
}
*op++ = *ip++ - 1;
cp = *ip++;
} else {
ileft = m->m_len;
ip = mtod(m, u_char *);
nfscget(*op++);
nfscget(*op++);
nfscget(*op++);
nfscget(*op++);
bzero(op, 3);
op += 3;
nfscget(*op);
if (*op == '\0') {
m_freem(om);
return (m0);
}
(*op)--;
op++;
nfscget(cp);
}
noteof = 1;
while (noteof) {
if ((cp & NFSCRL) == NFSCRL) {
nfscget(nextcp);
if (cp == nextcp) {
nfscput(cp);
goto readit;
} else {
i = (cp & 0xf) + 2;
for (j = 0; j < i; j++) {
nfscput('\0');
}
cp = nextcp;
}
} else {
nfscput(cp);
readit:
nfscget(cp);
}
}
m_freem(m0);
if (i = (olen & 0x3))
om->m_len -= i;
return (retm);
}