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