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