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