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