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