nfs_serv.c revision 1.117 1 /* $NetBSD: nfs_serv.c,v 1.117 2006/09/02 12:40:36 yamt Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)nfs_serv.c 8.8 (Berkeley) 7/31/95
35 */
36
37 /*
38 * nfs version 2 and 3 server calls to vnode ops
39 * - these routines generally have 3 phases
40 * 1 - break down and validate rpc request in mbuf list
41 * 2 - do the vnode ops for the request
42 * (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c)
43 * 3 - build the rpc reply in an mbuf list
44 * nb:
45 * - do not mix the phases, since the nfsm_?? macros can return failures
46 * on a bad rpc or similar and do not do any vrele() or vput()'s
47 *
48 * - the nfsm_reply() macro generates an nfs rpc reply with the nfs
49 * error number iff error != 0 whereas
50 * returning an error from the server function implies a fatal error
51 * such as a badly constructed rpc request that should be dropped without
52 * a reply.
53 * For Version 3, nfsm_reply() does not return for the error case, since
54 * most version 3 rpcs return more than the status for error cases.
55 */
56
57 #include <sys/cdefs.h>
58 __KERNEL_RCSID(0, "$NetBSD: nfs_serv.c,v 1.117 2006/09/02 12:40:36 yamt Exp $");
59
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/proc.h>
63 #include <sys/file.h>
64 #include <sys/namei.h>
65 #include <sys/vnode.h>
66 #include <sys/mount.h>
67 #include <sys/socket.h>
68 #include <sys/socketvar.h>
69 #include <sys/mbuf.h>
70 #include <sys/dirent.h>
71 #include <sys/stat.h>
72 #include <sys/kernel.h>
73 #include <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 vrele(ndp->ni_startdir);
440 PNBUF_PUT(nd.ni_cnd.cn_pnbuf);
441 vp = ndp->ni_vp;
442 error = nfsrv_composefh(vp, &nsfh, v3);
443 if (!error)
444 error = VOP_GETATTR(vp, &va, cred, lwp);
445 vput(vp);
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) {
1426 if (v3)
1427 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, lwp);
1428 else {
1429 vrele(dirp);
1430 dirp = (struct vnode *)0;
1431 }
1432 }
1433 if (error) {
1434 nfsm_reply(NFSX_WCCDATA(v3));
1435 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1436 if (dirp)
1437 vrele(dirp);
1438 vn_finished_write(mp, 0);
1439 return (0);
1440 }
1441 VATTR_NULL(&va);
1442 if (v3) {
1443 va.va_mode = 0;
1444 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1445 how = fxdr_unsigned(int, *tl);
1446 switch (how) {
1447 case NFSV3CREATE_GUARDED:
1448 if (nd.ni_vp) {
1449 error = EEXIST;
1450 break;
1451 }
1452 case NFSV3CREATE_UNCHECKED:
1453 nfsm_srvsattr(&va);
1454 break;
1455 case NFSV3CREATE_EXCLUSIVE:
1456 nfsm_dissect(cp, caddr_t, NFSX_V3CREATEVERF);
1457 memcpy(cverf, cp, NFSX_V3CREATEVERF);
1458 exclusive_flag = 1;
1459 break;
1460 };
1461 va.va_type = VREG;
1462 } else {
1463 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1464 va.va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1465 if (va.va_type == VNON)
1466 va.va_type = VREG;
1467 va.va_mode = nfstov_mode(sp->sa_mode);
1468 switch (va.va_type) {
1469 case VREG:
1470 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1471 if (tsize != -1)
1472 va.va_size = (u_quad_t)tsize;
1473 break;
1474 case VCHR:
1475 case VBLK:
1476 case VFIFO:
1477 rdev = fxdr_unsigned(int32_t, sp->sa_size);
1478 break;
1479 default:
1480 break;
1481 };
1482 }
1483
1484 /*
1485 * Iff doesn't exist, create it
1486 * otherwise just truncate to 0 length
1487 * should I set the mode too ??
1488 */
1489 if (nd.ni_vp == NULL) {
1490 if (va.va_type == VREG || va.va_type == VSOCK) {
1491 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1492 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
1493 if (!error) {
1494 if (exclusive_flag) {
1495 exclusive_flag = 0;
1496 VATTR_NULL(&va);
1497 /*
1498 * XXX
1499 * assuming NFSX_V3CREATEVERF
1500 * == sizeof(nfstime3)
1501 */
1502 fxdr_nfsv3time(cverf, &va.va_atime);
1503 error = VOP_SETATTR(nd.ni_vp, &va, cred,
1504 lwp);
1505 }
1506 }
1507 } else if (va.va_type == VCHR || va.va_type == VBLK ||
1508 va.va_type == VFIFO) {
1509 if (va.va_type == VCHR && rdev == 0xffffffff)
1510 va.va_type = VFIFO;
1511 if (va.va_type != VFIFO &&
1512 (error = kauth_authorize_generic(cred,
1513 KAUTH_GENERIC_ISSUSER, (u_short *)0))) {
1514 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1515 vput(nd.ni_dvp);
1516 nfsm_reply(0);
1517 vn_finished_write(mp, 0);
1518 return (error);
1519 } else
1520 va.va_rdev = (dev_t)rdev;
1521 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1522 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd,
1523 &va);
1524 if (error) {
1525 nfsm_reply(0);
1526 }
1527 if (nd.ni_cnd.cn_flags & ISSYMLINK) {
1528 vrele(nd.ni_dvp);
1529 vput(nd.ni_vp);
1530 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1531 error = EINVAL;
1532 nfsm_reply(0);
1533 }
1534 } else {
1535 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1536 vput(nd.ni_dvp);
1537 error = ENXIO;
1538 }
1539 vp = nd.ni_vp;
1540 } else {
1541 vp = nd.ni_vp;
1542 if (nd.ni_dvp == vp)
1543 vrele(nd.ni_dvp);
1544 else
1545 vput(nd.ni_dvp);
1546 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1547 if (!error && va.va_size != -1) {
1548 error = nfsrv_access(vp, VWRITE, cred,
1549 (nd.ni_cnd.cn_flags & RDONLY), lwp, 0);
1550 if (!error) {
1551 nqsrv_getl(vp, ND_WRITE);
1552 tempsize = va.va_size;
1553 VATTR_NULL(&va);
1554 va.va_size = tempsize;
1555 error = VOP_SETATTR(vp, &va, cred, lwp);
1556 }
1557 }
1558 if (error)
1559 vput(vp);
1560 }
1561 if (!error) {
1562 error = nfsrv_composefh(vp, &nsfh, v3);
1563 if (!error)
1564 error = VOP_GETATTR(vp, &va, cred, lwp);
1565 vput(vp);
1566 }
1567 if (v3) {
1568 if (exclusive_flag && !error) {
1569 /*
1570 * XXX assuming NFSX_V3CREATEVERF == sizeof(nfstime3)
1571 */
1572 char oldverf[NFSX_V3CREATEVERF];
1573
1574 txdr_nfsv3time(&va.va_atime, oldverf);
1575 if (memcmp(cverf, oldverf, NFSX_V3CREATEVERF))
1576 error = EEXIST;
1577 }
1578 if (dirp) {
1579 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, lwp);
1580 vrele(dirp);
1581 }
1582 }
1583 nfsm_reply(NFSX_SRVFH(&nsfh, v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1584 if (v3) {
1585 if (!error) {
1586 nfsm_srvpostop_fh(&nsfh);
1587 nfsm_srvpostop_attr(0, &va);
1588 }
1589 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1590 } else {
1591 nfsm_srvfhtom(&nsfh, v3);
1592 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1593 nfsm_srvfillattr(&va, fp);
1594 }
1595 vn_finished_write(mp, 0);
1596 return (0);
1597 nfsmout:
1598 if (dirp)
1599 vrele(dirp);
1600 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1601 if (nd.ni_dvp == nd.ni_vp)
1602 vrele(nd.ni_dvp);
1603 else
1604 vput(nd.ni_dvp);
1605 if (nd.ni_vp)
1606 vput(nd.ni_vp);
1607 vn_finished_write(mp, 0);
1608 return (error);
1609 }
1610
1611 /*
1612 * nfs v3 mknod service
1613 */
1614 int
1615 nfsrv_mknod(nfsd, slp, lwp, mrq)
1616 struct nfsrv_descript *nfsd;
1617 struct nfssvc_sock *slp;
1618 struct lwp *lwp;
1619 struct mbuf **mrq;
1620 {
1621 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1622 struct mbuf *nam = nfsd->nd_nam;
1623 caddr_t dpos = nfsd->nd_dpos;
1624 kauth_cred_t cred = nfsd->nd_cr;
1625 struct vattr va, dirfor, diraft;
1626 u_int32_t *tl;
1627 struct nameidata nd;
1628 int32_t t1;
1629 caddr_t bpos;
1630 int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
1631 u_int32_t major, minor;
1632 enum vtype vtyp;
1633 char *cp2;
1634 struct mbuf *mb, *mreq;
1635 struct vnode *vp, *dirp = (struct vnode *)0;
1636 nfsrvfh_t nsfh;
1637 u_quad_t frev;
1638 struct mount *mp = NULL;
1639
1640 nd.ni_cnd.cn_nameiop = 0;
1641 nfsm_srvmtofh(&nsfh);
1642 if ((mp = vfs_getvfs(&NFSRVFH_FHANDLE(&nsfh)->fh_fsid)) == NULL)
1643 return (ESTALE);
1644 vn_start_write(NULL, &mp, V_WAIT);
1645 nfsm_srvnamesiz(len);
1646 nd.ni_cnd.cn_cred = cred;
1647 nd.ni_cnd.cn_nameiop = CREATE;
1648 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
1649 error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
1650 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1651 if (dirp)
1652 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, lwp);
1653 if (error) {
1654 nfsm_reply(NFSX_WCCDATA(1));
1655 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1656 if (dirp)
1657 vrele(dirp);
1658 vn_finished_write(mp, 0);
1659 return (0);
1660 }
1661 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1662 vtyp = nfsv3tov_type(*tl);
1663 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1664 error = NFSERR_BADTYPE;
1665 goto abort;
1666 }
1667 VATTR_NULL(&va);
1668 va.va_mode = 0;
1669 nfsm_srvsattr(&va);
1670 if (vtyp == VCHR || vtyp == VBLK) {
1671 dev_t rdev;
1672
1673 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1674 major = fxdr_unsigned(u_int32_t, *tl++);
1675 minor = fxdr_unsigned(u_int32_t, *tl);
1676 rdev = makedev(major, minor);
1677 if (major(rdev) != major || minor(rdev) != minor) {
1678 error = EINVAL;
1679 goto abort;
1680 }
1681 va.va_rdev = rdev;
1682 }
1683
1684 /*
1685 * Iff doesn't exist, create it.
1686 */
1687 if (nd.ni_vp) {
1688 error = EEXIST;
1689 abort:
1690 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1691 if (nd.ni_dvp == nd.ni_vp)
1692 vrele(nd.ni_dvp);
1693 else
1694 vput(nd.ni_dvp);
1695 if (nd.ni_vp)
1696 vput(nd.ni_vp);
1697 goto out;
1698 }
1699 va.va_type = vtyp;
1700 if (vtyp == VSOCK) {
1701 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1702 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
1703 } else {
1704 if (va.va_type != VFIFO &&
1705 (error = kauth_authorize_generic(cred,
1706 KAUTH_GENERIC_ISSUSER, (u_short *)0))) {
1707 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1708 vput(nd.ni_dvp);
1709 goto out;
1710 }
1711 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1712 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
1713 if (error)
1714 goto out;
1715 if (nd.ni_cnd.cn_flags & ISSYMLINK) {
1716 vput(nd.ni_vp);
1717 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1718 error = EINVAL;
1719 }
1720 }
1721 out:
1722 vp = nd.ni_vp;
1723 if (!error) {
1724 error = nfsrv_composefh(vp, &nsfh, TRUE);
1725 if (!error)
1726 error = VOP_GETATTR(vp, &va, cred, lwp);
1727 vput(vp);
1728 }
1729 if (dirp) {
1730 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, lwp);
1731 vrele(dirp);
1732 }
1733 nfsm_reply(NFSX_SRVFH(&nsfh, TRUE) + NFSX_POSTOPATTR(1) +
1734 NFSX_WCCDATA(1));
1735 if (!error) {
1736 nfsm_srvpostop_fh(&nsfh);
1737 nfsm_srvpostop_attr(0, &va);
1738 }
1739 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1740 vn_finished_write(mp, 0);
1741 return (0);
1742 nfsmout:
1743 if (dirp)
1744 vrele(dirp);
1745 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1746 if (nd.ni_dvp == nd.ni_vp)
1747 vrele(nd.ni_dvp);
1748 else
1749 vput(nd.ni_dvp);
1750 if (nd.ni_vp)
1751 vput(nd.ni_vp);
1752 vn_finished_write(mp, 0);
1753 return (error);
1754 }
1755
1756 /*
1757 * nfs remove service
1758 */
1759 int
1760 nfsrv_remove(nfsd, slp, lwp, mrq)
1761 struct nfsrv_descript *nfsd;
1762 struct nfssvc_sock *slp;
1763 struct lwp *lwp;
1764 struct mbuf **mrq;
1765 {
1766 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1767 struct mbuf *nam = nfsd->nd_nam;
1768 caddr_t dpos = nfsd->nd_dpos;
1769 kauth_cred_t cred = nfsd->nd_cr;
1770 struct nameidata nd;
1771 u_int32_t *tl;
1772 int32_t t1;
1773 caddr_t bpos;
1774 int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
1775 int v3 = (nfsd->nd_flag & ND_NFSV3);
1776 char *cp2;
1777 struct mbuf *mb, *mreq;
1778 struct vnode *vp, *dirp;
1779 struct vattr dirfor, diraft;
1780 nfsrvfh_t nsfh;
1781 u_quad_t frev;
1782 struct mount *mp = NULL;
1783
1784 #ifndef nolint
1785 vp = (struct vnode *)0;
1786 #endif
1787 nfsm_srvmtofh(&nsfh);
1788 if ((mp = vfs_getvfs(&NFSRVFH_FHANDLE(&nsfh)->fh_fsid)) == NULL)
1789 return (ESTALE);
1790 vn_start_write(NULL, &mp, V_WAIT);
1791 nfsm_srvnamesiz(len);
1792 nd.ni_cnd.cn_cred = cred;
1793 nd.ni_cnd.cn_nameiop = DELETE;
1794 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
1795 error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
1796 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1797 if (dirp) {
1798 if (v3)
1799 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, lwp);
1800 else
1801 vrele(dirp);
1802 }
1803 if (!error) {
1804 vp = nd.ni_vp;
1805 if (vp->v_type == VDIR &&
1806 (error = kauth_authorize_generic(cred,
1807 KAUTH_GENERIC_ISSUSER, (u_short *)0)) != 0)
1808 goto out;
1809 /*
1810 * The root of a mounted filesystem cannot be deleted.
1811 */
1812 if (vp->v_flag & VROOT) {
1813 error = EBUSY;
1814 goto out;
1815 }
1816 out:
1817 if (!error) {
1818 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1819 nqsrv_getl(vp, ND_WRITE);
1820 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1821 } else {
1822 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1823 if (nd.ni_dvp == vp)
1824 vrele(nd.ni_dvp);
1825 else
1826 vput(nd.ni_dvp);
1827 vput(vp);
1828 }
1829 }
1830 if (dirp && v3) {
1831 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, lwp);
1832 vrele(dirp);
1833 }
1834 nfsm_reply(NFSX_WCCDATA(v3));
1835 if (v3) {
1836 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1837 vn_finished_write(mp, 0);
1838 return (0);
1839 }
1840 vn_finished_write(mp, 0);
1841 nfsm_srvdone;
1842 }
1843
1844 /*
1845 * nfs rename service
1846 */
1847 int
1848 nfsrv_rename(nfsd, slp, lwp, mrq)
1849 struct nfsrv_descript *nfsd;
1850 struct nfssvc_sock *slp;
1851 struct lwp *lwp;
1852 struct mbuf **mrq;
1853 {
1854 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1855 struct mbuf *nam = nfsd->nd_nam;
1856 caddr_t dpos = nfsd->nd_dpos;
1857 kauth_cred_t cred = nfsd->nd_cr;
1858 u_int32_t *tl;
1859 int32_t t1;
1860 caddr_t bpos;
1861 int error = 0, cache = 0, fdirfor_ret = 1, fdiraft_ret = 1;
1862 uint32_t len, len2;
1863 int tdirfor_ret = 1, tdiraft_ret = 1;
1864 int v3 = (nfsd->nd_flag & ND_NFSV3);
1865 char *cp2;
1866 struct mbuf *mb, *mreq;
1867 struct nameidata fromnd, tond;
1868 struct vnode *fvp, *tvp, *tdvp, *fdirp = (struct vnode *)0;
1869 struct vnode *tdirp = (struct vnode *)0;
1870 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
1871 nfsrvfh_t fnsfh, tnsfh;
1872 u_quad_t frev;
1873 uid_t saved_uid;
1874 struct mount *mp = NULL;
1875
1876 #ifndef nolint
1877 fvp = (struct vnode *)0;
1878 #endif
1879 fromnd.ni_cnd.cn_nameiop = 0;
1880 tond.ni_cnd.cn_nameiop = 0;
1881 nfsm_srvmtofh(&fnsfh);
1882 if ((mp = vfs_getvfs(&NFSRVFH_FHANDLE(&fnsfh)->fh_fsid)) == NULL)
1883 return (ESTALE);
1884 vn_start_write(NULL, &mp, V_WAIT);
1885 nfsm_srvnamesiz(len);
1886 /*
1887 * Remember our original uid so that we can reset cr_uid before
1888 * the second nfs_namei() call, in case it is remapped.
1889 */
1890 saved_uid = kauth_cred_geteuid(cred);
1891 fromnd.ni_cnd.cn_cred = cred;
1892 fromnd.ni_cnd.cn_nameiop = DELETE;
1893 fromnd.ni_cnd.cn_flags = WANTPARENT | SAVESTART;
1894 error = nfs_namei(&fromnd, &fnsfh, len, slp, nam, &md,
1895 &dpos, &fdirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1896 if (fdirp) {
1897 if (v3)
1898 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, cred, lwp);
1899 else {
1900 vrele(fdirp);
1901 fdirp = (struct vnode *)0;
1902 }
1903 }
1904 if (error) {
1905 nfsm_reply(2 * NFSX_WCCDATA(v3));
1906 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
1907 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
1908 if (fdirp)
1909 vrele(fdirp);
1910 vn_finished_write(mp, 0);
1911 return (0);
1912 }
1913 fvp = fromnd.ni_vp;
1914 nfsm_srvmtofh(&tnsfh);
1915 if (v3) {
1916 nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED);
1917 len2 = fxdr_unsigned(uint32_t, *tl);
1918 /* len2 will be checked by nfs_namei */
1919 }
1920 else {
1921 /* NFSv2 */
1922 nfsm_strsiz(len2, NFS_MAXNAMLEN);
1923 }
1924 kauth_cred_seteuid(cred, saved_uid);
1925 tond.ni_cnd.cn_cred = cred;
1926 tond.ni_cnd.cn_nameiop = RENAME;
1927 tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART;
1928 error = nfs_namei(&tond, &tnsfh, len2, slp, nam, &md,
1929 &dpos, &tdirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1930 if (tdirp) {
1931 if (v3)
1932 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, cred, lwp);
1933 else {
1934 vrele(tdirp);
1935 tdirp = (struct vnode *)0;
1936 }
1937 }
1938 if (error) {
1939 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1940 vrele(fromnd.ni_dvp);
1941 vrele(fvp);
1942 goto out1;
1943 }
1944 tdvp = tond.ni_dvp;
1945 tvp = tond.ni_vp;
1946 if (tvp != NULL) {
1947 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1948 if (v3)
1949 error = EEXIST;
1950 else
1951 error = EISDIR;
1952 goto out;
1953 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1954 if (v3)
1955 error = EEXIST;
1956 else
1957 error = ENOTDIR;
1958 goto out;
1959 }
1960 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1961 if (v3)
1962 error = EXDEV;
1963 else
1964 error = ENOTEMPTY;
1965 goto out;
1966 }
1967 }
1968 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1969 if (v3)
1970 error = EXDEV;
1971 else
1972 error = ENOTEMPTY;
1973 goto out;
1974 }
1975 if (fvp->v_mount != tdvp->v_mount) {
1976 if (v3)
1977 error = EXDEV;
1978 else
1979 error = ENOTEMPTY;
1980 goto out;
1981 }
1982 if (fvp == tdvp) {
1983 if (v3)
1984 error = EINVAL;
1985 else
1986 error = ENOTEMPTY;
1987 }
1988 /*
1989 * If source is the same as the destination (that is the
1990 * same vnode with the same name in the same directory),
1991 * then there is nothing to do.
1992 */
1993 if (fvp == tvp && fromnd.ni_dvp == tdvp &&
1994 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
1995 !memcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
1996 fromnd.ni_cnd.cn_namelen))
1997 error = -1;
1998 out:
1999 if (!error) {
2000 nqsrv_getl(fromnd.ni_dvp, ND_WRITE);
2001 nqsrv_getl(tdvp, ND_WRITE);
2002 if (tvp) {
2003 nqsrv_getl(tvp, ND_WRITE);
2004 }
2005 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2006 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2007 } else {
2008 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
2009 if (tdvp == tvp)
2010 vrele(tdvp);
2011 else
2012 vput(tdvp);
2013 if (tvp)
2014 vput(tvp);
2015 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2016 vrele(fromnd.ni_dvp);
2017 vrele(fvp);
2018 if (error == -1)
2019 error = 0;
2020 }
2021 vrele(tond.ni_startdir);
2022 PNBUF_PUT(tond.ni_cnd.cn_pnbuf);
2023 out1:
2024 if (fdirp) {
2025 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, cred, lwp);
2026 vrele(fdirp);
2027 }
2028 if (tdirp) {
2029 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, cred, lwp);
2030 vrele(tdirp);
2031 }
2032 vrele(fromnd.ni_startdir);
2033 PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
2034 nfsm_reply(2 * NFSX_WCCDATA(v3));
2035 if (v3) {
2036 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2037 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2038 }
2039 vn_finished_write(mp, 0);
2040 return (0);
2041
2042 nfsmout:
2043 if (fdirp)
2044 vrele(fdirp);
2045 #ifdef notdef
2046 if (tdirp)
2047 vrele(tdirp);
2048 #endif
2049 if (tond.ni_cnd.cn_nameiop) {
2050 vrele(tond.ni_startdir);
2051 PNBUF_PUT(tond.ni_cnd.cn_pnbuf);
2052 }
2053 if (fromnd.ni_cnd.cn_nameiop) {
2054 vrele(fromnd.ni_startdir);
2055 PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
2056 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2057 vrele(fromnd.ni_dvp);
2058 vrele(fvp);
2059 }
2060 return (error);
2061 }
2062
2063 /*
2064 * nfs link service
2065 */
2066 int
2067 nfsrv_link(nfsd, slp, lwp, mrq)
2068 struct nfsrv_descript *nfsd;
2069 struct nfssvc_sock *slp;
2070 struct lwp *lwp;
2071 struct mbuf **mrq;
2072 {
2073 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2074 struct mbuf *nam = nfsd->nd_nam;
2075 caddr_t dpos = nfsd->nd_dpos;
2076 kauth_cred_t cred = nfsd->nd_cr;
2077 struct nameidata nd;
2078 u_int32_t *tl;
2079 int32_t t1;
2080 caddr_t bpos;
2081 int error = 0, rdonly, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
2082 int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
2083 char *cp2;
2084 struct mbuf *mb, *mreq;
2085 struct vnode *vp, *xp, *dirp = (struct vnode *)0;
2086 struct vattr dirfor, diraft, at;
2087 nfsrvfh_t nsfh, dnsfh;
2088 u_quad_t frev;
2089 struct mount *mp = NULL;
2090
2091 nfsm_srvmtofh(&nsfh);
2092 if ((mp = vfs_getvfs(&NFSRVFH_FHANDLE(&nsfh)->fh_fsid)) == NULL)
2093 return (ESTALE);
2094 vn_start_write(NULL, &mp, V_WAIT);
2095 nfsm_srvmtofh(&dnsfh);
2096 nfsm_srvnamesiz(len);
2097 error = nfsrv_fhtovp(&nsfh, FALSE, &vp, cred, slp, nam,
2098 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2099 if (error) {
2100 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2101 nfsm_srvpostop_attr(getret, &at);
2102 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2103 vn_finished_write(mp, 0);
2104 return (0);
2105 }
2106 if (vp->v_type == VDIR && (error = kauth_authorize_generic(cred,
2107 KAUTH_GENERIC_ISSUSER, (u_short *)0)) != 0)
2108 goto out1;
2109 nd.ni_cnd.cn_cred = cred;
2110 nd.ni_cnd.cn_nameiop = CREATE;
2111 nd.ni_cnd.cn_flags = LOCKPARENT;
2112 error = nfs_namei(&nd, &dnsfh, len, slp, nam, &md, &dpos,
2113 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2114 if (dirp) {
2115 if (v3)
2116 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, lwp);
2117 else {
2118 vrele(dirp);
2119 dirp = (struct vnode *)0;
2120 }
2121 }
2122 if (error)
2123 goto out1;
2124 xp = nd.ni_vp;
2125 if (xp != NULL) {
2126 error = EEXIST;
2127 goto out;
2128 }
2129 xp = nd.ni_dvp;
2130 if (vp->v_mount != xp->v_mount)
2131 error = EXDEV;
2132 out:
2133 if (!error) {
2134 nqsrv_getl(vp, ND_WRITE);
2135 nqsrv_getl(xp, ND_WRITE);
2136 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
2137 } else {
2138 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2139 if (nd.ni_dvp == nd.ni_vp)
2140 vrele(nd.ni_dvp);
2141 else
2142 vput(nd.ni_dvp);
2143 if (nd.ni_vp)
2144 vrele(nd.ni_vp);
2145 }
2146 out1:
2147 if (v3)
2148 getret = VOP_GETATTR(vp, &at, cred, lwp);
2149 if (dirp) {
2150 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, lwp);
2151 vrele(dirp);
2152 }
2153 vrele(vp);
2154 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2155 if (v3) {
2156 nfsm_srvpostop_attr(getret, &at);
2157 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2158 vn_finished_write(mp, 0);
2159 return (0);
2160 }
2161 vn_finished_write(mp, 0);
2162 nfsm_srvdone;
2163 }
2164
2165 /*
2166 * nfs symbolic link service
2167 */
2168 int
2169 nfsrv_symlink(nfsd, slp, lwp, mrq)
2170 struct nfsrv_descript *nfsd;
2171 struct nfssvc_sock *slp;
2172 struct lwp *lwp;
2173 struct mbuf **mrq;
2174 {
2175 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2176 struct mbuf *nam = nfsd->nd_nam;
2177 caddr_t dpos = nfsd->nd_dpos;
2178 kauth_cred_t cred = nfsd->nd_cr;
2179 struct vattr va, dirfor, diraft;
2180 struct nameidata nd;
2181 u_int32_t *tl;
2182 int32_t t1;
2183 struct nfsv2_sattr *sp;
2184 char *bpos, *pathcp = NULL, *cp2;
2185 struct uio io;
2186 struct iovec iv;
2187 int error = 0, cache = 0, dirfor_ret = 1, diraft_ret = 1;
2188 uint32_t len, len2;
2189 int v3 = (nfsd->nd_flag & ND_NFSV3);
2190 struct mbuf *mb, *mreq;
2191 struct vnode *dirp = (struct vnode *)0;
2192 nfsrvfh_t nsfh;
2193 u_quad_t frev;
2194 struct mount *mp = NULL;
2195
2196 nd.ni_cnd.cn_nameiop = 0;
2197 nfsm_srvmtofh(&nsfh);
2198 if ((mp = vfs_getvfs(&NFSRVFH_FHANDLE(&nsfh)->fh_fsid)) == NULL)
2199 return (ESTALE);
2200 vn_start_write(NULL, &mp, V_WAIT);
2201 nfsm_srvnamesiz(len);
2202 nd.ni_cnd.cn_cred = cred;
2203 nd.ni_cnd.cn_nameiop = CREATE;
2204 nd.ni_cnd.cn_flags = LOCKPARENT;
2205 error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
2206 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2207 if (dirp) {
2208 if (v3)
2209 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, lwp);
2210 else {
2211 vrele(dirp);
2212 dirp = (struct vnode *)0;
2213 }
2214 }
2215 if (error)
2216 goto out;
2217 VATTR_NULL(&va);
2218 va.va_type = VLNK;
2219 if (v3) {
2220 va.va_mode = 0;
2221 nfsm_srvsattr(&va);
2222 nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED);
2223 len2 = fxdr_unsigned(uint32_t, *tl);
2224 if (len2 > PATH_MAX) {
2225 /* XXX should check _PC_NO_TRUNC */
2226 error = ENAMETOOLONG;
2227 goto abortop;
2228 }
2229 }
2230 else {
2231 /* NFSv2 */
2232 nfsm_strsiz(len2, NFS_MAXPATHLEN);
2233 }
2234 pathcp = malloc(len2 + 1, M_TEMP, M_WAITOK);
2235 iv.iov_base = pathcp;
2236 iv.iov_len = len2;
2237 io.uio_resid = len2;
2238 io.uio_offset = 0;
2239 io.uio_iov = &iv;
2240 io.uio_iovcnt = 1;
2241 io.uio_rw = UIO_READ;
2242 UIO_SETUP_SYSSPACE(&io);
2243 nfsm_mtouio(&io, len2);
2244 if (!v3) {
2245 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2246 va.va_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
2247 }
2248 *(pathcp + len2) = '\0';
2249 if (nd.ni_vp) {
2250 error = EEXIST;
2251 abortop:
2252 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2253 if (nd.ni_dvp == nd.ni_vp)
2254 vrele(nd.ni_dvp);
2255 else
2256 vput(nd.ni_dvp);
2257 if (nd.ni_vp)
2258 vrele(nd.ni_vp);
2259 goto out;
2260 }
2261 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2262 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va, pathcp);
2263 if (!error) {
2264 if (v3) {
2265 error = nfsrv_composefh(nd.ni_vp, &nsfh, v3);
2266 if (!error)
2267 error = VOP_GETATTR(nd.ni_vp, &va, cred, lwp);
2268 vput(nd.ni_vp);
2269 } else {
2270 vput(nd.ni_vp);
2271 }
2272 }
2273 out:
2274 if (pathcp)
2275 free(pathcp, M_TEMP);
2276 if (dirp) {
2277 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, lwp);
2278 vrele(dirp);
2279 }
2280 nfsm_reply(NFSX_SRVFH(&nsfh, v3) + NFSX_POSTOPATTR(v3) +
2281 NFSX_WCCDATA(v3));
2282 if (v3) {
2283 if (!error) {
2284 nfsm_srvpostop_fh(&nsfh);
2285 nfsm_srvpostop_attr(0, &va);
2286 }
2287 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2288 }
2289 vn_finished_write(mp, 0);
2290 return (0);
2291 nfsmout:
2292 if (dirp)
2293 vrele(dirp);
2294 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2295 if (nd.ni_dvp == nd.ni_vp)
2296 vrele(nd.ni_dvp);
2297 else
2298 vput(nd.ni_dvp);
2299 if (nd.ni_vp)
2300 vrele(nd.ni_vp);
2301 if (pathcp)
2302 free(pathcp, M_TEMP);
2303 vn_finished_write(mp, 0);
2304 return (error);
2305 }
2306
2307 /*
2308 * nfs mkdir service
2309 */
2310 int
2311 nfsrv_mkdir(nfsd, slp, lwp, mrq)
2312 struct nfsrv_descript *nfsd;
2313 struct nfssvc_sock *slp;
2314 struct lwp *lwp;
2315 struct mbuf **mrq;
2316 {
2317 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2318 struct mbuf *nam = nfsd->nd_nam;
2319 caddr_t dpos = nfsd->nd_dpos;
2320 kauth_cred_t cred = nfsd->nd_cr;
2321 struct vattr va, dirfor, diraft;
2322 struct nfs_fattr *fp;
2323 struct nameidata nd;
2324 caddr_t cp;
2325 u_int32_t *tl;
2326 int32_t t1;
2327 caddr_t bpos;
2328 int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
2329 int v3 = (nfsd->nd_flag & ND_NFSV3);
2330 char *cp2;
2331 struct mbuf *mb, *mreq;
2332 struct vnode *vp, *dirp = (struct vnode *)0;
2333 nfsrvfh_t nsfh;
2334 u_quad_t frev;
2335 struct mount *mp = NULL;
2336
2337 nfsm_srvmtofh(&nsfh);
2338 if ((mp = vfs_getvfs(&NFSRVFH_FHANDLE(&nsfh)->fh_fsid)) == NULL)
2339 return (ESTALE);
2340 vn_start_write(NULL, &mp, V_WAIT);
2341 nfsm_srvnamesiz(len);
2342 nd.ni_cnd.cn_cred = cred;
2343 nd.ni_cnd.cn_nameiop = CREATE;
2344 nd.ni_cnd.cn_flags = LOCKPARENT;
2345 error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
2346 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2347 if (dirp) {
2348 if (v3)
2349 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, lwp);
2350 else {
2351 vrele(dirp);
2352 dirp = (struct vnode *)0;
2353 }
2354 }
2355 if (error) {
2356 nfsm_reply(NFSX_WCCDATA(v3));
2357 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2358 if (dirp)
2359 vrele(dirp);
2360 vn_finished_write(mp, 0);
2361 return (0);
2362 }
2363 VATTR_NULL(&va);
2364 if (v3) {
2365 va.va_mode = 0;
2366 nfsm_srvsattr(&va);
2367 } else {
2368 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2369 va.va_mode = nfstov_mode(*tl++);
2370 }
2371 va.va_type = VDIR;
2372 vp = nd.ni_vp;
2373 if (vp != NULL) {
2374 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2375 if (nd.ni_dvp == vp)
2376 vrele(nd.ni_dvp);
2377 else
2378 vput(nd.ni_dvp);
2379 vrele(vp);
2380 error = EEXIST;
2381 goto out;
2382 }
2383 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2384 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
2385 if (!error) {
2386 vp = nd.ni_vp;
2387 error = nfsrv_composefh(vp, &nsfh, v3);
2388 if (!error)
2389 error = VOP_GETATTR(vp, &va, cred, lwp);
2390 vput(vp);
2391 }
2392 out:
2393 if (dirp) {
2394 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, lwp);
2395 vrele(dirp);
2396 }
2397 nfsm_reply(NFSX_SRVFH(&nsfh, v3) + NFSX_POSTOPATTR(v3) +
2398 NFSX_WCCDATA(v3));
2399 if (v3) {
2400 if (!error) {
2401 nfsm_srvpostop_fh(&nsfh);
2402 nfsm_srvpostop_attr(0, &va);
2403 }
2404 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2405 } else {
2406 nfsm_srvfhtom(&nsfh, v3);
2407 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
2408 nfsm_srvfillattr(&va, fp);
2409 }
2410 vn_finished_write(mp, 0);
2411 return (0);
2412 nfsmout:
2413 if (dirp)
2414 vrele(dirp);
2415 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2416 if (nd.ni_dvp == nd.ni_vp)
2417 vrele(nd.ni_dvp);
2418 else
2419 vput(nd.ni_dvp);
2420 if (nd.ni_vp)
2421 vrele(nd.ni_vp);
2422 vn_finished_write(mp, 0);
2423 return (error);
2424 }
2425
2426 /*
2427 * nfs rmdir service
2428 */
2429 int
2430 nfsrv_rmdir(nfsd, slp, lwp, mrq)
2431 struct nfsrv_descript *nfsd;
2432 struct nfssvc_sock *slp;
2433 struct lwp *lwp;
2434 struct mbuf **mrq;
2435 {
2436 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2437 struct mbuf *nam = nfsd->nd_nam;
2438 caddr_t dpos = nfsd->nd_dpos;
2439 kauth_cred_t cred = nfsd->nd_cr;
2440 u_int32_t *tl;
2441 int32_t t1;
2442 caddr_t bpos;
2443 int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
2444 int v3 = (nfsd->nd_flag & ND_NFSV3);
2445 char *cp2;
2446 struct mbuf *mb, *mreq;
2447 struct vnode *vp, *dirp = (struct vnode *)0;
2448 struct vattr dirfor, diraft;
2449 nfsrvfh_t nsfh;
2450 struct nameidata nd;
2451 u_quad_t frev;
2452 struct mount *mp = NULL;
2453
2454 nfsm_srvmtofh(&nsfh);
2455 if ((mp = vfs_getvfs(&NFSRVFH_FHANDLE(&nsfh)->fh_fsid)) == NULL)
2456 return (ESTALE);
2457 vn_start_write(NULL, &mp, V_WAIT);
2458 nfsm_srvnamesiz(len);
2459 nd.ni_cnd.cn_cred = cred;
2460 nd.ni_cnd.cn_nameiop = DELETE;
2461 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
2462 error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos,
2463 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2464 if (dirp) {
2465 if (v3)
2466 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, lwp);
2467 else {
2468 vrele(dirp);
2469 dirp = (struct vnode *)0;
2470 }
2471 }
2472 if (error) {
2473 nfsm_reply(NFSX_WCCDATA(v3));
2474 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2475 if (dirp)
2476 vrele(dirp);
2477 vn_finished_write(mp, 0);
2478 return (0);
2479 }
2480 vp = nd.ni_vp;
2481 if (vp->v_type != VDIR) {
2482 error = ENOTDIR;
2483 goto out;
2484 }
2485 /*
2486 * No rmdir "." please.
2487 */
2488 if (nd.ni_dvp == vp) {
2489 error = EINVAL;
2490 goto out;
2491 }
2492 /*
2493 * The root of a mounted filesystem cannot be deleted.
2494 */
2495 if (vp->v_flag & VROOT)
2496 error = EBUSY;
2497 out:
2498 if (!error) {
2499 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2500 nqsrv_getl(vp, ND_WRITE);
2501 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2502 } else {
2503 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2504 if (nd.ni_dvp == nd.ni_vp)
2505 vrele(nd.ni_dvp);
2506 else
2507 vput(nd.ni_dvp);
2508 vput(vp);
2509 }
2510 if (dirp) {
2511 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, lwp);
2512 vrele(dirp);
2513 }
2514 nfsm_reply(NFSX_WCCDATA(v3));
2515 if (v3) {
2516 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2517 vn_finished_write(mp, 0);
2518 return (0);
2519 }
2520 vn_finished_write(mp, 0);
2521 nfsm_srvdone;
2522 }
2523
2524 /*
2525 * nfs readdir service
2526 * - mallocs what it thinks is enough to read
2527 * count rounded up to a multiple of NFS_SRVDIRBLKSIZ <= NFS_MAXREADDIR
2528 * - calls VOP_READDIR()
2529 * - loops around building the reply
2530 * if the output generated exceeds count break out of loop
2531 * The nfsm_clget macro is used here so that the reply will be packed
2532 * tightly in mbuf clusters.
2533 * - it only knows that it has encountered eof when the VOP_READDIR()
2534 * reads nothing
2535 * - as such one readdir rpc will return eof false although you are there
2536 * and then the next will return eof
2537 * - it trims out records with d_fileno == 0
2538 * this doesn't matter for Unix clients, but they might confuse clients
2539 * for other os'.
2540 * - it trims out records with d_type == DT_WHT
2541 * these cannot be seen through NFS (unless we extend the protocol)
2542 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2543 * than requested, but this may not apply to all filesystems. For
2544 * example, client NFS does not { although it is never remote mounted
2545 * anyhow }
2546 * The alternate call nfsrv_readdirplus() does lookups as well.
2547 * PS: The NFS protocol spec. does not clarify what the "count" byte
2548 * argument is a count of.. just name strings and file id's or the
2549 * entire reply rpc or ...
2550 * I tried just file name and id sizes and it confused the Sun client,
2551 * so I am using the full rpc size now. The "paranoia.." comment refers
2552 * to including the status longwords that are not a part of the dir.
2553 * "entry" structures, but are in the rpc.
2554 */
2555
2556 #define NFS_SRVDIRBLKSIZ 1024
2557
2558 struct flrep {
2559 nfsuint64 fl_off;
2560 u_int32_t fl_postopok;
2561 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2562 u_int32_t fl_fhok;
2563 u_int32_t fl_fhsize;
2564 };
2565
2566 int
2567 nfsrv_readdir(nfsd, slp, lwp, mrq)
2568 struct nfsrv_descript *nfsd;
2569 struct nfssvc_sock *slp;
2570 struct lwp *lwp;
2571 struct mbuf **mrq;
2572 {
2573 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2574 struct mbuf *nam = nfsd->nd_nam;
2575 caddr_t dpos = nfsd->nd_dpos;
2576 kauth_cred_t cred = nfsd->nd_cr;
2577 char *bp, *be;
2578 struct mbuf *mp;
2579 struct dirent *dp;
2580 caddr_t cp;
2581 u_int32_t *tl;
2582 int32_t t1;
2583 caddr_t bpos;
2584 struct mbuf *mb, *mreq, *mp2;
2585 char *cpos, *cend, *cp2, *rbuf;
2586 struct vnode *vp;
2587 struct vattr at;
2588 nfsrvfh_t nsfh;
2589 struct uio io;
2590 struct iovec iv;
2591 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2592 int siz, cnt, fullsiz, eofflag, rdonly, cache = 0, ncookies;
2593 int v3 = (nfsd->nd_flag & ND_NFSV3);
2594 u_quad_t frev, off, toff, verf;
2595 off_t *cookies = NULL, *cookiep;
2596 nfsuint64 jar;
2597
2598 nfsm_srvmtofh(&nsfh);
2599 if (v3) {
2600 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2601 toff = fxdr_hyper(tl);
2602 tl += 2;
2603 verf = fxdr_hyper(tl);
2604 tl += 2;
2605 } else {
2606 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2607 toff = fxdr_unsigned(u_quad_t, *tl++);
2608 }
2609 off = toff;
2610 cnt = fxdr_unsigned(int, *tl);
2611 siz = ((cnt + NFS_SRVDIRBLKSIZ - 1) & ~(NFS_SRVDIRBLKSIZ - 1));
2612 xfer = NFS_SRVMAXDATA(nfsd);
2613 if (siz > xfer)
2614 siz = xfer;
2615 fullsiz = siz;
2616 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
2617 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2618 if (!error && vp->v_type != VDIR) {
2619 error = ENOTDIR;
2620 vput(vp);
2621 }
2622 if (error) {
2623 nfsm_reply(NFSX_UNSIGNED);
2624 nfsm_srvpostop_attr(getret, &at);
2625 return (0);
2626 }
2627 nqsrv_getl(vp, ND_READ);
2628 if (v3) {
2629 error = getret = VOP_GETATTR(vp, &at, cred, lwp);
2630 #ifdef NFS3_STRICTVERF
2631 /*
2632 * XXX This check is too strict for Solaris 2.5 clients.
2633 */
2634 if (!error && toff && verf != at.va_filerev)
2635 error = NFSERR_BAD_COOKIE;
2636 #endif
2637 }
2638 if (!error)
2639 error = nfsrv_access(vp, VEXEC, cred, rdonly, lwp, 0);
2640 if (error) {
2641 vput(vp);
2642 nfsm_reply(NFSX_POSTOPATTR(v3));
2643 nfsm_srvpostop_attr(getret, &at);
2644 return (0);
2645 }
2646 VOP_UNLOCK(vp, 0);
2647 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2648 again:
2649 iv.iov_base = rbuf;
2650 iv.iov_len = fullsiz;
2651 io.uio_iov = &iv;
2652 io.uio_iovcnt = 1;
2653 io.uio_offset = (off_t)off;
2654 io.uio_resid = fullsiz;
2655 io.uio_rw = UIO_READ;
2656 UIO_SETUP_SYSSPACE(&io);
2657 eofflag = 0;
2658 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2659
2660 error = VOP_READDIR(vp, &io, cred, &eofflag, &cookies, &ncookies);
2661
2662 off = (off_t)io.uio_offset;
2663 if (!cookies && !error)
2664 error = NFSERR_PERM;
2665 if (v3) {
2666 getret = VOP_GETATTR(vp, &at, cred, lwp);
2667 if (!error)
2668 error = getret;
2669 }
2670
2671 VOP_UNLOCK(vp, 0);
2672 if (error) {
2673 vrele(vp);
2674 free((caddr_t)rbuf, M_TEMP);
2675 if (cookies)
2676 free((caddr_t)cookies, M_TEMP);
2677 nfsm_reply(NFSX_POSTOPATTR(v3));
2678 nfsm_srvpostop_attr(getret, &at);
2679 return (0);
2680 }
2681 if (io.uio_resid) {
2682 siz -= io.uio_resid;
2683
2684 /*
2685 * If nothing read, return eof
2686 * rpc reply
2687 */
2688 if (siz == 0) {
2689 vrele(vp);
2690 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
2691 2 * NFSX_UNSIGNED);
2692 if (v3) {
2693 nfsm_srvpostop_attr(getret, &at);
2694 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2695 txdr_hyper(at.va_filerev, tl);
2696 tl += 2;
2697 } else
2698 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2699 *tl++ = nfs_false;
2700 *tl = nfs_true;
2701 free((caddr_t)rbuf, M_TEMP);
2702 free((caddr_t)cookies, M_TEMP);
2703 return (0);
2704 }
2705 }
2706
2707 /*
2708 * Check for degenerate cases of nothing useful read.
2709 * If so go try again
2710 */
2711 cpos = rbuf;
2712 cend = rbuf + siz;
2713 dp = (struct dirent *)cpos;
2714 cookiep = cookies;
2715
2716 while (cpos < cend && ncookies > 0 &&
2717 (dp->d_fileno == 0 || dp->d_type == DT_WHT)) {
2718 cpos += dp->d_reclen;
2719 dp = (struct dirent *)cpos;
2720 cookiep++;
2721 ncookies--;
2722 }
2723 if (cpos >= cend || ncookies == 0) {
2724 toff = off;
2725 siz = fullsiz;
2726 free(cookies, M_TEMP);
2727 goto again;
2728 }
2729
2730 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
2731 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
2732 if (v3) {
2733 nfsm_srvpostop_attr(getret, &at);
2734 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2735 txdr_hyper(at.va_filerev, tl);
2736 }
2737 mp = mp2 = mb;
2738 bp = bpos;
2739 be = bp + M_TRAILINGSPACE(mp);
2740
2741 /* Loop through the records and build reply */
2742 while (cpos < cend && ncookies > 0) {
2743 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
2744 nlen = dp->d_namlen;
2745 rem = nfsm_rndup(nlen)-nlen;
2746 len += (4 * NFSX_UNSIGNED + nlen + rem);
2747 if (v3)
2748 len += 2 * NFSX_UNSIGNED;
2749 if (len > cnt) {
2750 eofflag = 0;
2751 break;
2752 }
2753 /*
2754 * Build the directory record xdr from
2755 * the dirent entry.
2756 */
2757 nfsm_clget;
2758 *tl = nfs_true;
2759 bp += NFSX_UNSIGNED;
2760 if (v3) {
2761 nfsm_clget;
2762 *tl = txdr_unsigned(dp->d_fileno >> 32);
2763 bp += NFSX_UNSIGNED;
2764 }
2765 nfsm_clget;
2766 *tl = txdr_unsigned(dp->d_fileno);
2767 bp += NFSX_UNSIGNED;
2768 nfsm_clget;
2769 *tl = txdr_unsigned(nlen);
2770 bp += NFSX_UNSIGNED;
2771
2772 /* And loop around copying the name */
2773 xfer = nlen;
2774 cp = dp->d_name;
2775 while (xfer > 0) {
2776 nfsm_clget;
2777 if ((bp+xfer) > be)
2778 tsiz = be-bp;
2779 else
2780 tsiz = xfer;
2781 memcpy(bp, cp, tsiz);
2782 bp += tsiz;
2783 xfer -= tsiz;
2784 if (xfer > 0)
2785 cp += tsiz;
2786 }
2787 /* And null pad to an int32_t boundary */
2788 for (i = 0; i < rem; i++)
2789 *bp++ = '\0';
2790 nfsm_clget;
2791
2792 /* Finish off the record */
2793 txdr_hyper(*cookiep, &jar);
2794 if (v3) {
2795 *tl = jar.nfsuquad[0];
2796 bp += NFSX_UNSIGNED;
2797 nfsm_clget;
2798 }
2799 *tl = jar.nfsuquad[1];
2800 bp += NFSX_UNSIGNED;
2801 }
2802 cpos += dp->d_reclen;
2803 dp = (struct dirent *)cpos;
2804 cookiep++;
2805 ncookies--;
2806 }
2807 vrele(vp);
2808 nfsm_clget;
2809 *tl = nfs_false;
2810 bp += NFSX_UNSIGNED;
2811 nfsm_clget;
2812 if (eofflag)
2813 *tl = nfs_true;
2814 else
2815 *tl = nfs_false;
2816 bp += NFSX_UNSIGNED;
2817 if (mp != mb) {
2818 if (bp < be)
2819 mp->m_len = bp - mtod(mp, caddr_t);
2820 } else
2821 mp->m_len += bp - bpos;
2822 free((caddr_t)rbuf, M_TEMP);
2823 free((caddr_t)cookies, M_TEMP);
2824 nfsm_srvdone;
2825 }
2826
2827 int
2828 nfsrv_readdirplus(nfsd, slp, lwp, mrq)
2829 struct nfsrv_descript *nfsd;
2830 struct nfssvc_sock *slp;
2831 struct lwp *lwp;
2832 struct mbuf **mrq;
2833 {
2834 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2835 struct mbuf *nam = nfsd->nd_nam;
2836 caddr_t dpos = nfsd->nd_dpos;
2837 kauth_cred_t cred = nfsd->nd_cr;
2838 char *bp, *be;
2839 struct mbuf *mp;
2840 struct dirent *dp;
2841 caddr_t cp;
2842 u_int32_t *tl;
2843 int32_t t1;
2844 caddr_t bpos;
2845 struct mbuf *mb, *mreq, *mp2;
2846 char *cpos, *cend, *cp2, *rbuf;
2847 struct vnode *vp, *nvp;
2848 struct flrep fl;
2849 nfsrvfh_t nsfh;
2850 struct uio io;
2851 struct iovec iv;
2852 struct vattr va, at, *vap = &va;
2853 struct nfs_fattr *fp;
2854 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2855 int siz, cnt, fullsiz, eofflag, rdonly, cache = 0, dirlen, ncookies;
2856 u_quad_t frev, off, toff, verf;
2857 off_t *cookies = NULL, *cookiep;
2858
2859 nfsm_srvmtofh(&nsfh);
2860 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2861 toff = fxdr_hyper(tl);
2862 tl += 2;
2863 verf = fxdr_hyper(tl);
2864 tl += 2;
2865 siz = fxdr_unsigned(int, *tl++);
2866 cnt = fxdr_unsigned(int, *tl);
2867 off = toff;
2868 siz = ((siz + NFS_SRVDIRBLKSIZ - 1) & ~(NFS_SRVDIRBLKSIZ - 1));
2869 xfer = NFS_SRVMAXDATA(nfsd);
2870 if (siz > xfer)
2871 siz = xfer;
2872 fullsiz = siz;
2873 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
2874 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2875 if (!error && vp->v_type != VDIR) {
2876 error = ENOTDIR;
2877 vput(vp);
2878 }
2879 if (error) {
2880 nfsm_reply(NFSX_UNSIGNED);
2881 nfsm_srvpostop_attr(getret, &at);
2882 return (0);
2883 }
2884 error = getret = VOP_GETATTR(vp, &at, cred, lwp);
2885 #ifdef NFS3_STRICTVERF
2886 /*
2887 * XXX This check is too strict for Solaris 2.5 clients.
2888 */
2889 if (!error && toff && verf != at.va_filerev)
2890 error = NFSERR_BAD_COOKIE;
2891 #endif
2892 if (!error) {
2893 nqsrv_getl(vp, ND_READ);
2894 error = nfsrv_access(vp, VEXEC, cred, rdonly, lwp, 0);
2895 }
2896 if (error) {
2897 vput(vp);
2898 nfsm_reply(NFSX_V3POSTOPATTR);
2899 nfsm_srvpostop_attr(getret, &at);
2900 return (0);
2901 }
2902 VOP_UNLOCK(vp, 0);
2903
2904 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2905 again:
2906 iv.iov_base = rbuf;
2907 iv.iov_len = fullsiz;
2908 io.uio_iov = &iv;
2909 io.uio_iovcnt = 1;
2910 io.uio_offset = (off_t)off;
2911 io.uio_resid = fullsiz;
2912 io.uio_rw = UIO_READ;
2913 UIO_SETUP_SYSSPACE(&io);
2914 eofflag = 0;
2915
2916 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2917
2918 error = VOP_READDIR(vp, &io, cred, &eofflag, &cookies, &ncookies);
2919
2920 off = (u_quad_t)io.uio_offset;
2921 getret = VOP_GETATTR(vp, &at, cred, lwp);
2922
2923 VOP_UNLOCK(vp, 0);
2924
2925 /*
2926 * If the VGET operation doesn't work for this filesystem,
2927 * we can't support readdirplus. Returning NOTSUPP should
2928 * make clients fall back to plain readdir.
2929 * There's no need to check for VPTOFH as well, we wouldn't
2930 * even be here otherwise.
2931 */
2932 if (!getret) {
2933 if ((getret = VFS_VGET(vp->v_mount, at.va_fileid, &nvp)))
2934 getret = (getret == EOPNOTSUPP) ?
2935 NFSERR_NOTSUPP : NFSERR_IO;
2936 else
2937 vput(nvp);
2938 }
2939
2940 if (!cookies && !error)
2941 error = NFSERR_PERM;
2942 if (!error)
2943 error = getret;
2944 if (error) {
2945 vrele(vp);
2946 if (cookies)
2947 free((caddr_t)cookies, M_TEMP);
2948 free((caddr_t)rbuf, M_TEMP);
2949 nfsm_reply(NFSX_V3POSTOPATTR);
2950 nfsm_srvpostop_attr(getret, &at);
2951 return (0);
2952 }
2953 if (io.uio_resid) {
2954 siz -= io.uio_resid;
2955
2956 /*
2957 * If nothing read, return eof
2958 * rpc reply
2959 */
2960 if (siz == 0) {
2961 vrele(vp);
2962 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
2963 2 * NFSX_UNSIGNED);
2964 nfsm_srvpostop_attr(getret, &at);
2965 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2966 txdr_hyper(at.va_filerev, tl);
2967 tl += 2;
2968 *tl++ = nfs_false;
2969 *tl = nfs_true;
2970 free((caddr_t)cookies, M_TEMP);
2971 free((caddr_t)rbuf, M_TEMP);
2972 return (0);
2973 }
2974 }
2975
2976 /*
2977 * Check for degenerate cases of nothing useful read.
2978 * If so go try again
2979 */
2980 cpos = rbuf;
2981 cend = rbuf + siz;
2982 dp = (struct dirent *)cpos;
2983 cookiep = cookies;
2984
2985 while (cpos < cend && ncookies > 0 &&
2986 (dp->d_fileno == 0 || dp->d_type == DT_WHT)) {
2987 cpos += dp->d_reclen;
2988 dp = (struct dirent *)cpos;
2989 cookiep++;
2990 ncookies--;
2991 }
2992 if (cpos >= cend || ncookies == 0) {
2993 toff = off;
2994 siz = fullsiz;
2995 free(cookies, M_TEMP);
2996 goto again;
2997 }
2998
2999 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2 * NFSX_UNSIGNED;
3000 nfsm_reply(cnt);
3001 nfsm_srvpostop_attr(getret, &at);
3002 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3003 txdr_hyper(at.va_filerev, tl);
3004 mp = mp2 = mb;
3005 bp = bpos;
3006 be = bp + M_TRAILINGSPACE(mp);
3007
3008 /* Loop through the records and build reply */
3009 while (cpos < cend && ncookies > 0) {
3010 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3011 nfsrvfh_t nnsfh;
3012
3013 nlen = dp->d_namlen;
3014 rem = nfsm_rndup(nlen)-nlen;
3015
3016 /*
3017 * For readdir_and_lookup get the vnode using
3018 * the file number.
3019 */
3020 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
3021 goto invalid;
3022 if (nfsrv_composefh(nvp, &nnsfh, TRUE)) {
3023 vput(nvp);
3024 goto invalid;
3025 }
3026 if (VOP_GETATTR(nvp, vap, cred, lwp)) {
3027 vput(nvp);
3028 goto invalid;
3029 }
3030 vput(nvp);
3031
3032 /*
3033 * If either the dircount or maxcount will be
3034 * exceeded, get out now. Both of these lengths
3035 * are calculated conservatively, including all
3036 * XDR overheads.
3037 */
3038 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3039 NFSX_V3POSTOPATTR);
3040 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3041 if (len > cnt || dirlen > fullsiz) {
3042 eofflag = 0;
3043 break;
3044 }
3045
3046 /*
3047 * Build the directory record xdr from
3048 * the dirent entry.
3049 */
3050 fp = (struct nfs_fattr *)&fl.fl_fattr;
3051 nfsm_srvfillattr(vap, fp);
3052 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3053 fl.fl_fhok = nfs_true;
3054 fl.fl_postopok = nfs_true;
3055 txdr_hyper(*cookiep, fl.fl_off.nfsuquad);
3056
3057 nfsm_clget;
3058 *tl = nfs_true;
3059 bp += NFSX_UNSIGNED;
3060 nfsm_clget;
3061 *tl = txdr_unsigned(dp->d_fileno >> 32);
3062 bp += NFSX_UNSIGNED;
3063 nfsm_clget;
3064 *tl = txdr_unsigned(dp->d_fileno);
3065 bp += NFSX_UNSIGNED;
3066 nfsm_clget;
3067 *tl = txdr_unsigned(nlen);
3068 bp += NFSX_UNSIGNED;
3069
3070 /* And loop around copying the name */
3071 xfer = nlen;
3072 cp = dp->d_name;
3073 while (xfer > 0) {
3074 nfsm_clget;
3075 if ((bp + xfer) > be)
3076 tsiz = be - bp;
3077 else
3078 tsiz = xfer;
3079 memcpy(bp, cp, tsiz);
3080 bp += tsiz;
3081 xfer -= tsiz;
3082 if (xfer > 0)
3083 cp += tsiz;
3084 }
3085 /* And null pad to an int32_t boundary */
3086 for (i = 0; i < rem; i++)
3087 *bp++ = '\0';
3088
3089 /*
3090 * Now copy the flrep structure out.
3091 */
3092 xfer = sizeof(struct flrep);
3093 cp = (caddr_t)&fl;
3094 while (xfer > 0) {
3095 nfsm_clget;
3096 if ((bp + xfer) > be)
3097 tsiz = be - bp;
3098 else
3099 tsiz = xfer;
3100 memcpy(bp, cp, tsiz);
3101 bp += tsiz;
3102 xfer -= tsiz;
3103 if (xfer > 0)
3104 cp += tsiz;
3105 }
3106
3107 /*
3108 * ... and filehandle.
3109 */
3110 xfer = NFSRVFH_SIZE(&nnsfh);
3111 cp = NFSRVFH_DATA(&nnsfh);
3112 while (xfer > 0) {
3113 nfsm_clget;
3114 if ((bp + xfer) > be)
3115 tsiz = be - bp;
3116 else
3117 tsiz = xfer;
3118 memcpy(bp, cp, tsiz);
3119 bp += tsiz;
3120 xfer -= tsiz;
3121 if (xfer > 0)
3122 cp += tsiz;
3123 }
3124 }
3125 invalid:
3126 cpos += dp->d_reclen;
3127 dp = (struct dirent *)cpos;
3128 cookiep++;
3129 ncookies--;
3130 }
3131 vrele(vp);
3132 nfsm_clget;
3133 *tl = nfs_false;
3134 bp += NFSX_UNSIGNED;
3135 nfsm_clget;
3136 if (eofflag)
3137 *tl = nfs_true;
3138 else
3139 *tl = nfs_false;
3140 bp += NFSX_UNSIGNED;
3141 if (mp != mb) {
3142 if (bp < be)
3143 mp->m_len = bp - mtod(mp, caddr_t);
3144 } else
3145 mp->m_len += bp - bpos;
3146 free((caddr_t)cookies, M_TEMP);
3147 free((caddr_t)rbuf, M_TEMP);
3148 nfsm_srvdone;
3149 }
3150
3151 /*
3152 * nfs commit service
3153 */
3154 int
3155 nfsrv_commit(nfsd, slp, lwp, mrq)
3156 struct nfsrv_descript *nfsd;
3157 struct nfssvc_sock *slp;
3158 struct lwp *lwp;
3159 struct mbuf **mrq;
3160 {
3161 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3162 struct mbuf *nam = nfsd->nd_nam;
3163 caddr_t dpos = nfsd->nd_dpos;
3164 kauth_cred_t cred = nfsd->nd_cr;
3165 struct vattr bfor, aft;
3166 struct vnode *vp;
3167 nfsrvfh_t nsfh;
3168 u_int32_t *tl;
3169 int32_t t1;
3170 caddr_t bpos;
3171 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cache = 0;
3172 uint32_t cnt;
3173 char *cp2;
3174 struct mbuf *mb, *mreq;
3175 u_quad_t frev, off, end;
3176 struct mount *mp = NULL;
3177
3178 nfsm_srvmtofh(&nsfh);
3179 if ((mp = vfs_getvfs(&NFSRVFH_FHANDLE(&nsfh)->fh_fsid)) == NULL)
3180 return (ESTALE);
3181 vn_start_write(NULL, &mp, V_WAIT);
3182 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3183
3184 off = fxdr_hyper(tl);
3185 tl += 2;
3186 cnt = fxdr_unsigned(uint32_t, *tl);
3187 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
3188 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
3189 if (error) {
3190 nfsm_reply(2 * NFSX_UNSIGNED);
3191 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3192 vn_finished_write(mp, 0);
3193 return (0);
3194 }
3195 for_ret = VOP_GETATTR(vp, &bfor, cred, lwp);
3196 end = (cnt > 0) ? off + cnt : vp->v_size;
3197 if (end < off || end > vp->v_size)
3198 end = vp->v_size;
3199 if (off < vp->v_size)
3200 error = VOP_FSYNC(vp, cred, FSYNC_WAIT, off, end, lwp);
3201 /* else error == 0, from nfsrv_fhtovp() */
3202 aft_ret = VOP_GETATTR(vp, &aft, cred, lwp);
3203 vput(vp);
3204 nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3205 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3206 if (!error) {
3207 nfsm_build(tl, u_int32_t *, NFSX_V3WRITEVERF);
3208 *tl++ = txdr_unsigned(boottime.tv_sec);
3209 *tl = txdr_unsigned(boottime.tv_usec);
3210 } else {
3211 vn_finished_write(mp, 0);
3212 return (0);
3213 }
3214 vn_finished_write(mp, 0);
3215 nfsm_srvdone;
3216 }
3217
3218 /*
3219 * nfs statfs service
3220 */
3221 int
3222 nfsrv_statfs(nfsd, slp, lwp, mrq)
3223 struct nfsrv_descript *nfsd;
3224 struct nfssvc_sock *slp;
3225 struct lwp *lwp;
3226 struct mbuf **mrq;
3227 {
3228 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3229 struct mbuf *nam = nfsd->nd_nam;
3230 caddr_t dpos = nfsd->nd_dpos;
3231 kauth_cred_t cred = nfsd->nd_cr;
3232 struct statvfs *sf = NULL;
3233 struct nfs_statfs *sfp;
3234 u_int32_t *tl;
3235 int32_t t1;
3236 caddr_t bpos;
3237 int error = 0, rdonly, cache = 0, getret = 1;
3238 int v3 = (nfsd->nd_flag & ND_NFSV3);
3239 char *cp2;
3240 struct mbuf *mb, *mreq;
3241 struct vnode *vp;
3242 struct vattr at;
3243 nfsrvfh_t nsfh;
3244 u_quad_t frev, tval;
3245
3246 nfsm_srvmtofh(&nsfh);
3247 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
3248 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
3249 if (error) {
3250 nfsm_reply(NFSX_UNSIGNED);
3251 nfsm_srvpostop_attr(getret, &at);
3252 return (0);
3253 }
3254 sf = malloc(sizeof(*sf), M_TEMP, M_WAITOK);
3255 error = VFS_STATVFS(vp->v_mount, sf, lwp);
3256 getret = VOP_GETATTR(vp, &at, cred, lwp);
3257 vput(vp);
3258 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3259 if (v3)
3260 nfsm_srvpostop_attr(getret, &at);
3261 if (error) {
3262 free(sf, M_TEMP);
3263 return (0);
3264 }
3265 nfsm_build(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
3266 if (v3) {
3267 tval = (u_quad_t)((quad_t)sf->f_blocks * (quad_t)sf->f_frsize);
3268 txdr_hyper(tval, &sfp->sf_tbytes);
3269 tval = (u_quad_t)((quad_t)sf->f_bfree * (quad_t)sf->f_frsize);
3270 txdr_hyper(tval, &sfp->sf_fbytes);
3271 tval = (u_quad_t)((quad_t)sf->f_bavail * (quad_t)sf->f_frsize);
3272 txdr_hyper(tval, &sfp->sf_abytes);
3273 tval = (u_quad_t)sf->f_files;
3274 txdr_hyper(tval, &sfp->sf_tfiles);
3275 tval = (u_quad_t)sf->f_ffree;
3276 txdr_hyper(tval, &sfp->sf_ffiles);
3277 txdr_hyper(tval, &sfp->sf_afiles);
3278 sfp->sf_invarsec = 0;
3279 } else {
3280 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3281 sfp->sf_bsize = txdr_unsigned(sf->f_frsize);
3282 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3283 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3284 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3285 }
3286 nfsmout:
3287 if (sf)
3288 free(sf, M_TEMP);
3289 return error;
3290 }
3291
3292 /*
3293 * nfs fsinfo service
3294 */
3295 int
3296 nfsrv_fsinfo(nfsd, slp, lwp, mrq)
3297 struct nfsrv_descript *nfsd;
3298 struct nfssvc_sock *slp;
3299 struct lwp *lwp;
3300 struct mbuf **mrq;
3301 {
3302 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3303 struct mbuf *nam = nfsd->nd_nam;
3304 caddr_t dpos = nfsd->nd_dpos;
3305 kauth_cred_t cred = nfsd->nd_cr;
3306 u_int32_t *tl;
3307 struct nfsv3_fsinfo *sip;
3308 int32_t t1;
3309 caddr_t bpos;
3310 int error = 0, rdonly, cache = 0, getret = 1;
3311 uint32_t maxdata;
3312 char *cp2;
3313 struct mbuf *mb, *mreq;
3314 struct vnode *vp;
3315 struct vattr at;
3316 nfsrvfh_t nsfh;
3317 u_quad_t frev, maxfsize;
3318 struct statvfs *sb;
3319
3320 nfsm_srvmtofh(&nsfh);
3321 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
3322 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
3323 if (error) {
3324 nfsm_reply(NFSX_UNSIGNED);
3325 nfsm_srvpostop_attr(getret, &at);
3326 return (0);
3327 }
3328
3329 /* XXX Try to make a guess on the max file size. */
3330 sb = malloc(sizeof(*sb), M_TEMP, M_WAITOK);
3331 VFS_STATVFS(vp->v_mount, sb, (struct lwp *)0);
3332 maxfsize = (u_quad_t)0x80000000 * sb->f_frsize - 1;
3333 free(sb, M_TEMP);
3334
3335 getret = VOP_GETATTR(vp, &at, cred, lwp);
3336 vput(vp);
3337 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3338 nfsm_srvpostop_attr(getret, &at);
3339 nfsm_build(sip, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3340
3341 /*
3342 * XXX
3343 * There should be file system VFS OP(s) to get this information.
3344 * For now, assume ufs.
3345 */
3346 if (slp->ns_so->so_type == SOCK_DGRAM)
3347 maxdata = NFS_MAXDGRAMDATA;
3348 else
3349 maxdata = NFS_MAXDATA;
3350 sip->fs_rtmax = txdr_unsigned(maxdata);
3351 sip->fs_rtpref = txdr_unsigned(maxdata);
3352 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3353 sip->fs_wtmax = txdr_unsigned(maxdata);
3354 sip->fs_wtpref = txdr_unsigned(maxdata);
3355 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3356 sip->fs_dtpref = txdr_unsigned(maxdata);
3357 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3358 sip->fs_timedelta.nfsv3_sec = 0;
3359 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3360 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3361 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3362 NFSV3FSINFO_CANSETTIME);
3363 nfsm_srvdone;
3364 }
3365
3366 /*
3367 * nfs pathconf service
3368 */
3369 int
3370 nfsrv_pathconf(nfsd, slp, lwp, mrq)
3371 struct nfsrv_descript *nfsd;
3372 struct nfssvc_sock *slp;
3373 struct lwp *lwp;
3374 struct mbuf **mrq;
3375 {
3376 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3377 struct mbuf *nam = nfsd->nd_nam;
3378 caddr_t dpos = nfsd->nd_dpos;
3379 kauth_cred_t cred = nfsd->nd_cr;
3380 u_int32_t *tl;
3381 struct nfsv3_pathconf *pc;
3382 int32_t t1;
3383 caddr_t bpos;
3384 int error = 0, rdonly, cache = 0, getret = 1;
3385 register_t linkmax, namemax, chownres, notrunc;
3386 char *cp2;
3387 struct mbuf *mb, *mreq;
3388 struct vnode *vp;
3389 struct vattr at;
3390 nfsrvfh_t nsfh;
3391 u_quad_t frev;
3392
3393 nfsm_srvmtofh(&nsfh);
3394 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam,
3395 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
3396 if (error) {
3397 nfsm_reply(NFSX_UNSIGNED);
3398 nfsm_srvpostop_attr(getret, &at);
3399 return (0);
3400 }
3401 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3402 if (!error)
3403 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3404 if (!error)
3405 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3406 if (!error)
3407 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3408 getret = VOP_GETATTR(vp, &at, cred, lwp);
3409 vput(vp);
3410 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3411 nfsm_srvpostop_attr(getret, &at);
3412 if (error)
3413 return (0);
3414 nfsm_build(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3415
3416 pc->pc_linkmax = txdr_unsigned(linkmax);
3417 pc->pc_namemax = txdr_unsigned(namemax);
3418 pc->pc_notrunc = txdr_unsigned(notrunc);
3419 pc->pc_chownrestricted = txdr_unsigned(chownres);
3420
3421 /*
3422 * These should probably be supported by VOP_PATHCONF(), but
3423 * until msdosfs is exportable (why would you want to?), the
3424 * Unix defaults should be ok.
3425 */
3426 pc->pc_caseinsensitive = nfs_false;
3427 pc->pc_casepreserving = nfs_true;
3428 nfsm_srvdone;
3429 }
3430
3431 /*
3432 * Null operation, used by clients to ping server
3433 */
3434 /* ARGSUSED */
3435 int
3436 nfsrv_null(nfsd, slp, lwp, mrq)
3437 struct nfsrv_descript *nfsd;
3438 struct nfssvc_sock *slp;
3439 struct lwp *lwp;
3440 struct mbuf **mrq;
3441 {
3442 struct mbuf *mrep = nfsd->nd_mrep;
3443 caddr_t bpos;
3444 int error = NFSERR_RETVOID, cache = 0;
3445 struct mbuf *mb, *mreq;
3446 u_quad_t frev;
3447
3448 nfsm_reply(0);
3449 return (0);
3450 }
3451
3452 /*
3453 * No operation, used for obsolete procedures
3454 */
3455 /* ARGSUSED */
3456 int
3457 nfsrv_noop(nfsd, slp, lwp, mrq)
3458 struct nfsrv_descript *nfsd;
3459 struct nfssvc_sock *slp;
3460 struct lwp *lwp;
3461 struct mbuf **mrq;
3462 {
3463 struct mbuf *mrep = nfsd->nd_mrep;
3464 caddr_t bpos;
3465 int error, cache = 0;
3466 struct mbuf *mb, *mreq;
3467 u_quad_t frev;
3468
3469 if (nfsd->nd_repstat)
3470 error = nfsd->nd_repstat;
3471 else
3472 error = EPROCUNAVAIL;
3473 nfsm_reply(0);
3474 return (0);
3475 }
3476
3477 /*
3478 * Perform access checking for vnodes obtained from file handles that would
3479 * refer to files already opened by a Unix client. You cannot just use
3480 * vn_writechk() and VOP_ACCESS() for two reasons.
3481 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
3482 * 2 - The owner is to be given access irrespective of mode bits for some
3483 * operations, so that processes that chmod after opening a file don't
3484 * break. I don't like this because it opens a security hole, but since
3485 * the nfs server opens a security hole the size of a barn door anyhow,
3486 * what the heck.
3487 *
3488 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
3489 * will return EPERM instead of EACCESS. EPERM is always an error.
3490 */
3491 int
3492 nfsrv_access(vp, flags, cred, rdonly, lwp, override)
3493 struct vnode *vp;
3494 int flags;
3495 kauth_cred_t cred;
3496 int rdonly;
3497 struct lwp *lwp;
3498 int override;
3499 {
3500 struct vattr vattr;
3501 int error;
3502 if (flags & VWRITE) {
3503 /* Just vn_writechk() changed to check rdonly */
3504 /*
3505 * Disallow write attempts on read-only file systems;
3506 * unless the file is a socket or a block or character
3507 * device resident on the file system.
3508 */
3509 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3510 switch (vp->v_type) {
3511 case VREG:
3512 case VDIR:
3513 case VLNK:
3514 return (EROFS);
3515 default:
3516 break;
3517 }
3518 }
3519
3520 /*
3521 * If the vnode is in use as a process's text,
3522 * we can't allow writing.
3523 */
3524 if (vp->v_flag & VTEXT)
3525 return (ETXTBSY);
3526 }
3527 error = VOP_GETATTR(vp, &vattr, cred, lwp);
3528 if (error)
3529 return (error);
3530 error = VOP_ACCESS(vp, flags, cred, lwp);
3531 /*
3532 * Allow certain operations for the owner (reads and writes
3533 * on files that are already open).
3534 */
3535 if (override && error == EACCES && kauth_cred_geteuid(cred) == vattr.va_uid)
3536 error = 0;
3537 return error;
3538 }
3539