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.10 2008/09/17 21:44:25 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 (struct vnode
*vp
, u_long procid
, int hsiz
,
59 struct mbuf
*nfsm_rpchead (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, MB_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, lflags) \
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, lflags)) != 0) { \
241 * This function updates the attribute cache based on data returned in the
242 * NFS reply for NFS RPCs that modify the target file. If the RPC succeeds
243 * a 'before' and 'after' mtime is returned that allows us to determine if
244 * the new mtime attribute represents our modification or someone else's
247 * The flag argument returns non-0 if the original times matched, zero if
248 * they did not match. NRMODIFIED is automatically set if the before time
249 * does not match the original n_mtime, and n_mtime is automatically updated
250 * to the new after time (by nfsm_postop_attr()).
252 /* Used as (f) for nfsm_wcc_data() */
253 #define NFSV3_WCCRATTR 0
254 #define NFSV3_WCCCHK 1
256 #define nfsm_wcc_data(v, f) \
258 int ttattrf, ttretf = 0; \
259 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
260 if (*tl == nfs_true) { \
261 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED); \
263 ttretf = (VTONFS(v)->n_mtime == \
264 fxdr_unsigned(u_int32_t, *(tl + 2))); \
266 VTONFS(v)->n_flag |= NRMODIFIED; \
268 nfsm_postop_attr((v), ttattrf, NFS_LATTR_NOSHRINK|NFS_LATTR_NOMTIMECHECK); \
270 nfsm_postop_attr((v), ttattrf, NFS_LATTR_NOSHRINK); \
279 /* If full is true, set all fields, otherwise just set mode and time fields */
280 #define nfsm_v3attrbuild(a, full) \
282 if ((a)->va_mode != (mode_t)VNOVAL) { \
283 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
285 *tl = txdr_unsigned((a)->va_mode); \
287 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
290 if ((full) && (a)->va_uid != (uid_t)VNOVAL) { \
291 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
293 *tl = txdr_unsigned((a)->va_uid); \
295 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
298 if ((full) && (a)->va_gid != (gid_t)VNOVAL) { \
299 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
301 *tl = txdr_unsigned((a)->va_gid); \
303 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
306 if ((full) && (a)->va_size != VNOVAL) { \
307 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); \
309 txdr_hyper((a)->va_size, tl); \
311 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
314 if ((a)->va_atime.tv_sec != VNOVAL) { \
315 if ((a)->va_atime.tv_sec != time_second) { \
316 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);\
317 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);\
318 txdr_nfsv3time(&(a)->va_atime, tl); \
320 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
321 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); \
324 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
325 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); \
327 if ((a)->va_mtime.tv_sec != VNOVAL) { \
328 if ((a)->va_mtime.tv_sec != time_second) { \
329 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);\
330 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);\
331 txdr_nfsv3time(&(a)->va_mtime, tl); \
333 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
334 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); \
337 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
338 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); \
343 #define nfsm_strsiz(s,m) \
345 nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
346 if (((s) = fxdr_unsigned(int32_t,*tl)) > (m)) { \
353 #define nfsm_srvstrsiz(s,m) \
355 nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
356 if (((s) = fxdr_unsigned(int32_t,*tl)) > (m) || (s) <= 0) { \
362 #define nfsm_srvnamesiz(s) \
364 nfsm_dissect(tl,u_int32_t *,NFSX_UNSIGNED); \
365 if (((s) = fxdr_unsigned(int32_t,*tl)) > NFS_MAXNAMLEN) \
366 error = NFSERR_NAMETOL; \
373 #define nfsm_mtouio(p,s) \
376 (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos)) != 0) { \
383 #define nfsm_uiotom(p,s) \
385 if ((t1 = nfsm_uiotombuf((p),&mb,(s),&bpos)) != 0) { \
392 #define nfsm_reqhead(v,a,s) \
394 mb = mreq = nfsm_reqh((v),(a),(s),&bpos); \
397 #define nfsm_rndup(a) (((a)+3)&(~0x3))
399 #define nfsm_request(v, t, td, c) \
401 if ((error = nfs_request((v), mreq, (t), (td), \
402 (c), &mrep, &md, &dpos)) != 0) { \
403 if (error & NFSERR_RETERR) \
404 error &= ~NFSERR_RETERR; \
410 #define nfsm_strtom(a,s,m) \
414 error = ENAMETOOLONG; \
417 t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
418 if (t2 <= M_TRAILINGSPACE(mb)) { \
419 nfsm_build(tl,u_int32_t *,t2); \
420 *tl++ = txdr_unsigned(s); \
421 *(tl+((t2>>2)-2)) = 0; \
422 bcopy((const char *)(a), (caddr_t)tl, (s)); \
423 } else if ((t2 = nfsm_strtmbuf(&mb, &bpos, (a), (s))) != 0) { \
430 #define nfsm_srvdone \
436 #define nfsm_reply(s) \
438 nfsd->nd_repstat = error; \
439 if (error && !(nfsd->nd_flag & ND_NFSV3)) \
440 nfs_rephead(0, nfsd, slp, error, mrq, &mb, &bpos); \
442 nfs_rephead((s), nfsd, slp, error, mrq, &mb, &bpos); \
443 if (mrep != NULL) { \
448 if (error && (!(nfsd->nd_flag & ND_NFSV3) || \
449 error == EBADRPC)) { \
455 #define nfsm_writereply(s, v3) \
457 nfsd->nd_repstat = error; \
458 if (error && !(v3)) \
459 nfs_rephead(0, nfsd, slp, error, &mreq, &mb, &bpos); \
461 nfs_rephead((s), nfsd, slp, error, &mreq, &mb, &bpos); \
464 #define nfsm_adv(s) \
466 t1 = mtod(md, caddr_t)+md->m_len-dpos; \
469 } else if ((t1 = nfs_adv(&md, &dpos, (s), t1)) != 0) { \
476 #define nfsm_srvmtofh(f) \
479 if (nfsd->nd_flag & ND_NFSV3) { \
480 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
481 fhlen = fxdr_unsigned(int, *tl); \
482 if (fhlen != 0 && fhlen != NFSX_V3FH) { \
490 nfsm_dissect(tl, u_int32_t *, fhlen); \
491 bcopy((caddr_t)tl, (caddr_t)(f), fhlen); \
493 bzero((caddr_t)(f), NFSX_V3FH); \
497 #define nfsm_clget(mp1, mp2, mb, bp, be, tl) \
501 mp1->m_len += bp-bpos; \
502 mp1 = m_getcl(MB_WAIT, MT_DATA, 0); \
503 mp1->m_len = MCLBYTES; \
506 bp = mtod(mp1, caddr_t); \
507 be = bp+mp1->m_len; \
509 tl = (u_int32_t *)bp; \
512 #define nfsm_srvfillattr(a, f) \
514 nfsm_srvfattr(nfsd, (a), (f)); \
517 #define nfsm_srvwcc_data(br, b, ar, a) \
519 nfsm_srvwcc(nfsd, (br), (b), (ar), (a), &mb, &bpos); \
522 #define nfsm_srvpostop_attr(r, a) \
524 nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos); \
527 #define nfsm_srvsattr(a) \
529 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
530 if (*tl == nfs_true) { \
531 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
532 (a)->va_mode = nfstov_mode(*tl); \
534 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
535 if (*tl == nfs_true) { \
536 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
537 (a)->va_uid = fxdr_unsigned(uid_t, *tl); \
539 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
540 if (*tl == nfs_true) { \
541 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
542 (a)->va_gid = fxdr_unsigned(gid_t, *tl); \
544 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
545 if (*tl == nfs_true) { \
546 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
547 (a)->va_size = fxdr_hyper(tl); \
549 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
550 switch (fxdr_unsigned(int, *tl)) { \
551 case NFSV3SATTRTIME_TOCLIENT: \
552 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
553 fxdr_nfsv3time(tl, &(a)->va_atime); \
555 case NFSV3SATTRTIME_TOSERVER: \
556 getnanotime(&(a)->va_atime); \
559 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
560 switch (fxdr_unsigned(int, *tl)) { \
561 case NFSV3SATTRTIME_TOCLIENT: \
562 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
563 fxdr_nfsv3time(tl, &(a)->va_mtime); \
565 case NFSV3SATTRTIME_TOSERVER: \
566 getnanotime(&(a)->va_mtime); \