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