xref: /titanic_41/usr/src/uts/common/fs/smbsrv/smb_vops.c (revision 8b2cc8ac894f2d58f38cf2fb7c3ac778f4c57c09)
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) {
389 			tmp_attr.sa_vattr.va_mask = AT_SIZE;
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 		}
398 	}
399 
400 	if (ret_attr->sa_vattr.va_type == VDIR)
401 		ret_attr->sa_dosattr |= FILE_ATTRIBUTE_DIRECTORY;
402 
403 	return (error);
404 }
405 
406 /*
407  * smb_vop_setattr()
408  *
409  * smb_fsop_setattr()/smb_vop_setattr() should always be used instead of
410  * VOP_SETATTR() when calling from the CIFS service, due to special processing
411  * for streams files.
412  *
413  * Streams have a size but otherwise do not have separate attributes from
414  * the (unnamed stream) file, i.e., the security and ownership of the file
415  * applies to the stream.  In contrast, extended attribute files, which are
416  * used to implement streams, are independent objects with their own
417  * attributes.
418  *
419  * For compatibility with streams, we set the size on the extended attribute
420  * file and apply other attributes to the (unnamed stream) file.  The one
421  * exception is that the UID and GID can be set on the stream by passing a
422  * NULL unnamed_vp, which allows callers to synchronize stream ownership
423  * with the (unnamed stream) file.
424  */
425 int
426 smb_vop_setattr(vnode_t *vp, vnode_t *unnamed_vp, smb_attr_t *set_attr,
427     int flags, cred_t *cr)
428 {
429 	int error = 0;
430 	int at_size = 0;
431 	vnode_t *use_vp;
432 	xvattr_t xvattr;
433 	vattr_t *vap;
434 
435 	if (unnamed_vp) {
436 		use_vp = unnamed_vp;
437 		if (set_attr->sa_mask & SMB_AT_SIZE) {
438 			at_size = 1;
439 			set_attr->sa_mask &= ~SMB_AT_SIZE;
440 		}
441 	} else {
442 		use_vp = vp;
443 	}
444 
445 	/*
446 	 * The caller should not be setting sa_vattr.va_mask,
447 	 * but rather sa_mask.
448 	 */
449 
450 	set_attr->sa_vattr.va_mask = 0;
451 
452 	if (vfs_has_feature(use_vp->v_vfsp, VFSFT_XVATTR)) {
453 		smb_vop_setup_xvattr(set_attr, &xvattr);
454 		vap = &xvattr.xva_vattr;
455 	} else {
456 		smb_sa_to_va_mask(set_attr->sa_mask,
457 		    &set_attr->sa_vattr.va_mask);
458 		vap = &set_attr->sa_vattr;
459 	}
460 
461 	if ((error = VOP_SETATTR(use_vp, vap, flags, cr, &smb_ct)) != 0)
462 		return (error);
463 
464 	if (at_size) {
465 		set_attr->sa_vattr.va_mask = AT_SIZE;
466 		error = VOP_SETATTR(vp, &set_attr->sa_vattr, flags, cr,
467 		    &smb_ct);
468 	}
469 
470 	return (error);
471 }
472 
473 /*
474  * smb_vop_access
475  *
476  * This is a wrapper round VOP_ACCESS. VOP_ACCESS checks the given mode
477  * against file's ACL or Unix permissions. CIFS on the other hand needs to
478  * know if the requested operation can succeed for the given object, this
479  * requires more checks in case of DELETE bit since permissions on the parent
480  * directory are important as well. Based on Windows rules if parent's ACL
481  * grant FILE_DELETE_CHILD a file can be delete regardless of the file's
482  * permissions.
483  */
484 int
485 smb_vop_access(vnode_t *vp, int mode, int flags, vnode_t *dir_vp, cred_t *cr)
486 {
487 	int error = 0;
488 
489 	if (mode == 0)
490 		return (0);
491 
492 	if ((flags == V_ACE_MASK) && (mode & ACE_DELETE)) {
493 		if (dir_vp) {
494 			error = VOP_ACCESS(dir_vp, ACE_DELETE_CHILD, flags,
495 			    cr, NULL);
496 
497 			if (error == 0)
498 				mode &= ~ACE_DELETE;
499 		}
500 	}
501 
502 	if (mode) {
503 		error = VOP_ACCESS(vp, mode, flags, cr, NULL);
504 	}
505 
506 	return (error);
507 }
508 
509 /*
510  * smb_vop_lookup
511  *
512  * dvp:		directory vnode (in)
513  * name:	name of file to be looked up (in)
514  * vpp:		looked-up vnode (out)
515  * od_name:	on-disk name of file (out).
516  *		This parameter is optional.  If a pointer is passed in, it
517  * 		must be allocated with MAXNAMELEN bytes
518  * rootvp:	vnode of the tree root (in)
519  *		This parameter is always passed in non-NULL except at the time
520  *		of share set up.
521  * direntflags:	dirent flags returned from VOP_LOOKUP
522  */
523 int
524 smb_vop_lookup(
525     vnode_t		*dvp,
526     char		*name,
527     vnode_t		**vpp,
528     char		*od_name,
529     int			flags,
530     int			*direntflags,
531     vnode_t		*rootvp,
532     cred_t		*cr)
533 {
534 	int error = 0;
535 	int option_flags = 0;
536 	pathname_t rpn;
537 	char *np = name;
538 	char namebuf[MAXNAMELEN];
539 
540 	if (*name == '\0')
541 		return (EINVAL);
542 
543 	ASSERT(vpp);
544 	*vpp = NULL;
545 	*direntflags = 0;
546 
547 	if ((name[0] == '.') && (name[1] == '.') && (name[2] == 0)) {
548 		if (rootvp && (dvp == rootvp)) {
549 			VN_HOLD(dvp);
550 			*vpp = dvp;
551 			return (0);
552 		}
553 
554 		if (dvp->v_flag & VROOT) {
555 			vfs_t *vfsp;
556 			vnode_t *cvp = dvp;
557 
558 			/*
559 			 * Set dvp and check for races with forced unmount
560 			 * (see lookuppnvp())
561 			 */
562 
563 			vfsp = cvp->v_vfsp;
564 			vfs_rlock_wait(vfsp);
565 			if (((dvp = cvp->v_vfsp->vfs_vnodecovered) == NULL) ||
566 			    (cvp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) {
567 				vfs_unlock(vfsp);
568 				return (EIO);
569 			}
570 			vfs_unlock(vfsp);
571 		}
572 	}
573 
574 	if (flags & SMB_IGNORE_CASE)
575 		option_flags = FIGNORECASE;
576 
577 	if (flags & SMB_CATIA)
578 		np = smb_vop_catia_v5tov4(name, namebuf, sizeof (namebuf));
579 
580 	pn_alloc(&rpn);
581 
582 	error = VOP_LOOKUP(dvp, np, vpp, NULL, option_flags, NULL, cr,
583 	    &smb_ct, direntflags, &rpn);
584 
585 	if ((error == 0) && od_name) {
586 		bzero(od_name, MAXNAMELEN);
587 		np = (option_flags == FIGNORECASE) ? rpn.pn_buf : name;
588 
589 		if (flags & SMB_CATIA)
590 			smb_vop_catia_v4tov5(np, od_name, MAXNAMELEN);
591 		else
592 			(void) strlcpy(od_name, np, MAXNAMELEN);
593 	}
594 
595 	pn_free(&rpn);
596 	return (error);
597 }
598 
599 int
600 smb_vop_create(vnode_t *dvp, char *name, smb_attr_t *attr, vnode_t **vpp,
601     int flags, cred_t *cr, vsecattr_t *vsap)
602 {
603 	int error;
604 	int option_flags = 0;
605 	xvattr_t xvattr;
606 	vattr_t *vap;
607 	char *np = name;
608 	char namebuf[MAXNAMELEN];
609 
610 	if (flags & SMB_IGNORE_CASE)
611 		option_flags = FIGNORECASE;
612 
613 	attr->sa_vattr.va_mask = 0;
614 
615 	if (vfs_has_feature(dvp->v_vfsp, VFSFT_XVATTR)) {
616 		smb_vop_setup_xvattr(attr, &xvattr);
617 		vap = &xvattr.xva_vattr;
618 	} else {
619 		smb_sa_to_va_mask(attr->sa_mask, &attr->sa_vattr.va_mask);
620 		vap = &attr->sa_vattr;
621 	}
622 
623 	if (flags & SMB_CATIA) {
624 		np = smb_vop_catia_v5tov4(name, namebuf, sizeof (namebuf));
625 		if (strchr(np, '/') != NULL)
626 			return (EILSEQ);
627 	}
628 
629 	error = VOP_CREATE(dvp, np, vap, EXCL, attr->sa_vattr.va_mode,
630 	    vpp, cr, option_flags, &smb_ct, vsap);
631 
632 	return (error);
633 }
634 
635 int
636 smb_vop_remove(vnode_t *dvp, char *name, int flags, cred_t *cr)
637 {
638 	int error;
639 	int option_flags = 0;
640 	char *np = name;
641 	char namebuf[MAXNAMELEN];
642 
643 	if (flags & SMB_IGNORE_CASE)
644 		option_flags = FIGNORECASE;
645 
646 	if (flags & SMB_CATIA)
647 		np = smb_vop_catia_v5tov4(name, namebuf, sizeof (namebuf));
648 
649 	error = VOP_REMOVE(dvp, np, cr, &smb_ct, option_flags);
650 
651 	return (error);
652 }
653 
654 /*
655  * smb_vop_rename()
656  *
657  * The rename is for files in the same tree (identical TID) only.
658  */
659 int
660 smb_vop_rename(vnode_t *from_dvp, char *from_name, vnode_t *to_dvp,
661     char *to_name, int flags, cred_t *cr)
662 {
663 	int error;
664 	int option_flags = 0;
665 	char *from, *to, *fbuf, *tbuf;
666 
667 	if (flags & SMB_IGNORE_CASE)
668 		option_flags = FIGNORECASE;
669 
670 	if (flags & SMB_CATIA) {
671 		tbuf = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
672 		to = smb_vop_catia_v5tov4(to_name, tbuf, MAXNAMELEN);
673 		if (strchr(to, '/') != NULL) {
674 			kmem_free(tbuf, MAXNAMELEN);
675 			return (EILSEQ);
676 		}
677 
678 		fbuf = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
679 		from = smb_vop_catia_v5tov4(from_name, fbuf, MAXNAMELEN);
680 
681 		error = VOP_RENAME(from_dvp, from, to_dvp, to, cr,
682 		    &smb_ct, option_flags);
683 
684 		kmem_free(tbuf, MAXNAMELEN);
685 		kmem_free(fbuf, MAXNAMELEN);
686 		return (error);
687 	}
688 
689 	error = VOP_RENAME(from_dvp, from_name, to_dvp, to_name, cr,
690 	    &smb_ct, option_flags);
691 
692 	return (error);
693 }
694 
695 int
696 smb_vop_mkdir(vnode_t *dvp, char *name, smb_attr_t *attr, vnode_t **vpp,
697     int flags, cred_t *cr, vsecattr_t *vsap)
698 {
699 	int error;
700 	int option_flags = 0;
701 	xvattr_t xvattr;
702 	vattr_t *vap;
703 	char *np = name;
704 	char namebuf[MAXNAMELEN];
705 
706 	if (flags & SMB_IGNORE_CASE)
707 		option_flags = FIGNORECASE;
708 
709 	attr->sa_vattr.va_mask = 0;
710 
711 	if (vfs_has_feature(dvp->v_vfsp, VFSFT_XVATTR)) {
712 		smb_vop_setup_xvattr(attr, &xvattr);
713 		vap = &xvattr.xva_vattr;
714 	} else {
715 		smb_sa_to_va_mask(attr->sa_mask, &attr->sa_vattr.va_mask);
716 		vap = &attr->sa_vattr;
717 	}
718 
719 	if (flags & SMB_CATIA) {
720 		np = smb_vop_catia_v5tov4(name, namebuf, sizeof (namebuf));
721 		if (strchr(np, '/') != NULL)
722 			return (EILSEQ);
723 	}
724 
725 	error = VOP_MKDIR(dvp, np, vap, vpp, cr, &smb_ct, option_flags, vsap);
726 
727 	return (error);
728 }
729 
730 /*
731  * smb_vop_rmdir()
732  *
733  * Only simple rmdir supported, consistent with NT semantics
734  * (can only remove an empty directory).
735  *
736  * The third argument to VOP_RMDIR  is the current directory of
737  * the process.  It allows rmdir wants to EINVAL if one tries to
738  * remove ".".  Since SMB servers do not know what their clients'
739  * current directories are, we fake it by supplying a vnode known
740  * to exist and illegal to remove (rootdir).
741  */
742 int
743 smb_vop_rmdir(vnode_t *dvp, char *name, int flags, cred_t *cr)
744 {
745 	int error;
746 	int option_flags = 0;
747 	char *np = name;
748 	char namebuf[MAXNAMELEN];
749 
750 	if (flags & SMB_IGNORE_CASE)
751 		option_flags = FIGNORECASE;
752 
753 	if (flags & SMB_CATIA)
754 		np = smb_vop_catia_v5tov4(name, namebuf, sizeof (namebuf));
755 
756 	error = VOP_RMDIR(dvp, np, rootdir, cr, &smb_ct, option_flags);
757 	return (error);
758 }
759 
760 int
761 smb_vop_commit(vnode_t *vp, cred_t *cr)
762 {
763 	return (VOP_FSYNC(vp, 1, cr, &smb_ct));
764 }
765 
766 static void
767 smb_vop_setup_xvattr(smb_attr_t *smb_attr, xvattr_t *xvattr)
768 {
769 	xoptattr_t *xoap = NULL;
770 	uint_t xva_mask;
771 
772 	/*
773 	 * Initialize xvattr, including bzero
774 	 */
775 	xva_init(xvattr);
776 	xoap = xva_getxoptattr(xvattr);
777 
778 	ASSERT(xoap);
779 
780 	/*
781 	 * Copy caller-specified classic attributes to xvattr.
782 	 * First save xvattr's mask (set in xva_init()), which
783 	 * contains AT_XVATTR.  This is |'d in later if needed.
784 	 */
785 
786 	xva_mask = xvattr->xva_vattr.va_mask;
787 	xvattr->xva_vattr = smb_attr->sa_vattr;
788 
789 	smb_sa_to_va_mask(smb_attr->sa_mask, &xvattr->xva_vattr.va_mask);
790 
791 	/*
792 	 * Do not set ctime (only the file system can do it)
793 	 */
794 
795 	xvattr->xva_vattr.va_mask &= ~AT_CTIME;
796 
797 	if (smb_attr->sa_mask & SMB_AT_DOSATTR) {
798 
799 		/*
800 		 * "|" in the original xva_mask, which contains
801 		 * AT_XVATTR
802 		 */
803 
804 		xvattr->xva_vattr.va_mask |= xva_mask;
805 
806 		XVA_SET_REQ(xvattr, XAT_ARCHIVE);
807 		XVA_SET_REQ(xvattr, XAT_SYSTEM);
808 		XVA_SET_REQ(xvattr, XAT_READONLY);
809 		XVA_SET_REQ(xvattr, XAT_HIDDEN);
810 
811 		/*
812 		 * smb_attr->sa_dosattr: If a given bit is not set,
813 		 * that indicates that the corresponding field needs
814 		 * to be updated with a "0" value.  This is done
815 		 * implicitly as the xoap->xoa_* fields were bzero'd.
816 		 */
817 
818 		if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_ARCHIVE)
819 			xoap->xoa_archive = 1;
820 
821 		if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_SYSTEM)
822 			xoap->xoa_system = 1;
823 
824 		if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_READONLY)
825 			xoap->xoa_readonly = 1;
826 
827 		if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_HIDDEN)
828 			xoap->xoa_hidden = 1;
829 	}
830 
831 	if (smb_attr->sa_mask & SMB_AT_CRTIME) {
832 		/*
833 		 * "|" in the original xva_mask, which contains
834 		 * AT_XVATTR
835 		 */
836 
837 		xvattr->xva_vattr.va_mask |= xva_mask;
838 		XVA_SET_REQ(xvattr, XAT_CREATETIME);
839 		xoap->xoa_createtime = smb_attr->sa_crtime;
840 	}
841 }
842 
843 /*
844  * smb_vop_readdir()
845  *
846  * Collects an SMB_MINLEN_RDDIR_BUF "page" of directory entries.
847  * The directory entries are returned in an fs-independent format by the
848  * underlying file system.  That is, the "page" of information returned is
849  * not literally stored on-disk in the format returned.
850  * If the file system supports extended directory entries (has features
851  * VFSFT_DIRENTFLAGS), set V_RDDIR_ENTFLAGS to cause the buffer to be
852  * filled with edirent_t structures, instead of dirent64_t structures.
853  */
854 int
855 smb_vop_readdir(vnode_t *vp, uint32_t offset,
856     void *buf, int *count, int *eof, cred_t *cr)
857 {
858 	int error = 0;
859 	int rdirent_flags = 0;
860 	int rdirent_size;
861 	struct uio auio;
862 	struct iovec aiov;
863 
864 	if (vp->v_type != VDIR)
865 		return (ENOTDIR);
866 
867 	if (vfs_has_feature(vp->v_vfsp, VFSFT_DIRENTFLAGS)) {
868 		rdirent_flags = V_RDDIR_ENTFLAGS;
869 		rdirent_size = sizeof (edirent_t);
870 	} else {
871 		rdirent_size = sizeof (dirent64_t);
872 	}
873 
874 	if (*count < rdirent_size)
875 		return (EINVAL);
876 
877 	aiov.iov_base = buf;
878 	aiov.iov_len = *count;
879 	auio.uio_iov = &aiov;
880 	auio.uio_iovcnt = 1;
881 	auio.uio_loffset = (uint64_t)offset;
882 	auio.uio_segflg = UIO_SYSSPACE;
883 	auio.uio_resid = *count;
884 	auio.uio_fmode = 0;
885 
886 	(void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, &smb_ct);
887 	error = VOP_READDIR(vp, &auio, cr, eof, &smb_ct, rdirent_flags);
888 	VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, &smb_ct);
889 
890 	if (error == 0)
891 		*count = *count - auio.uio_resid;
892 
893 	return (error);
894 }
895 
896 /*
897  * smb_sa_to_va_mask
898  *
899  * Set va_mask by running through the SMB_AT_* #define's and
900  * setting those bits that correspond to the SMB_AT_* bits
901  * set in sa_mask.
902  */
903 void
904 smb_sa_to_va_mask(uint_t sa_mask, uint_t *va_maskp)
905 {
906 	int i;
907 	uint_t smask;
908 
909 	smask = (sa_mask);
910 	for (i = SMB_AT_TYPE; (i < SMB_AT_MAX) && (smask != 0); ++i) {
911 		if (smask & 1)
912 			*(va_maskp) |= smb_attrmap[i];
913 
914 		smask >>= 1;
915 	}
916 }
917 
918 /*
919  * smb_vop_stream_lookup()
920  *
921  * The name returned in od_name is the on-disk name of the stream with the
922  * SMB_STREAM_PREFIX stripped off.  od_name should be allocated to MAXNAMELEN
923  * by the caller.
924  */
925 int
926 smb_vop_stream_lookup(
927     vnode_t		*fvp,
928     char		*stream_name,
929     vnode_t		**vpp,
930     char		*od_name,
931     vnode_t		**xattrdirvpp,
932     int			flags,
933     vnode_t		*rootvp,
934     cred_t		*cr)
935 {
936 	char *solaris_stream_name;
937 	char *name;
938 	int error, tmpflgs;
939 
940 	if ((error = smb_vop_lookup_xattrdir(fvp, xattrdirvpp,
941 	    LOOKUP_XATTR | CREATE_XATTR_DIR, cr)) != 0)
942 		return (error);
943 
944 	/*
945 	 * Prepend SMB_STREAM_PREFIX to stream name
946 	 */
947 
948 	solaris_stream_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
949 	(void) sprintf(solaris_stream_name, "%s%s", SMB_STREAM_PREFIX,
950 	    stream_name);
951 
952 	/*
953 	 * "name" will hold the on-disk name returned from smb_vop_lookup
954 	 * for the stream, including the SMB_STREAM_PREFIX.
955 	 */
956 
957 	name = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
958 
959 	if ((error = smb_vop_lookup(*xattrdirvpp, solaris_stream_name, vpp,
960 	    name, flags, &tmpflgs, rootvp, cr)) != 0) {
961 		VN_RELE(*xattrdirvpp);
962 	} else {
963 		(void) strlcpy(od_name, &(name[SMB_STREAM_PREFIX_LEN]),
964 		    MAXNAMELEN);
965 	}
966 
967 	kmem_free(solaris_stream_name, MAXNAMELEN);
968 	kmem_free(name, MAXNAMELEN);
969 
970 	return (error);
971 }
972 
973 int
974 smb_vop_stream_create(vnode_t *fvp, char *stream_name, smb_attr_t *attr,
975     vnode_t **vpp, vnode_t **xattrdirvpp, int flags, cred_t *cr)
976 {
977 	char *solaris_stream_name;
978 	int error;
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 	if ((error = smb_vop_create(*xattrdirvpp, solaris_stream_name, attr,
993 	    vpp, flags, cr, NULL)) != 0)
994 		VN_RELE(*xattrdirvpp);
995 
996 	kmem_free(solaris_stream_name, MAXNAMELEN);
997 
998 	return (error);
999 }
1000 
1001 int
1002 smb_vop_stream_remove(vnode_t *vp, char *stream_name, int flags, cred_t *cr)
1003 {
1004 	char *solaris_stream_name;
1005 	vnode_t *xattrdirvp;
1006 	int error;
1007 
1008 	error = smb_vop_lookup_xattrdir(vp, &xattrdirvp, LOOKUP_XATTR, cr);
1009 	if (error != 0)
1010 		return (error);
1011 
1012 	/*
1013 	 * Prepend SMB_STREAM_PREFIX to stream name
1014 	 */
1015 
1016 	solaris_stream_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1017 	(void) sprintf(solaris_stream_name, "%s%s", SMB_STREAM_PREFIX,
1018 	    stream_name);
1019 
1020 	/* XXX might have to use kcred */
1021 	error = smb_vop_remove(xattrdirvp, solaris_stream_name, flags, cr);
1022 
1023 	kmem_free(solaris_stream_name, MAXNAMELEN);
1024 
1025 	return (error);
1026 }
1027 
1028 int
1029 smb_vop_lookup_xattrdir(vnode_t *fvp, vnode_t **xattrdirvpp, int flags,
1030     cred_t *cr)
1031 {
1032 	int error;
1033 
1034 	error = VOP_LOOKUP(fvp, "", xattrdirvpp, NULL, flags, NULL, cr,
1035 	    &smb_ct, NULL, NULL);
1036 	return (error);
1037 }
1038 
1039 /*
1040  * smb_vop_traverse_check()
1041  *
1042  * This function checks to see if the passed-in vnode has a file system
1043  * mounted on it.  If it does, the mount point is "traversed" and the
1044  * vnode for the root of the file system is returned.
1045  */
1046 int
1047 smb_vop_traverse_check(vnode_t **vpp)
1048 {
1049 	int error;
1050 
1051 	if (vn_mountedvfs(*vpp) == 0)
1052 		return (0);
1053 
1054 	/*
1055 	 * traverse() may return a different held vnode, even in the error case.
1056 	 * If it returns a different vnode, it will have released the original.
1057 	 */
1058 
1059 	error = traverse(vpp);
1060 
1061 	return (error);
1062 }
1063 
1064 int /*ARGSUSED*/
1065 smb_vop_statfs(vnode_t *vp, struct statvfs64 *statp, cred_t *cr)
1066 {
1067 	int error;
1068 
1069 	error = VFS_STATVFS(vp->v_vfsp, statp);
1070 
1071 	return (error);
1072 }
1073 
1074 /*
1075  * smb_vop_acl_read
1076  *
1077  * Reads the ACL of the specified file into 'aclp'.
1078  * acl_type is the type of ACL which the filesystem supports.
1079  *
1080  * Caller has to free the allocated memory for aclp by calling
1081  * acl_free().
1082  */
1083 int
1084 smb_vop_acl_read(vnode_t *vp, acl_t **aclp, int flags, acl_type_t acl_type,
1085     cred_t *cr)
1086 {
1087 	int error;
1088 	vsecattr_t vsecattr;
1089 
1090 	ASSERT(vp);
1091 	ASSERT(aclp);
1092 
1093 	*aclp = NULL;
1094 	bzero(&vsecattr, sizeof (vsecattr_t));
1095 
1096 	switch (acl_type) {
1097 	case ACLENT_T:
1098 		vsecattr.vsa_mask = VSA_ACL | VSA_ACLCNT | VSA_DFACL |
1099 		    VSA_DFACLCNT;
1100 		break;
1101 
1102 	case ACE_T:
1103 		vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT | VSA_ACE_ACLFLAGS;
1104 		break;
1105 
1106 	default:
1107 		return (EINVAL);
1108 	}
1109 
1110 	if (error = VOP_GETSECATTR(vp, &vsecattr, flags, cr, &smb_ct))
1111 		return (error);
1112 
1113 	*aclp = smb_fsacl_from_vsa(&vsecattr, acl_type);
1114 	if (vp->v_type == VDIR)
1115 		(*aclp)->acl_flags |= ACL_IS_DIR;
1116 
1117 	return (0);
1118 }
1119 
1120 /*
1121  * smb_vop_acl_write
1122  *
1123  * Writes the given ACL in aclp for the specified file.
1124  */
1125 int
1126 smb_vop_acl_write(vnode_t *vp, acl_t *aclp, int flags, cred_t *cr)
1127 {
1128 	int error;
1129 	vsecattr_t vsecattr;
1130 	int aclbsize;
1131 
1132 	ASSERT(vp);
1133 	ASSERT(aclp);
1134 
1135 	error = smb_fsacl_to_vsa(aclp, &vsecattr, &aclbsize);
1136 
1137 	if (error == 0) {
1138 		(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, &smb_ct);
1139 		error = VOP_SETSECATTR(vp, &vsecattr, flags, cr, &smb_ct);
1140 		VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, &smb_ct);
1141 	}
1142 
1143 	if (aclbsize && vsecattr.vsa_aclentp)
1144 		kmem_free(vsecattr.vsa_aclentp, aclbsize);
1145 
1146 	return (error);
1147 }
1148 
1149 /*
1150  * smb_vop_acl_type
1151  *
1152  * Determines the ACL type for the given vnode.
1153  * ACLENT_T is a Posix ACL and ACE_T is a ZFS ACL.
1154  */
1155 acl_type_t
1156 smb_vop_acl_type(vnode_t *vp)
1157 {
1158 	int error;
1159 	ulong_t whichacl;
1160 
1161 	error = VOP_PATHCONF(vp, _PC_ACL_ENABLED, &whichacl, kcred, NULL);
1162 	if (error != 0) {
1163 		/*
1164 		 * If we got an error, then the filesystem
1165 		 * likely does not understand the _PC_ACL_ENABLED
1166 		 * pathconf.  In this case, we fall back to trying
1167 		 * POSIX-draft (aka UFS-style) ACLs.
1168 		 */
1169 		whichacl = _ACL_ACLENT_ENABLED;
1170 	}
1171 
1172 	if (!(whichacl & (_ACL_ACE_ENABLED | _ACL_ACLENT_ENABLED))) {
1173 		/*
1174 		 * If the file system supports neither ACE nor
1175 		 * ACLENT ACLs we will fall back to UFS-style ACLs
1176 		 * like we did above if there was an error upon
1177 		 * calling VOP_PATHCONF.
1178 		 *
1179 		 * ACE and ACLENT type ACLs are the only interfaces
1180 		 * supported thus far.  If any other bits are set on
1181 		 * 'whichacl' upon return from VOP_PATHCONF, we will
1182 		 * ignore them.
1183 		 */
1184 		whichacl = _ACL_ACLENT_ENABLED;
1185 	}
1186 
1187 	if (whichacl == _ACL_ACLENT_ENABLED)
1188 		return (ACLENT_T);
1189 
1190 	return (ACE_T);
1191 }
1192 
1193 static int zfs_perms[] = {
1194 	ACE_READ_DATA, ACE_WRITE_DATA, ACE_APPEND_DATA, ACE_READ_NAMED_ATTRS,
1195 	ACE_WRITE_NAMED_ATTRS, ACE_EXECUTE, ACE_DELETE_CHILD,
1196 	ACE_READ_ATTRIBUTES, ACE_WRITE_ATTRIBUTES, ACE_DELETE, ACE_READ_ACL,
1197 	ACE_WRITE_ACL, ACE_WRITE_OWNER, ACE_SYNCHRONIZE
1198 };
1199 
1200 static int unix_perms[] = { VREAD, VWRITE, VEXEC };
1201 /*
1202  * smb_vop_eaccess
1203  *
1204  * Returns the effective permission of the given credential for the
1205  * specified object.
1206  *
1207  * This is just a workaround. We need VFS/FS support for this.
1208  */
1209 void
1210 smb_vop_eaccess(vnode_t *vp, int *mode, int flags, vnode_t *dir_vp, cred_t *cr)
1211 {
1212 	int error, i;
1213 	int pnum;
1214 
1215 	*mode = 0;
1216 
1217 	if (flags == V_ACE_MASK) {
1218 		pnum = sizeof (zfs_perms) / sizeof (int);
1219 
1220 		for (i = 0; i < pnum; i++) {
1221 			error = smb_vop_access(vp, zfs_perms[i], flags,
1222 			    dir_vp, cr);
1223 			if (error == 0)
1224 				*mode |= zfs_perms[i];
1225 		}
1226 	} else {
1227 		pnum = sizeof (unix_perms) / sizeof (int);
1228 
1229 		for (i = 0; i < pnum; i++) {
1230 			error = smb_vop_access(vp, unix_perms[i], flags,
1231 			    dir_vp, cr);
1232 			if (error == 0)
1233 				*mode |= unix_perms[i];
1234 		}
1235 	}
1236 }
1237 
1238 /*
1239  * smb_vop_shrlock()
1240  *
1241  * See comments for smb_fsop_shrlock()
1242  */
1243 int
1244 smb_vop_shrlock(vnode_t *vp, uint32_t uniq_fid, uint32_t desired_access,
1245     uint32_t share_access, cred_t *cr)
1246 {
1247 	struct shrlock shr;
1248 	struct shr_locowner shr_own;
1249 	short new_access = 0;
1250 	short deny = 0;
1251 	int flag = 0;
1252 	int cmd;
1253 
1254 	cmd = (nbl_need_check(vp)) ? F_SHARE_NBMAND : F_SHARE;
1255 
1256 	/*
1257 	 * Check if this is a metadata access
1258 	 */
1259 
1260 	if ((desired_access & FILE_DATA_ALL) == 0) {
1261 		new_access |= F_MDACC;
1262 	} else {
1263 		if (desired_access & (ACE_READ_DATA | ACE_EXECUTE)) {
1264 			new_access |= F_RDACC;
1265 			flag |= FREAD;
1266 		}
1267 
1268 		if (desired_access & (ACE_WRITE_DATA | ACE_APPEND_DATA |
1269 		    ACE_ADD_FILE)) {
1270 			new_access |= F_WRACC;
1271 			flag |= FWRITE;
1272 		}
1273 
1274 		if (SMB_DENY_READ(share_access)) {
1275 			deny |= F_RDDNY;
1276 		}
1277 
1278 		if (SMB_DENY_WRITE(share_access)) {
1279 			deny |= F_WRDNY;
1280 		}
1281 
1282 		if (cmd == F_SHARE_NBMAND) {
1283 			if (desired_access & ACE_DELETE)
1284 				new_access |= F_RMACC;
1285 
1286 			if (SMB_DENY_DELETE(share_access)) {
1287 				deny |= F_RMDNY;
1288 			}
1289 		}
1290 	}
1291 
1292 	shr.s_access = new_access;
1293 	shr.s_deny = deny;
1294 	shr.s_sysid = smb_ct.cc_sysid;
1295 	shr.s_pid = uniq_fid;
1296 	shr.s_own_len = sizeof (shr_own);
1297 	shr.s_owner = (caddr_t)&shr_own;
1298 	shr_own.sl_id = shr.s_sysid;
1299 	shr_own.sl_pid = shr.s_pid;
1300 
1301 	return (VOP_SHRLOCK(vp, cmd, &shr, flag, cr, NULL));
1302 }
1303 
1304 int
1305 smb_vop_unshrlock(vnode_t *vp, uint32_t uniq_fid, cred_t *cr)
1306 {
1307 	struct shrlock shr;
1308 	struct shr_locowner shr_own;
1309 
1310 	/*
1311 	 * For s_access and s_deny, we do not need to pass in the original
1312 	 * values.
1313 	 */
1314 
1315 	shr.s_access = 0;
1316 	shr.s_deny = 0;
1317 	shr.s_sysid = smb_ct.cc_sysid;
1318 	shr.s_pid = uniq_fid;
1319 	shr.s_own_len = sizeof (shr_own);
1320 	shr.s_owner = (caddr_t)&shr_own;
1321 	shr_own.sl_id = shr.s_sysid;
1322 	shr_own.sl_pid = shr.s_pid;
1323 
1324 	return (VOP_SHRLOCK(vp, F_UNSHARE, &shr, 0, cr, NULL));
1325 }
1326 
1327 int
1328 smb_vop_frlock(vnode_t *vp, cred_t *cr, int flag, flock64_t *bf)
1329 {
1330 	int cmd = nbl_need_check(vp) ? F_SETLK_NBMAND : F_SETLK;
1331 	flk_callback_t flk_cb;
1332 
1333 	flk_init_callback(&flk_cb, smb_lock_frlock_callback, NULL);
1334 
1335 	return (VOP_FRLOCK(vp, cmd, bf, flag, 0, &flk_cb, cr, &smb_ct));
1336 }
1337 
1338 static callb_cpr_t *
1339 /* ARGSUSED */
1340 smb_lock_frlock_callback(flk_cb_when_t when, void *error)
1341 {
1342 	return (0);
1343 }
1344 
1345 /*
1346  * smb_vop_catia_init_v4_lookup
1347  * Initialize  mapping between wide characters in the range from
1348  * 0x00A4 to 0x00FF and their UNIX (v4) equivalent (wide character).
1349  * Indexed by the decimal value of the wide character (164-255)
1350  * with an offset of -164.
1351  */
1352 static void
1353 smb_vop_catia_init_v4_lookup()
1354 {
1355 	int i, idx, offset = SMB_CATIA_V4_LOOKUP_LOW;
1356 
1357 	for (i = 0; i < SMB_CATIA_V4_LOOKUP_MAX; i++)
1358 		smb_catia_v4_lookup[i] = (mts_wchar_t)(i + offset);
1359 
1360 	for (i = 0; i < SMB_CATIA_NUM_MAPS; i++) {
1361 		idx = (int)catia_maps[i].winchar - offset;
1362 		smb_catia_v4_lookup[idx] = (mts_wchar_t)catia_maps[i].unixchar;
1363 	}
1364 }
1365 
1366 /*
1367  * smb_vop_catia_init_v5_lookup
1368  * Initialize mapping between UNIX ASCII (v4) characters and equivalent
1369  * or translated wide characters.
1370  * Indexed by the decimal value of the ASCII character (0-127).
1371  */
1372 static void
1373 smb_vop_catia_init_v5_lookup()
1374 {
1375 	int i, idx;
1376 
1377 	for (i = 0; i < SMB_CATIA_V5_LOOKUP_MAX; i++)
1378 		smb_catia_v5_lookup[i] = (mts_wchar_t)i;
1379 
1380 	for (i = 0; i < SMB_CATIA_NUM_MAPS; i++) {
1381 		idx = (int)catia_maps[i].unixchar;
1382 		smb_catia_v5_lookup[idx] = catia_maps[i].winchar;
1383 	}
1384 }
1385 
1386 static void
1387 smb_vop_catia_init()
1388 {
1389 	smb_vop_catia_init_v4_lookup();
1390 	smb_vop_catia_init_v5_lookup();
1391 }
1392 
1393 /*
1394  * smb_vop_catia_v5tov4
1395  * (windows (v5) to unix (v4))
1396  *
1397  * Traverse each character in the given source filename and convert the
1398  * multibyte that is equivalent to any special Windows character listed
1399  * in the catia_maps table to the Unix ASCII character if any is
1400  * encountered in the filename. The translated name is returned in buf.
1401  *
1402  * If an error occurs the conversion terminates and name is returned,
1403  * otherwise buf is returned.
1404  */
1405 char *
1406 smb_vop_catia_v5tov4(char *name, char *buf, int buflen)
1407 {
1408 	int v4_idx, numbytes, inc;
1409 	int space_left = buflen - 1; /* one byte reserved for null */
1410 	mts_wchar_t wc;
1411 	char mbstring[MTS_MB_CHAR_MAX];
1412 	char *p, *src = name, *dst = buf;
1413 
1414 	ASSERT(name);
1415 	ASSERT(buf);
1416 
1417 	if (!buf || !name)
1418 		return (name);
1419 
1420 	bzero(buf, buflen);
1421 
1422 	while (*src) {
1423 		if ((numbytes = mts_mbtowc(&wc, src, MTS_MB_CHAR_MAX)) < 0)
1424 			return (name);
1425 
1426 		if (wc < SMB_CATIA_V4_LOOKUP_LOW ||
1427 		    wc > SMB_CATIA_V4_LOOKUP_UPPER) {
1428 			inc = numbytes;
1429 			p = src;
1430 		} else {
1431 			/* Lookup required. */
1432 			v4_idx = (int)wc - SMB_CATIA_V4_LOOKUP_LOW;
1433 			inc = mts_wctomb(mbstring, smb_catia_v4_lookup[v4_idx]);
1434 			p = mbstring;
1435 		}
1436 
1437 		if (space_left < inc)
1438 			return (name);
1439 
1440 		(void) strncpy(dst, p, inc);
1441 		dst += inc;
1442 		space_left -= inc;
1443 		src += numbytes;
1444 	}
1445 
1446 	return (buf);
1447 }
1448 
1449 /*
1450  * smb_vop_catia_v4tov5
1451  * (unix (v4) to windows (v5))
1452  *
1453  * Traverse each character in the given filename 'srcbuf' and convert
1454  * the special Unix character that is listed in the catia_maps table to
1455  * the UTF-8 encoding of the corresponding Windows character if any is
1456  * encountered in the filename.
1457  *
1458  * The translated name is returned in buf.
1459  * If an error occurs the conversion terminates and the original name
1460  * is returned in buf.
1461  */
1462 void
1463 smb_vop_catia_v4tov5(char *name, char *buf, int buflen)
1464 {
1465 	int v5_idx, numbytes;
1466 	int space_left = buflen - 1; /* one byte reserved for null */
1467 	mts_wchar_t wc;
1468 	char mbstring[MTS_MB_CHAR_MAX];
1469 	char *src = name, *dst = buf;
1470 
1471 	ASSERT(name);
1472 	ASSERT(buf);
1473 
1474 	if (!buf || !name)
1475 		return;
1476 
1477 	(void) bzero(buf, buflen);
1478 	while (*src) {
1479 		if (mts_isascii(*src)) {
1480 			/* Lookup required */
1481 			v5_idx = (int)*src++;
1482 			numbytes = mts_wctomb(mbstring,
1483 			    smb_catia_v5_lookup[v5_idx]);
1484 			if (space_left < numbytes)
1485 				break;
1486 			(void) strncpy(dst, mbstring, numbytes);
1487 		} else {
1488 			if ((numbytes = mts_mbtowc(&wc, src,
1489 			    MTS_MB_CHAR_MAX)) < 0)
1490 				break;
1491 			if (space_left < numbytes)
1492 				break;
1493 			(void) strncpy(dst, src, numbytes);
1494 			src += numbytes;
1495 		}
1496 
1497 		dst += numbytes;
1498 		space_left -= numbytes;
1499 	}
1500 
1501 	if (*src)
1502 		(void) strlcpy(buf, name, buflen);
1503 }
1504