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