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