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