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