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