nfs_serv.c revision 1.129 1 /* $NetBSD: nfs_serv.c,v 1.129 2007/07/27 10:03:58 yamt 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 * @(#)nfs_serv.c 8.8 (Berkeley) 7/31/95
35 */
36
37 /*
38 * nfs version 2 and 3 server calls to vnode ops
39 * - these routines generally have 3 phases
40 * 1 - break down and validate rpc request in mbuf list
41 * 2 - do the vnode ops for the request
42 * (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c)
43 * 3 - build the rpc reply in an mbuf list
44 * nb:
45 * - do not mix the phases, since the nfsm_?? macros can return failures
46 * on a bad rpc or similar and do not do any vrele() or vput()'s
47 *
48 * - the nfsm_reply() macro generates an nfs rpc reply with the nfs
49 * error number iff error != 0 whereas
50 * returning an error from the server function implies a fatal error
51 * such as a badly constructed rpc request that should be dropped without
52 * a reply.
53 * For Version 3, nfsm_reply() does not return for the error case, since
54 * most version 3 rpcs return more than the status for error cases.
55 */
56
57 #include <sys/cdefs.h>
58 __KERNEL_RCSID(0, "$NetBSD: nfs_serv.c,v 1.129 2007/07/27 10:03:58 yamt Exp $");
59
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/proc.h>
63 #include <sys/file.h>
64 #include <sys/namei.h>
65 #include <sys/vnode.h>
66 #include <sys/mount.h>
67 #include <sys/socket.h>
68 #include <sys/socketvar.h>
69 #include <sys/mbuf.h>
70 #include <sys/dirent.h>
71 #include <sys/stat.h>
72 #include <sys/kernel.h>
73 #include <sys/hash.h>
74 #include <sys/kauth.h>
75
76 #include <uvm/uvm.h>
77
78 #include <nfs/nfsproto.h>
79 #include <nfs/rpcv2.h>
80 #include <nfs/nfs.h>
81 #include <nfs/xdr_subs.h>
82 #include <nfs/nfsm_subs.h>
83 #include <nfs/nfs_var.h>
84
85 /* Global vars */
86 extern u_int32_t nfs_xdrneg1;
87 extern u_int32_t nfs_false, nfs_true;
88 extern enum vtype nv3tov_type[8];
89 extern struct nfsstats nfsstats;
90 extern const nfstype nfsv2_type[9];
91 extern const nfstype nfsv3_type[9];
92 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
93 int nfsd_use_loan = 1; /* use page-loan for READ OP */
94
95 #define nqsrv_getl(vp, rw) /* nothing */
96
97 /*
98 * nfs v3 access service
99 */
100 int
101 nfsrv3_access(nfsd, slp, lwp, mrq)
102 struct nfsrv_descript *nfsd;
103 struct nfssvc_sock *slp;
104 struct lwp *lwp;
105 struct mbuf **mrq;
106 {
107 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
108 struct mbuf *nam = nfsd->nd_nam;
109 char *dpos = nfsd->nd_dpos;
110 kauth_cred_t cred = nfsd->nd_cr;
111 struct vnode *vp;
112 nfsrvfh_t nsfh;
113 u_int32_t *tl;
114 int32_t t1;
115 char *bpos;
116 int error = 0, rdonly, cache = 0, getret;
117 char *cp2;
118 struct mbuf *mb, *mreq;
119 struct vattr va;
120 u_long inmode, testmode, outmode;
121 u_quad_t frev;
122
123 nfsm_srvmtofh(&nsfh);
124 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
125 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam, &rdonly,
126 (nfsd->nd_flag & ND_KERBAUTH), false);
127 if (error) {
128 nfsm_reply(NFSX_UNSIGNED);
129 nfsm_srvpostop_attr(1, (struct vattr *)0);
130 return (0);
131 }
132 inmode = fxdr_unsigned(u_int32_t, *tl);
133 outmode = 0;
134 if ((inmode & NFSV3ACCESS_READ) &&
135 nfsrv_access(vp, VREAD, cred, rdonly, lwp, 0) == 0)
136 outmode |= NFSV3ACCESS_READ;
137 if (vp->v_type != VDIR) {
138 testmode = inmode & (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
139 if (testmode &&
140 nfsrv_access(vp, VWRITE, cred, rdonly, lwp, 0) == 0)
141 outmode |= testmode;
142 if ((inmode & NFSV3ACCESS_EXECUTE) &&
143 nfsrv_access(vp, VEXEC, cred, rdonly, lwp, 0) == 0)
144 outmode |= NFSV3ACCESS_EXECUTE;
145 } else {
146 testmode = inmode & (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
147 NFSV3ACCESS_DELETE);
148 if (testmode &&
149 nfsrv_access(vp, VWRITE, cred, rdonly, lwp, 0) == 0)
150 outmode |= testmode;
151 if ((inmode & NFSV3ACCESS_LOOKUP) &&
152 nfsrv_access(vp, VEXEC, cred, rdonly, lwp, 0) == 0)
153 outmode |= NFSV3ACCESS_LOOKUP;
154 }
155 getret = VOP_GETATTR(vp, &va, cred, lwp);
156 vput(vp);
157 nfsm_reply(NFSX_POSTOPATTR(1) + NFSX_UNSIGNED);
158 nfsm_srvpostop_attr(getret, &va);
159 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
160 *tl = txdr_unsigned(outmode);
161 nfsm_srvdone;
162 }
163
164 /*
165 * nfs getattr service
166 */
167 int
168 nfsrv_getattr(nfsd, slp, lwp, mrq)
169 struct nfsrv_descript *nfsd;
170 struct nfssvc_sock *slp;
171 struct lwp *lwp;
172 struct mbuf **mrq;
173 {
174 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
175 struct mbuf *nam = nfsd->nd_nam;
176 char *dpos = nfsd->nd_dpos;
177 kauth_cred_t cred = nfsd->nd_cr;
178 struct nfs_fattr *fp;
179 struct vattr va;
180 struct vnode *vp;
181 nfsrvfh_t nsfh;
182 u_int32_t *tl;
183 int32_t t1;
184 char *bpos;
185 int error = 0, rdonly, cache = 0;
186 char *cp2;
187 struct mbuf *mb, *mreq;
188 u_quad_t frev;
189
190 nfsm_srvmtofh(&nsfh);
191 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam, &rdonly,
192 (nfsd->nd_flag & ND_KERBAUTH), false);
193 if (error) {
194 nfsm_reply(0);
195 return (0);
196 }
197 nqsrv_getl(vp, ND_READ);
198 error = VOP_GETATTR(vp, &va, cred, lwp);
199 vput(vp);
200 nfsm_reply(NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
201 if (error)
202 return (0);
203 nfsm_build(fp, struct nfs_fattr *, NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
204 nfsm_srvfillattr(&va, fp);
205 nfsm_srvdone;
206 }
207
208 /*
209 * nfs setattr service
210 */
211 int
212 nfsrv_setattr(nfsd, slp, lwp, mrq)
213 struct nfsrv_descript *nfsd;
214 struct nfssvc_sock *slp;
215 struct lwp *lwp;
216 struct mbuf **mrq;
217 {
218 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
219 struct mbuf *nam = nfsd->nd_nam;
220 char *dpos = nfsd->nd_dpos;
221 kauth_cred_t cred = nfsd->nd_cr;
222 struct vattr va, preat;
223 struct nfsv2_sattr *sp;
224 struct nfs_fattr *fp;
225 struct vnode *vp;
226 nfsrvfh_t nsfh;
227 u_int32_t *tl;
228 int32_t t1;
229 char *bpos;
230 int error = 0, rdonly, cache = 0, preat_ret = 1, postat_ret = 1;
231 int v3 = (nfsd->nd_flag & ND_NFSV3), gcheck = 0;
232 char *cp2;
233 struct mbuf *mb, *mreq;
234 u_quad_t frev;
235 struct timespec guard;
236
237 memset(&guard, 0, sizeof guard); /* XXX gcc */
238
239 nfsm_srvmtofh(&nsfh);
240 VATTR_NULL(&va);
241 if (v3) {
242 nfsm_srvsattr(&va);
243 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
244 gcheck = fxdr_unsigned(int, *tl);
245 if (gcheck) {
246 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
247 fxdr_nfsv3time(tl, &guard);
248 }
249 } else {
250 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
251 /*
252 * Nah nah nah nah na nah
253 * There is a bug in the Sun client that puts 0xffff in the mode
254 * field of sattr when it should put in 0xffffffff. The u_short
255 * doesn't sign extend.
256 * --> check the low order 2 bytes for 0xffff
257 */
258 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
259 va.va_mode = nfstov_mode(sp->sa_mode);
260 if (sp->sa_uid != nfs_xdrneg1)
261 va.va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
262 if (sp->sa_gid != nfs_xdrneg1)
263 va.va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
264 if (sp->sa_size != nfs_xdrneg1)
265 va.va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
266 if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
267 #ifdef notyet
268 fxdr_nfsv2time(&sp->sa_atime, &va.va_atime);
269 #else
270 va.va_atime.tv_sec =
271 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
272 va.va_atime.tv_nsec = 0;
273 #endif
274 }
275 if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
276 fxdr_nfsv2time(&sp->sa_mtime, &va.va_mtime);
277
278 }
279
280 /*
281 * Now that we have all the fields, lets do it.
282 */
283 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam, &rdonly,
284 (nfsd->nd_flag & ND_KERBAUTH), false);
285 if (error) {
286 nfsm_reply(2 * NFSX_UNSIGNED);
287 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, &va);
288 return (0);
289 }
290 nqsrv_getl(vp, ND_WRITE);
291 if (v3) {
292 error = preat_ret = VOP_GETATTR(vp, &preat, cred, lwp);
293 if (!error && gcheck &&
294 (preat.va_ctime.tv_sec != guard.tv_sec ||
295 preat.va_ctime.tv_nsec != guard.tv_nsec))
296 error = NFSERR_NOT_SYNC;
297 if (error) {
298 vput(vp);
299 nfsm_reply(NFSX_WCCDATA(v3));
300 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, &va);
301 return (0);
302 }
303 }
304
305 /*
306 * If the size is being changed write acces is required, otherwise
307 * just check for a read only file system.
308 */
309 if (va.va_size == ((u_quad_t)((quad_t) -1))) {
310 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
311 error = EROFS;
312 goto out;
313 }
314 } else {
315 if (vp->v_type == VDIR) {
316 error = EISDIR;
317 goto out;
318 } else if ((error = nfsrv_access(vp, VWRITE, cred, rdonly,
319 lwp, 0)) != 0)
320 goto out;
321 }
322 error = VOP_SETATTR(vp, &va, cred, lwp);
323 postat_ret = VOP_GETATTR(vp, &va, cred, lwp);
324 if (!error)
325 error = postat_ret;
326 out:
327 vput(vp);
328 nfsm_reply(NFSX_WCCORFATTR(v3));
329 if (v3) {
330 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, &va);
331 return (0);
332 } else {
333 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
334 nfsm_srvfillattr(&va, fp);
335 }
336 nfsm_srvdone;
337 }
338
339 /*
340 * nfs lookup rpc
341 */
342 int
343 nfsrv_lookup(nfsd, slp, lwp, mrq)
344 struct nfsrv_descript *nfsd;
345 struct nfssvc_sock *slp;
346 struct lwp *lwp;
347 struct mbuf **mrq;
348 {
349 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
350 struct mbuf *nam = nfsd->nd_nam;
351 char *dpos = nfsd->nd_dpos;
352 kauth_cred_t cred = nfsd->nd_cr;
353 struct nfs_fattr *fp;
354 struct nameidata nd, ind, *ndp = &nd;
355 struct vnode *vp, *dirp;
356 nfsrvfh_t nsfh;
357 char *cp;
358 u_int32_t *tl;
359 int32_t t1;
360 char *bpos;
361 int error = 0, cache = 0, dirattr_ret = 1;
362 uint32_t len;
363 int v3 = (nfsd->nd_flag & ND_NFSV3), pubflag;
364 char *cp2;
365 struct mbuf *mb, *mreq;
366 struct vattr va, dirattr;
367 u_quad_t frev;
368
369 nfsm_srvmtofh(&nsfh);
370 nfsm_srvnamesiz(len);
371
372 pubflag = nfs_ispublicfh(&nsfh);
373
374 nd.ni_cnd.cn_cred = cred;
375 nd.ni_cnd.cn_nameiop = LOOKUP;
376 nd.ni_cnd.cn_flags = LOCKLEAF | SAVESTART;
377 error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
378 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), pubflag);
379
380 if (!error && pubflag) {
381 if (nd.ni_vp->v_type == VDIR && nfs_pub.np_index != NULL) {
382 /*
383 * Setup call to lookup() to see if we can find
384 * the index file. Arguably, this doesn't belong
385 * in a kernel.. Ugh.
386 */
387 ind = nd;
388 VOP_UNLOCK(nd.ni_vp, 0);
389 ind.ni_pathlen = strlen(nfs_pub.np_index);
390 ind.ni_cnd.cn_nameptr = ind.ni_cnd.cn_pnbuf =
391 nfs_pub.np_index;
392 ind.ni_startdir = nd.ni_vp;
393 VREF(ind.ni_startdir);
394 error = lookup(&ind);
395 if (!error) {
396 /*
397 * Found an index file. Get rid of
398 * the old references.
399 */
400 if (dirp)
401 vrele(dirp);
402 dirp = nd.ni_vp;
403 vrele(nd.ni_startdir);
404 ndp = &ind;
405 } else
406 error = 0;
407 }
408 /*
409 * If the public filehandle was used, check that this lookup
410 * didn't result in a filehandle outside the publicly exported
411 * filesystem.
412 */
413
414 if (!error && ndp->ni_vp->v_mount != nfs_pub.np_mount) {
415 vput(nd.ni_vp);
416 error = EPERM;
417 }
418 }
419
420 if (dirp) {
421 if (v3)
422 dirattr_ret = VOP_GETATTR(dirp, &dirattr, cred, lwp);
423 vrele(dirp);
424 }
425
426 if (error) {
427 nfsm_reply(NFSX_POSTOPATTR(v3));
428 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
429 return (0);
430 }
431
432 nqsrv_getl(ndp->ni_startdir, ND_READ);
433 PNBUF_PUT(nd.ni_cnd.cn_pnbuf);
434 vp = ndp->ni_vp;
435 error = nfsrv_composefh(vp, &nsfh, v3);
436 if (!error)
437 error = VOP_GETATTR(vp, &va, cred, lwp);
438 vput(vp);
439 vrele(ndp->ni_startdir);
440 nfsm_reply(NFSX_SRVFH(&nsfh, v3) + NFSX_POSTOPORFATTR(v3) +
441 NFSX_POSTOPATTR(v3));
442 if (error) {
443 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
444 return (0);
445 }
446 nfsm_srvfhtom(&nsfh, v3);
447 if (v3) {
448 nfsm_srvpostop_attr(0, &va);
449 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
450 } else {
451 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
452 nfsm_srvfillattr(&va, fp);
453 }
454 nfsm_srvdone;
455 }
456
457 /*
458 * nfs readlink service
459 */
460 int
461 nfsrv_readlink(nfsd, slp, lwp, mrq)
462 struct nfsrv_descript *nfsd;
463 struct nfssvc_sock *slp;
464 struct lwp *lwp;
465 struct mbuf **mrq;
466 {
467 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
468 struct mbuf *nam = nfsd->nd_nam;
469 char *dpos = nfsd->nd_dpos;
470 kauth_cred_t cred = nfsd->nd_cr;
471 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
472 struct iovec *ivp = iv;
473 struct mbuf *mp;
474 u_int32_t *tl;
475 int32_t t1;
476 char *bpos;
477 int error = 0, rdonly, cache = 0, i, padlen, getret;
478 uint32_t len;
479 int v3 = (nfsd->nd_flag & ND_NFSV3);
480 char *cp2;
481 struct mbuf *mb, *mp2 = NULL, *mp3 = NULL, *mreq;
482 struct vnode *vp;
483 struct vattr attr;
484 nfsrvfh_t nsfh;
485 struct uio io, *uiop = &io;
486 u_quad_t frev;
487
488 nfsm_srvmtofh(&nsfh);
489 len = 0;
490 i = 0;
491 while (len < NFS_MAXPATHLEN) {
492 mp = m_get(M_WAIT, MT_DATA);
493 MCLAIM(mp, &nfs_mowner);
494 m_clget(mp, M_WAIT);
495 mp->m_len = NFSMSIZ(mp);
496 if (len == 0)
497 mp3 = mp2 = mp;
498 else {
499 mp2->m_next = mp;
500 mp2 = mp;
501 }
502 if ((len+mp->m_len) > NFS_MAXPATHLEN) {
503 mp->m_len = NFS_MAXPATHLEN-len;
504 len = NFS_MAXPATHLEN;
505 } else
506 len += mp->m_len;
507 ivp->iov_base = mtod(mp, void *);
508 ivp->iov_len = mp->m_len;
509 i++;
510 ivp++;
511 }
512 uiop->uio_iov = iv;
513 uiop->uio_iovcnt = i;
514 uiop->uio_offset = 0;
515 uiop->uio_resid = len;
516 uiop->uio_rw = UIO_READ;
517 UIO_SETUP_SYSSPACE(uiop);
518 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
519 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
520 if (error) {
521 m_freem(mp3);
522 nfsm_reply(2 * NFSX_UNSIGNED);
523 nfsm_srvpostop_attr(1, (struct vattr *)0);
524 return (0);
525 }
526 if (vp->v_type != VLNK) {
527 if (v3)
528 error = EINVAL;
529 else
530 error = ENXIO;
531 goto out;
532 }
533 nqsrv_getl(vp, ND_READ);
534 error = VOP_READLINK(vp, uiop, cred);
535 out:
536 getret = VOP_GETATTR(vp, &attr, cred, lwp);
537 vput(vp);
538 if (error)
539 m_freem(mp3);
540 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_UNSIGNED);
541 if (v3) {
542 nfsm_srvpostop_attr(getret, &attr);
543 if (error)
544 return (0);
545 }
546 len -= uiop->uio_resid;
547 padlen = nfsm_padlen(len);
548 if (uiop->uio_resid || padlen)
549 nfs_zeropad(mp3, uiop->uio_resid, padlen);
550 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
551 *tl = txdr_unsigned(len);
552 mb->m_next = mp3;
553 nfsm_srvdone;
554 }
555
556 /*
557 * nfs read service
558 */
559 int
560 nfsrv_read(nfsd, slp, lwp, mrq)
561 struct nfsrv_descript *nfsd;
562 struct nfssvc_sock *slp;
563 struct lwp *lwp;
564 struct mbuf **mrq;
565 {
566 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
567 struct mbuf *nam = nfsd->nd_nam;
568 char *dpos = nfsd->nd_dpos;
569 kauth_cred_t cred = nfsd->nd_cr;
570 struct mbuf *m;
571 struct nfs_fattr *fp;
572 u_int32_t *tl;
573 int32_t t1;
574 int i;
575 char *bpos;
576 int error = 0, rdonly, cache = 0, getret;
577 int v3 = (nfsd->nd_flag & ND_NFSV3);
578 uint32_t reqlen, len, cnt, left;
579 int padlen;
580 char *cp2;
581 struct mbuf *mb, *mreq;
582 struct vnode *vp;
583 nfsrvfh_t nsfh;
584 struct uio io, *uiop = &io;
585 struct vattr va;
586 off_t off;
587 u_quad_t frev;
588
589 nfsm_srvmtofh(&nsfh);
590 if (v3) {
591 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
592 off = fxdr_hyper(tl);
593 } else {
594 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
595 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
596 }
597 nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED);
598 reqlen = fxdr_unsigned(uint32_t, *tl);
599 reqlen = MIN(reqlen, NFS_SRVMAXDATA(nfsd));
600 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
601 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
602 if (error) {
603 nfsm_reply(2 * NFSX_UNSIGNED);
604 nfsm_srvpostop_attr(1, (struct vattr *)0);
605 return (0);
606 }
607 if (vp->v_type != VREG) {
608 if (v3)
609 error = EINVAL;
610 else
611 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
612 }
613 if (!error) {
614 nqsrv_getl(vp, ND_READ);
615 if ((error = nfsrv_access(vp, VREAD, cred, rdonly, lwp, 1)) != 0)
616 error = nfsrv_access(vp, VEXEC, cred, rdonly, lwp, 1);
617 }
618 getret = VOP_GETATTR(vp, &va, cred, lwp);
619 if (!error)
620 error = getret;
621 if (error) {
622 vput(vp);
623 nfsm_reply(NFSX_POSTOPATTR(v3));
624 nfsm_srvpostop_attr(getret, &va);
625 return (0);
626 }
627 if (off >= va.va_size)
628 cnt = 0;
629 else if ((off + reqlen) > va.va_size)
630 cnt = va.va_size - off;
631 else
632 cnt = reqlen;
633 nfsm_reply(NFSX_POSTOPORFATTR(v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
634 if (v3) {
635 nfsm_build(tl, u_int32_t *, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
636 *tl++ = nfs_true;
637 fp = (struct nfs_fattr *)tl;
638 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
639 } else {
640 nfsm_build(tl, u_int32_t *, NFSX_V2FATTR + NFSX_UNSIGNED);
641 fp = (struct nfs_fattr *)tl;
642 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
643 }
644 len = left = cnt;
645 if (cnt > 0) {
646 if (nfsd_use_loan) {
647 struct vm_page **pgpp;
648 voff_t pgoff = trunc_page(off);
649 int npages;
650 vaddr_t lva;
651
652 npages = (round_page(off + cnt) - pgoff) >> PAGE_SHIFT;
653 KASSERT(npages <= M_EXT_MAXPAGES); /* XXX */
654
655 /* allocate kva for mbuf data */
656 lva = sokvaalloc(npages << PAGE_SHIFT, slp->ns_so);
657 if (lva == 0) {
658 /* fall back to VOP_READ */
659 goto loan_fail;
660 }
661
662 /* allocate mbuf */
663 m = m_get(M_WAIT, MT_DATA);
664 MCLAIM(m, &nfs_mowner);
665 pgpp = m->m_ext.ext_pgs;
666
667 /* loan pages */
668 error = uvm_loanuobjpages(&vp->v_uobj, pgoff, npages,
669 pgpp);
670 if (error) {
671 sokvafree(lva, npages << PAGE_SHIFT);
672 m_free(m);
673 if (error == EBUSY)
674 goto loan_fail;
675 goto read_error;
676 }
677
678 /* associate kva to mbuf */
679 MEXTADD(m, (void *)(lva + ((vaddr_t)off & PAGE_MASK)),
680 cnt, M_MBUF, soloanfree, slp->ns_so);
681 m->m_flags |= M_EXT_PAGES | M_EXT_ROMAP;
682 m->m_len = cnt;
683
684 /* map pages */
685 for (i = 0; i < npages; i++) {
686 pmap_kenter_pa(lva, VM_PAGE_TO_PHYS(pgpp[i]),
687 VM_PROT_READ);
688 lva += PAGE_SIZE;
689 }
690
691 pmap_update(pmap_kernel());
692
693 mb->m_next = m;
694 mb = m;
695 error = 0;
696 uiop->uio_resid = 0;
697 } else {
698 struct iovec *iv;
699 struct iovec *iv2;
700 struct mbuf *m2;
701 int siz;
702 loan_fail:
703 /*
704 * Generate the mbuf list with the uio_iov ref. to it.
705 */
706 i = 0;
707 m = m2 = mb;
708 while (left > 0) {
709 siz = min(M_TRAILINGSPACE(m), left);
710 if (siz > 0) {
711 left -= siz;
712 i++;
713 }
714 if (left > 0) {
715 m = m_get(M_WAIT, MT_DATA);
716 MCLAIM(m, &nfs_mowner);
717 m_clget(m, M_WAIT);
718 m->m_len = 0;
719 m2->m_next = m;
720 m2 = m;
721 }
722 }
723 iv = malloc(i * sizeof(struct iovec), M_TEMP, M_WAITOK);
724 uiop->uio_iov = iv2 = iv;
725 m = mb;
726 left = cnt;
727 i = 0;
728 while (left > 0) {
729 if (m == NULL)
730 panic("nfsrv_read iov");
731 siz = min(M_TRAILINGSPACE(m), left);
732 if (siz > 0) {
733 iv->iov_base = mtod(m, char *) +
734 m->m_len;
735 iv->iov_len = siz;
736 m->m_len += siz;
737 left -= siz;
738 iv++;
739 i++;
740 }
741 m = m->m_next;
742 }
743 uiop->uio_iovcnt = i;
744 uiop->uio_offset = off;
745 uiop->uio_resid = cnt;
746 uiop->uio_rw = UIO_READ;
747 UIO_SETUP_SYSSPACE(uiop);
748 error = VOP_READ(vp, uiop, IO_NODELOCKED, cred);
749 free((void *)iv2, M_TEMP);
750 }
751 read_error:
752 if (error || (getret = VOP_GETATTR(vp, &va, cred, lwp)) != 0){
753 if (!error)
754 error = getret;
755 m_freem(mreq);
756 vput(vp);
757 nfsm_reply(NFSX_POSTOPATTR(v3));
758 nfsm_srvpostop_attr(getret, &va);
759 return (0);
760 }
761 } else {
762 uiop->uio_resid = 0;
763 }
764 vput(vp);
765 nfsm_srvfillattr(&va, fp);
766 len -= uiop->uio_resid;
767 padlen = nfsm_padlen(len);
768 if (uiop->uio_resid || padlen)
769 nfs_zeropad(mb, uiop->uio_resid, padlen);
770 if (v3) {
771 /* count */
772 *tl++ = txdr_unsigned(len);
773 /* eof */
774 if (off + len >= va.va_size)
775 *tl++ = nfs_true;
776 else
777 *tl++ = nfs_false;
778 }
779 *tl = txdr_unsigned(len);
780 nfsm_srvdone;
781 }
782
783 /*
784 * nfs write service
785 */
786 int
787 nfsrv_write(nfsd, slp, lwp, mrq)
788 struct nfsrv_descript *nfsd;
789 struct nfssvc_sock *slp;
790 struct lwp *lwp;
791 struct mbuf **mrq;
792 {
793 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
794 struct mbuf *nam = nfsd->nd_nam;
795 char *dpos = nfsd->nd_dpos;
796 kauth_cred_t cred = nfsd->nd_cr;
797 struct iovec *ivp;
798 int i, cnt;
799 struct mbuf *mp;
800 struct nfs_fattr *fp;
801 struct iovec *iv;
802 struct vattr va, forat;
803 u_int32_t *tl;
804 int32_t t1;
805 char *bpos;
806 int error = 0, rdonly, cache = 0, len, forat_ret = 1;
807 int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
808 int stable = NFSV3WRITE_FILESYNC;
809 int v3 = (nfsd->nd_flag & ND_NFSV3);
810 char *cp2;
811 struct mbuf *mb, *mreq;
812 struct vnode *vp;
813 nfsrvfh_t nsfh;
814 struct uio io, *uiop = &io;
815 off_t off;
816 u_quad_t frev;
817
818 if (mrep == NULL) {
819 *mrq = NULL;
820 return (0);
821 }
822 nfsm_srvmtofh(&nsfh);
823 if (v3) {
824 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
825 off = fxdr_hyper(tl);
826 tl += 3;
827 stable = fxdr_unsigned(int, *tl++);
828 } else {
829 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
830 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
831 tl += 2;
832 }
833 retlen = len = fxdr_unsigned(int32_t, *tl);
834 cnt = i = 0;
835
836 /*
837 * For NFS Version 2, it is not obvious what a write of zero length
838 * should do, but I might as well be consistent with Version 3,
839 * which is to return ok so long as there are no permission problems.
840 */
841 if (len > 0) {
842 zeroing = 1;
843 mp = mrep;
844 while (mp) {
845 if (mp == md) {
846 zeroing = 0;
847 adjust = dpos - mtod(mp, char *);
848 mp->m_len -= adjust;
849 if (mp->m_len > 0 && adjust > 0)
850 NFSMADV(mp, adjust);
851 }
852 if (zeroing)
853 mp->m_len = 0;
854 else if (mp->m_len > 0) {
855 i += mp->m_len;
856 if (i > len) {
857 mp->m_len -= (i - len);
858 zeroing = 1;
859 }
860 if (mp->m_len > 0)
861 cnt++;
862 }
863 mp = mp->m_next;
864 }
865 }
866 if (len > NFS_MAXDATA || len < 0 || i < len) {
867 error = EIO;
868 nfsm_reply(2 * NFSX_UNSIGNED);
869 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
870 return (0);
871 }
872 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
873 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
874 if (error) {
875 nfsm_reply(2 * NFSX_UNSIGNED);
876 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
877 return (0);
878 }
879 if (v3)
880 forat_ret = VOP_GETATTR(vp, &forat, cred, lwp);
881 if (vp->v_type != VREG) {
882 if (v3)
883 error = EINVAL;
884 else
885 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
886 }
887 if (!error) {
888 nqsrv_getl(vp, ND_WRITE);
889 error = nfsrv_access(vp, VWRITE, cred, rdonly, lwp, 1);
890 }
891 if (error) {
892 vput(vp);
893 nfsm_reply(NFSX_WCCDATA(v3));
894 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
895 return (0);
896 }
897
898 if (len > 0) {
899 ivp = malloc(cnt * sizeof (struct iovec), M_TEMP, M_WAITOK);
900 uiop->uio_iov = iv = ivp;
901 uiop->uio_iovcnt = cnt;
902 mp = mrep;
903 while (mp) {
904 if (mp->m_len > 0) {
905 ivp->iov_base = mtod(mp, void *);
906 ivp->iov_len = mp->m_len;
907 ivp++;
908 }
909 mp = mp->m_next;
910 }
911
912 /*
913 * XXX
914 * The IO_METASYNC flag indicates that all metadata (and not
915 * just enough to ensure data integrity) must be written to
916 * stable storage synchronously.
917 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
918 */
919 if (stable == NFSV3WRITE_UNSTABLE)
920 ioflags = IO_NODELOCKED;
921 else if (stable == NFSV3WRITE_DATASYNC)
922 ioflags = (IO_SYNC | IO_NODELOCKED);
923 else
924 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
925 uiop->uio_resid = len;
926 uiop->uio_rw = UIO_WRITE;
927 uiop->uio_offset = off;
928 UIO_SETUP_SYSSPACE(uiop);
929 error = VOP_WRITE(vp, uiop, ioflags, cred);
930 nfsstats.srvvop_writes++;
931 free(iv, M_TEMP);
932 }
933 aftat_ret = VOP_GETATTR(vp, &va, cred, lwp);
934 vput(vp);
935 if (!error)
936 error = aftat_ret;
937 nfsm_reply(NFSX_PREOPATTR(v3) + NFSX_POSTOPORFATTR(v3) +
938 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(v3));
939 if (v3) {
940 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
941 if (error)
942 return (0);
943 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
944 *tl++ = txdr_unsigned(retlen);
945 if (stable == NFSV3WRITE_UNSTABLE)
946 *tl++ = txdr_unsigned(stable);
947 else
948 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
949 /*
950 * Actually, there is no need to txdr these fields,
951 * but it may make the values more human readable,
952 * for debugging purposes.
953 */
954 *tl++ = txdr_unsigned(boottime.tv_sec);
955 *tl = txdr_unsigned(boottime.tv_usec);
956 } else {
957 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
958 nfsm_srvfillattr(&va, fp);
959 }
960 nfsm_srvdone;
961 }
962
963 /*
964 * XXX elad: the original NFSW_SAMECRED() macro also made sure the
965 * two nd_flag fields of the descriptors contained
966 * ND_KERBAUTH.
967 */
968 static int
969 nfsrv_samecred(kauth_cred_t cred1, kauth_cred_t cred2)
970 {
971 int i, do_ngroups;
972
973 if (kauth_cred_geteuid(cred1) != kauth_cred_geteuid(cred2))
974 return (0);
975 if (kauth_cred_ngroups(cred1) != kauth_cred_ngroups(cred2))
976 return (0);
977 do_ngroups = kauth_cred_ngroups(cred1);
978 for (i = 0; i < do_ngroups; i++)
979 if (kauth_cred_group(cred1, i) !=
980 kauth_cred_group(cred2, i))
981 return (0);
982
983 return (1);
984 }
985
986 static struct nfsrvw_delayhash *
987 nfsrv_nwdelayhash(struct nfssvc_sock *slp, const nfsrvfh_t *nsfh)
988 {
989 uint32_t hash;
990
991 hash = hash32_buf(NFSRVFH_DATA(nsfh), NFSRVFH_SIZE(nsfh),
992 HASH32_BUF_INIT);
993 return &slp->ns_wdelayhashtbl[hash % NFS_WDELAYHASHSIZ];
994 }
995
996 /*
997 * NFS write service with write gathering support. Called when
998 * nfsrvw_procrastinate > 0.
999 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1000 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1001 * Jan. 1994.
1002 */
1003 int
1004 nfsrv_writegather(ndp, slp, lwp, mrq)
1005 struct nfsrv_descript **ndp;
1006 struct nfssvc_sock *slp;
1007 struct lwp *lwp;
1008 struct mbuf **mrq;
1009 {
1010 struct timeval now;
1011 struct iovec *ivp;
1012 struct mbuf *mp;
1013 struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1014 struct nfs_fattr *fp;
1015 int i = 0;
1016 struct iovec *iov;
1017 struct nfsrvw_delayhash *wpp;
1018 kauth_cred_t cred;
1019 struct vattr va, forat;
1020 u_int32_t *tl;
1021 int32_t t1;
1022 char *bpos, *dpos;
1023 int error = 0, rdonly, cache = 0, len = 0, forat_ret = 1;
1024 int ioflags, aftat_ret = 1, s, adjust, v3, zeroing;
1025 char *cp2;
1026 struct mbuf *mb, *mreq, *mrep, *md;
1027 struct vnode *vp;
1028 struct uio io, *uiop = &io;
1029 u_quad_t frev, cur_usec;
1030
1031 *mrq = NULL;
1032 if (*ndp) {
1033 nfsd = *ndp;
1034 *ndp = NULL;
1035 mrep = nfsd->nd_mrep;
1036 md = nfsd->nd_md;
1037 dpos = nfsd->nd_dpos;
1038 cred = nfsd->nd_cr;
1039 v3 = (nfsd->nd_flag & ND_NFSV3);
1040 LIST_INIT(&nfsd->nd_coalesce);
1041 nfsd->nd_mreq = NULL;
1042 nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1043 getmicrotime(&now);
1044 cur_usec = (u_quad_t)now.tv_sec * 1000000 + (u_quad_t)now.tv_usec;
1045 nfsd->nd_time = cur_usec + nfsrvw_procrastinate;
1046
1047 /*
1048 * Now, get the write header..
1049 */
1050 nfsm_srvmtofh(&nfsd->nd_fh);
1051 if (v3) {
1052 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1053 nfsd->nd_off = fxdr_hyper(tl);
1054 tl += 3;
1055 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1056 } else {
1057 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1058 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1059 tl += 2;
1060 }
1061 len = fxdr_unsigned(int32_t, *tl);
1062 nfsd->nd_len = len;
1063 nfsd->nd_eoff = nfsd->nd_off + len;
1064
1065 /*
1066 * Trim the header out of the mbuf list and trim off any trailing
1067 * junk so that the mbuf list has only the write data.
1068 */
1069 zeroing = 1;
1070 i = 0;
1071 mp = mrep;
1072 while (mp) {
1073 if (mp == md) {
1074 zeroing = 0;
1075 adjust = dpos - mtod(mp, char *);
1076 mp->m_len -= adjust;
1077 if (mp->m_len > 0 && adjust > 0)
1078 NFSMADV(mp, adjust);
1079 }
1080 if (zeroing)
1081 mp->m_len = 0;
1082 else {
1083 i += mp->m_len;
1084 if (i > len) {
1085 mp->m_len -= (i - len);
1086 zeroing = 1;
1087 }
1088 }
1089 mp = mp->m_next;
1090 }
1091 if (len > NFS_MAXDATA || len < 0 || i < len) {
1092 nfsmout:
1093 m_freem(mrep);
1094 error = EIO;
1095 nfsm_writereply(2 * NFSX_UNSIGNED, v3);
1096 if (v3)
1097 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1098 nfsd->nd_mreq = mreq;
1099 nfsd->nd_mrep = NULL;
1100 nfsd->nd_time = 0;
1101 }
1102
1103 /*
1104 * Add this entry to the hash and time queues.
1105 */
1106 s = splsoftclock();
1107 owp = NULL;
1108 wp = LIST_FIRST(&slp->ns_tq);
1109 while (wp && wp->nd_time < nfsd->nd_time) {
1110 owp = wp;
1111 wp = LIST_NEXT(wp, nd_tq);
1112 }
1113 if (owp) {
1114 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1115 } else {
1116 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1117 }
1118 if (nfsd->nd_mrep) {
1119 wpp = nfsrv_nwdelayhash(slp, &nfsd->nd_fh);
1120 owp = NULL;
1121 wp = LIST_FIRST(wpp);
1122 while (wp && nfsrv_comparefh(&nfsd->nd_fh, &wp->nd_fh)) {
1123 owp = wp;
1124 wp = LIST_NEXT(wp, nd_hash);
1125 }
1126 while (wp && wp->nd_off < nfsd->nd_off &&
1127 !nfsrv_comparefh(&nfsd->nd_fh, &wp->nd_fh)) {
1128 owp = wp;
1129 wp = LIST_NEXT(wp, nd_hash);
1130 }
1131 if (owp) {
1132 LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1133
1134 /*
1135 * Search the hash list for overlapping entries and
1136 * coalesce.
1137 */
1138 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1139 wp = LIST_NEXT(nfsd, nd_hash);
1140 if (nfsrv_samecred(owp->nd_cr, nfsd->nd_cr))
1141 nfsrvw_coalesce(owp, nfsd);
1142 }
1143 } else {
1144 LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1145 }
1146 }
1147 splx(s);
1148 }
1149
1150 /*
1151 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1152 * and generate the associated reply mbuf list(s).
1153 */
1154 loop1:
1155 getmicrotime(&now);
1156 cur_usec = (u_quad_t)now.tv_sec * 1000000 + (u_quad_t)now.tv_usec;
1157 s = splsoftclock();
1158 for (nfsd = LIST_FIRST(&slp->ns_tq); nfsd; nfsd = owp) {
1159 owp = LIST_NEXT(nfsd, nd_tq);
1160 if (nfsd->nd_time > cur_usec)
1161 break;
1162 if (nfsd->nd_mreq)
1163 continue;
1164 LIST_REMOVE(nfsd, nd_tq);
1165 LIST_REMOVE(nfsd, nd_hash);
1166 splx(s);
1167 mrep = nfsd->nd_mrep;
1168 nfsd->nd_mrep = NULL;
1169 cred = nfsd->nd_cr;
1170 v3 = (nfsd->nd_flag & ND_NFSV3);
1171 forat_ret = aftat_ret = 1;
1172 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &vp, cred, slp,
1173 nfsd->nd_nam, &rdonly, (nfsd->nd_flag & ND_KERBAUTH),
1174 false);
1175 if (!error) {
1176 if (v3)
1177 forat_ret = VOP_GETATTR(vp, &forat, cred, lwp);
1178 if (vp->v_type != VREG) {
1179 if (v3)
1180 error = EINVAL;
1181 else
1182 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1183 }
1184 } else
1185 vp = NULL;
1186 if (!error) {
1187 nqsrv_getl(vp, ND_WRITE);
1188 error = nfsrv_access(vp, VWRITE, cred, rdonly, lwp, 1);
1189 }
1190
1191 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1192 ioflags = IO_NODELOCKED;
1193 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1194 ioflags = (IO_SYNC | IO_NODELOCKED);
1195 else
1196 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1197 uiop->uio_rw = UIO_WRITE;
1198 uiop->uio_offset = nfsd->nd_off;
1199 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1200 UIO_SETUP_SYSSPACE(uiop);
1201 if (uiop->uio_resid > 0) {
1202 mp = mrep;
1203 i = 0;
1204 while (mp) {
1205 if (mp->m_len > 0)
1206 i++;
1207 mp = mp->m_next;
1208 }
1209 uiop->uio_iovcnt = i;
1210 iov = malloc(i * sizeof (struct iovec), M_TEMP, M_WAITOK);
1211 uiop->uio_iov = ivp = iov;
1212 mp = mrep;
1213 while (mp) {
1214 if (mp->m_len > 0) {
1215 ivp->iov_base = mtod(mp, void *);
1216 ivp->iov_len = mp->m_len;
1217 ivp++;
1218 }
1219 mp = mp->m_next;
1220 }
1221 if (!error) {
1222 error = VOP_WRITE(vp, uiop, ioflags, cred);
1223 nfsstats.srvvop_writes++;
1224 }
1225 free((void *)iov, M_TEMP);
1226 }
1227 m_freem(mrep);
1228 if (vp) {
1229 aftat_ret = VOP_GETATTR(vp, &va, cred, lwp);
1230 vput(vp);
1231 }
1232
1233 /*
1234 * Loop around generating replies for all write rpcs that have
1235 * now been completed.
1236 */
1237 swp = nfsd;
1238 do {
1239 if (error) {
1240 nfsm_writereply(NFSX_WCCDATA(v3), v3);
1241 if (v3) {
1242 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1243 }
1244 } else {
1245 nfsm_writereply(NFSX_PREOPATTR(v3) +
1246 NFSX_POSTOPORFATTR(v3) + 2 * NFSX_UNSIGNED +
1247 NFSX_WRITEVERF(v3), v3);
1248 if (v3) {
1249 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1250 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1251 *tl++ = txdr_unsigned(nfsd->nd_len);
1252 *tl++ = txdr_unsigned(swp->nd_stable);
1253 /*
1254 * Actually, there is no need to txdr these fields,
1255 * but it may make the values more human readable,
1256 * for debugging purposes.
1257 */
1258 *tl++ = txdr_unsigned(boottime.tv_sec);
1259 *tl = txdr_unsigned(boottime.tv_usec);
1260 } else {
1261 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1262 nfsm_srvfillattr(&va, fp);
1263 }
1264 }
1265 nfsd->nd_mreq = mreq;
1266 if (nfsd->nd_mrep)
1267 panic("nfsrv_write: nd_mrep not free");
1268
1269 /*
1270 * Done. Put it at the head of the timer queue so that
1271 * the final phase can return the reply.
1272 */
1273 s = splsoftclock();
1274 if (nfsd != swp) {
1275 nfsd->nd_time = 0;
1276 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1277 }
1278 nfsd = LIST_FIRST(&swp->nd_coalesce);
1279 if (nfsd) {
1280 LIST_REMOVE(nfsd, nd_tq);
1281 }
1282 splx(s);
1283 } while (nfsd);
1284 s = splsoftclock();
1285 swp->nd_time = 0;
1286 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1287 splx(s);
1288 goto loop1;
1289 }
1290 nfs_timer_start();
1291 splx(s);
1292
1293 /*
1294 * Search for a reply to return.
1295 */
1296 s = splsoftclock();
1297 LIST_FOREACH(nfsd, &slp->ns_tq, nd_tq) {
1298 if (nfsd->nd_mreq) {
1299 LIST_REMOVE(nfsd, nd_tq);
1300 *mrq = nfsd->nd_mreq;
1301 *ndp = nfsd;
1302 break;
1303 }
1304 }
1305 splx(s);
1306 return (0);
1307 }
1308
1309 /*
1310 * Coalesce the write request nfsd into owp. To do this we must:
1311 * - remove nfsd from the queues
1312 * - merge nfsd->nd_mrep into owp->nd_mrep
1313 * - update the nd_eoff and nd_stable for owp
1314 * - put nfsd on owp's nd_coalesce list
1315 * NB: Must be called at splsoftclock().
1316 */
1317 void
1318 nfsrvw_coalesce(owp, nfsd)
1319 struct nfsrv_descript *owp;
1320 struct nfsrv_descript *nfsd;
1321 {
1322 int overlap;
1323 struct mbuf *mp;
1324 struct nfsrv_descript *m;
1325
1326 LIST_REMOVE(nfsd, nd_hash);
1327 LIST_REMOVE(nfsd, nd_tq);
1328 if (owp->nd_eoff < nfsd->nd_eoff) {
1329 overlap = owp->nd_eoff - nfsd->nd_off;
1330 if (overlap < 0)
1331 panic("nfsrv_coalesce: bad off");
1332 if (overlap > 0)
1333 m_adj(nfsd->nd_mrep, overlap);
1334 mp = owp->nd_mrep;
1335 while (mp->m_next)
1336 mp = mp->m_next;
1337 mp->m_next = nfsd->nd_mrep;
1338 owp->nd_eoff = nfsd->nd_eoff;
1339 } else
1340 m_freem(nfsd->nd_mrep);
1341 nfsd->nd_mrep = NULL;
1342 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1343 owp->nd_stable = NFSV3WRITE_FILESYNC;
1344 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1345 owp->nd_stable == NFSV3WRITE_UNSTABLE)
1346 owp->nd_stable = NFSV3WRITE_DATASYNC;
1347 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1348 /*
1349 * nfsd might hold coalesce elements! Move them to owp.
1350 * Otherwise, requests may be lost and clients will be stuck.
1351 */
1352 while ((m = LIST_FIRST(&nfsd->nd_coalesce)) != NULL) {
1353 LIST_REMOVE(m, nd_tq);
1354 LIST_INSERT_HEAD(&owp->nd_coalesce, m, nd_tq);
1355 }
1356 }
1357
1358 /*
1359 * nfs create service
1360 * now does a truncate to 0 length via. setattr if it already exists
1361 */
1362 int
1363 nfsrv_create(nfsd, slp, lwp, mrq)
1364 struct nfsrv_descript *nfsd;
1365 struct nfssvc_sock *slp;
1366 struct lwp *lwp;
1367 struct mbuf **mrq;
1368 {
1369 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1370 struct mbuf *nam = nfsd->nd_nam;
1371 char *dpos = nfsd->nd_dpos;
1372 kauth_cred_t cred = nfsd->nd_cr;
1373 struct nfs_fattr *fp;
1374 struct vattr va, dirfor, diraft;
1375 struct nfsv2_sattr *sp;
1376 u_int32_t *tl;
1377 struct nameidata nd;
1378 char *cp;
1379 int32_t t1;
1380 char *bpos;
1381 int error = 0, cache = 0, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1382 int rdev = 0;
1383 int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0;
1384 char *cp2;
1385 struct mbuf *mb, *mreq;
1386 struct vnode *vp = NULL, *dirp = NULL;
1387 nfsrvfh_t nsfh;
1388 u_quad_t frev, tempsize;
1389 u_char cverf[NFSX_V3CREATEVERF];
1390
1391 nd.ni_cnd.cn_nameiop = 0;
1392 nfsm_srvmtofh(&nsfh);
1393 nfsm_srvnamesiz(len);
1394 nd.ni_cnd.cn_cred = cred;
1395 nd.ni_cnd.cn_nameiop = CREATE;
1396 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
1397 error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
1398 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
1399 if (dirp && v3) {
1400 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, lwp);
1401 }
1402 if (error) {
1403 nfsm_reply(NFSX_WCCDATA(v3));
1404 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1405 if (dirp)
1406 vrele(dirp);
1407 return (0);
1408 }
1409 VATTR_NULL(&va);
1410 if (v3) {
1411 va.va_mode = 0;
1412 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1413 how = fxdr_unsigned(int, *tl);
1414 switch (how) {
1415 case NFSV3CREATE_GUARDED:
1416 if (nd.ni_vp) {
1417 error = EEXIST;
1418 break;
1419 }
1420 case NFSV3CREATE_UNCHECKED:
1421 nfsm_srvsattr(&va);
1422 break;
1423 case NFSV3CREATE_EXCLUSIVE:
1424 nfsm_dissect(cp, void *, NFSX_V3CREATEVERF);
1425 memcpy(cverf, cp, NFSX_V3CREATEVERF);
1426 exclusive_flag = 1;
1427 break;
1428 };
1429 va.va_type = VREG;
1430 } else {
1431 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1432 va.va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1433 if (va.va_type == VNON)
1434 va.va_type = VREG;
1435 va.va_mode = nfstov_mode(sp->sa_mode);
1436 switch (va.va_type) {
1437 case VREG:
1438 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1439 if (tsize != -1)
1440 va.va_size = (u_quad_t)tsize;
1441 break;
1442 case VCHR:
1443 case VBLK:
1444 case VFIFO:
1445 rdev = fxdr_unsigned(int32_t, sp->sa_size);
1446 break;
1447 default:
1448 break;
1449 };
1450 }
1451
1452 /*
1453 * Iff doesn't exist, create it
1454 * otherwise just truncate to 0 length
1455 * should I set the mode too ??
1456 */
1457 if (nd.ni_vp == NULL) {
1458 if (va.va_type == VREG || va.va_type == VSOCK) {
1459 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1460 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
1461 if (!error) {
1462 if (exclusive_flag) {
1463 exclusive_flag = 0;
1464 VATTR_NULL(&va);
1465 /*
1466 * XXX
1467 * assuming NFSX_V3CREATEVERF
1468 * == sizeof(nfstime3)
1469 */
1470 fxdr_nfsv3time(cverf, &va.va_atime);
1471 error = VOP_SETATTR(nd.ni_vp, &va, cred,
1472 lwp);
1473 }
1474 }
1475 } else if (va.va_type == VCHR || va.va_type == VBLK ||
1476 va.va_type == VFIFO) {
1477 if (va.va_type == VCHR && rdev == 0xffffffff)
1478 va.va_type = VFIFO;
1479 if (va.va_type != VFIFO &&
1480 (error = kauth_authorize_generic(cred,
1481 KAUTH_GENERIC_ISSUSER, NULL))) {
1482 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1483 vput(nd.ni_dvp);
1484 nfsm_reply(0);
1485 return (error);
1486 } else
1487 va.va_rdev = (dev_t)rdev;
1488 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1489 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd,
1490 &va);
1491 if (error) {
1492 nfsm_reply(0);
1493 }
1494 if (nd.ni_cnd.cn_flags & ISSYMLINK) {
1495 vput(nd.ni_vp);
1496 vrele(nd.ni_dvp);
1497 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1498 error = EINVAL;
1499 nfsm_reply(0);
1500 }
1501 } else {
1502 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1503 vput(nd.ni_dvp);
1504 error = ENXIO;
1505 }
1506 vp = nd.ni_vp;
1507 } else {
1508 vp = nd.ni_vp;
1509 if (nd.ni_dvp == vp)
1510 vrele(nd.ni_dvp);
1511 else
1512 vput(nd.ni_dvp);
1513 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1514 if (!error && va.va_size != -1) {
1515 error = nfsrv_access(vp, VWRITE, cred,
1516 (nd.ni_cnd.cn_flags & RDONLY), lwp, 0);
1517 if (!error) {
1518 nqsrv_getl(vp, ND_WRITE);
1519 tempsize = va.va_size;
1520 VATTR_NULL(&va);
1521 va.va_size = tempsize;
1522 error = VOP_SETATTR(vp, &va, cred, lwp);
1523 }
1524 }
1525 if (error)
1526 vput(vp);
1527 }
1528 if (!error) {
1529 error = nfsrv_composefh(vp, &nsfh, v3);
1530 if (!error)
1531 error = VOP_GETATTR(vp, &va, cred, lwp);
1532 vput(vp);
1533 }
1534 if (v3) {
1535 if (exclusive_flag && !error) {
1536 /*
1537 * XXX assuming NFSX_V3CREATEVERF == sizeof(nfstime3)
1538 */
1539 char oldverf[NFSX_V3CREATEVERF];
1540
1541 txdr_nfsv3time(&va.va_atime, oldverf);
1542 if (memcmp(cverf, oldverf, NFSX_V3CREATEVERF))
1543 error = EEXIST;
1544 }
1545 if (dirp) {
1546 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, lwp);
1547 }
1548 }
1549 if (dirp) {
1550 vrele(dirp);
1551 }
1552 nfsm_reply(NFSX_SRVFH(&nsfh, v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1553 if (v3) {
1554 if (!error) {
1555 nfsm_srvpostop_fh(&nsfh);
1556 nfsm_srvpostop_attr(0, &va);
1557 }
1558 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1559 } else {
1560 nfsm_srvfhtom(&nsfh, v3);
1561 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1562 nfsm_srvfillattr(&va, fp);
1563 }
1564 return (0);
1565 nfsmout:
1566 if (dirp)
1567 vrele(dirp);
1568 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1569 if (nd.ni_dvp == nd.ni_vp)
1570 vrele(nd.ni_dvp);
1571 else
1572 vput(nd.ni_dvp);
1573 if (nd.ni_vp)
1574 vput(nd.ni_vp);
1575 return (error);
1576 }
1577
1578 /*
1579 * nfs v3 mknod service
1580 */
1581 int
1582 nfsrv_mknod(nfsd, slp, lwp, mrq)
1583 struct nfsrv_descript *nfsd;
1584 struct nfssvc_sock *slp;
1585 struct lwp *lwp;
1586 struct mbuf **mrq;
1587 {
1588 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1589 struct mbuf *nam = nfsd->nd_nam;
1590 char *dpos = nfsd->nd_dpos;
1591 kauth_cred_t cred = nfsd->nd_cr;
1592 struct vattr va, dirfor, diraft;
1593 u_int32_t *tl;
1594 struct nameidata nd;
1595 int32_t t1;
1596 char *bpos;
1597 int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
1598 u_int32_t major, minor;
1599 enum vtype vtyp;
1600 char *cp2;
1601 struct mbuf *mb, *mreq;
1602 struct vnode *vp, *dirp = (struct vnode *)0;
1603 nfsrvfh_t nsfh;
1604 u_quad_t frev;
1605
1606 nd.ni_cnd.cn_nameiop = 0;
1607 nfsm_srvmtofh(&nsfh);
1608 nfsm_srvnamesiz(len);
1609 nd.ni_cnd.cn_cred = cred;
1610 nd.ni_cnd.cn_nameiop = CREATE;
1611 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
1612 error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
1613 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
1614 if (dirp)
1615 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, lwp);
1616 if (error) {
1617 nfsm_reply(NFSX_WCCDATA(1));
1618 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1619 if (dirp)
1620 vrele(dirp);
1621 return (0);
1622 }
1623 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1624 vtyp = nfsv3tov_type(*tl);
1625 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1626 error = NFSERR_BADTYPE;
1627 goto abort;
1628 }
1629 VATTR_NULL(&va);
1630 va.va_mode = 0;
1631 nfsm_srvsattr(&va);
1632 if (vtyp == VCHR || vtyp == VBLK) {
1633 dev_t rdev;
1634
1635 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1636 major = fxdr_unsigned(u_int32_t, *tl++);
1637 minor = fxdr_unsigned(u_int32_t, *tl);
1638 rdev = makedev(major, minor);
1639 if (major(rdev) != major || minor(rdev) != minor) {
1640 error = EINVAL;
1641 goto abort;
1642 }
1643 va.va_rdev = rdev;
1644 }
1645
1646 /*
1647 * Iff doesn't exist, create it.
1648 */
1649 if (nd.ni_vp) {
1650 error = EEXIST;
1651 abort:
1652 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1653 if (nd.ni_dvp == nd.ni_vp)
1654 vrele(nd.ni_dvp);
1655 else
1656 vput(nd.ni_dvp);
1657 if (nd.ni_vp)
1658 vput(nd.ni_vp);
1659 goto out;
1660 }
1661 va.va_type = vtyp;
1662 if (vtyp == VSOCK) {
1663 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1664 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
1665 } else {
1666 if (va.va_type != VFIFO &&
1667 (error = kauth_authorize_generic(cred,
1668 KAUTH_GENERIC_ISSUSER, NULL))) {
1669 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1670 vput(nd.ni_dvp);
1671 goto out;
1672 }
1673 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1674 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
1675 if (error)
1676 goto out;
1677 if (nd.ni_cnd.cn_flags & ISSYMLINK) {
1678 vput(nd.ni_vp);
1679 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1680 error = EINVAL;
1681 }
1682 }
1683 out:
1684 vp = nd.ni_vp;
1685 if (!error) {
1686 error = nfsrv_composefh(vp, &nsfh, true);
1687 if (!error)
1688 error = VOP_GETATTR(vp, &va, cred, lwp);
1689 vput(vp);
1690 }
1691 if (dirp) {
1692 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, lwp);
1693 vrele(dirp);
1694 }
1695 nfsm_reply(NFSX_SRVFH(&nsfh, true) + NFSX_POSTOPATTR(1) +
1696 NFSX_WCCDATA(1));
1697 if (!error) {
1698 nfsm_srvpostop_fh(&nsfh);
1699 nfsm_srvpostop_attr(0, &va);
1700 }
1701 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1702 return (0);
1703 nfsmout:
1704 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1705 if (nd.ni_dvp == nd.ni_vp)
1706 vrele(nd.ni_dvp);
1707 else
1708 vput(nd.ni_dvp);
1709 if (nd.ni_vp)
1710 vput(nd.ni_vp);
1711 if (dirp)
1712 vrele(dirp);
1713 return (error);
1714 }
1715
1716 /*
1717 * nfs remove service
1718 */
1719 int
1720 nfsrv_remove(nfsd, slp, lwp, mrq)
1721 struct nfsrv_descript *nfsd;
1722 struct nfssvc_sock *slp;
1723 struct lwp *lwp;
1724 struct mbuf **mrq;
1725 {
1726 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1727 struct mbuf *nam = nfsd->nd_nam;
1728 char *dpos = nfsd->nd_dpos;
1729 kauth_cred_t cred = nfsd->nd_cr;
1730 struct nameidata nd;
1731 u_int32_t *tl;
1732 int32_t t1;
1733 char *bpos;
1734 int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
1735 int v3 = (nfsd->nd_flag & ND_NFSV3);
1736 char *cp2;
1737 struct mbuf *mb, *mreq;
1738 struct vnode *vp, *dirp;
1739 struct vattr dirfor, diraft;
1740 nfsrvfh_t nsfh;
1741 u_quad_t frev;
1742
1743 #ifndef nolint
1744 vp = (struct vnode *)0;
1745 #endif
1746 nfsm_srvmtofh(&nsfh);
1747 nfsm_srvnamesiz(len);
1748 nd.ni_cnd.cn_cred = cred;
1749 nd.ni_cnd.cn_nameiop = DELETE;
1750 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
1751 error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
1752 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
1753 if (dirp && v3) {
1754 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, lwp);
1755 }
1756 if (!error) {
1757 vp = nd.ni_vp;
1758 if (vp->v_type == VDIR &&
1759 (error = kauth_authorize_generic(cred,
1760 KAUTH_GENERIC_ISSUSER, NULL)) != 0)
1761 goto out;
1762 /*
1763 * The root of a mounted filesystem cannot be deleted.
1764 */
1765 if (vp->v_flag & VROOT) {
1766 error = EBUSY;
1767 goto out;
1768 }
1769 out:
1770 if (!error) {
1771 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1772 nqsrv_getl(vp, ND_WRITE);
1773 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1774 } else {
1775 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1776 if (nd.ni_dvp == vp)
1777 vrele(nd.ni_dvp);
1778 else
1779 vput(nd.ni_dvp);
1780 vput(vp);
1781 }
1782 }
1783 if (dirp) {
1784 if (v3) {
1785 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, lwp);
1786 }
1787 vrele(dirp);
1788 }
1789 nfsm_reply(NFSX_WCCDATA(v3));
1790 if (v3) {
1791 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1792 return (0);
1793 }
1794 nfsm_srvdone;
1795 }
1796
1797 /*
1798 * nfs rename service
1799 */
1800 int
1801 nfsrv_rename(nfsd, slp, lwp, mrq)
1802 struct nfsrv_descript *nfsd;
1803 struct nfssvc_sock *slp;
1804 struct lwp *lwp;
1805 struct mbuf **mrq;
1806 {
1807 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1808 struct mbuf *nam = nfsd->nd_nam;
1809 char *dpos = nfsd->nd_dpos;
1810 kauth_cred_t cred = nfsd->nd_cr;
1811 u_int32_t *tl;
1812 int32_t t1;
1813 char *bpos;
1814 int error = 0, cache = 0, fdirfor_ret = 1, fdiraft_ret = 1;
1815 uint32_t len, len2;
1816 int tdirfor_ret = 1, tdiraft_ret = 1;
1817 int v3 = (nfsd->nd_flag & ND_NFSV3);
1818 char *cp2;
1819 struct mbuf *mb, *mreq;
1820 struct nameidata fromnd, tond;
1821 struct vnode *fvp, *tvp, *tdvp;
1822 struct vnode *fdirp = NULL, *tdirp = NULL;
1823 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
1824 nfsrvfh_t fnsfh, tnsfh;
1825 u_quad_t frev;
1826 uid_t saved_uid;
1827
1828 #ifndef nolint
1829 fvp = (struct vnode *)0;
1830 #endif
1831 fromnd.ni_cnd.cn_nameiop = 0;
1832 tond.ni_cnd.cn_nameiop = 0;
1833 nfsm_srvmtofh(&fnsfh);
1834 nfsm_srvnamesiz(len);
1835 /*
1836 * Remember our original uid so that we can reset cr_uid before
1837 * the second nfs_namei() call, in case it is remapped.
1838 */
1839 saved_uid = kauth_cred_geteuid(cred);
1840 fromnd.ni_cnd.cn_cred = cred;
1841 fromnd.ni_cnd.cn_nameiop = DELETE;
1842 fromnd.ni_cnd.cn_flags = LOCKPARENT | SAVESTART;
1843 error = nfs_namei(&fromnd, &fnsfh, len, slp, nam, &md,
1844 &dpos, &fdirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
1845 if (fdirp && v3) {
1846 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, cred, lwp);
1847 }
1848 if (error) {
1849 nfsm_reply(2 * NFSX_WCCDATA(v3));
1850 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
1851 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
1852 if (fdirp)
1853 vrele(fdirp);
1854 return (0);
1855 }
1856 if (fromnd.ni_dvp != fromnd.ni_vp) {
1857 VOP_UNLOCK(fromnd.ni_dvp, 0);
1858 }
1859 fvp = fromnd.ni_vp;
1860 nfsm_srvmtofh(&tnsfh);
1861 if (v3) {
1862 nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED);
1863 len2 = fxdr_unsigned(uint32_t, *tl);
1864 /* len2 will be checked by nfs_namei */
1865 }
1866 else {
1867 /* NFSv2 */
1868 nfsm_strsiz(len2, NFS_MAXNAMLEN);
1869 }
1870 kauth_cred_seteuid(cred, saved_uid);
1871 tond.ni_cnd.cn_cred = cred;
1872 tond.ni_cnd.cn_nameiop = RENAME;
1873 tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART;
1874 error = nfs_namei(&tond, &tnsfh, len2, slp, nam, &md,
1875 &dpos, &tdirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
1876 if (tdirp && v3) {
1877 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, cred, lwp);
1878 }
1879 if (error) {
1880 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1881 vrele(fromnd.ni_dvp);
1882 vrele(fvp);
1883 goto out1;
1884 }
1885 tdvp = tond.ni_dvp;
1886 tvp = tond.ni_vp;
1887 if (tvp != NULL) {
1888 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1889 if (v3)
1890 error = EEXIST;
1891 else
1892 error = EISDIR;
1893 goto out;
1894 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1895 if (v3)
1896 error = EEXIST;
1897 else
1898 error = ENOTDIR;
1899 goto out;
1900 }
1901 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1902 if (v3)
1903 error = EXDEV;
1904 else
1905 error = ENOTEMPTY;
1906 goto out;
1907 }
1908 }
1909 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1910 if (v3)
1911 error = EXDEV;
1912 else
1913 error = ENOTEMPTY;
1914 goto out;
1915 }
1916 if (fvp->v_mount != tdvp->v_mount) {
1917 if (v3)
1918 error = EXDEV;
1919 else
1920 error = ENOTEMPTY;
1921 goto out;
1922 }
1923 if (fvp == tdvp) {
1924 if (v3)
1925 error = EINVAL;
1926 else
1927 error = ENOTEMPTY;
1928 }
1929 /*
1930 * If source is the same as the destination (that is the
1931 * same vnode with the same name in the same directory),
1932 * then there is nothing to do.
1933 */
1934 if (fvp == tvp && fromnd.ni_dvp == tdvp &&
1935 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
1936 !memcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
1937 fromnd.ni_cnd.cn_namelen))
1938 error = -1;
1939 out:
1940 if (!error) {
1941 nqsrv_getl(fromnd.ni_dvp, ND_WRITE);
1942 nqsrv_getl(tdvp, ND_WRITE);
1943 if (tvp) {
1944 nqsrv_getl(tvp, ND_WRITE);
1945 }
1946 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
1947 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
1948 } else {
1949 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
1950 if (tdvp == tvp)
1951 vrele(tdvp);
1952 else
1953 vput(tdvp);
1954 if (tvp)
1955 vput(tvp);
1956 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1957 vrele(fromnd.ni_dvp);
1958 vrele(fvp);
1959 if (error == -1)
1960 error = 0;
1961 }
1962 vrele(tond.ni_startdir);
1963 PNBUF_PUT(tond.ni_cnd.cn_pnbuf);
1964 out1:
1965 if (fdirp) {
1966 if (v3) {
1967 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, cred, lwp);
1968 }
1969 vrele(fdirp);
1970 }
1971 if (tdirp) {
1972 if (v3) {
1973 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, cred, lwp);
1974 }
1975 vrele(tdirp);
1976 }
1977 vrele(fromnd.ni_startdir);
1978 PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
1979 nfsm_reply(2 * NFSX_WCCDATA(v3));
1980 if (v3) {
1981 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
1982 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
1983 }
1984 return (0);
1985
1986 nfsmout:
1987 if (fdirp)
1988 vrele(fdirp);
1989 #ifdef notdef
1990 if (tdirp)
1991 vrele(tdirp);
1992 #endif
1993 if (tond.ni_cnd.cn_nameiop) {
1994 vrele(tond.ni_startdir);
1995 PNBUF_PUT(tond.ni_cnd.cn_pnbuf);
1996 }
1997 if (fromnd.ni_cnd.cn_nameiop) {
1998 vrele(fromnd.ni_startdir);
1999 PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
2000 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2001 vrele(fromnd.ni_dvp);
2002 vrele(fvp);
2003 }
2004 return (error);
2005 }
2006
2007 /*
2008 * nfs link service
2009 */
2010 int
2011 nfsrv_link(nfsd, slp, lwp, mrq)
2012 struct nfsrv_descript *nfsd;
2013 struct nfssvc_sock *slp;
2014 struct lwp *lwp;
2015 struct mbuf **mrq;
2016 {
2017 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2018 struct mbuf *nam = nfsd->nd_nam;
2019 char *dpos = nfsd->nd_dpos;
2020 kauth_cred_t cred = nfsd->nd_cr;
2021 struct nameidata nd;
2022 u_int32_t *tl;
2023 int32_t t1;
2024 char *bpos;
2025 int error = 0, rdonly, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
2026 int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
2027 char *cp2;
2028 struct mbuf *mb, *mreq;
2029 struct vnode *vp, *xp, *dirp = (struct vnode *)0;
2030 struct vattr dirfor, diraft, at;
2031 nfsrvfh_t nsfh, dnsfh;
2032 u_quad_t frev;
2033
2034 nfsm_srvmtofh(&nsfh);
2035 nfsm_srvmtofh(&dnsfh);
2036 nfsm_srvnamesiz(len);
2037 error = nfsrv_fhtovp(&nsfh, false, &vp, cred, slp, nam,
2038 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
2039 if (error) {
2040 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2041 nfsm_srvpostop_attr(getret, &at);
2042 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2043 return (0);
2044 }
2045 if (vp->v_type == VDIR && (error = kauth_authorize_generic(cred,
2046 KAUTH_GENERIC_ISSUSER, NULL)) != 0)
2047 goto out1;
2048 nd.ni_cnd.cn_cred = cred;
2049 nd.ni_cnd.cn_nameiop = CREATE;
2050 nd.ni_cnd.cn_flags = LOCKPARENT;
2051 error = nfs_namei(&nd, &dnsfh, len, slp, nam, &md, &dpos,
2052 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
2053 if (dirp && v3) {
2054 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, lwp);
2055 }
2056 if (error)
2057 goto out1;
2058 xp = nd.ni_vp;
2059 if (xp != NULL) {
2060 error = EEXIST;
2061 goto out;
2062 }
2063 xp = nd.ni_dvp;
2064 if (vp->v_mount != xp->v_mount)
2065 error = EXDEV;
2066 out:
2067 if (!error) {
2068 nqsrv_getl(vp, ND_WRITE);
2069 nqsrv_getl(xp, ND_WRITE);
2070 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
2071 } else {
2072 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2073 if (nd.ni_dvp == nd.ni_vp)
2074 vrele(nd.ni_dvp);
2075 else
2076 vput(nd.ni_dvp);
2077 if (nd.ni_vp)
2078 vrele(nd.ni_vp);
2079 }
2080 out1:
2081 if (v3)
2082 getret = VOP_GETATTR(vp, &at, cred, lwp);
2083 if (dirp) {
2084 if (v3) {
2085 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, lwp);
2086 }
2087 vrele(dirp);
2088 }
2089 vrele(vp);
2090 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2091 if (v3) {
2092 nfsm_srvpostop_attr(getret, &at);
2093 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2094 return (0);
2095 }
2096 nfsm_srvdone;
2097 }
2098
2099 /*
2100 * nfs symbolic link service
2101 */
2102 int
2103 nfsrv_symlink(nfsd, slp, lwp, mrq)
2104 struct nfsrv_descript *nfsd;
2105 struct nfssvc_sock *slp;
2106 struct lwp *lwp;
2107 struct mbuf **mrq;
2108 {
2109 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2110 struct mbuf *nam = nfsd->nd_nam;
2111 char *dpos = nfsd->nd_dpos;
2112 kauth_cred_t cred = nfsd->nd_cr;
2113 struct vattr va, dirfor, diraft;
2114 struct nameidata nd;
2115 u_int32_t *tl;
2116 int32_t t1;
2117 struct nfsv2_sattr *sp;
2118 char *bpos, *pathcp = NULL, *cp2;
2119 struct uio io;
2120 struct iovec iv;
2121 int error = 0, cache = 0, dirfor_ret = 1, diraft_ret = 1;
2122 uint32_t len, len2;
2123 int v3 = (nfsd->nd_flag & ND_NFSV3);
2124 struct mbuf *mb, *mreq;
2125 struct vnode *dirp = (struct vnode *)0;
2126 nfsrvfh_t nsfh;
2127 u_quad_t frev;
2128
2129 nd.ni_cnd.cn_nameiop = 0;
2130 nfsm_srvmtofh(&nsfh);
2131 nfsm_srvnamesiz(len);
2132 nd.ni_cnd.cn_cred = cred;
2133 nd.ni_cnd.cn_nameiop = CREATE;
2134 nd.ni_cnd.cn_flags = LOCKPARENT;
2135 error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
2136 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
2137 if (dirp && v3) {
2138 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, lwp);
2139 }
2140 if (error)
2141 goto out;
2142 VATTR_NULL(&va);
2143 va.va_type = VLNK;
2144 if (v3) {
2145 va.va_mode = 0;
2146 nfsm_srvsattr(&va);
2147 nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED);
2148 len2 = fxdr_unsigned(uint32_t, *tl);
2149 if (len2 > PATH_MAX) {
2150 /* XXX should check _PC_NO_TRUNC */
2151 error = ENAMETOOLONG;
2152 goto abortop;
2153 }
2154 }
2155 else {
2156 /* NFSv2 */
2157 nfsm_strsiz(len2, NFS_MAXPATHLEN);
2158 }
2159 pathcp = malloc(len2 + 1, M_TEMP, M_WAITOK);
2160 iv.iov_base = pathcp;
2161 iv.iov_len = len2;
2162 io.uio_resid = len2;
2163 io.uio_offset = 0;
2164 io.uio_iov = &iv;
2165 io.uio_iovcnt = 1;
2166 io.uio_rw = UIO_READ;
2167 UIO_SETUP_SYSSPACE(&io);
2168 nfsm_mtouio(&io, len2);
2169 if (!v3) {
2170 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2171 va.va_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
2172 }
2173 *(pathcp + len2) = '\0';
2174 if (nd.ni_vp) {
2175 error = EEXIST;
2176 abortop:
2177 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2178 if (nd.ni_dvp == nd.ni_vp)
2179 vrele(nd.ni_dvp);
2180 else
2181 vput(nd.ni_dvp);
2182 if (nd.ni_vp)
2183 vrele(nd.ni_vp);
2184 goto out;
2185 }
2186 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2187 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va, pathcp);
2188 if (!error) {
2189 if (v3) {
2190 error = nfsrv_composefh(nd.ni_vp, &nsfh, v3);
2191 if (!error)
2192 error = VOP_GETATTR(nd.ni_vp, &va, cred, lwp);
2193 vput(nd.ni_vp);
2194 } else {
2195 vput(nd.ni_vp);
2196 }
2197 }
2198 out:
2199 if (pathcp)
2200 free(pathcp, M_TEMP);
2201 if (dirp) {
2202 if (v3) {
2203 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, lwp);
2204 }
2205 vrele(dirp);
2206 }
2207 nfsm_reply(NFSX_SRVFH(&nsfh, v3) + NFSX_POSTOPATTR(v3) +
2208 NFSX_WCCDATA(v3));
2209 if (v3) {
2210 if (!error) {
2211 nfsm_srvpostop_fh(&nsfh);
2212 nfsm_srvpostop_attr(0, &va);
2213 }
2214 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2215 }
2216 return (0);
2217 nfsmout:
2218 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2219 if (nd.ni_dvp == nd.ni_vp)
2220 vrele(nd.ni_dvp);
2221 else
2222 vput(nd.ni_dvp);
2223 if (nd.ni_vp)
2224 vrele(nd.ni_vp);
2225 if (dirp)
2226 vrele(dirp);
2227 if (pathcp)
2228 free(pathcp, M_TEMP);
2229 return (error);
2230 }
2231
2232 /*
2233 * nfs mkdir service
2234 */
2235 int
2236 nfsrv_mkdir(nfsd, slp, lwp, mrq)
2237 struct nfsrv_descript *nfsd;
2238 struct nfssvc_sock *slp;
2239 struct lwp *lwp;
2240 struct mbuf **mrq;
2241 {
2242 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2243 struct mbuf *nam = nfsd->nd_nam;
2244 char *dpos = nfsd->nd_dpos;
2245 kauth_cred_t cred = nfsd->nd_cr;
2246 struct vattr va, dirfor, diraft;
2247 struct nfs_fattr *fp;
2248 struct nameidata nd;
2249 char *cp;
2250 u_int32_t *tl;
2251 int32_t t1;
2252 char *bpos;
2253 int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
2254 int v3 = (nfsd->nd_flag & ND_NFSV3);
2255 char *cp2;
2256 struct mbuf *mb, *mreq;
2257 struct vnode *vp, *dirp = (struct vnode *)0;
2258 nfsrvfh_t nsfh;
2259 u_quad_t frev;
2260
2261 nfsm_srvmtofh(&nsfh);
2262 nfsm_srvnamesiz(len);
2263 nd.ni_cnd.cn_cred = cred;
2264 nd.ni_cnd.cn_nameiop = CREATE;
2265 nd.ni_cnd.cn_flags = LOCKPARENT;
2266 error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
2267 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
2268 if (dirp && v3) {
2269 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, lwp);
2270 }
2271 if (error) {
2272 nfsm_reply(NFSX_WCCDATA(v3));
2273 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2274 if (dirp)
2275 vrele(dirp);
2276 return (0);
2277 }
2278 VATTR_NULL(&va);
2279 if (v3) {
2280 va.va_mode = 0;
2281 nfsm_srvsattr(&va);
2282 } else {
2283 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2284 va.va_mode = nfstov_mode(*tl++);
2285 }
2286 va.va_type = VDIR;
2287 vp = nd.ni_vp;
2288 if (vp != NULL) {
2289 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2290 if (nd.ni_dvp == vp)
2291 vrele(nd.ni_dvp);
2292 else
2293 vput(nd.ni_dvp);
2294 vrele(vp);
2295 error = EEXIST;
2296 goto out;
2297 }
2298 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2299 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
2300 if (!error) {
2301 vp = nd.ni_vp;
2302 error = nfsrv_composefh(vp, &nsfh, v3);
2303 if (!error)
2304 error = VOP_GETATTR(vp, &va, cred, lwp);
2305 vput(vp);
2306 }
2307 out:
2308 if (dirp) {
2309 if (v3) {
2310 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, lwp);
2311 }
2312 vrele(dirp);
2313 }
2314 nfsm_reply(NFSX_SRVFH(&nsfh, v3) + NFSX_POSTOPATTR(v3) +
2315 NFSX_WCCDATA(v3));
2316 if (v3) {
2317 if (!error) {
2318 nfsm_srvpostop_fh(&nsfh);
2319 nfsm_srvpostop_attr(0, &va);
2320 }
2321 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2322 } else {
2323 nfsm_srvfhtom(&nsfh, v3);
2324 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
2325 nfsm_srvfillattr(&va, fp);
2326 }
2327 return (0);
2328 nfsmout:
2329 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2330 if (nd.ni_dvp == nd.ni_vp)
2331 vrele(nd.ni_dvp);
2332 else
2333 vput(nd.ni_dvp);
2334 if (nd.ni_vp)
2335 vrele(nd.ni_vp);
2336 if (dirp)
2337 vrele(dirp);
2338 return (error);
2339 }
2340
2341 /*
2342 * nfs rmdir service
2343 */
2344 int
2345 nfsrv_rmdir(nfsd, slp, lwp, mrq)
2346 struct nfsrv_descript *nfsd;
2347 struct nfssvc_sock *slp;
2348 struct lwp *lwp;
2349 struct mbuf **mrq;
2350 {
2351 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2352 struct mbuf *nam = nfsd->nd_nam;
2353 char *dpos = nfsd->nd_dpos;
2354 kauth_cred_t cred = nfsd->nd_cr;
2355 u_int32_t *tl;
2356 int32_t t1;
2357 char *bpos;
2358 int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
2359 int v3 = (nfsd->nd_flag & ND_NFSV3);
2360 char *cp2;
2361 struct mbuf *mb, *mreq;
2362 struct vnode *vp, *dirp = (struct vnode *)0;
2363 struct vattr dirfor, diraft;
2364 nfsrvfh_t nsfh;
2365 struct nameidata nd;
2366 u_quad_t frev;
2367
2368 nfsm_srvmtofh(&nsfh);
2369 nfsm_srvnamesiz(len);
2370 nd.ni_cnd.cn_cred = cred;
2371 nd.ni_cnd.cn_nameiop = DELETE;
2372 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
2373 error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
2374 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false);
2375 if (dirp && v3) {
2376 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, lwp);
2377 }
2378 if (error) {
2379 nfsm_reply(NFSX_WCCDATA(v3));
2380 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2381 if (dirp)
2382 vrele(dirp);
2383 return (0);
2384 }
2385 vp = nd.ni_vp;
2386 if (vp->v_type != VDIR) {
2387 error = ENOTDIR;
2388 goto out;
2389 }
2390 /*
2391 * No rmdir "." please.
2392 */
2393 if (nd.ni_dvp == vp) {
2394 error = EINVAL;
2395 goto out;
2396 }
2397 /*
2398 * The root of a mounted filesystem cannot be deleted.
2399 */
2400 if (vp->v_flag & VROOT)
2401 error = EBUSY;
2402 out:
2403 if (!error) {
2404 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2405 nqsrv_getl(vp, ND_WRITE);
2406 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2407 } else {
2408 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2409 if (nd.ni_dvp == nd.ni_vp)
2410 vrele(nd.ni_dvp);
2411 else
2412 vput(nd.ni_dvp);
2413 vput(vp);
2414 }
2415 if (dirp) {
2416 if (v3) {
2417 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, lwp);
2418 }
2419 vrele(dirp);
2420 }
2421 nfsm_reply(NFSX_WCCDATA(v3));
2422 if (v3) {
2423 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2424 return (0);
2425 }
2426 nfsm_srvdone;
2427 }
2428
2429 /*
2430 * nfs readdir service
2431 * - mallocs what it thinks is enough to read
2432 * count rounded up to a multiple of NFS_SRVDIRBLKSIZ <= NFS_MAXREADDIR
2433 * - calls VOP_READDIR()
2434 * - loops around building the reply
2435 * if the output generated exceeds count break out of loop
2436 * The nfsm_clget macro is used here so that the reply will be packed
2437 * tightly in mbuf clusters.
2438 * - it only knows that it has encountered eof when the VOP_READDIR()
2439 * reads nothing
2440 * - as such one readdir rpc will return eof false although you are there
2441 * and then the next will return eof
2442 * - it trims out records with d_fileno == 0
2443 * this doesn't matter for Unix clients, but they might confuse clients
2444 * for other os'.
2445 * - it trims out records with d_type == DT_WHT
2446 * these cannot be seen through NFS (unless we extend the protocol)
2447 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2448 * than requested, but this may not apply to all filesystems. For
2449 * example, client NFS does not { although it is never remote mounted
2450 * anyhow }
2451 * The alternate call nfsrv_readdirplus() does lookups as well.
2452 * PS: The NFS protocol spec. does not clarify what the "count" byte
2453 * argument is a count of.. just name strings and file id's or the
2454 * entire reply rpc or ...
2455 * I tried just file name and id sizes and it confused the Sun client,
2456 * so I am using the full rpc size now. The "paranoia.." comment refers
2457 * to including the status longwords that are not a part of the dir.
2458 * "entry" structures, but are in the rpc.
2459 */
2460
2461 #define NFS_SRVDIRBLKSIZ 1024
2462
2463 struct flrep {
2464 nfsuint64 fl_off;
2465 u_int32_t fl_postopok;
2466 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2467 u_int32_t fl_fhok;
2468 u_int32_t fl_fhsize;
2469 };
2470
2471 int
2472 nfsrv_readdir(nfsd, slp, lwp, mrq)
2473 struct nfsrv_descript *nfsd;
2474 struct nfssvc_sock *slp;
2475 struct lwp *lwp;
2476 struct mbuf **mrq;
2477 {
2478 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2479 struct mbuf *nam = nfsd->nd_nam;
2480 char *dpos = nfsd->nd_dpos;
2481 kauth_cred_t cred = nfsd->nd_cr;
2482 char *bp, *be;
2483 struct mbuf *mp;
2484 struct dirent *dp;
2485 char *cp;
2486 u_int32_t *tl;
2487 int32_t t1;
2488 char *bpos;
2489 struct mbuf *mb, *mreq, *mp2;
2490 char *cpos, *cend, *cp2, *rbuf;
2491 struct vnode *vp;
2492 struct vattr at;
2493 nfsrvfh_t nsfh;
2494 struct uio io;
2495 struct iovec iv;
2496 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2497 int siz, cnt, fullsiz, eofflag, rdonly, cache = 0, ncookies;
2498 int v3 = (nfsd->nd_flag & ND_NFSV3);
2499 u_quad_t frev, off, toff, verf;
2500 off_t *cookies = NULL, *cookiep;
2501 nfsuint64 jar;
2502
2503 nfsm_srvmtofh(&nsfh);
2504 if (v3) {
2505 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2506 toff = fxdr_hyper(tl);
2507 tl += 2;
2508 verf = fxdr_hyper(tl);
2509 tl += 2;
2510 } else {
2511 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2512 toff = fxdr_unsigned(u_quad_t, *tl++);
2513 }
2514 off = toff;
2515 cnt = fxdr_unsigned(int, *tl);
2516 siz = ((cnt + NFS_SRVDIRBLKSIZ - 1) & ~(NFS_SRVDIRBLKSIZ - 1));
2517 xfer = NFS_SRVMAXDATA(nfsd);
2518 if (siz > xfer)
2519 siz = xfer;
2520 fullsiz = siz;
2521 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
2522 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
2523 if (!error && vp->v_type != VDIR) {
2524 error = ENOTDIR;
2525 vput(vp);
2526 }
2527 if (error) {
2528 nfsm_reply(NFSX_UNSIGNED);
2529 nfsm_srvpostop_attr(getret, &at);
2530 return (0);
2531 }
2532 nqsrv_getl(vp, ND_READ);
2533 if (v3) {
2534 error = getret = VOP_GETATTR(vp, &at, cred, lwp);
2535 #ifdef NFS3_STRICTVERF
2536 /*
2537 * XXX This check is too strict for Solaris 2.5 clients.
2538 */
2539 if (!error && toff && verf != at.va_filerev)
2540 error = NFSERR_BAD_COOKIE;
2541 #endif
2542 }
2543 if (!error)
2544 error = nfsrv_access(vp, VEXEC, cred, rdonly, lwp, 0);
2545 if (error) {
2546 vput(vp);
2547 nfsm_reply(NFSX_POSTOPATTR(v3));
2548 nfsm_srvpostop_attr(getret, &at);
2549 return (0);
2550 }
2551 VOP_UNLOCK(vp, 0);
2552 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2553 again:
2554 iv.iov_base = rbuf;
2555 iv.iov_len = fullsiz;
2556 io.uio_iov = &iv;
2557 io.uio_iovcnt = 1;
2558 io.uio_offset = (off_t)off;
2559 io.uio_resid = fullsiz;
2560 io.uio_rw = UIO_READ;
2561 UIO_SETUP_SYSSPACE(&io);
2562 eofflag = 0;
2563 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2564
2565 error = VOP_READDIR(vp, &io, cred, &eofflag, &cookies, &ncookies);
2566
2567 off = (off_t)io.uio_offset;
2568 if (!cookies && !error)
2569 error = NFSERR_PERM;
2570 if (v3) {
2571 getret = VOP_GETATTR(vp, &at, cred, lwp);
2572 if (!error)
2573 error = getret;
2574 }
2575
2576 VOP_UNLOCK(vp, 0);
2577 if (error) {
2578 vrele(vp);
2579 free((void *)rbuf, M_TEMP);
2580 if (cookies)
2581 free((void *)cookies, M_TEMP);
2582 nfsm_reply(NFSX_POSTOPATTR(v3));
2583 nfsm_srvpostop_attr(getret, &at);
2584 return (0);
2585 }
2586 if (io.uio_resid) {
2587 siz -= io.uio_resid;
2588
2589 /*
2590 * If nothing read, return eof
2591 * rpc reply
2592 */
2593 if (siz == 0) {
2594 vrele(vp);
2595 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
2596 2 * NFSX_UNSIGNED);
2597 if (v3) {
2598 nfsm_srvpostop_attr(getret, &at);
2599 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2600 txdr_hyper(at.va_filerev, tl);
2601 tl += 2;
2602 } else
2603 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2604 *tl++ = nfs_false;
2605 *tl = nfs_true;
2606 free((void *)rbuf, M_TEMP);
2607 free((void *)cookies, M_TEMP);
2608 return (0);
2609 }
2610 }
2611
2612 /*
2613 * Check for degenerate cases of nothing useful read.
2614 * If so go try again
2615 */
2616 cpos = rbuf;
2617 cend = rbuf + siz;
2618 dp = (struct dirent *)cpos;
2619 cookiep = cookies;
2620
2621 while (cpos < cend && ncookies > 0 &&
2622 (dp->d_fileno == 0 || dp->d_type == DT_WHT)) {
2623 cpos += dp->d_reclen;
2624 dp = (struct dirent *)cpos;
2625 cookiep++;
2626 ncookies--;
2627 }
2628 if (cpos >= cend || ncookies == 0) {
2629 toff = off;
2630 siz = fullsiz;
2631 free(cookies, M_TEMP);
2632 cookies = NULL;
2633 goto again;
2634 }
2635
2636 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
2637 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
2638 if (v3) {
2639 nfsm_srvpostop_attr(getret, &at);
2640 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2641 txdr_hyper(at.va_filerev, tl);
2642 }
2643 mp = mp2 = mb;
2644 bp = bpos;
2645 be = bp + M_TRAILINGSPACE(mp);
2646
2647 /* Loop through the records and build reply */
2648 while (cpos < cend && ncookies > 0) {
2649 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
2650 nlen = dp->d_namlen;
2651 rem = nfsm_rndup(nlen)-nlen;
2652 len += (4 * NFSX_UNSIGNED + nlen + rem);
2653 if (v3)
2654 len += 2 * NFSX_UNSIGNED;
2655 if (len > cnt) {
2656 eofflag = 0;
2657 break;
2658 }
2659 /*
2660 * Build the directory record xdr from
2661 * the dirent entry.
2662 */
2663 nfsm_clget;
2664 *tl = nfs_true;
2665 bp += NFSX_UNSIGNED;
2666 if (v3) {
2667 nfsm_clget;
2668 *tl = txdr_unsigned(dp->d_fileno >> 32);
2669 bp += NFSX_UNSIGNED;
2670 }
2671 nfsm_clget;
2672 *tl = txdr_unsigned(dp->d_fileno);
2673 bp += NFSX_UNSIGNED;
2674 nfsm_clget;
2675 *tl = txdr_unsigned(nlen);
2676 bp += NFSX_UNSIGNED;
2677
2678 /* And loop around copying the name */
2679 xfer = nlen;
2680 cp = dp->d_name;
2681 while (xfer > 0) {
2682 nfsm_clget;
2683 if ((bp+xfer) > be)
2684 tsiz = be-bp;
2685 else
2686 tsiz = xfer;
2687 memcpy(bp, cp, tsiz);
2688 bp += tsiz;
2689 xfer -= tsiz;
2690 if (xfer > 0)
2691 cp += tsiz;
2692 }
2693 /* And null pad to an int32_t boundary */
2694 for (i = 0; i < rem; i++)
2695 *bp++ = '\0';
2696 nfsm_clget;
2697
2698 /* Finish off the record */
2699 txdr_hyper(*cookiep, &jar);
2700 if (v3) {
2701 *tl = jar.nfsuquad[0];
2702 bp += NFSX_UNSIGNED;
2703 nfsm_clget;
2704 }
2705 *tl = jar.nfsuquad[1];
2706 bp += NFSX_UNSIGNED;
2707 }
2708 cpos += dp->d_reclen;
2709 dp = (struct dirent *)cpos;
2710 cookiep++;
2711 ncookies--;
2712 }
2713 vrele(vp);
2714 nfsm_clget;
2715 *tl = nfs_false;
2716 bp += NFSX_UNSIGNED;
2717 nfsm_clget;
2718 if (eofflag)
2719 *tl = nfs_true;
2720 else
2721 *tl = nfs_false;
2722 bp += NFSX_UNSIGNED;
2723 if (mp != mb) {
2724 if (bp < be)
2725 mp->m_len = bp - mtod(mp, char *);
2726 } else
2727 mp->m_len += bp - bpos;
2728 free((void *)rbuf, M_TEMP);
2729 free((void *)cookies, M_TEMP);
2730 nfsm_srvdone;
2731 }
2732
2733 int
2734 nfsrv_readdirplus(nfsd, slp, lwp, mrq)
2735 struct nfsrv_descript *nfsd;
2736 struct nfssvc_sock *slp;
2737 struct lwp *lwp;
2738 struct mbuf **mrq;
2739 {
2740 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2741 struct mbuf *nam = nfsd->nd_nam;
2742 char *dpos = nfsd->nd_dpos;
2743 kauth_cred_t cred = nfsd->nd_cr;
2744 char *bp, *be;
2745 struct mbuf *mp;
2746 struct dirent *dp;
2747 char *cp;
2748 u_int32_t *tl;
2749 int32_t t1;
2750 char *bpos;
2751 struct mbuf *mb, *mreq, *mp2;
2752 char *cpos, *cend, *cp2, *rbuf;
2753 struct vnode *vp, *nvp;
2754 struct flrep fl;
2755 nfsrvfh_t nsfh;
2756 struct uio io;
2757 struct iovec iv;
2758 struct vattr va, at, *vap = &va;
2759 struct nfs_fattr *fp;
2760 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2761 int siz, cnt, fullsiz, eofflag, rdonly, cache = 0, dirlen, ncookies;
2762 u_quad_t frev, off, toff, verf;
2763 off_t *cookies = NULL, *cookiep;
2764
2765 nfsm_srvmtofh(&nsfh);
2766 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2767 toff = fxdr_hyper(tl);
2768 tl += 2;
2769 verf = fxdr_hyper(tl);
2770 tl += 2;
2771 siz = fxdr_unsigned(int, *tl++);
2772 cnt = fxdr_unsigned(int, *tl);
2773 off = toff;
2774 siz = ((siz + NFS_SRVDIRBLKSIZ - 1) & ~(NFS_SRVDIRBLKSIZ - 1));
2775 xfer = NFS_SRVMAXDATA(nfsd);
2776 if (siz > xfer)
2777 siz = xfer;
2778 fullsiz = siz;
2779 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
2780 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
2781 if (!error && vp->v_type != VDIR) {
2782 error = ENOTDIR;
2783 vput(vp);
2784 }
2785 if (error) {
2786 nfsm_reply(NFSX_UNSIGNED);
2787 nfsm_srvpostop_attr(getret, &at);
2788 return (0);
2789 }
2790 error = getret = VOP_GETATTR(vp, &at, cred, lwp);
2791 #ifdef NFS3_STRICTVERF
2792 /*
2793 * XXX This check is too strict for Solaris 2.5 clients.
2794 */
2795 if (!error && toff && verf != at.va_filerev)
2796 error = NFSERR_BAD_COOKIE;
2797 #endif
2798 if (!error) {
2799 nqsrv_getl(vp, ND_READ);
2800 error = nfsrv_access(vp, VEXEC, cred, rdonly, lwp, 0);
2801 }
2802 if (error) {
2803 vput(vp);
2804 nfsm_reply(NFSX_V3POSTOPATTR);
2805 nfsm_srvpostop_attr(getret, &at);
2806 return (0);
2807 }
2808 VOP_UNLOCK(vp, 0);
2809
2810 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2811 again:
2812 iv.iov_base = rbuf;
2813 iv.iov_len = fullsiz;
2814 io.uio_iov = &iv;
2815 io.uio_iovcnt = 1;
2816 io.uio_offset = (off_t)off;
2817 io.uio_resid = fullsiz;
2818 io.uio_rw = UIO_READ;
2819 UIO_SETUP_SYSSPACE(&io);
2820 eofflag = 0;
2821
2822 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2823
2824 error = VOP_READDIR(vp, &io, cred, &eofflag, &cookies, &ncookies);
2825
2826 off = (u_quad_t)io.uio_offset;
2827 getret = VOP_GETATTR(vp, &at, cred, lwp);
2828
2829 VOP_UNLOCK(vp, 0);
2830
2831 /*
2832 * If the VGET operation doesn't work for this filesystem,
2833 * we can't support readdirplus. Returning NOTSUPP should
2834 * make clients fall back to plain readdir.
2835 * There's no need to check for VPTOFH as well, we wouldn't
2836 * even be here otherwise.
2837 */
2838 if (!getret) {
2839 if ((getret = VFS_VGET(vp->v_mount, at.va_fileid, &nvp)))
2840 getret = (getret == EOPNOTSUPP) ?
2841 NFSERR_NOTSUPP : NFSERR_IO;
2842 else
2843 vput(nvp);
2844 }
2845
2846 if (!cookies && !error)
2847 error = NFSERR_PERM;
2848 if (!error)
2849 error = getret;
2850 if (error) {
2851 vrele(vp);
2852 if (cookies)
2853 free((void *)cookies, M_TEMP);
2854 free((void *)rbuf, M_TEMP);
2855 nfsm_reply(NFSX_V3POSTOPATTR);
2856 nfsm_srvpostop_attr(getret, &at);
2857 return (0);
2858 }
2859 if (io.uio_resid) {
2860 siz -= io.uio_resid;
2861
2862 /*
2863 * If nothing read, return eof
2864 * rpc reply
2865 */
2866 if (siz == 0) {
2867 vrele(vp);
2868 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
2869 2 * NFSX_UNSIGNED);
2870 nfsm_srvpostop_attr(getret, &at);
2871 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2872 txdr_hyper(at.va_filerev, tl);
2873 tl += 2;
2874 *tl++ = nfs_false;
2875 *tl = nfs_true;
2876 free((void *)cookies, M_TEMP);
2877 free((void *)rbuf, M_TEMP);
2878 return (0);
2879 }
2880 }
2881
2882 /*
2883 * Check for degenerate cases of nothing useful read.
2884 * If so go try again
2885 */
2886 cpos = rbuf;
2887 cend = rbuf + siz;
2888 dp = (struct dirent *)cpos;
2889 cookiep = cookies;
2890
2891 while (cpos < cend && ncookies > 0 &&
2892 (dp->d_fileno == 0 || dp->d_type == DT_WHT)) {
2893 cpos += dp->d_reclen;
2894 dp = (struct dirent *)cpos;
2895 cookiep++;
2896 ncookies--;
2897 }
2898 if (cpos >= cend || ncookies == 0) {
2899 toff = off;
2900 siz = fullsiz;
2901 free(cookies, M_TEMP);
2902 cookies = NULL;
2903 goto again;
2904 }
2905
2906 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2 * NFSX_UNSIGNED;
2907 nfsm_reply(cnt);
2908 nfsm_srvpostop_attr(getret, &at);
2909 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2910 txdr_hyper(at.va_filerev, tl);
2911 mp = mp2 = mb;
2912 bp = bpos;
2913 be = bp + M_TRAILINGSPACE(mp);
2914
2915 /* Loop through the records and build reply */
2916 while (cpos < cend && ncookies > 0) {
2917 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
2918 nfsrvfh_t nnsfh;
2919
2920 nlen = dp->d_namlen;
2921 rem = nfsm_rndup(nlen)-nlen;
2922
2923 /*
2924 * For readdir_and_lookup get the vnode using
2925 * the file number.
2926 */
2927 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
2928 goto invalid;
2929 if (nfsrv_composefh(nvp, &nnsfh, true)) {
2930 vput(nvp);
2931 goto invalid;
2932 }
2933 if (VOP_GETATTR(nvp, vap, cred, lwp)) {
2934 vput(nvp);
2935 goto invalid;
2936 }
2937 vput(nvp);
2938
2939 /*
2940 * If either the dircount or maxcount will be
2941 * exceeded, get out now. Both of these lengths
2942 * are calculated conservatively, including all
2943 * XDR overheads.
2944 */
2945 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
2946 NFSX_V3POSTOPATTR);
2947 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
2948 if (len > cnt || dirlen > fullsiz) {
2949 eofflag = 0;
2950 break;
2951 }
2952
2953 /*
2954 * Build the directory record xdr from
2955 * the dirent entry.
2956 */
2957 fp = (struct nfs_fattr *)&fl.fl_fattr;
2958 nfsm_srvfillattr(vap, fp);
2959 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
2960 fl.fl_fhok = nfs_true;
2961 fl.fl_postopok = nfs_true;
2962 txdr_hyper(*cookiep, fl.fl_off.nfsuquad);
2963
2964 nfsm_clget;
2965 *tl = nfs_true;
2966 bp += NFSX_UNSIGNED;
2967 nfsm_clget;
2968 *tl = txdr_unsigned(dp->d_fileno >> 32);
2969 bp += NFSX_UNSIGNED;
2970 nfsm_clget;
2971 *tl = txdr_unsigned(dp->d_fileno);
2972 bp += NFSX_UNSIGNED;
2973 nfsm_clget;
2974 *tl = txdr_unsigned(nlen);
2975 bp += NFSX_UNSIGNED;
2976
2977 /* And loop around copying the name */
2978 xfer = nlen;
2979 cp = dp->d_name;
2980 while (xfer > 0) {
2981 nfsm_clget;
2982 if ((bp + xfer) > be)
2983 tsiz = be - bp;
2984 else
2985 tsiz = xfer;
2986 memcpy(bp, cp, tsiz);
2987 bp += tsiz;
2988 xfer -= tsiz;
2989 if (xfer > 0)
2990 cp += tsiz;
2991 }
2992 /* And null pad to an int32_t boundary */
2993 for (i = 0; i < rem; i++)
2994 *bp++ = '\0';
2995
2996 /*
2997 * Now copy the flrep structure out.
2998 */
2999 xfer = sizeof(struct flrep);
3000 cp = (void *)&fl;
3001 while (xfer > 0) {
3002 nfsm_clget;
3003 if ((bp + xfer) > be)
3004 tsiz = be - bp;
3005 else
3006 tsiz = xfer;
3007 memcpy(bp, cp, tsiz);
3008 bp += tsiz;
3009 xfer -= tsiz;
3010 if (xfer > 0)
3011 cp += tsiz;
3012 }
3013
3014 /*
3015 * ... and filehandle.
3016 */
3017 xfer = NFSRVFH_SIZE(&nnsfh);
3018 cp = NFSRVFH_DATA(&nnsfh);
3019 while (xfer > 0) {
3020 nfsm_clget;
3021 if ((bp + xfer) > be)
3022 tsiz = be - bp;
3023 else
3024 tsiz = xfer;
3025 memcpy(bp, cp, tsiz);
3026 bp += tsiz;
3027 xfer -= tsiz;
3028 if (xfer > 0)
3029 cp += tsiz;
3030 }
3031 }
3032 invalid:
3033 cpos += dp->d_reclen;
3034 dp = (struct dirent *)cpos;
3035 cookiep++;
3036 ncookies--;
3037 }
3038 vrele(vp);
3039 nfsm_clget;
3040 *tl = nfs_false;
3041 bp += NFSX_UNSIGNED;
3042 nfsm_clget;
3043 if (eofflag)
3044 *tl = nfs_true;
3045 else
3046 *tl = nfs_false;
3047 bp += NFSX_UNSIGNED;
3048 if (mp != mb) {
3049 if (bp < be)
3050 mp->m_len = bp - mtod(mp, char *);
3051 } else
3052 mp->m_len += bp - bpos;
3053 free((void *)cookies, M_TEMP);
3054 free((void *)rbuf, M_TEMP);
3055 nfsm_srvdone;
3056 }
3057
3058 /*
3059 * nfs commit service
3060 */
3061 int
3062 nfsrv_commit(nfsd, slp, lwp, mrq)
3063 struct nfsrv_descript *nfsd;
3064 struct nfssvc_sock *slp;
3065 struct lwp *lwp;
3066 struct mbuf **mrq;
3067 {
3068 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3069 struct mbuf *nam = nfsd->nd_nam;
3070 char *dpos = nfsd->nd_dpos;
3071 kauth_cred_t cred = nfsd->nd_cr;
3072 struct vattr bfor, aft;
3073 struct vnode *vp;
3074 nfsrvfh_t nsfh;
3075 u_int32_t *tl;
3076 int32_t t1;
3077 char *bpos;
3078 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cache = 0;
3079 uint32_t cnt;
3080 char *cp2;
3081 struct mbuf *mb, *mreq;
3082 u_quad_t frev, off, end;
3083
3084 nfsm_srvmtofh(&nsfh);
3085 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3086
3087 off = fxdr_hyper(tl);
3088 tl += 2;
3089 cnt = fxdr_unsigned(uint32_t, *tl);
3090 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
3091 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
3092 if (error) {
3093 nfsm_reply(2 * NFSX_UNSIGNED);
3094 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3095 return (0);
3096 }
3097 for_ret = VOP_GETATTR(vp, &bfor, cred, lwp);
3098 end = (cnt > 0) ? off + cnt : vp->v_size;
3099 if (end < off || end > vp->v_size)
3100 end = vp->v_size;
3101 if (off < vp->v_size)
3102 error = VOP_FSYNC(vp, cred, FSYNC_WAIT, off, end, lwp);
3103 /* else error == 0, from nfsrv_fhtovp() */
3104 aft_ret = VOP_GETATTR(vp, &aft, cred, lwp);
3105 vput(vp);
3106 nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3107 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3108 if (!error) {
3109 nfsm_build(tl, u_int32_t *, NFSX_V3WRITEVERF);
3110 *tl++ = txdr_unsigned(boottime.tv_sec);
3111 *tl = txdr_unsigned(boottime.tv_usec);
3112 } else {
3113 return (0);
3114 }
3115 nfsm_srvdone;
3116 }
3117
3118 /*
3119 * nfs statfs service
3120 */
3121 int
3122 nfsrv_statfs(nfsd, slp, lwp, mrq)
3123 struct nfsrv_descript *nfsd;
3124 struct nfssvc_sock *slp;
3125 struct lwp *lwp;
3126 struct mbuf **mrq;
3127 {
3128 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3129 struct mbuf *nam = nfsd->nd_nam;
3130 char *dpos = nfsd->nd_dpos;
3131 kauth_cred_t cred = nfsd->nd_cr;
3132 struct statvfs *sf = NULL;
3133 struct nfs_statfs *sfp;
3134 u_int32_t *tl;
3135 int32_t t1;
3136 char *bpos;
3137 int error = 0, rdonly, cache = 0, getret = 1;
3138 int v3 = (nfsd->nd_flag & ND_NFSV3);
3139 char *cp2;
3140 struct mbuf *mb, *mreq;
3141 struct vnode *vp;
3142 struct vattr at;
3143 nfsrvfh_t nsfh;
3144 u_quad_t frev, tval;
3145
3146 nfsm_srvmtofh(&nsfh);
3147 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
3148 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
3149 if (error) {
3150 nfsm_reply(NFSX_UNSIGNED);
3151 nfsm_srvpostop_attr(getret, &at);
3152 return (0);
3153 }
3154 sf = malloc(sizeof(*sf), M_TEMP, M_WAITOK);
3155 error = VFS_STATVFS(vp->v_mount, sf, lwp);
3156 getret = VOP_GETATTR(vp, &at, cred, lwp);
3157 vput(vp);
3158 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3159 if (v3)
3160 nfsm_srvpostop_attr(getret, &at);
3161 if (error) {
3162 free(sf, M_TEMP);
3163 return (0);
3164 }
3165 nfsm_build(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
3166 if (v3) {
3167 tval = (u_quad_t)((quad_t)sf->f_blocks * (quad_t)sf->f_frsize);
3168 txdr_hyper(tval, &sfp->sf_tbytes);
3169 tval = (u_quad_t)((quad_t)sf->f_bfree * (quad_t)sf->f_frsize);
3170 txdr_hyper(tval, &sfp->sf_fbytes);
3171 tval = (u_quad_t)((quad_t)sf->f_bavail * (quad_t)sf->f_frsize);
3172 txdr_hyper(tval, &sfp->sf_abytes);
3173 tval = (u_quad_t)sf->f_files;
3174 txdr_hyper(tval, &sfp->sf_tfiles);
3175 tval = (u_quad_t)sf->f_ffree;
3176 txdr_hyper(tval, &sfp->sf_ffiles);
3177 txdr_hyper(tval, &sfp->sf_afiles);
3178 sfp->sf_invarsec = 0;
3179 } else {
3180 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3181 sfp->sf_bsize = txdr_unsigned(sf->f_frsize);
3182 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3183 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3184 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3185 }
3186 nfsmout:
3187 if (sf)
3188 free(sf, M_TEMP);
3189 return error;
3190 }
3191
3192 /*
3193 * nfs fsinfo service
3194 */
3195 int
3196 nfsrv_fsinfo(nfsd, slp, lwp, mrq)
3197 struct nfsrv_descript *nfsd;
3198 struct nfssvc_sock *slp;
3199 struct lwp *lwp;
3200 struct mbuf **mrq;
3201 {
3202 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3203 struct mbuf *nam = nfsd->nd_nam;
3204 char *dpos = nfsd->nd_dpos;
3205 kauth_cred_t cred = nfsd->nd_cr;
3206 u_int32_t *tl;
3207 struct nfsv3_fsinfo *sip;
3208 int32_t t1;
3209 char *bpos;
3210 int error = 0, rdonly, cache = 0, getret = 1;
3211 uint32_t maxdata;
3212 char *cp2;
3213 struct mbuf *mb, *mreq;
3214 struct vnode *vp;
3215 struct vattr at;
3216 nfsrvfh_t nsfh;
3217 u_quad_t frev, maxfsize;
3218 struct statvfs *sb;
3219
3220 nfsm_srvmtofh(&nsfh);
3221 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
3222 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
3223 if (error) {
3224 nfsm_reply(NFSX_UNSIGNED);
3225 nfsm_srvpostop_attr(getret, &at);
3226 return (0);
3227 }
3228
3229 /* XXX Try to make a guess on the max file size. */
3230 sb = malloc(sizeof(*sb), M_TEMP, M_WAITOK);
3231 VFS_STATVFS(vp->v_mount, sb, (struct lwp *)0);
3232 maxfsize = (u_quad_t)0x80000000 * sb->f_frsize - 1;
3233 free(sb, M_TEMP);
3234
3235 getret = VOP_GETATTR(vp, &at, cred, lwp);
3236 vput(vp);
3237 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3238 nfsm_srvpostop_attr(getret, &at);
3239 nfsm_build(sip, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3240
3241 /*
3242 * XXX
3243 * There should be file system VFS OP(s) to get this information.
3244 * For now, assume ufs.
3245 */
3246 if (slp->ns_so->so_type == SOCK_DGRAM)
3247 maxdata = NFS_MAXDGRAMDATA;
3248 else
3249 maxdata = NFS_MAXDATA;
3250 sip->fs_rtmax = txdr_unsigned(maxdata);
3251 sip->fs_rtpref = txdr_unsigned(maxdata);
3252 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3253 sip->fs_wtmax = txdr_unsigned(maxdata);
3254 sip->fs_wtpref = txdr_unsigned(maxdata);
3255 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3256 sip->fs_dtpref = txdr_unsigned(maxdata);
3257 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3258 sip->fs_timedelta.nfsv3_sec = 0;
3259 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3260 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3261 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3262 NFSV3FSINFO_CANSETTIME);
3263 nfsm_srvdone;
3264 }
3265
3266 /*
3267 * nfs pathconf service
3268 */
3269 int
3270 nfsrv_pathconf(nfsd, slp, lwp, mrq)
3271 struct nfsrv_descript *nfsd;
3272 struct nfssvc_sock *slp;
3273 struct lwp *lwp;
3274 struct mbuf **mrq;
3275 {
3276 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3277 struct mbuf *nam = nfsd->nd_nam;
3278 char *dpos = nfsd->nd_dpos;
3279 kauth_cred_t cred = nfsd->nd_cr;
3280 u_int32_t *tl;
3281 struct nfsv3_pathconf *pc;
3282 int32_t t1;
3283 char *bpos;
3284 int error = 0, rdonly, cache = 0, getret = 1;
3285 register_t linkmax, namemax, chownres, notrunc;
3286 char *cp2;
3287 struct mbuf *mb, *mreq;
3288 struct vnode *vp;
3289 struct vattr at;
3290 nfsrvfh_t nsfh;
3291 u_quad_t frev;
3292
3293 nfsm_srvmtofh(&nsfh);
3294 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
3295 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false);
3296 if (error) {
3297 nfsm_reply(NFSX_UNSIGNED);
3298 nfsm_srvpostop_attr(getret, &at);
3299 return (0);
3300 }
3301 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3302 if (!error)
3303 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3304 if (!error)
3305 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3306 if (!error)
3307 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3308 getret = VOP_GETATTR(vp, &at, cred, lwp);
3309 vput(vp);
3310 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3311 nfsm_srvpostop_attr(getret, &at);
3312 if (error)
3313 return (0);
3314 nfsm_build(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3315
3316 pc->pc_linkmax = txdr_unsigned(linkmax);
3317 pc->pc_namemax = txdr_unsigned(namemax);
3318 pc->pc_notrunc = txdr_unsigned(notrunc);
3319 pc->pc_chownrestricted = txdr_unsigned(chownres);
3320
3321 /*
3322 * These should probably be supported by VOP_PATHCONF(), but
3323 * until msdosfs is exportable (why would you want to?), the
3324 * Unix defaults should be ok.
3325 */
3326 pc->pc_caseinsensitive = nfs_false;
3327 pc->pc_casepreserving = nfs_true;
3328 nfsm_srvdone;
3329 }
3330
3331 /*
3332 * Null operation, used by clients to ping server
3333 */
3334 /* ARGSUSED */
3335 int
3336 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3337 struct lwp *lwp, struct mbuf **mrq)
3338 {
3339 struct mbuf *mrep = nfsd->nd_mrep;
3340 char *bpos;
3341 int error = NFSERR_RETVOID, cache = 0;
3342 struct mbuf *mb, *mreq;
3343 u_quad_t frev;
3344
3345 nfsm_reply(0);
3346 return (0);
3347 }
3348
3349 /*
3350 * No operation, used for obsolete procedures
3351 */
3352 /* ARGSUSED */
3353 int
3354 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3355 struct lwp *lwp, struct mbuf **mrq)
3356 {
3357 struct mbuf *mrep = nfsd->nd_mrep;
3358 char *bpos;
3359 int error, cache = 0;
3360 struct mbuf *mb, *mreq;
3361 u_quad_t frev;
3362
3363 if (nfsd->nd_repstat)
3364 error = nfsd->nd_repstat;
3365 else
3366 error = EPROCUNAVAIL;
3367 nfsm_reply(0);
3368 return (0);
3369 }
3370
3371 /*
3372 * Perform access checking for vnodes obtained from file handles that would
3373 * refer to files already opened by a Unix client. You cannot just use
3374 * vn_writechk() and VOP_ACCESS() for two reasons.
3375 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
3376 * 2 - The owner is to be given access irrespective of mode bits for some
3377 * operations, so that processes that chmod after opening a file don't
3378 * break. I don't like this because it opens a security hole, but since
3379 * the nfs server opens a security hole the size of a barn door anyhow,
3380 * what the heck.
3381 *
3382 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
3383 * will return EPERM instead of EACCESS. EPERM is always an error.
3384 */
3385 int
3386 nfsrv_access(vp, flags, cred, rdonly, lwp, override)
3387 struct vnode *vp;
3388 int flags;
3389 kauth_cred_t cred;
3390 int rdonly;
3391 struct lwp *lwp;
3392 int override;
3393 {
3394 struct vattr vattr;
3395 int error;
3396 if (flags & VWRITE) {
3397 /* Just vn_writechk() changed to check rdonly */
3398 /*
3399 * Disallow write attempts on read-only file systems;
3400 * unless the file is a socket or a block or character
3401 * device resident on the file system.
3402 */
3403 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3404 switch (vp->v_type) {
3405 case VREG:
3406 case VDIR:
3407 case VLNK:
3408 return (EROFS);
3409 default:
3410 break;
3411 }
3412 }
3413
3414 /*
3415 * If the vnode is in use as a process's text,
3416 * we can't allow writing.
3417 */
3418 if (vp->v_flag & VTEXT)
3419 return (ETXTBSY);
3420 }
3421 error = VOP_GETATTR(vp, &vattr, cred, lwp);
3422 if (error)
3423 return (error);
3424 error = VOP_ACCESS(vp, flags, cred, lwp);
3425 /*
3426 * Allow certain operations for the owner (reads and writes
3427 * on files that are already open).
3428 */
3429 if (override && error == EACCES && kauth_cred_geteuid(cred) == vattr.va_uid)
3430 error = 0;
3431 return error;
3432 }
3433