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