2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)nfsm_subs.h 8.2 (Berkeley) 3/30/95
37 * $FreeBSD: src/sys/nfs/nfsm_subs.h,v 1.27.2.1 2000/10/28 16:27:27 dwmalone Exp $
41 #ifndef _NFS_NFSM_SUBS_H_
42 #define _NFS_NFSM_SUBS_H_
48 * These macros do strange and peculiar things to mbuf chains for
49 * the assistance of the nfs code. To attempt to use them for any
50 * other purpose will be dangerous. (they make weird assumptions)
54 * First define what the actual subs. return
56 struct mbuf *nfsm_reqh __P((struct vnode *vp, u_long procid, int hsiz,
58 struct mbuf *nfsm_rpchead __P((struct ucred *cr, int nmflag, int procid,
59 int auth_type, int auth_len, char *auth_str,
60 int verf_len, char *verf_str,
61 struct mbuf *mrest, int mrest_len,
62 struct mbuf **mbp, u_int32_t *xidp));
64 #define M_HASCL(m) ((m)->m_flags & M_EXT)
65 #define NFSMINOFF(m) \
68 (m)->m_data = (m)->m_ext.ext_buf; \
69 else if ((m)->m_flags & M_PKTHDR) \
70 (m)->m_data = (m)->m_pktdat; \
72 (m)->m_data = (m)->m_dat; \
74 #define NFSMADV(m, s) \
78 #define NFSMSIZ(m) ((M_HASCL(m))?MCLBYTES: \
79 (((m)->m_flags & M_PKTHDR)?MHLEN:MLEN))
82 * Now for the macros that do the simple stuff and call the functions
84 * These macros use several vars. declared in nfsm_reqhead and these
85 * vars. must not be used elsewhere unless you are careful not to corrupt
86 * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries
87 * that may be used so long as the value is not expected to retained
89 * I know, this is kind of dorkey, but it makes the actual op functions
90 * fairly clean and deals with the mess caused by the xdr discriminating
94 #define nfsm_build(a,c,s) \
96 if ((s) > M_TRAILINGSPACE(mb)) { \
97 MGET(mb2, M_WAIT, MT_DATA); \
99 panic("build > MLEN"); \
103 bpos = mtod(mb, caddr_t); \
110 #define nfsm_dissect(a, c, s) \
112 t1 = mtod(md, caddr_t)+md->m_len-dpos; \
116 } else if ((t1 = nfsm_disct(&md, &dpos, (s), t1, &cp2)) != 0){ \
125 #define nfsm_fhtom(v, v3) \
128 t2 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED; \
129 if (t2 <= M_TRAILINGSPACE(mb)) { \
130 nfsm_build(tl, u_int32_t *, t2); \
131 *tl++ = txdr_unsigned(VTONFS(v)->n_fhsize); \
132 *(tl + ((t2>>2) - 2)) = 0; \
133 bcopy((caddr_t)VTONFS(v)->n_fhp,(caddr_t)tl, \
134 VTONFS(v)->n_fhsize); \
135 } else if ((t2 = nfsm_strtmbuf(&mb, &bpos, \
136 (caddr_t)VTONFS(v)->n_fhp, \
137 VTONFS(v)->n_fhsize)) != 0) { \
143 nfsm_build(cp, caddr_t, NFSX_V2FH); \
144 bcopy((caddr_t)VTONFS(v)->n_fhp, cp, NFSX_V2FH); \
148 #define nfsm_srvfhtom(f, v3) \
151 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FH);\
152 *tl++ = txdr_unsigned(NFSX_V3FH); \
153 bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
155 nfsm_build(cp, caddr_t, NFSX_V2FH); \
156 bcopy((caddr_t)(f), cp, NFSX_V2FH); \
160 #define nfsm_srvpostop_fh(f) \
162 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED + NFSX_V3FH); \
164 *tl++ = txdr_unsigned(NFSX_V3FH); \
165 bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
168 #define nfsm_mtofh(d, v, v3, f) \
170 struct nfsnode *ttnp; nfsfh_t *ttfhp; int ttfhsize; \
172 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
173 (f) = fxdr_unsigned(int, *tl); \
177 nfsm_getfh(ttfhp, ttfhsize, (v3)); \
178 if ((t1 = nfs_nget((d)->v_mount, ttfhp, ttfhsize, \
184 (v) = NFSTOV(ttnp); \
187 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
189 (f) = fxdr_unsigned(int, *tl); \
190 else if (fxdr_unsigned(int, *tl)) \
191 nfsm_adv(NFSX_V3FATTR); \
194 nfsm_loadattr((v), (struct vattr *)0); \
197 #define nfsm_getfh(f, s, v3) \
200 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
201 if (((s) = fxdr_unsigned(int, *tl)) <= 0 || \
202 (s) > NFSX_V3FHMAX) { \
209 nfsm_dissect((f), nfsfh_t *, nfsm_rndup(s)); \
212 #define nfsm_loadattr(v, a) \
214 struct vnode *ttvp = (v); \
215 if ((t1 = nfs_loadattrcache(&ttvp, &md, &dpos, (a), 0)) != 0) { \
223 #define nfsm_postop_attr(v, f) \
225 struct vnode *ttvp = (v); \
226 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
227 if (((f) = fxdr_unsigned(int, *tl)) != 0) { \
228 if ((t1 = nfs_loadattrcache(&ttvp, &md, &dpos, \
229 (struct vattr *)0, 1)) != 0) { \
239 /* Used as (f) for nfsm_wcc_data() */
240 #define NFSV3_WCCRATTR 0
241 #define NFSV3_WCCCHK 1
243 #define nfsm_wcc_data(v, f) \
245 int ttattrf, ttretf = 0; \
246 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
247 if (*tl == nfs_true) { \
248 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED); \
250 ttretf = (VTONFS(v)->n_mtime == \
251 fxdr_unsigned(u_int32_t, *(tl + 2))); \
253 nfsm_postop_attr((v), ttattrf); \
261 /* If full is true, set all fields, otherwise just set mode and time fields */
262 #define nfsm_v3attrbuild(a, full) \
264 if ((a)->va_mode != (mode_t)VNOVAL) { \
265 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
267 *tl = txdr_unsigned((a)->va_mode); \
269 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
272 if ((full) && (a)->va_uid != (uid_t)VNOVAL) { \
273 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
275 *tl = txdr_unsigned((a)->va_uid); \
277 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
280 if ((full) && (a)->va_gid != (gid_t)VNOVAL) { \
281 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
283 *tl = txdr_unsigned((a)->va_gid); \
285 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
288 if ((full) && (a)->va_size != VNOVAL) { \
289 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); \
291 txdr_hyper((a)->va_size, tl); \
293 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
296 if ((a)->va_atime.tv_sec != VNOVAL) { \
297 if ((a)->va_atime.tv_sec != time_second) { \
298 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);\
299 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);\
300 txdr_nfsv3time(&(a)->va_atime, tl); \
302 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
303 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); \
306 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
307 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); \
309 if ((a)->va_mtime.tv_sec != VNOVAL) { \
310 if ((a)->va_mtime.tv_sec != time_second) { \
311 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);\
312 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);\
313 txdr_nfsv3time(&(a)->va_mtime, tl); \
315 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
316 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); \
319 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
320 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); \
325 #define nfsm_strsiz(s,m) \
327 nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
328 if (((s) = fxdr_unsigned(int32_t,*tl)) > (m)) { \
335 #define nfsm_srvstrsiz(s,m) \
337 nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
338 if (((s) = fxdr_unsigned(int32_t,*tl)) > (m) || (s) <= 0) { \
344 #define nfsm_srvnamesiz(s) \
346 nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
347 if (((s) = fxdr_unsigned(int32_t,*tl)) > NFS_MAXNAMLEN) \
348 error = NFSERR_NAMETOL; \
355 #define nfsm_mtouio(p,s) \
358 (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos)) != 0) { \
365 #define nfsm_uiotom(p,s) \
367 if ((t1 = nfsm_uiotombuf((p),&mb,(s),&bpos)) != 0) { \
374 #define nfsm_reqhead(v,a,s) \
376 mb = mreq = nfsm_reqh((v),(a),(s),&bpos); \
379 #define nfsm_reqdone \
385 #define nfsm_rndup(a) (((a)+3)&(~0x3))
387 #define nfsm_request(v, t, p, c) \
389 if ((error = nfs_request((v), mreq, (t), (p), \
390 (c), &mrep, &md, &dpos)) != 0) { \
391 if (error & NFSERR_RETERR) \
392 error &= ~NFSERR_RETERR; \
398 #define nfsm_strtom(a,s,m) \
402 error = ENAMETOOLONG; \
405 t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
406 if (t2 <= M_TRAILINGSPACE(mb)) { \
407 nfsm_build(tl,u_int32_t *,t2); \
408 *tl++ = txdr_unsigned(s); \
409 *(tl+((t2>>2)-2)) = 0; \
410 bcopy((const char *)(a), (caddr_t)tl, (s)); \
411 } else if ((t2 = nfsm_strtmbuf(&mb, &bpos, (a), (s))) != 0) { \
418 #define nfsm_srvdone \
424 #define nfsm_reply(s) \
426 nfsd->nd_repstat = error; \
427 if (error && !(nfsd->nd_flag & ND_NFSV3)) \
428 (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
431 (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
433 if (mrep != NULL) { \
438 if (error && (!(nfsd->nd_flag & ND_NFSV3) || \
439 error == EBADRPC)) { \
445 #define nfsm_writereply(s, v3) \
447 nfsd->nd_repstat = error; \
448 if (error && !(v3)) \
449 (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
450 &mreq, &mb, &bpos); \
452 (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
453 &mreq, &mb, &bpos); \
456 #define nfsm_adv(s) \
458 t1 = mtod(md, caddr_t)+md->m_len-dpos; \
461 } else if ((t1 = nfs_adv(&md, &dpos, (s), t1)) != 0) { \
468 #define nfsm_srvmtofh(f) \
471 if (nfsd->nd_flag & ND_NFSV3) { \
472 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
473 fhlen = fxdr_unsigned(int, *tl); \
474 if (fhlen != 0 && fhlen != NFSX_V3FH) { \
482 nfsm_dissect(tl, u_int32_t *, fhlen); \
483 bcopy((caddr_t)tl, (caddr_t)(f), fhlen); \
485 bzero((caddr_t)(f), NFSX_V3FH); \
493 mp->m_len += bp-bpos; \
494 MGET(mp, M_WAIT, MT_DATA); \
495 MCLGET(mp, M_WAIT); \
496 mp->m_len = NFSMSIZ(mp); \
499 bp = mtod(mp, caddr_t); \
502 tl = (u_int32_t *)bp; \
505 #define nfsm_srvfillattr(a, f) \
507 nfsm_srvfattr(nfsd, (a), (f)); \
510 #define nfsm_srvwcc_data(br, b, ar, a) \
512 nfsm_srvwcc(nfsd, (br), (b), (ar), (a), &mb, &bpos); \
515 #define nfsm_srvpostop_attr(r, a) \
517 nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos); \
520 #define nfsm_srvsattr(a) \
522 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
523 if (*tl == nfs_true) { \
524 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
525 (a)->va_mode = nfstov_mode(*tl); \
527 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
528 if (*tl == nfs_true) { \
529 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
530 (a)->va_uid = fxdr_unsigned(uid_t, *tl); \
532 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
533 if (*tl == nfs_true) { \
534 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
535 (a)->va_gid = fxdr_unsigned(gid_t, *tl); \
537 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
538 if (*tl == nfs_true) { \
539 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
540 (a)->va_size = fxdr_hyper(tl); \
542 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
543 switch (fxdr_unsigned(int, *tl)) { \
544 case NFSV3SATTRTIME_TOCLIENT: \
545 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
546 fxdr_nfsv3time(tl, &(a)->va_atime); \
548 case NFSV3SATTRTIME_TOSERVER: \
549 getnanotime(&(a)->va_atime); \
552 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
553 switch (fxdr_unsigned(int, *tl)) { \
554 case NFSV3SATTRTIME_TOCLIENT: \
555 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
556 fxdr_nfsv3time(tl, &(a)->va_mtime); \
558 case NFSV3SATTRTIME_TOSERVER: \
559 getnanotime(&(a)->va_mtime); \