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