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