xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_vops.c (revision 29267a9d01e87d9c5d871a7bb863719d89a51281)
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 2020 Tintri by DDN, 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 <sys/priv_const.h>
39 #include <sys/policy.h>
40 #include <nfs/lm.h>
41 
42 #include <smbsrv/smb_kproto.h>
43 #include <smbsrv/string.h>
44 #include <smbsrv/smb_vops.h>
45 #include <smbsrv/smb_fsops.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 	smb_wchar_t winchar;	/* v5 */
114 } smb_catia_map_t;
115 
116 smb_catia_map_t const 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 smb_wchar_t smb_catia_v5_lookup[SMB_CATIA_V5_LOOKUP_MAX];
130 static smb_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 void smb_sa_to_va_mask_get(uint_t sa_mask, uint_t *va_maskp);
135 static callb_cpr_t *smb_lock_frlock_callback(flk_cb_when_t, void *);
136 static void smb_vop_catia_init();
137 
138 extern sysid_t lm_alloc_sysidt();
139 
140 #define	SMB_AT_MAX	16
141 static const uint_t smb_attrmap[SMB_AT_MAX] = {
142 	0,
143 	AT_TYPE,
144 	AT_MODE,
145 	AT_UID,
146 	AT_GID,
147 	AT_FSID,
148 	AT_NODEID,
149 	AT_NLINK,
150 	AT_SIZE,
151 	AT_ATIME,
152 	AT_MTIME,
153 	AT_CTIME,
154 	AT_RDEV,
155 	AT_BLKSIZE,
156 	AT_NBLOCKS,
157 	AT_SEQ
158 };
159 
160 static boolean_t	smb_vop_initialized = B_FALSE;
161 caller_context_t	smb_ct;
162 
163 /*
164  * smb_vop_init
165  *
166  * This function is not multi-thread safe. The caller must make sure only one
167  * thread makes the call.
168  */
169 int
170 smb_vop_init(void)
171 {
172 	if (smb_vop_initialized)
173 		return (0);
174 	/*
175 	 * The caller_context will be used primarily for range locking.
176 	 * Since the CIFS server is mapping its locks to POSIX locks,
177 	 * only one pid is used for operations originating from the
178 	 * CIFS server (to represent CIFS in the VOP_FRLOCK routines).
179 	 *
180 	 * XXX: Should smb_ct be per-zone?
181 	 */
182 	smb_ct.cc_sysid = lm_alloc_sysidt();
183 	if (smb_ct.cc_sysid == LM_NOSYSID)
184 		return (ENOMEM);
185 
186 	smb_ct.cc_caller_id = fs_new_caller_id();
187 	smb_ct.cc_pid = IGN_PID;
188 	smb_ct.cc_flags = 0;
189 	smb_vop_catia_init();
190 
191 	smb_vop_initialized = B_TRUE;
192 	return (0);
193 }
194 
195 /*
196  * smb_vop_fini
197  *
198  * This function is not multi-thread safe. The caller must make sure only one
199  * thread makes the call.
200  */
201 void
202 smb_vop_fini(void)
203 {
204 	if (!smb_vop_initialized)
205 		return;
206 
207 	lm_free_sysidt(smb_ct.cc_sysid);
208 	smb_ct.cc_pid = IGN_PID;
209 	smb_ct.cc_sysid = LM_NOSYSID;
210 	smb_vop_initialized = B_FALSE;
211 }
212 
213 /*
214  * The smb_ct will be used primarily for range locking.
215  * Since the CIFS server is mapping its locks to POSIX locks,
216  * only one pid is used for operations originating from the
217  * CIFS server (to represent CIFS in the VOP_FRLOCK routines).
218  */
219 int
220 smb_vop_open(vnode_t **vpp, int mode, cred_t *cred)
221 {
222 	return (VOP_OPEN(vpp, mode, cred, &smb_ct));
223 }
224 
225 void
226 smb_vop_close(vnode_t *vp, int mode, cred_t *cred)
227 {
228 	(void) VOP_CLOSE(vp, mode, 1, (offset_t)0, cred, &smb_ct);
229 }
230 
231 int
232 smb_vop_other_opens(vnode_t *vp, int mode)
233 {
234 	return (((mode & FWRITE) && vn_has_other_opens(vp, V_WRITE)) ||
235 	    (((mode & FWRITE) == 0) && vn_is_opened(vp, V_WRITE)) ||
236 	    ((mode & FREAD) && vn_has_other_opens(vp, V_READ)) ||
237 	    (((mode & FREAD) == 0) && vn_is_opened(vp, V_READ)) ||
238 	    vn_is_mapped(vp, V_RDORWR));
239 }
240 
241 /*
242  * The smb_vop_* functions have minimal knowledge of CIFS semantics and
243  * serve as an interface to the VFS layer.
244  *
245  * Only smb_fsop_* layer functions should call smb_vop_* layer functions.
246  * (Higher-level CIFS service code should never skip the smb_fsop_* layer
247  * to call smb_vop_* layer functions directly.)
248  */
249 
250 /*
251  * XXX - Extended attributes support in the file system assumed.
252  * This is needed for full NT Streams functionality.
253  */
254 
255 int
256 smb_vop_read(vnode_t *vp, uio_t *uiop, int ioflag, cred_t *cr)
257 {
258 	int error;
259 
260 	(void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, &smb_ct);
261 	error = VOP_READ(vp, uiop, ioflag, cr, &smb_ct);
262 	VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, &smb_ct);
263 	return (error);
264 }
265 
266 int
267 smb_vop_write(vnode_t *vp, uio_t *uiop, int ioflag, uint32_t *lcount,
268     cred_t *cr)
269 {
270 	int error;
271 
272 	*lcount = uiop->uio_resid;
273 
274 	uiop->uio_llimit = MAXOFFSET_T;
275 
276 	(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, &smb_ct);
277 	error = VOP_WRITE(vp, uiop, ioflag, cr, &smb_ct);
278 	VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, &smb_ct);
279 
280 	*lcount -= uiop->uio_resid;
281 
282 	return (error);
283 }
284 
285 int
286 smb_vop_ioctl(vnode_t *vp, int cmd, void *arg, cred_t *cr)
287 {
288 	int error, rval = 0;
289 	uint_t flags = 0;
290 
291 #ifdef	FKIOCTL
292 	flags |= FKIOCTL;
293 #endif
294 	error = VOP_IOCTL(vp, cmd, (intptr_t)arg, (int)flags, cr,
295 	    &rval, &smb_ct);
296 	if (error != 0)
297 		rval = error;
298 
299 	return (rval);
300 }
301 
302 /*
303  * smb_vop_getattr()
304  *
305  * smb_fsop_getattr()/smb_vop_getattr() should always be called from the CIFS
306  * service (instead of calling VOP_GETATTR directly) to retrieve attributes
307  * due to special processing needed for streams files.
308  *
309  * All attributes are retrieved.
310  *
311  * When vp denotes a named stream, then unnamed_vp should be passed in (denoting
312  * the corresponding unnamed stream).
313  * A named stream's attributes (as far as CIFS is concerned) are those of the
314  * unnamed stream (minus the size attribute, and the type), plus  the size of
315  * the named stream, and a type value of VREG.
316  * Although the file system may store other attributes with the named stream,
317  * these should not be used by CIFS for any purpose.
318  *
319  * File systems without VFSFT_XVATTR do not support DOS attributes or create
320  * time (crtime). In this case the mtime is used as the crtime.
321  * Likewise if VOP_GETATTR doesn't return any system attributes the dosattr
322  * is 0 and the mtime is used as the crtime.
323  */
324 int
325 smb_vop_getattr(vnode_t *vp, vnode_t *unnamed_vp, smb_attr_t *ret_attr,
326     int flags, cred_t *cr)
327 {
328 	int error;
329 	vnode_t *use_vp;
330 	xvattr_t tmp_xvattr;
331 	xoptattr_t *xoap = NULL;
332 
333 	if (unnamed_vp)
334 		use_vp = unnamed_vp;
335 	else
336 		use_vp = vp;
337 
338 	if (vfs_has_feature(use_vp->v_vfsp, VFSFT_XVATTR)) {
339 		xva_init(&tmp_xvattr);
340 		xoap = xva_getxoptattr(&tmp_xvattr);
341 		ASSERT(xoap);
342 
343 		smb_sa_to_va_mask_get(ret_attr->sa_mask,
344 		    &tmp_xvattr.xva_vattr.va_mask);
345 
346 		XVA_SET_REQ(&tmp_xvattr, XAT_READONLY);
347 		XVA_SET_REQ(&tmp_xvattr, XAT_HIDDEN);
348 		XVA_SET_REQ(&tmp_xvattr, XAT_SYSTEM);
349 		XVA_SET_REQ(&tmp_xvattr, XAT_ARCHIVE);
350 		XVA_SET_REQ(&tmp_xvattr, XAT_CREATETIME);
351 		XVA_SET_REQ(&tmp_xvattr, XAT_REPARSE);
352 		XVA_SET_REQ(&tmp_xvattr, XAT_OFFLINE);
353 		XVA_SET_REQ(&tmp_xvattr, XAT_SPARSE);
354 
355 		error = VOP_GETATTR(use_vp, &tmp_xvattr.xva_vattr, flags,
356 		    cr, &smb_ct);
357 		if (error != 0)
358 			return (error);
359 
360 		ret_attr->sa_vattr = tmp_xvattr.xva_vattr;
361 		ret_attr->sa_dosattr = 0;
362 
363 		if (tmp_xvattr.xva_vattr.va_mask & AT_XVATTR) {
364 			xoap = xva_getxoptattr(&tmp_xvattr);
365 			ASSERT(xoap);
366 
367 			if ((XVA_ISSET_RTN(&tmp_xvattr, XAT_READONLY)) &&
368 			    (xoap->xoa_readonly)) {
369 				ret_attr->sa_dosattr |= FILE_ATTRIBUTE_READONLY;
370 			}
371 
372 			if ((XVA_ISSET_RTN(&tmp_xvattr, XAT_HIDDEN)) &&
373 			    (xoap->xoa_hidden)) {
374 				ret_attr->sa_dosattr |= FILE_ATTRIBUTE_HIDDEN;
375 			}
376 
377 			if ((XVA_ISSET_RTN(&tmp_xvattr, XAT_SYSTEM)) &&
378 			    (xoap->xoa_system)) {
379 				ret_attr->sa_dosattr |= FILE_ATTRIBUTE_SYSTEM;
380 			}
381 
382 			if ((XVA_ISSET_RTN(&tmp_xvattr, XAT_ARCHIVE)) &&
383 			    (xoap->xoa_archive)) {
384 				ret_attr->sa_dosattr |= FILE_ATTRIBUTE_ARCHIVE;
385 			}
386 
387 			if ((XVA_ISSET_RTN(&tmp_xvattr, XAT_REPARSE)) &&
388 			    (xoap->xoa_reparse)) {
389 				ret_attr->sa_dosattr |=
390 				    FILE_ATTRIBUTE_REPARSE_POINT;
391 			}
392 
393 			if ((XVA_ISSET_RTN(&tmp_xvattr, XAT_OFFLINE)) &&
394 			    (xoap->xoa_offline)) {
395 				ret_attr->sa_dosattr |= FILE_ATTRIBUTE_OFFLINE;
396 			}
397 
398 			if ((XVA_ISSET_RTN(&tmp_xvattr, XAT_SPARSE)) &&
399 			    (xoap->xoa_sparse)) {
400 				ret_attr->sa_dosattr |=
401 				    FILE_ATTRIBUTE_SPARSE_FILE;
402 			}
403 
404 			ret_attr->sa_crtime = xoap->xoa_createtime;
405 		} else {
406 			ret_attr->sa_crtime = ret_attr->sa_vattr.va_mtime;
407 		}
408 	} else {
409 		/*
410 		 * Support for file systems without VFSFT_XVATTR
411 		 */
412 		smb_sa_to_va_mask_get(ret_attr->sa_mask,
413 		    &ret_attr->sa_vattr.va_mask);
414 
415 		error = VOP_GETATTR(use_vp, &ret_attr->sa_vattr,
416 		    flags, cr, &smb_ct);
417 		if (error != 0)
418 			return (error);
419 
420 		ret_attr->sa_dosattr = 0;
421 		ret_attr->sa_crtime = ret_attr->sa_vattr.va_mtime;
422 	}
423 
424 	if (unnamed_vp) {
425 		/*
426 		 * vp is a named stream under "unnamed_vp"
427 		 * Need to get the size from vp (not use_vp)
428 		 */
429 		smb_attr_t tmp_attr;
430 		ret_attr->sa_vattr.va_type = VREG;
431 
432 		if (ret_attr->sa_mask &
433 		    (SMB_AT_SIZE | SMB_AT_NBLOCKS | SMB_AT_ALLOCSZ)) {
434 			tmp_attr.sa_vattr.va_mask = AT_SIZE | AT_NBLOCKS;
435 
436 			error = VOP_GETATTR(vp, &tmp_attr.sa_vattr,
437 			    flags, cr, &smb_ct);
438 			if (error != 0)
439 				return (error);
440 
441 			ret_attr->sa_vattr.va_size = tmp_attr.sa_vattr.va_size;
442 			ret_attr->sa_vattr.va_nblocks =
443 			    tmp_attr.sa_vattr.va_nblocks;
444 		}
445 	}
446 
447 	/*
448 	 * Override a few things so they're as SMB expects.
449 	 * SMB allocsz is always zero for directories.
450 	 * For plain files, allocsz is the larger of:
451 	 * size, allocsize  (See smb_node_getattr)
452 	 */
453 	if (ret_attr->sa_vattr.va_type == VDIR) {
454 		ret_attr->sa_dosattr |= FILE_ATTRIBUTE_DIRECTORY;
455 		/* SMB expectes directories to have... */
456 		ret_attr->sa_vattr.va_nlink = 1;
457 		ret_attr->sa_vattr.va_size = 0;
458 		ret_attr->sa_allocsz = 0;
459 	} else {
460 		if (ret_attr->sa_dosattr == 0)
461 			ret_attr->sa_dosattr = FILE_ATTRIBUTE_NORMAL;
462 		if ((ret_attr->sa_mask & SMB_AT_ALLOCSZ) != 0) {
463 			/*
464 			 * ZFS includes meta-data in va_nblocks.
465 			 * Special case zero to keep tests happy.
466 			 */
467 			if (ret_attr->sa_vattr.va_size == 0 &&
468 			    ret_attr->sa_vattr.va_nblocks == 1)
469 				ret_attr->sa_allocsz = 0;
470 			else
471 				ret_attr->sa_allocsz =
472 				    ret_attr->sa_vattr.va_nblocks * DEV_BSIZE;
473 			if (ret_attr->sa_allocsz < ret_attr->sa_vattr.va_size) {
474 				ret_attr->sa_allocsz = P2ROUNDUP(
475 				    ret_attr->sa_vattr.va_size, DEV_BSIZE);
476 			}
477 		}
478 	}
479 
480 	return (error);
481 }
482 
483 /*
484  * smb_vop_setattr()
485  *
486  * smb_fsop_setattr()/smb_vop_setattr() should always be used instead of
487  * VOP_SETATTR() when calling from the CIFS service, due to special processing
488  * for streams files.
489  *
490  * Streams have a size but otherwise do not have separate attributes from
491  * the (unnamed stream) file, i.e., the security and ownership of the file
492  * applies to the stream.  In contrast, extended attribute files, which are
493  * used to implement streams, are independent objects with their own
494  * attributes.
495  *
496  * For compatibility with streams, we set the size on the extended attribute
497  * file and apply other attributes to the (unnamed stream) file.  The one
498  * exception is that the UID and GID can be set on the stream by passing a
499  * NULL unnamed_vp, which allows callers to synchronize stream ownership
500  * with the (unnamed stream) file.
501  */
502 int
503 smb_vop_setattr(vnode_t *vp, vnode_t *unnamed_vp, smb_attr_t *attr,
504     int flags, cred_t *cr)
505 {
506 	int error = 0;
507 	int at_size = 0;
508 	vnode_t *use_vp;
509 	xvattr_t xvattr;
510 	vattr_t *vap;
511 
512 	if (attr->sa_mask & SMB_AT_DOSATTR) {
513 		attr->sa_dosattr &=
514 		    (FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_READONLY |
515 		    FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM |
516 		    FILE_ATTRIBUTE_OFFLINE | FILE_ATTRIBUTE_SPARSE_FILE);
517 	}
518 
519 	if (unnamed_vp) {
520 		use_vp = unnamed_vp;
521 		if (attr->sa_mask & SMB_AT_SIZE) {
522 			at_size = 1;
523 			attr->sa_mask &= ~SMB_AT_SIZE;
524 		}
525 	} else {
526 		use_vp = vp;
527 	}
528 
529 	/*
530 	 * The caller should not be setting sa_vattr.va_mask,
531 	 * but rather sa_mask.
532 	 */
533 
534 	attr->sa_vattr.va_mask = 0;
535 
536 	if (vfs_has_feature(use_vp->v_vfsp, VFSFT_XVATTR)) {
537 		smb_vop_setup_xvattr(attr, &xvattr);
538 		vap = &xvattr.xva_vattr;
539 	} else {
540 		smb_sa_to_va_mask(attr->sa_mask,
541 		    &attr->sa_vattr.va_mask);
542 		vap = &attr->sa_vattr;
543 	}
544 
545 	if ((error = VOP_SETATTR(use_vp, vap, flags, cr, &smb_ct)) != 0)
546 		return (error);
547 
548 	if (at_size) {
549 		attr->sa_vattr.va_mask = AT_SIZE;
550 		error = VOP_SETATTR(vp, &attr->sa_vattr, flags,
551 		    zone_kcred(), &smb_ct);
552 	}
553 
554 	return (error);
555 }
556 
557 int
558 smb_vop_space(vnode_t *vp, int cmd, flock64_t *bfp, int flags,
559     offset_t offset, cred_t *cr)
560 {
561 	int error;
562 
563 	error = VOP_SPACE(vp, cmd, bfp, flags, offset, cr, &smb_ct);
564 
565 	return (error);
566 }
567 
568 /*
569  * smb_vop_access
570  *
571  * This is a wrapper round VOP_ACCESS. VOP_ACCESS checks the given mode
572  * against file's ACL or Unix permissions. CIFS on the other hand needs to
573  * know if the requested operation can succeed for the given object, this
574  * requires more checks in case of DELETE bit since permissions on the parent
575  * directory are important as well. Based on Windows rules if parent's ACL
576  * grant FILE_DELETE_CHILD a file can be delete regardless of the file's
577  * permissions.
578  */
579 int
580 smb_vop_access(vnode_t *vp, int mode, int flags, vnode_t *dir_vp, cred_t *cr)
581 {
582 	int error = 0;
583 
584 	if (mode == 0)
585 		return (0);
586 
587 	error = VOP_ACCESS(vp, mode, flags, cr, NULL);
588 
589 	if (error == 0)
590 		return (0);
591 
592 	if ((mode & (ACE_DELETE|ACE_READ_ATTRIBUTES)) == 0 ||
593 	    flags != V_ACE_MASK || dir_vp == NULL)
594 		return (error);
595 
596 	if ((mode & ACE_DELETE) != 0) {
597 		error = VOP_ACCESS(dir_vp, ACE_DELETE_CHILD, flags,
598 		    cr, NULL);
599 
600 		if (error == 0)
601 			mode &= ~ACE_DELETE;
602 	}
603 	if ((mode & ACE_READ_ATTRIBUTES) != 0) {
604 		error = VOP_ACCESS(dir_vp, ACE_LIST_DIRECTORY, flags,
605 		    cr, NULL);
606 
607 		if (error == 0)
608 			mode &= ~ACE_READ_ATTRIBUTES;
609 	}
610 
611 	if (mode != 0)
612 		error = VOP_ACCESS(vp, mode, flags, cr, NULL);
613 
614 	return (error);
615 }
616 
617 /*
618  * smb_vop_lookup
619  *
620  * dvp:		directory vnode (in)
621  * name:	name of file to be looked up (in)
622  * vpp:		looked-up vnode (out)
623  * od_name:	on-disk name of file (out).
624  *		This parameter is optional.  If a pointer is passed in, it
625  *		must be allocated with MAXNAMELEN bytes
626  * rootvp:	vnode of the tree root (in)
627  *		This parameter is always passed in non-NULL except at the time
628  *		of share set up.
629  * direntflags:	dirent flags returned from VOP_LOOKUP
630  */
631 int
632 smb_vop_lookup(
633     vnode_t		*dvp,
634     char		*name,
635     vnode_t		**vpp,
636     char		*od_name,
637     int			flags,
638     int			*direntflags,
639     vnode_t		*rootvp,
640     smb_attr_t		*attr,
641     cred_t		*cr)
642 {
643 	int error = 0;
644 	int option_flags = 0;
645 	pathname_t rpn;
646 	char *np = name;
647 	char namebuf[MAXNAMELEN];
648 
649 	if (*name == '\0') {
650 		/*
651 		 * This happens creating named streams at the share root.
652 		 */
653 		VN_HOLD(dvp);
654 		*vpp = dvp;
655 		return (0);
656 	}
657 
658 	ASSERT(vpp);
659 	*vpp = NULL;
660 	*direntflags = 0;
661 
662 	if ((name[0] == '.') && (name[1] == '.') && (name[2] == 0)) {
663 		if (rootvp && (dvp == rootvp)) {
664 			VN_HOLD(dvp);
665 			*vpp = dvp;
666 			return (0);
667 		}
668 
669 		if (dvp->v_flag & VROOT) {
670 			vfs_t *vfsp;
671 			vnode_t *cvp = dvp;
672 
673 			/*
674 			 * Set dvp and check for races with forced unmount
675 			 * (see lookuppnvp())
676 			 */
677 
678 			vfsp = cvp->v_vfsp;
679 			vfs_rlock_wait(vfsp);
680 			if (((dvp = cvp->v_vfsp->vfs_vnodecovered) == NULL) ||
681 			    (cvp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) {
682 				vfs_unlock(vfsp);
683 				return (EIO);
684 			}
685 			vfs_unlock(vfsp);
686 		}
687 	}
688 
689 	if (flags & SMB_IGNORE_CASE)
690 		option_flags = FIGNORECASE;
691 
692 	if (flags & SMB_CATIA)
693 		np = smb_vop_catia_v5tov4(name, namebuf, sizeof (namebuf));
694 
695 #ifdef _KERNEL
696 	/*
697 	 * The SMB server enables BYPASS_TRAVERSE_CHECKING by default.
698 	 * This grants PRIV_FILE_DAC_SEARCH to all users.
699 	 * If the user has this privilege, we'll always succeed ACE_EXECUTE
700 	 * checks on directories, so skip the (potentially expensive)
701 	 * ACL check.
702 	 */
703 	if (PRIV_POLICY_ONLY(cr, PRIV_FILE_DAC_SEARCH, B_FALSE))
704 		option_flags |= ATTR_NOACLCHECK;
705 #endif
706 	pn_alloc(&rpn);
707 
708 	/*
709 	 * Easier to not have junk in rpn, as not every FS type
710 	 * will necessarily fill that in for us.
711 	 */
712 	bzero(rpn.pn_buf, rpn.pn_bufsize);
713 
714 	error = VOP_LOOKUP(dvp, np, vpp, NULL, option_flags, NULL, cr,
715 	    &smb_ct, direntflags, &rpn);
716 
717 	if (error == 0) {
718 		if (od_name) {
719 			bzero(od_name, MAXNAMELEN);
720 			if ((option_flags & FIGNORECASE) != 0 &&
721 			    rpn.pn_buf[0] != '\0')
722 				np = rpn.pn_buf;
723 			else
724 				np = name;
725 			if (flags & SMB_CATIA)
726 				smb_vop_catia_v4tov5(np, od_name, MAXNAMELEN);
727 			else
728 				(void) strlcpy(od_name, np, MAXNAMELEN);
729 		}
730 
731 		if (attr != NULL) {
732 			attr->sa_mask = SMB_AT_ALL;
733 			(void) smb_vop_getattr(*vpp, NULL, attr, 0,
734 			    zone_kcred());
735 		}
736 	}
737 
738 	pn_free(&rpn);
739 	return (error);
740 }
741 
742 int
743 smb_vop_create(vnode_t *dvp, char *name, smb_attr_t *attr, vnode_t **vpp,
744     int flags, cred_t *cr, vsecattr_t *vsap)
745 {
746 	int error;
747 	int option_flags = 0;
748 	xvattr_t xvattr;
749 	vattr_t *vap;
750 	char *np = name;
751 	char namebuf[MAXNAMELEN];
752 
753 	if (flags & SMB_IGNORE_CASE)
754 		option_flags = FIGNORECASE;
755 
756 	attr->sa_vattr.va_mask = 0;
757 
758 	if (vfs_has_feature(dvp->v_vfsp, VFSFT_XVATTR)) {
759 		smb_vop_setup_xvattr(attr, &xvattr);
760 		vap = &xvattr.xva_vattr;
761 	} else {
762 		smb_sa_to_va_mask(attr->sa_mask, &attr->sa_vattr.va_mask);
763 		vap = &attr->sa_vattr;
764 	}
765 
766 	if (flags & SMB_CATIA) {
767 		np = smb_vop_catia_v5tov4(name, namebuf, sizeof (namebuf));
768 		if (strchr(np, '/') != NULL)
769 			return (EILSEQ);
770 	}
771 
772 	error = VOP_CREATE(dvp, np, vap, EXCL, attr->sa_vattr.va_mode,
773 	    vpp, cr, option_flags, &smb_ct, vsap);
774 
775 	/*
776 	 * One could argue that filesystems should obey the size
777 	 * if specified in the create attributes.  Unfortunately,
778 	 * they only appear to let you truncate the size to zero.
779 	 * SMB needs to set a non-zero size, so work-around.
780 	 */
781 	if (error == 0 && *vpp != NULL &&
782 	    (vap->va_mask & AT_SIZE) != 0 &&
783 	    vap->va_size > 0) {
784 		vattr_t ta = *vap;
785 		ta.va_mask = AT_SIZE;
786 		(void) VOP_SETATTR(*vpp, &ta, 0, cr, &smb_ct);
787 	}
788 
789 	return (error);
790 }
791 
792 int
793 smb_vop_remove(vnode_t *dvp, char *name, int flags, cred_t *cr)
794 {
795 	int error;
796 	int option_flags = 0;
797 	char *np = name;
798 	char namebuf[MAXNAMELEN];
799 
800 	if (flags & SMB_IGNORE_CASE)
801 		option_flags = FIGNORECASE;
802 
803 	if (flags & SMB_CATIA)
804 		np = smb_vop_catia_v5tov4(name, namebuf, sizeof (namebuf));
805 
806 	error = VOP_REMOVE(dvp, np, cr, &smb_ct, option_flags);
807 
808 	return (error);
809 }
810 
811 /*
812  * smb_vop_link(target-dir-vp, source-file-vp, target-name)
813  *
814  * Create a link - same tree (identical TID) only.
815  */
816 int
817 smb_vop_link(vnode_t *to_dvp, vnode_t *from_vp, char *to_name,
818     int flags, cred_t *cr)
819 {
820 	int option_flags = 0;
821 	char *np, *buf;
822 	int rc;
823 
824 	if (flags & SMB_IGNORE_CASE)
825 		option_flags = FIGNORECASE;
826 
827 	if (flags & SMB_CATIA) {
828 		buf = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
829 		np = smb_vop_catia_v5tov4(to_name, buf, MAXNAMELEN);
830 		if (strchr(np, '/') != NULL) {
831 			kmem_free(buf, MAXNAMELEN);
832 			return (EILSEQ);
833 		}
834 
835 		rc = VOP_LINK(to_dvp, from_vp, np, cr, &smb_ct, option_flags);
836 		kmem_free(buf, MAXNAMELEN);
837 		return (rc);
838 	}
839 
840 	rc = VOP_LINK(to_dvp, from_vp, to_name, cr, &smb_ct, option_flags);
841 	return (rc);
842 }
843 
844 /*
845  * smb_vop_rename()
846  *
847  * The rename is for files in the same tree (identical TID) only.
848  */
849 int
850 smb_vop_rename(vnode_t *from_dvp, char *from_name, vnode_t *to_dvp,
851     char *to_name, int flags, cred_t *cr)
852 {
853 	int error;
854 	int option_flags = 0;
855 	char *from, *to, *fbuf, *tbuf;
856 
857 	if (flags & SMB_IGNORE_CASE)
858 		option_flags = FIGNORECASE;
859 
860 	if (flags & SMB_CATIA) {
861 		tbuf = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
862 		to = smb_vop_catia_v5tov4(to_name, tbuf, MAXNAMELEN);
863 		if (strchr(to, '/') != NULL) {
864 			kmem_free(tbuf, MAXNAMELEN);
865 			return (EILSEQ);
866 		}
867 
868 		fbuf = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
869 		from = smb_vop_catia_v5tov4(from_name, fbuf, MAXNAMELEN);
870 
871 		error = VOP_RENAME(from_dvp, from, to_dvp, to, cr,
872 		    &smb_ct, option_flags);
873 
874 		kmem_free(tbuf, MAXNAMELEN);
875 		kmem_free(fbuf, MAXNAMELEN);
876 		return (error);
877 	}
878 
879 	error = VOP_RENAME(from_dvp, from_name, to_dvp, to_name, cr,
880 	    &smb_ct, option_flags);
881 
882 	return (error);
883 }
884 
885 int
886 smb_vop_mkdir(vnode_t *dvp, char *name, smb_attr_t *attr, vnode_t **vpp,
887     int flags, cred_t *cr, vsecattr_t *vsap)
888 {
889 	int error;
890 	int option_flags = 0;
891 	xvattr_t xvattr;
892 	vattr_t *vap;
893 	char *np = name;
894 	char namebuf[MAXNAMELEN];
895 
896 	if (flags & SMB_IGNORE_CASE)
897 		option_flags = FIGNORECASE;
898 
899 	attr->sa_vattr.va_mask = 0;
900 
901 	if (vfs_has_feature(dvp->v_vfsp, VFSFT_XVATTR)) {
902 		smb_vop_setup_xvattr(attr, &xvattr);
903 		vap = &xvattr.xva_vattr;
904 	} else {
905 		smb_sa_to_va_mask(attr->sa_mask, &attr->sa_vattr.va_mask);
906 		vap = &attr->sa_vattr;
907 	}
908 
909 	if (flags & SMB_CATIA) {
910 		np = smb_vop_catia_v5tov4(name, namebuf, sizeof (namebuf));
911 		if (strchr(np, '/') != NULL)
912 			return (EILSEQ);
913 	}
914 
915 	error = VOP_MKDIR(dvp, np, vap, vpp, cr, &smb_ct, option_flags, vsap);
916 
917 	return (error);
918 }
919 
920 /*
921  * smb_vop_rmdir()
922  *
923  * Only simple rmdir supported, consistent with NT semantics
924  * (can only remove an empty directory).
925  *
926  * The third argument to VOP_RMDIR  is the current directory of
927  * the process.  It allows rmdir wants to EINVAL if one tries to
928  * remove ".".  Since SMB servers do not know what their clients'
929  * current directories are, we fake it by supplying a vnode known
930  * to exist and illegal to remove (rootdir).
931  */
932 int
933 smb_vop_rmdir(vnode_t *dvp, char *name, int flags, cred_t *cr)
934 {
935 	int error;
936 	int option_flags = 0;
937 	char *np = name;
938 	char namebuf[MAXNAMELEN];
939 
940 	if (flags & SMB_IGNORE_CASE)
941 		option_flags = FIGNORECASE;
942 
943 	if (flags & SMB_CATIA)
944 		np = smb_vop_catia_v5tov4(name, namebuf, sizeof (namebuf));
945 
946 	error = VOP_RMDIR(dvp, np, rootdir, cr, &smb_ct, option_flags);
947 	return (error);
948 }
949 
950 int
951 smb_vop_commit(vnode_t *vp, cred_t *cr)
952 {
953 	return (VOP_FSYNC(vp, 1, cr, &smb_ct));
954 }
955 
956 /*
957  * Some code in smb_node.c needs to know which DOS attributes
958  * we can actually store.  Let's define a mask here of all the
959  * DOS attribute flags supported by the following function.
960  */
961 const uint32_t
962 smb_vop_dosattr_settable =
963 	FILE_ATTRIBUTE_ARCHIVE |
964 	FILE_ATTRIBUTE_SYSTEM |
965 	FILE_ATTRIBUTE_HIDDEN |
966 	FILE_ATTRIBUTE_READONLY |
967 	FILE_ATTRIBUTE_OFFLINE |
968 	FILE_ATTRIBUTE_SPARSE_FILE;
969 
970 static void
971 smb_vop_setup_xvattr(smb_attr_t *smb_attr, xvattr_t *xvattr)
972 {
973 	xoptattr_t *xoap = NULL;
974 	uint_t xva_mask;
975 
976 	/*
977 	 * Initialize xvattr, including bzero
978 	 */
979 	xva_init(xvattr);
980 	xoap = xva_getxoptattr(xvattr);
981 
982 	ASSERT(xoap);
983 
984 	/*
985 	 * Copy caller-specified classic attributes to xvattr.
986 	 * First save xvattr's mask (set in xva_init()), which
987 	 * contains AT_XVATTR.  This is |'d in later if needed.
988 	 */
989 
990 	xva_mask = xvattr->xva_vattr.va_mask;
991 	xvattr->xva_vattr = smb_attr->sa_vattr;
992 
993 	smb_sa_to_va_mask(smb_attr->sa_mask, &xvattr->xva_vattr.va_mask);
994 
995 	/*
996 	 * Do not set ctime (only the file system can do it)
997 	 */
998 
999 	xvattr->xva_vattr.va_mask &= ~AT_CTIME;
1000 
1001 	if (smb_attr->sa_mask & SMB_AT_DOSATTR) {
1002 
1003 		/*
1004 		 * "|" in the original xva_mask, which contains
1005 		 * AT_XVATTR
1006 		 */
1007 
1008 		xvattr->xva_vattr.va_mask |= xva_mask;
1009 
1010 		XVA_SET_REQ(xvattr, XAT_ARCHIVE);
1011 		XVA_SET_REQ(xvattr, XAT_SYSTEM);
1012 		XVA_SET_REQ(xvattr, XAT_READONLY);
1013 		XVA_SET_REQ(xvattr, XAT_HIDDEN);
1014 		XVA_SET_REQ(xvattr, XAT_OFFLINE);
1015 		XVA_SET_REQ(xvattr, XAT_SPARSE);
1016 
1017 		/*
1018 		 * smb_attr->sa_dosattr: If a given bit is not set,
1019 		 * that indicates that the corresponding field needs
1020 		 * to be updated with a "0" value.  This is done
1021 		 * implicitly as the xoap->xoa_* fields were bzero'd.
1022 		 */
1023 
1024 		if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_ARCHIVE)
1025 			xoap->xoa_archive = 1;
1026 
1027 		if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_SYSTEM)
1028 			xoap->xoa_system = 1;
1029 
1030 		if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_READONLY)
1031 			xoap->xoa_readonly = 1;
1032 
1033 		if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_HIDDEN)
1034 			xoap->xoa_hidden = 1;
1035 
1036 		if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_OFFLINE)
1037 			xoap->xoa_offline = 1;
1038 
1039 		if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_SPARSE_FILE)
1040 			xoap->xoa_sparse = 1;
1041 	}
1042 
1043 	if (smb_attr->sa_mask & SMB_AT_CRTIME) {
1044 		/*
1045 		 * "|" in the original xva_mask, which contains
1046 		 * AT_XVATTR
1047 		 */
1048 
1049 		xvattr->xva_vattr.va_mask |= xva_mask;
1050 		XVA_SET_REQ(xvattr, XAT_CREATETIME);
1051 		xoap->xoa_createtime = smb_attr->sa_crtime;
1052 	}
1053 }
1054 
1055 /*
1056  * smb_vop_readdir()
1057  *
1058  * Collects an SMB_MINLEN_RDDIR_BUF "page" of directory entries.
1059  * The directory entries are returned in an fs-independent format by the
1060  * underlying file system.  That is, the "page" of information returned is
1061  * not literally stored on-disk in the format returned.
1062  * If the file system supports extended directory entries (has features
1063  * VFSFT_DIRENTFLAGS), set V_RDDIR_ENTFLAGS to cause the buffer to be
1064  * filled with edirent_t structures, instead of dirent64_t structures.
1065  * If the file system supports access based enumeration (abe), set
1066  * V_RDDIR_ACCFILTER to filter directory entries based on user cred.
1067  */
1068 int
1069 smb_vop_readdir(vnode_t *vp, uint32_t offset,
1070     void *buf, int *count, int *eof, uint32_t rddir_flag, cred_t *cr)
1071 {
1072 	int error = 0;
1073 	int flags = 0;
1074 	int rdirent_size;
1075 	struct uio auio;
1076 	struct iovec aiov;
1077 
1078 	if (vp->v_type != VDIR)
1079 		return (ENOTDIR);
1080 
1081 	if ((rddir_flag & SMB_EDIRENT) != 0 &&
1082 	    vfs_has_feature(vp->v_vfsp, VFSFT_DIRENTFLAGS)) {
1083 		flags |= V_RDDIR_ENTFLAGS;
1084 		rdirent_size = sizeof (edirent_t);
1085 	} else {
1086 		rdirent_size = sizeof (dirent64_t);
1087 	}
1088 
1089 	if (*count < rdirent_size)
1090 		return (EINVAL);
1091 
1092 	if (rddir_flag & SMB_ABE)
1093 		flags |= V_RDDIR_ACCFILTER;
1094 
1095 	aiov.iov_base = buf;
1096 	aiov.iov_len = *count;
1097 	auio.uio_iov = &aiov;
1098 	auio.uio_iovcnt = 1;
1099 	auio.uio_loffset = (uint64_t)offset;
1100 	auio.uio_segflg = UIO_SYSSPACE;
1101 	auio.uio_extflg = UIO_COPY_DEFAULT;
1102 	auio.uio_resid = *count;
1103 	auio.uio_fmode = 0;
1104 
1105 	(void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, &smb_ct);
1106 	error = VOP_READDIR(vp, &auio, cr, eof, &smb_ct, flags);
1107 	VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, &smb_ct);
1108 
1109 	if (error == 0)
1110 		*count = *count - auio.uio_resid;
1111 
1112 	return (error);
1113 }
1114 
1115 /*
1116  * smb_sa_to_va_mask
1117  *
1118  * Set va_mask by running through the SMB_AT_* #define's and
1119  * setting those bits that correspond to the SMB_AT_* bits
1120  * set in sa_mask.
1121  */
1122 void
1123 smb_sa_to_va_mask(uint_t sa_mask, uint_t *va_maskp)
1124 {
1125 	int i;
1126 	uint_t smask;
1127 
1128 	smask = (sa_mask);
1129 	for (i = SMB_AT_TYPE; (i < SMB_AT_MAX) && (smask != 0); ++i) {
1130 		if (smask & 1)
1131 			*(va_maskp) |= smb_attrmap[i];
1132 
1133 		smask >>= 1;
1134 	}
1135 }
1136 
1137 /*
1138  * Variant of smb_sa_to_va_mask for vop_getattr,
1139  * adding some bits for SMB_AT_ALLOCSZ etc.
1140  */
1141 void
1142 smb_sa_to_va_mask_get(uint_t sa_mask, uint_t *va_maskp)
1143 {
1144 	smb_sa_to_va_mask(sa_mask, va_maskp);
1145 
1146 	*va_maskp |= AT_TYPE;
1147 	if ((sa_mask & SMB_AT_ALLOCSZ) != 0) {
1148 		*va_maskp |= (AT_SIZE | AT_NBLOCKS);
1149 	}
1150 }
1151 
1152 /*
1153  * smb_vop_stream_lookup()
1154  *
1155  * The name returned in od_name is the on-disk name of the stream with the
1156  * SMB_STREAM_PREFIX stripped off.  od_name should be allocated to MAXNAMELEN
1157  * by the caller.
1158  */
1159 int
1160 smb_vop_stream_lookup(
1161     vnode_t		*fvp,
1162     char		*stream_name,
1163     vnode_t		**vpp,
1164     char		*od_name,
1165     vnode_t		**xattrdirvpp,
1166     int			flags,
1167     vnode_t		*rootvp,
1168     cred_t		*cr)
1169 {
1170 	char *solaris_stream_name;
1171 	char *name;
1172 	int error, tmpflgs;
1173 
1174 	if ((error = smb_vop_lookup_xattrdir(fvp, xattrdirvpp,
1175 	    LOOKUP_XATTR | CREATE_XATTR_DIR, cr)) != 0)
1176 		return (error);
1177 
1178 	/*
1179 	 * Prepend SMB_STREAM_PREFIX to stream name
1180 	 */
1181 
1182 	solaris_stream_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1183 	(void) snprintf(solaris_stream_name, MAXNAMELEN,
1184 	    "%s%s", SMB_STREAM_PREFIX, stream_name);
1185 
1186 	/*
1187 	 * "name" will hold the on-disk name returned from smb_vop_lookup
1188 	 * for the stream, including the SMB_STREAM_PREFIX.
1189 	 */
1190 
1191 	name = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
1192 
1193 	if ((error = smb_vop_lookup(*xattrdirvpp, solaris_stream_name, vpp,
1194 	    name, flags, &tmpflgs, rootvp, NULL, cr)) != 0) {
1195 		VN_RELE(*xattrdirvpp);
1196 	} else {
1197 		(void) strlcpy(od_name, &(name[SMB_STREAM_PREFIX_LEN]),
1198 		    MAXNAMELEN);
1199 	}
1200 
1201 	kmem_free(solaris_stream_name, MAXNAMELEN);
1202 	kmem_free(name, MAXNAMELEN);
1203 
1204 	return (error);
1205 }
1206 
1207 int
1208 smb_vop_stream_create(vnode_t *fvp, char *stream_name, smb_attr_t *attr,
1209     vnode_t **vpp, vnode_t **xattrdirvpp, int flags, cred_t *cr)
1210 {
1211 	char *solaris_stream_name;
1212 	int error;
1213 
1214 	if ((error = smb_vop_lookup_xattrdir(fvp, xattrdirvpp,
1215 	    LOOKUP_XATTR | CREATE_XATTR_DIR, cr)) != 0)
1216 		return (error);
1217 
1218 	/*
1219 	 * Prepend SMB_STREAM_PREFIX to stream name
1220 	 */
1221 
1222 	solaris_stream_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1223 	(void) snprintf(solaris_stream_name, MAXNAMELEN,
1224 	    "%s%s", SMB_STREAM_PREFIX, stream_name);
1225 
1226 	if ((error = smb_vop_create(*xattrdirvpp, solaris_stream_name, attr,
1227 	    vpp, flags, cr, NULL)) != 0)
1228 		VN_RELE(*xattrdirvpp);
1229 
1230 	kmem_free(solaris_stream_name, MAXNAMELEN);
1231 
1232 	return (error);
1233 }
1234 
1235 int
1236 smb_vop_stream_remove(vnode_t *vp, char *stream_name, int flags, cred_t *cr)
1237 {
1238 	char *solaris_stream_name;
1239 	vnode_t *xattrdirvp;
1240 	int error;
1241 
1242 	error = smb_vop_lookup_xattrdir(vp, &xattrdirvp, LOOKUP_XATTR, cr);
1243 	if (error != 0)
1244 		return (error);
1245 
1246 	/*
1247 	 * Prepend SMB_STREAM_PREFIX to stream name
1248 	 */
1249 
1250 	solaris_stream_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1251 	(void) snprintf(solaris_stream_name, MAXNAMELEN,
1252 	    "%s%s", SMB_STREAM_PREFIX, stream_name);
1253 
1254 	/* XXX might have to use kcred */
1255 	error = smb_vop_remove(xattrdirvp, solaris_stream_name, flags, cr);
1256 
1257 	kmem_free(solaris_stream_name, MAXNAMELEN);
1258 	VN_RELE(xattrdirvp);
1259 
1260 	return (error);
1261 }
1262 
1263 int
1264 smb_vop_lookup_xattrdir(vnode_t *fvp, vnode_t **xattrdirvpp, int flags,
1265     cred_t *cr)
1266 {
1267 	int error;
1268 
1269 	error = VOP_LOOKUP(fvp, "", xattrdirvpp, NULL, flags, NULL, cr,
1270 	    &smb_ct, NULL, NULL);
1271 	return (error);
1272 }
1273 
1274 /*
1275  * smb_vop_traverse_check()
1276  *
1277  * This function checks to see if the passed-in vnode has a file system
1278  * mounted on it.  If it does, the mount point is "traversed" and the
1279  * vnode for the root of the file system is returned.
1280  */
1281 int
1282 smb_vop_traverse_check(vnode_t **vpp)
1283 {
1284 	int error;
1285 
1286 	if (vn_mountedvfs(*vpp) == 0)
1287 		return (0);
1288 
1289 	/*
1290 	 * traverse() may return a different held vnode, even in the error case.
1291 	 * If it returns a different vnode, it will have released the original.
1292 	 */
1293 
1294 	error = traverse(vpp);
1295 
1296 	return (error);
1297 }
1298 
1299 int /*ARGSUSED*/
1300 smb_vop_statfs(vnode_t *vp, struct statvfs64 *statp, cred_t *cr)
1301 {
1302 	int error;
1303 
1304 	error = VFS_STATVFS(vp->v_vfsp, statp);
1305 
1306 	return (error);
1307 }
1308 
1309 /*
1310  * smb_vop_acl_read
1311  *
1312  * Reads the ACL of the specified file into 'aclp'.
1313  * acl_type is the type of ACL which the filesystem supports.
1314  *
1315  * Caller has to free the allocated memory for aclp by calling
1316  * acl_free().
1317  */
1318 int
1319 smb_vop_acl_read(vnode_t *vp, acl_t **aclp, int flags, acl_type_t acl_type,
1320     cred_t *cr)
1321 {
1322 	int error;
1323 	vsecattr_t vsecattr;
1324 
1325 	ASSERT(vp);
1326 	ASSERT(aclp);
1327 
1328 	*aclp = NULL;
1329 	bzero(&vsecattr, sizeof (vsecattr_t));
1330 
1331 	switch (acl_type) {
1332 	case ACLENT_T:
1333 		vsecattr.vsa_mask = VSA_ACL | VSA_ACLCNT | VSA_DFACL |
1334 		    VSA_DFACLCNT;
1335 		break;
1336 
1337 	case ACE_T:
1338 		vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT | VSA_ACE_ACLFLAGS;
1339 		break;
1340 
1341 	default:
1342 		return (EINVAL);
1343 	}
1344 
1345 	if ((error = VOP_GETSECATTR(vp, &vsecattr, flags, cr, &smb_ct)) != 0)
1346 		return (error);
1347 
1348 	*aclp = smb_fsacl_from_vsa(&vsecattr, acl_type);
1349 	if (vp->v_type == VDIR)
1350 		(*aclp)->acl_flags |= ACL_IS_DIR;
1351 
1352 	return (0);
1353 }
1354 
1355 /*
1356  * smb_vop_acl_write
1357  *
1358  * Writes the given ACL in aclp for the specified file.
1359  */
1360 int
1361 smb_vop_acl_write(vnode_t *vp, acl_t *aclp, int flags, cred_t *cr)
1362 {
1363 	int error;
1364 	vsecattr_t vsecattr;
1365 	int aclbsize;
1366 
1367 	ASSERT(vp);
1368 	ASSERT(aclp);
1369 
1370 	error = smb_fsacl_to_vsa(aclp, &vsecattr, &aclbsize);
1371 
1372 	if (error == 0) {
1373 		(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, &smb_ct);
1374 		error = VOP_SETSECATTR(vp, &vsecattr, flags, cr, &smb_ct);
1375 		VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, &smb_ct);
1376 	}
1377 
1378 	if (aclbsize && vsecattr.vsa_aclentp)
1379 		kmem_free(vsecattr.vsa_aclentp, aclbsize);
1380 
1381 	return (error);
1382 }
1383 
1384 /*
1385  * smb_vop_acl_type
1386  *
1387  * Determines the ACL type for the given vnode.
1388  * ACLENT_T is a Posix ACL and ACE_T is a ZFS ACL.
1389  */
1390 acl_type_t
1391 smb_vop_acl_type(vnode_t *vp)
1392 {
1393 	int error;
1394 	ulong_t whichacl;
1395 
1396 	error = VOP_PATHCONF(vp, _PC_ACL_ENABLED, &whichacl,
1397 	    zone_kcred(), NULL);
1398 	if (error != 0) {
1399 		/*
1400 		 * If we got an error, then the filesystem
1401 		 * likely does not understand the _PC_ACL_ENABLED
1402 		 * pathconf.  In this case, we fall back to trying
1403 		 * POSIX-draft (aka UFS-style) ACLs.
1404 		 */
1405 		whichacl = _ACL_ACLENT_ENABLED;
1406 	}
1407 
1408 	if (!(whichacl & (_ACL_ACE_ENABLED | _ACL_ACLENT_ENABLED))) {
1409 		/*
1410 		 * If the file system supports neither ACE nor
1411 		 * ACLENT ACLs we will fall back to UFS-style ACLs
1412 		 * like we did above if there was an error upon
1413 		 * calling VOP_PATHCONF.
1414 		 *
1415 		 * ACE and ACLENT type ACLs are the only interfaces
1416 		 * supported thus far.  If any other bits are set on
1417 		 * 'whichacl' upon return from VOP_PATHCONF, we will
1418 		 * ignore them.
1419 		 */
1420 		whichacl = _ACL_ACLENT_ENABLED;
1421 	}
1422 
1423 	if (whichacl == _ACL_ACLENT_ENABLED)
1424 		return (ACLENT_T);
1425 
1426 	return (ACE_T);
1427 }
1428 
1429 static const int zfs_perms[] = {
1430 	ACE_READ_DATA, ACE_WRITE_DATA, ACE_APPEND_DATA, ACE_READ_NAMED_ATTRS,
1431 	ACE_WRITE_NAMED_ATTRS, ACE_EXECUTE, ACE_DELETE_CHILD,
1432 	ACE_READ_ATTRIBUTES, ACE_WRITE_ATTRIBUTES, ACE_DELETE, ACE_READ_ACL,
1433 	ACE_WRITE_ACL, ACE_WRITE_OWNER, ACE_SYNCHRONIZE
1434 };
1435 
1436 static const int unix_perms[] = { VREAD, VWRITE, VEXEC };
1437 /*
1438  * smb_vop_eaccess
1439  *
1440  * Returns the effective permission of the given credential for the
1441  * specified object.
1442  *
1443  * This is just a workaround. We need VFS/FS support for this.
1444  */
1445 void
1446 smb_vop_eaccess(vnode_t *vp, int *mode, int flags, vnode_t *dir_vp, cred_t *cr)
1447 {
1448 	int error, i;
1449 	int pnum;
1450 
1451 	*mode = 0;
1452 
1453 	if (flags == V_ACE_MASK) {
1454 		pnum = sizeof (zfs_perms) / sizeof (int);
1455 
1456 		for (i = 0; i < pnum; i++) {
1457 			error = smb_vop_access(vp, zfs_perms[i], flags,
1458 			    dir_vp, cr);
1459 			if (error == 0)
1460 				*mode |= zfs_perms[i];
1461 		}
1462 	} else {
1463 		pnum = sizeof (unix_perms) / sizeof (int);
1464 
1465 		for (i = 0; i < pnum; i++) {
1466 			error = smb_vop_access(vp, unix_perms[i], flags,
1467 			    dir_vp, cr);
1468 			if (error == 0)
1469 				*mode |= unix_perms[i];
1470 		}
1471 	}
1472 }
1473 
1474 /*
1475  * See comments for smb_fsop_shrlock()
1476  */
1477 int
1478 smb_vop_shrlock(vnode_t *vp, uint32_t uniq_fid, uint32_t desired_access,
1479     uint32_t share_access, cred_t *cr)
1480 {
1481 	struct shrlock shr;
1482 	struct shr_locowner shr_own;
1483 	short new_access = 0;
1484 	short deny = 0;
1485 	int flag = 0;
1486 	int cmd;
1487 
1488 	/*
1489 	 * share locking is not supported for non-regular
1490 	 * objects in NBMAND mode.
1491 	 */
1492 	if (nbl_need_check(vp)) {
1493 		if (vp->v_type != VREG)
1494 			return (0);
1495 
1496 		cmd = F_SHARE_NBMAND;
1497 	} else {
1498 		cmd = F_SHARE;
1499 	}
1500 
1501 	if ((desired_access & FILE_DATA_ALL) == 0) {
1502 		/* metadata access only */
1503 		new_access |= F_MDACC;
1504 	} else {
1505 		if (desired_access & (ACE_READ_DATA | ACE_EXECUTE)) {
1506 			new_access |= F_RDACC;
1507 			flag |= FREAD;
1508 		}
1509 
1510 		if (desired_access & (ACE_WRITE_DATA | ACE_APPEND_DATA |
1511 		    ACE_ADD_FILE)) {
1512 			new_access |= F_WRACC;
1513 			flag |= FWRITE;
1514 		}
1515 
1516 		if (SMB_DENY_READ(share_access)) {
1517 			deny |= F_RDDNY;
1518 		}
1519 
1520 		if (SMB_DENY_WRITE(share_access)) {
1521 			deny |= F_WRDNY;
1522 		}
1523 
1524 		if (cmd == F_SHARE_NBMAND) {
1525 			if (desired_access & ACE_DELETE)
1526 				new_access |= F_RMACC;
1527 
1528 			if (SMB_DENY_DELETE(share_access)) {
1529 				deny |= F_RMDNY;
1530 			}
1531 		}
1532 	}
1533 
1534 	shr.s_access = new_access;
1535 	shr.s_deny = deny;
1536 	shr.s_sysid = smb_ct.cc_sysid;
1537 	shr.s_pid = uniq_fid;
1538 	shr.s_own_len = sizeof (shr_own);
1539 	shr.s_owner = (caddr_t)&shr_own;
1540 	shr_own.sl_id = shr.s_sysid;
1541 	shr_own.sl_pid = shr.s_pid;
1542 
1543 	return (VOP_SHRLOCK(vp, cmd, &shr, flag, cr, NULL));
1544 }
1545 
1546 int
1547 smb_vop_unshrlock(vnode_t *vp, uint32_t uniq_fid, cred_t *cr)
1548 {
1549 	struct shrlock shr;
1550 	struct shr_locowner shr_own;
1551 
1552 	/*
1553 	 * share locking is not supported for non-regular
1554 	 * objects in NBMAND mode.
1555 	 */
1556 	if (nbl_need_check(vp) && (vp->v_type != VREG))
1557 		return (0);
1558 
1559 	/*
1560 	 * For s_access and s_deny, we do not need to pass in the original
1561 	 * values.
1562 	 */
1563 	shr.s_access = 0;
1564 	shr.s_deny = 0;
1565 	shr.s_sysid = smb_ct.cc_sysid;
1566 	shr.s_pid = uniq_fid;
1567 	shr.s_own_len = sizeof (shr_own);
1568 	shr.s_owner = (caddr_t)&shr_own;
1569 	shr_own.sl_id = shr.s_sysid;
1570 	shr_own.sl_pid = shr.s_pid;
1571 
1572 	return (VOP_SHRLOCK(vp, F_UNSHARE, &shr, 0, cr, NULL));
1573 }
1574 
1575 /*
1576  * Note about mandatory vs advisory locks:
1577  *
1578  * The SMB server really should always request mandatory locks, and
1579  * if the file system does not support them, the SMB server should
1580  * just tell the client it could not get the lock. If we were to
1581  * tell the SMB client "you got the lock" when what they really
1582  * got was only an advisory lock, we would be lying to the client
1583  * about their having exclusive access to the locked range, which
1584  * could easily lead to data corruption.  If someone really wants
1585  * the (dangerous) behavior they can set: smb_allow_advisory_locks
1586  */
1587 int
1588 smb_vop_frlock(vnode_t *vp, cred_t *cr, int flag, flock64_t *bf)
1589 {
1590 	flk_callback_t flk_cb;
1591 	int cmd = F_SETLK_NBMAND;
1592 
1593 	if (smb_allow_advisory_locks != 0 && !nbl_need_check(vp)) {
1594 		/*
1595 		 * The file system does not support nbmand, and
1596 		 * smb_allow_advisory_locks is enabled. (danger!)
1597 		 */
1598 		cmd = F_SETLK;
1599 	}
1600 
1601 	flk_init_callback(&flk_cb, smb_lock_frlock_callback, NULL);
1602 
1603 	return (VOP_FRLOCK(vp, cmd, bf, flag, 0, &flk_cb, cr, &smb_ct));
1604 }
1605 
1606 static callb_cpr_t *
1607 /* ARGSUSED */
1608 smb_lock_frlock_callback(flk_cb_when_t when, void *error)
1609 {
1610 	return (0);
1611 }
1612 
1613 /*
1614  * smb_vop_catia_init_v4_lookup
1615  * Initialize  mapping between wide characters in the range from
1616  * 0x00A4 to 0x00FF and their UNIX (v4) equivalent (wide character).
1617  * Indexed by the decimal value of the wide character (164-255)
1618  * with an offset of -164.
1619  */
1620 static void
1621 smb_vop_catia_init_v4_lookup()
1622 {
1623 	int i, idx, offset = SMB_CATIA_V4_LOOKUP_LOW;
1624 
1625 	for (i = 0; i < SMB_CATIA_V4_LOOKUP_MAX; i++)
1626 		smb_catia_v4_lookup[i] = (smb_wchar_t)(i + offset);
1627 
1628 	for (i = 0; i < SMB_CATIA_NUM_MAPS; i++) {
1629 		idx = (int)catia_maps[i].winchar - offset;
1630 		smb_catia_v4_lookup[idx] = (smb_wchar_t)catia_maps[i].unixchar;
1631 	}
1632 }
1633 
1634 /*
1635  * smb_vop_catia_init_v5_lookup
1636  * Initialize mapping between UNIX ASCII (v4) characters and equivalent
1637  * or translated wide characters.
1638  * Indexed by the decimal value of the ASCII character (0-127).
1639  */
1640 static void
1641 smb_vop_catia_init_v5_lookup()
1642 {
1643 	int i, idx;
1644 
1645 	for (i = 0; i < SMB_CATIA_V5_LOOKUP_MAX; i++)
1646 		smb_catia_v5_lookup[i] = (smb_wchar_t)i;
1647 
1648 	for (i = 0; i < SMB_CATIA_NUM_MAPS; i++) {
1649 		idx = (int)catia_maps[i].unixchar;
1650 		smb_catia_v5_lookup[idx] = catia_maps[i].winchar;
1651 	}
1652 }
1653 
1654 static void
1655 smb_vop_catia_init()
1656 {
1657 	smb_vop_catia_init_v4_lookup();
1658 	smb_vop_catia_init_v5_lookup();
1659 }
1660 
1661 /*
1662  * smb_vop_catia_v5tov4
1663  * (windows (v5) to unix (v4))
1664  *
1665  * Traverse each character in the given source filename and convert the
1666  * multibyte that is equivalent to any special Windows character listed
1667  * in the catia_maps table to the Unix ASCII character if any is
1668  * encountered in the filename. The translated name is returned in buf.
1669  *
1670  * If an error occurs the conversion terminates and name is returned,
1671  * otherwise buf is returned.
1672  */
1673 char *
1674 smb_vop_catia_v5tov4(char *name, char *buf, int buflen)
1675 {
1676 	int v4_idx, numbytes, inc;
1677 	int space_left = buflen - 1; /* one byte reserved for null */
1678 	uint32_t wc;
1679 	char mbstring[MTS_MB_CHAR_MAX];
1680 	char *p, *src = name, *dst = buf;
1681 
1682 	ASSERT(name);
1683 	ASSERT(buf);
1684 
1685 	if (!buf || !name)
1686 		return (name);
1687 
1688 	bzero(buf, buflen);
1689 
1690 	while (*src) {
1691 		if ((numbytes = smb_mbtowc(&wc, src, MTS_MB_CHAR_MAX)) < 0)
1692 			return (name);
1693 
1694 		if (wc < SMB_CATIA_V4_LOOKUP_LOW ||
1695 		    wc > SMB_CATIA_V4_LOOKUP_UPPER) {
1696 			inc = numbytes;
1697 			p = src;
1698 		} else {
1699 			/* Lookup required. */
1700 			v4_idx = (int)wc - SMB_CATIA_V4_LOOKUP_LOW;
1701 			inc = smb_wctomb(mbstring, smb_catia_v4_lookup[v4_idx]);
1702 			p = mbstring;
1703 		}
1704 
1705 		if (space_left < inc)
1706 			return (name);
1707 
1708 		(void) strncpy(dst, p, inc);
1709 		dst += inc;
1710 		space_left -= inc;
1711 		src += numbytes;
1712 	}
1713 
1714 	return (buf);
1715 }
1716 
1717 /*
1718  * smb_vop_catia_v4tov5
1719  * (unix (v4) to windows (v5))
1720  *
1721  * Traverse each character in the given filename 'srcbuf' and convert
1722  * the special Unix character that is listed in the catia_maps table to
1723  * the UTF-8 encoding of the corresponding Windows character if any is
1724  * encountered in the filename.
1725  *
1726  * The translated name is returned in buf.
1727  * If an error occurs the conversion terminates and the original name
1728  * is returned in buf.
1729  */
1730 void
1731 smb_vop_catia_v4tov5(char *name, char *buf, int buflen)
1732 {
1733 	int v5_idx, numbytes;
1734 	int space_left = buflen - 1; /* one byte reserved for null */
1735 	uint32_t wc;
1736 	char mbstring[MTS_MB_CHAR_MAX];
1737 	char *src = name, *dst = buf;
1738 
1739 	ASSERT(name);
1740 	ASSERT(buf);
1741 
1742 	if (!buf || !name)
1743 		return;
1744 
1745 	(void) bzero(buf, buflen);
1746 	while (*src) {
1747 		if (smb_isascii(*src)) {
1748 			/* Lookup required */
1749 			v5_idx = (int)*src++;
1750 			numbytes = smb_wctomb(mbstring,
1751 			    smb_catia_v5_lookup[v5_idx]);
1752 			if (space_left < numbytes)
1753 				break;
1754 			(void) strncpy(dst, mbstring, numbytes);
1755 		} else {
1756 			if ((numbytes = smb_mbtowc(&wc, src,
1757 			    MTS_MB_CHAR_MAX)) < 0)
1758 				break;
1759 			if (space_left < numbytes)
1760 				break;
1761 			(void) strncpy(dst, src, numbytes);
1762 			src += numbytes;
1763 		}
1764 
1765 		dst += numbytes;
1766 		space_left -= numbytes;
1767 	}
1768 
1769 	if (*src)
1770 		(void) strlcpy(buf, name, buflen);
1771 }
1772