xref: /titanic_41/usr/src/uts/common/fs/smbsrv/smb_vops.c (revision 26fd77009b17f8c8fb32eb362584cfd635e87ad9)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/uio.h>
29 #include <sys/statvfs.h>
30 #include <sys/vnode.h>
31 #include <sys/thread.h>
32 #include <sys/pathname.h>
33 #include <sys/cred.h>
34 #include <sys/extdirent.h>
35 #include <sys/nbmlock.h>
36 #include <sys/share.h>
37 #include <sys/fcntl.h>
38 #include <nfs/lm.h>
39 
40 #include <smbsrv/smb_vops.h>
41 #include <smbsrv/string.h>
42 
43 #include <smbsrv/smb_fsops.h>
44 #include <smbsrv/smb_kproto.h>
45 #include <smbsrv/smb_incl.h>
46 
47 /*
48  * CATIA support
49  *
50  * CATIA V4 is a UNIX product and uses characters in filenames that
51  * are considered invalid by Windows. CATIA V5 is available on both
52  * UNIX and Windows.  Thus, as CATIA customers migrate from V4 to V5,
53  * some V4 files could become inaccessible to windows clients if the
54  * filename contains the characters that are considered illegal in
55  * Windows.  In order to address this issue an optional character
56  * translation is applied to filenames at the smb_vop interface.
57  *
58  * Character Translation Table
59  * ----------------------------------
60  * Unix-char (v4) | Windows-char (v5)
61  * ----------------------------------
62  *        *       |  0x00a4  Currency Sign
63  *        |       |  0x00a6  Broken Bar
64  *        "       |  0x00a8  Diaeresis
65  *        <       |  0x00ab  Left-Pointing Double Angle Quotation Mark
66  *        >       |  0x00bb  Right-Pointing Double Angle Quotation Mark
67  *        ?       |  0x00bf  Inverted Question mark
68  *        :       |  0x00f7  Division Sign
69  *        /       |  0x00f8  Latin Small Letter o with stroke
70  *        \       |  0x00ff  Latin Small Letter Y with Diaeresis
71  *
72  *
73  * Two lookup tables are used to perform the character translation:
74  *
75  * smb_catia_v5_lookup - provides the mapping between UNIX ASCII (v4)
76  * characters and equivalent or translated wide characters.
77  * It is indexed by the decimal value of the ASCII character (0-127).
78  *
79  * smb_catia_v4_lookup - provides the mapping between wide characters
80  * in the range from 0x00A4 to 0x00FF and their UNIX (v4) equivalent
81  * (in wide character format).  It is indexed by the decimal value of
82  * the wide character (164-255) with an offset of -164.
83  * If this translation produces a filename containing a '/' create, mkdir
84  * or rename (to the '/' name)  operations will not be permitted. It is
85  * not valid to create a filename with a '/' in it. However, if such a
86  * file already exists other operations (e.g, lookup, delete, rename)
87  * are permitted on it.
88  */
89 
90 /* number of characters mapped */
91 #define	SMB_CATIA_NUM_MAPS		9
92 
93 /* Windows Characters used in special character mapping */
94 #define	SMB_CATIA_WIN_CURRENCY		0x00a4
95 #define	SMB_CATIA_WIN_BROKEN_BAR	0x00a6
96 #define	SMB_CATIA_WIN_DIAERESIS		0x00a8
97 #define	SMB_CATIA_WIN_LEFT_ANGLE	0x00ab
98 #define	SMB_CATIA_WIN_RIGHT_ANGLE	0x00bb
99 #define	SMB_CATIA_WIN_INVERTED_QUESTION	0x00bf
100 #define	SMB_CATIA_WIN_DIVISION		0x00f7
101 #define	SMB_CATIA_WIN_LATIN_O		0x00f8
102 #define	SMB_CATIA_WIN_LATIN_Y		0x00ff
103 
104 #define	SMB_CATIA_V4_LOOKUP_LOW		SMB_CATIA_WIN_CURRENCY
105 #define	SMB_CATIA_V4_LOOKUP_UPPER	SMB_CATIA_WIN_LATIN_Y
106 #define	SMB_CATIA_V4_LOOKUP_MAX		\
107 	(SMB_CATIA_V4_LOOKUP_UPPER - SMB_CATIA_V4_LOOKUP_LOW + 1)
108 #define	SMB_CATIA_V5_LOOKUP_MAX		0x0080
109 
110 typedef struct smb_catia_map
111 {
112 	unsigned char unixchar;	/* v4 */
113 	mts_wchar_t winchar;	/* v5 */
114 } smb_catia_map_t;
115 
116 smb_catia_map_t catia_maps[SMB_CATIA_NUM_MAPS] =
117 {
118 	{'"',  SMB_CATIA_WIN_DIAERESIS},
119 	{'*',  SMB_CATIA_WIN_CURRENCY},
120 	{':',  SMB_CATIA_WIN_DIVISION},
121 	{'<',  SMB_CATIA_WIN_LEFT_ANGLE},
122 	{'>',  SMB_CATIA_WIN_RIGHT_ANGLE},
123 	{'?',  SMB_CATIA_WIN_INVERTED_QUESTION},
124 	{'\\', SMB_CATIA_WIN_LATIN_Y},
125 	{'/',  SMB_CATIA_WIN_LATIN_O},
126 	{'|',  SMB_CATIA_WIN_BROKEN_BAR}
127 };
128 
129 static mts_wchar_t smb_catia_v5_lookup[SMB_CATIA_V5_LOOKUP_MAX];
130 static mts_wchar_t smb_catia_v4_lookup[SMB_CATIA_V4_LOOKUP_MAX];
131 
132 static void smb_vop_setup_xvattr(smb_attr_t *smb_attr, xvattr_t *xvattr);
133 static void smb_sa_to_va_mask(uint_t sa_mask, uint_t *va_maskp);
134 static callb_cpr_t *smb_lock_frlock_callback(flk_cb_when_t, void *);
135 static void smb_vop_catia_init();
136 
137 extern sysid_t lm_alloc_sysidt();
138 
139 #define	SMB_AT_MAX	16
140 static uint_t smb_attrmap[SMB_AT_MAX] = {
141 	0,
142 	AT_TYPE,
143 	AT_MODE,
144 	AT_UID,
145 	AT_GID,
146 	AT_FSID,
147 	AT_NODEID,
148 	AT_NLINK,
149 	AT_SIZE,
150 	AT_ATIME,
151 	AT_MTIME,
152 	AT_CTIME,
153 	AT_RDEV,
154 	AT_BLKSIZE,
155 	AT_NBLOCKS,
156 	AT_SEQ
157 };
158 
159 static boolean_t	smb_vop_initialized = B_FALSE;
160 caller_context_t	smb_ct;
161 
162 /*
163  * smb_vop_init
164  *
165  * This function is not multi-thread safe. The caller must make sure only one
166  * thread makes the call.
167  */
168 int
169 smb_vop_init(void)
170 {
171 	if (smb_vop_initialized)
172 		return (0);
173 	/*
174 	 * The caller_context will be used primarily for range locking.
175 	 * Since the CIFS server is mapping its locks to POSIX locks,
176 	 * only one pid is used for operations originating from the
177 	 * CIFS server (to represent CIFS in the VOP_FRLOCK routines).
178 	 */
179 	smb_ct.cc_sysid = lm_alloc_sysidt();
180 	if (smb_ct.cc_sysid == LM_NOSYSID)
181 		return (ENOMEM);
182 
183 	smb_ct.cc_caller_id = fs_new_caller_id();
184 	smb_ct.cc_pid = IGN_PID;
185 	smb_ct.cc_flags = 0;
186 	smb_vop_catia_init();
187 
188 	smb_vop_initialized = B_TRUE;
189 	return (0);
190 }
191 
192 /*
193  * smb_vop_fini
194  *
195  * This function is not multi-thread safe. The caller must make sure only one
196  * thread makes the call.
197  */
198 void
199 smb_vop_fini(void)
200 {
201 	if (!smb_vop_initialized)
202 		return;
203 
204 	lm_free_sysidt(smb_ct.cc_sysid);
205 	smb_ct.cc_pid = IGN_PID;
206 	smb_ct.cc_sysid = LM_NOSYSID;
207 	smb_vop_initialized = B_FALSE;
208 }
209 
210 /*
211  * The smb_ct will be used primarily for range locking.
212  * Since the CIFS server is mapping its locks to POSIX locks,
213  * only one pid is used for operations originating from the
214  * CIFS server (to represent CIFS in the VOP_FRLOCK routines).
215  */
216 int
217 smb_vop_open(vnode_t **vpp, int mode, cred_t *cred)
218 {
219 	return (VOP_OPEN(vpp, mode, cred, &smb_ct));
220 }
221 
222 void
223 smb_vop_close(vnode_t *vp, int mode, cred_t *cred)
224 {
225 	(void) VOP_CLOSE(vp, mode, 1, (offset_t)0, cred, &smb_ct);
226 }
227 
228 int
229 smb_vop_other_opens(vnode_t *vp, int mode)
230 {
231 	return (((mode & FWRITE) && vn_has_other_opens(vp, V_WRITE)) ||
232 	    (((mode & FWRITE) == 0) && vn_is_opened(vp, V_WRITE)) ||
233 	    ((mode & FREAD) && vn_has_other_opens(vp, V_READ)) ||
234 	    (((mode & FREAD) == 0) && vn_is_opened(vp, V_READ)) ||
235 	    vn_is_mapped(vp, V_RDORWR));
236 }
237 
238 /*
239  * The smb_vop_* functions have minimal knowledge of CIFS semantics and
240  * serve as an interface to the VFS layer.
241  *
242  * Only smb_fsop_* layer functions should call smb_vop_* layer functions.
243  * (Higher-level CIFS service code should never skip the smb_fsop_* layer
244  * to call smb_vop_* layer functions directly.)
245  */
246 
247 /*
248  * XXX - Extended attributes support in the file system assumed.
249  * This is needed for full NT Streams functionality.
250  */
251 
252 int
253 smb_vop_read(vnode_t *vp, uio_t *uiop, cred_t *cr)
254 {
255 	int error;
256 
257 	(void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, &smb_ct);
258 	error = VOP_READ(vp, uiop, 0, cr, &smb_ct);
259 	VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, &smb_ct);
260 	return (error);
261 }
262 
263 int
264 smb_vop_write(vnode_t *vp, uio_t *uiop, int ioflag, uint32_t *lcount,
265     cred_t *cr)
266 {
267 	int error;
268 
269 	*lcount = uiop->uio_resid;
270 
271 	uiop->uio_llimit = MAXOFFSET_T;
272 
273 	(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, &smb_ct);
274 	error = VOP_WRITE(vp, uiop, ioflag, cr, &smb_ct);
275 	VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, &smb_ct);
276 
277 	*lcount -= uiop->uio_resid;
278 
279 	return (error);
280 }
281 
282 /*
283  * smb_vop_getattr()
284  *
285  * smb_fsop_getattr()/smb_vop_getattr() should always be called from the CIFS
286  * service (instead of calling VOP_GETATTR directly) to retrieve attributes
287  * due to special processing needed for streams files.
288  *
289  * All attributes are retrieved.
290  *
291  * When vp denotes a named stream, then unnamed_vp should be passed in (denoting
292  * the corresponding unnamed stream).
293  * A named stream's attributes (as far as CIFS is concerned) are those of the
294  * unnamed stream (minus the size attribute, and the type), plus  the size of
295  * the named stream, and a type value of VREG.
296  * Although the file system may store other attributes with the named stream,
297  * these should not be used by CIFS for any purpose.
298  *
299  * File systems without VFSFT_XVATTR do not support DOS attributes or create
300  * time (crtime). In this case the mtime is used as the crtime.
301  * Likewise if VOP_GETATTR doesn't return any system attributes the dosattr
302  * is 0 and the mtime is used as the crtime.
303  */
304 int
305 smb_vop_getattr(vnode_t *vp, vnode_t *unnamed_vp, smb_attr_t *ret_attr,
306     int flags, cred_t *cr)
307 {
308 	int error;
309 	vnode_t *use_vp;
310 	smb_attr_t tmp_attr;
311 	xvattr_t tmp_xvattr;
312 	xoptattr_t *xoap = NULL;
313 
314 	if (unnamed_vp)
315 		use_vp = unnamed_vp;
316 	else
317 		use_vp = vp;
318 
319 	if (vfs_has_feature(use_vp->v_vfsp, VFSFT_XVATTR)) {
320 		xva_init(&tmp_xvattr);
321 		xoap = xva_getxoptattr(&tmp_xvattr);
322 		ASSERT(xoap);
323 
324 		smb_sa_to_va_mask(ret_attr->sa_mask,
325 		    &tmp_xvattr.xva_vattr.va_mask);
326 
327 		XVA_SET_REQ(&tmp_xvattr, XAT_READONLY);
328 		XVA_SET_REQ(&tmp_xvattr, XAT_HIDDEN);
329 		XVA_SET_REQ(&tmp_xvattr, XAT_SYSTEM);
330 		XVA_SET_REQ(&tmp_xvattr, XAT_ARCHIVE);
331 		XVA_SET_REQ(&tmp_xvattr, XAT_CREATETIME);
332 
333 		error = VOP_GETATTR(use_vp, &tmp_xvattr.xva_vattr, flags,
334 		    cr, &smb_ct);
335 		if (error != 0)
336 			return (error);
337 
338 		ret_attr->sa_vattr = tmp_xvattr.xva_vattr;
339 		ret_attr->sa_dosattr = 0;
340 
341 		if (tmp_xvattr.xva_vattr.va_mask & AT_XVATTR) {
342 			xoap = xva_getxoptattr(&tmp_xvattr);
343 			ASSERT(xoap);
344 
345 			if ((XVA_ISSET_RTN(&tmp_xvattr, XAT_READONLY)) &&
346 			    (xoap->xoa_readonly)) {
347 				ret_attr->sa_dosattr |= FILE_ATTRIBUTE_READONLY;
348 			}
349 
350 			if ((XVA_ISSET_RTN(&tmp_xvattr, XAT_HIDDEN)) &&
351 			    (xoap->xoa_hidden)) {
352 				ret_attr->sa_dosattr |= FILE_ATTRIBUTE_HIDDEN;
353 			}
354 
355 			if ((XVA_ISSET_RTN(&tmp_xvattr, XAT_SYSTEM)) &&
356 			    (xoap->xoa_system)) {
357 				ret_attr->sa_dosattr |= FILE_ATTRIBUTE_SYSTEM;
358 			}
359 
360 			if ((XVA_ISSET_RTN(&tmp_xvattr, XAT_ARCHIVE)) &&
361 			    (xoap->xoa_archive)) {
362 				ret_attr->sa_dosattr |= FILE_ATTRIBUTE_ARCHIVE;
363 			}
364 
365 			ret_attr->sa_crtime = xoap->xoa_createtime;
366 		} else {
367 			ret_attr->sa_crtime = ret_attr->sa_vattr.va_mtime;
368 		}
369 	} else {
370 		/*
371 		 * Support for file systems without VFSFT_XVATTR
372 		 */
373 		smb_sa_to_va_mask(ret_attr->sa_mask,
374 		    &ret_attr->sa_vattr.va_mask);
375 
376 		error = VOP_GETATTR(use_vp, &ret_attr->sa_vattr,
377 		    flags, cr, &smb_ct);
378 		if (error != 0)
379 			return (error);
380 
381 		ret_attr->sa_dosattr = 0;
382 		ret_attr->sa_crtime = ret_attr->sa_vattr.va_mtime;
383 	}
384 
385 	if (unnamed_vp) {
386 		ret_attr->sa_vattr.va_type = VREG;
387 
388 		if (ret_attr->sa_mask & (SMB_AT_SIZE | SMB_AT_NBLOCKS)) {
389 			tmp_attr.sa_vattr.va_mask = AT_SIZE | AT_NBLOCKS;
390 
391 			error = VOP_GETATTR(vp, &tmp_attr.sa_vattr,
392 			    flags, cr, &smb_ct);
393 			if (error != 0)
394 				return (error);
395 
396 			ret_attr->sa_vattr.va_size = tmp_attr.sa_vattr.va_size;
397 			ret_attr->sa_vattr.va_nblocks =
398 			    tmp_attr.sa_vattr.va_nblocks;
399 		}
400 	}
401 
402 	if (ret_attr->sa_vattr.va_type == VDIR)
403 		ret_attr->sa_dosattr |= FILE_ATTRIBUTE_DIRECTORY;
404 
405 	return (error);
406 }
407 
408 /*
409  * smb_vop_setattr()
410  *
411  * smb_fsop_setattr()/smb_vop_setattr() should always be used instead of
412  * VOP_SETATTR() when calling from the CIFS service, due to special processing
413  * for streams files.
414  *
415  * Streams have a size but otherwise do not have separate attributes from
416  * the (unnamed stream) file, i.e., the security and ownership of the file
417  * applies to the stream.  In contrast, extended attribute files, which are
418  * used to implement streams, are independent objects with their own
419  * attributes.
420  *
421  * For compatibility with streams, we set the size on the extended attribute
422  * file and apply other attributes to the (unnamed stream) file.  The one
423  * exception is that the UID and GID can be set on the stream by passing a
424  * NULL unnamed_vp, which allows callers to synchronize stream ownership
425  * with the (unnamed stream) file.
426  */
427 int
428 smb_vop_setattr(vnode_t *vp, vnode_t *unnamed_vp, smb_attr_t *attr,
429     int flags, cred_t *cr)
430 {
431 	int error = 0;
432 	int at_size = 0;
433 	vnode_t *use_vp;
434 	xvattr_t xvattr;
435 	vattr_t *vap;
436 
437 	if (attr->sa_mask & SMB_AT_DOSATTR) {
438 		attr->sa_dosattr &=
439 		    (FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_READONLY |
440 		    FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
441 	}
442 
443 	if (unnamed_vp) {
444 		use_vp = unnamed_vp;
445 		if (attr->sa_mask & SMB_AT_SIZE) {
446 			at_size = 1;
447 			attr->sa_mask &= ~SMB_AT_SIZE;
448 		}
449 	} else {
450 		use_vp = vp;
451 	}
452 
453 	/*
454 	 * The caller should not be setting sa_vattr.va_mask,
455 	 * but rather sa_mask.
456 	 */
457 
458 	attr->sa_vattr.va_mask = 0;
459 
460 	if (vfs_has_feature(use_vp->v_vfsp, VFSFT_XVATTR)) {
461 		smb_vop_setup_xvattr(attr, &xvattr);
462 		vap = &xvattr.xva_vattr;
463 	} else {
464 		smb_sa_to_va_mask(attr->sa_mask,
465 		    &attr->sa_vattr.va_mask);
466 		vap = &attr->sa_vattr;
467 	}
468 
469 	if ((error = VOP_SETATTR(use_vp, vap, flags, cr, &smb_ct)) != 0)
470 		return (error);
471 
472 	if (at_size) {
473 		attr->sa_vattr.va_mask = AT_SIZE;
474 		error = VOP_SETATTR(vp, &attr->sa_vattr, flags, kcred, &smb_ct);
475 	}
476 
477 	return (error);
478 }
479 
480 /*
481  * smb_vop_access
482  *
483  * This is a wrapper round VOP_ACCESS. VOP_ACCESS checks the given mode
484  * against file's ACL or Unix permissions. CIFS on the other hand needs to
485  * know if the requested operation can succeed for the given object, this
486  * requires more checks in case of DELETE bit since permissions on the parent
487  * directory are important as well. Based on Windows rules if parent's ACL
488  * grant FILE_DELETE_CHILD a file can be delete regardless of the file's
489  * permissions.
490  */
491 int
492 smb_vop_access(vnode_t *vp, int mode, int flags, vnode_t *dir_vp, cred_t *cr)
493 {
494 	int error = 0;
495 
496 	if (mode == 0)
497 		return (0);
498 
499 	if ((flags == V_ACE_MASK) && (mode & ACE_DELETE)) {
500 		if (dir_vp) {
501 			error = VOP_ACCESS(dir_vp, ACE_DELETE_CHILD, flags,
502 			    cr, NULL);
503 
504 			if (error == 0)
505 				mode &= ~ACE_DELETE;
506 		}
507 	}
508 
509 	if (mode) {
510 		error = VOP_ACCESS(vp, mode, flags, cr, NULL);
511 	}
512 
513 	return (error);
514 }
515 
516 /*
517  * smb_vop_lookup
518  *
519  * dvp:		directory vnode (in)
520  * name:	name of file to be looked up (in)
521  * vpp:		looked-up vnode (out)
522  * od_name:	on-disk name of file (out).
523  *		This parameter is optional.  If a pointer is passed in, it
524  * 		must be allocated with MAXNAMELEN bytes
525  * rootvp:	vnode of the tree root (in)
526  *		This parameter is always passed in non-NULL except at the time
527  *		of share set up.
528  * direntflags:	dirent flags returned from VOP_LOOKUP
529  */
530 int
531 smb_vop_lookup(
532     vnode_t		*dvp,
533     char		*name,
534     vnode_t		**vpp,
535     char		*od_name,
536     int			flags,
537     int			*direntflags,
538     vnode_t		*rootvp,
539     cred_t		*cr)
540 {
541 	int error = 0;
542 	int option_flags = 0;
543 	pathname_t rpn;
544 	char *np = name;
545 	char namebuf[MAXNAMELEN];
546 
547 	if (*name == '\0')
548 		return (EINVAL);
549 
550 	ASSERT(vpp);
551 	*vpp = NULL;
552 	*direntflags = 0;
553 
554 	if ((name[0] == '.') && (name[1] == '.') && (name[2] == 0)) {
555 		if (rootvp && (dvp == rootvp)) {
556 			VN_HOLD(dvp);
557 			*vpp = dvp;
558 			return (0);
559 		}
560 
561 		if (dvp->v_flag & VROOT) {
562 			vfs_t *vfsp;
563 			vnode_t *cvp = dvp;
564 
565 			/*
566 			 * Set dvp and check for races with forced unmount
567 			 * (see lookuppnvp())
568 			 */
569 
570 			vfsp = cvp->v_vfsp;
571 			vfs_rlock_wait(vfsp);
572 			if (((dvp = cvp->v_vfsp->vfs_vnodecovered) == NULL) ||
573 			    (cvp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) {
574 				vfs_unlock(vfsp);
575 				return (EIO);
576 			}
577 			vfs_unlock(vfsp);
578 		}
579 	}
580 
581 	if (flags & SMB_IGNORE_CASE)
582 		option_flags = FIGNORECASE;
583 
584 	if (flags & SMB_CATIA)
585 		np = smb_vop_catia_v5tov4(name, namebuf, sizeof (namebuf));
586 
587 	pn_alloc(&rpn);
588 
589 	error = VOP_LOOKUP(dvp, np, vpp, NULL, option_flags, NULL, cr,
590 	    &smb_ct, direntflags, &rpn);
591 
592 	if ((error == 0) && od_name) {
593 		bzero(od_name, MAXNAMELEN);
594 		np = (option_flags == FIGNORECASE) ? rpn.pn_buf : name;
595 
596 		if (flags & SMB_CATIA)
597 			smb_vop_catia_v4tov5(np, od_name, MAXNAMELEN);
598 		else
599 			(void) strlcpy(od_name, np, MAXNAMELEN);
600 	}
601 
602 	pn_free(&rpn);
603 	return (error);
604 }
605 
606 int
607 smb_vop_create(vnode_t *dvp, char *name, smb_attr_t *attr, vnode_t **vpp,
608     int flags, cred_t *cr, vsecattr_t *vsap)
609 {
610 	int error;
611 	int option_flags = 0;
612 	xvattr_t xvattr;
613 	vattr_t *vap;
614 	char *np = name;
615 	char namebuf[MAXNAMELEN];
616 
617 	if (flags & SMB_IGNORE_CASE)
618 		option_flags = FIGNORECASE;
619 
620 	attr->sa_vattr.va_mask = 0;
621 
622 	if (vfs_has_feature(dvp->v_vfsp, VFSFT_XVATTR)) {
623 		smb_vop_setup_xvattr(attr, &xvattr);
624 		vap = &xvattr.xva_vattr;
625 	} else {
626 		smb_sa_to_va_mask(attr->sa_mask, &attr->sa_vattr.va_mask);
627 		vap = &attr->sa_vattr;
628 	}
629 
630 	if (flags & SMB_CATIA) {
631 		np = smb_vop_catia_v5tov4(name, namebuf, sizeof (namebuf));
632 		if (strchr(np, '/') != NULL)
633 			return (EILSEQ);
634 	}
635 
636 	error = VOP_CREATE(dvp, np, vap, EXCL, attr->sa_vattr.va_mode,
637 	    vpp, cr, option_flags, &smb_ct, vsap);
638 
639 	return (error);
640 }
641 
642 int
643 smb_vop_remove(vnode_t *dvp, char *name, int flags, cred_t *cr)
644 {
645 	int error;
646 	int option_flags = 0;
647 	char *np = name;
648 	char namebuf[MAXNAMELEN];
649 
650 	if (flags & SMB_IGNORE_CASE)
651 		option_flags = FIGNORECASE;
652 
653 	if (flags & SMB_CATIA)
654 		np = smb_vop_catia_v5tov4(name, namebuf, sizeof (namebuf));
655 
656 	error = VOP_REMOVE(dvp, np, cr, &smb_ct, option_flags);
657 
658 	return (error);
659 }
660 
661 /*
662  * smb_vop_link(target-dir-vp, source-file-vp, target-name)
663  *
664  * Create a link - same tree (identical TID) only.
665  */
666 int
667 smb_vop_link(vnode_t *to_dvp, vnode_t *from_vp, char *to_name,
668     int flags, cred_t *cr)
669 {
670 	int option_flags = 0;
671 	char *np, *buf;
672 	int rc;
673 
674 	if (flags & SMB_IGNORE_CASE)
675 		option_flags = FIGNORECASE;
676 
677 	if (flags & SMB_CATIA) {
678 		buf = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
679 		np = smb_vop_catia_v5tov4(to_name, buf, MAXNAMELEN);
680 		if (strchr(np, '/') != NULL) {
681 			kmem_free(buf, MAXNAMELEN);
682 			return (EILSEQ);
683 		}
684 
685 		rc = VOP_LINK(to_dvp, from_vp, np, cr, &smb_ct, option_flags);
686 		kmem_free(buf, MAXNAMELEN);
687 		return (rc);
688 	}
689 
690 	rc = VOP_LINK(to_dvp, from_vp, to_name, cr, &smb_ct, option_flags);
691 	return (rc);
692 }
693 
694 /*
695  * smb_vop_rename()
696  *
697  * The rename is for files in the same tree (identical TID) only.
698  */
699 int
700 smb_vop_rename(vnode_t *from_dvp, char *from_name, vnode_t *to_dvp,
701     char *to_name, int flags, cred_t *cr)
702 {
703 	int error;
704 	int option_flags = 0;
705 	char *from, *to, *fbuf, *tbuf;
706 
707 	if (flags & SMB_IGNORE_CASE)
708 		option_flags = FIGNORECASE;
709 
710 	if (flags & SMB_CATIA) {
711 		tbuf = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
712 		to = smb_vop_catia_v5tov4(to_name, tbuf, MAXNAMELEN);
713 		if (strchr(to, '/') != NULL) {
714 			kmem_free(tbuf, MAXNAMELEN);
715 			return (EILSEQ);
716 		}
717 
718 		fbuf = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
719 		from = smb_vop_catia_v5tov4(from_name, fbuf, MAXNAMELEN);
720 
721 		error = VOP_RENAME(from_dvp, from, to_dvp, to, cr,
722 		    &smb_ct, option_flags);
723 
724 		kmem_free(tbuf, MAXNAMELEN);
725 		kmem_free(fbuf, MAXNAMELEN);
726 		return (error);
727 	}
728 
729 	error = VOP_RENAME(from_dvp, from_name, to_dvp, to_name, cr,
730 	    &smb_ct, option_flags);
731 
732 	return (error);
733 }
734 
735 int
736 smb_vop_mkdir(vnode_t *dvp, char *name, smb_attr_t *attr, vnode_t **vpp,
737     int flags, cred_t *cr, vsecattr_t *vsap)
738 {
739 	int error;
740 	int option_flags = 0;
741 	xvattr_t xvattr;
742 	vattr_t *vap;
743 	char *np = name;
744 	char namebuf[MAXNAMELEN];
745 
746 	if (flags & SMB_IGNORE_CASE)
747 		option_flags = FIGNORECASE;
748 
749 	attr->sa_vattr.va_mask = 0;
750 
751 	if (vfs_has_feature(dvp->v_vfsp, VFSFT_XVATTR)) {
752 		smb_vop_setup_xvattr(attr, &xvattr);
753 		vap = &xvattr.xva_vattr;
754 	} else {
755 		smb_sa_to_va_mask(attr->sa_mask, &attr->sa_vattr.va_mask);
756 		vap = &attr->sa_vattr;
757 	}
758 
759 	if (flags & SMB_CATIA) {
760 		np = smb_vop_catia_v5tov4(name, namebuf, sizeof (namebuf));
761 		if (strchr(np, '/') != NULL)
762 			return (EILSEQ);
763 	}
764 
765 	error = VOP_MKDIR(dvp, np, vap, vpp, cr, &smb_ct, option_flags, vsap);
766 
767 	return (error);
768 }
769 
770 /*
771  * smb_vop_rmdir()
772  *
773  * Only simple rmdir supported, consistent with NT semantics
774  * (can only remove an empty directory).
775  *
776  * The third argument to VOP_RMDIR  is the current directory of
777  * the process.  It allows rmdir wants to EINVAL if one tries to
778  * remove ".".  Since SMB servers do not know what their clients'
779  * current directories are, we fake it by supplying a vnode known
780  * to exist and illegal to remove (rootdir).
781  */
782 int
783 smb_vop_rmdir(vnode_t *dvp, char *name, int flags, cred_t *cr)
784 {
785 	int error;
786 	int option_flags = 0;
787 	char *np = name;
788 	char namebuf[MAXNAMELEN];
789 
790 	if (flags & SMB_IGNORE_CASE)
791 		option_flags = FIGNORECASE;
792 
793 	if (flags & SMB_CATIA)
794 		np = smb_vop_catia_v5tov4(name, namebuf, sizeof (namebuf));
795 
796 	error = VOP_RMDIR(dvp, np, rootdir, cr, &smb_ct, option_flags);
797 	return (error);
798 }
799 
800 int
801 smb_vop_commit(vnode_t *vp, cred_t *cr)
802 {
803 	return (VOP_FSYNC(vp, 1, cr, &smb_ct));
804 }
805 
806 static void
807 smb_vop_setup_xvattr(smb_attr_t *smb_attr, xvattr_t *xvattr)
808 {
809 	xoptattr_t *xoap = NULL;
810 	uint_t xva_mask;
811 
812 	/*
813 	 * Initialize xvattr, including bzero
814 	 */
815 	xva_init(xvattr);
816 	xoap = xva_getxoptattr(xvattr);
817 
818 	ASSERT(xoap);
819 
820 	/*
821 	 * Copy caller-specified classic attributes to xvattr.
822 	 * First save xvattr's mask (set in xva_init()), which
823 	 * contains AT_XVATTR.  This is |'d in later if needed.
824 	 */
825 
826 	xva_mask = xvattr->xva_vattr.va_mask;
827 	xvattr->xva_vattr = smb_attr->sa_vattr;
828 
829 	smb_sa_to_va_mask(smb_attr->sa_mask, &xvattr->xva_vattr.va_mask);
830 
831 	/*
832 	 * Do not set ctime (only the file system can do it)
833 	 */
834 
835 	xvattr->xva_vattr.va_mask &= ~AT_CTIME;
836 
837 	if (smb_attr->sa_mask & SMB_AT_DOSATTR) {
838 
839 		/*
840 		 * "|" in the original xva_mask, which contains
841 		 * AT_XVATTR
842 		 */
843 
844 		xvattr->xva_vattr.va_mask |= xva_mask;
845 
846 		XVA_SET_REQ(xvattr, XAT_ARCHIVE);
847 		XVA_SET_REQ(xvattr, XAT_SYSTEM);
848 		XVA_SET_REQ(xvattr, XAT_READONLY);
849 		XVA_SET_REQ(xvattr, XAT_HIDDEN);
850 
851 		/*
852 		 * smb_attr->sa_dosattr: If a given bit is not set,
853 		 * that indicates that the corresponding field needs
854 		 * to be updated with a "0" value.  This is done
855 		 * implicitly as the xoap->xoa_* fields were bzero'd.
856 		 */
857 
858 		if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_ARCHIVE)
859 			xoap->xoa_archive = 1;
860 
861 		if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_SYSTEM)
862 			xoap->xoa_system = 1;
863 
864 		if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_READONLY)
865 			xoap->xoa_readonly = 1;
866 
867 		if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_HIDDEN)
868 			xoap->xoa_hidden = 1;
869 	}
870 
871 	if (smb_attr->sa_mask & SMB_AT_CRTIME) {
872 		/*
873 		 * "|" in the original xva_mask, which contains
874 		 * AT_XVATTR
875 		 */
876 
877 		xvattr->xva_vattr.va_mask |= xva_mask;
878 		XVA_SET_REQ(xvattr, XAT_CREATETIME);
879 		xoap->xoa_createtime = smb_attr->sa_crtime;
880 	}
881 }
882 
883 /*
884  * smb_vop_readdir()
885  *
886  * Collects an SMB_MINLEN_RDDIR_BUF "page" of directory entries.
887  * The directory entries are returned in an fs-independent format by the
888  * underlying file system.  That is, the "page" of information returned is
889  * not literally stored on-disk in the format returned.
890  * If the file system supports extended directory entries (has features
891  * VFSFT_DIRENTFLAGS), set V_RDDIR_ENTFLAGS to cause the buffer to be
892  * filled with edirent_t structures, instead of dirent64_t structures.
893  */
894 int
895 smb_vop_readdir(vnode_t *vp, uint32_t offset,
896     void *buf, int *count, int *eof, cred_t *cr)
897 {
898 	int error = 0;
899 	int rdirent_flags = 0;
900 	int rdirent_size;
901 	struct uio auio;
902 	struct iovec aiov;
903 
904 	if (vp->v_type != VDIR)
905 		return (ENOTDIR);
906 
907 	if (vfs_has_feature(vp->v_vfsp, VFSFT_DIRENTFLAGS)) {
908 		rdirent_flags = V_RDDIR_ENTFLAGS;
909 		rdirent_size = sizeof (edirent_t);
910 	} else {
911 		rdirent_size = sizeof (dirent64_t);
912 	}
913 
914 	if (*count < rdirent_size)
915 		return (EINVAL);
916 
917 	aiov.iov_base = buf;
918 	aiov.iov_len = *count;
919 	auio.uio_iov = &aiov;
920 	auio.uio_iovcnt = 1;
921 	auio.uio_loffset = (uint64_t)offset;
922 	auio.uio_segflg = UIO_SYSSPACE;
923 	auio.uio_resid = *count;
924 	auio.uio_fmode = 0;
925 
926 	(void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, &smb_ct);
927 	error = VOP_READDIR(vp, &auio, cr, eof, &smb_ct, rdirent_flags);
928 	VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, &smb_ct);
929 
930 	if (error == 0)
931 		*count = *count - auio.uio_resid;
932 
933 	return (error);
934 }
935 
936 /*
937  * smb_sa_to_va_mask
938  *
939  * Set va_mask by running through the SMB_AT_* #define's and
940  * setting those bits that correspond to the SMB_AT_* bits
941  * set in sa_mask.
942  */
943 void
944 smb_sa_to_va_mask(uint_t sa_mask, uint_t *va_maskp)
945 {
946 	int i;
947 	uint_t smask;
948 
949 	smask = (sa_mask);
950 	for (i = SMB_AT_TYPE; (i < SMB_AT_MAX) && (smask != 0); ++i) {
951 		if (smask & 1)
952 			*(va_maskp) |= smb_attrmap[i];
953 
954 		smask >>= 1;
955 	}
956 }
957 
958 /*
959  * smb_vop_stream_lookup()
960  *
961  * The name returned in od_name is the on-disk name of the stream with the
962  * SMB_STREAM_PREFIX stripped off.  od_name should be allocated to MAXNAMELEN
963  * by the caller.
964  */
965 int
966 smb_vop_stream_lookup(
967     vnode_t		*fvp,
968     char		*stream_name,
969     vnode_t		**vpp,
970     char		*od_name,
971     vnode_t		**xattrdirvpp,
972     int			flags,
973     vnode_t		*rootvp,
974     cred_t		*cr)
975 {
976 	char *solaris_stream_name;
977 	char *name;
978 	int error, tmpflgs;
979 
980 	if ((error = smb_vop_lookup_xattrdir(fvp, xattrdirvpp,
981 	    LOOKUP_XATTR | CREATE_XATTR_DIR, cr)) != 0)
982 		return (error);
983 
984 	/*
985 	 * Prepend SMB_STREAM_PREFIX to stream name
986 	 */
987 
988 	solaris_stream_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
989 	(void) sprintf(solaris_stream_name, "%s%s", SMB_STREAM_PREFIX,
990 	    stream_name);
991 
992 	/*
993 	 * "name" will hold the on-disk name returned from smb_vop_lookup
994 	 * for the stream, including the SMB_STREAM_PREFIX.
995 	 */
996 
997 	name = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
998 
999 	if ((error = smb_vop_lookup(*xattrdirvpp, solaris_stream_name, vpp,
1000 	    name, flags, &tmpflgs, rootvp, cr)) != 0) {
1001 		VN_RELE(*xattrdirvpp);
1002 	} else {
1003 		(void) strlcpy(od_name, &(name[SMB_STREAM_PREFIX_LEN]),
1004 		    MAXNAMELEN);
1005 	}
1006 
1007 	kmem_free(solaris_stream_name, MAXNAMELEN);
1008 	kmem_free(name, MAXNAMELEN);
1009 
1010 	return (error);
1011 }
1012 
1013 int
1014 smb_vop_stream_create(vnode_t *fvp, char *stream_name, smb_attr_t *attr,
1015     vnode_t **vpp, vnode_t **xattrdirvpp, int flags, cred_t *cr)
1016 {
1017 	char *solaris_stream_name;
1018 	int error;
1019 
1020 	if ((error = smb_vop_lookup_xattrdir(fvp, xattrdirvpp,
1021 	    LOOKUP_XATTR | CREATE_XATTR_DIR, cr)) != 0)
1022 		return (error);
1023 
1024 	/*
1025 	 * Prepend SMB_STREAM_PREFIX to stream name
1026 	 */
1027 
1028 	solaris_stream_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1029 	(void) sprintf(solaris_stream_name, "%s%s", SMB_STREAM_PREFIX,
1030 	    stream_name);
1031 
1032 	if ((error = smb_vop_create(*xattrdirvpp, solaris_stream_name, attr,
1033 	    vpp, flags, cr, NULL)) != 0)
1034 		VN_RELE(*xattrdirvpp);
1035 
1036 	kmem_free(solaris_stream_name, MAXNAMELEN);
1037 
1038 	return (error);
1039 }
1040 
1041 int
1042 smb_vop_stream_remove(vnode_t *vp, char *stream_name, int flags, cred_t *cr)
1043 {
1044 	char *solaris_stream_name;
1045 	vnode_t *xattrdirvp;
1046 	int error;
1047 
1048 	error = smb_vop_lookup_xattrdir(vp, &xattrdirvp, LOOKUP_XATTR, cr);
1049 	if (error != 0)
1050 		return (error);
1051 
1052 	/*
1053 	 * Prepend SMB_STREAM_PREFIX to stream name
1054 	 */
1055 
1056 	solaris_stream_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1057 	(void) sprintf(solaris_stream_name, "%s%s", SMB_STREAM_PREFIX,
1058 	    stream_name);
1059 
1060 	/* XXX might have to use kcred */
1061 	error = smb_vop_remove(xattrdirvp, solaris_stream_name, flags, cr);
1062 
1063 	kmem_free(solaris_stream_name, MAXNAMELEN);
1064 
1065 	return (error);
1066 }
1067 
1068 int
1069 smb_vop_lookup_xattrdir(vnode_t *fvp, vnode_t **xattrdirvpp, int flags,
1070     cred_t *cr)
1071 {
1072 	int error;
1073 
1074 	error = VOP_LOOKUP(fvp, "", xattrdirvpp, NULL, flags, NULL, cr,
1075 	    &smb_ct, NULL, NULL);
1076 	return (error);
1077 }
1078 
1079 /*
1080  * smb_vop_traverse_check()
1081  *
1082  * This function checks to see if the passed-in vnode has a file system
1083  * mounted on it.  If it does, the mount point is "traversed" and the
1084  * vnode for the root of the file system is returned.
1085  */
1086 int
1087 smb_vop_traverse_check(vnode_t **vpp)
1088 {
1089 	int error;
1090 
1091 	if (vn_mountedvfs(*vpp) == 0)
1092 		return (0);
1093 
1094 	/*
1095 	 * traverse() may return a different held vnode, even in the error case.
1096 	 * If it returns a different vnode, it will have released the original.
1097 	 */
1098 
1099 	error = traverse(vpp);
1100 
1101 	return (error);
1102 }
1103 
1104 int /*ARGSUSED*/
1105 smb_vop_statfs(vnode_t *vp, struct statvfs64 *statp, cred_t *cr)
1106 {
1107 	int error;
1108 
1109 	error = VFS_STATVFS(vp->v_vfsp, statp);
1110 
1111 	return (error);
1112 }
1113 
1114 /*
1115  * smb_vop_acl_read
1116  *
1117  * Reads the ACL of the specified file into 'aclp'.
1118  * acl_type is the type of ACL which the filesystem supports.
1119  *
1120  * Caller has to free the allocated memory for aclp by calling
1121  * acl_free().
1122  */
1123 int
1124 smb_vop_acl_read(vnode_t *vp, acl_t **aclp, int flags, acl_type_t acl_type,
1125     cred_t *cr)
1126 {
1127 	int error;
1128 	vsecattr_t vsecattr;
1129 
1130 	ASSERT(vp);
1131 	ASSERT(aclp);
1132 
1133 	*aclp = NULL;
1134 	bzero(&vsecattr, sizeof (vsecattr_t));
1135 
1136 	switch (acl_type) {
1137 	case ACLENT_T:
1138 		vsecattr.vsa_mask = VSA_ACL | VSA_ACLCNT | VSA_DFACL |
1139 		    VSA_DFACLCNT;
1140 		break;
1141 
1142 	case ACE_T:
1143 		vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT | VSA_ACE_ACLFLAGS;
1144 		break;
1145 
1146 	default:
1147 		return (EINVAL);
1148 	}
1149 
1150 	if (error = VOP_GETSECATTR(vp, &vsecattr, flags, cr, &smb_ct))
1151 		return (error);
1152 
1153 	*aclp = smb_fsacl_from_vsa(&vsecattr, acl_type);
1154 	if (vp->v_type == VDIR)
1155 		(*aclp)->acl_flags |= ACL_IS_DIR;
1156 
1157 	return (0);
1158 }
1159 
1160 /*
1161  * smb_vop_acl_write
1162  *
1163  * Writes the given ACL in aclp for the specified file.
1164  */
1165 int
1166 smb_vop_acl_write(vnode_t *vp, acl_t *aclp, int flags, cred_t *cr)
1167 {
1168 	int error;
1169 	vsecattr_t vsecattr;
1170 	int aclbsize;
1171 
1172 	ASSERT(vp);
1173 	ASSERT(aclp);
1174 
1175 	error = smb_fsacl_to_vsa(aclp, &vsecattr, &aclbsize);
1176 
1177 	if (error == 0) {
1178 		(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, &smb_ct);
1179 		error = VOP_SETSECATTR(vp, &vsecattr, flags, cr, &smb_ct);
1180 		VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, &smb_ct);
1181 	}
1182 
1183 	if (aclbsize && vsecattr.vsa_aclentp)
1184 		kmem_free(vsecattr.vsa_aclentp, aclbsize);
1185 
1186 	return (error);
1187 }
1188 
1189 /*
1190  * smb_vop_acl_type
1191  *
1192  * Determines the ACL type for the given vnode.
1193  * ACLENT_T is a Posix ACL and ACE_T is a ZFS ACL.
1194  */
1195 acl_type_t
1196 smb_vop_acl_type(vnode_t *vp)
1197 {
1198 	int error;
1199 	ulong_t whichacl;
1200 
1201 	error = VOP_PATHCONF(vp, _PC_ACL_ENABLED, &whichacl, kcred, NULL);
1202 	if (error != 0) {
1203 		/*
1204 		 * If we got an error, then the filesystem
1205 		 * likely does not understand the _PC_ACL_ENABLED
1206 		 * pathconf.  In this case, we fall back to trying
1207 		 * POSIX-draft (aka UFS-style) ACLs.
1208 		 */
1209 		whichacl = _ACL_ACLENT_ENABLED;
1210 	}
1211 
1212 	if (!(whichacl & (_ACL_ACE_ENABLED | _ACL_ACLENT_ENABLED))) {
1213 		/*
1214 		 * If the file system supports neither ACE nor
1215 		 * ACLENT ACLs we will fall back to UFS-style ACLs
1216 		 * like we did above if there was an error upon
1217 		 * calling VOP_PATHCONF.
1218 		 *
1219 		 * ACE and ACLENT type ACLs are the only interfaces
1220 		 * supported thus far.  If any other bits are set on
1221 		 * 'whichacl' upon return from VOP_PATHCONF, we will
1222 		 * ignore them.
1223 		 */
1224 		whichacl = _ACL_ACLENT_ENABLED;
1225 	}
1226 
1227 	if (whichacl == _ACL_ACLENT_ENABLED)
1228 		return (ACLENT_T);
1229 
1230 	return (ACE_T);
1231 }
1232 
1233 static int zfs_perms[] = {
1234 	ACE_READ_DATA, ACE_WRITE_DATA, ACE_APPEND_DATA, ACE_READ_NAMED_ATTRS,
1235 	ACE_WRITE_NAMED_ATTRS, ACE_EXECUTE, ACE_DELETE_CHILD,
1236 	ACE_READ_ATTRIBUTES, ACE_WRITE_ATTRIBUTES, ACE_DELETE, ACE_READ_ACL,
1237 	ACE_WRITE_ACL, ACE_WRITE_OWNER, ACE_SYNCHRONIZE
1238 };
1239 
1240 static int unix_perms[] = { VREAD, VWRITE, VEXEC };
1241 /*
1242  * smb_vop_eaccess
1243  *
1244  * Returns the effective permission of the given credential for the
1245  * specified object.
1246  *
1247  * This is just a workaround. We need VFS/FS support for this.
1248  */
1249 void
1250 smb_vop_eaccess(vnode_t *vp, int *mode, int flags, vnode_t *dir_vp, cred_t *cr)
1251 {
1252 	int error, i;
1253 	int pnum;
1254 
1255 	*mode = 0;
1256 
1257 	if (flags == V_ACE_MASK) {
1258 		pnum = sizeof (zfs_perms) / sizeof (int);
1259 
1260 		for (i = 0; i < pnum; i++) {
1261 			error = smb_vop_access(vp, zfs_perms[i], flags,
1262 			    dir_vp, cr);
1263 			if (error == 0)
1264 				*mode |= zfs_perms[i];
1265 		}
1266 	} else {
1267 		pnum = sizeof (unix_perms) / sizeof (int);
1268 
1269 		for (i = 0; i < pnum; i++) {
1270 			error = smb_vop_access(vp, unix_perms[i], flags,
1271 			    dir_vp, cr);
1272 			if (error == 0)
1273 				*mode |= unix_perms[i];
1274 		}
1275 	}
1276 }
1277 
1278 /*
1279  * smb_vop_shrlock()
1280  *
1281  * See comments for smb_fsop_shrlock()
1282  */
1283 int
1284 smb_vop_shrlock(vnode_t *vp, uint32_t uniq_fid, uint32_t desired_access,
1285     uint32_t share_access, cred_t *cr)
1286 {
1287 	struct shrlock shr;
1288 	struct shr_locowner shr_own;
1289 	short new_access = 0;
1290 	short deny = 0;
1291 	int flag = 0;
1292 	int cmd;
1293 
1294 	cmd = (nbl_need_check(vp)) ? F_SHARE_NBMAND : F_SHARE;
1295 
1296 	/*
1297 	 * Check if this is a metadata access
1298 	 */
1299 
1300 	if ((desired_access & FILE_DATA_ALL) == 0) {
1301 		new_access |= F_MDACC;
1302 	} else {
1303 		if (desired_access & (ACE_READ_DATA | ACE_EXECUTE)) {
1304 			new_access |= F_RDACC;
1305 			flag |= FREAD;
1306 		}
1307 
1308 		if (desired_access & (ACE_WRITE_DATA | ACE_APPEND_DATA |
1309 		    ACE_ADD_FILE)) {
1310 			new_access |= F_WRACC;
1311 			flag |= FWRITE;
1312 		}
1313 
1314 		if (SMB_DENY_READ(share_access)) {
1315 			deny |= F_RDDNY;
1316 		}
1317 
1318 		if (SMB_DENY_WRITE(share_access)) {
1319 			deny |= F_WRDNY;
1320 		}
1321 
1322 		if (cmd == F_SHARE_NBMAND) {
1323 			if (desired_access & ACE_DELETE)
1324 				new_access |= F_RMACC;
1325 
1326 			if (SMB_DENY_DELETE(share_access)) {
1327 				deny |= F_RMDNY;
1328 			}
1329 		}
1330 	}
1331 
1332 	shr.s_access = new_access;
1333 	shr.s_deny = deny;
1334 	shr.s_sysid = smb_ct.cc_sysid;
1335 	shr.s_pid = uniq_fid;
1336 	shr.s_own_len = sizeof (shr_own);
1337 	shr.s_owner = (caddr_t)&shr_own;
1338 	shr_own.sl_id = shr.s_sysid;
1339 	shr_own.sl_pid = shr.s_pid;
1340 
1341 	return (VOP_SHRLOCK(vp, cmd, &shr, flag, cr, NULL));
1342 }
1343 
1344 int
1345 smb_vop_unshrlock(vnode_t *vp, uint32_t uniq_fid, cred_t *cr)
1346 {
1347 	struct shrlock shr;
1348 	struct shr_locowner shr_own;
1349 
1350 	/*
1351 	 * For s_access and s_deny, we do not need to pass in the original
1352 	 * values.
1353 	 */
1354 
1355 	shr.s_access = 0;
1356 	shr.s_deny = 0;
1357 	shr.s_sysid = smb_ct.cc_sysid;
1358 	shr.s_pid = uniq_fid;
1359 	shr.s_own_len = sizeof (shr_own);
1360 	shr.s_owner = (caddr_t)&shr_own;
1361 	shr_own.sl_id = shr.s_sysid;
1362 	shr_own.sl_pid = shr.s_pid;
1363 
1364 	return (VOP_SHRLOCK(vp, F_UNSHARE, &shr, 0, cr, NULL));
1365 }
1366 
1367 int
1368 smb_vop_frlock(vnode_t *vp, cred_t *cr, int flag, flock64_t *bf)
1369 {
1370 	int cmd = nbl_need_check(vp) ? F_SETLK_NBMAND : F_SETLK;
1371 	flk_callback_t flk_cb;
1372 
1373 	flk_init_callback(&flk_cb, smb_lock_frlock_callback, NULL);
1374 
1375 	return (VOP_FRLOCK(vp, cmd, bf, flag, 0, &flk_cb, cr, &smb_ct));
1376 }
1377 
1378 static callb_cpr_t *
1379 /* ARGSUSED */
1380 smb_lock_frlock_callback(flk_cb_when_t when, void *error)
1381 {
1382 	return (0);
1383 }
1384 
1385 /*
1386  * smb_vop_catia_init_v4_lookup
1387  * Initialize  mapping between wide characters in the range from
1388  * 0x00A4 to 0x00FF and their UNIX (v4) equivalent (wide character).
1389  * Indexed by the decimal value of the wide character (164-255)
1390  * with an offset of -164.
1391  */
1392 static void
1393 smb_vop_catia_init_v4_lookup()
1394 {
1395 	int i, idx, offset = SMB_CATIA_V4_LOOKUP_LOW;
1396 
1397 	for (i = 0; i < SMB_CATIA_V4_LOOKUP_MAX; i++)
1398 		smb_catia_v4_lookup[i] = (mts_wchar_t)(i + offset);
1399 
1400 	for (i = 0; i < SMB_CATIA_NUM_MAPS; i++) {
1401 		idx = (int)catia_maps[i].winchar - offset;
1402 		smb_catia_v4_lookup[idx] = (mts_wchar_t)catia_maps[i].unixchar;
1403 	}
1404 }
1405 
1406 /*
1407  * smb_vop_catia_init_v5_lookup
1408  * Initialize mapping between UNIX ASCII (v4) characters and equivalent
1409  * or translated wide characters.
1410  * Indexed by the decimal value of the ASCII character (0-127).
1411  */
1412 static void
1413 smb_vop_catia_init_v5_lookup()
1414 {
1415 	int i, idx;
1416 
1417 	for (i = 0; i < SMB_CATIA_V5_LOOKUP_MAX; i++)
1418 		smb_catia_v5_lookup[i] = (mts_wchar_t)i;
1419 
1420 	for (i = 0; i < SMB_CATIA_NUM_MAPS; i++) {
1421 		idx = (int)catia_maps[i].unixchar;
1422 		smb_catia_v5_lookup[idx] = catia_maps[i].winchar;
1423 	}
1424 }
1425 
1426 static void
1427 smb_vop_catia_init()
1428 {
1429 	smb_vop_catia_init_v4_lookup();
1430 	smb_vop_catia_init_v5_lookup();
1431 }
1432 
1433 /*
1434  * smb_vop_catia_v5tov4
1435  * (windows (v5) to unix (v4))
1436  *
1437  * Traverse each character in the given source filename and convert the
1438  * multibyte that is equivalent to any special Windows character listed
1439  * in the catia_maps table to the Unix ASCII character if any is
1440  * encountered in the filename. The translated name is returned in buf.
1441  *
1442  * If an error occurs the conversion terminates and name is returned,
1443  * otherwise buf is returned.
1444  */
1445 char *
1446 smb_vop_catia_v5tov4(char *name, char *buf, int buflen)
1447 {
1448 	int v4_idx, numbytes, inc;
1449 	int space_left = buflen - 1; /* one byte reserved for null */
1450 	mts_wchar_t wc;
1451 	char mbstring[MTS_MB_CHAR_MAX];
1452 	char *p, *src = name, *dst = buf;
1453 
1454 	ASSERT(name);
1455 	ASSERT(buf);
1456 
1457 	if (!buf || !name)
1458 		return (name);
1459 
1460 	bzero(buf, buflen);
1461 
1462 	while (*src) {
1463 		if ((numbytes = mts_mbtowc(&wc, src, MTS_MB_CHAR_MAX)) < 0)
1464 			return (name);
1465 
1466 		if (wc < SMB_CATIA_V4_LOOKUP_LOW ||
1467 		    wc > SMB_CATIA_V4_LOOKUP_UPPER) {
1468 			inc = numbytes;
1469 			p = src;
1470 		} else {
1471 			/* Lookup required. */
1472 			v4_idx = (int)wc - SMB_CATIA_V4_LOOKUP_LOW;
1473 			inc = mts_wctomb(mbstring, smb_catia_v4_lookup[v4_idx]);
1474 			p = mbstring;
1475 		}
1476 
1477 		if (space_left < inc)
1478 			return (name);
1479 
1480 		(void) strncpy(dst, p, inc);
1481 		dst += inc;
1482 		space_left -= inc;
1483 		src += numbytes;
1484 	}
1485 
1486 	return (buf);
1487 }
1488 
1489 /*
1490  * smb_vop_catia_v4tov5
1491  * (unix (v4) to windows (v5))
1492  *
1493  * Traverse each character in the given filename 'srcbuf' and convert
1494  * the special Unix character that is listed in the catia_maps table to
1495  * the UTF-8 encoding of the corresponding Windows character if any is
1496  * encountered in the filename.
1497  *
1498  * The translated name is returned in buf.
1499  * If an error occurs the conversion terminates and the original name
1500  * is returned in buf.
1501  */
1502 void
1503 smb_vop_catia_v4tov5(char *name, char *buf, int buflen)
1504 {
1505 	int v5_idx, numbytes;
1506 	int space_left = buflen - 1; /* one byte reserved for null */
1507 	mts_wchar_t wc;
1508 	char mbstring[MTS_MB_CHAR_MAX];
1509 	char *src = name, *dst = buf;
1510 
1511 	ASSERT(name);
1512 	ASSERT(buf);
1513 
1514 	if (!buf || !name)
1515 		return;
1516 
1517 	(void) bzero(buf, buflen);
1518 	while (*src) {
1519 		if (mts_isascii(*src)) {
1520 			/* Lookup required */
1521 			v5_idx = (int)*src++;
1522 			numbytes = mts_wctomb(mbstring,
1523 			    smb_catia_v5_lookup[v5_idx]);
1524 			if (space_left < numbytes)
1525 				break;
1526 			(void) strncpy(dst, mbstring, numbytes);
1527 		} else {
1528 			if ((numbytes = mts_mbtowc(&wc, src,
1529 			    MTS_MB_CHAR_MAX)) < 0)
1530 				break;
1531 			if (space_left < numbytes)
1532 				break;
1533 			(void) strncpy(dst, src, numbytes);
1534 			src += numbytes;
1535 		}
1536 
1537 		dst += numbytes;
1538 		space_left -= numbytes;
1539 	}
1540 
1541 	if (*src)
1542 		(void) strlcpy(buf, name, buflen);
1543 }
1544