xref: /illumos-gate/usr/src/uts/common/fs/nfs/nfs3_srv.c (revision dfdcac05)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2018 Nexenta Systems, Inc.
24  * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
25  * Copyright (c) 2013 by Delphix. All rights reserved.
26  */
27 
28 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
30 
31 
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/systm.h>
35 #include <sys/cred.h>
36 #include <sys/buf.h>
37 #include <sys/vfs.h>
38 #include <sys/vnode.h>
39 #include <sys/uio.h>
40 #include <sys/errno.h>
41 #include <sys/sysmacros.h>
42 #include <sys/statvfs.h>
43 #include <sys/kmem.h>
44 #include <sys/dirent.h>
45 #include <sys/cmn_err.h>
46 #include <sys/debug.h>
47 #include <sys/systeminfo.h>
48 #include <sys/flock.h>
49 #include <sys/nbmlock.h>
50 #include <sys/policy.h>
51 #include <sys/sdt.h>
52 
53 #include <rpc/types.h>
54 #include <rpc/auth.h>
55 #include <rpc/svc.h>
56 #include <rpc/rpc_rdma.h>
57 
58 #include <nfs/nfs.h>
59 #include <nfs/export.h>
60 #include <nfs/nfs_cmd.h>
61 
62 #include <sys/strsubr.h>
63 #include <sys/tsol/label.h>
64 #include <sys/tsol/tndb.h>
65 
66 #include <sys/zone.h>
67 
68 #include <inet/ip.h>
69 #include <inet/ip6.h>
70 
71 /*
72  * Zone global variables of NFSv3 server
73  */
74 typedef struct nfs3_srv {
75 	writeverf3	write3verf;
76 } nfs3_srv_t;
77 
78 /*
79  * These are the interface routines for the server side of the
80  * Network File System.  See the NFS version 3 protocol specification
81  * for a description of this interface.
82  */
83 
84 static int	sattr3_to_vattr(sattr3 *, struct vattr *);
85 static int	vattr_to_fattr3(struct vattr *, fattr3 *);
86 static int	vattr_to_wcc_attr(struct vattr *, wcc_attr *);
87 static void	vattr_to_pre_op_attr(struct vattr *, pre_op_attr *);
88 static void	vattr_to_wcc_data(struct vattr *, struct vattr *, wcc_data *);
89 static int	rdma_setup_read_data3(READ3args *, READ3resok *);
90 
91 extern int nfs_loaned_buffers;
92 
93 u_longlong_t nfs3_srv_caller_id;
94 
95 static nfs3_srv_t *
nfs3_get_srv(void)96 nfs3_get_srv(void)
97 {
98 	nfs_globals_t *ng = nfs_srv_getzg();
99 	nfs3_srv_t *srv = ng->nfs3_srv;
100 	ASSERT(srv != NULL);
101 	return (srv);
102 }
103 
104 /* ARGSUSED */
105 void
rfs3_getattr(GETATTR3args * args,GETATTR3res * resp,struct exportinfo * exi,struct svc_req * req,cred_t * cr,bool_t ro)106 rfs3_getattr(GETATTR3args *args, GETATTR3res *resp, struct exportinfo *exi,
107     struct svc_req *req, cred_t *cr, bool_t ro)
108 {
109 	int error;
110 	vnode_t *vp;
111 	struct vattr va;
112 
113 	vp = nfs3_fhtovp(&args->object, exi);
114 
115 	DTRACE_NFSV3_5(op__getattr__start, struct svc_req *, req,
116 	    cred_t *, cr, vnode_t *, vp, struct exportinfo *, exi,
117 	    GETATTR3args *, args);
118 
119 	if (vp == NULL) {
120 		error = ESTALE;
121 		goto out;
122 	}
123 
124 	va.va_mask = AT_ALL;
125 	error = rfs4_delegated_getattr(vp, &va, 0, cr);
126 
127 	if (!error) {
128 		/* Lie about the object type for a referral */
129 		if (vn_is_nfs_reparse(vp, cr))
130 			va.va_type = VLNK;
131 
132 		/* overflow error if time or size is out of range */
133 		error = vattr_to_fattr3(&va, &resp->resok.obj_attributes);
134 		if (error)
135 			goto out;
136 		resp->status = NFS3_OK;
137 
138 		DTRACE_NFSV3_5(op__getattr__done, struct svc_req *, req,
139 		    cred_t *, cr, vnode_t *, vp, struct exportinfo *, exi,
140 		    GETATTR3res *, resp);
141 
142 		VN_RELE(vp);
143 
144 		return;
145 	}
146 
147 out:
148 	if (curthread->t_flag & T_WOULDBLOCK) {
149 		curthread->t_flag &= ~T_WOULDBLOCK;
150 		resp->status = NFS3ERR_JUKEBOX;
151 	} else
152 		resp->status = puterrno3(error);
153 
154 	DTRACE_NFSV3_5(op__getattr__done, struct svc_req *, req,
155 	    cred_t *, cr, vnode_t *, vp, struct exportinfo *, exi,
156 	    GETATTR3res *, resp);
157 
158 	if (vp != NULL)
159 		VN_RELE(vp);
160 }
161 
162 void *
rfs3_getattr_getfh(GETATTR3args * args)163 rfs3_getattr_getfh(GETATTR3args *args)
164 {
165 
166 	return (&args->object);
167 }
168 
169 void
rfs3_setattr(SETATTR3args * args,SETATTR3res * resp,struct exportinfo * exi,struct svc_req * req,cred_t * cr,bool_t ro)170 rfs3_setattr(SETATTR3args *args, SETATTR3res *resp, struct exportinfo *exi,
171     struct svc_req *req, cred_t *cr, bool_t ro)
172 {
173 	int error;
174 	vnode_t *vp;
175 	struct vattr *bvap;
176 	struct vattr bva;
177 	struct vattr *avap;
178 	struct vattr ava;
179 	int flag;
180 	int in_crit = 0;
181 	struct flock64 bf;
182 	caller_context_t ct;
183 
184 	bvap = NULL;
185 	avap = NULL;
186 
187 	vp = nfs3_fhtovp(&args->object, exi);
188 
189 	DTRACE_NFSV3_5(op__setattr__start, struct svc_req *, req,
190 	    cred_t *, cr, vnode_t *, vp, struct exportinfo *, exi,
191 	    SETATTR3args *, args);
192 
193 	if (vp == NULL) {
194 		error = ESTALE;
195 		goto out;
196 	}
197 
198 	error = sattr3_to_vattr(&args->new_attributes, &ava);
199 	if (error)
200 		goto out;
201 
202 	if (is_system_labeled()) {
203 		bslabel_t *clabel = req->rq_label;
204 
205 		ASSERT(clabel != NULL);
206 		DTRACE_PROBE2(tx__rfs3__log__info__opsetattr__clabel, char *,
207 		    "got client label from request(1)", struct svc_req *, req);
208 
209 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
210 			if (!do_rfs_label_check(clabel, vp, EQUALITY_CHECK,
211 			    exi)) {
212 				resp->status = NFS3ERR_ACCES;
213 				goto out1;
214 			}
215 		}
216 	}
217 
218 	/*
219 	 * We need to specially handle size changes because of
220 	 * possible conflicting NBMAND locks. Get into critical
221 	 * region before VOP_GETATTR, so the size attribute is
222 	 * valid when checking conflicts.
223 	 *
224 	 * Also, check to see if the v4 side of the server has
225 	 * delegated this file.  If so, then we return JUKEBOX to
226 	 * allow the client to retrasmit its request.
227 	 */
228 	if (vp->v_type == VREG && (ava.va_mask & AT_SIZE)) {
229 		if (nbl_need_check(vp)) {
230 			nbl_start_crit(vp, RW_READER);
231 			in_crit = 1;
232 		}
233 	}
234 
235 	bva.va_mask = AT_ALL;
236 	error = rfs4_delegated_getattr(vp, &bva, 0, cr);
237 
238 	/*
239 	 * If we can't get the attributes, then we can't do the
240 	 * right access checking.  So, we'll fail the request.
241 	 */
242 	if (error)
243 		goto out;
244 
245 	bvap = &bva;
246 
247 	if (rdonly(ro, vp)) {
248 		resp->status = NFS3ERR_ROFS;
249 		goto out1;
250 	}
251 
252 	if (args->guard.check &&
253 	    (args->guard.obj_ctime.seconds != bva.va_ctime.tv_sec ||
254 	    args->guard.obj_ctime.nseconds != bva.va_ctime.tv_nsec)) {
255 		resp->status = NFS3ERR_NOT_SYNC;
256 		goto out1;
257 	}
258 
259 	if (args->new_attributes.mtime.set_it == SET_TO_CLIENT_TIME)
260 		flag = ATTR_UTIME;
261 	else
262 		flag = 0;
263 
264 	/*
265 	 * If the filesystem is exported with nosuid, then mask off
266 	 * the setuid and setgid bits.
267 	 */
268 	if ((ava.va_mask & AT_MODE) && vp->v_type == VREG &&
269 	    (exi->exi_export.ex_flags & EX_NOSUID))
270 		ava.va_mode &= ~(VSUID | VSGID);
271 
272 	ct.cc_sysid = 0;
273 	ct.cc_pid = 0;
274 	ct.cc_caller_id = nfs3_srv_caller_id;
275 	ct.cc_flags = CC_DONTBLOCK;
276 
277 	/*
278 	 * We need to specially handle size changes because it is
279 	 * possible for the client to create a file with modes
280 	 * which indicate read-only, but with the file opened for
281 	 * writing.  If the client then tries to set the size of
282 	 * the file, then the normal access checking done in
283 	 * VOP_SETATTR would prevent the client from doing so,
284 	 * although it should be legal for it to do so.  To get
285 	 * around this, we do the access checking for ourselves
286 	 * and then use VOP_SPACE which doesn't do the access
287 	 * checking which VOP_SETATTR does. VOP_SPACE can only
288 	 * operate on VREG files, let VOP_SETATTR handle the other
289 	 * extremely rare cases.
290 	 * Also the client should not be allowed to change the
291 	 * size of the file if there is a conflicting non-blocking
292 	 * mandatory lock in the region the change.
293 	 */
294 	if (vp->v_type == VREG && (ava.va_mask & AT_SIZE)) {
295 		if (in_crit) {
296 			u_offset_t offset;
297 			ssize_t length;
298 
299 			if (ava.va_size < bva.va_size) {
300 				offset = ava.va_size;
301 				length = bva.va_size - ava.va_size;
302 			} else {
303 				offset = bva.va_size;
304 				length = ava.va_size - bva.va_size;
305 			}
306 			if (nbl_conflict(vp, NBL_WRITE, offset, length, 0,
307 			    NULL)) {
308 				error = EACCES;
309 				goto out;
310 			}
311 		}
312 
313 		if (crgetuid(cr) == bva.va_uid && ava.va_size != bva.va_size) {
314 			ava.va_mask &= ~AT_SIZE;
315 			bf.l_type = F_WRLCK;
316 			bf.l_whence = 0;
317 			bf.l_start = (off64_t)ava.va_size;
318 			bf.l_len = 0;
319 			bf.l_sysid = 0;
320 			bf.l_pid = 0;
321 			error = VOP_SPACE(vp, F_FREESP, &bf, FWRITE,
322 			    (offset_t)ava.va_size, cr, &ct);
323 		}
324 	}
325 
326 	if (!error && ava.va_mask)
327 		error = VOP_SETATTR(vp, &ava, flag, cr, &ct);
328 
329 	/* check if a monitor detected a delegation conflict */
330 	if (error == EAGAIN && (ct.cc_flags & CC_WOULDBLOCK)) {
331 		resp->status = NFS3ERR_JUKEBOX;
332 		goto out1;
333 	}
334 
335 	ava.va_mask = AT_ALL;
336 	avap = rfs4_delegated_getattr(vp, &ava, 0, cr) ? NULL : &ava;
337 
338 	/*
339 	 * Force modified metadata out to stable storage.
340 	 */
341 	(void) VOP_FSYNC(vp, FNODSYNC, cr, &ct);
342 
343 	if (error)
344 		goto out;
345 
346 	if (in_crit)
347 		nbl_end_crit(vp);
348 
349 	resp->status = NFS3_OK;
350 	vattr_to_wcc_data(bvap, avap, &resp->resok.obj_wcc);
351 
352 	DTRACE_NFSV3_5(op__setattr__done, struct svc_req *, req,
353 	    cred_t *, cr, vnode_t *, vp, struct exportinfo *, exi,
354 	    SETATTR3res *, resp);
355 
356 	VN_RELE(vp);
357 
358 	return;
359 
360 out:
361 	if (curthread->t_flag & T_WOULDBLOCK) {
362 		curthread->t_flag &= ~T_WOULDBLOCK;
363 		resp->status = NFS3ERR_JUKEBOX;
364 	} else
365 		resp->status = puterrno3(error);
366 out1:
367 	DTRACE_NFSV3_5(op__setattr__done, struct svc_req *, req,
368 	    cred_t *, cr, vnode_t *, vp, struct exportinfo *, exi,
369 	    SETATTR3res *, resp);
370 
371 	if (vp != NULL) {
372 		if (in_crit)
373 			nbl_end_crit(vp);
374 		VN_RELE(vp);
375 	}
376 	vattr_to_wcc_data(bvap, avap, &resp->resfail.obj_wcc);
377 }
378 
379 void *
rfs3_setattr_getfh(SETATTR3args * args)380 rfs3_setattr_getfh(SETATTR3args *args)
381 {
382 
383 	return (&args->object);
384 }
385 
386 /* ARGSUSED */
387 void
rfs3_lookup(LOOKUP3args * args,LOOKUP3res * resp,struct exportinfo * exi,struct svc_req * req,cred_t * cr,bool_t ro)388 rfs3_lookup(LOOKUP3args *args, LOOKUP3res *resp, struct exportinfo *exi,
389     struct svc_req *req, cred_t *cr, bool_t ro)
390 {
391 	int error;
392 	vnode_t *vp;
393 	vnode_t *dvp;
394 	struct vattr *vap;
395 	struct vattr va;
396 	struct vattr *dvap;
397 	struct vattr dva;
398 	nfs_fh3 *fhp;
399 	struct sec_ol sec = {0, 0};
400 	bool_t publicfh_flag = FALSE, auth_weak = FALSE;
401 	struct sockaddr *ca;
402 	char *name = NULL;
403 
404 	dvap = NULL;
405 
406 	if (exi != NULL)
407 		exi_hold(exi);
408 
409 	/*
410 	 * Allow lookups from the root - the default
411 	 * location of the public filehandle.
412 	 */
413 	if (exi != NULL && (exi->exi_export.ex_flags & EX_PUBLIC)) {
414 		ASSERT3U(exi->exi_zoneid, ==, curzone->zone_id);
415 		dvp = ZONE_ROOTVP();
416 		VN_HOLD(dvp);
417 
418 		DTRACE_NFSV3_5(op__lookup__start, struct svc_req *, req,
419 		    cred_t *, cr, vnode_t *, dvp, struct exportinfo *, exi,
420 		    LOOKUP3args *, args);
421 	} else {
422 		dvp = nfs3_fhtovp(&args->what.dir, exi);
423 
424 		DTRACE_NFSV3_5(op__lookup__start, struct svc_req *, req,
425 		    cred_t *, cr, vnode_t *, dvp, struct exportinfo *, exi,
426 		    LOOKUP3args *, args);
427 
428 		if (dvp == NULL) {
429 			error = ESTALE;
430 			goto out;
431 		}
432 	}
433 
434 	dva.va_mask = AT_ALL;
435 	dvap = VOP_GETATTR(dvp, &dva, 0, cr, NULL) ? NULL : &dva;
436 
437 	if (args->what.name == nfs3nametoolong) {
438 		resp->status = NFS3ERR_NAMETOOLONG;
439 		goto out1;
440 	}
441 
442 	if (args->what.name == NULL || *(args->what.name) == '\0') {
443 		resp->status = NFS3ERR_ACCES;
444 		goto out1;
445 	}
446 
447 	fhp = &args->what.dir;
448 	ASSERT3U(curzone->zone_id, ==, exi->exi_zoneid); /* exi is non-NULL */
449 	if (strcmp(args->what.name, "..") == 0 &&
450 	    EQFID(&exi->exi_fid, FH3TOFIDP(fhp))) {
451 		if ((exi->exi_export.ex_flags & EX_NOHIDE) &&
452 		    ((dvp->v_flag & VROOT) || VN_IS_CURZONEROOT(dvp))) {
453 			/*
454 			 * special case for ".." and 'nohide'exported root
455 			 */
456 			if (rfs_climb_crossmnt(&dvp, &exi, cr) != 0) {
457 				resp->status = NFS3ERR_ACCES;
458 				goto out1;
459 			}
460 		} else {
461 			resp->status = NFS3ERR_NOENT;
462 			goto out1;
463 		}
464 	}
465 
466 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
467 	name = nfscmd_convname(ca, exi, args->what.name,
468 	    NFSCMD_CONV_INBOUND, MAXPATHLEN + 1);
469 
470 	if (name == NULL) {
471 		resp->status = NFS3ERR_ACCES;
472 		goto out1;
473 	}
474 
475 	/*
476 	 * If the public filehandle is used then allow
477 	 * a multi-component lookup
478 	 */
479 	if (PUBLIC_FH3(&args->what.dir)) {
480 		publicfh_flag = TRUE;
481 
482 		exi_rele(exi);
483 		exi = NULL;
484 
485 		error = rfs_publicfh_mclookup(name, dvp, cr, &vp,
486 		    &exi, &sec);
487 
488 		/*
489 		 * Since WebNFS may bypass MOUNT, we need to ensure this
490 		 * request didn't come from an unlabeled admin_low client.
491 		 */
492 		if (is_system_labeled() && error == 0) {
493 			int		addr_type;
494 			void		*ipaddr;
495 			tsol_tpc_t	*tp;
496 
497 			if (ca->sa_family == AF_INET) {
498 				addr_type = IPV4_VERSION;
499 				ipaddr = &((struct sockaddr_in *)ca)->sin_addr;
500 			} else if (ca->sa_family == AF_INET6) {
501 				addr_type = IPV6_VERSION;
502 				ipaddr = &((struct sockaddr_in6 *)
503 				    ca)->sin6_addr;
504 			}
505 			tp = find_tpc(ipaddr, addr_type, B_FALSE);
506 			if (tp == NULL || tp->tpc_tp.tp_doi !=
507 			    l_admin_low->tsl_doi || tp->tpc_tp.host_type !=
508 			    SUN_CIPSO) {
509 				VN_RELE(vp);
510 				error = EACCES;
511 			}
512 			if (tp != NULL)
513 				TPC_RELE(tp);
514 		}
515 	} else {
516 		error = VOP_LOOKUP(dvp, name, &vp,
517 		    NULL, 0, NULL, cr, NULL, NULL, NULL);
518 	}
519 
520 	if (name != args->what.name)
521 		kmem_free(name, MAXPATHLEN + 1);
522 
523 	if (error == 0 && vn_ismntpt(vp)) {
524 		error = rfs_cross_mnt(&vp, &exi);
525 		if (error)
526 			VN_RELE(vp);
527 	}
528 
529 	if (is_system_labeled() && error == 0) {
530 		bslabel_t *clabel = req->rq_label;
531 
532 		ASSERT(clabel != NULL);
533 		DTRACE_PROBE2(tx__rfs3__log__info__oplookup__clabel, char *,
534 		    "got client label from request(1)", struct svc_req *, req);
535 
536 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
537 			if (!do_rfs_label_check(clabel, dvp,
538 			    DOMINANCE_CHECK, exi)) {
539 				VN_RELE(vp);
540 				error = EACCES;
541 			}
542 		}
543 	}
544 
545 	dva.va_mask = AT_ALL;
546 	dvap = VOP_GETATTR(dvp, &dva, 0, cr, NULL) ? NULL : &dva;
547 
548 	if (error)
549 		goto out;
550 
551 	if (sec.sec_flags & SEC_QUERY) {
552 		error = makefh3_ol(&resp->resok.object, exi, sec.sec_index);
553 	} else {
554 		error = makefh3(&resp->resok.object, vp, exi);
555 		if (!error && publicfh_flag && !chk_clnt_sec(exi, req))
556 			auth_weak = TRUE;
557 	}
558 
559 	if (error) {
560 		VN_RELE(vp);
561 		goto out;
562 	}
563 
564 	va.va_mask = AT_ALL;
565 	vap = rfs4_delegated_getattr(vp, &va, 0, cr) ? NULL : &va;
566 
567 	VN_RELE(vp);
568 
569 	resp->status = NFS3_OK;
570 	vattr_to_post_op_attr(vap, &resp->resok.obj_attributes);
571 	vattr_to_post_op_attr(dvap, &resp->resok.dir_attributes);
572 
573 	/*
574 	 * If it's public fh, no 0x81, and client's flavor is
575 	 * invalid, set WebNFS status to WNFSERR_CLNT_FLAVOR now.
576 	 * Then set RPC status to AUTH_TOOWEAK in common_dispatch.
577 	 */
578 	if (auth_weak)
579 		resp->status = (enum nfsstat3)WNFSERR_CLNT_FLAVOR;
580 
581 	DTRACE_NFSV3_5(op__lookup__done, struct svc_req *, req,
582 	    cred_t *, cr, vnode_t *, dvp, struct exportinfo *, exi,
583 	    LOOKUP3res *, resp);
584 	VN_RELE(dvp);
585 	exi_rele(exi);
586 
587 	return;
588 
589 out:
590 	if (curthread->t_flag & T_WOULDBLOCK) {
591 		curthread->t_flag &= ~T_WOULDBLOCK;
592 		resp->status = NFS3ERR_JUKEBOX;
593 	} else
594 		resp->status = puterrno3(error);
595 out1:
596 	DTRACE_NFSV3_5(op__lookup__done, struct svc_req *, req,
597 	    cred_t *, cr, vnode_t *, dvp, struct exportinfo *, exi,
598 	    LOOKUP3res *, resp);
599 
600 	if (exi != NULL)
601 		exi_rele(exi);
602 
603 	if (dvp != NULL)
604 		VN_RELE(dvp);
605 	vattr_to_post_op_attr(dvap, &resp->resfail.dir_attributes);
606 
607 }
608 
609 void *
rfs3_lookup_getfh(LOOKUP3args * args)610 rfs3_lookup_getfh(LOOKUP3args *args)
611 {
612 
613 	return (&args->what.dir);
614 }
615 
616 /* ARGSUSED */
617 void
rfs3_access(ACCESS3args * args,ACCESS3res * resp,struct exportinfo * exi,struct svc_req * req,cred_t * cr,bool_t ro)618 rfs3_access(ACCESS3args *args, ACCESS3res *resp, struct exportinfo *exi,
619     struct svc_req *req, cred_t *cr, bool_t ro)
620 {
621 	int error;
622 	vnode_t *vp;
623 	struct vattr *vap;
624 	struct vattr va;
625 	int checkwriteperm;
626 	boolean_t dominant_label = B_FALSE;
627 	boolean_t equal_label = B_FALSE;
628 	boolean_t admin_low_client;
629 
630 	vap = NULL;
631 
632 	vp = nfs3_fhtovp(&args->object, exi);
633 
634 	DTRACE_NFSV3_5(op__access__start, struct svc_req *, req,
635 	    cred_t *, cr, vnode_t *, vp, struct exportinfo *, exi,
636 	    ACCESS3args *, args);
637 
638 	if (vp == NULL) {
639 		error = ESTALE;
640 		goto out;
641 	}
642 
643 	/*
644 	 * If the file system is exported read only, it is not appropriate
645 	 * to check write permissions for regular files and directories.
646 	 * Special files are interpreted by the client, so the underlying
647 	 * permissions are sent back to the client for interpretation.
648 	 */
649 	if (rdonly(ro, vp) && (vp->v_type == VREG || vp->v_type == VDIR))
650 		checkwriteperm = 0;
651 	else
652 		checkwriteperm = 1;
653 
654 	/*
655 	 * We need the mode so that we can correctly determine access
656 	 * permissions relative to a mandatory lock file.  Access to
657 	 * mandatory lock files is denied on the server, so it might
658 	 * as well be reflected to the server during the open.
659 	 */
660 	va.va_mask = AT_MODE;
661 	error = VOP_GETATTR(vp, &va, 0, cr, NULL);
662 	if (error)
663 		goto out;
664 
665 	vap = &va;
666 
667 	resp->resok.access = 0;
668 
669 	if (is_system_labeled()) {
670 		bslabel_t *clabel = req->rq_label;
671 
672 		ASSERT(clabel != NULL);
673 		DTRACE_PROBE2(tx__rfs3__log__info__opaccess__clabel, char *,
674 		    "got client label from request(1)", struct svc_req *, req);
675 
676 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
677 			if ((equal_label = do_rfs_label_check(clabel, vp,
678 			    EQUALITY_CHECK, exi)) == B_FALSE) {
679 				dominant_label = do_rfs_label_check(clabel,
680 				    vp, DOMINANCE_CHECK, exi);
681 			} else
682 				dominant_label = B_TRUE;
683 			admin_low_client = B_FALSE;
684 		} else
685 			admin_low_client = B_TRUE;
686 	}
687 
688 	if (args->access & ACCESS3_READ) {
689 		error = VOP_ACCESS(vp, VREAD, 0, cr, NULL);
690 		if (error) {
691 			if (curthread->t_flag & T_WOULDBLOCK)
692 				goto out;
693 		} else if (!MANDLOCK(vp, va.va_mode) &&
694 		    (!is_system_labeled() || admin_low_client ||
695 		    dominant_label))
696 			resp->resok.access |= ACCESS3_READ;
697 	}
698 	if ((args->access & ACCESS3_LOOKUP) && vp->v_type == VDIR) {
699 		error = VOP_ACCESS(vp, VEXEC, 0, cr, NULL);
700 		if (error) {
701 			if (curthread->t_flag & T_WOULDBLOCK)
702 				goto out;
703 		} else if (!is_system_labeled() || admin_low_client ||
704 		    dominant_label)
705 			resp->resok.access |= ACCESS3_LOOKUP;
706 	}
707 	if (checkwriteperm &&
708 	    (args->access & (ACCESS3_MODIFY|ACCESS3_EXTEND))) {
709 		error = VOP_ACCESS(vp, VWRITE, 0, cr, NULL);
710 		if (error) {
711 			if (curthread->t_flag & T_WOULDBLOCK)
712 				goto out;
713 		} else if (!MANDLOCK(vp, va.va_mode) &&
714 		    (!is_system_labeled() || admin_low_client || equal_label)) {
715 			resp->resok.access |=
716 			    (args->access & (ACCESS3_MODIFY|ACCESS3_EXTEND));
717 		}
718 	}
719 	if (checkwriteperm &&
720 	    (args->access & ACCESS3_DELETE) && vp->v_type == VDIR) {
721 		error = VOP_ACCESS(vp, VWRITE, 0, cr, NULL);
722 		if (error) {
723 			if (curthread->t_flag & T_WOULDBLOCK)
724 				goto out;
725 		} else if (!is_system_labeled() || admin_low_client ||
726 		    equal_label)
727 			resp->resok.access |= ACCESS3_DELETE;
728 	}
729 	if (args->access & ACCESS3_EXECUTE) {
730 		error = VOP_ACCESS(vp, VEXEC, 0, cr, NULL);
731 		if (error) {
732 			if (curthread->t_flag & T_WOULDBLOCK)
733 				goto out;
734 		} else if (!MANDLOCK(vp, va.va_mode) &&
735 		    (!is_system_labeled() || admin_low_client ||
736 		    dominant_label))
737 			resp->resok.access |= ACCESS3_EXECUTE;
738 	}
739 
740 	va.va_mask = AT_ALL;
741 	vap = rfs4_delegated_getattr(vp, &va, 0, cr) ? NULL : &va;
742 
743 	resp->status = NFS3_OK;
744 	vattr_to_post_op_attr(vap, &resp->resok.obj_attributes);
745 
746 	DTRACE_NFSV3_5(op__access__done, struct svc_req *, req,
747 	    cred_t *, cr, vnode_t *, vp, struct exportinfo *, exi,
748 	    ACCESS3res *, resp);
749 
750 	VN_RELE(vp);
751 
752 	return;
753 
754 out:
755 	if (curthread->t_flag & T_WOULDBLOCK) {
756 		curthread->t_flag &= ~T_WOULDBLOCK;
757 		resp->status = NFS3ERR_JUKEBOX;
758 	} else
759 		resp->status = puterrno3(error);
760 	DTRACE_NFSV3_5(op__access__done, struct svc_req *, req,
761 	    cred_t *, cr, vnode_t *, vp, struct exportinfo *, exi,
762 	    ACCESS3res *, resp);
763 	if (vp != NULL)
764 		VN_RELE(vp);
765 	vattr_to_post_op_attr(vap, &resp->resfail.obj_attributes);
766 }
767 
768 void *
rfs3_access_getfh(ACCESS3args * args)769 rfs3_access_getfh(ACCESS3args *args)
770 {
771 
772 	return (&args->object);
773 }
774 
775 /* ARGSUSED */
776 void
rfs3_readlink(READLINK3args * args,READLINK3res * resp,struct exportinfo * exi,struct svc_req * req,cred_t * cr,bool_t ro)777 rfs3_readlink(READLINK3args *args, READLINK3res *resp, struct exportinfo *exi,
778     struct svc_req *req, cred_t *cr, bool_t ro)
779 {
780 	int error;
781 	vnode_t *vp;
782 	struct vattr *vap;
783 	struct vattr va;
784 	struct iovec iov;
785 	struct uio uio;
786 	char *data;
787 	struct sockaddr *ca;
788 	char *name = NULL;
789 	int is_referral = 0;
790 
791 	vap = NULL;
792 
793 	vp = nfs3_fhtovp(&args->symlink, exi);
794 
795 	DTRACE_NFSV3_5(op__readlink__start, struct svc_req *, req,
796 	    cred_t *, cr, vnode_t *, vp, struct exportinfo *, exi,
797 	    READLINK3args *, args);
798 
799 	if (vp == NULL) {
800 		error = ESTALE;
801 		goto out;
802 	}
803 
804 	va.va_mask = AT_ALL;
805 	error = VOP_GETATTR(vp, &va, 0, cr, NULL);
806 	if (error)
807 		goto out;
808 
809 	vap = &va;
810 
811 	/* We lied about the object type for a referral */
812 	if (vn_is_nfs_reparse(vp, cr))
813 		is_referral = 1;
814 
815 	if (vp->v_type != VLNK && !is_referral) {
816 		resp->status = NFS3ERR_INVAL;
817 		goto out1;
818 	}
819 
820 	if (MANDLOCK(vp, va.va_mode)) {
821 		resp->status = NFS3ERR_ACCES;
822 		goto out1;
823 	}
824 
825 	if (is_system_labeled()) {
826 		bslabel_t *clabel = req->rq_label;
827 
828 		ASSERT(clabel != NULL);
829 		DTRACE_PROBE2(tx__rfs3__log__info__opreadlink__clabel, char *,
830 		    "got client label from request(1)", struct svc_req *, req);
831 
832 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
833 			if (!do_rfs_label_check(clabel, vp, DOMINANCE_CHECK,
834 			    exi)) {
835 				resp->status = NFS3ERR_ACCES;
836 				goto out1;
837 			}
838 		}
839 	}
840 
841 	data = kmem_alloc(MAXPATHLEN + 1, KM_SLEEP);
842 
843 	if (is_referral) {
844 		char *s;
845 		size_t strsz;
846 		kstat_named_t *stat = exi->exi_ne->ne_globals->svstat[NFS_V3];
847 
848 		/* Get an artificial symlink based on a referral */
849 		s = build_symlink(vp, cr, &strsz);
850 		stat[NFS_REFERLINKS].value.ui64++;
851 		DTRACE_PROBE2(nfs3serv__func__referral__reflink,
852 		    vnode_t *, vp, char *, s);
853 		if (s == NULL)
854 			error = EINVAL;
855 		else {
856 			error = 0;
857 			(void) strlcpy(data, s, MAXPATHLEN + 1);
858 			kmem_free(s, strsz);
859 		}
860 
861 	} else {
862 
863 		iov.iov_base = data;
864 		iov.iov_len = MAXPATHLEN;
865 		uio.uio_iov = &iov;
866 		uio.uio_iovcnt = 1;
867 		uio.uio_segflg = UIO_SYSSPACE;
868 		uio.uio_extflg = UIO_COPY_CACHED;
869 		uio.uio_loffset = 0;
870 		uio.uio_resid = MAXPATHLEN;
871 
872 		error = VOP_READLINK(vp, &uio, cr, NULL);
873 
874 		if (!error)
875 			*(data + MAXPATHLEN - uio.uio_resid) = '\0';
876 	}
877 
878 	va.va_mask = AT_ALL;
879 	vap = VOP_GETATTR(vp, &va, 0, cr, NULL) ? NULL : &va;
880 
881 	/* Lie about object type again just to be consistent */
882 	if (is_referral && vap != NULL)
883 		vap->va_type = VLNK;
884 
885 #if 0 /* notyet */
886 	/*
887 	 * Don't do this.  It causes local disk writes when just
888 	 * reading the file and the overhead is deemed larger
889 	 * than the benefit.
890 	 */
891 	/*
892 	 * Force modified metadata out to stable storage.
893 	 */
894 	(void) VOP_FSYNC(vp, FNODSYNC, cr, NULL);
895 #endif
896 
897 	if (error) {
898 		kmem_free(data, MAXPATHLEN + 1);
899 		goto out;
900 	}
901 
902 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
903 	name = nfscmd_convname(ca, exi, data, NFSCMD_CONV_OUTBOUND,
904 	    MAXPATHLEN + 1);
905 
906 	if (name == NULL) {
907 		/*
908 		 * Even though the conversion failed, we return
909 		 * something. We just don't translate it.
910 		 */
911 		name = data;
912 	}
913 
914 	resp->status = NFS3_OK;
915 	vattr_to_post_op_attr(vap, &resp->resok.symlink_attributes);
916 	resp->resok.data = name;
917 
918 	DTRACE_NFSV3_5(op__readlink__done, struct svc_req *, req,
919 	    cred_t *, cr, vnode_t *, vp, struct exportinfo *, exi,
920 	    READLINK3res *, resp);
921 	VN_RELE(vp);
922 
923 	if (name != data)
924 		kmem_free(data, MAXPATHLEN + 1);
925 
926 	return;
927 
928 out:
929 	if (curthread->t_flag & T_WOULDBLOCK) {
930 		curthread->t_flag &= ~T_WOULDBLOCK;
931 		resp->status = NFS3ERR_JUKEBOX;
932 	} else
933 		resp->status = puterrno3(error);
934 out1:
935 	DTRACE_NFSV3_5(op__readlink__done, struct svc_req *, req,
936 	    cred_t *, cr, vnode_t *, vp, struct exportinfo *, exi,
937 	    READLINK3res *, resp);
938 	if (vp != NULL)
939 		VN_RELE(vp);
940 	vattr_to_post_op_attr(vap, &resp->resfail.symlink_attributes);
941 }
942 
943 void *
rfs3_readlink_getfh(READLINK3args * args)944 rfs3_readlink_getfh(READLINK3args *args)
945 {
946 
947 	return (&args->symlink);
948 }
949 
950 void
rfs3_readlink_free(READLINK3res * resp)951 rfs3_readlink_free(READLINK3res *resp)
952 {
953 
954 	if (resp->status == NFS3_OK)
955 		kmem_free(resp->resok.data, MAXPATHLEN + 1);
956 }
957 
958 /*
959  * Server routine to handle read
960  * May handle RDMA data as well as mblks
961  */
962 /* ARGSUSED */
963 void
rfs3_read(READ3args * args,READ3res * resp,struct exportinfo * exi,struct svc_req * req,cred_t * cr,bool_t ro)964 rfs3_read(READ3args *args, READ3res *resp, struct exportinfo *exi,
965     struct svc_req *req, cred_t *cr, bool_t ro)
966 {
967 	int error;
968 	vnode_t *vp;
969 	struct vattr *vap;
970 	struct vattr va;
971 	struct iovec iov, *iovp = NULL;
972 	int iovcnt;
973 	struct uio uio;
974 	u_offset_t offset;
975 	mblk_t *mp = NULL;
976 	int in_crit = 0;
977 	int need_rwunlock = 0;
978 	caller_context_t ct;
979 	int rdma_used = 0;
980 	int loaned_buffers;
981 	struct uio *uiop;
982 
983 	vap = NULL;
984 
985 	vp = nfs3_fhtovp(&args->file, exi);
986 
987 	DTRACE_NFSV3_5(op__read__start, struct svc_req *, req,
988 	    cred_t *, cr, vnode_t *, vp, struct exportinfo *, exi,
989 	    READ3args *, args);
990 
991 
992 	if (vp == NULL) {
993 		error = ESTALE;
994 		goto out;
995 	}
996 
997 	if (args->wlist) {
998 		if (args->count > clist_len(args->wlist)) {
999 			error = EINVAL;
1000 			goto out;
1001 		}
1002 		rdma_used = 1;
1003 	}
1004 
1005 	/* use loaned buffers for TCP */
1006 	loaned_buffers = (nfs_loaned_buffers && !rdma_used) ? 1 : 0;
1007 
1008 	if (is_system_labeled()) {
1009 		bslabel_t *clabel = req->rq_label;
1010 
1011 		ASSERT(clabel != NULL);
1012 		DTRACE_PROBE2(tx__rfs3__log__info__opread__clabel, char *,
1013 		    "got client label from request(1)", struct svc_req *, req);
1014 
1015 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
1016 			if (!do_rfs_label_check(clabel, vp, DOMINANCE_CHECK,
1017 			    exi)) {
1018 				resp->status = NFS3ERR_ACCES;
1019 				goto out1;
1020 			}
1021 		}
1022 	}
1023 
1024 	ct.cc_sysid = 0;
1025 	ct.cc_pid = 0;
1026 	ct.cc_caller_id = nfs3_srv_caller_id;
1027 	ct.cc_flags = CC_DONTBLOCK;
1028 
1029 	/*
1030 	 * Enter the critical region before calling VOP_RWLOCK
1031 	 * to avoid a deadlock with write requests.
1032 	 */
1033 	if (nbl_need_check(vp)) {
1034 		nbl_start_crit(vp, RW_READER);
1035 		in_crit = 1;
1036 		if (nbl_conflict(vp, NBL_READ, args->offset, args->count, 0,
1037 		    NULL)) {
1038 			error = EACCES;
1039 			goto out;
1040 		}
1041 	}
1042 
1043 	error = VOP_RWLOCK(vp, V_WRITELOCK_FALSE, &ct);
1044 
1045 	/* check if a monitor detected a delegation conflict */
1046 	if (error == EAGAIN && (ct.cc_flags & CC_WOULDBLOCK)) {
1047 		resp->status = NFS3ERR_JUKEBOX;
1048 		goto out1;
1049 	}
1050 
1051 	need_rwunlock = 1;
1052 
1053 	va.va_mask = AT_ALL;
1054 	error = VOP_GETATTR(vp, &va, 0, cr, &ct);
1055 
1056 	/*
1057 	 * If we can't get the attributes, then we can't do the
1058 	 * right access checking.  So, we'll fail the request.
1059 	 */
1060 	if (error)
1061 		goto out;
1062 
1063 	vap = &va;
1064 
1065 	if (vp->v_type != VREG) {
1066 		resp->status = NFS3ERR_INVAL;
1067 		goto out1;
1068 	}
1069 
1070 	if (crgetuid(cr) != va.va_uid) {
1071 		error = VOP_ACCESS(vp, VREAD, 0, cr, &ct);
1072 		if (error) {
1073 			if (curthread->t_flag & T_WOULDBLOCK)
1074 				goto out;
1075 			error = VOP_ACCESS(vp, VEXEC, 0, cr, &ct);
1076 			if (error)
1077 				goto out;
1078 		}
1079 	}
1080 
1081 	if (MANDLOCK(vp, va.va_mode)) {
1082 		resp->status = NFS3ERR_ACCES;
1083 		goto out1;
1084 	}
1085 
1086 	offset = args->offset;
1087 	if (offset >= va.va_size) {
1088 		VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, &ct);
1089 		if (in_crit)
1090 			nbl_end_crit(vp);
1091 		resp->status = NFS3_OK;
1092 		vattr_to_post_op_attr(vap, &resp->resok.file_attributes);
1093 		resp->resok.count = 0;
1094 		resp->resok.eof = TRUE;
1095 		resp->resok.data.data_len = 0;
1096 		resp->resok.data.data_val = NULL;
1097 		resp->resok.data.mp = NULL;
1098 		/* RDMA */
1099 		resp->resok.wlist = args->wlist;
1100 		resp->resok.wlist_len = resp->resok.count;
1101 		if (resp->resok.wlist)
1102 			clist_zero_len(resp->resok.wlist);
1103 		goto done;
1104 	}
1105 
1106 	if (args->count == 0) {
1107 		VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, &ct);
1108 		if (in_crit)
1109 			nbl_end_crit(vp);
1110 		resp->status = NFS3_OK;
1111 		vattr_to_post_op_attr(vap, &resp->resok.file_attributes);
1112 		resp->resok.count = 0;
1113 		resp->resok.eof = FALSE;
1114 		resp->resok.data.data_len = 0;
1115 		resp->resok.data.data_val = NULL;
1116 		resp->resok.data.mp = NULL;
1117 		/* RDMA */
1118 		resp->resok.wlist = args->wlist;
1119 		resp->resok.wlist_len = resp->resok.count;
1120 		if (resp->resok.wlist)
1121 			clist_zero_len(resp->resok.wlist);
1122 		goto done;
1123 	}
1124 
1125 	/*
1126 	 * do not allocate memory more the max. allowed
1127 	 * transfer size
1128 	 */
1129 	if (args->count > rfs3_tsize(req))
1130 		args->count = rfs3_tsize(req);
1131 
1132 	if (loaned_buffers) {
1133 		uiop = (uio_t *)rfs_setup_xuio(vp);
1134 		ASSERT(uiop != NULL);
1135 		uiop->uio_segflg = UIO_SYSSPACE;
1136 		uiop->uio_loffset = args->offset;
1137 		uiop->uio_resid = args->count;
1138 
1139 		/* Jump to do the read if successful */
1140 		if (VOP_REQZCBUF(vp, UIO_READ, (xuio_t *)uiop, cr, &ct) == 0) {
1141 			/*
1142 			 * Need to hold the vnode until after VOP_RETZCBUF()
1143 			 * is called.
1144 			 */
1145 			VN_HOLD(vp);
1146 			goto doio_read;
1147 		}
1148 
1149 		DTRACE_PROBE2(nfss__i__reqzcbuf_failed, int,
1150 		    uiop->uio_loffset, int, uiop->uio_resid);
1151 
1152 		uiop->uio_extflg = 0;
1153 		/* failure to setup for zero copy */
1154 		rfs_free_xuio((void *)uiop);
1155 		loaned_buffers = 0;
1156 	}
1157 
1158 	/*
1159 	 * If returning data via RDMA Write, then grab the chunk list.
1160 	 * If we aren't returning READ data w/RDMA_WRITE, then grab
1161 	 * a mblk.
1162 	 */
1163 	if (rdma_used) {
1164 		(void) rdma_get_wchunk(req, &iov, args->wlist);
1165 		uio.uio_iov = &iov;
1166 		uio.uio_iovcnt = 1;
1167 	} else {
1168 		/*
1169 		 * mp will contain the data to be sent out in the read reply.
1170 		 * For UDP, this will be freed after the reply has been sent
1171 		 * out by the driver.  For TCP, it will be freed after the last
1172 		 * segment associated with the reply has been ACKed by the
1173 		 * client.
1174 		 */
1175 		mp = rfs_read_alloc(args->count, &iovp, &iovcnt);
1176 		uio.uio_iov = iovp;
1177 		uio.uio_iovcnt = iovcnt;
1178 	}
1179 
1180 	uio.uio_segflg = UIO_SYSSPACE;
1181 	uio.uio_extflg = UIO_COPY_CACHED;
1182 	uio.uio_loffset = args->offset;
1183 	uio.uio_resid = args->count;
1184 	uiop = &uio;
1185 
1186 doio_read:
1187 	error = VOP_READ(vp, uiop, 0, cr, &ct);
1188 
1189 	if (error) {
1190 		if (mp)
1191 			freemsg(mp);
1192 		/* check if a monitor detected a delegation conflict */
1193 		if (error == EAGAIN && (ct.cc_flags & CC_WOULDBLOCK)) {
1194 			resp->status = NFS3ERR_JUKEBOX;
1195 			goto out1;
1196 		}
1197 		goto out;
1198 	}
1199 
1200 	/* make mblk using zc buffers */
1201 	if (loaned_buffers) {
1202 		mp = uio_to_mblk(uiop);
1203 		ASSERT(mp != NULL);
1204 	}
1205 
1206 	va.va_mask = AT_ALL;
1207 	error = VOP_GETATTR(vp, &va, 0, cr, &ct);
1208 
1209 	if (error)
1210 		vap = NULL;
1211 	else
1212 		vap = &va;
1213 
1214 	VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, &ct);
1215 
1216 	if (in_crit)
1217 		nbl_end_crit(vp);
1218 
1219 	resp->status = NFS3_OK;
1220 	vattr_to_post_op_attr(vap, &resp->resok.file_attributes);
1221 	resp->resok.count = args->count - uiop->uio_resid;
1222 	if (!error && offset + resp->resok.count == va.va_size)
1223 		resp->resok.eof = TRUE;
1224 	else
1225 		resp->resok.eof = FALSE;
1226 	resp->resok.data.data_len = resp->resok.count;
1227 
1228 	if (mp)
1229 		rfs_rndup_mblks(mp, resp->resok.count, loaned_buffers);
1230 
1231 	resp->resok.data.mp = mp;
1232 	resp->resok.size = (uint_t)args->count;
1233 
1234 	if (rdma_used) {
1235 		resp->resok.data.data_val = (caddr_t)iov.iov_base;
1236 		if (!rdma_setup_read_data3(args, &(resp->resok))) {
1237 			resp->status = NFS3ERR_INVAL;
1238 		}
1239 	} else {
1240 		resp->resok.data.data_val = (caddr_t)mp->b_datap->db_base;
1241 		(resp->resok).wlist = NULL;
1242 	}
1243 
1244 done:
1245 	DTRACE_NFSV3_5(op__read__done, struct svc_req *, req,
1246 	    cred_t *, cr, vnode_t *, vp, struct exportinfo *, exi,
1247 	    READ3res *, resp);
1248 
1249 	VN_RELE(vp);
1250 
1251 	if (iovp != NULL)
1252 		kmem_free(iovp, iovcnt * sizeof (struct iovec));
1253 
1254 	return;
1255 
1256 out:
1257 	if (curthread->t_flag & T_WOULDBLOCK) {
1258 		curthread->t_flag &= ~T_WOULDBLOCK;
1259 		resp->status = NFS3ERR_JUKEBOX;
1260 	} else
1261 		resp->status = puterrno3(error);
1262 out1:
1263 	DTRACE_NFSV3_5(op__read__done, struct svc_req *, req,
1264 	    cred_t *, cr, vnode_t *, vp, struct exportinfo *, exi,
1265 	    READ3res *, resp);
1266 
1267 	if (vp != NULL) {
1268 		if (need_rwunlock)
1269 			VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, &ct);
1270 		if (in_crit)
1271 			nbl_end_crit(vp);
1272 		VN_RELE(vp);
1273 	}
1274 	vattr_to_post_op_attr(vap, &resp->resfail.file_attributes);
1275 
1276 	if (iovp != NULL)
1277 		kmem_free(iovp, iovcnt * sizeof (struct iovec));
1278 }
1279 
1280 void
rfs3_read_free(READ3res * resp)1281 rfs3_read_free(READ3res *resp)
1282 {
1283 	mblk_t *mp;
1284 
1285 	if (resp->status == NFS3_OK) {
1286 		mp = resp->resok.data.mp;
1287 		if (mp != NULL)
1288 			freemsg(mp);
1289 	}
1290 }
1291 
1292 void *
rfs3_read_getfh(READ3args * args)1293 rfs3_read_getfh(READ3args *args)
1294 {
1295 
1296 	return (&args->file);
1297 }
1298 
1299 #define	MAX_IOVECS	12
1300 
1301 #ifdef DEBUG
1302 static int rfs3_write_hits = 0;
1303 static int rfs3_write_misses = 0;
1304 #endif
1305 
1306 void
rfs3_write(WRITE3args * args,WRITE3res * resp,struct exportinfo * exi,struct svc_req * req,cred_t * cr,bool_t ro)1307 rfs3_write(WRITE3args *args, WRITE3res *resp, struct exportinfo *exi,
1308     struct svc_req *req, cred_t *cr, bool_t ro)
1309 {
1310 	nfs3_srv_t *ns;
1311 	int error;
1312 	vnode_t *vp;
1313 	struct vattr *bvap = NULL;
1314 	struct vattr bva;
1315 	struct vattr *avap = NULL;
1316 	struct vattr ava;
1317 	u_offset_t rlimit;
1318 	struct uio uio;
1319 	struct iovec iov[MAX_IOVECS];
1320 	mblk_t *m;
1321 	struct iovec *iovp;
1322 	int iovcnt;
1323 	int ioflag;
1324 	cred_t *savecred;
1325 	int in_crit = 0;
1326 	int rwlock_ret = -1;
1327 	caller_context_t ct;
1328 
1329 	vp = nfs3_fhtovp(&args->file, exi);
1330 
1331 	DTRACE_NFSV3_5(op__write__start, struct svc_req *, req,
1332 	    cred_t *, cr, vnode_t *, vp, struct exportinfo *, exi,
1333 	    WRITE3args *, args);
1334 
1335 	if (vp == NULL) {
1336 		error = ESTALE;
1337 		goto err;
1338 	}
1339 
1340 	ASSERT3U(curzone->zone_id, ==, exi->exi_zoneid); /* exi is non-NULL. */
1341 	ns = nfs3_get_srv();
1342 
1343 	if (is_system_labeled()) {
1344 		bslabel_t *clabel = req->rq_label;
1345 
1346 		ASSERT(clabel != NULL);
1347 		DTRACE_PROBE2(tx__rfs3__log__info__opwrite__clabel, char *,
1348 		    "got client label from request(1)", struct svc_req *, req);
1349 
1350 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
1351 			if (!do_rfs_label_check(clabel, vp, EQUALITY_CHECK,
1352 			    exi)) {
1353 				resp->status = NFS3ERR_ACCES;
1354 				goto err1;
1355 			}
1356 		}
1357 	}
1358 
1359 	ct.cc_sysid = 0;
1360 	ct.cc_pid = 0;
1361 	ct.cc_caller_id = nfs3_srv_caller_id;
1362 	ct.cc_flags = CC_DONTBLOCK;
1363 
1364 	/*
1365 	 * We have to enter the critical region before calling VOP_RWLOCK
1366 	 * to avoid a deadlock with ufs.
1367 	 */
1368 	if (nbl_need_check(vp)) {
1369 		nbl_start_crit(vp, RW_READER);
1370 		in_crit = 1;
1371 		if (nbl_conflict(vp, NBL_WRITE, args->offset, args->count, 0,
1372 		    NULL)) {
1373 			error = EACCES;
1374 			goto err;
1375 		}
1376 	}
1377 
1378 	rwlock_ret = VOP_RWLOCK(vp, V_WRITELOCK_TRUE, &ct);
1379 
1380 	/* check if a monitor detected a delegation conflict */
1381 	if (rwlock_ret == EAGAIN && (ct.cc_flags & CC_WOULDBLOCK)) {
1382 		resp->status = NFS3ERR_JUKEBOX;
1383 		rwlock_ret = -1;
1384 		goto err1;
1385 	}
1386 
1387 
1388 	bva.va_mask = AT_ALL;
1389 	error = VOP_GETATTR(vp, &bva, 0, cr, &ct);
1390 
1391 	/*
1392 	 * If we can't get the attributes, then we can't do the
1393 	 * right access checking.  So, we'll fail the request.
1394 	 */
1395 	if (error)
1396 		goto err;
1397 
1398 	bvap = &bva;
1399 	avap = bvap;
1400 
1401 	if (args->count != args->data.data_len) {
1402 		resp->status = NFS3ERR_INVAL;
1403 		goto err1;
1404 	}
1405 
1406 	if (rdonly(ro, vp)) {
1407 		resp->status = NFS3ERR_ROFS;
1408 		goto err1;
1409 	}
1410 
1411 	if (vp->v_type != VREG) {
1412 		resp->status = NFS3ERR_INVAL;
1413 		goto err1;
1414 	}
1415 
1416 	if (crgetuid(cr) != bva.va_uid &&
1417 	    (error = VOP_ACCESS(vp, VWRITE, 0, cr, &ct)))
1418 		goto err;
1419 
1420 	if (MANDLOCK(vp, bva.va_mode)) {
1421 		resp->status = NFS3ERR_ACCES;
1422 		goto err1;
1423 	}
1424 
1425 	if (args->count == 0) {
1426 		resp->status = NFS3_OK;
1427 		vattr_to_wcc_data(bvap, avap, &resp->resok.file_wcc);
1428 		resp->resok.count = 0;
1429 		resp->resok.committed = args->stable;
1430 		resp->resok.verf = ns->write3verf;
1431 		goto out;
1432 	}
1433 
1434 	if (args->mblk != NULL) {
1435 		iovcnt = 0;
1436 		for (m = args->mblk; m != NULL; m = m->b_cont)
1437 			iovcnt++;
1438 		if (iovcnt <= MAX_IOVECS) {
1439 #ifdef DEBUG
1440 			rfs3_write_hits++;
1441 #endif
1442 			iovp = iov;
1443 		} else {
1444 #ifdef DEBUG
1445 			rfs3_write_misses++;
1446 #endif
1447 			iovp = kmem_alloc(sizeof (*iovp) * iovcnt, KM_SLEEP);
1448 		}
1449 		mblk_to_iov(args->mblk, iovcnt, iovp);
1450 
1451 	} else if (args->rlist != NULL) {
1452 		iovcnt = 1;
1453 		iovp = iov;
1454 		iovp->iov_base = (char *)((args->rlist)->u.c_daddr3);
1455 		iovp->iov_len = args->count;
1456 	} else {
1457 		iovcnt = 1;
1458 		iovp = iov;
1459 		iovp->iov_base = args->data.data_val;
1460 		iovp->iov_len = args->count;
1461 	}
1462 
1463 	uio.uio_iov = iovp;
1464 	uio.uio_iovcnt = iovcnt;
1465 
1466 	uio.uio_segflg = UIO_SYSSPACE;
1467 	uio.uio_extflg = UIO_COPY_DEFAULT;
1468 	uio.uio_loffset = args->offset;
1469 	uio.uio_resid = args->count;
1470 	uio.uio_llimit = curproc->p_fsz_ctl;
1471 	rlimit = uio.uio_llimit - args->offset;
1472 	if (rlimit < (u_offset_t)uio.uio_resid)
1473 		uio.uio_resid = (int)rlimit;
1474 
1475 	if (args->stable == UNSTABLE)
1476 		ioflag = 0;
1477 	else if (args->stable == FILE_SYNC)
1478 		ioflag = FSYNC;
1479 	else if (args->stable == DATA_SYNC)
1480 		ioflag = FDSYNC;
1481 	else {
1482 		if (iovp != iov)
1483 			kmem_free(iovp, sizeof (*iovp) * iovcnt);
1484 		resp->status = NFS3ERR_INVAL;
1485 		goto err1;
1486 	}
1487 
1488 	/*
1489 	 * We're changing creds because VM may fault and we need
1490 	 * the cred of the current thread to be used if quota
1491 	 * checking is enabled.
1492 	 */
1493 	savecred = curthread->t_cred;
1494 	curthread->t_cred = cr;
1495 	error = VOP_WRITE(vp, &uio, ioflag, cr, &ct);
1496 	curthread->t_cred = savecred;
1497 
1498 	if (iovp != iov)
1499 		kmem_free(iovp, sizeof (*iovp) * iovcnt);
1500 
1501 	/* check if a monitor detected a delegation conflict */
1502 	if (error == EAGAIN && (ct.cc_flags & CC_WOULDBLOCK)) {
1503 		resp->status = NFS3ERR_JUKEBOX;
1504 		goto err1;
1505 	}
1506 
1507 	ava.va_mask = AT_ALL;
1508 	avap = VOP_GETATTR(vp, &ava, 0, cr, &ct) ? NULL : &ava;
1509 
1510 	if (error)
1511 		goto err;
1512 
1513 	/*
1514 	 * If we were unable to get the V_WRITELOCK_TRUE, then we
1515 	 * may not have accurate after attrs, so check if
1516 	 * we have both attributes, they have a non-zero va_seq, and
1517 	 * va_seq has changed by exactly one,
1518 	 * if not, turn off the before attr.
1519 	 */
1520 	if (rwlock_ret != V_WRITELOCK_TRUE) {
1521 		if (bvap == NULL || avap == NULL ||
1522 		    bvap->va_seq == 0 || avap->va_seq == 0 ||
1523 		    avap->va_seq != (bvap->va_seq + 1)) {
1524 			bvap = NULL;
1525 		}
1526 	}
1527 
1528 	resp->status = NFS3_OK;
1529 	vattr_to_wcc_data(bvap, avap, &resp->resok.file_wcc);
1530 	resp->resok.count = args->count - uio.uio_resid;
1531 	resp->resok.committed = args->stable;
1532 	resp->resok.verf = ns->write3verf;
1533 	goto out;
1534 
1535 err:
1536 	if (curthread->t_flag & T_WOULDBLOCK) {
1537 		curthread->t_flag &= ~T_WOULDBLOCK;
1538 		resp->status = NFS3ERR_JUKEBOX;
1539 	} else
1540 		resp->status = puterrno3(error);
1541 err1:
1542 	vattr_to_wcc_data(bvap, avap, &resp->resfail.file_wcc);
1543 out:
1544 	DTRACE_NFSV3_5(op__write__done, struct svc_req *, req,
1545 	    cred_t *, cr, vnode_t *, vp, struct exportinfo *, exi,
1546 	    WRITE3res *, resp);
1547 
1548 	if (vp != NULL) {
1549 		if (rwlock_ret != -1)
1550 			VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, &ct);
1551 		if (in_crit)
1552 			nbl_end_crit(vp);
1553 		VN_RELE(vp);
1554 	}
1555 }
1556 
1557 void *
rfs3_write_getfh(WRITE3args * args)1558 rfs3_write_getfh(WRITE3args *args)
1559 {
1560 
1561 	return (&args->file);
1562 }
1563 
1564 void
rfs3_create(CREATE3args * args,CREATE3res * resp,struct exportinfo * exi,struct svc_req * req,cred_t * cr,bool_t ro)1565 rfs3_create(CREATE3args *args, CREATE3res *resp, struct exportinfo *exi,
1566     struct svc_req *req, cred_t *cr, bool_t ro)
1567 {
1568 	int error;
1569 	int in_crit = 0;
1570 	vnode_t *vp;
1571 	vnode_t *tvp = NULL;
1572 	vnode_t *dvp;
1573 	struct vattr *vap;
1574 	struct vattr va;
1575 	struct vattr *dbvap;
1576 	struct vattr dbva;
1577 	struct vattr *davap;
1578 	struct vattr dava;
1579 	enum vcexcl excl;
1580 	nfstime3 *mtime;
1581 	len_t reqsize;
1582 	bool_t trunc;
1583 	struct sockaddr *ca;
1584 	char *name = NULL;
1585 
1586 	dbvap = NULL;
1587 	davap = NULL;
1588 
1589 	dvp = nfs3_fhtovp(&args->where.dir, exi);
1590 
1591 	DTRACE_NFSV3_5(op__create__start, struct svc_req *, req,
1592 	    cred_t *, cr, vnode_t *, dvp, struct exportinfo *, exi,
1593 	    CREATE3args *, args);
1594 
1595 	if (dvp == NULL) {
1596 		error = ESTALE;
1597 		goto out;
1598 	}
1599 
1600 	dbva.va_mask = AT_ALL;
1601 	dbvap = VOP_GETATTR(dvp, &dbva, 0, cr, NULL) ? NULL : &dbva;
1602 	davap = dbvap;
1603 
1604 	if (args->where.name == nfs3nametoolong) {
1605 		resp->status = NFS3ERR_NAMETOOLONG;
1606 		goto out1;
1607 	}
1608 
1609 	if (args->where.name == NULL || *(args->where.name) == '\0') {
1610 		resp->status = NFS3ERR_ACCES;
1611 		goto out1;
1612 	}
1613 
1614 	if (rdonly(ro, dvp)) {
1615 		resp->status = NFS3ERR_ROFS;
1616 		goto out1;
1617 	}
1618 
1619 	if (is_system_labeled()) {
1620 		bslabel_t *clabel = req->rq_label;
1621 
1622 		ASSERT(clabel != NULL);
1623 		DTRACE_PROBE2(tx__rfs3__log__info__opcreate__clabel, char *,
1624 		    "got client label from request(1)", struct svc_req *, req);
1625 
1626 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
1627 			if (!do_rfs_label_check(clabel, dvp, EQUALITY_CHECK,
1628 			    exi)) {
1629 				resp->status = NFS3ERR_ACCES;
1630 				goto out1;
1631 			}
1632 		}
1633 	}
1634 
1635 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
1636 	name = nfscmd_convname(ca, exi, args->where.name,
1637 	    NFSCMD_CONV_INBOUND, MAXPATHLEN + 1);
1638 
1639 	if (name == NULL) {
1640 		/* This is really a Solaris EILSEQ */
1641 		resp->status = NFS3ERR_INVAL;
1642 		goto out1;
1643 	}
1644 
1645 	if (args->how.mode == EXCLUSIVE) {
1646 		va.va_mask = AT_TYPE | AT_MODE | AT_MTIME;
1647 		va.va_type = VREG;
1648 		va.va_mode = (mode_t)0;
1649 		/*
1650 		 * Ensure no time overflows and that types match
1651 		 */
1652 		mtime = (nfstime3 *)&args->how.createhow3_u.verf;
1653 		va.va_mtime.tv_sec = mtime->seconds % INT32_MAX;
1654 		va.va_mtime.tv_nsec = mtime->nseconds;
1655 		excl = EXCL;
1656 	} else {
1657 		error = sattr3_to_vattr(&args->how.createhow3_u.obj_attributes,
1658 		    &va);
1659 		if (error)
1660 			goto out;
1661 		va.va_mask |= AT_TYPE;
1662 		va.va_type = VREG;
1663 		if (args->how.mode == GUARDED)
1664 			excl = EXCL;
1665 		else {
1666 			excl = NONEXCL;
1667 
1668 			/*
1669 			 * During creation of file in non-exclusive mode
1670 			 * if size of file is being set then make sure
1671 			 * that if the file already exists that no conflicting
1672 			 * non-blocking mandatory locks exists in the region
1673 			 * being modified. If there are conflicting locks fail
1674 			 * the operation with EACCES.
1675 			 */
1676 			if (va.va_mask & AT_SIZE) {
1677 				struct vattr tva;
1678 
1679 				/*
1680 				 * Does file already exist?
1681 				 */
1682 				error = VOP_LOOKUP(dvp, name, &tvp,
1683 				    NULL, 0, NULL, cr, NULL, NULL, NULL);
1684 
1685 				/*
1686 				 * Check to see if the file has been delegated
1687 				 * to a v4 client.  If so, then begin recall of
1688 				 * the delegation and return JUKEBOX to allow
1689 				 * the client to retrasmit its request.
1690 				 */
1691 
1692 				trunc = va.va_size == 0;
1693 				if (!error &&
1694 				    rfs4_check_delegated(FWRITE, tvp, trunc)) {
1695 					resp->status = NFS3ERR_JUKEBOX;
1696 					goto out1;
1697 				}
1698 
1699 				/*
1700 				 * Check for NBMAND lock conflicts
1701 				 */
1702 				if (!error && nbl_need_check(tvp)) {
1703 					u_offset_t offset;
1704 					ssize_t len;
1705 
1706 					nbl_start_crit(tvp, RW_READER);
1707 					in_crit = 1;
1708 
1709 					tva.va_mask = AT_SIZE;
1710 					error = VOP_GETATTR(tvp, &tva, 0, cr,
1711 					    NULL);
1712 					/*
1713 					 * Can't check for conflicts, so return
1714 					 * error.
1715 					 */
1716 					if (error)
1717 						goto out;
1718 
1719 					offset = tva.va_size < va.va_size ?
1720 					    tva.va_size : va.va_size;
1721 					len = tva.va_size < va.va_size ?
1722 					    va.va_size - tva.va_size :
1723 					    tva.va_size - va.va_size;
1724 					if (nbl_conflict(tvp, NBL_WRITE,
1725 					    offset, len, 0, NULL)) {
1726 						error = EACCES;
1727 						goto out;
1728 					}
1729 				} else if (tvp) {
1730 					VN_RELE(tvp);
1731 					tvp = NULL;
1732 				}
1733 			}
1734 		}
1735 		if (va.va_mask & AT_SIZE)
1736 			reqsize = va.va_size;
1737 	}
1738 
1739 	/*
1740 	 * Must specify the mode.
1741 	 */
1742 	if (!(va.va_mask & AT_MODE)) {
1743 		resp->status = NFS3ERR_INVAL;
1744 		goto out1;
1745 	}
1746 
1747 	/*
1748 	 * If the filesystem is exported with nosuid, then mask off
1749 	 * the setuid and setgid bits.
1750 	 */
1751 	if (va.va_type == VREG && (exi->exi_export.ex_flags & EX_NOSUID))
1752 		va.va_mode &= ~(VSUID | VSGID);
1753 
1754 tryagain:
1755 	/*
1756 	 * The file open mode used is VWRITE.  If the client needs
1757 	 * some other semantic, then it should do the access checking
1758 	 * itself.  It would have been nice to have the file open mode
1759 	 * passed as part of the arguments.
1760 	 */
1761 	error = VOP_CREATE(dvp, name, &va, excl, VWRITE,
1762 	    &vp, cr, 0, NULL, NULL);
1763 
1764 	dava.va_mask = AT_ALL;
1765 	davap = VOP_GETATTR(dvp, &dava, 0, cr, NULL) ? NULL : &dava;
1766 
1767 	if (error) {
1768 		/*
1769 		 * If we got something other than file already exists
1770 		 * then just return this error.  Otherwise, we got
1771 		 * EEXIST.  If we were doing a GUARDED create, then
1772 		 * just return this error.  Otherwise, we need to
1773 		 * make sure that this wasn't a duplicate of an
1774 		 * exclusive create request.
1775 		 *
1776 		 * The assumption is made that a non-exclusive create
1777 		 * request will never return EEXIST.
1778 		 */
1779 		if (error != EEXIST || args->how.mode == GUARDED)
1780 			goto out;
1781 		/*
1782 		 * Lookup the file so that we can get a vnode for it.
1783 		 */
1784 		error = VOP_LOOKUP(dvp, name, &vp, NULL, 0,
1785 		    NULL, cr, NULL, NULL, NULL);
1786 		if (error) {
1787 			/*
1788 			 * We couldn't find the file that we thought that
1789 			 * we just created.  So, we'll just try creating
1790 			 * it again.
1791 			 */
1792 			if (error == ENOENT)
1793 				goto tryagain;
1794 			goto out;
1795 		}
1796 
1797 		/*
1798 		 * If the file is delegated to a v4 client, go ahead
1799 		 * and initiate recall, this create is a hint that a
1800 		 * conflicting v3 open has occurred.
1801 		 */
1802 
1803 		if (rfs4_check_delegated(FWRITE, vp, FALSE)) {
1804 			VN_RELE(vp);
1805 			resp->status = NFS3ERR_JUKEBOX;
1806 			goto out1;
1807 		}
1808 
1809 		va.va_mask = AT_ALL;
1810 		vap = VOP_GETATTR(vp, &va, 0, cr, NULL) ? NULL : &va;
1811 
1812 		mtime = (nfstime3 *)&args->how.createhow3_u.verf;
1813 		/* % with INT32_MAX to prevent overflows */
1814 		if (args->how.mode == EXCLUSIVE && (vap == NULL ||
1815 		    vap->va_mtime.tv_sec !=
1816 		    (mtime->seconds % INT32_MAX) ||
1817 		    vap->va_mtime.tv_nsec != mtime->nseconds)) {
1818 			VN_RELE(vp);
1819 			error = EEXIST;
1820 			goto out;
1821 		}
1822 	} else {
1823 
1824 		if ((args->how.mode == UNCHECKED ||
1825 		    args->how.mode == GUARDED) &&
1826 		    args->how.createhow3_u.obj_attributes.size.set_it &&
1827 		    va.va_size == 0)
1828 			trunc = TRUE;
1829 		else
1830 			trunc = FALSE;
1831 
1832 		if (rfs4_check_delegated(FWRITE, vp, trunc)) {
1833 			VN_RELE(vp);
1834 			resp->status = NFS3ERR_JUKEBOX;
1835 			goto out1;
1836 		}
1837 
1838 		va.va_mask = AT_ALL;
1839 		vap = VOP_GETATTR(vp, &va, 0, cr, NULL) ? NULL : &va;
1840 
1841 		/*
1842 		 * We need to check to make sure that the file got
1843 		 * created to the indicated size.  If not, we do a
1844 		 * setattr to try to change the size, but we don't
1845 		 * try too hard.  This shouldn't a problem as most
1846 		 * clients will only specifiy a size of zero which
1847 		 * local file systems handle.  However, even if
1848 		 * the client does specify a non-zero size, it can
1849 		 * still recover by checking the size of the file
1850 		 * after it has created it and then issue a setattr
1851 		 * request of its own to set the size of the file.
1852 		 */
1853 		if (vap != NULL &&
1854 		    (args->how.mode == UNCHECKED ||
1855 		    args->how.mode == GUARDED) &&
1856 		    args->how.createhow3_u.obj_attributes.size.set_it &&
1857 		    vap->va_size != reqsize) {
1858 			va.va_mask = AT_SIZE;
1859 			va.va_size = reqsize;
1860 			(void) VOP_SETATTR(vp, &va, 0, cr, NULL);
1861 			va.va_mask = AT_ALL;
1862 			vap = VOP_GETATTR(vp, &va, 0, cr, NULL) ? NULL : &va;
1863 		}
1864 	}
1865 
1866 	if (name != args->where.name)
1867 		kmem_free(name, MAXPATHLEN + 1);
1868 
1869 	error = makefh3(&resp->resok.obj.handle, vp, exi);
1870 	if (error)
1871 		resp->resok.obj.handle_follows = FALSE;
1872 	else
1873 		resp->resok.obj.handle_follows = TRUE;
1874 
1875 	/*
1876 	 * Force modified data and metadata out to stable storage.
1877 	 */
1878 	(void) VOP_FSYNC(vp, FNODSYNC, cr, NULL);
1879 	(void) VOP_FSYNC(dvp, 0, cr, NULL);
1880 
1881 	VN_RELE(vp);
1882 	if (tvp != NULL) {
1883 		if (in_crit)
1884 			nbl_end_crit(tvp);
1885 		VN_RELE(tvp);
1886 	}
1887 
1888 	resp->status = NFS3_OK;
1889 	vattr_to_post_op_attr(vap, &resp->resok.obj_attributes);
1890 	vattr_to_wcc_data(dbvap, davap, &resp->resok.dir_wcc);
1891 
1892 	DTRACE_NFSV3_5(op__create__done, struct svc_req *, req,
1893 	    cred_t *, cr, vnode_t *, dvp, struct exportinfo *, exi,
1894 	    CREATE3res *, resp);
1895 
1896 	VN_RELE(dvp);
1897 	return;
1898 
1899 out:
1900 	if (curthread->t_flag & T_WOULDBLOCK) {
1901 		curthread->t_flag &= ~T_WOULDBLOCK;
1902 		resp->status = NFS3ERR_JUKEBOX;
1903 	} else
1904 		resp->status = puterrno3(error);
1905 out1:
1906 	DTRACE_NFSV3_5(op__create__done, struct svc_req *, req,
1907 	    cred_t *, cr, vnode_t *, dvp, struct exportinfo *, exi,
1908 	    CREATE3res *, resp);
1909 
1910 	if (name != NULL && name != args->where.name)
1911 		kmem_free(name, MAXPATHLEN + 1);
1912 
1913 	if (tvp != NULL) {
1914 		if (in_crit)
1915 			nbl_end_crit(tvp);
1916 		VN_RELE(tvp);
1917 	}
1918 	if (dvp != NULL)
1919 		VN_RELE(dvp);
1920 	vattr_to_wcc_data(dbvap, davap, &resp->resfail.dir_wcc);
1921 }
1922 
1923 void *
rfs3_create_getfh(CREATE3args * args)1924 rfs3_create_getfh(CREATE3args *args)
1925 {
1926 
1927 	return (&args->where.dir);
1928 }
1929 
1930 void
rfs3_mkdir(MKDIR3args * args,MKDIR3res * resp,struct exportinfo * exi,struct svc_req * req,cred_t * cr,bool_t ro)1931 rfs3_mkdir(MKDIR3args *args, MKDIR3res *resp, struct exportinfo *exi,
1932     struct svc_req *req, cred_t *cr, bool_t ro)
1933 {
1934 	int error;
1935 	vnode_t *vp = NULL;
1936 	vnode_t *dvp;
1937 	struct vattr *vap;
1938 	struct vattr va;
1939 	struct vattr *dbvap;
1940 	struct vattr dbva;
1941 	struct vattr *davap;
1942 	struct vattr dava;
1943 	struct sockaddr *ca;
1944 	char *name = NULL;
1945 
1946 	dbvap = NULL;
1947 	davap = NULL;
1948 
1949 	dvp = nfs3_fhtovp(&args->where.dir, exi);
1950 
1951 	DTRACE_NFSV3_5(op__mkdir__start, struct svc_req *, req,
1952 	    cred_t *, cr, vnode_t *, dvp, struct exportinfo *, exi,
1953 	    MKDIR3args *, args);
1954 
1955 	if (dvp == NULL) {
1956 		error = ESTALE;
1957 		goto out;
1958 	}
1959 
1960 	dbva.va_mask = AT_ALL;
1961 	dbvap = VOP_GETATTR(dvp, &dbva, 0, cr, NULL) ? NULL : &dbva;
1962 	davap = dbvap;
1963 
1964 	if (args->where.name == nfs3nametoolong) {
1965 		resp->status = NFS3ERR_NAMETOOLONG;
1966 		goto out1;
1967 	}
1968 
1969 	if (args->where.name == NULL || *(args->where.name) == '\0') {
1970 		resp->status = NFS3ERR_ACCES;
1971 		goto out1;
1972 	}
1973 
1974 	if (rdonly(ro, dvp)) {
1975 		resp->status = NFS3ERR_ROFS;
1976 		goto out1;
1977 	}
1978 
1979 	if (is_system_labeled()) {
1980 		bslabel_t *clabel = req->rq_label;
1981 
1982 		ASSERT(clabel != NULL);
1983 		DTRACE_PROBE2(tx__rfs3__log__info__opmkdir__clabel, char *,
1984 		    "got client label from request(1)", struct svc_req *, req);
1985 
1986 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
1987 			if (!do_rfs_label_check(clabel, dvp, EQUALITY_CHECK,
1988 			    exi)) {
1989 				resp->status = NFS3ERR_ACCES;
1990 				goto out1;
1991 			}
1992 		}
1993 	}
1994 
1995 	error = sattr3_to_vattr(&args->attributes, &va);
1996 	if (error)
1997 		goto out;
1998 
1999 	if (!(va.va_mask & AT_MODE)) {
2000 		resp->status = NFS3ERR_INVAL;
2001 		goto out1;
2002 	}
2003 
2004 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
2005 	name = nfscmd_convname(ca, exi, args->where.name,
2006 	    NFSCMD_CONV_INBOUND, MAXPATHLEN + 1);
2007 
2008 	if (name == NULL) {
2009 		resp->status = NFS3ERR_INVAL;
2010 		goto out1;
2011 	}
2012 
2013 	va.va_mask |= AT_TYPE;
2014 	va.va_type = VDIR;
2015 
2016 	error = VOP_MKDIR(dvp, name, &va, &vp, cr, NULL, 0, NULL);
2017 
2018 	if (name != args->where.name)
2019 		kmem_free(name, MAXPATHLEN + 1);
2020 
2021 	dava.va_mask = AT_ALL;
2022 	davap = VOP_GETATTR(dvp, &dava, 0, cr, NULL) ? NULL : &dava;
2023 
2024 	/*
2025 	 * Force modified data and metadata out to stable storage.
2026 	 */
2027 	(void) VOP_FSYNC(dvp, 0, cr, NULL);
2028 
2029 	if (error)
2030 		goto out;
2031 
2032 	error = makefh3(&resp->resok.obj.handle, vp, exi);
2033 	if (error)
2034 		resp->resok.obj.handle_follows = FALSE;
2035 	else
2036 		resp->resok.obj.handle_follows = TRUE;
2037 
2038 	va.va_mask = AT_ALL;
2039 	vap = VOP_GETATTR(vp, &va, 0, cr, NULL) ? NULL : &va;
2040 
2041 	/*
2042 	 * Force modified data and metadata out to stable storage.
2043 	 */
2044 	(void) VOP_FSYNC(vp, 0, cr, NULL);
2045 
2046 	VN_RELE(vp);
2047 
2048 	resp->status = NFS3_OK;
2049 	vattr_to_post_op_attr(vap, &resp->resok.obj_attributes);
2050 	vattr_to_wcc_data(dbvap, davap, &resp->resok.dir_wcc);
2051 
2052 	DTRACE_NFSV3_5(op__mkdir__done, struct svc_req *, req,
2053 	    cred_t *, cr, vnode_t *, dvp, struct exportinfo *, exi,
2054 	    MKDIR3res *, resp);
2055 	VN_RELE(dvp);
2056 
2057 	return;
2058 
2059 out:
2060 	if (curthread->t_flag & T_WOULDBLOCK) {
2061 		curthread->t_flag &= ~T_WOULDBLOCK;
2062 		resp->status = NFS3ERR_JUKEBOX;
2063 	} else
2064 		resp->status = puterrno3(error);
2065 out1:
2066 	DTRACE_NFSV3_5(op__mkdir__done, struct svc_req *, req,
2067 	    cred_t *, cr, vnode_t *, dvp, struct exportinfo *, exi,
2068 	    MKDIR3res *, resp);
2069 	if (dvp != NULL)
2070 		VN_RELE(dvp);
2071 	vattr_to_wcc_data(dbvap, davap, &resp->resfail.dir_wcc);
2072 }
2073 
2074 void *
rfs3_mkdir_getfh(MKDIR3args * args)2075 rfs3_mkdir_getfh(MKDIR3args *args)
2076 {
2077 
2078 	return (&args->where.dir);
2079 }
2080 
2081 void
rfs3_symlink(SYMLINK3args * args,SYMLINK3res * resp,struct exportinfo * exi,struct svc_req * req,cred_t * cr,bool_t ro)2082 rfs3_symlink(SYMLINK3args *args, SYMLINK3res *resp, struct exportinfo *exi,
2083     struct svc_req *req, cred_t *cr, bool_t ro)
2084 {
2085 	int error;
2086 	vnode_t *vp;
2087 	vnode_t *dvp;
2088 	struct vattr *vap;
2089 	struct vattr va;
2090 	struct vattr *dbvap;
2091 	struct vattr dbva;
2092 	struct vattr *davap;
2093 	struct vattr dava;
2094 	struct sockaddr *ca;
2095 	char *name = NULL;
2096 	char *symdata = NULL;
2097 
2098 	dbvap = NULL;
2099 	davap = NULL;
2100 
2101 	dvp = nfs3_fhtovp(&args->where.dir, exi);
2102 
2103 	DTRACE_NFSV3_5(op__symlink__start, struct svc_req *, req,
2104 	    cred_t *, cr, vnode_t *, dvp, struct exportinfo *, exi,
2105 	    SYMLINK3args *, args);
2106 
2107 	if (dvp == NULL) {
2108 		error = ESTALE;
2109 		goto err;
2110 	}
2111 
2112 	dbva.va_mask = AT_ALL;
2113 	dbvap = VOP_GETATTR(dvp, &dbva, 0, cr, NULL) ? NULL : &dbva;
2114 	davap = dbvap;
2115 
2116 	if (args->where.name == nfs3nametoolong) {
2117 		resp->status = NFS3ERR_NAMETOOLONG;
2118 		goto err1;
2119 	}
2120 
2121 	if (args->where.name == NULL || *(args->where.name) == '\0') {
2122 		resp->status = NFS3ERR_ACCES;
2123 		goto err1;
2124 	}
2125 
2126 	if (rdonly(ro, dvp)) {
2127 		resp->status = NFS3ERR_ROFS;
2128 		goto err1;
2129 	}
2130 
2131 	if (is_system_labeled()) {
2132 		bslabel_t *clabel = req->rq_label;
2133 
2134 		ASSERT(clabel != NULL);
2135 		DTRACE_PROBE2(tx__rfs3__log__info__opsymlink__clabel, char *,
2136 		    "got client label from request(1)", struct svc_req *, req);
2137 
2138 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
2139 			if (!do_rfs_label_check(clabel, dvp, EQUALITY_CHECK,
2140 			    exi)) {
2141 				resp->status = NFS3ERR_ACCES;
2142 				goto err1;
2143 			}
2144 		}
2145 	}
2146 
2147 	error = sattr3_to_vattr(&args->symlink.symlink_attributes, &va);
2148 	if (error)
2149 		goto err;
2150 
2151 	if (!(va.va_mask & AT_MODE)) {
2152 		resp->status = NFS3ERR_INVAL;
2153 		goto err1;
2154 	}
2155 
2156 	if (args->symlink.symlink_data == nfs3nametoolong) {
2157 		resp->status = NFS3ERR_NAMETOOLONG;
2158 		goto err1;
2159 	}
2160 
2161 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
2162 	name = nfscmd_convname(ca, exi, args->where.name,
2163 	    NFSCMD_CONV_INBOUND, MAXPATHLEN + 1);
2164 
2165 	if (name == NULL) {
2166 		/* This is really a Solaris EILSEQ */
2167 		resp->status = NFS3ERR_INVAL;
2168 		goto err1;
2169 	}
2170 
2171 	symdata = nfscmd_convname(ca, exi, args->symlink.symlink_data,
2172 	    NFSCMD_CONV_INBOUND, MAXPATHLEN + 1);
2173 	if (symdata == NULL) {
2174 		/* This is really a Solaris EILSEQ */
2175 		resp->status = NFS3ERR_INVAL;
2176 		goto err1;
2177 	}
2178 
2179 
2180 	va.va_mask |= AT_TYPE;
2181 	va.va_type = VLNK;
2182 
2183 	error = VOP_SYMLINK(dvp, name, &va, symdata, cr, NULL, 0);
2184 
2185 	dava.va_mask = AT_ALL;
2186 	davap = VOP_GETATTR(dvp, &dava, 0, cr, NULL) ? NULL : &dava;
2187 
2188 	if (error)
2189 		goto err;
2190 
2191 	error = VOP_LOOKUP(dvp, name, &vp, NULL, 0, NULL, cr,
2192 	    NULL, NULL, NULL);
2193 
2194 	/*
2195 	 * Force modified data and metadata out to stable storage.
2196 	 */
2197 	(void) VOP_FSYNC(dvp, 0, cr, NULL);
2198 
2199 
2200 	resp->status = NFS3_OK;
2201 	if (error) {
2202 		resp->resok.obj.handle_follows = FALSE;
2203 		vattr_to_post_op_attr(NULL, &resp->resok.obj_attributes);
2204 		vattr_to_wcc_data(dbvap, davap, &resp->resok.dir_wcc);
2205 		goto out;
2206 	}
2207 
2208 	error = makefh3(&resp->resok.obj.handle, vp, exi);
2209 	if (error)
2210 		resp->resok.obj.handle_follows = FALSE;
2211 	else
2212 		resp->resok.obj.handle_follows = TRUE;
2213 
2214 	va.va_mask = AT_ALL;
2215 	vap = VOP_GETATTR(vp, &va, 0, cr, NULL) ? NULL : &va;
2216 
2217 	/*
2218 	 * Force modified data and metadata out to stable storage.
2219 	 */
2220 	(void) VOP_FSYNC(vp, 0, cr, NULL);
2221 
2222 	VN_RELE(vp);
2223 
2224 	vattr_to_post_op_attr(vap, &resp->resok.obj_attributes);
2225 	vattr_to_wcc_data(dbvap, davap, &resp->resok.dir_wcc);
2226 	goto out;
2227 
2228 err:
2229 	if (curthread->t_flag & T_WOULDBLOCK) {
2230 		curthread->t_flag &= ~T_WOULDBLOCK;
2231 		resp->status = NFS3ERR_JUKEBOX;
2232 	} else
2233 		resp->status = puterrno3(error);
2234 err1:
2235 	vattr_to_wcc_data(dbvap, davap, &resp->resfail.dir_wcc);
2236 out:
2237 	if (name != NULL && name != args->where.name)
2238 		kmem_free(name, MAXPATHLEN + 1);
2239 	if (symdata != NULL && symdata != args->symlink.symlink_data)
2240 		kmem_free(symdata, MAXPATHLEN + 1);
2241 
2242 	DTRACE_NFSV3_5(op__symlink__done, struct svc_req *, req,
2243 	    cred_t *, cr, vnode_t *, dvp, struct exportinfo *, exi,
2244 	    SYMLINK3res *, resp);
2245 
2246 	if (dvp != NULL)
2247 		VN_RELE(dvp);
2248 }
2249 
2250 void *
rfs3_symlink_getfh(SYMLINK3args * args)2251 rfs3_symlink_getfh(SYMLINK3args *args)
2252 {
2253 
2254 	return (&args->where.dir);
2255 }
2256 
2257 void
rfs3_mknod(MKNOD3args * args,MKNOD3res * resp,struct exportinfo * exi,struct svc_req * req,cred_t * cr,bool_t ro)2258 rfs3_mknod(MKNOD3args *args, MKNOD3res *resp, struct exportinfo *exi,
2259     struct svc_req *req, cred_t *cr, bool_t ro)
2260 {
2261 	int error;
2262 	vnode_t *vp;
2263 	vnode_t *realvp;
2264 	vnode_t *dvp;
2265 	struct vattr *vap;
2266 	struct vattr va;
2267 	struct vattr *dbvap;
2268 	struct vattr dbva;
2269 	struct vattr *davap;
2270 	struct vattr dava;
2271 	int mode;
2272 	enum vcexcl excl;
2273 	struct sockaddr *ca;
2274 	char *name = NULL;
2275 
2276 	dbvap = NULL;
2277 	davap = NULL;
2278 
2279 	dvp = nfs3_fhtovp(&args->where.dir, exi);
2280 
2281 	DTRACE_NFSV3_5(op__mknod__start, struct svc_req *, req,
2282 	    cred_t *, cr, vnode_t *, dvp, struct exportinfo *, exi,
2283 	    MKNOD3args *, args);
2284 
2285 	if (dvp == NULL) {
2286 		error = ESTALE;
2287 		goto out;
2288 	}
2289 
2290 	dbva.va_mask = AT_ALL;
2291 	dbvap = VOP_GETATTR(dvp, &dbva, 0, cr, NULL) ? NULL : &dbva;
2292 	davap = dbvap;
2293 
2294 	if (args->where.name == nfs3nametoolong) {
2295 		resp->status = NFS3ERR_NAMETOOLONG;
2296 		goto out1;
2297 	}
2298 
2299 	if (args->where.name == NULL || *(args->where.name) == '\0') {
2300 		resp->status = NFS3ERR_ACCES;
2301 		goto out1;
2302 	}
2303 
2304 	if (rdonly(ro, dvp)) {
2305 		resp->status = NFS3ERR_ROFS;
2306 		goto out1;
2307 	}
2308 
2309 	if (is_system_labeled()) {
2310 		bslabel_t *clabel = req->rq_label;
2311 
2312 		ASSERT(clabel != NULL);
2313 		DTRACE_PROBE2(tx__rfs3__log__info__opmknod__clabel, char *,
2314 		    "got client label from request(1)", struct svc_req *, req);
2315 
2316 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
2317 			if (!do_rfs_label_check(clabel, dvp, EQUALITY_CHECK,
2318 			    exi)) {
2319 				resp->status = NFS3ERR_ACCES;
2320 				goto out1;
2321 			}
2322 		}
2323 	}
2324 
2325 	switch (args->what.type) {
2326 	case NF3CHR:
2327 	case NF3BLK:
2328 		error = sattr3_to_vattr(
2329 		    &args->what.mknoddata3_u.device.dev_attributes, &va);
2330 		if (error)
2331 			goto out;
2332 		if (secpolicy_sys_devices(cr) != 0) {
2333 			resp->status = NFS3ERR_PERM;
2334 			goto out1;
2335 		}
2336 		if (args->what.type == NF3CHR)
2337 			va.va_type = VCHR;
2338 		else
2339 			va.va_type = VBLK;
2340 		va.va_rdev = makedevice(
2341 		    args->what.mknoddata3_u.device.spec.specdata1,
2342 		    args->what.mknoddata3_u.device.spec.specdata2);
2343 		va.va_mask |= AT_TYPE | AT_RDEV;
2344 		break;
2345 	case NF3SOCK:
2346 		error = sattr3_to_vattr(
2347 		    &args->what.mknoddata3_u.pipe_attributes, &va);
2348 		if (error)
2349 			goto out;
2350 		va.va_type = VSOCK;
2351 		va.va_mask |= AT_TYPE;
2352 		break;
2353 	case NF3FIFO:
2354 		error = sattr3_to_vattr(
2355 		    &args->what.mknoddata3_u.pipe_attributes, &va);
2356 		if (error)
2357 			goto out;
2358 		va.va_type = VFIFO;
2359 		va.va_mask |= AT_TYPE;
2360 		break;
2361 	default:
2362 		resp->status = NFS3ERR_BADTYPE;
2363 		goto out1;
2364 	}
2365 
2366 	/*
2367 	 * Must specify the mode.
2368 	 */
2369 	if (!(va.va_mask & AT_MODE)) {
2370 		resp->status = NFS3ERR_INVAL;
2371 		goto out1;
2372 	}
2373 
2374 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
2375 	name = nfscmd_convname(ca, exi, args->where.name,
2376 	    NFSCMD_CONV_INBOUND, MAXPATHLEN + 1);
2377 
2378 	if (name == NULL) {
2379 		resp->status = NFS3ERR_INVAL;
2380 		goto out1;
2381 	}
2382 
2383 	excl = EXCL;
2384 
2385 	mode = 0;
2386 
2387 	error = VOP_CREATE(dvp, name, &va, excl, mode,
2388 	    &vp, cr, 0, NULL, NULL);
2389 
2390 	if (name != args->where.name)
2391 		kmem_free(name, MAXPATHLEN + 1);
2392 
2393 	dava.va_mask = AT_ALL;
2394 	davap = VOP_GETATTR(dvp, &dava, 0, cr, NULL) ? NULL : &dava;
2395 
2396 	/*
2397 	 * Force modified data and metadata out to stable storage.
2398 	 */
2399 	(void) VOP_FSYNC(dvp, 0, cr, NULL);
2400 
2401 	if (error)
2402 		goto out;
2403 
2404 	resp->status = NFS3_OK;
2405 
2406 	error = makefh3(&resp->resok.obj.handle, vp, exi);
2407 	if (error)
2408 		resp->resok.obj.handle_follows = FALSE;
2409 	else
2410 		resp->resok.obj.handle_follows = TRUE;
2411 
2412 	va.va_mask = AT_ALL;
2413 	vap = VOP_GETATTR(vp, &va, 0, cr, NULL) ? NULL : &va;
2414 
2415 	/*
2416 	 * Force modified metadata out to stable storage.
2417 	 *
2418 	 * if a underlying vp exists, pass it to VOP_FSYNC
2419 	 */
2420 	if (VOP_REALVP(vp, &realvp, NULL) == 0)
2421 		(void) VOP_FSYNC(realvp, FNODSYNC, cr, NULL);
2422 	else
2423 		(void) VOP_FSYNC(vp, FNODSYNC, cr, NULL);
2424 
2425 	VN_RELE(vp);
2426 
2427 	vattr_to_post_op_attr(vap, &resp->resok.obj_attributes);
2428 	vattr_to_wcc_data(dbvap, davap, &resp->resok.dir_wcc);
2429 	DTRACE_NFSV3_5(op__mknod__done, struct svc_req *, req,
2430 	    cred_t *, cr, vnode_t *, dvp, struct exportinfo *, exi,
2431 	    MKNOD3res *, resp);
2432 	VN_RELE(dvp);
2433 	return;
2434 
2435 out:
2436 	if (curthread->t_flag & T_WOULDBLOCK) {
2437 		curthread->t_flag &= ~T_WOULDBLOCK;
2438 		resp->status = NFS3ERR_JUKEBOX;
2439 	} else
2440 		resp->status = puterrno3(error);
2441 out1:
2442 	DTRACE_NFSV3_5(op__mknod__done, struct svc_req *, req,
2443 	    cred_t *, cr, vnode_t *, dvp, struct exportinfo *, exi,
2444 	    MKNOD3res *, resp);
2445 	if (dvp != NULL)
2446 		VN_RELE(dvp);
2447 	vattr_to_wcc_data(dbvap, davap, &resp->resfail.dir_wcc);
2448 }
2449 
2450 void *
rfs3_mknod_getfh(MKNOD3args * args)2451 rfs3_mknod_getfh(MKNOD3args *args)
2452 {
2453 
2454 	return (&args->where.dir);
2455 }
2456 
2457 void
rfs3_remove(REMOVE3args * args,REMOVE3res * resp,struct exportinfo * exi,struct svc_req * req,cred_t * cr,bool_t ro)2458 rfs3_remove(REMOVE3args *args, REMOVE3res *resp, struct exportinfo *exi,
2459     struct svc_req *req, cred_t *cr, bool_t ro)
2460 {
2461 	int error = 0;
2462 	vnode_t *vp;
2463 	struct vattr *bvap;
2464 	struct vattr bva;
2465 	struct vattr *avap;
2466 	struct vattr ava;
2467 	vnode_t *targvp = NULL;
2468 	struct sockaddr *ca;
2469 	char *name = NULL;
2470 
2471 	bvap = NULL;
2472 	avap = NULL;
2473 
2474 	vp = nfs3_fhtovp(&args->object.dir, exi);
2475 
2476 	DTRACE_NFSV3_5(op__remove__start, struct svc_req *, req,
2477 	    cred_t *, cr, vnode_t *, vp, struct exportinfo *, exi,
2478 	    REMOVE3args *, args);
2479 
2480 	if (vp == NULL) {
2481 		error = ESTALE;
2482 		goto err;
2483 	}
2484 
2485 	bva.va_mask = AT_ALL;
2486 	bvap = VOP_GETATTR(vp, &bva, 0, cr, NULL) ? NULL : &bva;
2487 	avap = bvap;
2488 
2489 	if (vp->v_type != VDIR) {
2490 		resp->status = NFS3ERR_NOTDIR;
2491 		goto err1;
2492 	}
2493 
2494 	if (args->object.name == nfs3nametoolong) {
2495 		resp->status = NFS3ERR_NAMETOOLONG;
2496 		goto err1;
2497 	}
2498 
2499 	if (args->object.name == NULL || *(args->object.name) == '\0') {
2500 		resp->status = NFS3ERR_ACCES;
2501 		goto err1;
2502 	}
2503 
2504 	if (rdonly(ro, vp)) {
2505 		resp->status = NFS3ERR_ROFS;
2506 		goto err1;
2507 	}
2508 
2509 	if (is_system_labeled()) {
2510 		bslabel_t *clabel = req->rq_label;
2511 
2512 		ASSERT(clabel != NULL);
2513 		DTRACE_PROBE2(tx__rfs3__log__info__opremove__clabel, char *,
2514 		    "got client label from request(1)", struct svc_req *, req);
2515 
2516 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
2517 			if (!do_rfs_label_check(clabel, vp, EQUALITY_CHECK,
2518 			    exi)) {
2519 				resp->status = NFS3ERR_ACCES;
2520 				goto err1;
2521 			}
2522 		}
2523 	}
2524 
2525 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
2526 	name = nfscmd_convname(ca, exi, args->object.name,
2527 	    NFSCMD_CONV_INBOUND, MAXPATHLEN + 1);
2528 
2529 	if (name == NULL) {
2530 		resp->status = NFS3ERR_INVAL;
2531 		goto err1;
2532 	}
2533 
2534 	/*
2535 	 * Check for a conflict with a non-blocking mandatory share
2536 	 * reservation and V4 delegations
2537 	 */
2538 	error = VOP_LOOKUP(vp, name, &targvp, NULL, 0,
2539 	    NULL, cr, NULL, NULL, NULL);
2540 	if (error != 0)
2541 		goto err;
2542 
2543 	if (rfs4_check_delegated(FWRITE, targvp, TRUE)) {
2544 		resp->status = NFS3ERR_JUKEBOX;
2545 		goto err1;
2546 	}
2547 
2548 	if (!nbl_need_check(targvp)) {
2549 		error = VOP_REMOVE(vp, name, cr, NULL, 0);
2550 	} else {
2551 		nbl_start_crit(targvp, RW_READER);
2552 		if (nbl_conflict(targvp, NBL_REMOVE, 0, 0, 0, NULL)) {
2553 			error = EACCES;
2554 		} else {
2555 			error = VOP_REMOVE(vp, name, cr, NULL, 0);
2556 		}
2557 		nbl_end_crit(targvp);
2558 	}
2559 	VN_RELE(targvp);
2560 	targvp = NULL;
2561 
2562 	ava.va_mask = AT_ALL;
2563 	avap = VOP_GETATTR(vp, &ava, 0, cr, NULL) ? NULL : &ava;
2564 
2565 	/*
2566 	 * Force modified data and metadata out to stable storage.
2567 	 */
2568 	(void) VOP_FSYNC(vp, 0, cr, NULL);
2569 
2570 	if (error)
2571 		goto err;
2572 
2573 	resp->status = NFS3_OK;
2574 	vattr_to_wcc_data(bvap, avap, &resp->resok.dir_wcc);
2575 	goto out;
2576 
2577 err:
2578 	if (curthread->t_flag & T_WOULDBLOCK) {
2579 		curthread->t_flag &= ~T_WOULDBLOCK;
2580 		resp->status = NFS3ERR_JUKEBOX;
2581 	} else
2582 		resp->status = puterrno3(error);
2583 err1:
2584 	vattr_to_wcc_data(bvap, avap, &resp->resfail.dir_wcc);
2585 out:
2586 	DTRACE_NFSV3_5(op__remove__done, struct svc_req *, req,
2587 	    cred_t *, cr, vnode_t *, vp, struct exportinfo *, exi,
2588 	    REMOVE3res *, resp);
2589 
2590 	if (name != NULL && name != args->object.name)
2591 		kmem_free(name, MAXPATHLEN + 1);
2592 
2593 	if (vp != NULL)
2594 		VN_RELE(vp);
2595 }
2596 
2597 void *
rfs3_remove_getfh(REMOVE3args * args)2598 rfs3_remove_getfh(REMOVE3args *args)
2599 {
2600 
2601 	return (&args->object.dir);
2602 }
2603 
2604 void
rfs3_rmdir(RMDIR3args * args,RMDIR3res * resp,struct exportinfo * exi,struct svc_req * req,cred_t * cr,bool_t ro)2605 rfs3_rmdir(RMDIR3args *args, RMDIR3res *resp, struct exportinfo *exi,
2606     struct svc_req *req, cred_t *cr, bool_t ro)
2607 {
2608 	int error;
2609 	vnode_t *vp;
2610 	struct vattr *bvap;
2611 	struct vattr bva;
2612 	struct vattr *avap;
2613 	struct vattr ava;
2614 	struct sockaddr *ca;
2615 	char *name = NULL;
2616 
2617 	bvap = NULL;
2618 	avap = NULL;
2619 
2620 	vp = nfs3_fhtovp(&args->object.dir, exi);
2621 
2622 	DTRACE_NFSV3_5(op__rmdir__start, struct svc_req *, req,
2623 	    cred_t *, cr, vnode_t *, vp, struct exportinfo *, exi,
2624 	    RMDIR3args *, args);
2625 
2626 	if (vp == NULL) {
2627 		error = ESTALE;
2628 		goto err;
2629 	}
2630 
2631 	bva.va_mask = AT_ALL;
2632 	bvap = VOP_GETATTR(vp, &bva, 0, cr, NULL) ? NULL : &bva;
2633 	avap = bvap;
2634 
2635 	if (vp->v_type != VDIR) {
2636 		resp->status = NFS3ERR_NOTDIR;
2637 		goto err1;
2638 	}
2639 
2640 	if (args->object.name == nfs3nametoolong) {
2641 		resp->status = NFS3ERR_NAMETOOLONG;
2642 		goto err1;
2643 	}
2644 
2645 	if (args->object.name == NULL || *(args->object.name) == '\0') {
2646 		resp->status = NFS3ERR_ACCES;
2647 		goto err1;
2648 	}
2649 
2650 	if (rdonly(ro, vp)) {
2651 		resp->status = NFS3ERR_ROFS;
2652 		goto err1;
2653 	}
2654 
2655 	if (is_system_labeled()) {
2656 		bslabel_t *clabel = req->rq_label;
2657 
2658 		ASSERT(clabel != NULL);
2659 		DTRACE_PROBE2(tx__rfs3__log__info__opremovedir__clabel, char *,
2660 		    "got client label from request(1)", struct svc_req *, req);
2661 
2662 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
2663 			if (!do_rfs_label_check(clabel, vp, EQUALITY_CHECK,
2664 			    exi)) {
2665 				resp->status = NFS3ERR_ACCES;
2666 				goto err1;
2667 			}
2668 		}
2669 	}
2670 
2671 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
2672 	name = nfscmd_convname(ca, exi, args->object.name,
2673 	    NFSCMD_CONV_INBOUND, MAXPATHLEN + 1);
2674 
2675 	if (name == NULL) {
2676 		resp->status = NFS3ERR_INVAL;
2677 		goto err1;
2678 	}
2679 
2680 	ASSERT3U(exi->exi_zoneid, ==, curzone->zone_id);
2681 	error = VOP_RMDIR(vp, name, ZONE_ROOTVP(), cr, NULL, 0);
2682 
2683 	if (name != args->object.name)
2684 		kmem_free(name, MAXPATHLEN + 1);
2685 
2686 	ava.va_mask = AT_ALL;
2687 	avap = VOP_GETATTR(vp, &ava, 0, cr, NULL) ? NULL : &ava;
2688 
2689 	/*
2690 	 * Force modified data and metadata out to stable storage.
2691 	 */
2692 	(void) VOP_FSYNC(vp, 0, cr, NULL);
2693 
2694 	if (error) {
2695 		/*
2696 		 * System V defines rmdir to return EEXIST, not ENOTEMPTY,
2697 		 * if the directory is not empty.  A System V NFS server
2698 		 * needs to map NFS3ERR_EXIST to NFS3ERR_NOTEMPTY to transmit
2699 		 * over the wire.
2700 		 */
2701 		if (error == EEXIST)
2702 			error = ENOTEMPTY;
2703 		goto err;
2704 	}
2705 
2706 	resp->status = NFS3_OK;
2707 	vattr_to_wcc_data(bvap, avap, &resp->resok.dir_wcc);
2708 	goto out;
2709 
2710 err:
2711 	if (curthread->t_flag & T_WOULDBLOCK) {
2712 		curthread->t_flag &= ~T_WOULDBLOCK;
2713 		resp->status = NFS3ERR_JUKEBOX;
2714 	} else
2715 		resp->status = puterrno3(error);
2716 err1:
2717 	vattr_to_wcc_data(bvap, avap, &resp->resfail.dir_wcc);
2718 out:
2719 	DTRACE_NFSV3_5(op__rmdir__done, struct svc_req *, req,
2720 	    cred_t *, cr, vnode_t *, vp, struct exportinfo *, exi,
2721 	    RMDIR3res *, resp);
2722 	if (vp != NULL)
2723 		VN_RELE(vp);
2724 
2725 }
2726 
2727 void *
rfs3_rmdir_getfh(RMDIR3args * args)2728 rfs3_rmdir_getfh(RMDIR3args *args)
2729 {
2730 
2731 	return (&args->object.dir);
2732 }
2733 
2734 void
rfs3_rename(RENAME3args * args,RENAME3res * resp,struct exportinfo * exi,struct svc_req * req,cred_t * cr,bool_t ro)2735 rfs3_rename(RENAME3args *args, RENAME3res *resp, struct exportinfo *exi,
2736     struct svc_req *req, cred_t *cr, bool_t ro)
2737 {
2738 	int error = 0;
2739 	vnode_t *fvp;
2740 	vnode_t *tvp;
2741 	vnode_t *targvp;
2742 	struct vattr *fbvap;
2743 	struct vattr fbva;
2744 	struct vattr *favap;
2745 	struct vattr fava;
2746 	struct vattr *tbvap;
2747 	struct vattr tbva;
2748 	struct vattr *tavap;
2749 	struct vattr tava;
2750 	nfs_fh3 *fh3;
2751 	struct exportinfo *to_exi;
2752 	vnode_t *srcvp = NULL;
2753 	bslabel_t *clabel;
2754 	struct sockaddr *ca;
2755 	char *name = NULL;
2756 	char *toname = NULL;
2757 
2758 	fbvap = NULL;
2759 	favap = NULL;
2760 	tbvap = NULL;
2761 	tavap = NULL;
2762 	tvp = NULL;
2763 
2764 	fvp = nfs3_fhtovp(&args->from.dir, exi);
2765 
2766 	DTRACE_NFSV3_5(op__rename__start, struct svc_req *, req,
2767 	    cred_t *, cr, vnode_t *, fvp, struct exportinfo *, exi,
2768 	    RENAME3args *, args);
2769 
2770 	if (fvp == NULL) {
2771 		error = ESTALE;
2772 		goto err;
2773 	}
2774 
2775 	if (is_system_labeled()) {
2776 		clabel = req->rq_label;
2777 		ASSERT(clabel != NULL);
2778 		DTRACE_PROBE2(tx__rfs3__log__info__oprename__clabel, char *,
2779 		    "got client label from request(1)", struct svc_req *, req);
2780 
2781 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
2782 			if (!do_rfs_label_check(clabel, fvp, EQUALITY_CHECK,
2783 			    exi)) {
2784 				resp->status = NFS3ERR_ACCES;
2785 				goto err1;
2786 			}
2787 		}
2788 	}
2789 
2790 	fbva.va_mask = AT_ALL;
2791 	fbvap = VOP_GETATTR(fvp, &fbva, 0, cr, NULL) ? NULL : &fbva;
2792 	favap = fbvap;
2793 
2794 	fh3 = &args->to.dir;
2795 	to_exi = checkexport(&fh3->fh3_fsid, FH3TOXFIDP(fh3));
2796 	if (to_exi == NULL) {
2797 		resp->status = NFS3ERR_ACCES;
2798 		goto err1;
2799 	}
2800 	exi_rele(to_exi);
2801 
2802 	if (to_exi != exi) {
2803 		resp->status = NFS3ERR_XDEV;
2804 		goto err1;
2805 	}
2806 
2807 	tvp = nfs3_fhtovp(&args->to.dir, exi);
2808 	if (tvp == NULL) {
2809 		error = ESTALE;
2810 		goto err;
2811 	}
2812 
2813 	tbva.va_mask = AT_ALL;
2814 	tbvap = VOP_GETATTR(tvp, &tbva, 0, cr, NULL) ? NULL : &tbva;
2815 	tavap = tbvap;
2816 
2817 	if (fvp->v_type != VDIR || tvp->v_type != VDIR) {
2818 		resp->status = NFS3ERR_NOTDIR;
2819 		goto err1;
2820 	}
2821 
2822 	if (args->from.name == nfs3nametoolong ||
2823 	    args->to.name == nfs3nametoolong) {
2824 		resp->status = NFS3ERR_NAMETOOLONG;
2825 		goto err1;
2826 	}
2827 	if (args->from.name == NULL || *(args->from.name) == '\0' ||
2828 	    args->to.name == NULL || *(args->to.name) == '\0') {
2829 		resp->status = NFS3ERR_ACCES;
2830 		goto err1;
2831 	}
2832 
2833 	if (rdonly(ro, tvp)) {
2834 		resp->status = NFS3ERR_ROFS;
2835 		goto err1;
2836 	}
2837 
2838 	if (is_system_labeled()) {
2839 		if (!blequal(&l_admin_low->tsl_label, clabel)) {
2840 			if (!do_rfs_label_check(clabel, tvp, EQUALITY_CHECK,
2841 			    exi)) {
2842 				resp->status = NFS3ERR_ACCES;
2843 				goto err1;
2844 			}
2845 		}
2846 	}
2847 
2848 	ca = (struct sockaddr *)svc_getrpccaller(req->rq_xprt)->buf;
2849 	name = nfscmd_convname(ca, exi, args->from.name,
2850 	    NFSCMD_CONV_INBOUND, MAXPATHLEN + 1);
2851 
2852 	if (name == NULL) {
2853 		resp->status = NFS3ERR_INVAL;
2854 		goto err1;
2855 	}
2856 
2857 	toname = nfscmd_convname(ca, exi, args->to.name,
2858 	    NFSCMD_CONV_INBOUND, MAXPATHLEN + 1);
2859 
2860 	if (toname == NULL) {
2861 		resp->status = NFS3ERR_INVAL;
2862 		goto err1;
2863 	}
2864 
2865 	/*
2866 	 * Check for a conflict with a non-blocking mandatory share
2867 	 * reservation or V4 delegations.
2868 	 */
2869 	error = VOP_LOOKUP(fvp, name, &srcvp, NULL, 0,
2870 	    NULL, cr, NULL, NULL, NULL);
2871 	if (error != 0)
2872 		goto err;
2873 
2874 	/*
2875 	 * If we rename a delegated file we should recall the
2876 	 * delegation, since future opens should fail or would
2877 	 * refer to a new file.
2878 	 */
2879 	if (rfs4_check_delegated(FWRITE, srcvp, FALSE)) {
2880 		resp->status = NFS3ERR_JUKEBOX;
2881 		goto err1;
2882 	}
2883 
2884 	/*
2885 	 * Check for renaming over a delegated file.  Check nfs4_deleg_policy
2886 	 * first to avoid VOP_LOOKUP if possible.
2887 	 */
2888 	if (nfs4_get_deleg_policy() != SRV_NEVER_DELEGATE &&
2889 	    VOP_LOOKUP(tvp, toname, &targvp, NULL, 0, NULL, cr,
2890 	    NULL, NULL, NULL) == 0) {
2891 
2892 		if (rfs4_check_delegated(FWRITE, targvp, TRUE)) {
2893 			VN_RELE(targvp);
2894 			resp->status = NFS3ERR_JUKEBOX;
2895 			goto err1;
2896 		}
2897 		VN_RELE(targvp);
2898 	}
2899 
2900 	if (!nbl_need_check(srcvp)) {
2901 		error = VOP_RENAME(fvp, name, tvp, toname, cr, NULL, 0);
2902 	} else {
2903 		nbl_start_crit(srcvp, RW_READER);
2904 		if (nbl_conflict(srcvp, NBL_RENAME, 0, 0, 0, NULL))
2905 			error = EACCES;
2906 		else
2907 			error = VOP_RENAME(fvp, name, tvp, toname, cr, NULL, 0);
2908 		nbl_end_crit(srcvp);
2909 	}
2910 	if (error == 0)
2911 		vn_renamepath(tvp, srcvp, args->to.name,
2912 		    strlen(args->to.name));
2913 	VN_RELE(srcvp);
2914 	srcvp = NULL;
2915 
2916 	fava.va_mask = AT_ALL;
2917 	favap = VOP_GETATTR(fvp, &fava, 0, cr, NULL) ? NULL : &fava;
2918 	tava.va_mask = AT_ALL;
2919 	tavap = VOP_GETATTR(tvp, &tava, 0, cr, NULL) ? NULL : &tava;
2920 
2921 	/*
2922 	 * Force modified data and metadata out to stable storage.
2923 	 */
2924 	(void) VOP_FSYNC(fvp, 0, cr, NULL);
2925 	(void) VOP_FSYNC(tvp, 0, cr, NULL);
2926