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