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