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