]>
git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfsm_subs.h
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1989, 1993
31 * The Regents of the University of California. All rights reserved.
33 * This code is derived from software contributed to Berkeley by
34 * Rick Macklem at The University of Guelph.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * @(#)nfsm_subs.h 8.2 (Berkeley) 3/30/95
65 * FreeBSD-Id: nfsm_subs.h,v 1.13 1997/07/16 09:06:30 dfr Exp $
69 #ifndef _NFS_NFSM_SUBS_H_
70 #define _NFS_NFSM_SUBS_H_
72 #include <sys/appleapiopts.h>
74 #ifdef __APPLE_API_PRIVATE
76 * These macros do strange and peculiar things to mbuf chains for
77 * the assistance of the nfs code. To attempt to use them for any
78 * other purpose will be dangerous. (they make weird assumptions)
82 * First define what the actual subs. return
84 int nfsm_reqh(int hsiz
, caddr_t
*bposp
, mbuf_t
*mbp
);
85 int nfsm_rpchead(struct ucred
*cr
, int nmflag
, int procid
,
86 int auth_type
, int auth_len
, char *auth_str
,
87 int verf_len
, char *verf_str
,
88 mbuf_t mrest
, int mrest_len
,
89 mbuf_t
*mbp
, u_long
*xidp
, mbuf_t
*mreqp
);
92 * Now for the macros that do the simple stuff and call the functions
94 * These macros use several vars. declared in nfsm_reqhead and these
95 * vars. must not be used elsewhere unless you are careful not to corrupt
96 * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries
97 * that may be used so long as the value is not expected to retained
99 * I know, this is kind of dorkey, but it makes the actual op functions
100 * fairly clean and deals with the mess caused by the xdr discriminating
104 #define nfsm_build(a,c,s) \
105 { if ((s) > mbuf_trailingspace(mb)) { \
107 __nfsm_error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_DATA, &mb2); \
109 panic("nfsm_build mbuf_get error %d", __nfsm_error); \
110 if ((s) > mbuf_maxlen(mb2)) \
111 panic("nfsm_build size error"); \
112 __nfsm_error = mbuf_setnext(mb, mb2); \
114 panic("nfsm_build mbuf_setnext error %d", __nfsm_error); \
116 bpos = mbuf_data(mb); \
119 mbuf_setlen(mb, (mbuf_len(mb) + (s))); \
122 #define nfsm_dissect(a, c, s) \
123 { t1 = ((caddr_t)mbuf_data(md)) + mbuf_len(md) - dpos; \
127 } else if ((t1 = nfsm_disct(&md, &dpos, (s), t1, &cp2))) { \
135 #define nfsm_fhtom(v, v3) \
137 t2 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED; \
138 if (t2 <= mbuf_trailingspace(mb)) { \
139 nfsm_build(tl, u_long *, t2); \
140 *tl++ = txdr_unsigned(VTONFS(v)->n_fhsize); \
141 *(tl + ((t2>>2) - 2)) = 0; \
142 bcopy((caddr_t)VTONFS(v)->n_fhp,(caddr_t)tl, \
143 VTONFS(v)->n_fhsize); \
144 } else if ((t2 = nfsm_strtmbuf(&mb, &bpos, \
145 (caddr_t)VTONFS(v)->n_fhp, VTONFS(v)->n_fhsize))) { \
151 nfsm_build(cp, caddr_t, NFSX_V2FH); \
152 bcopy((caddr_t)VTONFS(v)->n_fhp, cp, NFSX_V2FH); \
155 #define nfsm_srvfhtom(f, v3) \
157 nfsm_build(tl, u_long *, NFSX_UNSIGNED + (unsigned)(f)->nfh_len); \
158 *tl++ = txdr_unsigned((f)->nfh_len); \
159 bcopy((caddr_t)&(f)->nfh_xh, (caddr_t)tl, (f)->nfh_len); \
161 nfsm_build(cp, caddr_t, NFSX_V2FH); \
162 bcopy((caddr_t)&(f)->nfh_xh, cp, NFSX_V2FH); \
165 #define nfsm_srvpostop_fh(f) \
166 { nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED + (unsigned)(f)->nfh_len); \
168 *tl++ = txdr_unsigned((f)->nfh_len); \
169 bcopy((caddr_t)&(f)->nfh_xh, (caddr_t)tl, (f)->nfh_len); \
172 #define nfsm_mtofh(d, cnp, v, v3, xp, f) \
174 struct nfsnode *ttnp; u_char *ttfhp = NULL; \
175 int ttfhsize = 0, ttgotfh = 1, ttgotattr = 1, ttgotnode = 0; \
176 struct nfs_vattr ttvattr; \
178 /* XXX would be nice to not bail to nfsmout on error */ \
179 if (v3) { /* check for file handle */ \
180 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
181 ttgotfh = fxdr_unsigned(int, *tl); \
184 /* get file handle */ \
185 nfsm_getfh(ttfhp, ttfhsize, (v3)); \
187 if (v3) { /* check for attributes */ \
188 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
189 ttgotattr = fxdr_unsigned(int, *tl); \
191 /* get attributes */ \
194 nfsm_adv(NFSX_V3FATTR); \
196 nfsm_attr_get(v3, &ttvattr); \
198 } else if (ttgotfh) { \
199 /* We need valid attributes in order */ \
200 /* to call nfs_nget/vnode_create(). */ \
201 t1 = nfs_getattr_no_vnode(vnode_mount(d), \
202 ttfhp, ttfhsize, cred, p, &ttvattr, xp); \
206 if (ttgotfh && ttgotattr) { \
207 int ttngflags = NG_MAKEENTRY; \
208 if ((t1 = nfs_nget(vnode_mount(d), d, cnp, ttfhp, ttfhsize, \
209 &ttvattr, xp, ttngflags, &ttnp))) { \
214 (v) = NFSTOV(ttnp); \
220 #define nfsm_getfh(f, s, v3) \
222 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
223 if (((s) = fxdr_unsigned(int, *tl)) <= 0 || \
224 (s) > NFSX_V3FHMAX) { \
232 nfsm_dissect((f), u_char *, nfsm_rndup(s)); }
234 #define nfsm_loadattr(v, v3, a, x) \
235 { struct nfs_vattr ttvattr; \
236 if ((t1 = nfs_parsefattr(&md, &dpos, v3, &ttvattr))) { \
241 if ((t1 = nfs_loadattrcache(VTONFS(v), &ttvattr, (x), 0))) { \
247 bcopy(&ttvattr, (a), sizeof(ttvattr)); \
251 #define nfsm_attr_get(v3, vap) \
253 if ((t1 = nfs_parsefattr(&md, &dpos, v3, vap))) { \
260 #define nfsm_postop_attr_get(v3, f, vap) \
262 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
263 if (((f) = fxdr_unsigned(int, *tl))) { \
264 if ((t1 = nfs_parsefattr(&md, &dpos, v3, vap))) { \
272 #define nfsm_postop_attr_update(v, v3, f, x) \
274 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
275 if (((f) = fxdr_unsigned(int, *tl))) { \
276 struct nfs_vattr ttvattr; \
277 if ((t1 = nfs_parsefattr(&md, &dpos, v3, &ttvattr))) { \
283 if ((t1 = nfs_loadattrcache(VTONFS(v), &ttvattr, (x), 1))) { \
293 #define nfsm_wcc_data(v, premtime, newpostattr, x) \
295 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
296 if (*tl == nfs_true) { \
297 nfsm_dissect(tl, u_long *, 6 * NFSX_UNSIGNED); \
298 (premtime)->tv_sec = fxdr_unsigned(time_t, *(tl + 2)); \
299 (premtime)->tv_nsec = fxdr_unsigned(time_t, *(tl + 3)); \
301 (premtime)->tv_sec = 0; \
302 (premtime)->tv_nsec = 0; \
304 nfsm_postop_attr_update((v), 1, (newpostattr), (x)); \
307 #define nfsm_v3sattr(vap) \
309 struct timeval now; \
310 if (VATTR_IS_ACTIVE(vap, va_mode)) { \
311 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED); \
313 *tl = txdr_unsigned(vap->va_mode); \
315 nfsm_build(tl, u_long *, NFSX_UNSIGNED); \
318 if (VATTR_IS_ACTIVE(vap, va_uid)) { \
319 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED); \
321 *tl = txdr_unsigned(vap->va_uid); \
323 nfsm_build(tl, u_long *, NFSX_UNSIGNED); \
326 if (VATTR_IS_ACTIVE(vap, va_gid)) { \
327 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED); \
329 *tl = txdr_unsigned(vap->va_gid); \
331 nfsm_build(tl, u_long *, NFSX_UNSIGNED); \
334 if (VATTR_IS_ACTIVE(vap, va_data_size)) { \
335 nfsm_build(tl, u_long *, 3 * NFSX_UNSIGNED); \
337 txdr_hyper(&vap->va_data_size, tl); \
339 nfsm_build(tl, u_long *, NFSX_UNSIGNED); \
343 if (VATTR_IS_ACTIVE(vap, va_access_time)) { \
344 if (vap->va_access_time.tv_sec != now.tv_sec) { \
345 nfsm_build(tl, u_long *, 3 * NFSX_UNSIGNED); \
346 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \
347 txdr_nfsv3time(&vap->va_access_time, tl); \
349 nfsm_build(tl, u_long *, NFSX_UNSIGNED); \
350 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); \
353 nfsm_build(tl, u_long *, NFSX_UNSIGNED); \
354 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); \
356 if (VATTR_IS_ACTIVE(vap, va_modify_time)) { \
357 if (vap->va_modify_time.tv_sec != now.tv_sec) { \
358 nfsm_build(tl, u_long *, 3 * NFSX_UNSIGNED); \
359 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \
360 txdr_nfsv3time(&vap->va_modify_time, tl); \
362 nfsm_build(tl, u_long *, NFSX_UNSIGNED); \
363 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); \
366 nfsm_build(tl, u_long *, NFSX_UNSIGNED); \
367 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); \
371 #define nfsm_strsiz(s,m,v3) \
372 { nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \
373 (s) = fxdr_unsigned(long,*tl); \
374 if (!(v3) && ((s) > (m))) { \
380 #define nfsm_srvstrsiz(s,m) \
381 { nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \
382 if (((s) = fxdr_unsigned(long,*tl)) > (m) || (s) <= 0) { \
387 #define nfsm_srvnamesiz(s,v3) \
388 { nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \
389 (s) = fxdr_unsigned(long,*tl); \
390 if (!(v3) && ((s) > NFS_MAXNAMLEN)) \
391 error = NFSERR_NAMETOL; \
398 #define nfsm_mtouio(p,s) \
400 (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos))) { \
406 #define nfsm_uiotom(p,s) \
407 if ((t1 = nfsm_uiotombuf((p),&mb,(s),&bpos))) { \
413 #define nfsm_reqhead(s) \
414 error = nfsm_reqh((s), &bpos, &mreq); \
417 #define nfsm_reqdone mbuf_freem(mrep); \
420 #define nfsm_rndup(a) (((a)+3)&(~0x3))
422 #define nfsm_request(v, t, p, c, x) \
423 if ((error = nfs_request((v), vnode_mount(v), mreq, (t), (p), \
424 (c), &mrep, &md, &dpos, (x)))) { \
425 if (error & NFSERR_RETERR) \
426 error &= ~NFSERR_RETERR; \
431 #define nfsm_strtom(a,s,m,v3) \
432 if (!(v3) && ((s) > (m))) { \
434 error = ENAMETOOLONG; \
437 t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
438 if (t2 <= mbuf_trailingspace(mb)) { \
439 nfsm_build(tl,u_long *,t2); \
440 *tl++ = txdr_unsigned(s); \
441 *(tl+((t2>>2)-2)) = 0; \
442 bcopy((caddr_t)(a), (caddr_t)tl, (s)); \
443 } else if ((t2 = nfsm_strtmbuf(&mb, &bpos, (a), (s)))) { \
449 #define nfsm_srvdone \
453 #define nfsm_reply(s) \
455 nfsd->nd_repstat = error; \
456 if (error && !(nfsd->nd_flag & ND_NFSV3)) \
457 nfs_rephead(0, nfsd, slp, error, mrq, &mb, &bpos); \
459 nfs_rephead((s), nfsd, slp, error, mrq, &mb, &bpos); \
463 if (error && (!(nfsd->nd_flag & ND_NFSV3) || \
464 error == EBADRPC)) { \
470 #define nfsm_writereply(s, v3) \
472 nfsd->nd_repstat = error; \
473 if (error && !(v3)) \
474 nfs_rephead(0, nfsd, slp, error, &mreq, &mb, &bpos); \
476 nfs_rephead((s), nfsd, slp, error, &mreq, &mb, &bpos); \
479 #define nfsm_adv(s) \
480 { t1 = ((caddr_t)mbuf_data(md)) + mbuf_len(md) - dpos; \
483 } else if ((t1 = nfs_adv(&md, &dpos, (s), t1))) { \
489 #define nfsm_srvmtofh(f) \
491 if (nfsd->nd_flag & ND_NFSV3) { \
492 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
493 (f)->nfh_len = fxdr_unsigned(int, *tl); \
494 if (((f)->nfh_len < (int)sizeof(struct nfs_exphandle)) || \
495 ((f)->nfh_len > NFSX_V3FHMAX)) { \
500 (f)->nfh_len = NFSX_V2FH; \
502 nfsm_dissect(tl, u_long *, (f)->nfh_len); \
503 bcopy((caddr_t)tl, (caddr_t)&(f)->nfh_xh, (f)->nfh_len); \
508 int __nfsm_error, __nfsm_len; \
510 mbuf_setlen(mp, mbuf_len(mp) + bp - bpos); \
512 __nfsm_error = mbuf_mclget(MBUF_WAITOK, MBUF_TYPE_DATA, &mp); \
514 panic("nfsm_clget: mbuf_mclget error %d", __nfsm_error); \
515 __nfsm_len = mbuf_maxlen(mp); \
516 mbuf_setlen(mp, __nfsm_len); \
517 __nfsm_error = mbuf_setnext(mp2, mp); \
519 panic("nfsm_clget: mbuf_setnext error %d", __nfsm_error); \
521 bp = mbuf_data(mp); \
522 be = bp + __nfsm_len; \
526 #define nfsm_srv_vattr_init(vap, v3) \
529 VATTR_WANTED((vap), va_type); \
530 VATTR_WANTED((vap), va_mode); \
531 VATTR_WANTED((vap), va_nlink); \
532 VATTR_WANTED((vap), va_uid); \
533 VATTR_WANTED((vap), va_gid); \
534 VATTR_WANTED((vap), va_data_size); \
535 VATTR_WANTED((vap), va_data_alloc); \
536 VATTR_WANTED((vap), va_rdev); \
537 VATTR_WANTED((vap), va_fsid); \
538 VATTR_WANTED((vap), va_fileid); \
539 VATTR_WANTED((vap), va_access_time); \
540 VATTR_WANTED((vap), va_modify_time); \
541 VATTR_WANTED((vap), va_change_time); \
542 if (!v3) VATTR_WANTED((vap), va_iosize); \
545 #define nfsm_srv_pre_vattr_init(vap, v3) \
548 VATTR_WANTED((vap), va_data_size); \
549 VATTR_WANTED((vap), va_modify_time); \
550 VATTR_WANTED((vap), va_change_time); \
553 #define nfsm_srvfillattr(a, f) \
554 nfsm_srvfattr(nfsd, (a), (f))
556 #define nfsm_srvwcc_data(br, b, ar, a) \
557 nfsm_srvwcc(nfsd, (br), (b), (ar), (a), &mb, &bpos)
559 #define nfsm_srvpostop_attr(r, a) \
560 nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos)
562 #define nfsm_srvsattr(a) \
564 struct timespec now; \
565 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
566 if (*tl == nfs_true) { \
567 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
568 VATTR_SET(a, va_mode, nfstov_mode(*tl)); \
570 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
571 if (*tl == nfs_true) { \
572 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
573 VATTR_SET(a, va_uid, fxdr_unsigned(uid_t, *tl)); \
575 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
576 if (*tl == nfs_true) { \
577 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
578 VATTR_SET(a, va_gid, fxdr_unsigned(gid_t, *tl)); \
580 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
581 if (*tl == nfs_true) { \
582 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED); \
583 fxdr_hyper(tl, &(a)->va_data_size); \
584 VATTR_SET_ACTIVE(a, va_data_size); \
586 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
588 switch (fxdr_unsigned(int, *tl)) { \
589 case NFSV3SATTRTIME_TOCLIENT: \
590 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED); \
591 fxdr_nfsv3time(tl, &(a)->va_access_time); \
592 VATTR_SET_ACTIVE(a, va_access_time); \
594 case NFSV3SATTRTIME_TOSERVER: \
595 VATTR_SET(a, va_access_time, now); \
598 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
599 switch (fxdr_unsigned(int, *tl)) { \
600 case NFSV3SATTRTIME_TOCLIENT: \
601 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED); \
602 fxdr_nfsv3time(tl, &(a)->va_modify_time); \
603 VATTR_SET_ACTIVE(a, va_modify_time); \
605 case NFSV3SATTRTIME_TOSERVER: \
606 VATTR_SET(a, va_modify_time, now); \
610 #endif /* __APPLE_API_PRIVATE */
611 #endif /* _NFS_NFSM_SUBS_H_ */