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