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