xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_fsops.c (revision d2488fe8c5950495aef5404c8d98081be7d43487)
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  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
24  */
25 
26 #include <sys/sid.h>
27 #include <sys/nbmlock.h>
28 #include <smbsrv/smb_fsops.h>
29 #include <smbsrv/smb_kproto.h>
30 #include <acl/acl_common.h>
31 #include <sys/fcntl.h>
32 #include <sys/flock.h>
33 #include <fs/fs_subr.h>
34 
35 extern caller_context_t smb_ct;
36 
37 static int smb_fsop_create_stream(smb_request_t *, cred_t *, smb_node_t *,
38     char *, char *, int, smb_attr_t *, smb_node_t **);
39 
40 static int smb_fsop_create_file(smb_request_t *, cred_t *, smb_node_t *,
41     char *, int, smb_attr_t *, smb_node_t **);
42 
43 #ifdef	_KERNEL
44 static int smb_fsop_create_with_sd(smb_request_t *, cred_t *, smb_node_t *,
45     char *, smb_attr_t *, smb_node_t **, smb_fssd_t *);
46 static int smb_fsop_sdinherit(smb_request_t *, smb_node_t *, smb_fssd_t *);
47 #endif	/* _KERNEL */
48 
49 /*
50  * The smb_fsop_* functions have knowledge of CIFS semantics.
51  *
52  * The smb_vop_* functions have minimal knowledge of CIFS semantics and
53  * serve as an interface to the VFS layer.
54  *
55  * Hence, smb_request_t and smb_node_t structures should not be passed
56  * from the smb_fsop_* layer to the smb_vop_* layer.
57  *
58  * In general, CIFS service code should only ever call smb_fsop_*
59  * functions directly, and never smb_vop_* functions directly.
60  *
61  * smb_fsop_* functions should call smb_vop_* functions where possible, instead
62  * of their smb_fsop_* counterparts.  However, there are times when
63  * this cannot be avoided.
64  */
65 
66 /*
67  * Note: Stream names cannot be mangled.
68  */
69 
70 /*
71  * smb_fsop_amask_to_omode
72  *
73  * Convert the access mask to the open mode (for use
74  * with the VOP_OPEN call).
75  *
76  * Note that opening a file for attribute only access
77  * will also translate into an FREAD or FWRITE open mode
78  * (i.e., it's not just for data).
79  *
80  * This is needed so that opens are tracked appropriately
81  * for oplock processing.
82  */
83 
84 int
85 smb_fsop_amask_to_omode(uint32_t access)
86 {
87 	int mode = 0;
88 
89 	if (access & (FILE_READ_DATA | FILE_EXECUTE |
90 	    FILE_READ_ATTRIBUTES | FILE_READ_EA))
91 		mode |= FREAD;
92 
93 	if (access & (FILE_WRITE_DATA | FILE_APPEND_DATA |
94 	    FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA))
95 		mode |= FWRITE;
96 
97 	if (access & FILE_APPEND_DATA)
98 		mode |= FAPPEND;
99 
100 	return (mode);
101 }
102 
103 int
104 smb_fsop_open(smb_node_t *node, int mode, cred_t *cred)
105 {
106 	/*
107 	 * Assuming that the same vnode is returned as we had before.
108 	 * (I.e., with certain types of files or file systems, a
109 	 * different vnode might be returned by VOP_OPEN)
110 	 */
111 	return (smb_vop_open(&node->vp, mode, cred));
112 }
113 
114 void
115 smb_fsop_close(smb_node_t *node, int mode, cred_t *cred)
116 {
117 	smb_vop_close(node->vp, mode, cred);
118 }
119 
120 #ifdef	_KERNEL
121 static int
122 smb_fsop_create_with_sd(smb_request_t *sr, cred_t *cr,
123     smb_node_t *dnode, char *name,
124     smb_attr_t *attr, smb_node_t **ret_snode, smb_fssd_t *fs_sd)
125 {
126 	vsecattr_t *vsap;
127 	vsecattr_t vsecattr;
128 	smb_attr_t set_attr;
129 	acl_t *acl, *dacl, *sacl;
130 	vnode_t *vp;
131 	cred_t *kcr = zone_kcred();
132 	int aclbsize = 0;	/* size of acl list in bytes */
133 	int flags = 0;
134 	int rc;
135 	boolean_t is_dir;
136 
137 	ASSERT(fs_sd);
138 
139 	if (SMB_TREE_IS_CASEINSENSITIVE(sr))
140 		flags = SMB_IGNORE_CASE;
141 	if (SMB_TREE_SUPPORTS_CATIA(sr))
142 		flags |= SMB_CATIA;
143 
144 	ASSERT(cr);
145 
146 	is_dir = ((fs_sd->sd_flags & SMB_FSSD_FLAGS_DIR) != 0);
147 
148 	if (smb_tree_has_feature(sr->tid_tree, SMB_TREE_ACLONCREATE)) {
149 		if (fs_sd->sd_secinfo & SMB_ACL_SECINFO) {
150 			dacl = fs_sd->sd_zdacl;
151 			sacl = fs_sd->sd_zsacl;
152 			ASSERT(dacl || sacl);
153 			if (dacl && sacl) {
154 				acl = smb_fsacl_merge(dacl, sacl);
155 			} else if (dacl) {
156 				acl = dacl;
157 			} else {
158 				acl = sacl;
159 			}
160 
161 			rc = smb_fsacl_to_vsa(acl, &vsecattr, &aclbsize);
162 
163 			if (dacl && sacl)
164 				acl_free(acl);
165 
166 			if (rc != 0)
167 				return (rc);
168 
169 			vsap = &vsecattr;
170 		} else {
171 			vsap = NULL;
172 		}
173 
174 		/* The tree ACEs may prevent a create */
175 		rc = EACCES;
176 		if (is_dir) {
177 			if (SMB_TREE_HAS_ACCESS(sr, ACE_ADD_SUBDIRECTORY) != 0)
178 				rc = smb_vop_mkdir(dnode->vp, name, attr,
179 				    &vp, flags, cr, vsap);
180 		} else {
181 			if (SMB_TREE_HAS_ACCESS(sr, ACE_ADD_FILE) != 0)
182 				rc = smb_vop_create(dnode->vp, name, attr,
183 				    &vp, flags, cr, vsap);
184 		}
185 
186 		if (vsap != NULL)
187 			kmem_free(vsap->vsa_aclentp, aclbsize);
188 
189 		if (rc != 0)
190 			return (rc);
191 
192 		set_attr.sa_mask = 0;
193 
194 		/*
195 		 * Ideally we should be able to specify the owner and owning
196 		 * group at create time along with the ACL. Since we cannot
197 		 * do that right now, kcred is passed to smb_vop_setattr so it
198 		 * doesn't fail due to lack of permission.
199 		 */
200 		if (fs_sd->sd_secinfo & SMB_OWNER_SECINFO) {
201 			set_attr.sa_vattr.va_uid = fs_sd->sd_uid;
202 			set_attr.sa_mask |= SMB_AT_UID;
203 		}
204 
205 		if (fs_sd->sd_secinfo & SMB_GROUP_SECINFO) {
206 			set_attr.sa_vattr.va_gid = fs_sd->sd_gid;
207 			set_attr.sa_mask |= SMB_AT_GID;
208 		}
209 
210 		if (set_attr.sa_mask)
211 			rc = smb_vop_setattr(vp, NULL, &set_attr, 0, kcr);
212 
213 		if (rc == 0) {
214 			*ret_snode = smb_node_lookup(sr, &sr->arg.open, cr, vp,
215 			    name, dnode, NULL);
216 
217 			if (*ret_snode == NULL)
218 				rc = ENOMEM;
219 
220 			VN_RELE(vp);
221 		}
222 	} else {
223 		/*
224 		 * For filesystems that don't support ACL-on-create, try
225 		 * to set the specified SD after create, which could actually
226 		 * fail because of conflicts between inherited security
227 		 * attributes upon creation and the specified SD.
228 		 *
229 		 * Passing kcred to smb_fsop_sdwrite() to overcome this issue.
230 		 */
231 
232 		if (is_dir) {
233 			rc = smb_vop_mkdir(dnode->vp, name, attr, &vp,
234 			    flags, cr, NULL);
235 		} else {
236 			rc = smb_vop_create(dnode->vp, name, attr, &vp,
237 			    flags, cr, NULL);
238 		}
239 
240 		if (rc != 0)
241 			return (rc);
242 
243 		*ret_snode = smb_node_lookup(sr, &sr->arg.open, cr, vp,
244 		    name, dnode, NULL);
245 
246 		if (*ret_snode != NULL) {
247 			if (!smb_tree_has_feature(sr->tid_tree,
248 			    SMB_TREE_NFS_MOUNTED))
249 				rc = smb_fsop_sdwrite(sr, kcr, *ret_snode,
250 				    fs_sd, 1);
251 		} else {
252 			rc = ENOMEM;
253 		}
254 
255 		VN_RELE(vp);
256 	}
257 
258 	if (rc != 0) {
259 		if (is_dir)
260 			(void) smb_vop_rmdir(dnode->vp, name, flags, cr);
261 		else
262 			(void) smb_vop_remove(dnode->vp, name, flags, cr);
263 	}
264 
265 	return (rc);
266 }
267 #endif	/* _KERNEL */
268 
269 /*
270  * smb_fsop_create
271  *
272  * All SMB functions should use this wrapper to ensure that
273  * all the smb_vop_creates are performed with the appropriate credentials.
274  * Please document any direct calls to explain the reason for avoiding
275  * this wrapper.
276  *
277  * *ret_snode is returned with a reference upon success.  No reference is
278  * taken if an error is returned.
279  */
280 int
281 smb_fsop_create(smb_request_t *sr, cred_t *cr, smb_node_t *dnode,
282     char *name, smb_attr_t *attr, smb_node_t **ret_snode)
283 {
284 	int	rc = 0;
285 	int	flags = 0;
286 	char	*fname, *sname;
287 	char	*longname = NULL;
288 
289 	ASSERT(cr);
290 	ASSERT(dnode);
291 	ASSERT(dnode->n_magic == SMB_NODE_MAGIC);
292 	ASSERT(dnode->n_state != SMB_NODE_STATE_DESTROYING);
293 
294 	ASSERT(ret_snode);
295 	*ret_snode = 0;
296 
297 	ASSERT(name);
298 	if (*name == 0)
299 		return (EINVAL);
300 
301 	ASSERT(sr);
302 	ASSERT(sr->tid_tree);
303 
304 	if (SMB_TREE_CONTAINS_NODE(sr, dnode) == 0)
305 		return (EACCES);
306 
307 	if (SMB_TREE_IS_READONLY(sr))
308 		return (EROFS);
309 
310 	if (SMB_TREE_IS_CASEINSENSITIVE(sr))
311 		flags = SMB_IGNORE_CASE;
312 	if (SMB_TREE_SUPPORTS_CATIA(sr))
313 		flags |= SMB_CATIA;
314 	if (SMB_TREE_SUPPORTS_ABE(sr))
315 		flags |= SMB_ABE;
316 
317 	if (smb_is_stream_name(name)) {
318 		fname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
319 		sname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
320 		smb_stream_parse_name(name, fname, sname);
321 
322 		rc = smb_fsop_create_stream(sr, cr, dnode,
323 		    fname, sname, flags, attr, ret_snode);
324 
325 		kmem_free(fname, MAXNAMELEN);
326 		kmem_free(sname, MAXNAMELEN);
327 		return (rc);
328 	}
329 
330 	/* Not a named stream */
331 
332 	if (SMB_TREE_SUPPORTS_SHORTNAMES(sr) && smb_maybe_mangled(name)) {
333 		longname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
334 		rc = smb_unmangle(dnode, name, longname, MAXNAMELEN, flags);
335 		kmem_free(longname, MAXNAMELEN);
336 
337 		if (rc == 0)
338 			rc = EEXIST;
339 		if (rc != ENOENT)
340 			return (rc);
341 	}
342 
343 	rc = smb_fsop_create_file(sr, cr, dnode, name, flags,
344 	    attr, ret_snode);
345 	return (rc);
346 
347 }
348 
349 
350 /*
351  * smb_fsop_create_stream
352  *
353  * Create NTFS named stream file (sname) on unnamed stream
354  * file (fname), creating the unnamed stream file if it
355  * doesn't exist.
356  * If we created the unnamed stream file and then creation
357  * of the named stream file fails, we delete the unnamed stream.
358  * Since we use the real file name for the smb_vop_remove we
359  * clear the SMB_IGNORE_CASE flag to ensure a case sensitive
360  * match.
361  *
362  * The second parameter of smb_vop_setattr() is set to
363  * NULL, even though an unnamed stream exists.  This is
364  * because we want to set the UID and GID on the named
365  * stream in this case for consistency with the (unnamed
366  * stream) file (see comments for smb_vop_setattr()).
367  */
368 static int
369 smb_fsop_create_stream(smb_request_t *sr, cred_t *cr,
370     smb_node_t *dnode, char *fname, char *sname, int flags,
371     smb_attr_t *attr, smb_node_t **ret_snode)
372 {
373 	smb_attr_t	fattr;
374 	smb_node_t	*fnode;
375 	vnode_t		*xattrdvp;
376 	vnode_t		*vp;
377 	cred_t		*kcr = zone_kcred();
378 	int		rc = 0;
379 	boolean_t	fcreate = B_FALSE;
380 
381 	/* Look up / create the unnamed stream, fname */
382 	rc = smb_fsop_lookup(sr, cr, flags | SMB_FOLLOW_LINKS,
383 	    sr->tid_tree->t_snode, dnode, fname, &fnode);
384 	if (rc == 0) {
385 		if (smb_fsop_access(sr, sr->user_cr, fnode,
386 		    sr->sr_open.desired_access) != 0)
387 			rc = EACCES;
388 	} else if (rc == ENOENT) {
389 		fcreate = B_TRUE;
390 		rc = smb_fsop_create_file(sr, cr, dnode, fname, flags,
391 		    attr, &fnode);
392 	}
393 	if (rc != 0)
394 		return (rc);
395 
396 	fattr.sa_mask = SMB_AT_UID | SMB_AT_GID;
397 	rc = smb_vop_getattr(fnode->vp, NULL, &fattr, 0, kcr);
398 
399 	if (rc == 0) {
400 		/* create the named stream, sname */
401 		rc = smb_vop_stream_create(fnode->vp, sname, attr,
402 		    &vp, &xattrdvp, flags, cr);
403 	}
404 	if (rc != 0) {
405 		if (fcreate) {
406 			flags &= ~SMB_IGNORE_CASE;
407 			(void) smb_vop_remove(dnode->vp,
408 			    fnode->od_name, flags, cr);
409 		}
410 		smb_node_release(fnode);
411 		return (rc);
412 	}
413 
414 	attr->sa_vattr.va_uid = fattr.sa_vattr.va_uid;
415 	attr->sa_vattr.va_gid = fattr.sa_vattr.va_gid;
416 	attr->sa_mask = SMB_AT_UID | SMB_AT_GID;
417 
418 	rc = smb_vop_setattr(vp, NULL, attr, 0, kcr);
419 	if (rc != 0) {
420 		smb_node_release(fnode);
421 		return (rc);
422 	}
423 
424 	*ret_snode = smb_stream_node_lookup(sr, cr, fnode, xattrdvp,
425 	    vp, sname);
426 
427 	smb_node_release(fnode);
428 	VN_RELE(xattrdvp);
429 	VN_RELE(vp);
430 
431 	if (*ret_snode == NULL)
432 		rc = ENOMEM;
433 
434 	/* notify change to the unnamed stream */
435 	if (rc == 0)
436 		smb_node_notify_change(dnode,
437 		    FILE_ACTION_ADDED_STREAM, fname);
438 
439 	return (rc);
440 }
441 
442 /*
443  * smb_fsop_create_file
444  */
445 static int
446 smb_fsop_create_file(smb_request_t *sr, cred_t *cr,
447     smb_node_t *dnode, char *name, int flags,
448     smb_attr_t *attr, smb_node_t **ret_snode)
449 {
450 	smb_arg_open_t	*op = &sr->sr_open;
451 	vnode_t		*vp;
452 	int		rc;
453 
454 #ifdef	_KERNEL
455 	smb_fssd_t	fs_sd;
456 	uint32_t	secinfo;
457 	uint32_t	status;
458 
459 	if (op->sd) {
460 		/*
461 		 * SD sent by client in Windows format. Needs to be
462 		 * converted to FS format. No inheritance.
463 		 */
464 		secinfo = smb_sd_get_secinfo(op->sd);
465 		smb_fssd_init(&fs_sd, secinfo, 0);
466 
467 		status = smb_sd_tofs(op->sd, &fs_sd);
468 		if (status == NT_STATUS_SUCCESS) {
469 			rc = smb_fsop_create_with_sd(sr, cr, dnode,
470 			    name, attr, ret_snode, &fs_sd);
471 		} else {
472 			rc = EINVAL;
473 		}
474 		smb_fssd_term(&fs_sd);
475 	} else if (sr->tid_tree->t_acltype == ACE_T) {
476 		/*
477 		 * No incoming SD and filesystem is ZFS
478 		 * Server applies Windows inheritance rules,
479 		 * see smb_fsop_sdinherit() comments as to why.
480 		 */
481 		smb_fssd_init(&fs_sd, SMB_ACL_SECINFO, 0);
482 		rc = smb_fsop_sdinherit(sr, dnode, &fs_sd);
483 		if (rc == 0) {
484 			rc = smb_fsop_create_with_sd(sr, cr, dnode,
485 			    name, attr, ret_snode, &fs_sd);
486 		}
487 
488 		smb_fssd_term(&fs_sd);
489 	} else
490 #endif	/* _KERNEL */
491 	{
492 		/*
493 		 * No incoming SD and filesystem is not ZFS
494 		 * let the filesystem handles the inheritance.
495 		 */
496 		rc = smb_vop_create(dnode->vp, name, attr, &vp,
497 		    flags, cr, NULL);
498 
499 		if (rc == 0) {
500 			*ret_snode = smb_node_lookup(sr, op, cr, vp,
501 			    name, dnode, NULL);
502 
503 			if (*ret_snode == NULL)
504 				rc = ENOMEM;
505 
506 			VN_RELE(vp);
507 		}
508 
509 	}
510 
511 	if (rc == 0)
512 		smb_node_notify_change(dnode, FILE_ACTION_ADDED, name);
513 
514 	return (rc);
515 }
516 
517 /*
518  * smb_fsop_mkdir
519  *
520  * All SMB functions should use this wrapper to ensure that
521  * the the calls are performed with the appropriate credentials.
522  * Please document any direct call to explain the reason
523  * for avoiding this wrapper.
524  *
525  * It is assumed that a reference exists on snode coming into this routine.
526  *
527  * *ret_snode is returned with a reference upon success.  No reference is
528  * taken if an error is returned.
529  */
530 int
531 smb_fsop_mkdir(
532     smb_request_t *sr,
533     cred_t *cr,
534     smb_node_t *dnode,
535     char *name,
536     smb_attr_t *attr,
537     smb_node_t **ret_snode)
538 {
539 	struct open_param *op = &sr->arg.open;
540 	char *longname;
541 	vnode_t *vp;
542 	int flags = 0;
543 	int rc;
544 
545 #ifdef	_KERNEL
546 	smb_fssd_t fs_sd;
547 	uint32_t secinfo;
548 	uint32_t status;
549 #endif	/* _KERNEL */
550 
551 	ASSERT(cr);
552 	ASSERT(dnode);
553 	ASSERT(dnode->n_magic == SMB_NODE_MAGIC);
554 	ASSERT(dnode->n_state != SMB_NODE_STATE_DESTROYING);
555 
556 	ASSERT(ret_snode);
557 	*ret_snode = 0;
558 
559 	ASSERT(name);
560 	if (*name == 0)
561 		return (EINVAL);
562 
563 	ASSERT(sr);
564 	ASSERT(sr->tid_tree);
565 
566 	if (SMB_TREE_CONTAINS_NODE(sr, dnode) == 0)
567 		return (EACCES);
568 
569 	if (SMB_TREE_IS_READONLY(sr))
570 		return (EROFS);
571 	if (SMB_TREE_SUPPORTS_CATIA(sr))
572 		flags |= SMB_CATIA;
573 	if (SMB_TREE_SUPPORTS_ABE(sr))
574 		flags |= SMB_ABE;
575 
576 	if (SMB_TREE_SUPPORTS_SHORTNAMES(sr) && smb_maybe_mangled(name)) {
577 		longname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
578 		rc = smb_unmangle(dnode, name, longname, MAXNAMELEN, flags);
579 		kmem_free(longname, MAXNAMELEN);
580 
581 		/*
582 		 * If the name passed in by the client has an unmangled
583 		 * equivalent that is found in the specified directory,
584 		 * then the mkdir cannot succeed.  Return EEXIST.
585 		 *
586 		 * Only if ENOENT is returned will a mkdir be attempted.
587 		 */
588 
589 		if (rc == 0)
590 			rc = EEXIST;
591 
592 		if (rc != ENOENT)
593 			return (rc);
594 	}
595 
596 	if (SMB_TREE_IS_CASEINSENSITIVE(sr))
597 		flags = SMB_IGNORE_CASE;
598 
599 #ifdef	_KERNEL
600 	if (op->sd) {
601 		/*
602 		 * SD sent by client in Windows format. Needs to be
603 		 * converted to FS format. No inheritance.
604 		 */
605 		secinfo = smb_sd_get_secinfo(op->sd);
606 		smb_fssd_init(&fs_sd, secinfo, SMB_FSSD_FLAGS_DIR);
607 
608 		status = smb_sd_tofs(op->sd, &fs_sd);
609 		if (status == NT_STATUS_SUCCESS) {
610 			rc = smb_fsop_create_with_sd(sr, cr, dnode,
611 			    name, attr, ret_snode, &fs_sd);
612 		}
613 		else
614 			rc = EINVAL;
615 		smb_fssd_term(&fs_sd);
616 	} else if (sr->tid_tree->t_acltype == ACE_T) {
617 		/*
618 		 * No incoming SD and filesystem is ZFS
619 		 * Server applies Windows inheritance rules,
620 		 * see smb_fsop_sdinherit() comments as to why.
621 		 */
622 		smb_fssd_init(&fs_sd, SMB_ACL_SECINFO, SMB_FSSD_FLAGS_DIR);
623 		rc = smb_fsop_sdinherit(sr, dnode, &fs_sd);
624 		if (rc == 0) {
625 			rc = smb_fsop_create_with_sd(sr, cr, dnode,
626 			    name, attr, ret_snode, &fs_sd);
627 		}
628 
629 		smb_fssd_term(&fs_sd);
630 
631 	} else
632 #endif	/* _KERNEL */
633 	{
634 		rc = smb_vop_mkdir(dnode->vp, name, attr, &vp, flags, cr,
635 		    NULL);
636 
637 		if (rc == 0) {
638 			*ret_snode = smb_node_lookup(sr, op, cr, vp, name,
639 			    dnode, NULL);
640 
641 			if (*ret_snode == NULL)
642 				rc = ENOMEM;
643 
644 			VN_RELE(vp);
645 		}
646 	}
647 
648 	if (rc == 0)
649 		smb_node_notify_change(dnode, FILE_ACTION_ADDED, name);
650 
651 	return (rc);
652 }
653 
654 /*
655  * smb_fsop_remove
656  *
657  * All SMB functions should use this wrapper to ensure that
658  * the the calls are performed with the appropriate credentials.
659  * Please document any direct call to explain the reason
660  * for avoiding this wrapper.
661  *
662  * It is assumed that a reference exists on snode coming into this routine.
663  *
664  * A null smb_request might be passed to this function.
665  */
666 int
667 smb_fsop_remove(
668     smb_request_t	*sr,
669     cred_t		*cr,
670     smb_node_t		*dnode,
671     char		*name,
672     uint32_t		flags)
673 {
674 	smb_node_t	*fnode;
675 	char		*longname;
676 	char		*fname;
677 	char		*sname;
678 	int		rc;
679 
680 	ASSERT(cr);
681 	/*
682 	 * The state of the node could be SMB_NODE_STATE_DESTROYING if this
683 	 * function is called during the deletion of the node (because of
684 	 * DELETE_ON_CLOSE).
685 	 */
686 	ASSERT(dnode);
687 	ASSERT(dnode->n_magic == SMB_NODE_MAGIC);
688 
689 	if (SMB_TREE_CONTAINS_NODE(sr, dnode) == 0 ||
690 	    SMB_TREE_HAS_ACCESS(sr, ACE_DELETE) == 0)
691 		return (EACCES);
692 
693 	if (SMB_TREE_IS_READONLY(sr))
694 		return (EROFS);
695 
696 	fname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
697 	sname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
698 
699 	if (dnode->flags & NODE_XATTR_DIR) {
700 		fnode = dnode->n_dnode;
701 		rc = smb_vop_stream_remove(fnode->vp, name, flags, cr);
702 
703 		/* notify change to the unnamed stream */
704 		if ((rc == 0) && fnode->n_dnode) {
705 			smb_node_notify_change(fnode->n_dnode,
706 			    FILE_ACTION_REMOVED_STREAM, fnode->od_name);
707 		}
708 	} else if (smb_is_stream_name(name)) {
709 		smb_stream_parse_name(name, fname, sname);
710 
711 		/*
712 		 * Look up the unnamed stream (i.e. fname).
713 		 * Unmangle processing will be done on fname
714 		 * as well as any link target.
715 		 */
716 
717 		rc = smb_fsop_lookup(sr, cr, flags | SMB_FOLLOW_LINKS,
718 		    sr->tid_tree->t_snode, dnode, fname, &fnode);
719 
720 		if (rc != 0) {
721 			kmem_free(fname, MAXNAMELEN);
722 			kmem_free(sname, MAXNAMELEN);
723 			return (rc);
724 		}
725 
726 		/*
727 		 * XXX
728 		 * Need to find out what permission is required by NTFS
729 		 * to remove a stream.
730 		 */
731 		rc = smb_vop_stream_remove(fnode->vp, sname, flags, cr);
732 
733 		smb_node_release(fnode);
734 
735 		/* notify change to the unnamed stream */
736 		if (rc == 0) {
737 			smb_node_notify_change(dnode,
738 			    FILE_ACTION_REMOVED_STREAM, fname);
739 		}
740 	} else {
741 		rc = smb_vop_remove(dnode->vp, name, flags, cr);
742 
743 		if (rc == ENOENT) {
744 			if (!SMB_TREE_SUPPORTS_SHORTNAMES(sr) ||
745 			    !smb_maybe_mangled(name)) {
746 				kmem_free(fname, MAXNAMELEN);
747 				kmem_free(sname, MAXNAMELEN);
748 				return (rc);
749 			}
750 			longname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
751 
752 			if (SMB_TREE_SUPPORTS_ABE(sr))
753 				flags |= SMB_ABE;
754 
755 			rc = smb_unmangle(dnode, name, longname, MAXNAMELEN,
756 			    flags);
757 
758 			if (rc == 0) {
759 				/*
760 				 * longname is the real (case-sensitive)
761 				 * on-disk name.
762 				 * We make sure we do a remove on this exact
763 				 * name, as the name was mangled and denotes
764 				 * a unique file.
765 				 */
766 				flags &= ~SMB_IGNORE_CASE;
767 				rc = smb_vop_remove(dnode->vp, longname,
768 				    flags, cr);
769 			}
770 			kmem_free(longname, MAXNAMELEN);
771 		}
772 		if (rc == 0) {
773 			smb_node_notify_change(dnode,
774 			    FILE_ACTION_REMOVED, name);
775 		}
776 	}
777 
778 	kmem_free(fname, MAXNAMELEN);
779 	kmem_free(sname, MAXNAMELEN);
780 
781 	return (rc);
782 }
783 
784 /*
785  * smb_fsop_remove_streams
786  *
787  * This function removes a file's streams without removing the
788  * file itself.
789  *
790  * It is assumed that fnode is not a link.
791  */
792 uint32_t
793 smb_fsop_remove_streams(smb_request_t *sr, cred_t *cr, smb_node_t *fnode)
794 {
795 	int rc, flags = 0;
796 	smb_odir_t *od;
797 	smb_odirent_t *odirent;
798 	uint32_t status;
799 	boolean_t eos;
800 
801 	ASSERT(sr);
802 	ASSERT(cr);
803 	ASSERT(fnode);
804 	ASSERT(fnode->n_magic == SMB_NODE_MAGIC);
805 	ASSERT(fnode->n_state != SMB_NODE_STATE_DESTROYING);
806 
807 	if (SMB_TREE_CONTAINS_NODE(sr, fnode) == 0)
808 		return (NT_STATUS_ACCESS_DENIED);
809 
810 	if (SMB_TREE_IS_READONLY(sr))
811 		return (NT_STATUS_ACCESS_DENIED);
812 
813 	if (SMB_TREE_IS_CASEINSENSITIVE(sr))
814 		flags = SMB_IGNORE_CASE;
815 
816 	if (SMB_TREE_SUPPORTS_CATIA(sr))
817 		flags |= SMB_CATIA;
818 
819 	status = smb_odir_openat(sr, fnode, &od);
820 	switch (status) {
821 	case 0:
822 		break;
823 	case NT_STATUS_OBJECT_NAME_NOT_FOUND:
824 	case NT_STATUS_NO_SUCH_FILE:
825 	case NT_STATUS_NOT_SUPPORTED:
826 		/* No streams to remove. */
827 		return (0);
828 	default:
829 		return (status);
830 	}
831 
832 	odirent = kmem_alloc(sizeof (smb_odirent_t), KM_SLEEP);
833 	for (;;) {
834 		rc = smb_odir_read(sr, od, odirent, &eos);
835 		if ((rc != 0) || (eos))
836 			break;
837 		(void) smb_vop_remove(od->d_dnode->vp, odirent->od_name,
838 		    flags, cr);
839 	}
840 	kmem_free(odirent, sizeof (smb_odirent_t));
841 	if (eos && rc == ENOENT)
842 		rc = 0;
843 
844 	smb_odir_close(od);
845 	smb_odir_release(od);
846 	if (rc)
847 		status = smb_errno2status(rc);
848 	return (status);
849 }
850 
851 /*
852  * smb_fsop_rmdir
853  *
854  * All SMB functions should use this wrapper to ensure that
855  * the the calls are performed with the appropriate credentials.
856  * Please document any direct call to explain the reason
857  * for avoiding this wrapper.
858  *
859  * It is assumed that a reference exists on snode coming into this routine.
860  */
861 int
862 smb_fsop_rmdir(
863     smb_request_t	*sr,
864     cred_t		*cr,
865     smb_node_t		*dnode,
866     char		*name,
867     uint32_t		flags)
868 {
869 	int		rc;
870 	char		*longname;
871 
872 	ASSERT(cr);
873 	/*
874 	 * The state of the node could be SMB_NODE_STATE_DESTROYING if this
875 	 * function is called during the deletion of the node (because of
876 	 * DELETE_ON_CLOSE).
877 	 */
878 	ASSERT(dnode);
879 	ASSERT(dnode->n_magic == SMB_NODE_MAGIC);
880 
881 	if (SMB_TREE_CONTAINS_NODE(sr, dnode) == 0 ||
882 	    SMB_TREE_HAS_ACCESS(sr, ACE_DELETE_CHILD) == 0)
883 		return (EACCES);
884 
885 	if (SMB_TREE_IS_READONLY(sr))
886 		return (EROFS);
887 
888 	rc = smb_vop_rmdir(dnode->vp, name, flags, cr);
889 
890 	if (rc == ENOENT) {
891 		if (!SMB_TREE_SUPPORTS_SHORTNAMES(sr) ||
892 		    !smb_maybe_mangled(name)) {
893 			return (rc);
894 		}
895 
896 		longname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
897 
898 		if (SMB_TREE_SUPPORTS_ABE(sr))
899 			flags |= SMB_ABE;
900 		rc = smb_unmangle(dnode, name, longname, MAXNAMELEN, flags);
901 
902 		if (rc == 0) {
903 			/*
904 			 * longname is the real (case-sensitive)
905 			 * on-disk name.
906 			 * We make sure we do a rmdir on this exact
907 			 * name, as the name was mangled and denotes
908 			 * a unique directory.
909 			 */
910 			flags &= ~SMB_IGNORE_CASE;
911 			rc = smb_vop_rmdir(dnode->vp, longname, flags, cr);
912 		}
913 
914 		kmem_free(longname, MAXNAMELEN);
915 	}
916 
917 	if (rc == 0)
918 		smb_node_notify_change(dnode, FILE_ACTION_REMOVED, name);
919 
920 	return (rc);
921 }
922 
923 /*
924  * smb_fsop_getattr
925  *
926  * All SMB functions should use this wrapper to ensure that
927  * the the calls are performed with the appropriate credentials.
928  * Please document any direct call to explain the reason
929  * for avoiding this wrapper.
930  *
931  * It is assumed that a reference exists on snode coming into this routine.
932  */
933 int
934 smb_fsop_getattr(smb_request_t *sr, cred_t *cr, smb_node_t *snode,
935     smb_attr_t *attr)
936 {
937 	smb_node_t *unnamed_node;
938 	vnode_t *unnamed_vp = NULL;
939 	uint32_t status;
940 	uint32_t access = 0;
941 	int flags = 0;
942 	int rc;
943 
944 	ASSERT(cr);
945 	ASSERT(snode);
946 	ASSERT(snode->n_magic == SMB_NODE_MAGIC);
947 	ASSERT(snode->n_state != SMB_NODE_STATE_DESTROYING);
948 
949 	if (SMB_TREE_CONTAINS_NODE(sr, snode) == 0 ||
950 	    SMB_TREE_HAS_ACCESS(sr, ACE_READ_ATTRIBUTES) == 0)
951 		return (EACCES);
952 
953 	/* sr could be NULL in some cases */
954 	if (sr && sr->fid_ofile) {
955 		/* if uid and/or gid is requested */
956 		if (attr->sa_mask & (SMB_AT_UID|SMB_AT_GID))
957 			access |= READ_CONTROL;
958 
959 		/* if anything else is also requested */
960 		if (attr->sa_mask & ~(SMB_AT_UID|SMB_AT_GID))
961 			access |= FILE_READ_ATTRIBUTES;
962 
963 		status = smb_ofile_access(sr->fid_ofile, cr, access);
964 		if (status != NT_STATUS_SUCCESS)
965 			return (EACCES);
966 
967 		if (smb_tree_has_feature(sr->tid_tree,
968 		    SMB_TREE_ACEMASKONACCESS))
969 			flags = ATTR_NOACLCHECK;
970 	}
971 
972 	unnamed_node = SMB_IS_STREAM(snode);
973 
974 	if (unnamed_node) {
975 		ASSERT(unnamed_node->n_magic == SMB_NODE_MAGIC);
976 		ASSERT(unnamed_node->n_state != SMB_NODE_STATE_DESTROYING);
977 		unnamed_vp = unnamed_node->vp;
978 	}
979 
980 	rc = smb_vop_getattr(snode->vp, unnamed_vp, attr, flags, cr);
981 
982 	if ((rc == 0) && smb_node_is_dfslink(snode)) {
983 		/* a DFS link should be treated as a directory */
984 		attr->sa_dosattr |= FILE_ATTRIBUTE_DIRECTORY;
985 	}
986 
987 	return (rc);
988 }
989 
990 /*
991  * smb_fsop_link
992  *
993  * All SMB functions should use this smb_vop_link wrapper to ensure that
994  * the smb_vop_link is performed with the appropriate credentials.
995  * Please document any direct call to smb_vop_link to explain the reason
996  * for avoiding this wrapper.
997  *
998  * It is assumed that references exist on from_dnode and to_dnode coming
999  * into this routine.
1000  */
1001 int
1002 smb_fsop_link(smb_request_t *sr, cred_t *cr, smb_node_t *from_fnode,
1003     smb_node_t *to_dnode, char *to_name)
1004 {
1005 	char	*longname = NULL;
1006 	int	flags = 0;
1007 	int	rc;
1008 
1009 	ASSERT(sr);
1010 	ASSERT(sr->tid_tree);
1011 	ASSERT(cr);
1012 	ASSERT(to_dnode);
1013 	ASSERT(to_dnode->n_magic == SMB_NODE_MAGIC);
1014 	ASSERT(to_dnode->n_state != SMB_NODE_STATE_DESTROYING);
1015 	ASSERT(from_fnode);
1016 	ASSERT(from_fnode->n_magic == SMB_NODE_MAGIC);
1017 	ASSERT(from_fnode->n_state != SMB_NODE_STATE_DESTROYING);
1018 
1019 	if (SMB_TREE_CONTAINS_NODE(sr, from_fnode) == 0)
1020 		return (EACCES);
1021 
1022 	if (SMB_TREE_CONTAINS_NODE(sr, to_dnode) == 0)
1023 		return (EACCES);
1024 
1025 	if (SMB_TREE_IS_READONLY(sr))
1026 		return (EROFS);
1027 
1028 	if (SMB_TREE_IS_CASEINSENSITIVE(sr))
1029 		flags = SMB_IGNORE_CASE;
1030 	if (SMB_TREE_SUPPORTS_CATIA(sr))
1031 		flags |= SMB_CATIA;
1032 	if (SMB_TREE_SUPPORTS_ABE(sr))
1033 		flags |= SMB_ABE;
1034 
1035 	if (SMB_TREE_SUPPORTS_SHORTNAMES(sr) && smb_maybe_mangled(to_name)) {
1036 		longname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1037 		rc = smb_unmangle(to_dnode, to_name, longname,
1038 		    MAXNAMELEN, flags);
1039 		kmem_free(longname, MAXNAMELEN);
1040 
1041 		if (rc == 0)
1042 			rc = EEXIST;
1043 		if (rc != ENOENT)
1044 			return (rc);
1045 	}
1046 
1047 	rc = smb_vop_link(to_dnode->vp, from_fnode->vp, to_name, flags, cr);
1048 
1049 	if (rc == 0)
1050 		smb_node_notify_change(to_dnode, FILE_ACTION_ADDED, to_name);
1051 
1052 	return (rc);
1053 }
1054 
1055 /*
1056  * smb_fsop_rename
1057  *
1058  * All SMB functions should use this smb_vop_rename wrapper to ensure that
1059  * the smb_vop_rename is performed with the appropriate credentials.
1060  * Please document any direct call to smb_vop_rename to explain the reason
1061  * for avoiding this wrapper.
1062  *
1063  * It is assumed that references exist on from_dnode and to_dnode coming
1064  * into this routine.
1065  */
1066 int
1067 smb_fsop_rename(
1068     smb_request_t *sr,
1069     cred_t *cr,
1070     smb_node_t *from_dnode,
1071     char *from_name,
1072     smb_node_t *to_dnode,
1073     char *to_name)
1074 {
1075 	smb_node_t *from_snode;
1076 	smb_attr_t from_attr;
1077 	vnode_t *from_vp;
1078 	int flags = 0, ret_flags;
1079 	int rc;
1080 	boolean_t isdir;
1081 
1082 	ASSERT(cr);
1083 	ASSERT(from_dnode);
1084 	ASSERT(from_dnode->n_magic == SMB_NODE_MAGIC);
1085 	ASSERT(from_dnode->n_state != SMB_NODE_STATE_DESTROYING);
1086 
1087 	ASSERT(to_dnode);
1088 	ASSERT(to_dnode->n_magic == SMB_NODE_MAGIC);
1089 	ASSERT(to_dnode->n_state != SMB_NODE_STATE_DESTROYING);
1090 
1091 	if (SMB_TREE_CONTAINS_NODE(sr, from_dnode) == 0)
1092 		return (EACCES);
1093 
1094 	if (SMB_TREE_CONTAINS_NODE(sr, to_dnode) == 0)
1095 		return (EACCES);
1096 
1097 	ASSERT(sr);
1098 	ASSERT(sr->tid_tree);
1099 	if (SMB_TREE_IS_READONLY(sr))
1100 		return (EROFS);
1101 
1102 	/*
1103 	 * Note: There is no need to check SMB_TREE_IS_CASEINSENSITIVE
1104 	 * here.
1105 	 *
1106 	 * A case-sensitive rename is always done in this routine
1107 	 * because we are using the on-disk name from an earlier lookup.
1108 	 * If a mangled name was passed in by the caller (denoting a
1109 	 * deterministic lookup), then the exact file must be renamed
1110 	 * (i.e. SMB_IGNORE_CASE must not be passed to VOP_RENAME, or
1111 	 * else the underlying file system might return a "first-match"
1112 	 * on this on-disk name, possibly resulting in the wrong file).
1113 	 */
1114 
1115 	if (SMB_TREE_SUPPORTS_CATIA(sr))
1116 		flags |= SMB_CATIA;
1117 
1118 	/*
1119 	 * XXX: Lock required through smb_node_release() below?
1120 	 */
1121 
1122 	rc = smb_vop_lookup(from_dnode->vp, from_name, &from_vp, NULL,
1123 	    flags, &ret_flags, NULL, &from_attr, cr);
1124 
1125 	if (rc != 0)
1126 		return (rc);
1127 
1128 	if (from_attr.sa_dosattr & FILE_ATTRIBUTE_REPARSE_POINT) {
1129 		VN_RELE(from_vp);
1130 		return (EACCES);
1131 	}
1132 
1133 	isdir = ((from_attr.sa_dosattr & FILE_ATTRIBUTE_DIRECTORY) != 0);
1134 
1135 	if ((isdir && SMB_TREE_HAS_ACCESS(sr,
1136 	    ACE_DELETE_CHILD | ACE_ADD_SUBDIRECTORY) !=
1137 	    (ACE_DELETE_CHILD | ACE_ADD_SUBDIRECTORY)) ||
1138 	    (!isdir && SMB_TREE_HAS_ACCESS(sr, ACE_DELETE | ACE_ADD_FILE) !=
1139 	    (ACE_DELETE | ACE_ADD_FILE))) {
1140 		VN_RELE(from_vp);
1141 		return (EACCES);
1142 	}
1143 
1144 	/*
1145 	 * SMB checks access on open and retains an access granted
1146 	 * mask for use while the file is open.  ACL changes should
1147 	 * not affect access to an open file.
1148 	 *
1149 	 * If the rename is being performed on an ofile:
1150 	 * - Check the ofile's access granted mask to see if the
1151 	 *   rename is permitted - requires DELETE access.
1152 	 * - If the file system does access checking, set the
1153 	 *   ATTR_NOACLCHECK flag to ensure that the file system
1154 	 *   does not check permissions on subsequent calls.
1155 	 */
1156 	if (sr && sr->fid_ofile) {
1157 		rc = smb_ofile_access(sr->fid_ofile, cr, DELETE);
1158 		if (rc != NT_STATUS_SUCCESS) {
1159 			VN_RELE(from_vp);
1160 			return (EACCES);
1161 		}
1162 
1163 		if (smb_tree_has_feature(sr->tid_tree,
1164 		    SMB_TREE_ACEMASKONACCESS))
1165 			flags = ATTR_NOACLCHECK;
1166 	}
1167 
1168 	rc = smb_vop_rename(from_dnode->vp, from_name, to_dnode->vp,
1169 	    to_name, flags, cr);
1170 
1171 	if (rc == 0) {
1172 		from_snode = smb_node_lookup(sr, NULL, cr, from_vp, from_name,
1173 		    from_dnode, NULL);
1174 
1175 		if (from_snode == NULL) {
1176 			rc = ENOMEM;
1177 		} else {
1178 			smb_node_rename(from_dnode, from_snode,
1179 			    to_dnode, to_name);
1180 			smb_node_release(from_snode);
1181 		}
1182 	}
1183 	VN_RELE(from_vp);
1184 
1185 	if (rc == 0) {
1186 		if (from_dnode == to_dnode) {
1187 			smb_node_notify_change(from_dnode,
1188 			    FILE_ACTION_RENAMED_OLD_NAME, from_name);
1189 			smb_node_notify_change(to_dnode,
1190 			    FILE_ACTION_RENAMED_NEW_NAME, to_name);
1191 		} else {
1192 			smb_node_notify_change(from_dnode,
1193 			    FILE_ACTION_REMOVED, from_name);
1194 			smb_node_notify_change(to_dnode,
1195 			    FILE_ACTION_ADDED, to_name);
1196 		}
1197 	}
1198 
1199 	/* XXX: unlock */
1200 
1201 	return (rc);
1202 }
1203 
1204 /*
1205  * smb_fsop_setattr
1206  *
1207  * All SMB functions should use this wrapper to ensure that
1208  * the the calls are performed with the appropriate credentials.
1209  * Please document any direct call to explain the reason
1210  * for avoiding this wrapper.
1211  *
1212  * It is assumed that a reference exists on snode coming into
1213  * this function.
1214  * A null smb_request might be passed to this function.
1215  */
1216 int
1217 smb_fsop_setattr(
1218     smb_request_t	*sr,
1219     cred_t		*cr,
1220     smb_node_t		*snode,
1221     smb_attr_t		*set_attr)
1222 {
1223 	smb_node_t *unnamed_node;
1224 	vnode_t *unnamed_vp = NULL;
1225 	uint32_t status;
1226 	uint32_t access;
1227 	int rc = 0;
1228 	int flags = 0;
1229 	uint_t sa_mask;
1230 
1231 	ASSERT(cr);
1232 	ASSERT(snode);
1233 	ASSERT(snode->n_magic == SMB_NODE_MAGIC);
1234 	ASSERT(snode->n_state != SMB_NODE_STATE_DESTROYING);
1235 
1236 	if (SMB_TREE_CONTAINS_NODE(sr, snode) == 0)
1237 		return (EACCES);
1238 
1239 	if (SMB_TREE_IS_READONLY(sr))
1240 		return (EROFS);
1241 
1242 	if (SMB_TREE_HAS_ACCESS(sr,
1243 	    ACE_WRITE_ATTRIBUTES | ACE_WRITE_NAMED_ATTRS) == 0)
1244 		return (EACCES);
1245 
1246 	/*
1247 	 * SMB checks access on open and retains an access granted
1248 	 * mask for use while the file is open.  ACL changes should
1249 	 * not affect access to an open file.
1250 	 *
1251 	 * If the setattr is being performed on an ofile:
1252 	 * - Check the ofile's access granted mask to see if the
1253 	 *   setattr is permitted.
1254 	 *   UID, GID - require WRITE_OWNER
1255 	 *   SIZE, ALLOCSZ - require FILE_WRITE_DATA
1256 	 *   all other attributes require FILE_WRITE_ATTRIBUTES
1257 	 *
1258 	 * - If the file system does access checking, set the
1259 	 *   ATTR_NOACLCHECK flag to ensure that the file system
1260 	 *   does not check permissions on subsequent calls.
1261 	 */
1262 	if (sr && sr->fid_ofile) {
1263 		sa_mask = set_attr->sa_mask;
1264 		access = 0;
1265 
1266 		if (sa_mask & (SMB_AT_SIZE | SMB_AT_ALLOCSZ)) {
1267 			access |= FILE_WRITE_DATA;
1268 			sa_mask &= ~(SMB_AT_SIZE | SMB_AT_ALLOCSZ);
1269 		}
1270 
1271 		if (sa_mask & (SMB_AT_UID|SMB_AT_GID)) {
1272 			access |= WRITE_OWNER;
1273 			sa_mask &= ~(SMB_AT_UID|SMB_AT_GID);
1274 		}
1275 
1276 		if (sa_mask)
1277 			access |= FILE_WRITE_ATTRIBUTES;
1278 
1279 		status = smb_ofile_access(sr->fid_ofile, cr, access);
1280 		if (status != NT_STATUS_SUCCESS)
1281 			return (EACCES);
1282 
1283 		if (smb_tree_has_feature(sr->tid_tree,
1284 		    SMB_TREE_ACEMASKONACCESS))
1285 			flags = ATTR_NOACLCHECK;
1286 	}
1287 
1288 	unnamed_node = SMB_IS_STREAM(snode);
1289 
1290 	if (unnamed_node) {
1291 		ASSERT(unnamed_node->n_magic == SMB_NODE_MAGIC);
1292 		ASSERT(unnamed_node->n_state != SMB_NODE_STATE_DESTROYING);
1293 		unnamed_vp = unnamed_node->vp;
1294 	}
1295 
1296 	rc = smb_vop_setattr(snode->vp, unnamed_vp, set_attr, flags, cr);
1297 	return (rc);
1298 }
1299 
1300 /*
1301  * Support for SMB2 setinfo FileValidDataLengthInformation.
1302  * Free data from the specified offset to EoF.
1303  *
1304  * This can effectively truncate data.  It truncates the data
1305  * leaving the file size as it was, leaving zeros after the
1306  * offset specified here.  That is effectively modifying the
1307  * file content, so for access control this is a write.
1308  */
1309 int
1310 smb_fsop_set_data_length(
1311     smb_request_t	*sr,
1312     cred_t		*cr,
1313     smb_node_t		*node,
1314     offset_t		end_of_data)
1315 {
1316 	flock64_t flk;
1317 	uint32_t status;
1318 	uint32_t access = FILE_WRITE_DATA;
1319 	int rc;
1320 
1321 	ASSERT(cr);
1322 	ASSERT(node);
1323 	ASSERT(node->n_magic == SMB_NODE_MAGIC);
1324 	ASSERT(node->n_state != SMB_NODE_STATE_DESTROYING);
1325 
1326 	if (SMB_TREE_CONTAINS_NODE(sr, node) == 0)
1327 		return (EACCES);
1328 
1329 	if (SMB_TREE_IS_READONLY(sr))
1330 		return (EROFS);
1331 
1332 	if (SMB_TREE_HAS_ACCESS(sr, access) == 0)
1333 		return (EACCES);
1334 
1335 	/*
1336 	 * SMB checks access on open and retains an access granted
1337 	 * mask for use while the file is open.  ACL changes should
1338 	 * not affect access to an open file.
1339 	 *
1340 	 * If the setattr is being performed on an ofile:
1341 	 * - Check the ofile's access granted mask to see if this
1342 	 *   modification should be permitted (FILE_WRITE_DATA)
1343 	 */
1344 	status = smb_ofile_access(sr->fid_ofile, cr, access);
1345 	if (status != NT_STATUS_SUCCESS)
1346 		return (EACCES);
1347 
1348 	bzero(&flk, sizeof (flk));
1349 	flk.l_start = end_of_data;
1350 
1351 	rc = smb_vop_space(node->vp, F_FREESP, &flk, FWRITE, 0LL, cr);
1352 	return (rc);
1353 }
1354 
1355 /*
1356  * smb_fsop_read
1357  *
1358  * All SMB functions should use this wrapper to ensure that
1359  * the the calls are performed with the appropriate credentials.
1360  * Please document any direct call to explain the reason
1361  * for avoiding this wrapper.
1362  *
1363  * It is assumed that a reference exists on snode coming into this routine.
1364  */
1365 int
1366 smb_fsop_read(smb_request_t *sr, cred_t *cr, smb_node_t *snode, uio_t *uio)
1367 {
1368 	caller_context_t ct;
1369 	cred_t *kcr = zone_kcred();
1370 	int svmand;
1371 	int rc;
1372 
1373 	ASSERT(cr);
1374 	ASSERT(snode);
1375 	ASSERT(snode->n_magic == SMB_NODE_MAGIC);
1376 	ASSERT(snode->n_state != SMB_NODE_STATE_DESTROYING);
1377 
1378 	ASSERT(sr);
1379 	ASSERT(sr->fid_ofile);
1380 
1381 	if (SMB_TREE_HAS_ACCESS(sr, ACE_READ_DATA) == 0)
1382 		return (EACCES);
1383 
1384 	rc = smb_ofile_access(sr->fid_ofile, cr, FILE_READ_DATA);
1385 	if ((rc != NT_STATUS_SUCCESS) &&
1386 	    (sr->smb_flg2 & SMB_FLAGS2_READ_IF_EXECUTE))
1387 		rc = smb_ofile_access(sr->fid_ofile, cr, FILE_EXECUTE);
1388 
1389 	if (rc != NT_STATUS_SUCCESS)
1390 		return (EACCES);
1391 
1392 	/*
1393 	 * Streams permission are checked against the unnamed stream,
1394 	 * but in FS level they have their own permissions. To avoid
1395 	 * rejection by FS due to lack of permission on the actual
1396 	 * extended attr kcred is passed for streams.
1397 	 */
1398 	if (SMB_IS_STREAM(snode))
1399 		cr = kcr;
1400 
1401 	smb_node_start_crit(snode, RW_READER);
1402 	rc = nbl_svmand(snode->vp, kcr, &svmand);
1403 	if (rc) {
1404 		smb_node_end_crit(snode);
1405 		return (rc);
1406 	}
1407 
1408 	/*
1409 	 * Note: SMB allows a zero-byte read, which should not
1410 	 * conflict with any locks.  However nbl_lock_conflict
1411 	 * takes a zero-byte length as lock to EOF, so we must
1412 	 * special case that here.
1413 	 */
1414 	if (uio->uio_resid > 0) {
1415 		ct = smb_ct;
1416 		ct.cc_pid = sr->fid_ofile->f_uniqid;
1417 		rc = nbl_lock_conflict(snode->vp, NBL_READ, uio->uio_loffset,
1418 		    uio->uio_resid, svmand, &ct);
1419 		if (rc != 0) {
1420 			smb_node_end_crit(snode);
1421 			return (ERANGE);
1422 		}
1423 	}
1424 
1425 	rc = smb_vop_read(snode->vp, uio, cr);
1426 	smb_node_end_crit(snode);
1427 
1428 	return (rc);
1429 }
1430 
1431 /*
1432  * smb_fsop_write
1433  *
1434  * This is a wrapper function used for smb_write and smb_write_raw operations.
1435  *
1436  * It is assumed that a reference exists on snode coming into this routine.
1437  */
1438 int
1439 smb_fsop_write(
1440     smb_request_t *sr,
1441     cred_t *cr,
1442     smb_node_t *snode,
1443     uio_t *uio,
1444     uint32_t *lcount,
1445     int ioflag)
1446 {
1447 	caller_context_t ct;
1448 	smb_attr_t attr;
1449 	smb_node_t *u_node;
1450 	vnode_t *u_vp = NULL;
1451 	smb_ofile_t *of;
1452 	vnode_t *vp;
1453 	cred_t *kcr = zone_kcred();
1454 	int svmand;
1455 	int rc;
1456 
1457 	ASSERT(cr);
1458 	ASSERT(snode);
1459 	ASSERT(snode->n_magic == SMB_NODE_MAGIC);
1460 	ASSERT(snode->n_state != SMB_NODE_STATE_DESTROYING);
1461 
1462 	ASSERT(sr);
1463 	ASSERT(sr->tid_tree);
1464 	of = sr->fid_ofile;
1465 	vp = snode->vp;
1466 
1467 	if (SMB_TREE_IS_READONLY(sr))
1468 		return (EROFS);
1469 
1470 	if (SMB_TREE_HAS_ACCESS(sr, ACE_WRITE_DATA | ACE_APPEND_DATA) == 0)
1471 		return (EACCES);
1472 
1473 	rc = smb_ofile_access(of, cr, FILE_WRITE_DATA);
1474 	if (rc != NT_STATUS_SUCCESS) {
1475 		rc = smb_ofile_access(of, cr, FILE_APPEND_DATA);
1476 		if (rc != NT_STATUS_SUCCESS)
1477 			return (EACCES);
1478 	}
1479 
1480 	/*
1481 	 * Streams permission are checked against the unnamed stream,
1482 	 * but in FS level they have their own permissions. To avoid
1483 	 * rejection by FS due to lack of permission on the actual
1484 	 * extended attr kcred is passed for streams.
1485 	 */
1486 	u_node = SMB_IS_STREAM(snode);
1487 	if (u_node != NULL) {
1488 		ASSERT(u_node->n_magic == SMB_NODE_MAGIC);
1489 		ASSERT(u_node->n_state != SMB_NODE_STATE_DESTROYING);
1490 		u_vp = u_node->vp;
1491 		cr = kcr;
1492 	}
1493 
1494 	smb_node_start_crit(snode, RW_WRITER);
1495 	rc = nbl_svmand(vp, kcr, &svmand);
1496 	if (rc) {
1497 		smb_node_end_crit(snode);
1498 		return (rc);
1499 	}
1500 
1501 	/*
1502 	 * Note: SMB allows a zero-byte write, which should not
1503 	 * conflict with any locks.  However nbl_lock_conflict
1504 	 * takes a zero-byte length as lock to EOF, so we must
1505 	 * special case that here.
1506 	 */
1507 	if (uio->uio_resid > 0) {
1508 		ct = smb_ct;
1509 		ct.cc_pid = of->f_uniqid;
1510 		rc = nbl_lock_conflict(vp, NBL_WRITE, uio->uio_loffset,
1511 		    uio->uio_resid, svmand, &ct);
1512 		if (rc != 0) {
1513 			smb_node_end_crit(snode);
1514 			return (ERANGE);
1515 		}
1516 	}
1517 
1518 	rc = smb_vop_write(vp, uio, ioflag, lcount, cr);
1519 
1520 	/*
1521 	 * Once the mtime has been set via this ofile, the
1522 	 * automatic mtime changes from writes via this ofile
1523 	 * should cease, preserving the mtime that was set.
1524 	 * See: [MS-FSA] 2.1.5.14 and smb_node_setattr.
1525 	 *
1526 	 * The VFS interface does not offer a way to ask it to
1527 	 * skip the mtime updates, so we simulate the desired
1528 	 * behavior by re-setting the mtime after writes on a
1529 	 * handle where the mtime has been set.
1530 	 */
1531 	if (of->f_pending_attr.sa_mask & SMB_AT_MTIME) {
1532 		bcopy(&of->f_pending_attr, &attr, sizeof (attr));
1533 		attr.sa_mask = SMB_AT_MTIME;
1534 		(void) smb_vop_setattr(vp, u_vp, &attr, 0, kcr);
1535 	}
1536 
1537 	smb_node_end_crit(snode);
1538 
1539 	return (rc);
1540 }
1541 
1542 /*
1543  * smb_fsop_statfs
1544  *
1545  * This is a wrapper function used for stat operations.
1546  */
1547 int
1548 smb_fsop_statfs(
1549     cred_t *cr,
1550     smb_node_t *snode,
1551     struct statvfs64 *statp)
1552 {
1553 	ASSERT(cr);
1554 	ASSERT(snode);
1555 	ASSERT(snode->n_magic == SMB_NODE_MAGIC);
1556 	ASSERT(snode->n_state != SMB_NODE_STATE_DESTROYING);
1557 
1558 	return (smb_vop_statfs(snode->vp, statp, cr));
1559 }
1560 
1561 /*
1562  * smb_fsop_access
1563  *
1564  * Named streams do not have separate permissions from the associated
1565  * unnamed stream.  Thus, if node is a named stream, the permissions
1566  * check will be performed on the associated unnamed stream.
1567  *
1568  * However, our named streams do have their own quarantine attribute,
1569  * separate from that on the unnamed stream. If READ or EXECUTE
1570  * access has been requested on a named stream, an additional access
1571  * check is performed on the named stream in case it has been
1572  * quarantined.  kcred is used to avoid issues with the permissions
1573  * set on the extended attribute file representing the named stream.
1574  */
1575 int
1576 smb_fsop_access(smb_request_t *sr, cred_t *cr, smb_node_t *snode,
1577     uint32_t faccess)
1578 {
1579 	int access = 0;
1580 	int error;
1581 	vnode_t *dir_vp;
1582 	boolean_t acl_check = B_TRUE;
1583 	smb_node_t *unnamed_node;
1584 
1585 	ASSERT(sr);
1586 	ASSERT(cr);
1587 	ASSERT(snode);
1588 	ASSERT(snode->n_magic == SMB_NODE_MAGIC);
1589 	ASSERT(snode->n_state != SMB_NODE_STATE_DESTROYING);
1590 
1591 	if (SMB_TREE_IS_READONLY(sr)) {
1592 		if (faccess & (FILE_WRITE_DATA|FILE_APPEND_DATA|
1593 		    FILE_WRITE_EA|FILE_DELETE_CHILD|FILE_WRITE_ATTRIBUTES|
1594 		    DELETE|WRITE_DAC|WRITE_OWNER)) {
1595 			return (NT_STATUS_ACCESS_DENIED);
1596 		}
1597 	}
1598 
1599 	if (smb_node_is_reparse(snode) && (faccess & DELETE))
1600 		return (NT_STATUS_ACCESS_DENIED);
1601 
1602 	unnamed_node = SMB_IS_STREAM(snode);
1603 	if (unnamed_node) {
1604 		ASSERT(unnamed_node->n_magic == SMB_NODE_MAGIC);
1605 		ASSERT(unnamed_node->n_state != SMB_NODE_STATE_DESTROYING);
1606 
1607 		/*
1608 		 * Perform VREAD access check on the named stream in case it
1609 		 * is quarantined. kcred is passed to smb_vop_access so it
1610 		 * doesn't fail due to lack of permission.
1611 		 */
1612 		if (faccess & (FILE_READ_DATA | FILE_EXECUTE)) {
1613 			error = smb_vop_access(snode->vp, VREAD,
1614 			    0, NULL, zone_kcred());
1615 			if (error)
1616 				return (NT_STATUS_ACCESS_DENIED);
1617 		}
1618 
1619 		/*
1620 		 * Streams authorization should be performed against the
1621 		 * unnamed stream.
1622 		 */
1623 		snode = unnamed_node;
1624 	}
1625 
1626 	if (faccess & ACCESS_SYSTEM_SECURITY) {
1627 		/*
1628 		 * This permission is required for reading/writing SACL and
1629 		 * it's not part of DACL. It's only granted via proper
1630 		 * privileges.
1631 		 */
1632 		if ((sr->uid_user->u_privileges &
1633 		    (SMB_USER_PRIV_BACKUP |
1634 		    SMB_USER_PRIV_RESTORE |
1635 		    SMB_USER_PRIV_SECURITY)) == 0)
1636 			return (NT_STATUS_PRIVILEGE_NOT_HELD);
1637 
1638 		faccess &= ~ACCESS_SYSTEM_SECURITY;
1639 	}
1640 
1641 	/* Links don't have ACL */
1642 	if ((!smb_tree_has_feature(sr->tid_tree, SMB_TREE_ACEMASKONACCESS)) ||
1643 	    smb_node_is_symlink(snode))
1644 		acl_check = B_FALSE;
1645 
1646 	/* Deny access based on the share access mask */
1647 
1648 	if ((faccess & ~sr->tid_tree->t_access) != 0)
1649 		return (NT_STATUS_ACCESS_DENIED);
1650 
1651 	if (acl_check) {
1652 		dir_vp = (snode->n_dnode) ? snode->n_dnode->vp : NULL;
1653 		error = smb_vop_access(snode->vp, faccess, V_ACE_MASK, dir_vp,
1654 		    cr);
1655 	} else {
1656 		/*
1657 		 * FS doesn't understand 32-bit mask, need to map
1658 		 */
1659 		if (faccess & (FILE_WRITE_DATA | FILE_APPEND_DATA))
1660 			access |= VWRITE;
1661 
1662 		if (faccess & FILE_READ_DATA)
1663 			access |= VREAD;
1664 
1665 		if (faccess & FILE_EXECUTE)
1666 			access |= VEXEC;
1667 
1668 		error = smb_vop_access(snode->vp, access, 0, NULL, cr);
1669 	}
1670 
1671 	return ((error) ? NT_STATUS_ACCESS_DENIED : NT_STATUS_SUCCESS);
1672 }
1673 
1674 /*
1675  * smb_fsop_lookup_name()
1676  *
1677  * If name indicates that the file is a stream file, perform
1678  * stream specific lookup, otherwise call smb_fsop_lookup.
1679  *
1680  * Return an error if the looked-up file is in outside the tree.
1681  * (Required when invoked from open path.)
1682  *
1683  * Case sensitivity flags (SMB_IGNORE_CASE, SMB_CASE_SENSITIVE):
1684  * if SMB_CASE_SENSITIVE is set, the SMB_IGNORE_CASE flag will NOT be set
1685  * based on the tree's case sensitivity. However, if the SMB_IGNORE_CASE
1686  * flag is set in the flags value passed as a parameter, a case insensitive
1687  * lookup WILL be done (regardless of whether SMB_CASE_SENSITIVE is set
1688  * or not).
1689  */
1690 
1691 int
1692 smb_fsop_lookup_name(
1693     smb_request_t *sr,
1694     cred_t	*cr,
1695     int		flags,
1696     smb_node_t	*root_node,
1697     smb_node_t	*dnode,
1698     char	*name,
1699     smb_node_t	**ret_snode)
1700 {
1701 	smb_node_t	*fnode;
1702 	vnode_t		*xattrdirvp;
1703 	vnode_t		*vp;
1704 	char		*od_name;
1705 	char		*fname;
1706 	char		*sname;
1707 	int		rc;
1708 
1709 	ASSERT(cr);
1710 	ASSERT(dnode);
1711 	ASSERT(dnode->n_magic == SMB_NODE_MAGIC);
1712 	ASSERT(dnode->n_state != SMB_NODE_STATE_DESTROYING);
1713 
1714 	/*
1715 	 * The following check is required for streams processing, below
1716 	 */
1717 
1718 	if (!(flags & SMB_CASE_SENSITIVE)) {
1719 		if (SMB_TREE_IS_CASEINSENSITIVE(sr))
1720 			flags |= SMB_IGNORE_CASE;
1721 	}
1722 
1723 	fname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1724 	sname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1725 
1726 	if (smb_is_stream_name(name)) {
1727 		smb_stream_parse_name(name, fname, sname);
1728 
1729 		/*
1730 		 * Look up the unnamed stream (i.e. fname).
1731 		 * Unmangle processing will be done on fname
1732 		 * as well as any link target.
1733 		 */
1734 		rc = smb_fsop_lookup(sr, cr, flags, root_node, dnode,
1735 		    fname, &fnode);
1736 
1737 		if (rc != 0) {
1738 			kmem_free(fname, MAXNAMELEN);
1739 			kmem_free(sname, MAXNAMELEN);
1740 			return (rc);
1741 		}
1742 
1743 		od_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1744 
1745 		/*
1746 		 * od_name is the on-disk name of the stream, except
1747 		 * without the prepended stream prefix (SMB_STREAM_PREFIX)
1748 		 */
1749 
1750 		/*
1751 		 * XXX
1752 		 * What permissions NTFS requires for stream lookup if any?
1753 		 */
1754 		rc = smb_vop_stream_lookup(fnode->vp, sname, &vp, od_name,
1755 		    &xattrdirvp, flags, root_node->vp, cr);
1756 
1757 		if (rc != 0) {
1758 			smb_node_release(fnode);
1759 			kmem_free(fname, MAXNAMELEN);
1760 			kmem_free(sname, MAXNAMELEN);
1761 			kmem_free(od_name, MAXNAMELEN);
1762 			return (rc);
1763 		}
1764 
1765 		*ret_snode = smb_stream_node_lookup(sr, cr, fnode, xattrdirvp,
1766 		    vp, od_name);
1767 
1768 		kmem_free(od_name, MAXNAMELEN);
1769 		smb_node_release(fnode);
1770 		VN_RELE(xattrdirvp);
1771 		VN_RELE(vp);
1772 
1773 		if (*ret_snode == NULL) {
1774 			kmem_free(fname, MAXNAMELEN);
1775 			kmem_free(sname, MAXNAMELEN);
1776 			return (ENOMEM);
1777 		}
1778 	} else {
1779 		rc = smb_fsop_lookup(sr, cr, flags, root_node, dnode, name,
1780 		    ret_snode);
1781 	}
1782 
1783 	if (rc == 0) {
1784 		ASSERT(ret_snode);
1785 		if (SMB_TREE_CONTAINS_NODE(sr, *ret_snode) == 0) {
1786 			smb_node_release(*ret_snode);
1787 			*ret_snode = NULL;
1788 			rc = EACCES;
1789 		}
1790 	}
1791 
1792 	kmem_free(fname, MAXNAMELEN);
1793 	kmem_free(sname, MAXNAMELEN);
1794 
1795 	return (rc);
1796 }
1797 
1798 /*
1799  * smb_fsop_lookup
1800  *
1801  * All SMB functions should use this smb_vop_lookup wrapper to ensure that
1802  * the smb_vop_lookup is performed with the appropriate credentials and using
1803  * case insensitive compares. Please document any direct call to smb_vop_lookup
1804  * to explain the reason for avoiding this wrapper.
1805  *
1806  * It is assumed that a reference exists on dnode coming into this routine
1807  * (and that it is safe from deallocation).
1808  *
1809  * Same with the root_node.
1810  *
1811  * *ret_snode is returned with a reference upon success.  No reference is
1812  * taken if an error is returned.
1813  *
1814  * Note: The returned ret_snode may be in a child mount.  This is ok for
1815  * readdir.
1816  *
1817  * Other smb_fsop_* routines will call SMB_TREE_CONTAINS_NODE() to prevent
1818  * operations on files not in the parent mount.
1819  *
1820  * Case sensitivity flags (SMB_IGNORE_CASE, SMB_CASE_SENSITIVE):
1821  * if SMB_CASE_SENSITIVE is set, the SMB_IGNORE_CASE flag will NOT be set
1822  * based on the tree's case sensitivity. However, if the SMB_IGNORE_CASE
1823  * flag is set in the flags value passed as a parameter, a case insensitive
1824  * lookup WILL be done (regardless of whether SMB_CASE_SENSITIVE is set
1825  * or not).
1826  */
1827 int
1828 smb_fsop_lookup(
1829     smb_request_t *sr,
1830     cred_t	*cr,
1831     int		flags,
1832     smb_node_t	*root_node,
1833     smb_node_t	*dnode,
1834     char	*name,
1835     smb_node_t	**ret_snode)
1836 {
1837 	smb_node_t *lnk_target_node;
1838 	smb_node_t *lnk_dnode;
1839 	char *longname;
1840 	char *od_name;
1841 	vnode_t *vp;
1842 	int rc;
1843 	int ret_flags;
1844 	smb_attr_t attr;
1845 
1846 	ASSERT(cr);
1847 	ASSERT(dnode);
1848 	ASSERT(dnode->n_magic == SMB_NODE_MAGIC);
1849 	ASSERT(dnode->n_state != SMB_NODE_STATE_DESTROYING);
1850 
1851 	if (name == NULL)
1852 		return (EINVAL);
1853 
1854 	if (SMB_TREE_CONTAINS_NODE(sr, dnode) == 0)
1855 		return (EACCES);
1856 
1857 	if (!(flags & SMB_CASE_SENSITIVE)) {
1858 		if (SMB_TREE_IS_CASEINSENSITIVE(sr))
1859 			flags |= SMB_IGNORE_CASE;
1860 	}
1861 	if (SMB_TREE_SUPPORTS_CATIA(sr))
1862 		flags |= SMB_CATIA;
1863 	if (SMB_TREE_SUPPORTS_ABE(sr))
1864 		flags |= SMB_ABE;
1865 
1866 	/*
1867 	 * Can have "" or "." when opening named streams on a directory.
1868 	 */
1869 	if (name[0] == '\0' || (name[0] == '.' && name[1] == '\0')) {
1870 		smb_node_ref(dnode);
1871 		*ret_snode = dnode;
1872 		return (0);
1873 	}
1874 
1875 	od_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1876 
1877 	rc = smb_vop_lookup(dnode->vp, name, &vp, od_name, flags,
1878 	    &ret_flags, root_node ? root_node->vp : NULL, &attr, cr);
1879 
1880 	if (rc != 0) {
1881 		if (!SMB_TREE_SUPPORTS_SHORTNAMES(sr) ||
1882 		    !smb_maybe_mangled(name)) {
1883 			kmem_free(od_name, MAXNAMELEN);
1884 			return (rc);
1885 		}
1886 
1887 		longname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1888 		rc = smb_unmangle(dnode, name, longname, MAXNAMELEN, flags);
1889 		if (rc != 0) {
1890 			kmem_free(od_name, MAXNAMELEN);
1891 			kmem_free(longname, MAXNAMELEN);
1892 			return (rc);
1893 		}
1894 
1895 		/*
1896 		 * longname is the real (case-sensitive)
1897 		 * on-disk name.
1898 		 * We make sure we do a lookup on this exact
1899 		 * name, as the name was mangled and denotes
1900 		 * a unique file.
1901 		 */
1902 
1903 		if (flags & SMB_IGNORE_CASE)
1904 			flags &= ~SMB_IGNORE_CASE;
1905 
1906 		rc = smb_vop_lookup(dnode->vp, longname, &vp, od_name,
1907 		    flags, &ret_flags, root_node ? root_node->vp : NULL, &attr,
1908 		    cr);
1909 
1910 		kmem_free(longname, MAXNAMELEN);
1911 
1912 		if (rc != 0) {
1913 			kmem_free(od_name, MAXNAMELEN);
1914 			return (rc);
1915 		}
1916 	}
1917 
1918 	if ((flags & SMB_FOLLOW_LINKS) && (vp->v_type == VLNK) &&
1919 	    ((attr.sa_dosattr & FILE_ATTRIBUTE_REPARSE_POINT) == 0)) {
1920 		rc = smb_pathname(sr, od_name, FOLLOW, root_node, dnode,
1921 		    &lnk_dnode, &lnk_target_node, cr);
1922 
1923 		if (rc != 0) {
1924 			/*
1925 			 * The link is assumed to be for the last component
1926 			 * of a path.  Hence any ENOTDIR error will be returned
1927 			 * as ENOENT.
1928 			 */
1929 			if (rc == ENOTDIR)
1930 				rc = ENOENT;
1931 
1932 			VN_RELE(vp);
1933 			kmem_free(od_name, MAXNAMELEN);
1934 			return (rc);
1935 		}
1936 
1937 		/*
1938 		 * Release the original VLNK vnode
1939 		 */
1940 
1941 		VN_RELE(vp);
1942 		vp = lnk_target_node->vp;
1943 
1944 		rc = smb_vop_traverse_check(&vp);
1945 
1946 		if (rc != 0) {
1947 			smb_node_release(lnk_dnode);
1948 			smb_node_release(lnk_target_node);
1949 			kmem_free(od_name, MAXNAMELEN);
1950 			return (rc);
1951 		}
1952 
1953 		/*
1954 		 * smb_vop_traverse_check() may have returned a different vnode
1955 		 */
1956 
1957 		if (lnk_target_node->vp == vp) {
1958 			*ret_snode = lnk_target_node;
1959 		} else {
1960 			*ret_snode = smb_node_lookup(sr, NULL, cr, vp,
1961 			    lnk_target_node->od_name, lnk_dnode, NULL);
1962 			VN_RELE(vp);
1963 
1964 			if (*ret_snode == NULL)
1965 				rc = ENOMEM;
1966 			smb_node_release(lnk_target_node);
1967 		}
1968 
1969 		smb_node_release(lnk_dnode);
1970 
1971 	} else {
1972 
1973 		rc = smb_vop_traverse_check(&vp);
1974 		if (rc) {
1975 			VN_RELE(vp);
1976 			kmem_free(od_name, MAXNAMELEN);
1977 			return (rc);
1978 		}
1979 
1980 		*ret_snode = smb_node_lookup(sr, NULL, cr, vp, od_name,
1981 		    dnode, NULL);
1982 		VN_RELE(vp);
1983 
1984 		if (*ret_snode == NULL)
1985 			rc = ENOMEM;
1986 	}
1987 
1988 	kmem_free(od_name, MAXNAMELEN);
1989 	return (rc);
1990 }
1991 
1992 int /*ARGSUSED*/
1993 smb_fsop_commit(smb_request_t *sr, cred_t *cr, smb_node_t *snode)
1994 {
1995 	ASSERT(cr);
1996 	ASSERT(snode);
1997 	ASSERT(snode->n_magic == SMB_NODE_MAGIC);
1998 	ASSERT(snode->n_state != SMB_NODE_STATE_DESTROYING);
1999 
2000 	ASSERT(sr);
2001 	ASSERT(sr->tid_tree);
2002 	if (SMB_TREE_IS_READONLY(sr))
2003 		return (EROFS);
2004 
2005 	return (smb_vop_commit(snode->vp, cr));
2006 }
2007 
2008 /*
2009  * smb_fsop_aclread
2010  *
2011  * Retrieve filesystem ACL. Depends on requested ACLs in
2012  * fs_sd->sd_secinfo, it'll set DACL and SACL pointers in
2013  * fs_sd. Note that requesting a DACL/SACL doesn't mean that
2014  * the corresponding field in fs_sd should be non-NULL upon
2015  * return, since the target ACL might not contain that type of
2016  * entries.
2017  *
2018  * Returned ACL is always in ACE_T (aka ZFS) format.
2019  * If successful the allocated memory for the ACL should be freed
2020  * using smb_fsacl_free() or smb_fssd_term()
2021  */
2022 int
2023 smb_fsop_aclread(smb_request_t *sr, cred_t *cr, smb_node_t *snode,
2024     smb_fssd_t *fs_sd)
2025 {
2026 	int error = 0;
2027 	int flags = 0;
2028 	int access = 0;
2029 	acl_t *acl;
2030 
2031 	ASSERT(cr);
2032 
2033 	/* Can't query security on named streams */
2034 	if (SMB_IS_STREAM(snode) != NULL)
2035 		return (EINVAL);
2036 
2037 	if (SMB_TREE_HAS_ACCESS(sr, ACE_READ_ACL) == 0)
2038 		return (EACCES);
2039 
2040 	if (sr->fid_ofile) {
2041 		if (fs_sd->sd_secinfo & SMB_DACL_SECINFO)
2042 			access = READ_CONTROL;
2043 
2044 		if (fs_sd->sd_secinfo & SMB_SACL_SECINFO)
2045 			access |= ACCESS_SYSTEM_SECURITY;
2046 
2047 		error = smb_ofile_access(sr->fid_ofile, cr, access);
2048 		if (error != NT_STATUS_SUCCESS) {
2049 			return (EACCES);
2050 		}
2051 	}
2052 
2053 
2054 	if (smb_tree_has_feature(sr->tid_tree, SMB_TREE_ACEMASKONACCESS))
2055 		flags = ATTR_NOACLCHECK;
2056 
2057 	error = smb_vop_acl_read(snode->vp, &acl, flags,
2058 	    sr->tid_tree->t_acltype, cr);
2059 	if (error != 0) {
2060 		return (error);
2061 	}
2062 
2063 	error = acl_translate(acl, _ACL_ACE_ENABLED,
2064 	    smb_node_is_dir(snode), fs_sd->sd_uid, fs_sd->sd_gid);
2065 
2066 	if (error == 0) {
2067 		smb_fsacl_split(acl, &fs_sd->sd_zdacl, &fs_sd->sd_zsacl,
2068 		    fs_sd->sd_secinfo);
2069 	}
2070 
2071 	acl_free(acl);
2072 	return (error);
2073 }
2074 
2075 /*
2076  * smb_fsop_aclwrite
2077  *
2078  * Stores the filesystem ACL provided in fs_sd->sd_acl.
2079  */
2080 int
2081 smb_fsop_aclwrite(smb_request_t *sr, cred_t *cr, smb_node_t *snode,
2082     smb_fssd_t *fs_sd)
2083 {
2084 	int target_flavor;
2085 	int error = 0;
2086 	int flags = 0;
2087 	int access = 0;
2088 	acl_t *acl, *dacl, *sacl;
2089 
2090 	ASSERT(cr);
2091 
2092 	ASSERT(sr);
2093 	ASSERT(sr->tid_tree);
2094 	if (SMB_TREE_IS_READONLY(sr))
2095 		return (EROFS);
2096 
2097 	/* Can't set security on named streams */
2098 	if (SMB_IS_STREAM(snode) != NULL)
2099 		return (EINVAL);
2100 
2101 	if (SMB_TREE_HAS_ACCESS(sr, ACE_WRITE_ACL) == 0)
2102 		return (EACCES);
2103 
2104 	if (sr->fid_ofile) {
2105 		if (fs_sd->sd_secinfo & SMB_DACL_SECINFO)
2106 			access = WRITE_DAC;
2107 
2108 		if (fs_sd->sd_secinfo & SMB_SACL_SECINFO)
2109 			access |= ACCESS_SYSTEM_SECURITY;
2110 
2111 		error = smb_ofile_access(sr->fid_ofile, cr, access);
2112 		if (error != NT_STATUS_SUCCESS)
2113 			return (EACCES);
2114 	}
2115 
2116 	switch (sr->tid_tree->t_acltype) {
2117 	case ACLENT_T:
2118 		target_flavor = _ACL_ACLENT_ENABLED;
2119 		break;
2120 
2121 	case ACE_T:
2122 		target_flavor = _ACL_ACE_ENABLED;
2123 		break;
2124 	default:
2125 		return (EINVAL);
2126 	}
2127 
2128 	dacl = fs_sd->sd_zdacl;
2129 	sacl = fs_sd->sd_zsacl;
2130 
2131 	ASSERT(dacl || sacl);
2132 	if ((dacl == NULL) && (sacl == NULL))
2133 		return (EINVAL);
2134 
2135 	if (dacl && sacl)
2136 		acl = smb_fsacl_merge(dacl, sacl);
2137 	else if (dacl)
2138 		acl = dacl;
2139 	else
2140 		acl = sacl;
2141 
2142 	error = acl_translate(acl, target_flavor, smb_node_is_dir(snode),
2143 	    fs_sd->sd_uid, fs_sd->sd_gid);
2144 	if (error == 0) {
2145 		if (smb_tree_has_feature(sr->tid_tree,
2146 		    SMB_TREE_ACEMASKONACCESS))
2147 			flags = ATTR_NOACLCHECK;
2148 
2149 		error = smb_vop_acl_write(snode->vp, acl, flags, cr);
2150 	}
2151 
2152 	if (dacl && sacl)
2153 		acl_free(acl);
2154 
2155 	return (error);
2156 }
2157 
2158 acl_type_t
2159 smb_fsop_acltype(smb_node_t *snode)
2160 {
2161 	return (smb_vop_acl_type(snode->vp));
2162 }
2163 
2164 /*
2165  * smb_fsop_sdread
2166  *
2167  * Read the requested security descriptor items from filesystem.
2168  * The items are specified in fs_sd->sd_secinfo.
2169  */
2170 int
2171 smb_fsop_sdread(smb_request_t *sr, cred_t *cr, smb_node_t *snode,
2172     smb_fssd_t *fs_sd)
2173 {
2174 	int error = 0;
2175 	int getowner = 0;
2176 	cred_t *ga_cred;
2177 	smb_attr_t attr;
2178 
2179 	ASSERT(cr);
2180 	ASSERT(fs_sd);
2181 
2182 	/* Can't query security on named streams */
2183 	if (SMB_IS_STREAM(snode) != NULL)
2184 		return (EINVAL);
2185 
2186 	/*
2187 	 * File's uid/gid is fetched in two cases:
2188 	 *
2189 	 * 1. it's explicitly requested
2190 	 *
2191 	 * 2. target ACL is ACE_T (ZFS ACL). They're needed for
2192 	 *    owner@/group@ entries. In this case kcred should be used
2193 	 *    because uid/gid are fetched on behalf of smb server.
2194 	 */
2195 	if (fs_sd->sd_secinfo & (SMB_OWNER_SECINFO | SMB_GROUP_SECINFO)) {
2196 		getowner = 1;
2197 		ga_cred = cr;
2198 	} else if (sr->tid_tree->t_acltype == ACE_T) {
2199 		getowner = 1;
2200 		ga_cred = zone_kcred();
2201 	}
2202 
2203 	if (getowner) {
2204 		/*
2205 		 * Windows require READ_CONTROL to read owner/group SID since
2206 		 * they're part of Security Descriptor.
2207 		 * ZFS only requires read_attribute. Need to have a explicit
2208 		 * access check here.
2209 		 */
2210 		if (sr->fid_ofile == NULL) {
2211 			error = smb_fsop_access(sr, ga_cred, snode,
2212 			    READ_CONTROL);
2213 			if (error)
2214 				return (EACCES);
2215 		}
2216 
2217 		attr.sa_mask = SMB_AT_UID | SMB_AT_GID;
2218 		error = smb_fsop_getattr(sr, ga_cred, snode, &attr);
2219 		if (error == 0) {
2220 			fs_sd->sd_uid = attr.sa_vattr.va_uid;
2221 			fs_sd->sd_gid = attr.sa_vattr.va_gid;
2222 		} else {
2223 			return (error);
2224 		}
2225 	}
2226 
2227 	if (fs_sd->sd_secinfo & SMB_ACL_SECINFO) {
2228 		error = smb_fsop_aclread(sr, cr, snode, fs_sd);
2229 	}
2230 
2231 	return (error);
2232 }
2233 
2234 /*
2235  * smb_fsop_sdmerge
2236  *
2237  * From SMB point of view DACL and SACL are two separate list
2238  * which can be manipulated independently without one affecting
2239  * the other, but entries for both DACL and SACL will end up
2240  * in the same ACL if target filesystem supports ACE_T ACLs.
2241  *
2242  * So, if either DACL or SACL is present in the client set request
2243  * the entries corresponding to the non-present ACL shouldn't
2244  * be touched in the FS ACL.
2245  *
2246  * fs_sd parameter contains DACL and SACL specified by SMB
2247  * client to be set on a file/directory. The client could
2248  * specify both or one of these ACLs (if none is specified
2249  * we don't get this far). When both DACL and SACL are given
2250  * by client the existing ACL should be overwritten. If only
2251  * one of them is specified the entries corresponding to the other
2252  * ACL should not be touched. For example, if only DACL
2253  * is specified in input fs_sd, the function reads audit entries
2254  * of the existing ACL of the file and point fs_sd->sd_zsdacl
2255  * pointer to the fetched SACL, this way when smb_fsop_sdwrite()
2256  * function is called the passed fs_sd would point to the specified
2257  * DACL by client and fetched SACL from filesystem, so the file
2258  * will end up with correct ACL.
2259  */
2260 static int
2261 smb_fsop_sdmerge(smb_request_t *sr, smb_node_t *snode, smb_fssd_t *fs_sd)
2262 {
2263 	smb_fssd_t cur_sd;
2264 	cred_t *kcr = zone_kcred();
2265 	int error = 0;
2266 
2267 	if (sr->tid_tree->t_acltype != ACE_T)
2268 		/* Don't bother if target FS doesn't support ACE_T */
2269 		return (0);
2270 
2271 	if ((fs_sd->sd_secinfo & SMB_ACL_SECINFO) != SMB_ACL_SECINFO) {
2272 		if (fs_sd->sd_secinfo & SMB_DACL_SECINFO) {
2273 			/*
2274 			 * Don't overwrite existing audit entries
2275 			 */
2276 			smb_fssd_init(&cur_sd, SMB_SACL_SECINFO,
2277 			    fs_sd->sd_flags);
2278 
2279 			error = smb_fsop_sdread(sr, kcr, snode, &cur_sd);
2280 			if (error == 0) {
2281 				ASSERT(fs_sd->sd_zsacl == NULL);
2282 				fs_sd->sd_zsacl = cur_sd.sd_zsacl;
2283 				if (fs_sd->sd_zsacl && fs_sd->sd_zdacl)
2284 					fs_sd->sd_zsacl->acl_flags =
2285 					    fs_sd->sd_zdacl->acl_flags;
2286 			}
2287 		} else {
2288 			/*
2289 			 * Don't overwrite existing access entries
2290 			 */
2291 			smb_fssd_init(&cur_sd, SMB_DACL_SECINFO,
2292 			    fs_sd->sd_flags);
2293 
2294 			error = smb_fsop_sdread(sr, kcr, snode, &cur_sd);
2295 			if (error == 0) {
2296 				ASSERT(fs_sd->sd_zdacl == NULL);
2297 				fs_sd->sd_zdacl = cur_sd.sd_zdacl;
2298 				if (fs_sd->sd_zdacl && fs_sd->sd_zsacl)
2299 					fs_sd->sd_zdacl->acl_flags =
2300 					    fs_sd->sd_zsacl->acl_flags;
2301 			}
2302 		}
2303 
2304 		if (error)
2305 			smb_fssd_term(&cur_sd);
2306 	}
2307 
2308 	return (error);
2309 }
2310 
2311 /*
2312  * smb_fsop_sdwrite
2313  *
2314  * Stores the given uid, gid and acl in filesystem.
2315  * Provided items in fs_sd are specified by fs_sd->sd_secinfo.
2316  *
2317  * A SMB security descriptor could contain owner, primary group,
2318  * DACL and SACL. Setting an SD should be atomic but here it has to
2319  * be done via two separate FS operations: VOP_SETATTR and
2320  * VOP_SETSECATTR. Therefore, this function has to simulate the
2321  * atomicity as well as it can.
2322  *
2323  * Get the current uid, gid before setting the new uid/gid
2324  * so if smb_fsop_aclwrite fails they can be restored. root cred is
2325  * used to get currend uid/gid since this operation is performed on
2326  * behalf of the server not the user.
2327  *
2328  * If setting uid/gid fails with EPERM it means that and invalid
2329  * owner has been specified. Callers should translate this to
2330  * STATUS_INVALID_OWNER which is not the normal mapping for EPERM
2331  * in upper layers, so EPERM is mapped to EBADE.
2332  */
2333 int
2334 smb_fsop_sdwrite(smb_request_t *sr, cred_t *cr, smb_node_t *snode,
2335     smb_fssd_t *fs_sd, int overwrite)
2336 {
2337 	smb_attr_t set_attr;
2338 	smb_attr_t orig_attr;
2339 	cred_t *kcr = zone_kcred();
2340 	int error = 0;
2341 	int access = 0;
2342 
2343 	ASSERT(cr);
2344 	ASSERT(fs_sd);
2345 
2346 	ASSERT(sr);
2347 	ASSERT(sr->tid_tree);
2348 	if (SMB_TREE_IS_READONLY(sr))
2349 		return (EROFS);
2350 
2351 	/* Can't set security on named streams */
2352 	if (SMB_IS_STREAM(snode) != NULL)
2353 		return (EINVAL);
2354 
2355 	bzero(&set_attr, sizeof (smb_attr_t));
2356 
2357 	if (fs_sd->sd_secinfo & SMB_OWNER_SECINFO) {
2358 		set_attr.sa_vattr.va_uid = fs_sd->sd_uid;
2359 		set_attr.sa_mask |= SMB_AT_UID;
2360 		access |= WRITE_OWNER;
2361 	}
2362 
2363 	if (fs_sd->sd_secinfo & SMB_GROUP_SECINFO) {
2364 		set_attr.sa_vattr.va_gid = fs_sd->sd_gid;
2365 		set_attr.sa_mask |= SMB_AT_GID;
2366 		access |= WRITE_OWNER;
2367 	}
2368 
2369 	if (fs_sd->sd_secinfo & SMB_DACL_SECINFO)
2370 		access |= WRITE_DAC;
2371 
2372 	if (fs_sd->sd_secinfo & SMB_SACL_SECINFO)
2373 		access |= ACCESS_SYSTEM_SECURITY;
2374 
2375 	if (sr->fid_ofile)
2376 		error = smb_ofile_access(sr->fid_ofile, cr, access);
2377 	else
2378 		error = smb_fsop_access(sr, cr, snode, access);
2379 
2380 	if (error)
2381 		return (EACCES);
2382 
2383 	if (set_attr.sa_mask) {
2384 		orig_attr.sa_mask = SMB_AT_UID | SMB_AT_GID;
2385 		error = smb_fsop_getattr(sr, kcr, snode, &orig_attr);
2386 		if (error == 0) {
2387 			error = smb_fsop_setattr(sr, cr, snode, &set_attr);
2388 			if (error == EPERM)
2389 				error = EBADE;
2390 		}
2391 
2392 		if (error)
2393 			return (error);
2394 	}
2395 
2396 	if (fs_sd->sd_secinfo & SMB_ACL_SECINFO) {
2397 		if (overwrite == 0) {
2398 			error = smb_fsop_sdmerge(sr, snode, fs_sd);
2399 			if (error)
2400 				return (error);
2401 		}
2402 
2403 		error = smb_fsop_aclwrite(sr, cr, snode, fs_sd);
2404 		if (error) {
2405 			/*
2406 			 * Revert uid/gid changes if required.
2407 			 */
2408 			if (set_attr.sa_mask) {
2409 				orig_attr.sa_mask = set_attr.sa_mask;
2410 				(void) smb_fsop_setattr(sr, kcr, snode,
2411 				    &orig_attr);
2412 			}
2413 		}
2414 	}
2415 
2416 	return (error);
2417 }
2418 
2419 #ifdef	_KERNEL
2420 /*
2421  * smb_fsop_sdinherit
2422  *
2423  * Inherit the security descriptor from the parent container.
2424  * This function is called after FS has created the file/folder
2425  * so if this doesn't do anything it means FS inheritance is
2426  * in place.
2427  *
2428  * Do inheritance for ZFS internally.
2429  *
2430  * If we want to let ZFS does the inheritance the
2431  * following setting should be true:
2432  *
2433  *  - aclinherit = passthrough
2434  *  - aclmode = passthrough
2435  *  - smbd umask = 0777
2436  *
2437  * This will result in right effective permissions but
2438  * ZFS will always add 6 ACEs for owner, owning group
2439  * and others to be POSIX compliant. This is not what
2440  * Windows clients/users expect, so we decided that CIFS
2441  * implements Windows rules and overwrite whatever ZFS
2442  * comes up with. This way we also don't have to care
2443  * about ZFS aclinherit and aclmode settings.
2444  */
2445 static int
2446 smb_fsop_sdinherit(smb_request_t *sr, smb_node_t *dnode, smb_fssd_t *fs_sd)
2447 {
2448 	acl_t *dacl = NULL;
2449 	acl_t *sacl = NULL;
2450 	int is_dir;
2451 	int error;
2452 
2453 	ASSERT(fs_sd);
2454 
2455 	if (sr->tid_tree->t_acltype != ACE_T) {
2456 		/*
2457 		 * No forced inheritance for non-ZFS filesystems.
2458 		 */
2459 		fs_sd->sd_secinfo = 0;
2460 		return (0);
2461 	}
2462 
2463 
2464 	/* Fetch parent directory's ACL */
2465 	error = smb_fsop_sdread(sr, zone_kcred(), dnode, fs_sd);
2466 	if (error) {
2467 		return (error);
2468 	}
2469 
2470 	is_dir = (fs_sd->sd_flags & SMB_FSSD_FLAGS_DIR);
2471 	dacl = smb_fsacl_inherit(fs_sd->sd_zdacl, is_dir, SMB_DACL_SECINFO,
2472 	    sr->user_cr);
2473 	sacl = smb_fsacl_inherit(fs_sd->sd_zsacl, is_dir, SMB_SACL_SECINFO,
2474 	    sr->user_cr);
2475 
2476 	if (sacl == NULL)
2477 		fs_sd->sd_secinfo &= ~SMB_SACL_SECINFO;
2478 
2479 	smb_fsacl_free(fs_sd->sd_zdacl);
2480 	smb_fsacl_free(fs_sd->sd_zsacl);
2481 
2482 	fs_sd->sd_zdacl = dacl;
2483 	fs_sd->sd_zsacl = sacl;
2484 
2485 	return (0);
2486 }
2487 #endif	/* _KERNEL */
2488 
2489 /*
2490  * smb_fsop_eaccess
2491  *
2492  * Returns the effective permission of the given credential for the
2493  * specified object.
2494  *
2495  * This is just a workaround. We need VFS/FS support for this.
2496  */
2497 void
2498 smb_fsop_eaccess(smb_request_t *sr, cred_t *cr, smb_node_t *snode,
2499     uint32_t *eaccess)
2500 {
2501 	int access = 0;
2502 	vnode_t *dir_vp;
2503 	smb_node_t *unnamed_node;
2504 
2505 	ASSERT(cr);
2506 	ASSERT(snode);
2507 	ASSERT(snode->n_magic == SMB_NODE_MAGIC);
2508 	ASSERT(snode->n_state != SMB_NODE_STATE_DESTROYING);
2509 
2510 	unnamed_node = SMB_IS_STREAM(snode);
2511 	if (unnamed_node) {
2512 		ASSERT(unnamed_node->n_magic == SMB_NODE_MAGIC);
2513 		ASSERT(unnamed_node->n_state != SMB_NODE_STATE_DESTROYING);
2514 		/*
2515 		 * Streams authorization should be performed against the
2516 		 * unnamed stream.
2517 		 */
2518 		snode = unnamed_node;
2519 	}
2520 
2521 	if (smb_tree_has_feature(sr->tid_tree, SMB_TREE_ACEMASKONACCESS)) {
2522 		dir_vp = (snode->n_dnode) ? snode->n_dnode->vp : NULL;
2523 		smb_vop_eaccess(snode->vp, (int *)eaccess, V_ACE_MASK, dir_vp,
2524 		    cr);
2525 		return;
2526 	}
2527 
2528 	/*
2529 	 * FS doesn't understand 32-bit mask
2530 	 */
2531 	smb_vop_eaccess(snode->vp, &access, 0, NULL, cr);
2532 	access &= sr->tid_tree->t_access;
2533 
2534 	*eaccess = READ_CONTROL | FILE_READ_EA | FILE_READ_ATTRIBUTES;
2535 
2536 	if (access & VREAD)
2537 		*eaccess |= FILE_READ_DATA;
2538 
2539 	if (access & VEXEC)
2540 		*eaccess |= FILE_EXECUTE;
2541 
2542 	if (access & VWRITE)
2543 		*eaccess |= FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES |
2544 		    FILE_WRITE_EA | FILE_APPEND_DATA | FILE_DELETE_CHILD;
2545 
2546 	if (access & (VREAD | VWRITE))
2547 		*eaccess |= SYNCHRONIZE;
2548 
2549 #ifdef	_FAKE_KERNEL
2550 	/* Should be: if (we are the owner)... */
2551 	if (access & VWRITE)
2552 		*eaccess |= DELETE | WRITE_DAC | WRITE_OWNER;
2553 #endif
2554 }
2555 
2556 /*
2557  * smb_fsop_shrlock
2558  *
2559  * For the current open request, check file sharing rules
2560  * against existing opens.
2561  *
2562  * Returns NT_STATUS_SHARING_VIOLATION if there is any
2563  * sharing conflict.  Returns NT_STATUS_SUCCESS otherwise.
2564  *
2565  * Full system-wide share reservation synchronization is available
2566  * when the nbmand (non-blocking mandatory) mount option is set
2567  * (i.e. nbl_need_crit() is true) and nbmand critical regions are used.
2568  * This provides synchronization with NFS and local processes.  The
2569  * critical regions are entered in VOP_SHRLOCK()/fs_shrlock() (called
2570  * from smb_open_subr()/smb_fsop_shrlock()/smb_vop_shrlock()) as well
2571  * as the CIFS rename and delete paths.
2572  *
2573  * The CIFS server will also enter the nbl critical region in the open,
2574  * rename, and delete paths when nbmand is not set.  There is limited
2575  * coordination with local and VFS share reservations in this case.
2576  * Note that when the nbmand mount option is not set, the VFS layer
2577  * only processes advisory reservations and the delete mode is not checked.
2578  *
2579  * Whether or not the nbmand mount option is set, intra-CIFS share
2580  * checking is done in the open, delete, and rename paths using a CIFS
2581  * critical region (node->n_share_lock).
2582  */
2583 uint32_t
2584 smb_fsop_shrlock(cred_t *cr, smb_node_t *node, uint32_t uniq_fid,
2585     uint32_t desired_access, uint32_t share_access)
2586 {
2587 	int rc;
2588 
2589 	/* Allow access if the request is just for meta data */
2590 	if ((desired_access & FILE_DATA_ALL) == 0)
2591 		return (NT_STATUS_SUCCESS);
2592 
2593 	rc = smb_node_open_check(node, desired_access, share_access);
2594 	if (rc)
2595 		return (NT_STATUS_SHARING_VIOLATION);
2596 
2597 	rc = smb_vop_shrlock(node->vp, uniq_fid, desired_access, share_access,
2598 	    cr);
2599 	if (rc)
2600 		return (NT_STATUS_SHARING_VIOLATION);
2601 
2602 	return (NT_STATUS_SUCCESS);
2603 }
2604 
2605 void
2606 smb_fsop_unshrlock(cred_t *cr, smb_node_t *node, uint32_t uniq_fid)
2607 {
2608 	(void) smb_vop_unshrlock(node->vp, uniq_fid, cr);
2609 }
2610 
2611 int
2612 smb_fsop_frlock(smb_node_t *node, smb_lock_t *lock, boolean_t unlock,
2613     cred_t *cr)
2614 {
2615 	flock64_t bf;
2616 	int flag = F_REMOTELOCK;
2617 
2618 	/*
2619 	 * VOP_FRLOCK() will not be called if:
2620 	 *
2621 	 * 1) The lock has a range of zero bytes. The semantics of Windows and
2622 	 *    POSIX are different. In the case of POSIX it asks for the locking
2623 	 *    of all the bytes from the offset provided until the end of the
2624 	 *    file. In the case of Windows a range of zero locks nothing and
2625 	 *    doesn't conflict with any other lock.
2626 	 *
2627 	 * 2) The lock rolls over (start + lenght < start). Solaris will assert
2628 	 *    if such a request is submitted. This will not create
2629 	 *    incompatibilities between POSIX and Windows. In the Windows world,
2630 	 *    if a client submits such a lock, the server will not lock any
2631 	 *    bytes. Interestingly if the same lock (same offset and length) is
2632 	 *    resubmitted Windows will consider that there is an overlap and
2633 	 *    the granting rules will then apply.
2634 	 *
2635 	 * 3) The SMB-level process IDs (smb_pid) are not passed down to the
2636 	 *    POSIX level in l_pid because (a) the rules about lock PIDs are
2637 	 *    different in SMB, and (b) we're putting our ofile f_uniqid in
2638 	 *    the POSIX l_pid field to segregate locks per SMB ofile.
2639 	 *    (We're also using a "remote" system ID in l_sysid.)
2640 	 *    All SMB locking PIDs are handled at the SMB level and
2641 	 *    not exposed in POSIX locking.
2642 	 */
2643 	if ((lock->l_length == 0) ||
2644 	    ((lock->l_start + lock->l_length - 1) < lock->l_start))
2645 		return (0);
2646 
2647 	bzero(&bf, sizeof (bf));
2648 
2649 	if (unlock) {
2650 		bf.l_type = F_UNLCK;
2651 	} else if (lock->l_type == SMB_LOCK_TYPE_READONLY) {
2652 		bf.l_type = F_RDLCK;
2653 		flag |= FREAD;
2654 	} else if (lock->l_type == SMB_LOCK_TYPE_READWRITE) {
2655 		bf.l_type = F_WRLCK;
2656 		flag |= FWRITE;
2657 	}
2658 
2659 	bf.l_start = lock->l_start;
2660 	bf.l_len = lock->l_length;
2661 	bf.l_pid = lock->l_file->f_uniqid;
2662 	bf.l_sysid = smb_ct.cc_sysid;
2663 
2664 	return (smb_vop_frlock(node->vp, cr, flag, &bf));
2665 }
2666