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