nfs_serv.c revision 1.100.4.1 1 /* $NetBSD: nfs_serv.c,v 1.100.4.1 2006/02/04 14:12:50 simonb 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.100.4.1 2006/02/04 14:12:50 simonb 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;
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, 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, 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 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPORFATTR(v3) + NFSX_POSTOPATTR(v3));
453 if (error) {
454 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
455 return (0);
456 }
457 nfsm_srvfhtom(fhp, v3);
458 if (v3) {
459 nfsm_srvpostop_attr(0, &va);
460 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
461 } else {
462 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
463 nfsm_srvfillattr(&va, fp);
464 }
465 nfsm_srvdone;
466 }
467
468 /*
469 * nfs readlink service
470 */
471 int
472 nfsrv_readlink(nfsd, slp, lwp, mrq)
473 struct nfsrv_descript *nfsd;
474 struct nfssvc_sock *slp;
475 struct lwp *lwp;
476 struct mbuf **mrq;
477 {
478 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
479 struct mbuf *nam = nfsd->nd_nam;
480 caddr_t dpos = nfsd->nd_dpos;
481 struct ucred *cred = &nfsd->nd_cr;
482 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
483 struct iovec *ivp = iv;
484 struct mbuf *mp;
485 u_int32_t *tl;
486 int32_t t1;
487 caddr_t bpos;
488 int error = 0, rdonly, cache, i, padlen, getret;
489 uint32_t len;
490 int v3 = (nfsd->nd_flag & ND_NFSV3);
491 char *cp2;
492 struct mbuf *mb, *mp2 = NULL, *mp3 = NULL, *mreq;
493 struct vnode *vp;
494 struct vattr attr;
495 nfsfh_t nfh;
496 fhandle_t *fhp;
497 struct uio io, *uiop = &io;
498 u_quad_t frev;
499
500 fhp = &nfh.fh_generic;
501 nfsm_srvmtofh(fhp);
502 len = 0;
503 i = 0;
504 while (len < NFS_MAXPATHLEN) {
505 mp = m_get(M_WAIT, MT_DATA);
506 MCLAIM(mp, &nfs_mowner);
507 m_clget(mp, M_WAIT);
508 mp->m_len = NFSMSIZ(mp);
509 if (len == 0)
510 mp3 = mp2 = mp;
511 else {
512 mp2->m_next = mp;
513 mp2 = mp;
514 }
515 if ((len+mp->m_len) > NFS_MAXPATHLEN) {
516 mp->m_len = NFS_MAXPATHLEN-len;
517 len = NFS_MAXPATHLEN;
518 } else
519 len += mp->m_len;
520 ivp->iov_base = mtod(mp, caddr_t);
521 ivp->iov_len = mp->m_len;
522 i++;
523 ivp++;
524 }
525 uiop->uio_iov = iv;
526 uiop->uio_iovcnt = i;
527 uiop->uio_offset = 0;
528 uiop->uio_resid = len;
529 uiop->uio_rw = UIO_READ;
530 uiop->uio_segflg = UIO_SYSSPACE;
531 uiop->uio_lwp = NULL;
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, 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 uiop->uio_segflg = UIO_SYSSPACE;
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, 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_segflg = UIO_SYSSPACE;
953 uiop->uio_lwp = NULL;
954 uiop->uio_offset = off;
955 error = VOP_WRITE(vp, uiop, ioflags, cred);
956 nfsstats.srvvop_writes++;
957 free(iv, M_TEMP);
958 }
959 aftat_ret = VOP_GETATTR(vp, &va, cred, lwp);
960 vput(vp);
961 vn_finished_write(mntp, 0);
962 if (!error)
963 error = aftat_ret;
964 nfsm_reply(NFSX_PREOPATTR(v3) + NFSX_POSTOPORFATTR(v3) +
965 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(v3));
966 if (v3) {
967 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
968 if (error)
969 return (0);
970 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
971 *tl++ = txdr_unsigned(retlen);
972 if (stable == NFSV3WRITE_UNSTABLE)
973 *tl++ = txdr_unsigned(stable);
974 else
975 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
976 /*
977 * Actually, there is no need to txdr these fields,
978 * but it may make the values more human readable,
979 * for debugging purposes.
980 */
981 *tl++ = txdr_unsigned(boottime.tv_sec);
982 *tl = txdr_unsigned(boottime.tv_usec);
983 } else {
984 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
985 nfsm_srvfillattr(&va, fp);
986 }
987 nfsm_srvdone;
988 }
989
990 /*
991 * NFS write service with write gathering support. Called when
992 * nfsrvw_procrastinate > 0.
993 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
994 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
995 * Jan. 1994.
996 */
997 int
998 nfsrv_writegather(ndp, slp, lwp, mrq)
999 struct nfsrv_descript **ndp;
1000 struct nfssvc_sock *slp;
1001 struct lwp *lwp;
1002 struct mbuf **mrq;
1003 {
1004 struct timeval now;
1005 struct iovec *ivp;
1006 struct mbuf *mp;
1007 struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1008 struct nfs_fattr *fp;
1009 int i = 0;
1010 struct iovec *iov;
1011 struct nfsrvw_delayhash *wpp;
1012 struct ucred *cred;
1013 struct vattr va, forat;
1014 u_int32_t *tl;
1015 int32_t t1;
1016 caddr_t bpos, dpos;
1017 int error = 0, rdonly, cache, len = 0, forat_ret = 1;
1018 int ioflags, aftat_ret = 1, s, adjust, v3, zeroing;
1019 char *cp2;
1020 struct mbuf *mb, *mreq, *mrep, *md;
1021 struct vnode *vp;
1022 struct uio io, *uiop = &io;
1023 u_quad_t frev, cur_usec;
1024 struct mount *mntp = NULL;
1025
1026 *mrq = NULL;
1027 if (*ndp) {
1028 nfsd = *ndp;
1029 *ndp = NULL;
1030 mrep = nfsd->nd_mrep;
1031 md = nfsd->nd_md;
1032 dpos = nfsd->nd_dpos;
1033 cred = &nfsd->nd_cr;
1034 v3 = (nfsd->nd_flag & ND_NFSV3);
1035 LIST_INIT(&nfsd->nd_coalesce);
1036 nfsd->nd_mreq = NULL;
1037 nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1038 getmicrotime(&now);
1039 cur_usec = (u_quad_t)now.tv_sec * 1000000 + (u_quad_t)now.tv_usec;
1040 nfsd->nd_time = cur_usec + nfsrvw_procrastinate;
1041
1042 /*
1043 * Now, get the write header..
1044 */
1045 nfsm_srvmtofh(&nfsd->nd_fh);
1046 if (v3) {
1047 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1048 nfsd->nd_off = fxdr_hyper(tl);
1049 tl += 3;
1050 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1051 } else {
1052 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1053 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1054 tl += 2;
1055 }
1056 len = fxdr_unsigned(int32_t, *tl);
1057 nfsd->nd_len = len;
1058 nfsd->nd_eoff = nfsd->nd_off + len;
1059
1060 /*
1061 * Trim the header out of the mbuf list and trim off any trailing
1062 * junk so that the mbuf list has only the write data.
1063 */
1064 zeroing = 1;
1065 i = 0;
1066 mp = mrep;
1067 while (mp) {
1068 if (mp == md) {
1069 zeroing = 0;
1070 adjust = dpos - mtod(mp, caddr_t);
1071 mp->m_len -= adjust;
1072 if (mp->m_len > 0 && adjust > 0)
1073 NFSMADV(mp, adjust);
1074 }
1075 if (zeroing)
1076 mp->m_len = 0;
1077 else {
1078 i += mp->m_len;
1079 if (i > len) {
1080 mp->m_len -= (i - len);
1081 zeroing = 1;
1082 }
1083 }
1084 mp = mp->m_next;
1085 }
1086 if (len > NFS_MAXDATA || len < 0 || i < len) {
1087 nfsmout:
1088 m_freem(mrep);
1089 error = EIO;
1090 nfsm_writereply(2 * NFSX_UNSIGNED, v3);
1091 if (v3)
1092 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1093 nfsd->nd_mreq = mreq;
1094 nfsd->nd_mrep = NULL;
1095 nfsd->nd_time = 0;
1096 }
1097
1098 /*
1099 * Add this entry to the hash and time queues.
1100 */
1101 s = splsoftclock();
1102 owp = NULL;
1103 wp = LIST_FIRST(&slp->ns_tq);
1104 while (wp && wp->nd_time < nfsd->nd_time) {
1105 owp = wp;
1106 wp = LIST_NEXT(wp, nd_tq);
1107 }
1108 if (owp) {
1109 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1110 } else {
1111 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1112 }
1113 if (nfsd->nd_mrep) {
1114 wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
1115 owp = NULL;
1116 wp = LIST_FIRST(wpp);
1117 while (wp &&
1118 memcmp(&nfsd->nd_fh, &wp->nd_fh, NFSX_V3FH)) {
1119 owp = wp;
1120 wp = LIST_NEXT(wp, nd_hash);
1121 }
1122 while (wp && wp->nd_off < nfsd->nd_off &&
1123 !memcmp(&nfsd->nd_fh, &wp->nd_fh, NFSX_V3FH)) {
1124 owp = wp;
1125 wp = LIST_NEXT(wp, nd_hash);
1126 }
1127 if (owp) {
1128 LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1129
1130 /*
1131 * Search the hash list for overlapping entries and
1132 * coalesce.
1133 */
1134 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1135 wp = LIST_NEXT(nfsd, nd_hash);
1136 if (NFSW_SAMECRED(owp, nfsd))
1137 nfsrvw_coalesce(owp, nfsd);
1138 }
1139 } else {
1140 LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1141 }
1142 }
1143 splx(s);
1144 }
1145
1146 /*
1147 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1148 * and generate the associated reply mbuf list(s).
1149 */
1150 loop1:
1151 getmicrotime(&now);
1152 cur_usec = (u_quad_t)now.tv_sec * 1000000 + (u_quad_t)now.tv_usec;
1153 s = splsoftclock();
1154 for (nfsd = LIST_FIRST(&slp->ns_tq); nfsd; nfsd = owp) {
1155 owp = LIST_NEXT(nfsd, nd_tq);
1156 if (nfsd->nd_time > cur_usec)
1157 break;
1158 if (nfsd->nd_mreq)
1159 continue;
1160 LIST_REMOVE(nfsd, nd_tq);
1161 LIST_REMOVE(nfsd, nd_hash);
1162 splx(s);
1163 mrep = nfsd->nd_mrep;
1164 nfsd->nd_mrep = NULL;
1165 cred = &nfsd->nd_cr;
1166 v3 = (nfsd->nd_flag & ND_NFSV3);
1167 forat_ret = aftat_ret = 1;
1168 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &vp, cred, slp,
1169 nfsd->nd_nam, &rdonly, (nfsd->nd_flag & ND_KERBAUTH),
1170 FALSE);
1171 if (!error) {
1172 if (v3)
1173 forat_ret = VOP_GETATTR(vp, &forat, cred, lwp);
1174 if (vp->v_type != VREG) {
1175 if (v3)
1176 error = EINVAL;
1177 else
1178 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1179 }
1180 } else
1181 vp = NULL;
1182 if (!error) {
1183 nqsrv_getl(vp, ND_WRITE);
1184 error = nfsrv_access(vp, VWRITE, cred, rdonly, lwp, 1);
1185 }
1186
1187 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1188 ioflags = IO_NODELOCKED;
1189 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1190 ioflags = (IO_SYNC | IO_NODELOCKED);
1191 else
1192 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1193 uiop->uio_rw = UIO_WRITE;
1194 uiop->uio_segflg = UIO_SYSSPACE;
1195 uiop->uio_lwp = NULL;
1196 uiop->uio_offset = nfsd->nd_off;
1197 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1198 if (uiop->uio_resid > 0) {
1199 mp = mrep;
1200 i = 0;
1201 while (mp) {
1202 if (mp->m_len > 0)
1203 i++;
1204 mp = mp->m_next;
1205 }
1206 uiop->uio_iovcnt = i;
1207 iov = malloc(i * sizeof (struct iovec), M_TEMP, M_WAITOK);
1208 uiop->uio_iov = ivp = iov;
1209 mp = mrep;
1210 while (mp) {
1211 if (mp->m_len > 0) {
1212 ivp->iov_base = mtod(mp, caddr_t);
1213 ivp->iov_len = mp->m_len;
1214 ivp++;
1215 }
1216 mp = mp->m_next;
1217 }
1218 if (!error) {
1219 if (vn_start_write(vp, &mntp, V_NOWAIT) != 0) {
1220 VOP_UNLOCK(vp, 0);
1221 vn_start_write(NULL, &mntp, V_WAIT);
1222 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1223 }
1224 if (!error) {
1225 error = VOP_WRITE(vp, uiop, ioflags, cred);
1226 nfsstats.srvvop_writes++;
1227 vn_finished_write(mntp, 0);
1228 }
1229 }
1230 free((caddr_t)iov, M_TEMP);
1231 }
1232 m_freem(mrep);
1233 if (vp) {
1234 aftat_ret = VOP_GETATTR(vp, &va, cred, lwp);
1235 vput(vp);
1236 }
1237
1238 /*
1239 * Loop around generating replies for all write rpcs that have
1240 * now been completed.
1241 */
1242 swp = nfsd;
1243 do {
1244 if (error) {
1245 nfsm_writereply(NFSX_WCCDATA(v3), v3);
1246 if (v3) {
1247 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1248 }
1249 } else {
1250 nfsm_writereply(NFSX_PREOPATTR(v3) +
1251 NFSX_POSTOPORFATTR(v3) + 2 * NFSX_UNSIGNED +
1252 NFSX_WRITEVERF(v3), v3);
1253 if (v3) {
1254 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1255 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1256 *tl++ = txdr_unsigned(nfsd->nd_len);
1257 *tl++ = txdr_unsigned(swp->nd_stable);
1258 /*
1259 * Actually, there is no need to txdr these fields,
1260 * but it may make the values more human readable,
1261 * for debugging purposes.
1262 */
1263 *tl++ = txdr_unsigned(boottime.tv_sec);
1264 *tl = txdr_unsigned(boottime.tv_usec);
1265 } else {
1266 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1267 nfsm_srvfillattr(&va, fp);
1268 }
1269 }
1270 nfsd->nd_mreq = mreq;
1271 if (nfsd->nd_mrep)
1272 panic("nfsrv_write: nd_mrep not free");
1273
1274 /*
1275 * Done. Put it at the head of the timer queue so that
1276 * the final phase can return the reply.
1277 */
1278 s = splsoftclock();
1279 if (nfsd != swp) {
1280 nfsd->nd_time = 0;
1281 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1282 }
1283 nfsd = LIST_FIRST(&swp->nd_coalesce);
1284 if (nfsd) {
1285 LIST_REMOVE(nfsd, nd_tq);
1286 }
1287 splx(s);
1288 } while (nfsd);
1289 s = splsoftclock();
1290 swp->nd_time = 0;
1291 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1292 splx(s);
1293 goto loop1;
1294 }
1295 splx(s);
1296
1297 /*
1298 * Search for a reply to return.
1299 */
1300 s = splsoftclock();
1301 LIST_FOREACH(nfsd, &slp->ns_tq, nd_tq) {
1302 if (nfsd->nd_mreq) {
1303 LIST_REMOVE(nfsd, nd_tq);
1304 *mrq = nfsd->nd_mreq;
1305 *ndp = nfsd;
1306 break;
1307 }
1308 }
1309 splx(s);
1310 return (0);
1311 }
1312
1313 /*
1314 * Coalesce the write request nfsd into owp. To do this we must:
1315 * - remove nfsd from the queues
1316 * - merge nfsd->nd_mrep into owp->nd_mrep
1317 * - update the nd_eoff and nd_stable for owp
1318 * - put nfsd on owp's nd_coalesce list
1319 * NB: Must be called at splsoftclock().
1320 */
1321 void
1322 nfsrvw_coalesce(owp, nfsd)
1323 struct nfsrv_descript *owp;
1324 struct nfsrv_descript *nfsd;
1325 {
1326 int overlap;
1327 struct mbuf *mp;
1328 struct nfsrv_descript *m;
1329
1330 LIST_REMOVE(nfsd, nd_hash);
1331 LIST_REMOVE(nfsd, nd_tq);
1332 if (owp->nd_eoff < nfsd->nd_eoff) {
1333 overlap = owp->nd_eoff - nfsd->nd_off;
1334 if (overlap < 0)
1335 panic("nfsrv_coalesce: bad off");
1336 if (overlap > 0)
1337 m_adj(nfsd->nd_mrep, overlap);
1338 mp = owp->nd_mrep;
1339 while (mp->m_next)
1340 mp = mp->m_next;
1341 mp->m_next = nfsd->nd_mrep;
1342 owp->nd_eoff = nfsd->nd_eoff;
1343 } else
1344 m_freem(nfsd->nd_mrep);
1345 nfsd->nd_mrep = NULL;
1346 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1347 owp->nd_stable = NFSV3WRITE_FILESYNC;
1348 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1349 owp->nd_stable == NFSV3WRITE_UNSTABLE)
1350 owp->nd_stable = NFSV3WRITE_DATASYNC;
1351 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1352 /*
1353 * nfsd might hold coalesce elements! Move them to owp.
1354 * Otherwise, requests may be lost and clients will be stuck.
1355 */
1356 while ((m = LIST_FIRST(&nfsd->nd_coalesce)) != NULL) {
1357 LIST_REMOVE(m, nd_tq);
1358 LIST_INSERT_HEAD(&owp->nd_coalesce, m, nd_tq);
1359 }
1360 }
1361
1362 /*
1363 * nfs create service
1364 * now does a truncate to 0 length via. setattr if it already exists
1365 */
1366 int
1367 nfsrv_create(nfsd, slp, lwp, mrq)
1368 struct nfsrv_descript *nfsd;
1369 struct nfssvc_sock *slp;
1370 struct lwp *lwp;
1371 struct mbuf **mrq;
1372 {
1373 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1374 struct mbuf *nam = nfsd->nd_nam;
1375 caddr_t dpos = nfsd->nd_dpos;
1376 struct ucred *cred = &nfsd->nd_cr;
1377 struct nfs_fattr *fp;
1378 struct vattr va, dirfor, diraft;
1379 struct nfsv2_sattr *sp;
1380 u_int32_t *tl;
1381 struct nameidata nd;
1382 caddr_t cp;
1383 int32_t t1;
1384 caddr_t bpos;
1385 int error = 0, cache, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1386 int rdev = 0;
1387 int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0;
1388 char *cp2;
1389 struct mbuf *mb, *mreq;
1390 struct vnode *vp = NULL, *dirp = NULL;
1391 nfsfh_t nfh;
1392 fhandle_t *fhp;
1393 u_quad_t frev, tempsize;
1394 u_char cverf[NFSX_V3CREATEVERF];
1395 struct mount *mp = NULL;
1396
1397 nd.ni_cnd.cn_nameiop = 0;
1398 fhp = &nfh.fh_generic;
1399 nfsm_srvmtofh(fhp);
1400 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL)
1401 return (ESTALE);
1402 vn_start_write(NULL, &mp, V_WAIT);
1403 nfsm_srvnamesiz(len);
1404 nd.ni_cnd.cn_cred = cred;
1405 nd.ni_cnd.cn_nameiop = CREATE;
1406 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
1407 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1408 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1409 if (dirp) {
1410 if (v3)
1411 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, lwp);
1412 else {
1413 vrele(dirp);
1414 dirp = (struct vnode *)0;
1415 }
1416 }
1417 if (error) {
1418 nfsm_reply(NFSX_WCCDATA(v3));
1419 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1420 if (dirp)
1421 vrele(dirp);
1422 vn_finished_write(mp, 0);
1423 return (0);
1424 }
1425 VATTR_NULL(&va);
1426 if (v3) {
1427 va.va_mode = 0;
1428 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1429 how = fxdr_unsigned(int, *tl);
1430 switch (how) {
1431 case NFSV3CREATE_GUARDED:
1432 if (nd.ni_vp) {
1433 error = EEXIST;
1434 break;
1435 }
1436 case NFSV3CREATE_UNCHECKED:
1437 nfsm_srvsattr(&va);
1438 break;
1439 case NFSV3CREATE_EXCLUSIVE:
1440 nfsm_dissect(cp, caddr_t, NFSX_V3CREATEVERF);
1441 memcpy(cverf, cp, NFSX_V3CREATEVERF);
1442 exclusive_flag = 1;
1443 break;
1444 };
1445 va.va_type = VREG;
1446 } else {
1447 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1448 va.va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1449 if (va.va_type == VNON)
1450 va.va_type = VREG;
1451 va.va_mode = nfstov_mode(sp->sa_mode);
1452 switch (va.va_type) {
1453 case VREG:
1454 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1455 if (tsize != -1)
1456 va.va_size = (u_quad_t)tsize;
1457 break;
1458 case VCHR:
1459 case VBLK:
1460 case VFIFO:
1461 rdev = fxdr_unsigned(int32_t, sp->sa_size);
1462 break;
1463 default:
1464 break;
1465 };
1466 }
1467
1468 /*
1469 * Iff doesn't exist, create it
1470 * otherwise just truncate to 0 length
1471 * should I set the mode too ??
1472 */
1473 if (nd.ni_vp == NULL) {
1474 if (va.va_type == VREG || va.va_type == VSOCK) {
1475 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1476 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
1477 if (!error) {
1478 if (exclusive_flag) {
1479 exclusive_flag = 0;
1480 VATTR_NULL(&va);
1481 /*
1482 * XXX
1483 * assuming NFSX_V3CREATEVERF
1484 * == sizeof(nfstime3)
1485 */
1486 fxdr_nfsv3time(cverf, &va.va_atime);
1487 error = VOP_SETATTR(nd.ni_vp, &va, cred,
1488 lwp);
1489 }
1490 }
1491 } else if (va.va_type == VCHR || va.va_type == VBLK ||
1492 va.va_type == VFIFO) {
1493 if (va.va_type == VCHR && rdev == 0xffffffff)
1494 va.va_type = VFIFO;
1495 if (va.va_type != VFIFO &&
1496 (error = suser(cred, (u_short *)0))) {
1497 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1498 vput(nd.ni_dvp);
1499 nfsm_reply(0);
1500 vn_finished_write(mp, 0);
1501 return (error);
1502 } else
1503 va.va_rdev = (dev_t)rdev;
1504 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1505 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd,
1506 &va);
1507 if (error) {
1508 nfsm_reply(0);
1509 }
1510 if (nd.ni_cnd.cn_flags & ISSYMLINK) {
1511 vrele(nd.ni_dvp);
1512 vput(nd.ni_vp);
1513 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1514 error = EINVAL;
1515 nfsm_reply(0);
1516 }
1517 } else {
1518 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1519 vput(nd.ni_dvp);
1520 error = ENXIO;
1521 }
1522 vp = nd.ni_vp;
1523 } else {
1524 vp = nd.ni_vp;
1525 if (nd.ni_dvp == vp)
1526 vrele(nd.ni_dvp);
1527 else
1528 vput(nd.ni_dvp);
1529 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1530 if (!error && va.va_size != -1) {
1531 error = nfsrv_access(vp, VWRITE, cred,
1532 (nd.ni_cnd.cn_flags & RDONLY), lwp, 0);
1533 if (!error) {
1534 nqsrv_getl(vp, ND_WRITE);
1535 tempsize = va.va_size;
1536 VATTR_NULL(&va);
1537 va.va_size = tempsize;
1538 error = VOP_SETATTR(vp, &va, cred, lwp);
1539 }
1540 }
1541 if (error)
1542 vput(vp);
1543 }
1544 if (!error) {
1545 memset((caddr_t)fhp, 0, sizeof(nfh));
1546 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsidx;
1547 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1548 if (!error)
1549 error = VOP_GETATTR(vp, &va, cred, lwp);
1550 vput(vp);
1551 }
1552 if (v3) {
1553 if (exclusive_flag && !error) {
1554 /*
1555 * XXX assuming NFSX_V3CREATEVERF == sizeof(nfstime3)
1556 */
1557 char oldverf[NFSX_V3CREATEVERF];
1558
1559 txdr_nfsv3time(&va.va_atime, oldverf);
1560 if (memcmp(cverf, oldverf, NFSX_V3CREATEVERF))
1561 error = EEXIST;
1562 }
1563 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, lwp);
1564 vrele(dirp);
1565 }
1566 nfsm_reply(NFSX_SRVFH(v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1567 if (v3) {
1568 if (!error) {
1569 nfsm_srvpostop_fh(fhp);
1570 nfsm_srvpostop_attr(0, &va);
1571 }
1572 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1573 } else {
1574 nfsm_srvfhtom(fhp, v3);
1575 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1576 nfsm_srvfillattr(&va, fp);
1577 }
1578 vn_finished_write(mp, 0);
1579 return (0);
1580 nfsmout:
1581 if (dirp)
1582 vrele(dirp);
1583 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1584 if (nd.ni_dvp == nd.ni_vp)
1585 vrele(nd.ni_dvp);
1586 else
1587 vput(nd.ni_dvp);
1588 if (nd.ni_vp)
1589 vput(nd.ni_vp);
1590 vn_finished_write(mp, 0);
1591 return (error);
1592 }
1593
1594 /*
1595 * nfs v3 mknod service
1596 */
1597 int
1598 nfsrv_mknod(nfsd, slp, lwp, mrq)
1599 struct nfsrv_descript *nfsd;
1600 struct nfssvc_sock *slp;
1601 struct lwp *lwp;
1602 struct mbuf **mrq;
1603 {
1604 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1605 struct mbuf *nam = nfsd->nd_nam;
1606 caddr_t dpos = nfsd->nd_dpos;
1607 struct ucred *cred = &nfsd->nd_cr;
1608 struct vattr va, dirfor, diraft;
1609 u_int32_t *tl;
1610 struct nameidata nd;
1611 int32_t t1;
1612 caddr_t bpos;
1613 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
1614 u_int32_t major, minor;
1615 enum vtype vtyp;
1616 char *cp2;
1617 struct mbuf *mb, *mreq;
1618 struct vnode *vp, *dirp = (struct vnode *)0;
1619 nfsfh_t nfh;
1620 fhandle_t *fhp;
1621 u_quad_t frev;
1622 struct mount *mp = NULL;
1623
1624 nd.ni_cnd.cn_nameiop = 0;
1625 fhp = &nfh.fh_generic;
1626 nfsm_srvmtofh(fhp);
1627 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL)
1628 return (ESTALE);
1629 vn_start_write(NULL, &mp, V_WAIT);
1630 nfsm_srvnamesiz(len);
1631 nd.ni_cnd.cn_cred = cred;
1632 nd.ni_cnd.cn_nameiop = CREATE;
1633 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
1634 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1635 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1636 if (dirp)
1637 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, lwp);
1638 if (error) {
1639 nfsm_reply(NFSX_WCCDATA(1));
1640 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1641 if (dirp)
1642 vrele(dirp);
1643 vn_finished_write(mp, 0);
1644 return (0);
1645 }
1646 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1647 vtyp = nfsv3tov_type(*tl);
1648 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1649 error = NFSERR_BADTYPE;
1650 goto abort;
1651 }
1652 VATTR_NULL(&va);
1653 va.va_mode = 0;
1654 nfsm_srvsattr(&va);
1655 if (vtyp == VCHR || vtyp == VBLK) {
1656 dev_t rdev;
1657
1658 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1659 major = fxdr_unsigned(u_int32_t, *tl++);
1660 minor = fxdr_unsigned(u_int32_t, *tl);
1661 rdev = makedev(major, minor);
1662 if (major(rdev) != major || minor(rdev) != minor) {
1663 error = EINVAL;
1664 goto abort;
1665 }
1666 va.va_rdev = rdev;
1667 }
1668
1669 /*
1670 * Iff doesn't exist, create it.
1671 */
1672 if (nd.ni_vp) {
1673 error = EEXIST;
1674 abort:
1675 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1676 if (nd.ni_dvp == nd.ni_vp)
1677 vrele(nd.ni_dvp);
1678 else
1679 vput(nd.ni_dvp);
1680 if (nd.ni_vp)
1681 vput(nd.ni_vp);
1682 goto out;
1683 }
1684 va.va_type = vtyp;
1685 if (vtyp == VSOCK) {
1686 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1687 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
1688 } else {
1689 if (va.va_type != VFIFO &&
1690 (error = suser(cred, (u_short *)0))) {
1691 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1692 vput(nd.ni_dvp);
1693 goto out;
1694 }
1695 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1696 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
1697 if (error) {
1698 goto out;
1699 }
1700 if (error)
1701 goto out;
1702 if (nd.ni_cnd.cn_flags & ISSYMLINK) {
1703 vput(nd.ni_vp);
1704 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1705 error = EINVAL;
1706 }
1707 }
1708 out:
1709 vp = nd.ni_vp;
1710 if (!error) {
1711 memset((caddr_t)fhp, 0, sizeof(nfh));
1712 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsidx;
1713 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1714 if (!error)
1715 error = VOP_GETATTR(vp, &va, cred, lwp);
1716 vput(vp);
1717 }
1718 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, lwp);
1719 vrele(dirp);
1720 nfsm_reply(NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1));
1721 if (!error) {
1722 nfsm_srvpostop_fh(fhp);
1723 nfsm_srvpostop_attr(0, &va);
1724 }
1725 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1726 vn_finished_write(mp, 0);
1727 return (0);
1728 nfsmout:
1729 if (dirp)
1730 vrele(dirp);
1731 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1732 if (nd.ni_dvp == nd.ni_vp)
1733 vrele(nd.ni_dvp);
1734 else
1735 vput(nd.ni_dvp);
1736 if (nd.ni_vp)
1737 vput(nd.ni_vp);
1738 vn_finished_write(mp, 0);
1739 return (error);
1740 }
1741
1742 /*
1743 * nfs remove service
1744 */
1745 int
1746 nfsrv_remove(nfsd, slp, lwp, mrq)
1747 struct nfsrv_descript *nfsd;
1748 struct nfssvc_sock *slp;
1749 struct lwp *lwp;
1750 struct mbuf **mrq;
1751 {
1752 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1753 struct mbuf *nam = nfsd->nd_nam;
1754 caddr_t dpos = nfsd->nd_dpos;
1755 struct ucred *cred = &nfsd->nd_cr;
1756 struct nameidata nd;
1757 u_int32_t *tl;
1758 int32_t t1;
1759 caddr_t bpos;
1760 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
1761 int v3 = (nfsd->nd_flag & ND_NFSV3);
1762 char *cp2;
1763 struct mbuf *mb, *mreq;
1764 struct vnode *vp, *dirp;
1765 struct vattr dirfor, diraft;
1766 nfsfh_t nfh;
1767 fhandle_t *fhp;
1768 u_quad_t frev;
1769 struct mount *mp = NULL;
1770
1771 #ifndef nolint
1772 vp = (struct vnode *)0;
1773 #endif
1774 fhp = &nfh.fh_generic;
1775 nfsm_srvmtofh(fhp);
1776 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL)
1777 return (ESTALE);
1778 vn_start_write(NULL, &mp, V_WAIT);
1779 nfsm_srvnamesiz(len);
1780 nd.ni_cnd.cn_cred = cred;
1781 nd.ni_cnd.cn_nameiop = DELETE;
1782 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
1783 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1784 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1785 if (dirp) {
1786 if (v3)
1787 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, lwp);
1788 else
1789 vrele(dirp);
1790 }
1791 if (!error) {
1792 vp = nd.ni_vp;
1793 if (vp->v_type == VDIR &&
1794 (error = suser(cred, (u_short *)0)) != 0)
1795 goto out;
1796 /*
1797 * The root of a mounted filesystem cannot be deleted.
1798 */
1799 if (vp->v_flag & VROOT) {
1800 error = EBUSY;
1801 goto out;
1802 }
1803 out:
1804 if (!error) {
1805 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1806 nqsrv_getl(vp, ND_WRITE);
1807 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1808 } else {
1809 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1810 if (nd.ni_dvp == vp)
1811 vrele(nd.ni_dvp);
1812 else
1813 vput(nd.ni_dvp);
1814 vput(vp);
1815 }
1816 }
1817 if (dirp && v3) {
1818 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, lwp);
1819 vrele(dirp);
1820 }
1821 nfsm_reply(NFSX_WCCDATA(v3));
1822 if (v3) {
1823 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1824 vn_finished_write(mp, 0);
1825 return (0);
1826 }
1827 vn_finished_write(mp, 0);
1828 nfsm_srvdone;
1829 }
1830
1831 /*
1832 * nfs rename service
1833 */
1834 int
1835 nfsrv_rename(nfsd, slp, lwp, mrq)
1836 struct nfsrv_descript *nfsd;
1837 struct nfssvc_sock *slp;
1838 struct lwp *lwp;
1839 struct mbuf **mrq;
1840 {
1841 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1842 struct mbuf *nam = nfsd->nd_nam;
1843 caddr_t dpos = nfsd->nd_dpos;
1844 struct ucred *cred = &nfsd->nd_cr;
1845 u_int32_t *tl;
1846 int32_t t1;
1847 caddr_t bpos;
1848 int error = 0, cache, fdirfor_ret = 1, fdiraft_ret = 1;
1849 uint32_t len, len2;
1850 int tdirfor_ret = 1, tdiraft_ret = 1;
1851 int v3 = (nfsd->nd_flag & ND_NFSV3);
1852 char *cp2;
1853 struct mbuf *mb, *mreq;
1854 struct nameidata fromnd, tond;
1855 struct vnode *fvp, *tvp, *tdvp, *fdirp = (struct vnode *)0;
1856 struct vnode *tdirp = (struct vnode *)0;
1857 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
1858 nfsfh_t fnfh, tnfh;
1859 fhandle_t *ffhp, *tfhp;
1860 u_quad_t frev;
1861 uid_t saved_uid;
1862 struct mount *mp = NULL;
1863
1864 #ifndef nolint
1865 fvp = (struct vnode *)0;
1866 #endif
1867 ffhp = &fnfh.fh_generic;
1868 tfhp = &tnfh.fh_generic;
1869 fromnd.ni_cnd.cn_nameiop = 0;
1870 tond.ni_cnd.cn_nameiop = 0;
1871 nfsm_srvmtofh(ffhp);
1872 if ((mp = vfs_getvfs(&ffhp->fh_fsid)) == NULL)
1873 return (ESTALE);
1874 vn_start_write(NULL, &mp, V_WAIT);
1875 nfsm_srvnamesiz(len);
1876 /*
1877 * Remember our original uid so that we can reset cr_uid before
1878 * the second nfs_namei() call, in case it is remapped.
1879 */
1880 saved_uid = cred->cr_uid;
1881 fromnd.ni_cnd.cn_cred = cred;
1882 fromnd.ni_cnd.cn_nameiop = DELETE;
1883 fromnd.ni_cnd.cn_flags = WANTPARENT | SAVESTART;
1884 error = nfs_namei(&fromnd, ffhp, len, slp, nam, &md,
1885 &dpos, &fdirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1886 if (fdirp) {
1887 if (v3)
1888 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, cred, lwp);
1889 else {
1890 vrele(fdirp);
1891 fdirp = (struct vnode *)0;
1892 }
1893 }
1894 if (error) {
1895 nfsm_reply(2 * NFSX_WCCDATA(v3));
1896 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
1897 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
1898 if (fdirp)
1899 vrele(fdirp);
1900 vn_finished_write(mp, 0);
1901 return (0);
1902 }
1903 fvp = fromnd.ni_vp;
1904 nfsm_srvmtofh(tfhp);
1905 if (v3) {
1906 nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED);
1907 len2 = fxdr_unsigned(uint32_t, *tl);
1908 /* len2 will be checked by nfs_namei */
1909 }
1910 else {
1911 /* NFSv2 */
1912 nfsm_strsiz(len2, NFS_MAXNAMLEN);
1913 }
1914 cred->cr_uid = saved_uid;
1915 tond.ni_cnd.cn_cred = cred;
1916 tond.ni_cnd.cn_nameiop = RENAME;
1917 tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART;
1918 error = nfs_namei(&tond, tfhp, len2, slp, nam, &md,
1919 &dpos, &tdirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1920 if (tdirp) {
1921 if (v3)
1922 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, cred, lwp);
1923 else {
1924 vrele(tdirp);
1925 tdirp = (struct vnode *)0;
1926 }
1927 }
1928 if (error) {
1929 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1930 vrele(fromnd.ni_dvp);
1931 vrele(fvp);
1932 goto out1;
1933 }
1934 tdvp = tond.ni_dvp;
1935 tvp = tond.ni_vp;
1936 if (tvp != NULL) {
1937 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1938 if (v3)
1939 error = EEXIST;
1940 else
1941 error = EISDIR;
1942 goto out;
1943 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1944 if (v3)
1945 error = EEXIST;
1946 else
1947 error = ENOTDIR;
1948 goto out;
1949 }
1950 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1951 if (v3)
1952 error = EXDEV;
1953 else
1954 error = ENOTEMPTY;
1955 goto out;
1956 }
1957 }
1958 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1959 if (v3)
1960 error = EXDEV;
1961 else
1962 error = ENOTEMPTY;
1963 goto out;
1964 }
1965 if (fvp->v_mount != tdvp->v_mount) {
1966 if (v3)
1967 error = EXDEV;
1968 else
1969 error = ENOTEMPTY;
1970 goto out;
1971 }
1972 if (fvp == tdvp) {
1973 if (v3)
1974 error = EINVAL;
1975 else
1976 error = ENOTEMPTY;
1977 }
1978 /*
1979 * If source is the same as the destination (that is the
1980 * same vnode with the same name in the same directory),
1981 * then there is nothing to do.
1982 */
1983 if (fvp == tvp && fromnd.ni_dvp == tdvp &&
1984 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
1985 !memcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
1986 fromnd.ni_cnd.cn_namelen))
1987 error = -1;
1988 out:
1989 if (!error) {
1990 nqsrv_getl(fromnd.ni_dvp, ND_WRITE);
1991 nqsrv_getl(tdvp, ND_WRITE);
1992 if (tvp) {
1993 nqsrv_getl(tvp, ND_WRITE);
1994 }
1995 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
1996 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
1997 } else {
1998 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
1999 if (tdvp == tvp)
2000 vrele(tdvp);
2001 else
2002 vput(tdvp);
2003 if (tvp)
2004 vput(tvp);
2005 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2006 vrele(fromnd.ni_dvp);
2007 vrele(fvp);
2008 if (error == -1)
2009 error = 0;
2010 }
2011 vrele(tond.ni_startdir);
2012 PNBUF_PUT(tond.ni_cnd.cn_pnbuf);
2013 out1:
2014 if (fdirp) {
2015 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, cred, lwp);
2016 vrele(fdirp);
2017 }
2018 if (tdirp) {
2019 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, cred, lwp);
2020 vrele(tdirp);
2021 }
2022 vrele(fromnd.ni_startdir);
2023 PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
2024 nfsm_reply(2 * NFSX_WCCDATA(v3));
2025 if (v3) {
2026 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2027 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2028 }
2029 vn_finished_write(mp, 0);
2030 return (0);
2031
2032 nfsmout:
2033 if (fdirp)
2034 vrele(fdirp);
2035 if (tdirp)
2036 vrele(tdirp);
2037 if (tond.ni_cnd.cn_nameiop) {
2038 vrele(tond.ni_startdir);
2039 PNBUF_PUT(tond.ni_cnd.cn_pnbuf);
2040 }
2041 if (fromnd.ni_cnd.cn_nameiop) {
2042 vrele(fromnd.ni_startdir);
2043 PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
2044 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2045 vrele(fromnd.ni_dvp);
2046 vrele(fvp);
2047 }
2048 return (error);
2049 }
2050
2051 /*
2052 * nfs link service
2053 */
2054 int
2055 nfsrv_link(nfsd, slp, lwp, mrq)
2056 struct nfsrv_descript *nfsd;
2057 struct nfssvc_sock *slp;
2058 struct lwp *lwp;
2059 struct mbuf **mrq;
2060 {
2061 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2062 struct mbuf *nam = nfsd->nd_nam;
2063 caddr_t dpos = nfsd->nd_dpos;
2064 struct ucred *cred = &nfsd->nd_cr;
2065 struct nameidata nd;
2066 u_int32_t *tl;
2067 int32_t t1;
2068 caddr_t bpos;
2069 int error = 0, rdonly, cache, len, dirfor_ret = 1, diraft_ret = 1;
2070 int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
2071 char *cp2;
2072 struct mbuf *mb, *mreq;
2073 struct vnode *vp, *xp, *dirp = (struct vnode *)0;
2074 struct vattr dirfor, diraft, at;
2075 nfsfh_t nfh, dnfh;
2076 fhandle_t *fhp, *dfhp;
2077 u_quad_t frev;
2078 struct mount *mp = NULL;
2079
2080 fhp = &nfh.fh_generic;
2081 dfhp = &dnfh.fh_generic;
2082 nfsm_srvmtofh(fhp);
2083 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL)
2084 return (ESTALE);
2085 vn_start_write(NULL, &mp, V_WAIT);
2086 nfsm_srvmtofh(dfhp);
2087 nfsm_srvnamesiz(len);
2088 error = nfsrv_fhtovp(fhp, FALSE, &vp, cred, slp, nam,
2089 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2090 if (error) {
2091 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2092 nfsm_srvpostop_attr(getret, &at);
2093 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2094 vn_finished_write(mp, 0);
2095 return (0);
2096 }
2097 if (vp->v_type == VDIR && (error = suser(cred, (u_short *)0)) != 0)
2098 goto out1;
2099 nd.ni_cnd.cn_cred = cred;
2100 nd.ni_cnd.cn_nameiop = CREATE;
2101 nd.ni_cnd.cn_flags = LOCKPARENT;
2102 error = nfs_namei(&nd, dfhp, len, slp, nam, &md, &dpos,
2103 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2104 if (dirp) {
2105 if (v3)
2106 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, lwp);
2107 else {
2108 vrele(dirp);
2109 dirp = (struct vnode *)0;
2110 }
2111 }
2112 if (error)
2113 goto out1;
2114 xp = nd.ni_vp;
2115 if (xp != NULL) {
2116 error = EEXIST;
2117 goto out;
2118 }
2119 xp = nd.ni_dvp;
2120 if (vp->v_mount != xp->v_mount)
2121 error = EXDEV;
2122 out:
2123 if (!error) {
2124 nqsrv_getl(vp, ND_WRITE);
2125 nqsrv_getl(xp, ND_WRITE);
2126 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
2127 } else {
2128 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2129 if (nd.ni_dvp == nd.ni_vp)
2130 vrele(nd.ni_dvp);
2131 else
2132 vput(nd.ni_dvp);
2133 if (nd.ni_vp)
2134 vrele(nd.ni_vp);
2135 }
2136 out1:
2137 if (v3)
2138 getret = VOP_GETATTR(vp, &at, cred, lwp);
2139 if (dirp) {
2140 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, lwp);
2141 vrele(dirp);
2142 }
2143 vrele(vp);
2144 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2145 if (v3) {
2146 nfsm_srvpostop_attr(getret, &at);
2147 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2148 vn_finished_write(mp, 0);
2149 return (0);
2150 }
2151 vn_finished_write(mp, 0);
2152 nfsm_srvdone;
2153 }
2154
2155 /*
2156 * nfs symbolic link service
2157 */
2158 int
2159 nfsrv_symlink(nfsd, slp, lwp, mrq)
2160 struct nfsrv_descript *nfsd;
2161 struct nfssvc_sock *slp;
2162 struct lwp *lwp;
2163 struct mbuf **mrq;
2164 {
2165 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2166 struct mbuf *nam = nfsd->nd_nam;
2167 caddr_t dpos = nfsd->nd_dpos;
2168 struct ucred *cred = &nfsd->nd_cr;
2169 struct vattr va, dirfor, diraft;
2170 struct nameidata nd;
2171 u_int32_t *tl;
2172 int32_t t1;
2173 struct nfsv2_sattr *sp;
2174 char *bpos, *pathcp = NULL, *cp2;
2175 struct uio io;
2176 struct iovec iv;
2177 int error = 0, cache, dirfor_ret = 1, diraft_ret = 1;
2178 uint32_t len, len2;
2179 int v3 = (nfsd->nd_flag & ND_NFSV3);
2180 struct mbuf *mb, *mreq;
2181 struct vnode *dirp = (struct vnode *)0;
2182 nfsfh_t nfh;
2183 fhandle_t *fhp;
2184 u_quad_t frev;
2185 struct mount *mp = NULL;
2186
2187 nd.ni_cnd.cn_nameiop = 0;
2188 fhp = &nfh.fh_generic;
2189 nfsm_srvmtofh(fhp);
2190 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL)
2191 return (ESTALE);
2192 vn_start_write(NULL, &mp, V_WAIT);
2193 nfsm_srvnamesiz(len);
2194 nd.ni_cnd.cn_cred = cred;
2195 nd.ni_cnd.cn_nameiop = CREATE;
2196 nd.ni_cnd.cn_flags = LOCKPARENT;
2197 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2198 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2199 if (dirp) {
2200 if (v3)
2201 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, lwp);
2202 else {
2203 vrele(dirp);
2204 dirp = (struct vnode *)0;
2205 }
2206 }
2207 if (error)
2208 goto out;
2209 VATTR_NULL(&va);
2210 va.va_type = VLNK;
2211 if (v3) {
2212 va.va_mode = 0;
2213 nfsm_srvsattr(&va);
2214 nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED);
2215 len2 = fxdr_unsigned(uint32_t, *tl);
2216 if (len2 > PATH_MAX) {
2217 /* XXX should check _PC_NO_TRUNC */
2218 error = ENAMETOOLONG;
2219 goto abortop;
2220 }
2221 }
2222 else {
2223 /* NFSv2 */
2224 nfsm_strsiz(len2, NFS_MAXPATHLEN);
2225 }
2226 pathcp = malloc(len2 + 1, M_TEMP, M_WAITOK);
2227 iv.iov_base = pathcp;
2228 iv.iov_len = len2;
2229 io.uio_resid = len2;
2230 io.uio_offset = 0;
2231 io.uio_iov = &iv;
2232 io.uio_iovcnt = 1;
2233 io.uio_segflg = UIO_SYSSPACE;
2234 io.uio_rw = UIO_READ;
2235 io.uio_lwp = NULL;
2236 nfsm_mtouio(&io, len2);
2237 if (!v3) {
2238 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2239 va.va_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
2240 }
2241 *(pathcp + len2) = '\0';
2242 if (nd.ni_vp) {
2243 error = EEXIST;
2244 abortop:
2245 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2246 if (nd.ni_dvp == nd.ni_vp)
2247 vrele(nd.ni_dvp);
2248 else
2249 vput(nd.ni_dvp);
2250 if (nd.ni_vp)
2251 vrele(nd.ni_vp);
2252 goto out;
2253 }
2254 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2255 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va, pathcp);
2256 if (!error) {
2257 if (v3) {
2258 memset((caddr_t)fhp, 0, sizeof(nfh));
2259 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsidx;
2260 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2261 if (!error)
2262 error = VOP_GETATTR(nd.ni_vp, &va, cred, lwp);
2263 vput(nd.ni_vp);
2264 } else {
2265 vput(nd.ni_vp);
2266 }
2267 }
2268 out:
2269 if (pathcp)
2270 free(pathcp, M_TEMP);
2271 if (dirp) {
2272 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, lwp);
2273 vrele(dirp);
2274 }
2275 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2276 if (v3) {
2277 if (!error) {
2278 nfsm_srvpostop_fh(fhp);
2279 nfsm_srvpostop_attr(0, &va);
2280 }
2281 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2282 }
2283 vn_finished_write(mp, 0);
2284 return (0);
2285 nfsmout:
2286 if (dirp)
2287 vrele(dirp);
2288 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2289 if (nd.ni_dvp == nd.ni_vp)
2290 vrele(nd.ni_dvp);
2291 else
2292 vput(nd.ni_dvp);
2293 if (nd.ni_vp)
2294 vrele(nd.ni_vp);
2295 if (pathcp)
2296 free(pathcp, M_TEMP);
2297 vn_finished_write(mp, 0);
2298 return (error);
2299 }
2300
2301 /*
2302 * nfs mkdir service
2303 */
2304 int
2305 nfsrv_mkdir(nfsd, slp, lwp, mrq)
2306 struct nfsrv_descript *nfsd;
2307 struct nfssvc_sock *slp;
2308 struct lwp *lwp;
2309 struct mbuf **mrq;
2310 {
2311 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2312 struct mbuf *nam = nfsd->nd_nam;
2313 caddr_t dpos = nfsd->nd_dpos;
2314 struct ucred *cred = &nfsd->nd_cr;
2315 struct vattr va, dirfor, diraft;
2316 struct nfs_fattr *fp;
2317 struct nameidata nd;
2318 caddr_t cp;
2319 u_int32_t *tl;
2320 int32_t t1;
2321 caddr_t bpos;
2322 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2323 int v3 = (nfsd->nd_flag & ND_NFSV3);
2324 char *cp2;
2325 struct mbuf *mb, *mreq;
2326 struct vnode *vp, *dirp = (struct vnode *)0;
2327 nfsfh_t nfh;
2328 fhandle_t *fhp;
2329 u_quad_t frev;
2330 struct mount *mp = NULL;
2331
2332 fhp = &nfh.fh_generic;
2333 nfsm_srvmtofh(fhp);
2334 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL)
2335 return (ESTALE);
2336 vn_start_write(NULL, &mp, V_WAIT);
2337 nfsm_srvnamesiz(len);
2338 nd.ni_cnd.cn_cred = cred;
2339 nd.ni_cnd.cn_nameiop = CREATE;
2340 nd.ni_cnd.cn_flags = LOCKPARENT;
2341 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2342 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2343 if (dirp) {
2344 if (v3)
2345 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, lwp);
2346 else {
2347 vrele(dirp);
2348 dirp = (struct vnode *)0;
2349 }
2350 }
2351 if (error) {
2352 nfsm_reply(NFSX_WCCDATA(v3));
2353 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2354 if (dirp)
2355 vrele(dirp);
2356 vn_finished_write(mp, 0);
2357 return (0);
2358 }
2359 VATTR_NULL(&va);
2360 if (v3) {
2361 va.va_mode = 0;
2362 nfsm_srvsattr(&va);
2363 } else {
2364 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2365 va.va_mode = nfstov_mode(*tl++);
2366 }
2367 va.va_type = VDIR;
2368 vp = nd.ni_vp;
2369 if (vp != NULL) {
2370 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2371 if (nd.ni_dvp == vp)
2372 vrele(nd.ni_dvp);
2373 else
2374 vput(nd.ni_dvp);
2375 vrele(vp);
2376 error = EEXIST;
2377 goto out;
2378 }
2379 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2380 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
2381 if (!error) {
2382 vp = nd.ni_vp;
2383 memset((caddr_t)fhp, 0, sizeof(nfh));
2384 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsidx;
2385 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2386 if (!error)
2387 error = VOP_GETATTR(vp, &va, cred, lwp);
2388 vput(vp);
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, 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, 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_segflg = UIO_SYSSPACE;
2658 io.uio_rw = UIO_READ;
2659 io.uio_lwp = NULL;
2660 eofflag = 0;
2661 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2662
2663 error = VOP_READDIR(vp, &io, cred, &eofflag, &cookies, &ncookies);
2664
2665 off = (off_t)io.uio_offset;
2666 if (!cookies && !error)
2667 error = NFSERR_PERM;
2668 if (v3) {
2669 getret = VOP_GETATTR(vp, &at, cred, lwp);
2670 if (!error)
2671 error = getret;
2672 }
2673
2674 VOP_UNLOCK(vp, 0);
2675 if (error) {
2676 vrele(vp);
2677 free((caddr_t)rbuf, M_TEMP);
2678 if (cookies)
2679 free((caddr_t)cookies, M_TEMP);
2680 nfsm_reply(NFSX_POSTOPATTR(v3));
2681 nfsm_srvpostop_attr(getret, &at);
2682 return (0);
2683 }
2684 if (io.uio_resid) {
2685 siz -= io.uio_resid;
2686
2687 /*
2688 * If nothing read, return eof
2689 * rpc reply
2690 */
2691 if (siz == 0) {
2692 vrele(vp);
2693 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
2694 2 * NFSX_UNSIGNED);
2695 if (v3) {
2696 nfsm_srvpostop_attr(getret, &at);
2697 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2698 txdr_hyper(at.va_filerev, tl);
2699 tl += 2;
2700 } else
2701 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2702 *tl++ = nfs_false;
2703 *tl = nfs_true;
2704 free((caddr_t)rbuf, M_TEMP);
2705 free((caddr_t)cookies, M_TEMP);
2706 return (0);
2707 }
2708 }
2709
2710 /*
2711 * Check for degenerate cases of nothing useful read.
2712 * If so go try again
2713 */
2714 cpos = rbuf;
2715 cend = rbuf + siz;
2716 dp = (struct dirent *)cpos;
2717 cookiep = cookies;
2718
2719 while (cpos < cend && ncookies > 0 &&
2720 (dp->d_fileno == 0 || dp->d_type == DT_WHT)) {
2721 cpos += dp->d_reclen;
2722 dp = (struct dirent *)cpos;
2723 cookiep++;
2724 ncookies--;
2725 }
2726 if (cpos >= cend || ncookies == 0) {
2727 toff = off;
2728 siz = fullsiz;
2729 goto again;
2730 }
2731
2732 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
2733 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
2734 if (v3) {
2735 nfsm_srvpostop_attr(getret, &at);
2736 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2737 txdr_hyper(at.va_filerev, tl);
2738 }
2739 mp = mp2 = mb;
2740 bp = bpos;
2741 be = bp + M_TRAILINGSPACE(mp);
2742
2743 /* Loop through the records and build reply */
2744 while (cpos < cend && ncookies > 0) {
2745 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
2746 nlen = dp->d_namlen;
2747 rem = nfsm_rndup(nlen)-nlen;
2748 len += (4 * NFSX_UNSIGNED + nlen + rem);
2749 if (v3)
2750 len += 2 * NFSX_UNSIGNED;
2751 if (len > cnt) {
2752 eofflag = 0;
2753 break;
2754 }
2755 /*
2756 * Build the directory record xdr from
2757 * the dirent entry.
2758 */
2759 nfsm_clget;
2760 *tl = nfs_true;
2761 bp += NFSX_UNSIGNED;
2762 if (v3) {
2763 nfsm_clget;
2764 *tl = txdr_unsigned(dp->d_fileno >> 32);
2765 bp += NFSX_UNSIGNED;
2766 }
2767 nfsm_clget;
2768 *tl = txdr_unsigned(dp->d_fileno);
2769 bp += NFSX_UNSIGNED;
2770 nfsm_clget;
2771 *tl = txdr_unsigned(nlen);
2772 bp += NFSX_UNSIGNED;
2773
2774 /* And loop around copying the name */
2775 xfer = nlen;
2776 cp = dp->d_name;
2777 while (xfer > 0) {
2778 nfsm_clget;
2779 if ((bp+xfer) > be)
2780 tsiz = be-bp;
2781 else
2782 tsiz = xfer;
2783 memcpy(bp, cp, tsiz);
2784 bp += tsiz;
2785 xfer -= tsiz;
2786 if (xfer > 0)
2787 cp += tsiz;
2788 }
2789 /* And null pad to an int32_t boundary */
2790 for (i = 0; i < rem; i++)
2791 *bp++ = '\0';
2792 nfsm_clget;
2793
2794 /* Finish off the record */
2795 txdr_hyper(*cookiep, &jar);
2796 if (v3) {
2797 *tl = jar.nfsuquad[0];
2798 bp += NFSX_UNSIGNED;
2799 nfsm_clget;
2800 }
2801 *tl = jar.nfsuquad[1];
2802 bp += NFSX_UNSIGNED;
2803 }
2804 cpos += dp->d_reclen;
2805 dp = (struct dirent *)cpos;
2806 cookiep++;
2807 ncookies--;
2808 }
2809 vrele(vp);
2810 nfsm_clget;
2811 *tl = nfs_false;
2812 bp += NFSX_UNSIGNED;
2813 nfsm_clget;
2814 if (eofflag)
2815 *tl = nfs_true;
2816 else
2817 *tl = nfs_false;
2818 bp += NFSX_UNSIGNED;
2819 if (mp != mb) {
2820 if (bp < be)
2821 mp->m_len = bp - mtod(mp, caddr_t);
2822 } else
2823 mp->m_len += bp - bpos;
2824 free((caddr_t)rbuf, M_TEMP);
2825 free((caddr_t)cookies, M_TEMP);
2826 nfsm_srvdone;
2827 }
2828
2829 int
2830 nfsrv_readdirplus(nfsd, slp, lwp, mrq)
2831 struct nfsrv_descript *nfsd;
2832 struct nfssvc_sock *slp;
2833 struct lwp *lwp;
2834 struct mbuf **mrq;
2835 {
2836 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2837 struct mbuf *nam = nfsd->nd_nam;
2838 caddr_t dpos = nfsd->nd_dpos;
2839 struct ucred *cred = &nfsd->nd_cr;
2840 char *bp, *be;
2841 struct mbuf *mp;
2842 struct dirent *dp;
2843 caddr_t cp;
2844 u_int32_t *tl;
2845 int32_t t1;
2846 caddr_t bpos;
2847 struct mbuf *mb, *mreq, *mp2;
2848 char *cpos, *cend, *cp2, *rbuf;
2849 struct vnode *vp, *nvp;
2850 struct flrep fl;
2851 nfsfh_t nfh;
2852 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
2853 struct uio io;
2854 struct iovec iv;
2855 struct vattr va, at, *vap = &va;
2856 struct nfs_fattr *fp;
2857 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2858 int siz, cnt, fullsiz, eofflag, rdonly, cache, dirlen, ncookies;
2859 u_quad_t frev, off, toff, verf;
2860 off_t *cookies = NULL, *cookiep;
2861
2862 fhp = &nfh.fh_generic;
2863 nfsm_srvmtofh(fhp);
2864 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2865 toff = fxdr_hyper(tl);
2866 tl += 2;
2867 verf = fxdr_hyper(tl);
2868 tl += 2;
2869 siz = fxdr_unsigned(int, *tl++);
2870 cnt = fxdr_unsigned(int, *tl);
2871 off = toff;
2872 siz = ((siz + NFS_SRVDIRBLKSIZ - 1) & ~(NFS_SRVDIRBLKSIZ - 1));
2873 xfer = NFS_SRVMAXDATA(nfsd);
2874 if (siz > xfer)
2875 siz = xfer;
2876 fullsiz = siz;
2877 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
2878 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2879 if (!error && vp->v_type != VDIR) {
2880 error = ENOTDIR;
2881 vput(vp);
2882 }
2883 if (error) {
2884 nfsm_reply(NFSX_UNSIGNED);
2885 nfsm_srvpostop_attr(getret, &at);
2886 return (0);
2887 }
2888 error = getret = VOP_GETATTR(vp, &at, cred, lwp);
2889 #ifdef NFS3_STRICTVERF
2890 /*
2891 * XXX This check is too strict for Solaris 2.5 clients.
2892 */
2893 if (!error && toff && verf != at.va_filerev)
2894 error = NFSERR_BAD_COOKIE;
2895 #endif
2896 if (!error) {
2897 nqsrv_getl(vp, ND_READ);
2898 error = nfsrv_access(vp, VEXEC, cred, rdonly, lwp, 0);
2899 }
2900 if (error) {
2901 vput(vp);
2902 nfsm_reply(NFSX_V3POSTOPATTR);
2903 nfsm_srvpostop_attr(getret, &at);
2904 return (0);
2905 }
2906 VOP_UNLOCK(vp, 0);
2907
2908 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2909 again:
2910 iv.iov_base = rbuf;
2911 iv.iov_len = fullsiz;
2912 io.uio_iov = &iv;
2913 io.uio_iovcnt = 1;
2914 io.uio_offset = (off_t)off;
2915 io.uio_resid = fullsiz;
2916 io.uio_segflg = UIO_SYSSPACE;
2917 io.uio_rw = UIO_READ;
2918 io.uio_lwp = NULL;
2919 eofflag = 0;
2920
2921 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2922
2923 error = VOP_READDIR(vp, &io, cred, &eofflag, &cookies, &ncookies);
2924
2925 off = (u_quad_t)io.uio_offset;
2926 getret = VOP_GETATTR(vp, &at, cred, lwp);
2927
2928 VOP_UNLOCK(vp, 0);
2929
2930 /*
2931 * If the VGET operation doesn't work for this filesystem,
2932 * we can't support readdirplus. Returning NOTSUPP should
2933 * make clients fall back to plain readdir.
2934 * There's no need to check for VPTOFH as well, we wouldn't
2935 * even be here otherwise.
2936 */
2937 if (!getret) {
2938 if ((getret = VFS_VGET(vp->v_mount, at.va_fileid, &nvp)))
2939 getret = (getret == EOPNOTSUPP) ?
2940 NFSERR_NOTSUPP : NFSERR_IO;
2941 else
2942 vput(nvp);
2943 }
2944
2945 if (!cookies && !error)
2946 error = NFSERR_PERM;
2947 if (!error)
2948 error = getret;
2949 if (error) {
2950 vrele(vp);
2951 if (cookies)
2952 free((caddr_t)cookies, M_TEMP);
2953 free((caddr_t)rbuf, M_TEMP);
2954 nfsm_reply(NFSX_V3POSTOPATTR);
2955 nfsm_srvpostop_attr(getret, &at);
2956 return (0);
2957 }
2958 if (io.uio_resid) {
2959 siz -= io.uio_resid;
2960
2961 /*
2962 * If nothing read, return eof
2963 * rpc reply
2964 */
2965 if (siz == 0) {
2966 vrele(vp);
2967 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
2968 2 * NFSX_UNSIGNED);
2969 nfsm_srvpostop_attr(getret, &at);
2970 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2971 txdr_hyper(at.va_filerev, tl);
2972 tl += 2;
2973 *tl++ = nfs_false;
2974 *tl = nfs_true;
2975 free((caddr_t)cookies, M_TEMP);
2976 free((caddr_t)rbuf, M_TEMP);
2977 return (0);
2978 }
2979 }
2980
2981 /*
2982 * Check for degenerate cases of nothing useful read.
2983 * If so go try again
2984 */
2985 cpos = rbuf;
2986 cend = rbuf + siz;
2987 dp = (struct dirent *)cpos;
2988 cookiep = cookies;
2989
2990 while (cpos < cend && ncookies > 0 &&
2991 (dp->d_fileno == 0 || dp->d_type == DT_WHT)) {
2992 cpos += dp->d_reclen;
2993 dp = (struct dirent *)cpos;
2994 cookiep++;
2995 ncookies--;
2996 }
2997 if (cpos >= cend || ncookies == 0) {
2998 toff = off;
2999 siz = fullsiz;
3000 goto again;
3001 }
3002
3003 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2 * NFSX_UNSIGNED;
3004 nfsm_reply(cnt);
3005 nfsm_srvpostop_attr(getret, &at);
3006 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3007 txdr_hyper(at.va_filerev, tl);
3008 mp = mp2 = mb;
3009 bp = bpos;
3010 be = bp + M_TRAILINGSPACE(mp);
3011
3012 /* Loop through the records and build reply */
3013 while (cpos < cend && ncookies > 0) {
3014 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3015 nlen = dp->d_namlen;
3016 rem = nfsm_rndup(nlen)-nlen;
3017
3018 /*
3019 * For readdir_and_lookup get the vnode using
3020 * the file number.
3021 */
3022 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
3023 goto invalid;
3024 memset((caddr_t)nfhp, 0, NFSX_V3FH);
3025 nfhp->fh_fsid =
3026 nvp->v_mount->mnt_stat.f_fsidx;
3027 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3028 vput(nvp);
3029 goto invalid;
3030 }
3031 if (VOP_GETATTR(nvp, vap, cred, lwp)) {
3032 vput(nvp);
3033 goto invalid;
3034 }
3035 vput(nvp);
3036
3037 /*
3038 * If either the dircount or maxcount will be
3039 * exceeded, get out now. Both of these lengths
3040 * are calculated conservatively, including all
3041 * XDR overheads.
3042 */
3043 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3044 NFSX_V3POSTOPATTR);
3045 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3046 if (len > cnt || dirlen > fullsiz) {
3047 eofflag = 0;
3048 break;
3049 }
3050
3051 /*
3052 * Build the directory record xdr from
3053 * the dirent entry.
3054 */
3055 fp = (struct nfs_fattr *)&fl.fl_fattr;
3056 nfsm_srvfillattr(vap, fp);
3057 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3058 fl.fl_fhok = nfs_true;
3059 fl.fl_postopok = nfs_true;
3060 txdr_hyper(*cookiep, fl.fl_off.nfsuquad);
3061
3062 nfsm_clget;
3063 *tl = nfs_true;
3064 bp += NFSX_UNSIGNED;
3065 nfsm_clget;
3066 *tl = txdr_unsigned(dp->d_fileno >> 32);
3067 bp += NFSX_UNSIGNED;
3068 nfsm_clget;
3069 *tl = txdr_unsigned(dp->d_fileno);
3070 bp += NFSX_UNSIGNED;
3071 nfsm_clget;
3072 *tl = txdr_unsigned(nlen);
3073 bp += NFSX_UNSIGNED;
3074
3075 /* And loop around copying the name */
3076 xfer = nlen;
3077 cp = dp->d_name;
3078 while (xfer > 0) {
3079 nfsm_clget;
3080 if ((bp + xfer) > be)
3081 tsiz = be - bp;
3082 else
3083 tsiz = xfer;
3084 memcpy(bp, cp, tsiz);
3085 bp += tsiz;
3086 xfer -= tsiz;
3087 if (xfer > 0)
3088 cp += tsiz;
3089 }
3090 /* And null pad to an int32_t boundary */
3091 for (i = 0; i < rem; i++)
3092 *bp++ = '\0';
3093
3094 /*
3095 * Now copy the flrep structure out.
3096 */
3097 xfer = sizeof (struct flrep);
3098 cp = (caddr_t)&fl;
3099 while (xfer > 0) {
3100 nfsm_clget;
3101 if ((bp + xfer) > be)
3102 tsiz = be - bp;
3103 else
3104 tsiz = xfer;
3105 memcpy(bp, cp, tsiz);
3106 bp += tsiz;
3107 xfer -= tsiz;
3108 if (xfer > 0)
3109 cp += tsiz;
3110 }
3111 }
3112 invalid:
3113 cpos += dp->d_reclen;
3114 dp = (struct dirent *)cpos;
3115 cookiep++;
3116 ncookies--;
3117 }
3118 vrele(vp);
3119 nfsm_clget;
3120 *tl = nfs_false;
3121 bp += NFSX_UNSIGNED;
3122 nfsm_clget;
3123 if (eofflag)
3124 *tl = nfs_true;
3125 else
3126 *tl = nfs_false;
3127 bp += NFSX_UNSIGNED;
3128 if (mp != mb) {
3129 if (bp < be)
3130 mp->m_len = bp - mtod(mp, caddr_t);
3131 } else
3132 mp->m_len += bp - bpos;
3133 free((caddr_t)cookies, M_TEMP);
3134 free((caddr_t)rbuf, M_TEMP);
3135 nfsm_srvdone;
3136 }
3137
3138 /*
3139 * nfs commit service
3140 */
3141 int
3142 nfsrv_commit(nfsd, slp, lwp, mrq)
3143 struct nfsrv_descript *nfsd;
3144 struct nfssvc_sock *slp;
3145 struct lwp *lwp;
3146 struct mbuf **mrq;
3147 {
3148 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3149 struct mbuf *nam = nfsd->nd_nam;
3150 caddr_t dpos = nfsd->nd_dpos;
3151 struct ucred *cred = &nfsd->nd_cr;
3152 struct vattr bfor, aft;
3153 struct vnode *vp;
3154 nfsfh_t nfh;
3155 fhandle_t *fhp;
3156 u_int32_t *tl;
3157 int32_t t1;
3158 caddr_t bpos;
3159 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cache;
3160 uint32_t cnt;
3161 char *cp2;
3162 struct mbuf *mb, *mreq;
3163 u_quad_t frev, off, end;
3164 struct mount *mp = NULL;
3165
3166 #ifndef nolint
3167 cache = 0;
3168 #endif
3169 fhp = &nfh.fh_generic;
3170 nfsm_srvmtofh(fhp);
3171 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL)
3172 return (ESTALE);
3173 vn_start_write(NULL, &mp, V_WAIT);
3174 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3175
3176 off = fxdr_hyper(tl);
3177 tl += 2;
3178 cnt = fxdr_unsigned(uint32_t, *tl);
3179 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3180 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
3181 if (error) {
3182 nfsm_reply(2 * NFSX_UNSIGNED);
3183 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3184 vn_finished_write(mp, 0);
3185 return (0);
3186 }
3187 for_ret = VOP_GETATTR(vp, &bfor, cred, lwp);
3188 end = (cnt > 0) ? off + cnt : vp->v_size;
3189 if (end < off || end > vp->v_size)
3190 end = vp->v_size;
3191 if (off < vp->v_size)
3192 error = VOP_FSYNC(vp, cred, FSYNC_WAIT, off, end, lwp);
3193 /* else error == 0, from nfsrv_fhtovp() */
3194 aft_ret = VOP_GETATTR(vp, &aft, cred, lwp);
3195 vput(vp);
3196 nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3197 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3198 if (!error) {
3199 nfsm_build(tl, u_int32_t *, NFSX_V3WRITEVERF);
3200 *tl++ = txdr_unsigned(boottime.tv_sec);
3201 *tl = txdr_unsigned(boottime.tv_usec);
3202 } else {
3203 vn_finished_write(mp, 0);
3204 return (0);
3205 }
3206 vn_finished_write(mp, 0);
3207 nfsm_srvdone;
3208 }
3209
3210 /*
3211 * nfs statfs service
3212 */
3213 int
3214 nfsrv_statfs(nfsd, slp, lwp, mrq)
3215 struct nfsrv_descript *nfsd;
3216 struct nfssvc_sock *slp;
3217 struct lwp *lwp;
3218 struct mbuf **mrq;
3219 {
3220 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3221 struct mbuf *nam = nfsd->nd_nam;
3222 caddr_t dpos = nfsd->nd_dpos;
3223 struct ucred *cred = &nfsd->nd_cr;
3224 struct statvfs *sf;
3225 struct nfs_statfs *sfp;
3226 u_int32_t *tl;
3227 int32_t t1;
3228 caddr_t bpos;
3229 int error = 0, rdonly, cache, getret = 1;
3230 int v3 = (nfsd->nd_flag & ND_NFSV3);
3231 char *cp2;
3232 struct mbuf *mb, *mreq;
3233 struct vnode *vp;
3234 struct vattr at;
3235 nfsfh_t nfh;
3236 fhandle_t *fhp;
3237 struct statvfs statvfs;
3238 u_quad_t frev, tval;
3239
3240 #ifndef nolint
3241 cache = 0;
3242 #endif
3243 fhp = &nfh.fh_generic;
3244 nfsm_srvmtofh(fhp);
3245 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3246 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
3247 if (error) {
3248 nfsm_reply(NFSX_UNSIGNED);
3249 nfsm_srvpostop_attr(getret, &at);
3250 return (0);
3251 }
3252 sf = &statvfs;
3253 error = VFS_STATVFS(vp->v_mount, sf, lwp);
3254 getret = VOP_GETATTR(vp, &at, cred, lwp);
3255 vput(vp);
3256 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3257 if (v3)
3258 nfsm_srvpostop_attr(getret, &at);
3259 if (error)
3260 return (0);
3261 nfsm_build(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
3262 if (v3) {
3263 tval = (u_quad_t)((quad_t)sf->f_blocks * (quad_t)sf->f_frsize);
3264 txdr_hyper(tval, &sfp->sf_tbytes);
3265 tval = (u_quad_t)((quad_t)sf->f_bfree * (quad_t)sf->f_frsize);
3266 txdr_hyper(tval, &sfp->sf_fbytes);
3267 tval = (u_quad_t)((quad_t)sf->f_bavail * (quad_t)sf->f_frsize);
3268 txdr_hyper(tval, &sfp->sf_abytes);
3269 tval = (u_quad_t)sf->f_files;
3270 txdr_hyper(tval, &sfp->sf_tfiles);
3271 tval = (u_quad_t)sf->f_ffree;
3272 txdr_hyper(tval, &sfp->sf_ffiles);
3273 txdr_hyper(tval, &sfp->sf_afiles);
3274 sfp->sf_invarsec = 0;
3275 } else {
3276 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3277 sfp->sf_bsize = txdr_unsigned(sf->f_frsize);
3278 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3279 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3280 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3281 }
3282 nfsm_srvdone;
3283 }
3284
3285 /*
3286 * nfs fsinfo service
3287 */
3288 int
3289 nfsrv_fsinfo(nfsd, slp, lwp, mrq)
3290 struct nfsrv_descript *nfsd;
3291 struct nfssvc_sock *slp;
3292 struct lwp *lwp;
3293 struct mbuf **mrq;
3294 {
3295 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3296 struct mbuf *nam = nfsd->nd_nam;
3297 caddr_t dpos = nfsd->nd_dpos;
3298 struct ucred *cred = &nfsd->nd_cr;
3299 u_int32_t *tl;
3300 struct nfsv3_fsinfo *sip;
3301 int32_t t1;
3302 caddr_t bpos;
3303 int error = 0, rdonly, cache, getret = 1;
3304 uint32_t maxdata;
3305 char *cp2;
3306 struct mbuf *mb, *mreq;
3307 struct vnode *vp;
3308 struct vattr at;
3309 nfsfh_t nfh;
3310 fhandle_t *fhp;
3311 u_quad_t frev, maxfsize;
3312 struct statvfs sb;
3313
3314 #ifndef nolint
3315 cache = 0;
3316 #endif
3317 fhp = &nfh.fh_generic;
3318 nfsm_srvmtofh(fhp);
3319 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3320 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
3321 if (error) {
3322 nfsm_reply(NFSX_UNSIGNED);
3323 nfsm_srvpostop_attr(getret, &at);
3324 return (0);
3325 }
3326
3327 /* XXX Try to make a guess on the max file size. */
3328 VFS_STATVFS(vp->v_mount, &sb, (struct lwp *)0);
3329 maxfsize = (u_quad_t)0x80000000 * sb.f_frsize - 1;
3330
3331 getret = VOP_GETATTR(vp, &at, cred, lwp);
3332 vput(vp);
3333 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3334 nfsm_srvpostop_attr(getret, &at);
3335 nfsm_build(sip, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3336
3337 /*
3338 * XXX
3339 * There should be file system VFS OP(s) to get this information.
3340 * For now, assume ufs.
3341 */
3342 if (slp->ns_so->so_type == SOCK_DGRAM)
3343 maxdata = NFS_MAXDGRAMDATA;
3344 else
3345 maxdata = NFS_MAXDATA;
3346 sip->fs_rtmax = txdr_unsigned(maxdata);
3347 sip->fs_rtpref = txdr_unsigned(maxdata);
3348 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3349 sip->fs_wtmax = txdr_unsigned(maxdata);
3350 sip->fs_wtpref = txdr_unsigned(maxdata);
3351 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3352 sip->fs_dtpref = txdr_unsigned(maxdata);
3353 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3354 sip->fs_timedelta.nfsv3_sec = 0;
3355 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3356 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3357 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3358 NFSV3FSINFO_CANSETTIME);
3359 nfsm_srvdone;
3360 }
3361
3362 /*
3363 * nfs pathconf service
3364 */
3365 int
3366 nfsrv_pathconf(nfsd, slp, lwp, mrq)
3367 struct nfsrv_descript *nfsd;
3368 struct nfssvc_sock *slp;
3369 struct lwp *lwp;
3370 struct mbuf **mrq;
3371 {
3372 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3373 struct mbuf *nam = nfsd->nd_nam;
3374 caddr_t dpos = nfsd->nd_dpos;
3375 struct ucred *cred = &nfsd->nd_cr;
3376 u_int32_t *tl;
3377 struct nfsv3_pathconf *pc;
3378 int32_t t1;
3379 caddr_t bpos;
3380 int error = 0, rdonly, cache, getret = 1;
3381 register_t linkmax, namemax, chownres, notrunc;
3382 char *cp2;
3383 struct mbuf *mb, *mreq;
3384 struct vnode *vp;
3385 struct vattr at;
3386 nfsfh_t nfh;
3387 fhandle_t *fhp;
3388 u_quad_t frev;
3389
3390 #ifndef nolint
3391 cache = 0;
3392 #endif
3393 fhp = &nfh.fh_generic;
3394 nfsm_srvmtofh(fhp);
3395 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3396 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
3397 if (error) {
3398 nfsm_reply(NFSX_UNSIGNED);
3399 nfsm_srvpostop_attr(getret, &at);
3400 return (0);
3401 }
3402 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3403 if (!error)
3404 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3405 if (!error)
3406 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3407 if (!error)
3408 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3409 getret = VOP_GETATTR(vp, &at, cred, lwp);
3410 vput(vp);
3411 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3412 nfsm_srvpostop_attr(getret, &at);
3413 if (error)
3414 return (0);
3415 nfsm_build(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3416
3417 pc->pc_linkmax = txdr_unsigned(linkmax);
3418 pc->pc_namemax = txdr_unsigned(namemax);
3419 pc->pc_notrunc = txdr_unsigned(notrunc);
3420 pc->pc_chownrestricted = txdr_unsigned(chownres);
3421
3422 /*
3423 * These should probably be supported by VOP_PATHCONF(), but
3424 * until msdosfs is exportable (why would you want to?), the
3425 * Unix defaults should be ok.
3426 */
3427 pc->pc_caseinsensitive = nfs_false;
3428 pc->pc_casepreserving = nfs_true;
3429 nfsm_srvdone;
3430 }
3431
3432 /*
3433 * Null operation, used by clients to ping server
3434 */
3435 /* ARGSUSED */
3436 int
3437 nfsrv_null(nfsd, slp, lwp, mrq)
3438 struct nfsrv_descript *nfsd;
3439 struct nfssvc_sock *slp;
3440 struct lwp *lwp;
3441 struct mbuf **mrq;
3442 {
3443 struct mbuf *mrep = nfsd->nd_mrep;
3444 caddr_t bpos;
3445 int error = NFSERR_RETVOID, cache = 0;
3446 struct mbuf *mb, *mreq;
3447 u_quad_t frev;
3448
3449 nfsm_reply(0);
3450 return (0);
3451 }
3452
3453 /*
3454 * No operation, used for obsolete procedures
3455 */
3456 /* ARGSUSED */
3457 int
3458 nfsrv_noop(nfsd, slp, lwp, mrq)
3459 struct nfsrv_descript *nfsd;
3460 struct nfssvc_sock *slp;
3461 struct lwp *lwp;
3462 struct mbuf **mrq;
3463 {
3464 struct mbuf *mrep = nfsd->nd_mrep;
3465 caddr_t bpos;
3466 int error, cache = 0;
3467 struct mbuf *mb, *mreq;
3468 u_quad_t frev;
3469
3470 if (nfsd->nd_repstat)
3471 error = nfsd->nd_repstat;
3472 else
3473 error = EPROCUNAVAIL;
3474 nfsm_reply(0);
3475 return (0);
3476 }
3477
3478 /*
3479 * Perform access checking for vnodes obtained from file handles that would
3480 * refer to files already opened by a Unix client. You cannot just use
3481 * vn_writechk() and VOP_ACCESS() for two reasons.
3482 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
3483 * 2 - The owner is to be given access irrespective of mode bits for some
3484 * operations, so that processes that chmod after opening a file don't
3485 * break. I don't like this because it opens a security hole, but since
3486 * the nfs server opens a security hole the size of a barn door anyhow,
3487 * what the heck.
3488 *
3489 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
3490 * will return EPERM instead of EACCESS. EPERM is always an error.
3491 */
3492 int
3493 nfsrv_access(vp, flags, cred, rdonly, lwp, override)
3494 struct vnode *vp;
3495 int flags;
3496 struct ucred *cred;
3497 int rdonly;
3498 struct lwp *lwp;
3499 {
3500 struct vattr vattr;
3501 int error;
3502 if (flags & VWRITE) {
3503 /* Just vn_writechk() changed to check rdonly */
3504 /*
3505 * Disallow write attempts on read-only file systems;
3506 * unless the file is a socket or a block or character
3507 * device resident on the file system.
3508 */
3509 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3510 switch (vp->v_type) {
3511 case VREG:
3512 case VDIR:
3513 case VLNK:
3514 return (EROFS);
3515 default:
3516 break;
3517 }
3518 }
3519
3520 /*
3521 * If the vnode is in use as a process's text,
3522 * we can't allow writing.
3523 */
3524 if (vp->v_flag & VTEXT)
3525 return (ETXTBSY);
3526 }
3527 error = VOP_GETATTR(vp, &vattr, cred, lwp);
3528 if (error)
3529 return (error);
3530 error = VOP_ACCESS(vp, flags, cred, lwp);
3531 /*
3532 * Allow certain operations for the owner (reads and writes
3533 * on files that are already open).
3534 */
3535 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)
3536 error = 0;
3537 return error;
3538 }
3539