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