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