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