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