xref: /titanic_50/usr/src/uts/common/fs/smbsrv/smb_vops.c (revision 7b0bedd42192a2f6bcd6fc4b637d23892303a962)
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 
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_extflg = UIO_COPY_DEFAULT;
927 	auio.uio_resid = *count;
928 	auio.uio_fmode = 0;
929 
930 	(void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, &smb_ct);
931 	error = VOP_READDIR(vp, &auio, cr, eof, &smb_ct, flags);
932 	VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, &smb_ct);
933 
934 	if (error == 0)
935 		*count = *count - auio.uio_resid;
936 
937 	return (error);
938 }
939 
940 /*
941  * smb_sa_to_va_mask
942  *
943  * Set va_mask by running through the SMB_AT_* #define's and
944  * setting those bits that correspond to the SMB_AT_* bits
945  * set in sa_mask.
946  */
947 void
948 smb_sa_to_va_mask(uint_t sa_mask, uint_t *va_maskp)
949 {
950 	int i;
951 	uint_t smask;
952 
953 	smask = (sa_mask);
954 	for (i = SMB_AT_TYPE; (i < SMB_AT_MAX) && (smask != 0); ++i) {
955 		if (smask & 1)
956 			*(va_maskp) |= smb_attrmap[i];
957 
958 		smask >>= 1;
959 	}
960 }
961 
962 /*
963  * smb_vop_stream_lookup()
964  *
965  * The name returned in od_name is the on-disk name of the stream with the
966  * SMB_STREAM_PREFIX stripped off.  od_name should be allocated to MAXNAMELEN
967  * by the caller.
968  */
969 int
970 smb_vop_stream_lookup(
971     vnode_t		*fvp,
972     char		*stream_name,
973     vnode_t		**vpp,
974     char		*od_name,
975     vnode_t		**xattrdirvpp,
976     int			flags,
977     vnode_t		*rootvp,
978     cred_t		*cr)
979 {
980 	char *solaris_stream_name;
981 	char *name;
982 	int error, tmpflgs;
983 
984 	if ((error = smb_vop_lookup_xattrdir(fvp, xattrdirvpp,
985 	    LOOKUP_XATTR | CREATE_XATTR_DIR, cr)) != 0)
986 		return (error);
987 
988 	/*
989 	 * Prepend SMB_STREAM_PREFIX to stream name
990 	 */
991 
992 	solaris_stream_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
993 	(void) sprintf(solaris_stream_name, "%s%s", SMB_STREAM_PREFIX,
994 	    stream_name);
995 
996 	/*
997 	 * "name" will hold the on-disk name returned from smb_vop_lookup
998 	 * for the stream, including the SMB_STREAM_PREFIX.
999 	 */
1000 
1001 	name = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
1002 
1003 	if ((error = smb_vop_lookup(*xattrdirvpp, solaris_stream_name, vpp,
1004 	    name, flags, &tmpflgs, rootvp, cr)) != 0) {
1005 		VN_RELE(*xattrdirvpp);
1006 	} else {
1007 		(void) strlcpy(od_name, &(name[SMB_STREAM_PREFIX_LEN]),
1008 		    MAXNAMELEN);
1009 	}
1010 
1011 	kmem_free(solaris_stream_name, MAXNAMELEN);
1012 	kmem_free(name, MAXNAMELEN);
1013 
1014 	return (error);
1015 }
1016 
1017 int
1018 smb_vop_stream_create(vnode_t *fvp, char *stream_name, smb_attr_t *attr,
1019     vnode_t **vpp, vnode_t **xattrdirvpp, int flags, cred_t *cr)
1020 {
1021 	char *solaris_stream_name;
1022 	int error;
1023 
1024 	if ((error = smb_vop_lookup_xattrdir(fvp, xattrdirvpp,
1025 	    LOOKUP_XATTR | CREATE_XATTR_DIR, cr)) != 0)
1026 		return (error);
1027 
1028 	/*
1029 	 * Prepend SMB_STREAM_PREFIX to stream name
1030 	 */
1031 
1032 	solaris_stream_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1033 	(void) sprintf(solaris_stream_name, "%s%s", SMB_STREAM_PREFIX,
1034 	    stream_name);
1035 
1036 	if ((error = smb_vop_create(*xattrdirvpp, solaris_stream_name, attr,
1037 	    vpp, flags, cr, NULL)) != 0)
1038 		VN_RELE(*xattrdirvpp);
1039 
1040 	kmem_free(solaris_stream_name, MAXNAMELEN);
1041 
1042 	return (error);
1043 }
1044 
1045 int
1046 smb_vop_stream_remove(vnode_t *vp, char *stream_name, int flags, cred_t *cr)
1047 {
1048 	char *solaris_stream_name;
1049 	vnode_t *xattrdirvp;
1050 	int error;
1051 
1052 	error = smb_vop_lookup_xattrdir(vp, &xattrdirvp, LOOKUP_XATTR, cr);
1053 	if (error != 0)
1054 		return (error);
1055 
1056 	/*
1057 	 * Prepend SMB_STREAM_PREFIX to stream name
1058 	 */
1059 
1060 	solaris_stream_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1061 	(void) sprintf(solaris_stream_name, "%s%s", SMB_STREAM_PREFIX,
1062 	    stream_name);
1063 
1064 	/* XXX might have to use kcred */
1065 	error = smb_vop_remove(xattrdirvp, solaris_stream_name, flags, cr);
1066 
1067 	kmem_free(solaris_stream_name, MAXNAMELEN);
1068 
1069 	return (error);
1070 }
1071 
1072 int
1073 smb_vop_lookup_xattrdir(vnode_t *fvp, vnode_t **xattrdirvpp, int flags,
1074     cred_t *cr)
1075 {
1076 	int error;
1077 
1078 	error = VOP_LOOKUP(fvp, "", xattrdirvpp, NULL, flags, NULL, cr,
1079 	    &smb_ct, NULL, NULL);
1080 	return (error);
1081 }
1082 
1083 /*
1084  * smb_vop_traverse_check()
1085  *
1086  * This function checks to see if the passed-in vnode has a file system
1087  * mounted on it.  If it does, the mount point is "traversed" and the
1088  * vnode for the root of the file system is returned.
1089  */
1090 int
1091 smb_vop_traverse_check(vnode_t **vpp)
1092 {
1093 	int error;
1094 
1095 	if (vn_mountedvfs(*vpp) == 0)
1096 		return (0);
1097 
1098 	/*
1099 	 * traverse() may return a different held vnode, even in the error case.
1100 	 * If it returns a different vnode, it will have released the original.
1101 	 */
1102 
1103 	error = traverse(vpp);
1104 
1105 	return (error);
1106 }
1107 
1108 int /*ARGSUSED*/
1109 smb_vop_statfs(vnode_t *vp, struct statvfs64 *statp, cred_t *cr)
1110 {
1111 	int error;
1112 
1113 	error = VFS_STATVFS(vp->v_vfsp, statp);
1114 
1115 	return (error);
1116 }
1117 
1118 /*
1119  * smb_vop_acl_read
1120  *
1121  * Reads the ACL of the specified file into 'aclp'.
1122  * acl_type is the type of ACL which the filesystem supports.
1123  *
1124  * Caller has to free the allocated memory for aclp by calling
1125  * acl_free().
1126  */
1127 int
1128 smb_vop_acl_read(vnode_t *vp, acl_t **aclp, int flags, acl_type_t acl_type,
1129     cred_t *cr)
1130 {
1131 	int error;
1132 	vsecattr_t vsecattr;
1133 
1134 	ASSERT(vp);
1135 	ASSERT(aclp);
1136 
1137 	*aclp = NULL;
1138 	bzero(&vsecattr, sizeof (vsecattr_t));
1139 
1140 	switch (acl_type) {
1141 	case ACLENT_T:
1142 		vsecattr.vsa_mask = VSA_ACL | VSA_ACLCNT | VSA_DFACL |
1143 		    VSA_DFACLCNT;
1144 		break;
1145 
1146 	case ACE_T:
1147 		vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT | VSA_ACE_ACLFLAGS;
1148 		break;
1149 
1150 	default:
1151 		return (EINVAL);
1152 	}
1153 
1154 	if (error = VOP_GETSECATTR(vp, &vsecattr, flags, cr, &smb_ct))
1155 		return (error);
1156 
1157 	*aclp = smb_fsacl_from_vsa(&vsecattr, acl_type);
1158 	if (vp->v_type == VDIR)
1159 		(*aclp)->acl_flags |= ACL_IS_DIR;
1160 
1161 	return (0);
1162 }
1163 
1164 /*
1165  * smb_vop_acl_write
1166  *
1167  * Writes the given ACL in aclp for the specified file.
1168  */
1169 int
1170 smb_vop_acl_write(vnode_t *vp, acl_t *aclp, int flags, cred_t *cr)
1171 {
1172 	int error;
1173 	vsecattr_t vsecattr;
1174 	int aclbsize;
1175 
1176 	ASSERT(vp);
1177 	ASSERT(aclp);
1178 
1179 	error = smb_fsacl_to_vsa(aclp, &vsecattr, &aclbsize);
1180 
1181 	if (error == 0) {
1182 		(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, &smb_ct);
1183 		error = VOP_SETSECATTR(vp, &vsecattr, flags, cr, &smb_ct);
1184 		VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, &smb_ct);
1185 	}
1186 
1187 	if (aclbsize && vsecattr.vsa_aclentp)
1188 		kmem_free(vsecattr.vsa_aclentp, aclbsize);
1189 
1190 	return (error);
1191 }
1192 
1193 /*
1194  * smb_vop_acl_type
1195  *
1196  * Determines the ACL type for the given vnode.
1197  * ACLENT_T is a Posix ACL and ACE_T is a ZFS ACL.
1198  */
1199 acl_type_t
1200 smb_vop_acl_type(vnode_t *vp)
1201 {
1202 	int error;
1203 	ulong_t whichacl;
1204 
1205 	error = VOP_PATHCONF(vp, _PC_ACL_ENABLED, &whichacl, kcred, NULL);
1206 	if (error != 0) {
1207 		/*
1208 		 * If we got an error, then the filesystem
1209 		 * likely does not understand the _PC_ACL_ENABLED
1210 		 * pathconf.  In this case, we fall back to trying
1211 		 * POSIX-draft (aka UFS-style) ACLs.
1212 		 */
1213 		whichacl = _ACL_ACLENT_ENABLED;
1214 	}
1215 
1216 	if (!(whichacl & (_ACL_ACE_ENABLED | _ACL_ACLENT_ENABLED))) {
1217 		/*
1218 		 * If the file system supports neither ACE nor
1219 		 * ACLENT ACLs we will fall back to UFS-style ACLs
1220 		 * like we did above if there was an error upon
1221 		 * calling VOP_PATHCONF.
1222 		 *
1223 		 * ACE and ACLENT type ACLs are the only interfaces
1224 		 * supported thus far.  If any other bits are set on
1225 		 * 'whichacl' upon return from VOP_PATHCONF, we will
1226 		 * ignore them.
1227 		 */
1228 		whichacl = _ACL_ACLENT_ENABLED;
1229 	}
1230 
1231 	if (whichacl == _ACL_ACLENT_ENABLED)
1232 		return (ACLENT_T);
1233 
1234 	return (ACE_T);
1235 }
1236 
1237 static int zfs_perms[] = {
1238 	ACE_READ_DATA, ACE_WRITE_DATA, ACE_APPEND_DATA, ACE_READ_NAMED_ATTRS,
1239 	ACE_WRITE_NAMED_ATTRS, ACE_EXECUTE, ACE_DELETE_CHILD,
1240 	ACE_READ_ATTRIBUTES, ACE_WRITE_ATTRIBUTES, ACE_DELETE, ACE_READ_ACL,
1241 	ACE_WRITE_ACL, ACE_WRITE_OWNER, ACE_SYNCHRONIZE
1242 };
1243 
1244 static int unix_perms[] = { VREAD, VWRITE, VEXEC };
1245 /*
1246  * smb_vop_eaccess
1247  *
1248  * Returns the effective permission of the given credential for the
1249  * specified object.
1250  *
1251  * This is just a workaround. We need VFS/FS support for this.
1252  */
1253 void
1254 smb_vop_eaccess(vnode_t *vp, int *mode, int flags, vnode_t *dir_vp, cred_t *cr)
1255 {
1256 	int error, i;
1257 	int pnum;
1258 
1259 	*mode = 0;
1260 
1261 	if (flags == V_ACE_MASK) {
1262 		pnum = sizeof (zfs_perms) / sizeof (int);
1263 
1264 		for (i = 0; i < pnum; i++) {
1265 			error = smb_vop_access(vp, zfs_perms[i], flags,
1266 			    dir_vp, cr);
1267 			if (error == 0)
1268 				*mode |= zfs_perms[i];
1269 		}
1270 	} else {
1271 		pnum = sizeof (unix_perms) / sizeof (int);
1272 
1273 		for (i = 0; i < pnum; i++) {
1274 			error = smb_vop_access(vp, unix_perms[i], flags,
1275 			    dir_vp, cr);
1276 			if (error == 0)
1277 				*mode |= unix_perms[i];
1278 		}
1279 	}
1280 }
1281 
1282 /*
1283  * smb_vop_shrlock()
1284  *
1285  * See comments for smb_fsop_shrlock()
1286  */
1287 int
1288 smb_vop_shrlock(vnode_t *vp, uint32_t uniq_fid, uint32_t desired_access,
1289     uint32_t share_access, cred_t *cr)
1290 {
1291 	struct shrlock shr;
1292 	struct shr_locowner shr_own;
1293 	short new_access = 0;
1294 	short deny = 0;
1295 	int flag = 0;
1296 	int cmd;
1297 
1298 	cmd = (nbl_need_check(vp)) ? F_SHARE_NBMAND : F_SHARE;
1299 
1300 	/*
1301 	 * Check if this is a metadata access
1302 	 */
1303 
1304 	if ((desired_access & FILE_DATA_ALL) == 0) {
1305 		new_access |= F_MDACC;
1306 	} else {
1307 		if (desired_access & (ACE_READ_DATA | ACE_EXECUTE)) {
1308 			new_access |= F_RDACC;
1309 			flag |= FREAD;
1310 		}
1311 
1312 		if (desired_access & (ACE_WRITE_DATA | ACE_APPEND_DATA |
1313 		    ACE_ADD_FILE)) {
1314 			new_access |= F_WRACC;
1315 			flag |= FWRITE;
1316 		}
1317 
1318 		if (SMB_DENY_READ(share_access)) {
1319 			deny |= F_RDDNY;
1320 		}
1321 
1322 		if (SMB_DENY_WRITE(share_access)) {
1323 			deny |= F_WRDNY;
1324 		}
1325 
1326 		if (cmd == F_SHARE_NBMAND) {
1327 			if (desired_access & ACE_DELETE)
1328 				new_access |= F_RMACC;
1329 
1330 			if (SMB_DENY_DELETE(share_access)) {
1331 				deny |= F_RMDNY;
1332 			}
1333 		}
1334 	}
1335 
1336 	shr.s_access = new_access;
1337 	shr.s_deny = deny;
1338 	shr.s_sysid = smb_ct.cc_sysid;
1339 	shr.s_pid = uniq_fid;
1340 	shr.s_own_len = sizeof (shr_own);
1341 	shr.s_owner = (caddr_t)&shr_own;
1342 	shr_own.sl_id = shr.s_sysid;
1343 	shr_own.sl_pid = shr.s_pid;
1344 
1345 	return (VOP_SHRLOCK(vp, cmd, &shr, flag, cr, NULL));
1346 }
1347 
1348 int
1349 smb_vop_unshrlock(vnode_t *vp, uint32_t uniq_fid, cred_t *cr)
1350 {
1351 	struct shrlock shr;
1352 	struct shr_locowner shr_own;
1353 
1354 	/*
1355 	 * For s_access and s_deny, we do not need to pass in the original
1356 	 * values.
1357 	 */
1358 
1359 	shr.s_access = 0;
1360 	shr.s_deny = 0;
1361 	shr.s_sysid = smb_ct.cc_sysid;
1362 	shr.s_pid = uniq_fid;
1363 	shr.s_own_len = sizeof (shr_own);
1364 	shr.s_owner = (caddr_t)&shr_own;
1365 	shr_own.sl_id = shr.s_sysid;
1366 	shr_own.sl_pid = shr.s_pid;
1367 
1368 	return (VOP_SHRLOCK(vp, F_UNSHARE, &shr, 0, cr, NULL));
1369 }
1370 
1371 int
1372 smb_vop_frlock(vnode_t *vp, cred_t *cr, int flag, flock64_t *bf)
1373 {
1374 	int cmd = nbl_need_check(vp) ? F_SETLK_NBMAND : F_SETLK;
1375 	flk_callback_t flk_cb;
1376 
1377 	flk_init_callback(&flk_cb, smb_lock_frlock_callback, NULL);
1378 
1379 	return (VOP_FRLOCK(vp, cmd, bf, flag, 0, &flk_cb, cr, &smb_ct));
1380 }
1381 
1382 static callb_cpr_t *
1383 /* ARGSUSED */
1384 smb_lock_frlock_callback(flk_cb_when_t when, void *error)
1385 {
1386 	return (0);
1387 }
1388 
1389 /*
1390  * smb_vop_catia_init_v4_lookup
1391  * Initialize  mapping between wide characters in the range from
1392  * 0x00A4 to 0x00FF and their UNIX (v4) equivalent (wide character).
1393  * Indexed by the decimal value of the wide character (164-255)
1394  * with an offset of -164.
1395  */
1396 static void
1397 smb_vop_catia_init_v4_lookup()
1398 {
1399 	int i, idx, offset = SMB_CATIA_V4_LOOKUP_LOW;
1400 
1401 	for (i = 0; i < SMB_CATIA_V4_LOOKUP_MAX; i++)
1402 		smb_catia_v4_lookup[i] = (smb_wchar_t)(i + offset);
1403 
1404 	for (i = 0; i < SMB_CATIA_NUM_MAPS; i++) {
1405 		idx = (int)catia_maps[i].winchar - offset;
1406 		smb_catia_v4_lookup[idx] = (smb_wchar_t)catia_maps[i].unixchar;
1407 	}
1408 }
1409 
1410 /*
1411  * smb_vop_catia_init_v5_lookup
1412  * Initialize mapping between UNIX ASCII (v4) characters and equivalent
1413  * or translated wide characters.
1414  * Indexed by the decimal value of the ASCII character (0-127).
1415  */
1416 static void
1417 smb_vop_catia_init_v5_lookup()
1418 {
1419 	int i, idx;
1420 
1421 	for (i = 0; i < SMB_CATIA_V5_LOOKUP_MAX; i++)
1422 		smb_catia_v5_lookup[i] = (smb_wchar_t)i;
1423 
1424 	for (i = 0; i < SMB_CATIA_NUM_MAPS; i++) {
1425 		idx = (int)catia_maps[i].unixchar;
1426 		smb_catia_v5_lookup[idx] = catia_maps[i].winchar;
1427 	}
1428 }
1429 
1430 static void
1431 smb_vop_catia_init()
1432 {
1433 	smb_vop_catia_init_v4_lookup();
1434 	smb_vop_catia_init_v5_lookup();
1435 }
1436 
1437 /*
1438  * smb_vop_catia_v5tov4
1439  * (windows (v5) to unix (v4))
1440  *
1441  * Traverse each character in the given source filename and convert the
1442  * multibyte that is equivalent to any special Windows character listed
1443  * in the catia_maps table to the Unix ASCII character if any is
1444  * encountered in the filename. The translated name is returned in buf.
1445  *
1446  * If an error occurs the conversion terminates and name is returned,
1447  * otherwise buf is returned.
1448  */
1449 char *
1450 smb_vop_catia_v5tov4(char *name, char *buf, int buflen)
1451 {
1452 	int v4_idx, numbytes, inc;
1453 	int space_left = buflen - 1; /* one byte reserved for null */
1454 	smb_wchar_t wc;
1455 	char mbstring[MTS_MB_CHAR_MAX];
1456 	char *p, *src = name, *dst = buf;
1457 
1458 	ASSERT(name);
1459 	ASSERT(buf);
1460 
1461 	if (!buf || !name)
1462 		return (name);
1463 
1464 	bzero(buf, buflen);
1465 
1466 	while (*src) {
1467 		if ((numbytes = smb_mbtowc(&wc, src, MTS_MB_CHAR_MAX)) < 0)
1468 			return (name);
1469 
1470 		if (wc < SMB_CATIA_V4_LOOKUP_LOW ||
1471 		    wc > SMB_CATIA_V4_LOOKUP_UPPER) {
1472 			inc = numbytes;
1473 			p = src;
1474 		} else {
1475 			/* Lookup required. */
1476 			v4_idx = (int)wc - SMB_CATIA_V4_LOOKUP_LOW;
1477 			inc = smb_wctomb(mbstring, smb_catia_v4_lookup[v4_idx]);
1478 			p = mbstring;
1479 		}
1480 
1481 		if (space_left < inc)
1482 			return (name);
1483 
1484 		(void) strncpy(dst, p, inc);
1485 		dst += inc;
1486 		space_left -= inc;
1487 		src += numbytes;
1488 	}
1489 
1490 	return (buf);
1491 }
1492 
1493 /*
1494  * smb_vop_catia_v4tov5
1495  * (unix (v4) to windows (v5))
1496  *
1497  * Traverse each character in the given filename 'srcbuf' and convert
1498  * the special Unix character that is listed in the catia_maps table to
1499  * the UTF-8 encoding of the corresponding Windows character if any is
1500  * encountered in the filename.
1501  *
1502  * The translated name is returned in buf.
1503  * If an error occurs the conversion terminates and the original name
1504  * is returned in buf.
1505  */
1506 void
1507 smb_vop_catia_v4tov5(char *name, char *buf, int buflen)
1508 {
1509 	int v5_idx, numbytes;
1510 	int space_left = buflen - 1; /* one byte reserved for null */
1511 	smb_wchar_t wc;
1512 	char mbstring[MTS_MB_CHAR_MAX];
1513 	char *src = name, *dst = buf;
1514 
1515 	ASSERT(name);
1516 	ASSERT(buf);
1517 
1518 	if (!buf || !name)
1519 		return;
1520 
1521 	(void) bzero(buf, buflen);
1522 	while (*src) {
1523 		if (smb_isascii(*src)) {
1524 			/* Lookup required */
1525 			v5_idx = (int)*src++;
1526 			numbytes = smb_wctomb(mbstring,
1527 			    smb_catia_v5_lookup[v5_idx]);
1528 			if (space_left < numbytes)
1529 				break;
1530 			(void) strncpy(dst, mbstring, numbytes);
1531 		} else {
1532 			if ((numbytes = smb_mbtowc(&wc, src,
1533 			    MTS_MB_CHAR_MAX)) < 0)
1534 				break;
1535 			if (space_left < numbytes)
1536 				break;
1537 			(void) strncpy(dst, src, numbytes);
1538 			src += numbytes;
1539 		}
1540 
1541 		dst += numbytes;
1542 		space_left -= numbytes;
1543 	}
1544 
1545 	if (*src)
1546 		(void) strlcpy(buf, name, buflen);
1547 }
1548