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