| 1 | /*	$NetBSD: nfsm_subs.h,v 1.59 2024/12/07 02:05:55 riastradh Exp $	*/ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1989, 1993 |
| 5 | *	The Regents of the University of California. All rights reserved. |
| 6 | * |
| 7 | * This code is derived from software contributed to Berkeley by |
| 8 | * Rick Macklem at The University of Guelph. |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions |
| 12 | * are met: |
| 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. |
| 21 | * |
| 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 |
| 32 | * SUCH DAMAGE. |
| 33 | * |
| 34 | *	@(#)nfsm_subs.h	8.2 (Berkeley) 3/30/95 |
| 35 | */ |
| 36 | |
| 37 | #ifndef _NFS_NFSM_SUBS_H_ |
| 38 | #define _NFS_NFSM_SUBS_H_ |
| 39 | |
| 40 | |
| 41 | /* |
| 42 | * These macros do strange and peculiar things to mbuf chains for |
| 43 | * the assistance of the nfs code. To attempt to use them for any |
| 44 | * other purpose will be dangerous. (they make weird assumptions) |
| 45 | */ |
| 46 | |
| 47 | /* |
| 48 | * First define what the actual subs. return |
| 49 | */ |
| 50 | |
| 51 | #define	M_HASCL(m)	((m)->m_flags & M_EXT) |
| 52 | #define	NFSMADV(m, s)	(m)->m_data += (s) |
| 53 | #define	NFSMSIZ(m)	((M_HASCL(m)) ? (m)->m_ext.ext_size : \ |
| 54 | 				(((m)->m_flags & M_PKTHDR) ? MHLEN : MLEN)) |
| 55 | |
| 56 | /* |
| 57 | * NFSv2 can only handle signed 32bit quantities and some clients |
| 58 | * get confused by larger than 16bit block sizes. Limit values |
| 59 | * for better compatibility. |
| 60 | */ |
| 61 | #define NFS_V2CLAMP32(x) ((x) > INT32_MAX ? INT32_MAX : (int32_t)(x)) |
| 62 | #define NFS_V2CLAMP16(x) ((x) > INT16_MAX ? INT16_MAX : (int32_t)(x)) |
| 63 | |
| 64 | /* |
| 65 | * Now for the macros that do the simple stuff and call the functions |
| 66 | * for the hard stuff. |
| 67 | * These macros use several vars. declared in nfsm_reqhead and these |
| 68 | * vars. must not be used elsewhere unless you are careful not to corrupt |
| 69 | * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries |
| 70 | * that may be used so long as the value is not expected to retained |
| 71 | * after a macro. |
| 72 | * I know, this is kind of dorkey, but it makes the actual op functions |
| 73 | * fairly clean and deals with the mess caused by the xdr discriminating |
| 74 | * unions. |
| 75 | */ |
| 76 | |
| 77 | #define	nfsm_build(a,c,s) \ |
| 78 | 		{ if ((s) > M_TRAILINGSPACE(mb)) { \ |
| 79 | 			struct mbuf *mb2; \ |
| 80 | 			mb2 = m_get(M_WAIT, MT_DATA); \ |
| 81 | 			MCLAIM(mb2, &nfs_mowner); \ |
| 82 | 			if ((s) > MLEN) \ |
| 83 | 				panic("build > MLEN"); \ |
| 84 | 			mb->m_next = mb2; \ |
| 85 | 			mb = mb2; \ |
| 86 | 			mb->m_len = 0; \ |
| 87 | 			bpos = mtod(mb, char *); \ |
| 88 | 		} \ |
| 89 | 		(a) = (c)(bpos); \ |
| 90 | 		mb->m_len += (s); \ |
| 91 | 		bpos += (s); } |
| 92 | |
| 93 | #define nfsm_aligned(p) ALIGNED_POINTER(p,u_int32_t) |
| 94 | |
| 95 | #define	nfsm_dissect(a, c, s) \ |
| 96 | 		{ t1 = mtod(md, char *) + md->m_len-dpos; \ |
| 97 | 		if (t1 >= (s) && nfsm_aligned(dpos)) { \ |
| 98 | 			(a) = (c)(dpos); \ |
| 99 | 			dpos += (s); \ |
| 100 | 		} else if ((t1 = nfsm_disct(&md, &dpos, (s), t1, &cp2)) != 0){ \ |
| 101 | 			error = t1; \ |
| 102 | 			m_freem(mrep); \ |
| 103 | 			goto nfsmout; \ |
| 104 | 		} else { \ |
| 105 | 			(a) = (c)cp2; \ |
| 106 | 		} } |
| 107 | |
| 108 | #define nfsm_fhtom(n, v3) \ |
| 109 | 	 { if (v3) { \ |
| 110 | 			t2 = nfsm_rndup((n)->n_fhsize) + NFSX_UNSIGNED; \ |
| 111 | 			if (t2 <= M_TRAILINGSPACE(mb)) { \ |
| 112 | 				nfsm_build(tl, u_int32_t *, t2); \ |
| 113 | 				*tl++ = txdr_unsigned((n)->n_fhsize); \ |
| 114 | 				*(tl + ((t2>>2) - 2)) = 0; \ |
| 115 | 				memcpy(tl,(n)->n_fhp, (n)->n_fhsize); \ |
| 116 | 			} else if ((t2 = nfsm_strtmbuf(&mb, &bpos, \ |
| 117 | 				(void *)(n)->n_fhp, (n)->n_fhsize)) != 0) { \ |
| 118 | 				error = t2; \ |
| 119 | 				m_freem(mreq); \ |
| 120 | 				goto nfsmout; \ |
| 121 | 			} \ |
| 122 | 		} else { \ |
| 123 | 			nfsm_build(cp, void *, NFSX_V2FH); \ |
| 124 | 			memcpy(cp, (n)->n_fhp, NFSX_V2FH); \ |
| 125 | 		} } |
| 126 | |
| 127 | #define nfsm_srvfhtom(f, v3) \ |
| 128 | 		{ if (v3) { \ |
| 129 | 			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + \ |
| 130 | 			 NFSRVFH_SIZE(f)); \ |
| 131 | 			*tl++ = txdr_unsigned(NFSRVFH_SIZE(f)); \ |
| 132 | 			memcpy(tl, NFSRVFH_DATA(f), NFSRVFH_SIZE(f)); \ |
| 133 | 		} else { \ |
| 134 | 			KASSERT(NFSRVFH_SIZE(f) == NFSX_V2FH); \ |
| 135 | 			nfsm_build(cp, void *, NFSX_V2FH); \ |
| 136 | 			memcpy(cp, NFSRVFH_DATA(f), NFSX_V2FH); \ |
| 137 | 		} } |
| 138 | |
| 139 | #define nfsm_srvpostop_fh(f) \ |
| 140 | 		{ nfsm_build(tl, u_int32_t *, \ |
| 141 | 		 2 * NFSX_UNSIGNED + NFSRVFH_SIZE(f)); \ |
| 142 | 		*tl++ = nfs_true; \ |
| 143 | 		*tl++ = txdr_unsigned(NFSRVFH_SIZE(f)); \ |
| 144 | 		memcpy(tl, NFSRVFH_DATA(f), NFSRVFH_SIZE(f)); \ |
| 145 | 		} |
| 146 | |
| 147 | /* |
| 148 | * nfsm_mtofh: dissect a "resulted obj" part of create-like operations |
| 149 | * like mkdir. |
| 150 | * |
| 151 | * for nfsv3, dissect post_op_fh3 and following post_op_attr. |
| 152 | * for nfsv2, dissect fhandle and following fattr. |
| 153 | * |
| 154 | * d: (IN) the vnode of the parent directory. |
| 155 | * v: (OUT) the corresponding vnode (we allocate one if needed) |
| 156 | * v3: (IN) true for nfsv3. |
| 157 | * f: (OUT) true if we got valid filehandle. always true for nfsv2. |
| 158 | */ |
| 159 | |
| 160 | #define nfsm_mtofh(d, v, v3, f) \ |
| 161 | 		{ struct nfsnode *ttnp; nfsfh_t *ttfhp; int ttfhsize; \ |
| 162 | 		int hasattr = 0; \ |
| 163 | 		if (v3) { \ |
| 164 | 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ |
| 165 | 			(f) = fxdr_unsigned(int, *tl); \ |
| 166 | 		} else { \ |
| 167 | 			(f) = 1; \ |
| 168 | 			hasattr = 1; \ |
| 169 | 		} \ |
| 170 | 		if (f) { \ |
| 171 | 			nfsm_getfh(ttfhp, ttfhsize, (v3)); \ |
| 172 | 			if ((t1 = nfs_nget((d)->v_mount, ttfhp, ttfhsize, \ |
| 173 | 				&ttnp)) != 0) { \ |
| 174 | 				error = t1; \ |
| 175 | 				m_freem(mrep); \ |
| 176 | 				goto nfsmout; \ |
| 177 | 			} \ |
| 178 | 			(v) = NFSTOV(ttnp); \ |
| 179 | 		} \ |
| 180 | 		if (v3) { \ |
| 181 | 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ |
| 182 | 			if (f) \ |
| 183 | 				hasattr = fxdr_unsigned(int, *tl); \ |
| 184 | 			else if (fxdr_unsigned(int, *tl)) \ |
| 185 | 				nfsm_adv(NFSX_V3FATTR); \ |
| 186 | 		} \ |
| 187 | 		if (f && hasattr) \ |
| 188 | 			nfsm_loadattr((v), (struct vattr *)0, 0); \ |
| 189 | 		} |
| 190 | |
| 191 | /* |
| 192 | * nfsm_getfh: dissect a filehandle. |
| 193 | * |
| 194 | * f: (OUT) a filehandle. |
| 195 | * s: (OUT) size of the filehandle in bytes. |
| 196 | * v3: (IN) true if nfsv3. |
| 197 | */ |
| 198 | |
| 199 | #define nfsm_getfh(f, s, v3) \ |
| 200 | 		{ if (v3) { \ |
| 201 | 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ |
| 202 | 			if (((s) = fxdr_unsigned(int, *tl)) <= 0 || \ |
| 203 | 				(s) > NFSX_V3FHMAX) { \ |
| 204 | 				m_freem(mrep); \ |
| 205 | 				error = EBADRPC; \ |
| 206 | 				goto nfsmout; \ |
| 207 | 			} \ |
| 208 | 		} else \ |
| 209 | 			(s) = NFSX_V2FH; \ |
| 210 | 		nfsm_dissect((f), nfsfh_t *, nfsm_rndup(s)); } |
| 211 | |
| 212 | #define	nfsm_loadattr(v, a, flags) \ |
| 213 | 		{ struct vnode *ttvp = (v); \ |
| 214 | 		if ((t1 = nfsm_loadattrcache(&ttvp, &md, &dpos, (a), (flags))) \ |
| 215 | 		 != 0) { \ |
| 216 | 			error = t1; \ |
| 217 | 			m_freem(mrep); \ |
| 218 | 			goto nfsmout; \ |
| 219 | 		} \ |
| 220 | 		(v) = ttvp; } |
| 221 | |
| 222 | /* |
| 223 | * nfsm_postop_attr: process nfsv3 post_op_attr |
| 224 | * |
| 225 | * dissect post_op_attr. if we got a one, |
| 226 | * call nfsm_loadattrcache to update attribute cache. |
| 227 | * |
| 228 | * v: (IN/OUT) the corresponding vnode |
| 229 | * f: (OUT) true if we got valid attribute |
| 230 | * flags: (IN) flags for nfsm_loadattrcache |
| 231 | */ |
| 232 | |
| 233 | #define	nfsm_postop_attr(v, f, flags) \ |
| 234 | 		{ struct vnode *ttvp = (v); \ |
| 235 | 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ |
| 236 | 		if (((f) = fxdr_unsigned(int, *tl)) != 0) { \ |
| 237 | 			if ((t1 = nfsm_loadattrcache(&ttvp, &md, &dpos, \ |
| 238 | 				(struct vattr *)0, (flags))) != 0) { \ |
| 239 | 				error = t1; \ |
| 240 | 				(f) = 0; \ |
| 241 | 				m_freem(mrep); \ |
| 242 | 				goto nfsmout; \ |
| 243 | 			} \ |
| 244 | 			(v) = ttvp; \ |
| 245 | 		} } |
| 246 | |
| 247 | /* |
| 248 | * nfsm_wcc_data: process nfsv3 wcc_data |
| 249 | * |
| 250 | * dissect pre_op_attr and then let nfsm_postop_attr dissect post_op_attr. |
| 251 | * |
| 252 | * v: (IN/OUT) the corresponding vnode |
| 253 | * f: (IN/OUT) |
| 254 | *	NFSV3_WCCRATTR	return true if we got valid post_op_attr. |
| 255 | *	NFSV3_WCCCHK	return true if pre_op_attr's mtime is the same |
| 256 | *			as our n_mtime. (ie. our cache isn't stale.) |
| 257 | * flags: (IN) flags for nfsm_loadattrcache |
| 258 | * docheck: (IN) true if timestamp change is expected |
| 259 | */ |
| 260 | |
| 261 | /* Used as (f) for nfsm_wcc_data() */ |
| 262 | #define NFSV3_WCCRATTR	0 |
| 263 | #define NFSV3_WCCCHK	1 |
| 264 | |
| 265 | #define	nfsm_wcc_data(v, f, flags, docheck) \ |
| 266 | 		{ int ttattrf, ttretf = 0, renewctime = 0, renewnctime = 0; \ |
| 267 | 		struct timespec ctime, mtime; \ |
| 268 | 		struct nfsnode *nfsp = VTONFS(v); \ |
| 269 | 		bool haspreopattr = false; \ |
| 270 | 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ |
| 271 | 		if (*tl == nfs_true) { \ |
| 272 | 			haspreopattr = true; \ |
| 273 | 			nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED); \ |
| 274 | 			fxdr_nfsv3time(tl + 2, &mtime); \ |
| 275 | 			fxdr_nfsv3time(tl + 4, &ctime); \ |
| 276 | 			if (nfsp->n_ctime == ctime.tv_sec) \ |
| 277 | 				renewctime = 1; \ |
| 278 | 			if ((v)->v_type == VDIR) { \ |
| 279 | 				if (timespeccmp(&nfsp->n_nctime, &ctime, ==)) \ |
| 280 | 					renewnctime = 1; \ |
| 281 | 			} \ |
| 282 | 			if (f) { \ |
| 283 | 				ttretf = timespeccmp(&nfsp->n_mtime, &mtime, ==);\ |
| 284 | 			} \ |
| 285 | 		} \ |
| 286 | 		nfsm_postop_attr((v), ttattrf, (flags)); \ |
| 287 | 		nfsp = VTONFS(v); \ |
| 288 | 		if (ttattrf) { \ |
| 289 | 			if (haspreopattr && \ |
| 290 | 			 nfs_check_wccdata(nfsp, &ctime, &mtime, (docheck))) \ |
| 291 | 				renewctime = renewnctime = ttretf = 0; \ |
| 292 | 			if (renewctime) \ |
| 293 | 				nfsp->n_ctime = nfsp->n_vattr->va_ctime.tv_sec; \ |
| 294 | 			if (renewnctime) \ |
| 295 | 				nfsp->n_nctime = nfsp->n_vattr->va_ctime; \ |
| 296 | 		} \ |
| 297 | 		if (f) { \ |
| 298 | 			(f) = ttretf; \ |
| 299 | 		} else { \ |
| 300 | 			(f) = ttattrf; \ |
| 301 | 		} } |
| 302 | |
| 303 | /* If full is true, set all fields, otherwise just set mode and time fields */ |
| 304 | #define nfsm_v3attrbuild(a, full)						\ |
| 305 | 		{ if ((a)->va_mode != (mode_t)VNOVAL) {				\ |
| 306 | 			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);		\ |
| 307 | 			*tl++ = nfs_true;					\ |
| 308 | 			*tl = txdr_unsigned((a)->va_mode);			\ |
| 309 | 		} else {							\ |
| 310 | 			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);		\ |
| 311 | 			*tl = nfs_false;					\ |
| 312 | 		}								\ |
| 313 | 		if ((full) && (a)->va_uid != (uid_t)VNOVAL) {			\ |
| 314 | 			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);		\ |
| 315 | 			*tl++ = nfs_true;					\ |
| 316 | 			*tl = txdr_unsigned((a)->va_uid);			\ |
| 317 | 		} else {							\ |
| 318 | 			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);		\ |
| 319 | 			*tl = nfs_false;					\ |
| 320 | 		}								\ |
| 321 | 		if ((full) && (a)->va_gid != (gid_t)VNOVAL) {			\ |
| 322 | 			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);		\ |
| 323 | 			*tl++ = nfs_true;					\ |
| 324 | 			*tl = txdr_unsigned((a)->va_gid);			\ |
| 325 | 		} else {							\ |
| 326 | 			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);		\ |
| 327 | 			*tl = nfs_false;					\ |
| 328 | 		}								\ |
| 329 | 		if ((full) && (a)->va_size != VNOVAL) {				\ |
| 330 | 			nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);		\ |
| 331 | 			*tl++ = nfs_true;					\ |
| 332 | 			txdr_hyper((a)->va_size, tl);				\ |
| 333 | 		} else {							\ |
| 334 | 			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);		\ |
| 335 | 			*tl = nfs_false;					\ |
| 336 | 		}								\ |
| 337 | 		if ((a)->va_atime.tv_sec != VNOVAL) {				\ |
| 338 | 			if ((a)->va_atime.tv_sec != time_second) {		\ |
| 339 | 				nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);	\ |
| 340 | 				*tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);	\ |
| 341 | 				txdr_nfsv3time(&(a)->va_atime, tl);		\ |
| 342 | 			} else {						\ |
| 343 | 				nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);	\ |
| 344 | 				*tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);	\ |
| 345 | 			}							\ |
| 346 | 		} else {							\ |
| 347 | 			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);		\ |
| 348 | 			*tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);		\ |
| 349 | 		}								\ |
| 350 | 		if ((a)->va_mtime.tv_sec != VNOVAL) {				\ |
| 351 | 			if ((a)->va_mtime.tv_sec != time_second) {		\ |
| 352 | 				nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);	\ |
| 353 | 				*tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);	\ |
| 354 | 				txdr_nfsv3time(&(a)->va_mtime, tl);		\ |
| 355 | 			} else {						\ |
| 356 | 				nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);	\ |
| 357 | 				*tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);	\ |
| 358 | 			}							\ |
| 359 | 		} else {							\ |
| 360 | 			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);		\ |
| 361 | 			*tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);		\ |
| 362 | 		}								\ |
| 363 | 		} |
| 364 | |
| 365 | |
| 366 | #define	nfsm_strsiz(s,m) \ |
| 367 | 		{ nfsm_dissect(tl,uint32_t *,NFSX_UNSIGNED); \ |
| 368 | 		if ((uint32_t)((s) = fxdr_unsigned(uint32_t,*tl)) > (m)) { \ |
| 369 | 			m_freem(mrep); \ |
| 370 | 			error = EBADRPC; \ |
| 371 | 			goto nfsmout; \ |
| 372 | 		} } |
| 373 | |
| 374 | #define	nfsm_srvnamesiz(s) \ |
| 375 | 		{ nfsm_dissect(tl,uint32_t *,NFSX_UNSIGNED); \ |
| 376 | 		if ((uint32_t)((s) = fxdr_unsigned(uint32_t,*tl)) > \ |
| 377 | 		 NFS_MAXNAMLEN) \ |
| 378 | 			error = NFSERR_NAMETOL; \ |
| 379 | 		if (error) \ |
| 380 | 			nfsm_reply(0); \ |
| 381 | 		} |
| 382 | |
| 383 | #define nfsm_mtouio(p,s) \ |
| 384 | 		if ((s) > 0 && \ |
| 385 | 		 (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos)) != 0) { \ |
| 386 | 			error = t1; \ |
| 387 | 			m_freem(mrep); \ |
| 388 | 			goto nfsmout; \ |
| 389 | 		} |
| 390 | |
| 391 | #define nfsm_uiotom(p,s) \ |
| 392 | 		if ((t1 = nfsm_uiotombuf((p),&mb,(s),&bpos)) != 0) { \ |
| 393 | 			error = t1; \ |
| 394 | 			m_freem(mreq); \ |
| 395 | 			goto nfsmout; \ |
| 396 | 		} |
| 397 | |
| 398 | #define	nfsm_reqhead(n,a,s) \ |
| 399 | 		mb = mreq = nfsm_reqh((n),(a),(s),&bpos) |
| 400 | |
| 401 | #define nfsm_reqdone	m_freem(mrep); \ |
| 402 | 		nfsmout: |
| 403 | |
| 404 | #define nfsm_rndup(a)	(((a)+3)&(~0x3)) |
| 405 | #define nfsm_padlen(a)	(nfsm_rndup(a) - (a)) |
| 406 | |
| 407 | #define	nfsm_request1(v, t, p, c, rexmitp)	\ |
| 408 | 		if ((error = nfs_request((v), mreq, (t), (p), \ |
| 409 | 		 (c), &mrep, &md, &dpos, (rexmitp))) != 0) { \ |
| 410 | 			if (error & NFSERR_RETERR) \ |
| 411 | 				error &= ~NFSERR_RETERR; \ |
| 412 | 			else \ |
| 413 | 				goto nfsmout; \ |
| 414 | 		} |
| 415 | |
| 416 | #define	nfsm_request(v, t, p, c)	nfsm_request1((v), (t), (p), (c), NULL) |
| 417 | |
| 418 | #define	nfsm_strtom(a,s,m) \ |
| 419 | 		if ((s) > (m)) { \ |
| 420 | 			m_freem(mreq); \ |
| 421 | 			error = ENAMETOOLONG; \ |
| 422 | 			goto nfsmout; \ |
| 423 | 		} \ |
| 424 | 		t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \ |
| 425 | 		if (t2 <= M_TRAILINGSPACE(mb)) { \ |
| 426 | 			nfsm_build(tl,u_int32_t *,t2); \ |
| 427 | 			*tl++ = txdr_unsigned(s); \ |
| 428 | 			*(tl+((t2>>2)-2)) = 0; \ |
| 429 | 			memcpy(tl, (const char *)(a), (s)); \ |
| 430 | 		} else if ((t2 = nfsm_strtmbuf(&mb, &bpos, (a), (s))) != 0) { \ |
| 431 | 			error = t2; \ |
| 432 | 			m_freem(mreq); \ |
| 433 | 			goto nfsmout; \ |
| 434 | 		} |
| 435 | |
| 436 | #define	nfsm_srvdone \ |
| 437 | 		nfsmout: \ |
| 438 | 		return(error) |
| 439 | |
| 440 | #define	nfsm_reply(s) \ |
| 441 | 		{ \ |
| 442 | 		nfsd->nd_repstat = error; \ |
| 443 | 		if (error && !(nfsd->nd_flag & ND_NFSV3)) \ |
| 444 | 		 (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \ |
| 445 | 			mrq, &mb, &bpos); \ |
| 446 | 		else \ |
| 447 | 		 (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \ |
| 448 | 			mrq, &mb, &bpos); \ |
| 449 | 		m_freem(mrep); \ |
| 450 | 		mrep = NULL; \ |
| 451 | 		mreq = *mrq; \ |
| 452 | 		if (error && (!(nfsd->nd_flag & ND_NFSV3) || \ |
| 453 | 			error == EBADRPC)) {\ |
| 454 | 			error = 0; \ |
| 455 | 			goto nfsmout; \ |
| 456 | 			} \ |
| 457 | 		} |
| 458 | |
| 459 | #define	nfsm_writereply(s, v3) \ |
| 460 | 		{ \ |
| 461 | 		nfsd->nd_repstat = error; \ |
| 462 | 		if (error && !(v3)) \ |
| 463 | 		 (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \ |
| 464 | 			&mreq, &mb, &bpos); \ |
| 465 | 		else \ |
| 466 | 		 (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \ |
| 467 | 			&mreq, &mb, &bpos); \ |
| 468 | 		} |
| 469 | |
| 470 | #define	nfsm_adv(s) \ |
| 471 | 		{ t1 = mtod(md, char *) + md->m_len - dpos; \ |
| 472 | 		if (t1 >= (s)) { \ |
| 473 | 			dpos += (s); \ |
| 474 | 		} else if ((t1 = nfs_adv(&md, &dpos, (s), t1)) != 0) { \ |
| 475 | 			error = t1; \ |
| 476 | 			m_freem(mrep); \ |
| 477 | 			goto nfsmout; \ |
| 478 | 		} } |
| 479 | |
| 480 | #define nfsm_srvmtofh(nsfh) \ |
| 481 | 	{ uint32_t fhlen = NFSX_V3FH; \ |
| 482 | 		if (nfsd->nd_flag & ND_NFSV3) { \ |
| 483 | 			nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED); \ |
| 484 | 			fhlen = fxdr_unsigned(uint32_t, *tl); \ |
| 485 | 			CTASSERT(NFSX_V3FHMAX <= FHANDLE_SIZE_MAX); \ |
| 486 | 			if (fhlen > NFSX_V3FHMAX || \ |
| 487 | 			 (fhlen < FHANDLE_SIZE_MIN && fhlen > 0)) { \ |
| 488 | 				error = EBADRPC; \ |
| 489 | 				nfsm_reply(0); \ |
| 490 | 			} \ |
| 491 | 		} else { \ |
| 492 | 			CTASSERT(NFSX_V2FH >= FHANDLE_SIZE_MIN); \ |
| 493 | 			fhlen = NFSX_V2FH; \ |
| 494 | 		} \ |
| 495 | 		(nsfh)->nsfh_size = fhlen; \ |
| 496 | 		if (fhlen != 0) { \ |
| 497 | 			KASSERT(fhlen >= FHANDLE_SIZE_MIN); \ |
| 498 | 			KASSERT(fhlen <= FHANDLE_SIZE_MAX); \ |
| 499 | 			nfsm_dissect(tl, u_int32_t *, fhlen); \ |
| 500 | 			memcpy(NFSRVFH_DATA(nsfh), tl, fhlen); \ |
| 501 | 		} \ |
| 502 | 	} |
| 503 | |
| 504 | #define	nfsm_clget \ |
| 505 | 		if (bp >= be) { \ |
| 506 | 			if (mp == mb) \ |
| 507 | 				mp->m_len += bp-bpos; \ |
| 508 | 			mp = m_get(M_WAIT, MT_DATA); \ |
| 509 | 			MCLAIM(mp, &nfs_mowner); \ |
| 510 | 			m_clget(mp, M_WAIT); \ |
| 511 | 			mp->m_len = NFSMSIZ(mp); \ |
| 512 | 			mp2->m_next = mp; \ |
| 513 | 			mp2 = mp; \ |
| 514 | 			bp = mtod(mp, char *); \ |
| 515 | 			be = bp+mp->m_len; \ |
| 516 | 		} \ |
| 517 | 		tl = (u_int32_t *)bp |
| 518 | |
| 519 | #define	nfsm_srvfillattr(a, f) \ |
| 520 | 		nfsm_srvfattr(nfsd, (a), (f)) |
| 521 | |
| 522 | #define nfsm_srvwcc_data(br, b, ar, a) \ |
| 523 | 		nfsm_srvwcc(nfsd, (br), (b), (ar), (a), &mb, &bpos) |
| 524 | |
| 525 | #define nfsm_srvpostop_attr(r, a) \ |
| 526 | 		nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos) |
| 527 | |
| 528 | #define nfsm_srvsattr(a) \ |
| 529 | 		{ \ |
| 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_mode = nfstov_mode(*tl); \ |
| 534 | 		} \ |
| 535 | 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ |
| 536 | 		if (*tl == nfs_true) { \ |
| 537 | 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ |
| 538 | 			(a)->va_uid = fxdr_unsigned(uid_t, *tl); \ |
| 539 | 		} \ |
| 540 | 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ |
| 541 | 		if (*tl == nfs_true) { \ |
| 542 | 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ |
| 543 | 			(a)->va_gid = fxdr_unsigned(gid_t, *tl); \ |
| 544 | 		} \ |
| 545 | 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ |
| 546 | 		if (*tl == nfs_true) { \ |
| 547 | 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \ |
| 548 | 			(a)->va_size = fxdr_hyper(tl); \ |
| 549 | 		} \ |
| 550 | 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ |
| 551 | 		switch (fxdr_unsigned(int, *tl)) { \ |
| 552 | 		case NFSV3SATTRTIME_TOCLIENT: \ |
| 553 | 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \ |
| 554 | 			fxdr_nfsv3time(tl, &(a)->va_atime); \ |
| 555 | 			break; \ |
| 556 | 		case NFSV3SATTRTIME_TOSERVER: \ |
| 557 | 			getnanotime(&(a)->va_atime); \ |
| 558 | 			(a)->va_vaflags |= VA_UTIMES_NULL; \ |
| 559 | 			break; \ |
| 560 | 		}; \ |
| 561 | 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); \ |
| 562 | 		switch (fxdr_unsigned(int, *tl)) { \ |
| 563 | 		case NFSV3SATTRTIME_TOCLIENT: \ |
| 564 | 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); \ |
| 565 | 			fxdr_nfsv3time(tl, &(a)->va_mtime); \ |
| 566 | 			(a)->va_vaflags &= ~VA_UTIMES_NULL; \ |
| 567 | 			break; \ |
| 568 | 		case NFSV3SATTRTIME_TOSERVER: \ |
| 569 | 			getnanotime(&(a)->va_mtime); \ |
| 570 | 			(a)->va_vaflags |= VA_UTIMES_NULL; \ |
| 571 | 			break; \ |
| 572 | 		}; } |
| 573 | |
| 574 | #endif	/* _NFS_NFSM_SUBS_H_ */ |