1 /* $NetBSD: nfsm_subs.h,v 1.50 2007/03/04 06:03:38 christos Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)nfsm_subs.h 8.2 (Berkeley) 3/30/95
38 #ifndef _NFS_NFSM_SUBS_H_
39 #define _NFS_NFSM_SUBS_H_
43 * These macros do strange and peculiar things to mbuf chains for
44 * the assistance of the nfs code. To attempt to use them for any
45 * other purpose will be dangerous. (they make weird assumptions)
49 * First define what the actual subs. return
52 #define M_HASCL(m) ((m)->m_flags & M_EXT)
53 #define NFSMADV(m, s) (m)->m_data += (s)
54 #define NFSMSIZ(m) ((M_HASCL(m)) ? (m)->m_ext.ext_size : \
55 (((m)->m_flags & M_PKTHDR) ? MHLEN : MLEN))
58 * Now for the macros that do the simple stuff and call the functions
60 * These macros use several vars. declared in nfsm_reqhead and these
61 * vars. must not be used elsewhere unless you are careful not to corrupt
62 * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries
63 * that may be used so long as the value is not expected to retained
65 * I know, this is kind of dorkey, but it makes the actual op functions
66 * fairly clean and deals with the mess caused by the xdr discriminating
70 #define nfsm_build(a,c,s) \
71 { if ((s) > M_TRAILINGSPACE(mb)) { \
73 mb2 = m_get(M_WAIT, MT_DATA); \
74 MCLAIM(mb2, &nfs_mowner); \
76 panic("build > MLEN"); \
80 bpos = mtod(mb, char *); \
86 #define nfsm_aligned(p) ALIGNED_POINTER(p,u_int32_t)
88 #define nfsm_dissect(a, c, s) \
89 { t1 = mtod(md, char *) + md->m_len-dpos; \
90 if (t1 >= (s) && nfsm_aligned(dpos)) { \
93 } else if ((t1 = nfsm_disct(&md, &dpos, (s), t1, &cp2)) != 0){ \
101 #define nfsm_fhtom(n, v3) \
103 t2 = nfsm_rndup((n)->n_fhsize) + NFSX_UNSIGNED; \
104 if (t2 <= M_TRAILINGSPACE(mb)) { \
105 nfsm_build(tl, u_int32_t *, t2); \
106 *tl++ = txdr_unsigned((n)->n_fhsize); \
107 *(tl + ((t2>>2) - 2)) = 0; \
108 memcpy(tl,(n)->n_fhp, (n)->n_fhsize); \
109 } else if ((t2 = nfsm_strtmbuf(&mb, &bpos, \
110 (void *)(n)->n_fhp, (n)->n_fhsize)) != 0) { \
116 nfsm_build(cp, void *, NFSX_V2FH); \
117 memcpy(cp, (n)->n_fhp, NFSX_V2FH); \
120 #define nfsm_srvfhtom(f, v3) \
122 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + \
124 *tl++ = txdr_unsigned(NFSRVFH_SIZE(f)); \
125 memcpy(tl, NFSRVFH_DATA(f), NFSRVFH_SIZE(f)); \
127 KASSERT(NFSRVFH_SIZE(f) == NFSX_V2FH); \
128 nfsm_build(cp, void *, NFSX_V2FH); \
129 memcpy(cp, NFSRVFH_DATA(f), NFSX_V2FH); \
132 #define nfsm_srvpostop_fh(f) \
133 { nfsm_build(tl, u_int32_t *, \
134 2 * NFSX_UNSIGNED + NFSRVFH_SIZE(f)); \
136 *tl++ = txdr_unsigned(NFSRVFH_SIZE(f)); \
137 memcpy(tl, NFSRVFH_DATA(f), NFSRVFH_SIZE(f)); \
141 * nfsm_mtofh: dissect a "resulted obj" part of create-like operations
144 * for nfsv3, dissect post_op_fh3 and following post_op_attr.
145 * for nfsv2, dissect fhandle and following fattr.
147 * d: (IN) the vnode of the parent directry.
148 * v: (OUT) the corresponding vnode (we allocate one if needed)
149 * v3: (IN) true for nfsv3.
150 * f: (OUT) true if we got valid filehandle. always true for nfsv2.
153 #define nfsm_mtofh(d, v, v3, f) \
154 { struct nfsnode *ttnp; nfsfh_t *ttfhp; int ttfhsize; \
157 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
158 (f) = fxdr_unsigned(int, *tl); \
164 nfsm_getfh(ttfhp, ttfhsize, (v3)); \
165 if ((t1 = nfs_nget((d)->v_mount, ttfhp, ttfhsize, \
171 (v) = NFSTOV(ttnp); \
174 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
176 hasattr = fxdr_unsigned(int, *tl); \
177 else if (fxdr_unsigned(int, *tl)) \
178 nfsm_adv(NFSX_V3FATTR); \
181 nfsm_loadattr((v), (struct vattr *)0, 0); \
185 * nfsm_getfh: dissect a filehandle.
187 * f: (OUT) a filehandle.
188 * s: (OUT) size of the filehandle in bytes.
189 * v3: (IN) true if nfsv3.
192 #define nfsm_getfh(f, s, v3) \
194 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
195 if (((s) = fxdr_unsigned(int, *tl)) <= 0 || \
196 (s) > NFSX_V3FHMAX) { \
203 nfsm_dissect((f), nfsfh_t *, nfsm_rndup(s)); }
205 #define nfsm_loadattr(v, a, flags) \
206 { struct vnode *ttvp = (v); \
207 if ((t1 = nfsm_loadattrcache(&ttvp, &md, &dpos, (a), (flags))) \
216 * nfsm_postop_attr: process nfsv3 post_op_attr
218 * dissect post_op_attr. if we got a one,
219 * call nfsm_loadattrcache to update attribute cache.
221 * v: (IN/OUT) the corresponding vnode
222 * f: (OUT) true if we got valid attribute
223 * flags: (IN) flags for nfsm_loadattrcache
226 #define nfsm_postop_attr(v, f, flags) \
227 { struct vnode *ttvp = (v); \
228 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
229 if (((f) = fxdr_unsigned(int, *tl)) != 0) { \
230 if ((t1 = nfsm_loadattrcache(&ttvp, &md, &dpos, \
231 (struct vattr *)0, (flags))) != 0) { \
241 * nfsm_wcc_data: process nfsv3 wcc_data
243 * dissect pre_op_attr and then let nfsm_postop_attr dissect post_op_attr.
245 * v: (IN/OUT) the corresponding vnode
247 * NFSV3_WCCRATTR return true if we got valid post_op_attr.
248 * NFSV3_WCCCHK return true if pre_op_attr's mtime is the same
249 * as our n_mtime. (ie. our cache isn't stale.)
250 * flags: (IN) flags for nfsm_loadattrcache
251 * docheck: (IN) true if timestamp change is expected
254 /* Used as (f) for nfsm_wcc_data() */
255 #define NFSV3_WCCRATTR 0
256 #define NFSV3_WCCCHK 1
258 #define nfsm_wcc_data(v, f, flags, docheck) \
259 { int ttattrf, ttretf = 0, renewctime = 0, renewnctime = 0; \
260 struct timespec ctime, mtime; \
261 struct nfsnode *nfsp = VTONFS(v); \
262 bool haspreopattr = false; \
263 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
264 if (*tl == nfs_true) { \
265 haspreopattr = true; \
266 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED); \
267 fxdr_nfsv3time(tl + 2, &mtime); \
268 fxdr_nfsv3time(tl + 4, &ctime); \
269 if (nfsp->n_ctime == ctime.tv_sec) \
271 if ((v)->v_type == VDIR) { \
272 if (timespeccmp(&nfsp->n_nctime, &ctime, ==)) \
276 ttretf = timespeccmp(&nfsp->n_mtime, &mtime, ==);\
279 nfsm_postop_attr((v), ttattrf, (flags)); \
282 if (haspreopattr && \
283 nfs_check_wccdata(nfsp, &ctime, &mtime, (docheck))) \
284 renewctime = renewnctime = ttretf = 0; \
286 nfsp->n_ctime = nfsp->n_vattr->va_ctime.tv_sec; \
288 nfsp->n_nctime = nfsp->n_vattr->va_ctime; \
296 /* If full is true, set all fields, otherwise just set mode and time fields */
297 #define nfsm_v3attrbuild(a, full) \
298 { if ((a)->va_mode != (mode_t)VNOVAL) { \
299 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
301 *tl = txdr_unsigned((a)->va_mode); \
303 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
306 if ((full) && (a)->va_uid != (uid_t)VNOVAL) { \
307 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
309 *tl = txdr_unsigned((a)->va_uid); \
311 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
314 if ((full) && (a)->va_gid != (gid_t)VNOVAL) { \
315 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
317 *tl = txdr_unsigned((a)->va_gid); \
319 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
322 if ((full) && (a)->va_size != VNOVAL) { \
323 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); \
325 txdr_hyper((a)->va_size, tl); \
327 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
330 if ((a)->va_atime.tv_sec != VNOVAL) { \
331 if ((a)->va_atime.tv_sec != time_second) { \
332 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); \
333 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \
334 txdr_nfsv3time(&(a)->va_atime, tl); \
336 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
337 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); \
340 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
341 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); \
343 if ((a)->va_mtime.tv_sec != VNOVAL) { \
344 if ((a)->va_mtime.tv_sec != time_second) { \
345 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED); \
346 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); \
347 txdr_nfsv3time(&(a)->va_mtime, tl); \
349 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
350 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); \
353 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); \
354 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); \
359 #define nfsm_strsiz(s,m) \
360 { nfsm_dissect(tl,uint32_t *,NFSX_UNSIGNED); \
361 if (((s) = fxdr_unsigned(uint32_t,*tl)) > (m)) { \
367 #define nfsm_srvnamesiz(s) \
368 { nfsm_dissect(tl,uint32_t *,NFSX_UNSIGNED); \
369 if (((s) = fxdr_unsigned(uint32_t,*tl)) > NFS_MAXNAMLEN) \
370 error = NFSERR_NAMETOL; \
375 #define nfsm_mtouio(p,s) \
377 (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos)) != 0) { \
383 #define nfsm_uiotom(p,s) \
384 if ((t1 = nfsm_uiotombuf((p),&mb,(s),&bpos)) != 0) { \
390 #define nfsm_reqhead(n,a,s) \
391 mb = mreq = nfsm_reqh((n),(a),(s),&bpos)
393 #define nfsm_reqdone m_freem(mrep); \
396 #define nfsm_rndup(a) (((a)+3)&(~0x3))
397 #define nfsm_padlen(a) (nfsm_rndup(a) - (a))
399 #define nfsm_request1(v, t, p, c, rexmitp) \
400 if ((error = nfs_request((v), mreq, (t), (p), \
401 (c), &mrep, &md, &dpos, (rexmitp))) != 0) { \
402 if (error & NFSERR_RETERR) \
403 error &= ~NFSERR_RETERR; \
408 #define nfsm_request(v, t, p, c) nfsm_request1((v), (t), (p), (c), NULL)
410 #define nfsm_strtom(a,s,m) \
413 error = ENAMETOOLONG; \
416 t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
417 if (t2 <= M_TRAILINGSPACE(mb)) { \
418 nfsm_build(tl,u_int32_t *,t2); \
419 *tl++ = txdr_unsigned(s); \
420 *(tl+((t2>>2)-2)) = 0; \
421 memcpy(tl, (const char *)(a), (s)); \
422 } else if ((t2 = nfsm_strtmbuf(&mb, &bpos, (a), (s))) != 0) { \
428 #define nfsm_srvdone \
432 #define nfsm_reply(s) \
434 nfsd->nd_repstat = error; \
435 if (error && !(nfsd->nd_flag & ND_NFSV3)) \
436 (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
439 (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
441 if (mrep != NULL) { \
446 if (error && (!(nfsd->nd_flag & ND_NFSV3) || \
447 error == EBADRPC)) {\
453 #define nfsm_writereply(s, v3) \
455 nfsd->nd_repstat = error; \
456 if (error && !(v3)) \
457 (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
458 &mreq, &mb, &bpos); \
460 (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
461 &mreq, &mb, &bpos); \
464 #define nfsm_adv(s) \
465 { t1 = mtod(md, char *) + md->m_len - dpos; \
468 } else if ((t1 = nfs_adv(&md, &dpos, (s), t1)) != 0) { \
474 #define nfsm_srvmtofh(nsfh) \
475 { int fhlen = NFSX_V3FH; \
476 if (nfsd->nd_flag & ND_NFSV3) { \
477 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
478 fhlen = fxdr_unsigned(int, *tl); \
479 if (fhlen > NFSX_V3FHMAX || \
480 (fhlen < FHANDLE_SIZE_MIN && fhlen > 0)) { \
487 (nsfh)->nsfh_size = fhlen; \
489 nfsm_dissect(tl, u_int32_t *, fhlen); \
490 memcpy(NFSRVFH_DATA(nsfh), tl, fhlen); \
497 mp->m_len += bp-bpos; \
498 mp = m_get(M_WAIT, MT_DATA); \
499 MCLAIM(mp, &nfs_mowner); \
500 m_clget(mp, M_WAIT); \
501 mp->m_len = NFSMSIZ(mp); \
504 bp = mtod(mp, char *); \
509 #define nfsm_srvfillattr(a, f) \
510 nfsm_srvfattr(nfsd, (a), (f))
512 #define nfsm_srvwcc_data(br, b, ar, a) \
513 nfsm_srvwcc(nfsd, (br), (b), (ar), (a), &mb, &bpos)
515 #define nfsm_srvpostop_attr(r, a) \
516 nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos)
518 #define nfsm_srvsattr(a) \
520 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
521 if (*tl == nfs_true) { \
522 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
523 (a)->va_mode = nfstov_mode(*tl); \
525 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
526 if (*tl == nfs_true) { \
527 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
528 (a)->va_uid = fxdr_unsigned(uid_t, *tl); \
530 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
531 if (*tl == nfs_true) { \
532 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
533 (a)->va_gid = fxdr_unsigned(gid_t, *tl); \
535 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
536 if (*tl == nfs_true) { \
537 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
538 (a)->va_size = fxdr_hyper(tl); \
540 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
541 switch (fxdr_unsigned(int, *tl)) { \
542 case NFSV3SATTRTIME_TOCLIENT: \
543 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
544 fxdr_nfsv3time(tl, &(a)->va_atime); \
546 case NFSV3SATTRTIME_TOSERVER: \
547 getnanotime(&(a)->va_atime); \
548 (a)->va_vaflags |= VA_UTIMES_NULL; \
551 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \
552 switch (fxdr_unsigned(int, *tl)) { \
553 case NFSV3SATTRTIME_TOCLIENT: \
554 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \
555 fxdr_nfsv3time(tl, &(a)->va_mtime); \
556 (a)->va_vaflags &= ~VA_UTIMES_NULL; \
558 case NFSV3SATTRTIME_TOSERVER: \
559 getnanotime(&(a)->va_mtime); \
560 (a)->va_vaflags |= VA_UTIMES_NULL; \