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