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