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