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