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