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