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 $
38 * $DragonFly: src/sys/vfs/nfs/nfsm_subs.h,v 1.2 2003/06/17 04:28:54 dillon Exp $
42 #ifndef _NFS_NFSM_SUBS_H_
43 #define _NFS_NFSM_SUBS_H_
49 * These macros do strange and peculiar things to mbuf chains for
50 * the assistance of the nfs code. To attempt to use them for any
51 * other purpose will be dangerous. (they make weird assumptions)
55 * First define what the actual subs. return
57 struct mbuf *nfsm_reqh __P((struct vnode *vp, u_long procid, int hsiz,
59 struct mbuf *nfsm_rpchead __P((struct ucred *cr, int nmflag, int procid,
60 int auth_type, int auth_len, char *auth_str,
61 int verf_len, char *verf_str,
62 struct mbuf *mrest, int mrest_len,
63 struct mbuf **mbp, u_int32_t *xidp));
65 #define M_HASCL(m) ((m)->m_flags & M_EXT)
66 #define NFSMINOFF(m) \
69 (m)->m_data = (m)->m_ext.ext_buf; \
70 else if ((m)->m_flags & M_PKTHDR) \
71 (m)->m_data = (m)->m_pktdat; \
73 (m)->m_data = (m)->m_dat; \
75 #define NFSMADV(m, s) \
79 #define NFSMSIZ(m) ((M_HASCL(m))?MCLBYTES: \
80 (((m)->m_flags & M_PKTHDR)?MHLEN:MLEN))
83 * Now for the macros that do the simple stuff and call the functions
85 * These macros use several vars. declared in nfsm_reqhead and these
86 * vars. must not be used elsewhere unless you are careful not to corrupt
87 * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries
88 * that may be used so long as the value is not expected to retained
90 * I know, this is kind of dorkey, but it makes the actual op functions
91 * fairly clean and deals with the mess caused by the xdr discriminating
95 #define nfsm_build(a,c,s) \
97 if ((s) > M_TRAILINGSPACE(mb)) { \
98 MGET(mb2, M_WAIT, MT_DATA); \
100 panic("build > MLEN"); \
104 bpos = mtod(mb, caddr_t); \
111 #define nfsm_dissect(a, c, s) \
113 t1 = mtod(md, caddr_t)+md->m_len-dpos; \
117 } else if ((t1 = nfsm_disct(&md, &dpos, (s), t1, &cp2)) != 0){ \
126 #define nfsm_fhtom(v, v3) \
129 t2 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED; \
130 if (t2 <= M_TRAILINGSPACE(mb)) { \
131 nfsm_build(tl, u_int32_t *, t2); \
132 *tl++ = txdr_unsigned(VTONFS(v)->n_fhsize); \
133 *(tl + ((t2>>2) - 2)) = 0; \
134 bcopy((caddr_t)VTONFS(v)->n_fhp,(caddr_t)tl, \
135 VTONFS(v)->n_fhsize); \
136 } else if ((t2 = nfsm_strtmbuf(&mb, &bpos, \
137 (caddr_t)VTONFS(v)->n_fhp, \
138 VTONFS(v)->n_fhsize)) != 0) { \
144 nfsm_build(cp, caddr_t, NFSX_V2FH); \
145 bcopy((caddr_t)VTONFS(v)->n_fhp, cp, NFSX_V2FH); \
149 #define nfsm_srvfhtom(f, v3) \
152 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FH);\
153 *tl++ = txdr_unsigned(NFSX_V3FH); \
154 bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
156 nfsm_build(cp, caddr_t, NFSX_V2FH); \
157 bcopy((caddr_t)(f), cp, NFSX_V2FH); \
161 #define nfsm_srvpostop_fh(f) \
163 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED + NFSX_V3FH); \
165 *tl++ = txdr_unsigned(NFSX_V3FH); \
166 bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
169 #define nfsm_mtofh(d, v, v3, f) \
171 struct nfsnode *ttnp; nfsfh_t *ttfhp; int ttfhsize; \
173 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
174 (f) = fxdr_unsigned(int, *tl); \
178 nfsm_getfh(ttfhp, ttfhsize, (v3)); \
179 if ((t1 = nfs_nget((d)->v_mount, ttfhp, ttfhsize, \
185 (v) = NFSTOV(ttnp); \
188 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
190 (f) = fxdr_unsigned(int, *tl); \
191 else if (fxdr_unsigned(int, *tl)) \
192 nfsm_adv(NFSX_V3FATTR); \
195 nfsm_loadattr((v), (struct vattr *)0); \
198 #define nfsm_getfh(f, s, v3) \
201 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
202 if (((s) = fxdr_unsigned(int, *tl)) <= 0 || \
203 (s) > NFSX_V3FHMAX) { \
210 nfsm_dissect((f), nfsfh_t *, nfsm_rndup(s)); \
213 #define nfsm_loadattr(v, a) \
215 struct vnode *ttvp = (v); \
216 if ((t1 = nfs_loadattrcache(&ttvp, &md, &dpos, (a), 0)) != 0) { \
224 #define nfsm_postop_attr(v, f) \
226 struct vnode *ttvp = (v); \
227 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
228 if (((f) = fxdr_unsigned(int, *tl)) != 0) { \
229 if ((t1 = nfs_loadattrcache(&ttvp, &md, &dpos, \
230 (struct vattr *)0, 1)) != 0) { \
240 /* Used as (f) for nfsm_wcc_data() */
241 #define NFSV3_WCCRATTR 0
242 #define NFSV3_WCCCHK 1
244 #define nfsm_wcc_data(v, f) \
246 int ttattrf, ttretf = 0; \
247 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
248 if (*tl == nfs_true) { \
249 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED); \
251 ttretf = (VTONFS(v)->n_mtime == \
252 fxdr_unsigned(u_int32_t, *(tl + 2))); \
254 nfsm_postop_attr((v), ttattrf); \
262 /* If full is true, set all fields, otherwise just set mode and time fields */
263 #define nfsm_v3attrbuild(a, full) \
265 if ((a)->va_mode != (mode_t)VNOVAL) { \
266 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
268 *tl = txdr_unsigned((a)->va_mode); \
270 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
273 if ((full) && (a)->va_uid != (uid_t)VNOVAL) { \
274 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
276 *tl = txdr_unsigned((a)->va_uid); \
278 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
281 if ((full) && (a)->va_gid != (gid_t)VNOVAL) { \
282 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
284 *tl = txdr_unsigned((a)->va_gid); \
286 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
289 if ((full) && (a)->va_size != VNOVAL) { \
290 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); \
292 txdr_hyper((a)->va_size, tl); \
294 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
297 if ((a)->va_atime.tv_sec != VNOVAL) { \
298 if ((a)->va_atime.tv_sec != time_second) { \
299 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);\
300 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);\
301 txdr_nfsv3time(&(a)->va_atime, tl); \
303 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
304 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); \
307 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
308 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); \
310 if ((a)->va_mtime.tv_sec != VNOVAL) { \
311 if ((a)->va_mtime.tv_sec != time_second) { \
312 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);\
313 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);\
314 txdr_nfsv3time(&(a)->va_mtime, tl); \
316 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
317 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); \
320 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
321 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); \
326 #define nfsm_strsiz(s,m) \
328 nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
329 if (((s) = fxdr_unsigned(int32_t,*tl)) > (m)) { \
336 #define nfsm_srvstrsiz(s,m) \
338 nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
339 if (((s) = fxdr_unsigned(int32_t,*tl)) > (m) || (s) <= 0) { \
345 #define nfsm_srvnamesiz(s) \
347 nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
348 if (((s) = fxdr_unsigned(int32_t,*tl)) > NFS_MAXNAMLEN) \
349 error = NFSERR_NAMETOL; \
356 #define nfsm_mtouio(p,s) \
359 (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos)) != 0) { \
366 #define nfsm_uiotom(p,s) \
368 if ((t1 = nfsm_uiotombuf((p),&mb,(s),&bpos)) != 0) { \
375 #define nfsm_reqhead(v,a,s) \
377 mb = mreq = nfsm_reqh((v),(a),(s),&bpos); \
380 #define nfsm_reqdone \
386 #define nfsm_rndup(a) (((a)+3)&(~0x3))
388 #define nfsm_request(v, t, p, c) \
390 if ((error = nfs_request((v), mreq, (t), (p), \
391 (c), &mrep, &md, &dpos)) != 0) { \
392 if (error & NFSERR_RETERR) \
393 error &= ~NFSERR_RETERR; \
399 #define nfsm_strtom(a,s,m) \
403 error = ENAMETOOLONG; \
406 t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
407 if (t2 <= M_TRAILINGSPACE(mb)) { \
408 nfsm_build(tl,u_int32_t *,t2); \
409 *tl++ = txdr_unsigned(s); \
410 *(tl+((t2>>2)-2)) = 0; \
411 bcopy((const char *)(a), (caddr_t)tl, (s)); \
412 } else if ((t2 = nfsm_strtmbuf(&mb, &bpos, (a), (s))) != 0) { \
419 #define nfsm_srvdone \
425 #define nfsm_reply(s) \
427 nfsd->nd_repstat = error; \
428 if (error && !(nfsd->nd_flag & ND_NFSV3)) \
429 (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
432 (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
434 if (mrep != NULL) { \
439 if (error && (!(nfsd->nd_flag & ND_NFSV3) || \
440 error == EBADRPC)) { \
446 #define nfsm_writereply(s, v3) \
448 nfsd->nd_repstat = error; \
449 if (error && !(v3)) \
450 (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
451 &mreq, &mb, &bpos); \
453 (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
454 &mreq, &mb, &bpos); \
457 #define nfsm_adv(s) \
459 t1 = mtod(md, caddr_t)+md->m_len-dpos; \
462 } else if ((t1 = nfs_adv(&md, &dpos, (s), t1)) != 0) { \
469 #define nfsm_srvmtofh(f) \
472 if (nfsd->nd_flag & ND_NFSV3) { \
473 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
474 fhlen = fxdr_unsigned(int, *tl); \
475 if (fhlen != 0 && fhlen != NFSX_V3FH) { \
483 nfsm_dissect(tl, u_int32_t *, fhlen); \
484 bcopy((caddr_t)tl, (caddr_t)(f), fhlen); \
486 bzero((caddr_t)(f), NFSX_V3FH); \
494 mp->m_len += bp-bpos; \
495 MGET(mp, M_WAIT, MT_DATA); \
496 MCLGET(mp, M_WAIT); \
497 mp->m_len = NFSMSIZ(mp); \
500 bp = mtod(mp, caddr_t); \
503 tl = (u_int32_t *)bp; \
506 #define nfsm_srvfillattr(a, f) \
508 nfsm_srvfattr(nfsd, (a), (f)); \
511 #define nfsm_srvwcc_data(br, b, ar, a) \
513 nfsm_srvwcc(nfsd, (br), (b), (ar), (a), &mb, &bpos); \
516 #define nfsm_srvpostop_attr(r, a) \
518 nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos); \
521 #define nfsm_srvsattr(a) \
523 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
524 if (*tl == nfs_true) { \
525 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
526 (a)->va_mode = nfstov_mode(*tl); \
528 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
529 if (*tl == nfs_true) { \
530 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
531 (a)->va_uid = fxdr_unsigned(uid_t, *tl); \
533 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
534 if (*tl == nfs_true) { \
535 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
536 (a)->va_gid = fxdr_unsigned(gid_t, *tl); \
538 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
539 if (*tl == nfs_true) { \
540 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
541 (a)->va_size = fxdr_hyper(tl); \
543 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
544 switch (fxdr_unsigned(int, *tl)) { \
545 case NFSV3SATTRTIME_TOCLIENT: \
546 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
547 fxdr_nfsv3time(tl, &(a)->va_atime); \
549 case NFSV3SATTRTIME_TOSERVER: \
550 getnanotime(&(a)->va_atime); \
553 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
554 switch (fxdr_unsigned(int, *tl)) { \
555 case NFSV3SATTRTIME_TOCLIENT: \
556 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
557 fxdr_nfsv3time(tl, &(a)->va_mtime); \
559 case NFSV3SATTRTIME_TOSERVER: \
560 getnanotime(&(a)->va_mtime); \