]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfsm_subs.h
0f31ebb57eb8fa5067ca82dd807e3cbb4488dad5
[apple/xnu.git] / bsd / nfs / nfsm_subs.h
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
23 /*
24 * Copyright (c) 1989, 1993
25 * The Regents of the University of California. All rights reserved.
26 *
27 * This code is derived from software contributed to Berkeley by
28 * Rick Macklem at The University of Guelph.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 *
58 * @(#)nfsm_subs.h 8.2 (Berkeley) 3/30/95
59 * FreeBSD-Id: nfsm_subs.h,v 1.13 1997/07/16 09:06:30 dfr Exp $
60 */
61
62
63 #ifndef _NFS_NFSM_SUBS_H_
64 #define _NFS_NFSM_SUBS_H_
65
66
67 /*
68 * These macros do strange and peculiar things to mbuf chains for
69 * the assistance of the nfs code. To attempt to use them for any
70 * other purpose will be dangerous. (they make weird assumptions)
71 */
72
73 /*
74 * First define what the actual subs. return
75 */
76 struct mbuf *nfsm_reqh __P((struct vnode *vp, u_long procid, int hsiz,
77 caddr_t *bposp));
78 struct mbuf *nfsm_rpchead __P((struct ucred *cr, int nmflag, int procid,
79 int auth_type, int auth_len, char *auth_str,
80 int verf_len, char *verf_str,
81 struct mbuf *mrest, int mrest_len,
82 struct mbuf **mbp, u_long *xidp));
83
84 #define M_HASCL(m) ((m)->m_flags & M_EXT)
85 #define NFSMINOFF(m) \
86 if (M_HASCL(m)) \
87 (m)->m_data = (m)->m_ext.ext_buf; \
88 else if ((m)->m_flags & M_PKTHDR) \
89 (m)->m_data = (m)->m_pktdat; \
90 else \
91 (m)->m_data = (m)->m_dat
92 #define NFSMADV(m, s) (m)->m_data += (s)
93 #define NFSMSIZ(m) ((M_HASCL(m))?MCLBYTES: \
94 (((m)->m_flags & M_PKTHDR)?MHLEN:MLEN))
95
96 /*
97 * Now for the macros that do the simple stuff and call the functions
98 * for the hard stuff.
99 * These macros use several vars. declared in nfsm_reqhead and these
100 * vars. must not be used elsewhere unless you are careful not to corrupt
101 * them. The vars. starting with pN and tN (N=1,2,3,..) are temporaries
102 * that may be used so long as the value is not expected to retained
103 * after a macro.
104 * I know, this is kind of dorkey, but it makes the actual op functions
105 * fairly clean and deals with the mess caused by the xdr discriminating
106 * unions.
107 */
108
109 #define nfsm_build(a,c,s) \
110 { if ((s) > M_TRAILINGSPACE(mb)) { \
111 MGET(mb2, M_WAIT, MT_DATA); \
112 if ((s) > MLEN) \
113 panic("build > MLEN"); \
114 mb->m_next = mb2; \
115 mb = mb2; \
116 mb->m_len = 0; \
117 bpos = mtod(mb, caddr_t); \
118 } \
119 (a) = (c)(bpos); \
120 mb->m_len += (s); \
121 bpos += (s); }
122
123 #define nfsm_dissect(a, c, s) \
124 { t1 = mtod(md, caddr_t)+md->m_len-dpos; \
125 if (t1 >= (s)) { \
126 (a) = (c)(dpos); \
127 dpos += (s); \
128 } else if ((t1 = nfsm_disct(&md, &dpos, (s), t1, &cp2))) { \
129 error = t1; \
130 m_freem(mrep); \
131 goto nfsmout; \
132 } else { \
133 (a) = (c)cp2; \
134 } }
135
136 #define nfsm_fhtom(v, v3) \
137 { if (v3) { \
138 t2 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED; \
139 if (t2 <= M_TRAILINGSPACE(mb)) { \
140 nfsm_build(tl, u_long *, t2); \
141 *tl++ = txdr_unsigned(VTONFS(v)->n_fhsize); \
142 *(tl + ((t2>>2) - 2)) = 0; \
143 bcopy((caddr_t)VTONFS(v)->n_fhp,(caddr_t)tl, \
144 VTONFS(v)->n_fhsize); \
145 } else if ((t2 = nfsm_strtmbuf(&mb, &bpos, \
146 (caddr_t)VTONFS(v)->n_fhp, VTONFS(v)->n_fhsize))) { \
147 error = t2; \
148 m_freem(mreq); \
149 goto nfsmout; \
150 } \
151 } else { \
152 nfsm_build(cp, caddr_t, NFSX_V2FH); \
153 bcopy((caddr_t)VTONFS(v)->n_fhp, cp, NFSX_V2FH); \
154 } }
155
156 #define nfsm_srvfhtom(f, v3) \
157 { if (v3) { \
158 nfsm_build(tl, u_long *, NFSX_UNSIGNED + NFSX_V3FH); \
159 *tl++ = txdr_unsigned(NFSX_V3FH); \
160 bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
161 } else { \
162 nfsm_build(cp, caddr_t, NFSX_V2FH); \
163 bcopy((caddr_t)(f), cp, NFSX_V2FH); \
164 } }
165
166 #define nfsm_srvpostop_fh(f) \
167 { nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED + NFSX_V3FH); \
168 *tl++ = nfs_true; \
169 *tl++ = txdr_unsigned(NFSX_V3FH); \
170 bcopy((caddr_t)(f), (caddr_t)tl, NFSX_V3FH); \
171 }
172
173 #define nfsm_mtofh(d, v, v3, f, x) \
174 { struct nfsnode *ttnp; nfsfh_t *ttfhp; int ttfhsize; \
175 if (v3) { \
176 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
177 (f) = fxdr_unsigned(int, *tl); \
178 } else \
179 (f) = 1; \
180 if (f) { \
181 nfsm_getfh(ttfhp, ttfhsize, (v3)); \
182 if ((t1 = nfs_nget((d)->v_mount, ttfhp, ttfhsize, \
183 &ttnp))) { \
184 error = t1; \
185 m_freem(mrep); \
186 goto nfsmout; \
187 } \
188 (v) = NFSTOV(ttnp); \
189 } \
190 if (v3) { \
191 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
192 if (f) \
193 (f) = fxdr_unsigned(int, *tl); \
194 else if (fxdr_unsigned(int, *tl)) \
195 nfsm_adv(NFSX_V3FATTR); \
196 } \
197 if (f) \
198 nfsm_loadattr((v), (struct vattr *)0, (x)); \
199 }
200
201 #define nfsm_getfh(f, s, v3) \
202 { if (v3) { \
203 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
204 if (((s) = fxdr_unsigned(int, *tl)) <= 0 || \
205 (s) > NFSX_V3FHMAX) { \
206 m_freem(mrep); \
207 error = EBADRPC; \
208 goto nfsmout; \
209 } \
210 } else \
211 (s) = NFSX_V2FH; \
212 nfsm_dissect((f), nfsfh_t *, nfsm_rndup(s)); }
213
214 #define nfsm_loadattr(v, a, x) \
215 { struct vnode *ttvp = (v); \
216 if ((t1 = nfs_loadattrcache(&ttvp, &md, &dpos, (a), 0, \
217 (x)))) { \
218 error = t1; \
219 m_freem(mrep); \
220 goto nfsmout; \
221 } \
222 (v) = ttvp; }
223
224 #define nfsm_postop_attr(v, f, x) \
225 { struct vnode *ttvp = (v); \
226 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
227 if (((f) = fxdr_unsigned(int, *tl))) { \
228 if ((t1 = nfs_loadattrcache(&ttvp, &md, &dpos, \
229 (struct vattr *)0, 1, (x)))) { \
230 error = t1; \
231 (f) = 0; \
232 m_freem(mrep); \
233 goto nfsmout; \
234 } \
235 if (*(x) == 0) \
236 (f) = 0; \
237 (v) = ttvp; \
238 } }
239
240 /* Used as (f) for nfsm_wcc_data() */
241 #define NFSV3_WCCRATTR 0
242 #define NFSV3_WCCCHK 1
243
244 #define nfsm_wcc_data(v, f, x) \
245 { int ttattrf, ttretf = 0; \
246 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
247 if (*tl == nfs_true) { \
248 nfsm_dissect(tl, u_long *, 6 * NFSX_UNSIGNED); \
249 if (f) \
250 ttretf = (VTONFS(v)->n_mtime == \
251 fxdr_unsigned(u_long, *(tl + 2))); \
252 } \
253 nfsm_postop_attr((v), ttattrf, (x)); \
254 if (f) { \
255 (f) = ttretf; \
256 } else { \
257 (f) = ttattrf; \
258 } }
259
260 #define nfsm_v3sattr(s, a, u, g) \
261 { (s)->sa_modetrue = nfs_true; \
262 (s)->sa_mode = vtonfsv3_mode((a)->va_mode); \
263 (s)->sa_uidtrue = nfs_true; \
264 (s)->sa_uid = txdr_unsigned(u); \
265 (s)->sa_gidtrue = nfs_true; \
266 (s)->sa_gid = txdr_unsigned(g); \
267 (s)->sa_sizefalse = nfs_false; \
268 (s)->sa_atimetype = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); \
269 (s)->sa_mtimetype = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); \
270 }
271
272 #define nfsm_strsiz(s,m) \
273 { nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \
274 if (((s) = fxdr_unsigned(long,*tl)) > (m)) { \
275 m_freem(mrep); \
276 error = EBADRPC; \
277 goto nfsmout; \
278 } }
279
280 #define nfsm_srvstrsiz(s,m) \
281 { nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \
282 if (((s) = fxdr_unsigned(long,*tl)) > (m) || (s) <= 0) { \
283 error = EBADRPC; \
284 nfsm_reply(0); \
285 } }
286
287 #define nfsm_srvnamesiz(s) \
288 { nfsm_dissect(tl,u_long *,NFSX_UNSIGNED); \
289 if (((s) = fxdr_unsigned(long,*tl)) > NFS_MAXNAMLEN) \
290 error = NFSERR_NAMETOL; \
291 if ((s) <= 0) \
292 error = EBADRPC; \
293 if (error) \
294 nfsm_reply(0); \
295 }
296
297 #define nfsm_mtouio(p,s) \
298 if ((s) > 0 && \
299 (t1 = nfsm_mbuftouio(&md,(p),(s),&dpos))) { \
300 error = t1; \
301 m_freem(mrep); \
302 goto nfsmout; \
303 }
304
305 #define nfsm_uiotom(p,s) \
306 if ((t1 = nfsm_uiotombuf((p),&mb,(s),&bpos))) { \
307 error = t1; \
308 m_freem(mreq); \
309 goto nfsmout; \
310 }
311
312 #define nfsm_reqhead(v,a,s) \
313 mb = mreq = nfsm_reqh((v),(a),(s),&bpos)
314
315 #define nfsm_reqdone m_freem(mrep); \
316 nfsmout:
317
318 #define nfsm_rndup(a) (((a)+3)&(~0x3))
319
320 /*
321 * We seem to see cases mainly on shutdown where the vnode got recycled
322 * on use while waiting on server. Maybe nfs vnode locking will help if
323 * we implement that, but for now, check for bad vnodes and return an
324 * error. This call spot should catch most of them. Note that NFSv2
325 * just goes to nfsmout here, while nfsV3 goes back to caller's next
326 * line for post-processing. It will do a nfsm_reqdone also making
327 * m_freem(mrep). Wondering if some of our freeing problems could be
328 * due to nfsv3 calling nfsm_reqdone unlike nfsv2. Separate problem.
329 */
330 #define nfsm_request(v, t, p, c, x) \
331 { \
332 int nfsv3 = (VFSTONFS((v)->v_mount))->nm_flag & NFSMNT_NFSV3; \
333 if ((error = nfs_request((v), mreq, (t), (p), \
334 (c), &mrep, &md, &dpos, (x)))) { \
335 if (error & NFSERR_RETERR) \
336 error &= ~NFSERR_RETERR; \
337 else \
338 goto nfsmout; \
339 } \
340 else if ((v)->v_type==VBAD) { \
341 error = EINVAL; \
342 if (!nfsv3) \
343 goto nfsmout; \
344 } \
345 }
346
347 #define nfsm_strtom(a,s,m) \
348 if ((s) > (m)) { \
349 m_freem(mreq); \
350 error = ENAMETOOLONG; \
351 goto nfsmout; \
352 } \
353 t2 = nfsm_rndup(s)+NFSX_UNSIGNED; \
354 if (t2 <= M_TRAILINGSPACE(mb)) { \
355 nfsm_build(tl,u_long *,t2); \
356 *tl++ = txdr_unsigned(s); \
357 *(tl+((t2>>2)-2)) = 0; \
358 bcopy((caddr_t)(a), (caddr_t)tl, (s)); \
359 } else if ((t2 = nfsm_strtmbuf(&mb, &bpos, (a), (s)))) { \
360 error = t2; \
361 m_freem(mreq); \
362 goto nfsmout; \
363 }
364
365 #define nfsm_srvdone \
366 nfsmout: \
367 return(error)
368
369 #define nfsm_reply(s) \
370 { \
371 nfsd->nd_repstat = error; \
372 if (error && !(nfsd->nd_flag & ND_NFSV3)) \
373 (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
374 mrq, &mb, &bpos); \
375 else \
376 (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
377 mrq, &mb, &bpos); \
378 m_freem(mrep); \
379 mreq = *mrq; \
380 if (error && (!(nfsd->nd_flag & ND_NFSV3) || \
381 error == EBADRPC)) \
382 return(0); \
383 }
384
385 #define nfsm_writereply(s, v3) \
386 { \
387 nfsd->nd_repstat = error; \
388 if (error && !(v3)) \
389 (void) nfs_rephead(0, nfsd, slp, error, cache, &frev, \
390 &mreq, &mb, &bpos); \
391 else \
392 (void) nfs_rephead((s), nfsd, slp, error, cache, &frev, \
393 &mreq, &mb, &bpos); \
394 }
395
396 #define nfsm_adv(s) \
397 { t1 = mtod(md, caddr_t)+md->m_len-dpos; \
398 if (t1 >= (s)) { \
399 dpos += (s); \
400 } else if ((t1 = nfs_adv(&md, &dpos, (s), t1))) { \
401 error = t1; \
402 m_freem(mrep); \
403 goto nfsmout; \
404 } }
405
406 #define nfsm_srvmtofh(f) \
407 { if (nfsd->nd_flag & ND_NFSV3) { \
408 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
409 if (fxdr_unsigned(int, *tl) != NFSX_V3FH) { \
410 error = EBADRPC; \
411 nfsm_reply(0); \
412 } \
413 } \
414 nfsm_dissect(tl, u_long *, NFSX_V3FH); \
415 bcopy((caddr_t)tl, (caddr_t)(f), NFSX_V3FH); \
416 if ((nfsd->nd_flag & ND_NFSV3) == 0) \
417 nfsm_adv(NFSX_V2FH - NFSX_V3FH); \
418 }
419
420 #define nfsm_clget \
421 if (bp >= be) { \
422 if (mp == mb) \
423 mp->m_len += bp-bpos; \
424 MGET(mp, M_WAIT, MT_DATA); \
425 MCLGET(mp, M_WAIT); \
426 mp->m_len = NFSMSIZ(mp); \
427 mp2->m_next = mp; \
428 mp2 = mp; \
429 bp = mtod(mp, caddr_t); \
430 be = bp+mp->m_len; \
431 } \
432 tl = (u_long *)bp
433
434 #define nfsm_srvfillattr(a, f) \
435 nfsm_srvfattr(nfsd, (a), (f))
436
437 #define nfsm_srvwcc_data(br, b, ar, a) \
438 nfsm_srvwcc(nfsd, (br), (b), (ar), (a), &mb, &bpos)
439
440 #define nfsm_srvpostop_attr(r, a) \
441 nfsm_srvpostopattr(nfsd, (r), (a), &mb, &bpos)
442
443 #define nfsm_srvsattr(a) \
444 { nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
445 if (*tl == nfs_true) { \
446 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
447 (a)->va_mode = nfstov_mode(*tl); \
448 } \
449 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
450 if (*tl == nfs_true) { \
451 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
452 (a)->va_uid = fxdr_unsigned(uid_t, *tl); \
453 } \
454 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
455 if (*tl == nfs_true) { \
456 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
457 (a)->va_gid = fxdr_unsigned(gid_t, *tl); \
458 } \
459 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
460 if (*tl == nfs_true) { \
461 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED); \
462 fxdr_hyper(tl, &(a)->va_size); \
463 } \
464 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
465 switch (fxdr_unsigned(int, *tl)) { \
466 case NFSV3SATTRTIME_TOCLIENT: \
467 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED); \
468 fxdr_nfsv3time(tl, &(a)->va_atime); \
469 break; \
470 case NFSV3SATTRTIME_TOSERVER: \
471 (a)->va_atime.tv_sec = time.tv_sec; \
472 (a)->va_atime.tv_nsec = time.tv_usec * 1000; \
473 break; \
474 }; \
475 nfsm_dissect(tl, u_long *, NFSX_UNSIGNED); \
476 switch (fxdr_unsigned(int, *tl)) { \
477 case NFSV3SATTRTIME_TOCLIENT: \
478 nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED); \
479 fxdr_nfsv3time(tl, &(a)->va_mtime); \
480 break; \
481 case NFSV3SATTRTIME_TOSERVER: \
482 (a)->va_mtime.tv_sec = time.tv_sec; \
483 (a)->va_mtime.tv_nsec = time.tv_usec * 1000; \
484 break; \
485 }; }
486
487 #endif