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