]>
git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfsm_subs.h
d61466396709a411f832f9e61b97f1f117ed0baf
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
25 * Copyright (c) 1989, 1993
26 * The Regents of the University of California. All rights reserved.
28 * This code is derived from software contributed to Berkeley by
29 * Rick Macklem at The University of Guelph.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * @(#)nfsm_subs.h 8.2 (Berkeley) 3/30/95
60 * FreeBSD-Id: nfsm_subs.h,v 1.13 1997/07/16 09:06:30 dfr Exp $
64 #ifndef _NFS_NFSM_SUBS_H_
65 #define _NFS_NFSM_SUBS_H_
67 #include <sys/appleapiopts.h>
69 #ifdef __APPLE_API_PRIVATE
71 * These macros do strange and peculiar things to mbuf chains for
72 * the assistance of the nfs code. To attempt to use them for any
73 * other purpose will be dangerous. (they make weird assumptions)
77 * First define what the actual subs. return
79 int nfsm_reqh(int hsiz
, caddr_t
*bposp
, mbuf_t
*mbp
);
80 int nfsm_rpchead(struct ucred
*cr
, int nmflag
, int procid
,
81 int auth_type
, int auth_len
, char *auth_str
,
82 int verf_len
, char *verf_str
,
83 mbuf_t mrest
, int mrest_len
,
84 mbuf_t
*mbp
, u_long
*xidp
, mbuf_t
*mreqp
);
87 * Now for the macros that do the simple stuff and call the functions
89 * These macros use several vars. declared in nfsm_reqhead and these
90 * vars. must not be used elsewhere unless you are careful not to corrupt
91 * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries
92 * that may be used so long as the value is not expected to retained
94 * I know, this is kind of dorkey, but it makes the actual op functions
95 * fairly clean and deals with the mess caused by the xdr discriminating
99 #define nfsm_build(a,c,s) \
100 { if ((s) > mbuf_trailingspace(mb)) { \
102 __nfsm_error = mbuf_get(MBUF_WAITOK, MBUF_TYPE_DATA, &mb2); \
104 panic("nfsm_build mbuf_get error %d", __nfsm_error); \
105 if ((s) > mbuf_maxlen(mb2)) \
106 panic("nfsm_build size error"); \
107 __nfsm_error = mbuf_setnext(mb, mb2); \
109 panic("nfsm_build mbuf_setnext error %d", __nfsm_error); \
111 bpos = mbuf_data(mb); \
114 mbuf_setlen(mb, (mbuf_len(mb) + (s))); \
117 #define nfsm_dissect(a, c, s) \
118 { t1 = ((caddr_t)mbuf_data(md)) + mbuf_len(md) - dpos; \
122 } else if ((t1 = nfsm_disct(&md, &dpos, (s), t1, &cp2))) { \
130 #define nfsm_fhtom(v, v3) \
132 t2 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED; \
133 if (t2 <= mbuf_trailingspace(mb)) { \
134 nfsm_build(tl, u_long *, t2); \
135 *tl++ = txdr_unsigned(VTONFS(v)->n_fhsize); \
136 *(tl + ((t2>>2) - 2)) = 0; \
137 bcopy((caddr_t)VTONFS(v)->n_fhp,(caddr_t)tl, \
138 VTONFS(v)->n_fhsize); \
139 } else if ((t2 = nfsm_strtmbuf(&mb, &bpos, \
140 (caddr_t)VTONFS(v)->n_fhp, VTONFS(v)->n_fhsize))) { \
146 nfsm_build(cp, caddr_t, NFSX_V2FH); \
147 bcopy((caddr_t)VTONFS(v)->n_fhp, cp, NFSX_V2FH); \
150 #define nfsm_srvfhtom(f, v3) \
152 nfsm_build(tl, u_long *, NFSX_UNSIGNED + (unsigned)(f)->nfh_len); \
153 *tl++ = txdr_unsigned((f)->nfh_len); \
154 bcopy((caddr_t)&(f)->nfh_xh, (caddr_t)tl, (f)->nfh_len); \
156 nfsm_build(cp, caddr_t, NFSX_V2FH); \
157 bcopy((caddr_t)&(f)->nfh_xh, cp, NFSX_V2FH); \
160 #define nfsm_srvpostop_fh(f) \
161 { nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED + (unsigned)(f)->nfh_len); \
163 *tl++ = txdr_unsigned((f)->nfh_len); \
164 bcopy((caddr_t)&(f)->nfh_xh, (caddr_t)tl, (f)->nfh_len); \
167 #define nfsm_mtofh(d, cnp, v, v3, xp, f) \
169 struct nfsnode *ttnp; u_char *ttfhp = NULL; \
170 int ttfhsize = 0, ttgotfh = 1, ttgotattr = 1, ttgotnode = 0; \
171 struct nfs_vattr ttvattr; \
173 /* XXX would be nice to not bail to nfsmout on error */ \
174 if (v3) { /* check for file handle */ \
175 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
176 ttgotfh = fxdr_unsigned(int, *tl); \
179 /* get file handle */ \
180 nfsm_getfh(ttfhp, ttfhsize, (v3)); \
182 if (v3) { /* check for attributes */ \
183 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
184 ttgotattr = fxdr_unsigned(int, *tl); \
186 /* get attributes */ \
189 nfsm_adv(NFSX_V3FATTR); \
191 nfsm_attr_get(v3, &ttvattr); \
193 } else if (ttgotfh) { \
194 /* We need valid attributes in order */ \
195 /* to call nfs_nget/vnode_create(). */ \
196 t1 = nfs_getattr_no_vnode(vnode_mount(d), \
197 ttfhp, ttfhsize, cred, p, &ttvattr, xp); \
201 if (ttgotfh && ttgotattr) { \
202 int ttngflags = NG_MAKEENTRY; \
203 if ((t1 = nfs_nget(vnode_mount(d), d, cnp, ttfhp, ttfhsize, \
204 &ttvattr, xp, ttngflags, &ttnp))) { \
209 (v) = NFSTOV(ttnp); \
215 #define nfsm_getfh(f, s, v3) \
217 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
218 if (((s) = fxdr_unsigned(int, *tl)) <= 0 || \
219 (s) > NFSX_V3FHMAX) { \
227 nfsm_dissect((f), u_char *, nfsm_rndup(s)); }
229 #define nfsm_loadattr(v, v3, a, x) \
230 { struct nfs_vattr ttvattr; \
231 if ((t1 = nfs_parsefattr(&md, &dpos, v3, &ttvattr))) { \
236 if ((t1 = nfs_loadattrcache(VTONFS(v), &ttvattr, (x), 0))) { \
242 bcopy(&ttvattr, (a), sizeof(ttvattr)); \
246 #define nfsm_attr_get(v3, vap) \
248 if ((t1 = nfs_parsefattr(&md, &dpos, v3, vap))) { \
255 #define nfsm_postop_attr_get(v3, f, vap) \
257 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
258 if (((f) = fxdr_unsigned(int, *tl))) { \
259 if ((t1 = nfs_parsefattr(&md, &dpos, v3, vap))) { \
267 #define nfsm_postop_attr_update(v, v3, f, x) \
269 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
270 if (((f) = fxdr_unsigned(int, *tl))) { \
271 struct nfs_vattr ttvattr; \
272 if ((t1 = nfs_parsefattr(&md, &dpos, v3, &ttvattr))) { \
278 if ((t1 = nfs_loadattrcache(VTONFS(v), &ttvattr, (x), 1))) { \
288 #define nfsm_wcc_data(v, premtime, newpostattr, x) \
290 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
291 if (*tl == nfs_true) { \
292 nfsm_dissect(tl, u_long *, 6 * NFSX_UNSIGNED); \
293 (premtime)->tv_sec = fxdr_unsigned(time_t, *(tl + 2)); \
294 (premtime)->tv_nsec = fxdr_unsigned(time_t, *(tl + 3)); \
296 (premtime)->tv_sec = 0; \
297 (premtime)->tv_nsec = 0; \
299 nfsm_postop_attr_update((v), 1, (newpostattr), (x)); \
302 #define nfsm_v3sattr(vap) \
304 struct timeval now; \
305 if (VATTR_IS_ACTIVE(vap, va_mode)) { \
306 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED); \
308 *tl = txdr_unsigned(vap->va_mode); \
310 nfsm_build(tl, u_long *, NFSX_UNSIGNED); \
313 if (VATTR_IS_ACTIVE(vap, va_uid)) { \
314 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED); \
316 *tl = txdr_unsigned(vap->va_uid); \
318 nfsm_build(tl, u_long *, NFSX_UNSIGNED); \
321 if (VATTR_IS_ACTIVE(vap, va_gid)) { \
322 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED); \
324 *tl = txdr_unsigned(vap->va_gid); \
326 nfsm_build(tl, u_long *, NFSX_UNSIGNED); \
329 if (VATTR_IS_ACTIVE(vap, va_data_size)) { \
330 nfsm_build(tl, u_long *, 3 * NFSX_UNSIGNED); \
332 txdr_hyper(&vap->va_data_size, tl); \
334 nfsm_build(tl, u_long *, NFSX_UNSIGNED); \
338 if (VATTR_IS_ACTIVE(vap, va_access_time)) { \
339 if (vap->va_access_time.tv_sec != now.tv_sec) { \
340 nfsm_build(tl, u_long *, 3 * NFSX_UNSIGNED); \
341 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \
342 txdr_nfsv3time(&vap->va_access_time, tl); \
344 nfsm_build(tl, u_long *, NFSX_UNSIGNED); \
345 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); \
348 nfsm_build(tl, u_long *, NFSX_UNSIGNED); \
349 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); \
351 if (VATTR_IS_ACTIVE(vap, va_modify_time)) { \
352 if (vap->va_modify_time.tv_sec != now.tv_sec) { \
353 nfsm_build(tl, u_long *, 3 * NFSX_UNSIGNED); \
354 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \
355 txdr_nfsv3time(&vap->va_modify_time, tl); \
357 nfsm_build(tl, u_long *, NFSX_UNSIGNED); \
358 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); \
361 nfsm_build(tl, u_long *, NFSX_UNSIGNED); \
362 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); \
366 #define nfsm_strsiz(s,m,v3) \
367 { nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \
368 (s) = fxdr_unsigned(long,*tl); \
369 if (!(v3) && ((s) > (m))) { \
375 #define nfsm_srvstrsiz(s,m) \
376 { nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \
377 if (((s) = fxdr_unsigned(long,*tl)) > (m) || (s) <= 0) { \
382 #define nfsm_srvnamesiz(s,v3) \
383 { nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \
384 (s) = fxdr_unsigned(long,*tl); \
385 if (!(v3) && ((s) > NFS_MAXNAMLEN)) \
386 error = NFSERR_NAMETOL; \
393 #define nfsm_mtouio(p,s) \
395 (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos))) { \
401 #define nfsm_uiotom(p,s) \
402 if ((t1 = nfsm_uiotombuf((p),&mb,(s),&bpos))) { \
408 #define nfsm_reqhead(s) \
409 error = nfsm_reqh((s), &bpos, &mreq); \
412 #define nfsm_reqdone mbuf_freem(mrep); \
415 #define nfsm_rndup(a) (((a)+3)&(~0x3))
417 #define nfsm_request(v, t, p, c, x) \
418 if ((error = nfs_request((v), vnode_mount(v), mreq, (t), (p), \
419 (c), &mrep, &md, &dpos, (x)))) { \
420 if (error & NFSERR_RETERR) \
421 error &= ~NFSERR_RETERR; \
426 #define nfsm_strtom(a,s,m,v3) \
427 if (!(v3) && ((s) > (m))) { \
429 error = ENAMETOOLONG; \
432 t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
433 if (t2 <= mbuf_trailingspace(mb)) { \
434 nfsm_build(tl,u_long *,t2); \
435 *tl++ = txdr_unsigned(s); \
436 *(tl+((t2>>2)-2)) = 0; \
437 bcopy((caddr_t)(a), (caddr_t)tl, (s)); \
438 } else if ((t2 = nfsm_strtmbuf(&mb, &bpos, (a), (s)))) { \
444 #define nfsm_srvdone \
448 #define nfsm_reply(s) \
450 nfsd->nd_repstat = error; \
451 if (error && !(nfsd->nd_flag & ND_NFSV3)) \
452 nfs_rephead(0, nfsd, slp, error, mrq, &mb, &bpos); \
454 nfs_rephead((s), nfsd, slp, error, mrq, &mb, &bpos); \
458 if (error && (!(nfsd->nd_flag & ND_NFSV3) || \
459 error == EBADRPC)) { \
465 #define nfsm_writereply(s, v3) \
467 nfsd->nd_repstat = error; \
468 if (error && !(v3)) \
469 nfs_rephead(0, nfsd, slp, error, &mreq, &mb, &bpos); \
471 nfs_rephead((s), nfsd, slp, error, &mreq, &mb, &bpos); \
474 #define nfsm_adv(s) \
475 { t1 = ((caddr_t)mbuf_data(md)) + mbuf_len(md) - dpos; \
478 } else if ((t1 = nfs_adv(&md, &dpos, (s), t1))) { \
484 #define nfsm_srvmtofh(f) \
486 if (nfsd->nd_flag & ND_NFSV3) { \
487 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
488 (f)->nfh_len = fxdr_unsigned(int, *tl); \
489 if (((f)->nfh_len < (int)sizeof(struct nfs_exphandle)) || \
490 ((f)->nfh_len > NFSX_V3FHMAX)) { \
495 (f)->nfh_len = NFSX_V2FH; \
497 nfsm_dissect(tl, u_long *, (f)->nfh_len); \
498 bcopy((caddr_t)tl, (caddr_t)&(f)->nfh_xh, (f)->nfh_len); \
503 int __nfsm_error, __nfsm_len; \
505 mbuf_setlen(mp, mbuf_len(mp) + bp - bpos); \
507 __nfsm_error = mbuf_mclget(MBUF_WAITOK, MBUF_TYPE_DATA, &mp); \
509 panic("nfsm_clget: mbuf_mclget error %d", __nfsm_error); \
510 __nfsm_len = mbuf_maxlen(mp); \
511 mbuf_setlen(mp, __nfsm_len); \
512 __nfsm_error = mbuf_setnext(mp2, mp); \
514 panic("nfsm_clget: mbuf_setnext error %d", __nfsm_error); \
516 bp = mbuf_data(mp); \
517 be = bp + __nfsm_len; \
521 #define nfsm_srv_vattr_init(vap, v3) \
524 VATTR_WANTED((vap), va_type); \
525 VATTR_WANTED((vap), va_mode); \
526 VATTR_WANTED((vap), va_nlink); \
527 VATTR_WANTED((vap), va_uid); \
528 VATTR_WANTED((vap), va_gid); \
529 VATTR_WANTED((vap), va_data_size); \
530 VATTR_WANTED((vap), va_data_alloc); \
531 VATTR_WANTED((vap), va_rdev); \
532 VATTR_WANTED((vap), va_fsid); \
533 VATTR_WANTED((vap), va_fileid); \
534 VATTR_WANTED((vap), va_access_time); \
535 VATTR_WANTED((vap), va_modify_time); \
536 VATTR_WANTED((vap), va_change_time); \
537 if (!v3) VATTR_WANTED((vap), va_iosize); \
540 #define nfsm_srv_pre_vattr_init(vap, v3) \
543 VATTR_WANTED((vap), va_data_size); \
544 VATTR_WANTED((vap), va_modify_time); \
545 VATTR_WANTED((vap), va_change_time); \
548 #define nfsm_srvfillattr(a, f) \
549 nfsm_srvfattr(nfsd, (a), (f))
551 #define nfsm_srvwcc_data(br, b, ar, a) \
552 nfsm_srvwcc(nfsd, (br), (b), (ar), (a), &mb, &bpos)
554 #define nfsm_srvpostop_attr(r, a) \
555 nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos)
557 #define nfsm_srvsattr(a) \
559 struct timespec now; \
560 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
561 if (*tl == nfs_true) { \
562 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
563 VATTR_SET(a, va_mode, nfstov_mode(*tl)); \
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_uid, fxdr_unsigned(uid_t, *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_gid, fxdr_unsigned(gid_t, *tl)); \
575 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
576 if (*tl == nfs_true) { \
577 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED); \
578 fxdr_hyper(tl, &(a)->va_data_size); \
579 VATTR_SET_ACTIVE(a, va_data_size); \
581 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
583 switch (fxdr_unsigned(int, *tl)) { \
584 case NFSV3SATTRTIME_TOCLIENT: \
585 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED); \
586 fxdr_nfsv3time(tl, &(a)->va_access_time); \
587 VATTR_SET_ACTIVE(a, va_access_time); \
589 case NFSV3SATTRTIME_TOSERVER: \
590 VATTR_SET(a, va_access_time, now); \
593 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
594 switch (fxdr_unsigned(int, *tl)) { \
595 case NFSV3SATTRTIME_TOCLIENT: \
596 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED); \
597 fxdr_nfsv3time(tl, &(a)->va_modify_time); \
598 VATTR_SET_ACTIVE(a, va_modify_time); \
600 case NFSV3SATTRTIME_TOSERVER: \
601 VATTR_SET(a, va_modify_time, now); \
605 #endif /* __APPLE_API_PRIVATE */
606 #endif /* _NFS_NFSM_SUBS_H_ */