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