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