xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_vops.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
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, int ioflag, cred_t *cr)
254 {
255 	int error;
256 
257 	(void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, &smb_ct);
258 	error = VOP_READ(vp, uiop, ioflag, 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 		/*
613 		 * This happens creating named streams at the share root.
614 		 */
615 		VN_HOLD(dvp);
616 		*vpp = dvp;
617 		return (0);
618 	}
619 
620 	ASSERT(vpp);
621 	*vpp = NULL;
622 	*direntflags = 0;
623 
624 	if ((name[0] == '.') && (name[1] == '.') && (name[2] == 0)) {
625 		if (rootvp && (dvp == rootvp)) {
626 			VN_HOLD(dvp);
627 			*vpp = dvp;
628 			return (0);
629 		}
630 
631 		if (dvp->v_flag & VROOT) {
632 			vfs_t *vfsp;
633 			vnode_t *cvp = dvp;
634 
635 			/*
636 			 * Set dvp and check for races with forced unmount
637 			 * (see lookuppnvp())
638 			 */
639 
640 			vfsp = cvp->v_vfsp;
641 			vfs_rlock_wait(vfsp);
642 			if (((dvp = cvp->v_vfsp->vfs_vnodecovered) == NULL) ||
643 			    (cvp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) {
644 				vfs_unlock(vfsp);
645 				return (EIO);
646 			}
647 			vfs_unlock(vfsp);
648 		}
649 	}
650 
651 	if (flags & SMB_IGNORE_CASE)
652 		option_flags = FIGNORECASE;
653 
654 	if (flags & SMB_CATIA)
655 		np = smb_vop_catia_v5tov4(name, namebuf, sizeof (namebuf));
656 
657 	pn_alloc(&rpn);
658 
659 	/*
660 	 * Easier to not have junk in rpn, as not every FS type
661 	 * will necessarily fill that in for us.
662 	 */
663 	bzero(rpn.pn_buf, rpn.pn_bufsize);
664 
665 	error = VOP_LOOKUP(dvp, np, vpp, NULL, option_flags, NULL, cr,
666 	    &smb_ct, direntflags, &rpn);
667 
668 	if (error == 0) {
669 		if (od_name) {
670 			bzero(od_name, MAXNAMELEN);
671 			if ((option_flags & FIGNORECASE) != 0 &&
672 			    rpn.pn_buf[0] != '\0')
673 				np = rpn.pn_buf;
674 			else
675 				np = name;
676 			if (flags & SMB_CATIA)
677 				smb_vop_catia_v4tov5(np, od_name, MAXNAMELEN);
678 			else
679 				(void) strlcpy(od_name, np, MAXNAMELEN);
680 		}
681 
682 		if (attr != NULL) {
683 			attr->sa_mask = SMB_AT_ALL;
684 			(void) smb_vop_getattr(*vpp, NULL, attr, 0,
685 			    zone_kcred());
686 		}
687 	}
688 
689 	pn_free(&rpn);
690 	return (error);
691 }
692 
693 int
694 smb_vop_create(vnode_t *dvp, char *name, smb_attr_t *attr, vnode_t **vpp,
695     int flags, cred_t *cr, vsecattr_t *vsap)
696 {
697 	int error;
698 	int option_flags = 0;
699 	xvattr_t xvattr;
700 	vattr_t *vap;
701 	char *np = name;
702 	char namebuf[MAXNAMELEN];
703 
704 	if (flags & SMB_IGNORE_CASE)
705 		option_flags = FIGNORECASE;
706 
707 	attr->sa_vattr.va_mask = 0;
708 
709 	if (vfs_has_feature(dvp->v_vfsp, VFSFT_XVATTR)) {
710 		smb_vop_setup_xvattr(attr, &xvattr);
711 		vap = &xvattr.xva_vattr;
712 	} else {
713 		smb_sa_to_va_mask(attr->sa_mask, &attr->sa_vattr.va_mask);
714 		vap = &attr->sa_vattr;
715 	}
716 
717 	if (flags & SMB_CATIA) {
718 		np = smb_vop_catia_v5tov4(name, namebuf, sizeof (namebuf));
719 		if (strchr(np, '/') != NULL)
720 			return (EILSEQ);
721 	}
722 
723 	error = VOP_CREATE(dvp, np, vap, EXCL, attr->sa_vattr.va_mode,
724 	    vpp, cr, option_flags, &smb_ct, vsap);
725 
726 	/*
727 	 * One could argue that filesystems should obey the size
728 	 * if specified in the create attributes.  Unfortunately,
729 	 * they only appear to let you truncate the size to zero.
730 	 * SMB needs to set a non-zero size, so work-around.
731 	 */
732 	if (error == 0 && *vpp != NULL &&
733 	    (vap->va_mask & AT_SIZE) != 0 &&
734 	    vap->va_size > 0) {
735 		vattr_t ta = *vap;
736 		ta.va_mask = AT_SIZE;
737 		(void) VOP_SETATTR(*vpp, &ta, 0, cr, &smb_ct);
738 	}
739 
740 	return (error);
741 }
742 
743 int
744 smb_vop_remove(vnode_t *dvp, char *name, int flags, cred_t *cr)
745 {
746 	int error;
747 	int option_flags = 0;
748 	char *np = name;
749 	char namebuf[MAXNAMELEN];
750 
751 	if (flags & SMB_IGNORE_CASE)
752 		option_flags = FIGNORECASE;
753 
754 	if (flags & SMB_CATIA)
755 		np = smb_vop_catia_v5tov4(name, namebuf, sizeof (namebuf));
756 
757 	error = VOP_REMOVE(dvp, np, cr, &smb_ct, option_flags);
758 
759 	return (error);
760 }
761 
762 /*
763  * smb_vop_link(target-dir-vp, source-file-vp, target-name)
764  *
765  * Create a link - same tree (identical TID) only.
766  */
767 int
768 smb_vop_link(vnode_t *to_dvp, vnode_t *from_vp, char *to_name,
769     int flags, cred_t *cr)
770 {
771 	int option_flags = 0;
772 	char *np, *buf;
773 	int rc;
774 
775 	if (flags & SMB_IGNORE_CASE)
776 		option_flags = FIGNORECASE;
777 
778 	if (flags & SMB_CATIA) {
779 		buf = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
780 		np = smb_vop_catia_v5tov4(to_name, buf, MAXNAMELEN);
781 		if (strchr(np, '/') != NULL) {
782 			kmem_free(buf, MAXNAMELEN);
783 			return (EILSEQ);
784 		}
785 
786 		rc = VOP_LINK(to_dvp, from_vp, np, cr, &smb_ct, option_flags);
787 		kmem_free(buf, MAXNAMELEN);
788 		return (rc);
789 	}
790 
791 	rc = VOP_LINK(to_dvp, from_vp, to_name, cr, &smb_ct, option_flags);
792 	return (rc);
793 }
794 
795 /*
796  * smb_vop_rename()
797  *
798  * The rename is for files in the same tree (identical TID) only.
799  */
800 int
801 smb_vop_rename(vnode_t *from_dvp, char *from_name, vnode_t *to_dvp,
802     char *to_name, int flags, cred_t *cr)
803 {
804 	int error;
805 	int option_flags = 0;
806 	char *from, *to, *fbuf, *tbuf;
807 
808 	if (flags & SMB_IGNORE_CASE)
809 		option_flags = FIGNORECASE;
810 
811 	if (flags & SMB_CATIA) {
812 		tbuf = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
813 		to = smb_vop_catia_v5tov4(to_name, tbuf, MAXNAMELEN);
814 		if (strchr(to, '/') != NULL) {
815 			kmem_free(tbuf, MAXNAMELEN);
816 			return (EILSEQ);
817 		}
818 
819 		fbuf = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
820 		from = smb_vop_catia_v5tov4(from_name, fbuf, MAXNAMELEN);
821 
822 		error = VOP_RENAME(from_dvp, from, to_dvp, to, cr,
823 		    &smb_ct, option_flags);
824 
825 		kmem_free(tbuf, MAXNAMELEN);
826 		kmem_free(fbuf, MAXNAMELEN);
827 		return (error);
828 	}
829 
830 	error = VOP_RENAME(from_dvp, from_name, to_dvp, to_name, cr,
831 	    &smb_ct, option_flags);
832 
833 	return (error);
834 }
835 
836 int
837 smb_vop_mkdir(vnode_t *dvp, char *name, smb_attr_t *attr, vnode_t **vpp,
838     int flags, cred_t *cr, vsecattr_t *vsap)
839 {
840 	int error;
841 	int option_flags = 0;
842 	xvattr_t xvattr;
843 	vattr_t *vap;
844 	char *np = name;
845 	char namebuf[MAXNAMELEN];
846 
847 	if (flags & SMB_IGNORE_CASE)
848 		option_flags = FIGNORECASE;
849 
850 	attr->sa_vattr.va_mask = 0;
851 
852 	if (vfs_has_feature(dvp->v_vfsp, VFSFT_XVATTR)) {
853 		smb_vop_setup_xvattr(attr, &xvattr);
854 		vap = &xvattr.xva_vattr;
855 	} else {
856 		smb_sa_to_va_mask(attr->sa_mask, &attr->sa_vattr.va_mask);
857 		vap = &attr->sa_vattr;
858 	}
859 
860 	if (flags & SMB_CATIA) {
861 		np = smb_vop_catia_v5tov4(name, namebuf, sizeof (namebuf));
862 		if (strchr(np, '/') != NULL)
863 			return (EILSEQ);
864 	}
865 
866 	error = VOP_MKDIR(dvp, np, vap, vpp, cr, &smb_ct, option_flags, vsap);
867 
868 	return (error);
869 }
870 
871 /*
872  * smb_vop_rmdir()
873  *
874  * Only simple rmdir supported, consistent with NT semantics
875  * (can only remove an empty directory).
876  *
877  * The third argument to VOP_RMDIR  is the current directory of
878  * the process.  It allows rmdir wants to EINVAL if one tries to
879  * remove ".".  Since SMB servers do not know what their clients'
880  * current directories are, we fake it by supplying a vnode known
881  * to exist and illegal to remove (rootdir).
882  */
883 int
884 smb_vop_rmdir(vnode_t *dvp, char *name, int flags, cred_t *cr)
885 {
886 	int error;
887 	int option_flags = 0;
888 	char *np = name;
889 	char namebuf[MAXNAMELEN];
890 
891 	if (flags & SMB_IGNORE_CASE)
892 		option_flags = FIGNORECASE;
893 
894 	if (flags & SMB_CATIA)
895 		np = smb_vop_catia_v5tov4(name, namebuf, sizeof (namebuf));
896 
897 	error = VOP_RMDIR(dvp, np, rootdir, cr, &smb_ct, option_flags);
898 	return (error);
899 }
900 
901 int
902 smb_vop_commit(vnode_t *vp, cred_t *cr)
903 {
904 	return (VOP_FSYNC(vp, 1, cr, &smb_ct));
905 }
906 
907 /*
908  * Some code in smb_node.c needs to know which DOS attributes
909  * we can actually store.  Let's define a mask here of all the
910  * DOS attribute flags supported by the following function.
911  */
912 const uint32_t
913 smb_vop_dosattr_settable =
914 	FILE_ATTRIBUTE_ARCHIVE |
915 	FILE_ATTRIBUTE_SYSTEM |
916 	FILE_ATTRIBUTE_HIDDEN |
917 	FILE_ATTRIBUTE_READONLY |
918 	FILE_ATTRIBUTE_OFFLINE |
919 	FILE_ATTRIBUTE_SPARSE_FILE;
920 
921 static void
922 smb_vop_setup_xvattr(smb_attr_t *smb_attr, xvattr_t *xvattr)
923 {
924 	xoptattr_t *xoap = NULL;
925 	uint_t xva_mask;
926 
927 	/*
928 	 * Initialize xvattr, including bzero
929 	 */
930 	xva_init(xvattr);
931 	xoap = xva_getxoptattr(xvattr);
932 
933 	ASSERT(xoap);
934 
935 	/*
936 	 * Copy caller-specified classic attributes to xvattr.
937 	 * First save xvattr's mask (set in xva_init()), which
938 	 * contains AT_XVATTR.  This is |'d in later if needed.
939 	 */
940 
941 	xva_mask = xvattr->xva_vattr.va_mask;
942 	xvattr->xva_vattr = smb_attr->sa_vattr;
943 
944 	smb_sa_to_va_mask(smb_attr->sa_mask, &xvattr->xva_vattr.va_mask);
945 
946 	/*
947 	 * Do not set ctime (only the file system can do it)
948 	 */
949 
950 	xvattr->xva_vattr.va_mask &= ~AT_CTIME;
951 
952 	if (smb_attr->sa_mask & SMB_AT_DOSATTR) {
953 
954 		/*
955 		 * "|" in the original xva_mask, which contains
956 		 * AT_XVATTR
957 		 */
958 
959 		xvattr->xva_vattr.va_mask |= xva_mask;
960 
961 		XVA_SET_REQ(xvattr, XAT_ARCHIVE);
962 		XVA_SET_REQ(xvattr, XAT_SYSTEM);
963 		XVA_SET_REQ(xvattr, XAT_READONLY);
964 		XVA_SET_REQ(xvattr, XAT_HIDDEN);
965 		XVA_SET_REQ(xvattr, XAT_OFFLINE);
966 		XVA_SET_REQ(xvattr, XAT_SPARSE);
967 
968 		/*
969 		 * smb_attr->sa_dosattr: If a given bit is not set,
970 		 * that indicates that the corresponding field needs
971 		 * to be updated with a "0" value.  This is done
972 		 * implicitly as the xoap->xoa_* fields were bzero'd.
973 		 */
974 
975 		if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_ARCHIVE)
976 			xoap->xoa_archive = 1;
977 
978 		if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_SYSTEM)
979 			xoap->xoa_system = 1;
980 
981 		if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_READONLY)
982 			xoap->xoa_readonly = 1;
983 
984 		if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_HIDDEN)
985 			xoap->xoa_hidden = 1;
986 
987 		if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_OFFLINE)
988 			xoap->xoa_offline = 1;
989 
990 		if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_SPARSE_FILE)
991 			xoap->xoa_sparse = 1;
992 	}
993 
994 	if (smb_attr->sa_mask & SMB_AT_CRTIME) {
995 		/*
996 		 * "|" in the original xva_mask, which contains
997 		 * AT_XVATTR
998 		 */
999 
1000 		xvattr->xva_vattr.va_mask |= xva_mask;
1001 		XVA_SET_REQ(xvattr, XAT_CREATETIME);
1002 		xoap->xoa_createtime = smb_attr->sa_crtime;
1003 	}
1004 }
1005 
1006 /*
1007  * smb_vop_readdir()
1008  *
1009  * Collects an SMB_MINLEN_RDDIR_BUF "page" of directory entries.
1010  * The directory entries are returned in an fs-independent format by the
1011  * underlying file system.  That is, the "page" of information returned is
1012  * not literally stored on-disk in the format returned.
1013  * If the file system supports extended directory entries (has features
1014  * VFSFT_DIRENTFLAGS), set V_RDDIR_ENTFLAGS to cause the buffer to be
1015  * filled with edirent_t structures, instead of dirent64_t structures.
1016  * If the file system supports access based enumeration (abe), set
1017  * V_RDDIR_ACCFILTER to filter directory entries based on user cred.
1018  */
1019 int
1020 smb_vop_readdir(vnode_t *vp, uint32_t offset,
1021     void *buf, int *count, int *eof, uint32_t rddir_flag, cred_t *cr)
1022 {
1023 	int error = 0;
1024 	int flags = 0;
1025 	int rdirent_size;
1026 	struct uio auio;
1027 	struct iovec aiov;
1028 
1029 	if (vp->v_type != VDIR)
1030 		return (ENOTDIR);
1031 
1032 	if ((rddir_flag & SMB_EDIRENT) != 0 &&
1033 	    vfs_has_feature(vp->v_vfsp, VFSFT_DIRENTFLAGS)) {
1034 		flags |= V_RDDIR_ENTFLAGS;
1035 		rdirent_size = sizeof (edirent_t);
1036 	} else {
1037 		rdirent_size = sizeof (dirent64_t);
1038 	}
1039 
1040 	if (*count < rdirent_size)
1041 		return (EINVAL);
1042 
1043 	if (rddir_flag & SMB_ABE)
1044 		flags |= V_RDDIR_ACCFILTER;
1045 
1046 	aiov.iov_base = buf;
1047 	aiov.iov_len = *count;
1048 	auio.uio_iov = &aiov;
1049 	auio.uio_iovcnt = 1;
1050 	auio.uio_loffset = (uint64_t)offset;
1051 	auio.uio_segflg = UIO_SYSSPACE;
1052 	auio.uio_extflg = UIO_COPY_DEFAULT;
1053 	auio.uio_resid = *count;
1054 	auio.uio_fmode = 0;
1055 
1056 	(void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, &smb_ct);
1057 	error = VOP_READDIR(vp, &auio, cr, eof, &smb_ct, flags);
1058 	VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, &smb_ct);
1059 
1060 	if (error == 0)
1061 		*count = *count - auio.uio_resid;
1062 
1063 	return (error);
1064 }
1065 
1066 /*
1067  * smb_sa_to_va_mask
1068  *
1069  * Set va_mask by running through the SMB_AT_* #define's and
1070  * setting those bits that correspond to the SMB_AT_* bits
1071  * set in sa_mask.
1072  */
1073 void
1074 smb_sa_to_va_mask(uint_t sa_mask, uint_t *va_maskp)
1075 {
1076 	int i;
1077 	uint_t smask;
1078 
1079 	smask = (sa_mask);
1080 	for (i = SMB_AT_TYPE; (i < SMB_AT_MAX) && (smask != 0); ++i) {
1081 		if (smask & 1)
1082 			*(va_maskp) |= smb_attrmap[i];
1083 
1084 		smask >>= 1;
1085 	}
1086 }
1087 
1088 /*
1089  * smb_vop_stream_lookup()
1090  *
1091  * The name returned in od_name is the on-disk name of the stream with the
1092  * SMB_STREAM_PREFIX stripped off.  od_name should be allocated to MAXNAMELEN
1093  * by the caller.
1094  */
1095 int
1096 smb_vop_stream_lookup(
1097     vnode_t		*fvp,
1098     char		*stream_name,
1099     vnode_t		**vpp,
1100     char		*od_name,
1101     vnode_t		**xattrdirvpp,
1102     int			flags,
1103     vnode_t		*rootvp,
1104     cred_t		*cr)
1105 {
1106 	char *solaris_stream_name;
1107 	char *name;
1108 	int error, tmpflgs;
1109 
1110 	if ((error = smb_vop_lookup_xattrdir(fvp, xattrdirvpp,
1111 	    LOOKUP_XATTR | CREATE_XATTR_DIR, cr)) != 0)
1112 		return (error);
1113 
1114 	/*
1115 	 * Prepend SMB_STREAM_PREFIX to stream name
1116 	 */
1117 
1118 	solaris_stream_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1119 	(void) snprintf(solaris_stream_name, MAXNAMELEN,
1120 	    "%s%s", SMB_STREAM_PREFIX, stream_name);
1121 
1122 	/*
1123 	 * "name" will hold the on-disk name returned from smb_vop_lookup
1124 	 * for the stream, including the SMB_STREAM_PREFIX.
1125 	 */
1126 
1127 	name = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
1128 
1129 	if ((error = smb_vop_lookup(*xattrdirvpp, solaris_stream_name, vpp,
1130 	    name, flags, &tmpflgs, rootvp, NULL, cr)) != 0) {
1131 		VN_RELE(*xattrdirvpp);
1132 	} else {
1133 		(void) strlcpy(od_name, &(name[SMB_STREAM_PREFIX_LEN]),
1134 		    MAXNAMELEN);
1135 	}
1136 
1137 	kmem_free(solaris_stream_name, MAXNAMELEN);
1138 	kmem_free(name, MAXNAMELEN);
1139 
1140 	return (error);
1141 }
1142 
1143 int
1144 smb_vop_stream_create(vnode_t *fvp, char *stream_name, smb_attr_t *attr,
1145     vnode_t **vpp, vnode_t **xattrdirvpp, int flags, cred_t *cr)
1146 {
1147 	char *solaris_stream_name;
1148 	int error;
1149 
1150 	if ((error = smb_vop_lookup_xattrdir(fvp, xattrdirvpp,
1151 	    LOOKUP_XATTR | CREATE_XATTR_DIR, cr)) != 0)
1152 		return (error);
1153 
1154 	/*
1155 	 * Prepend SMB_STREAM_PREFIX to stream name
1156 	 */
1157 
1158 	solaris_stream_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1159 	(void) snprintf(solaris_stream_name, MAXNAMELEN,
1160 	    "%s%s", SMB_STREAM_PREFIX, stream_name);
1161 
1162 	if ((error = smb_vop_create(*xattrdirvpp, solaris_stream_name, attr,
1163 	    vpp, flags, cr, NULL)) != 0)
1164 		VN_RELE(*xattrdirvpp);
1165 
1166 	kmem_free(solaris_stream_name, MAXNAMELEN);
1167 
1168 	return (error);
1169 }
1170 
1171 int
1172 smb_vop_stream_remove(vnode_t *vp, char *stream_name, int flags, cred_t *cr)
1173 {
1174 	char *solaris_stream_name;
1175 	vnode_t *xattrdirvp;
1176 	int error;
1177 
1178 	error = smb_vop_lookup_xattrdir(vp, &xattrdirvp, LOOKUP_XATTR, cr);
1179 	if (error != 0)
1180 		return (error);
1181 
1182 	/*
1183 	 * Prepend SMB_STREAM_PREFIX to stream name
1184 	 */
1185 
1186 	solaris_stream_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1187 	(void) snprintf(solaris_stream_name, MAXNAMELEN,
1188 	    "%s%s", SMB_STREAM_PREFIX, stream_name);
1189 
1190 	/* XXX might have to use kcred */
1191 	error = smb_vop_remove(xattrdirvp, solaris_stream_name, flags, cr);
1192 
1193 	kmem_free(solaris_stream_name, MAXNAMELEN);
1194 	VN_RELE(xattrdirvp);
1195 
1196 	return (error);
1197 }
1198 
1199 int
1200 smb_vop_lookup_xattrdir(vnode_t *fvp, vnode_t **xattrdirvpp, int flags,
1201     cred_t *cr)
1202 {
1203 	int error;
1204 
1205 	error = VOP_LOOKUP(fvp, "", xattrdirvpp, NULL, flags, NULL, cr,
1206 	    &smb_ct, NULL, NULL);
1207 	return (error);
1208 }
1209 
1210 /*
1211  * smb_vop_traverse_check()
1212  *
1213  * This function checks to see if the passed-in vnode has a file system
1214  * mounted on it.  If it does, the mount point is "traversed" and the
1215  * vnode for the root of the file system is returned.
1216  */
1217 int
1218 smb_vop_traverse_check(vnode_t **vpp)
1219 {
1220 	int error;
1221 
1222 	if (vn_mountedvfs(*vpp) == 0)
1223 		return (0);
1224 
1225 	/*
1226 	 * traverse() may return a different held vnode, even in the error case.
1227 	 * If it returns a different vnode, it will have released the original.
1228 	 */
1229 
1230 	error = traverse(vpp);
1231 
1232 	return (error);
1233 }
1234 
1235 int /*ARGSUSED*/
1236 smb_vop_statfs(vnode_t *vp, struct statvfs64 *statp, cred_t *cr)
1237 {
1238 	int error;
1239 
1240 	error = VFS_STATVFS(vp->v_vfsp, statp);
1241 
1242 	return (error);
1243 }
1244 
1245 /*
1246  * smb_vop_acl_read
1247  *
1248  * Reads the ACL of the specified file into 'aclp'.
1249  * acl_type is the type of ACL which the filesystem supports.
1250  *
1251  * Caller has to free the allocated memory for aclp by calling
1252  * acl_free().
1253  */
1254 int
1255 smb_vop_acl_read(vnode_t *vp, acl_t **aclp, int flags, acl_type_t acl_type,
1256     cred_t *cr)
1257 {
1258 	int error;
1259 	vsecattr_t vsecattr;
1260 
1261 	ASSERT(vp);
1262 	ASSERT(aclp);
1263 
1264 	*aclp = NULL;
1265 	bzero(&vsecattr, sizeof (vsecattr_t));
1266 
1267 	switch (acl_type) {
1268 	case ACLENT_T:
1269 		vsecattr.vsa_mask = VSA_ACL | VSA_ACLCNT | VSA_DFACL |
1270 		    VSA_DFACLCNT;
1271 		break;
1272 
1273 	case ACE_T:
1274 		vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT | VSA_ACE_ACLFLAGS;
1275 		break;
1276 
1277 	default:
1278 		return (EINVAL);
1279 	}
1280 
1281 	if ((error = VOP_GETSECATTR(vp, &vsecattr, flags, cr, &smb_ct)) != 0)
1282 		return (error);
1283 
1284 	*aclp = smb_fsacl_from_vsa(&vsecattr, acl_type);
1285 	if (vp->v_type == VDIR)
1286 		(*aclp)->acl_flags |= ACL_IS_DIR;
1287 
1288 	return (0);
1289 }
1290 
1291 /*
1292  * smb_vop_acl_write
1293  *
1294  * Writes the given ACL in aclp for the specified file.
1295  */
1296 int
1297 smb_vop_acl_write(vnode_t *vp, acl_t *aclp, int flags, cred_t *cr)
1298 {
1299 	int error;
1300 	vsecattr_t vsecattr;
1301 	int aclbsize;
1302 
1303 	ASSERT(vp);
1304 	ASSERT(aclp);
1305 
1306 	error = smb_fsacl_to_vsa(aclp, &vsecattr, &aclbsize);
1307 
1308 	if (error == 0) {
1309 		(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, &smb_ct);
1310 		error = VOP_SETSECATTR(vp, &vsecattr, flags, cr, &smb_ct);
1311 		VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, &smb_ct);
1312 	}
1313 
1314 	if (aclbsize && vsecattr.vsa_aclentp)
1315 		kmem_free(vsecattr.vsa_aclentp, aclbsize);
1316 
1317 	return (error);
1318 }
1319 
1320 /*
1321  * smb_vop_acl_type
1322  *
1323  * Determines the ACL type for the given vnode.
1324  * ACLENT_T is a Posix ACL and ACE_T is a ZFS ACL.
1325  */
1326 acl_type_t
1327 smb_vop_acl_type(vnode_t *vp)
1328 {
1329 	int error;
1330 	ulong_t whichacl;
1331 
1332 	error = VOP_PATHCONF(vp, _PC_ACL_ENABLED, &whichacl,
1333 	    zone_kcred(), NULL);
1334 	if (error != 0) {
1335 		/*
1336 		 * If we got an error, then the filesystem
1337 		 * likely does not understand the _PC_ACL_ENABLED
1338 		 * pathconf.  In this case, we fall back to trying
1339 		 * POSIX-draft (aka UFS-style) ACLs.
1340 		 */
1341 		whichacl = _ACL_ACLENT_ENABLED;
1342 	}
1343 
1344 	if (!(whichacl & (_ACL_ACE_ENABLED | _ACL_ACLENT_ENABLED))) {
1345 		/*
1346 		 * If the file system supports neither ACE nor
1347 		 * ACLENT ACLs we will fall back to UFS-style ACLs
1348 		 * like we did above if there was an error upon
1349 		 * calling VOP_PATHCONF.
1350 		 *
1351 		 * ACE and ACLENT type ACLs are the only interfaces
1352 		 * supported thus far.  If any other bits are set on
1353 		 * 'whichacl' upon return from VOP_PATHCONF, we will
1354 		 * ignore them.
1355 		 */
1356 		whichacl = _ACL_ACLENT_ENABLED;
1357 	}
1358 
1359 	if (whichacl == _ACL_ACLENT_ENABLED)
1360 		return (ACLENT_T);
1361 
1362 	return (ACE_T);
1363 }
1364 
1365 static const int zfs_perms[] = {
1366 	ACE_READ_DATA, ACE_WRITE_DATA, ACE_APPEND_DATA, ACE_READ_NAMED_ATTRS,
1367 	ACE_WRITE_NAMED_ATTRS, ACE_EXECUTE, ACE_DELETE_CHILD,
1368 	ACE_READ_ATTRIBUTES, ACE_WRITE_ATTRIBUTES, ACE_DELETE, ACE_READ_ACL,
1369 	ACE_WRITE_ACL, ACE_WRITE_OWNER, ACE_SYNCHRONIZE
1370 };
1371 
1372 static const int unix_perms[] = { VREAD, VWRITE, VEXEC };
1373 /*
1374  * smb_vop_eaccess
1375  *
1376  * Returns the effective permission of the given credential for the
1377  * specified object.
1378  *
1379  * This is just a workaround. We need VFS/FS support for this.
1380  */
1381 void
1382 smb_vop_eaccess(vnode_t *vp, int *mode, int flags, vnode_t *dir_vp, cred_t *cr)
1383 {
1384 	int error, i;
1385 	int pnum;
1386 
1387 	*mode = 0;
1388 
1389 	if (flags == V_ACE_MASK) {
1390 		pnum = sizeof (zfs_perms) / sizeof (int);
1391 
1392 		for (i = 0; i < pnum; i++) {
1393 			error = smb_vop_access(vp, zfs_perms[i], flags,
1394 			    dir_vp, cr);
1395 			if (error == 0)
1396 				*mode |= zfs_perms[i];
1397 		}
1398 	} else {
1399 		pnum = sizeof (unix_perms) / sizeof (int);
1400 
1401 		for (i = 0; i < pnum; i++) {
1402 			error = smb_vop_access(vp, unix_perms[i], flags,
1403 			    dir_vp, cr);
1404 			if (error == 0)
1405 				*mode |= unix_perms[i];
1406 		}
1407 	}
1408 }
1409 
1410 /*
1411  * See comments for smb_fsop_shrlock()
1412  */
1413 int
1414 smb_vop_shrlock(vnode_t *vp, uint32_t uniq_fid, uint32_t desired_access,
1415     uint32_t share_access, cred_t *cr)
1416 {
1417 	struct shrlock shr;
1418 	struct shr_locowner shr_own;
1419 	short new_access = 0;
1420 	short deny = 0;
1421 	int flag = 0;
1422 	int cmd;
1423 
1424 	/*
1425 	 * share locking is not supported for non-regular
1426 	 * objects in NBMAND mode.
1427 	 */
1428 	if (nbl_need_check(vp)) {
1429 		if (vp->v_type != VREG)
1430 			return (0);
1431 
1432 		cmd = F_SHARE_NBMAND;
1433 	} else {
1434 		cmd = F_SHARE;
1435 	}
1436 
1437 	if ((desired_access & FILE_DATA_ALL) == 0) {
1438 		/* metadata access only */
1439 		new_access |= F_MDACC;
1440 	} else {
1441 		if (desired_access & (ACE_READ_DATA | ACE_EXECUTE)) {
1442 			new_access |= F_RDACC;
1443 			flag |= FREAD;
1444 		}
1445 
1446 		if (desired_access & (ACE_WRITE_DATA | ACE_APPEND_DATA |
1447 		    ACE_ADD_FILE)) {
1448 			new_access |= F_WRACC;
1449 			flag |= FWRITE;
1450 		}
1451 
1452 		if (SMB_DENY_READ(share_access)) {
1453 			deny |= F_RDDNY;
1454 		}
1455 
1456 		if (SMB_DENY_WRITE(share_access)) {
1457 			deny |= F_WRDNY;
1458 		}
1459 
1460 		if (cmd == F_SHARE_NBMAND) {
1461 			if (desired_access & ACE_DELETE)
1462 				new_access |= F_RMACC;
1463 
1464 			if (SMB_DENY_DELETE(share_access)) {
1465 				deny |= F_RMDNY;
1466 			}
1467 		}
1468 	}
1469 
1470 	shr.s_access = new_access;
1471 	shr.s_deny = deny;
1472 	shr.s_sysid = smb_ct.cc_sysid;
1473 	shr.s_pid = uniq_fid;
1474 	shr.s_own_len = sizeof (shr_own);
1475 	shr.s_owner = (caddr_t)&shr_own;
1476 	shr_own.sl_id = shr.s_sysid;
1477 	shr_own.sl_pid = shr.s_pid;
1478 
1479 	return (VOP_SHRLOCK(vp, cmd, &shr, flag, cr, NULL));
1480 }
1481 
1482 int
1483 smb_vop_unshrlock(vnode_t *vp, uint32_t uniq_fid, cred_t *cr)
1484 {
1485 	struct shrlock shr;
1486 	struct shr_locowner shr_own;
1487 
1488 	/*
1489 	 * share locking is not supported for non-regular
1490 	 * objects in NBMAND mode.
1491 	 */
1492 	if (nbl_need_check(vp) && (vp->v_type != VREG))
1493 		return (0);
1494 
1495 	/*
1496 	 * For s_access and s_deny, we do not need to pass in the original
1497 	 * values.
1498 	 */
1499 	shr.s_access = 0;
1500 	shr.s_deny = 0;
1501 	shr.s_sysid = smb_ct.cc_sysid;
1502 	shr.s_pid = uniq_fid;
1503 	shr.s_own_len = sizeof (shr_own);
1504 	shr.s_owner = (caddr_t)&shr_own;
1505 	shr_own.sl_id = shr.s_sysid;
1506 	shr_own.sl_pid = shr.s_pid;
1507 
1508 	return (VOP_SHRLOCK(vp, F_UNSHARE, &shr, 0, cr, NULL));
1509 }
1510 
1511 /*
1512  * Note about mandatory vs advisory locks:
1513  *
1514  * The SMB server really should always request mandatory locks, and
1515  * if the file system does not support them, the SMB server should
1516  * just tell the client it could not get the lock. If we were to
1517  * tell the SMB client "you got the lock" when what they really
1518  * got was only an advisory lock, we would be lying to the client
1519  * about their having exclusive access to the locked range, which
1520  * could easily lead to data corruption.  If someone really wants
1521  * the (dangerous) behavior they can set: smb_allow_advisory_locks
1522  */
1523 int
1524 smb_vop_frlock(vnode_t *vp, cred_t *cr, int flag, flock64_t *bf)
1525 {
1526 	flk_callback_t flk_cb;
1527 	int cmd = F_SETLK_NBMAND;
1528 
1529 	if (smb_allow_advisory_locks != 0 && !nbl_need_check(vp)) {
1530 		/*
1531 		 * The file system does not support nbmand, and
1532 		 * smb_allow_advisory_locks is enabled. (danger!)
1533 		 */
1534 		cmd = F_SETLK;
1535 	}
1536 
1537 	flk_init_callback(&flk_cb, smb_lock_frlock_callback, NULL);
1538 
1539 	return (VOP_FRLOCK(vp, cmd, bf, flag, 0, &flk_cb, cr, &smb_ct));
1540 }
1541 
1542 static callb_cpr_t *
1543 /* ARGSUSED */
1544 smb_lock_frlock_callback(flk_cb_when_t when, void *error)
1545 {
1546 	return (0);
1547 }
1548 
1549 /*
1550  * smb_vop_catia_init_v4_lookup
1551  * Initialize  mapping between wide characters in the range from
1552  * 0x00A4 to 0x00FF and their UNIX (v4) equivalent (wide character).
1553  * Indexed by the decimal value of the wide character (164-255)
1554  * with an offset of -164.
1555  */
1556 static void
1557 smb_vop_catia_init_v4_lookup()
1558 {
1559 	int i, idx, offset = SMB_CATIA_V4_LOOKUP_LOW;
1560 
1561 	for (i = 0; i < SMB_CATIA_V4_LOOKUP_MAX; i++)
1562 		smb_catia_v4_lookup[i] = (smb_wchar_t)(i + offset);
1563 
1564 	for (i = 0; i < SMB_CATIA_NUM_MAPS; i++) {
1565 		idx = (int)catia_maps[i].winchar - offset;
1566 		smb_catia_v4_lookup[idx] = (smb_wchar_t)catia_maps[i].unixchar;
1567 	}
1568 }
1569 
1570 /*
1571  * smb_vop_catia_init_v5_lookup
1572  * Initialize mapping between UNIX ASCII (v4) characters and equivalent
1573  * or translated wide characters.
1574  * Indexed by the decimal value of the ASCII character (0-127).
1575  */
1576 static void
1577 smb_vop_catia_init_v5_lookup()
1578 {
1579 	int i, idx;
1580 
1581 	for (i = 0; i < SMB_CATIA_V5_LOOKUP_MAX; i++)
1582 		smb_catia_v5_lookup[i] = (smb_wchar_t)i;
1583 
1584 	for (i = 0; i < SMB_CATIA_NUM_MAPS; i++) {
1585 		idx = (int)catia_maps[i].unixchar;
1586 		smb_catia_v5_lookup[idx] = catia_maps[i].winchar;
1587 	}
1588 }
1589 
1590 static void
1591 smb_vop_catia_init()
1592 {
1593 	smb_vop_catia_init_v4_lookup();
1594 	smb_vop_catia_init_v5_lookup();
1595 }
1596 
1597 /*
1598  * smb_vop_catia_v5tov4
1599  * (windows (v5) to unix (v4))
1600  *
1601  * Traverse each character in the given source filename and convert the
1602  * multibyte that is equivalent to any special Windows character listed
1603  * in the catia_maps table to the Unix ASCII character if any is
1604  * encountered in the filename. The translated name is returned in buf.
1605  *
1606  * If an error occurs the conversion terminates and name is returned,
1607  * otherwise buf is returned.
1608  */
1609 char *
1610 smb_vop_catia_v5tov4(char *name, char *buf, int buflen)
1611 {
1612 	int v4_idx, numbytes, inc;
1613 	int space_left = buflen - 1; /* one byte reserved for null */
1614 	uint32_t wc;
1615 	char mbstring[MTS_MB_CHAR_MAX];
1616 	char *p, *src = name, *dst = buf;
1617 
1618 	ASSERT(name);
1619 	ASSERT(buf);
1620 
1621 	if (!buf || !name)
1622 		return (name);
1623 
1624 	bzero(buf, buflen);
1625 
1626 	while (*src) {
1627 		if ((numbytes = smb_mbtowc(&wc, src, MTS_MB_CHAR_MAX)) < 0)
1628 			return (name);
1629 
1630 		if (wc < SMB_CATIA_V4_LOOKUP_LOW ||
1631 		    wc > SMB_CATIA_V4_LOOKUP_UPPER) {
1632 			inc = numbytes;
1633 			p = src;
1634 		} else {
1635 			/* Lookup required. */
1636 			v4_idx = (int)wc - SMB_CATIA_V4_LOOKUP_LOW;
1637 			inc = smb_wctomb(mbstring, smb_catia_v4_lookup[v4_idx]);
1638 			p = mbstring;
1639 		}
1640 
1641 		if (space_left < inc)
1642 			return (name);
1643 
1644 		(void) strncpy(dst, p, inc);
1645 		dst += inc;
1646 		space_left -= inc;
1647 		src += numbytes;
1648 	}
1649 
1650 	return (buf);
1651 }
1652 
1653 /*
1654  * smb_vop_catia_v4tov5
1655  * (unix (v4) to windows (v5))
1656  *
1657  * Traverse each character in the given filename 'srcbuf' and convert
1658  * the special Unix character that is listed in the catia_maps table to
1659  * the UTF-8 encoding of the corresponding Windows character if any is
1660  * encountered in the filename.
1661  *
1662  * The translated name is returned in buf.
1663  * If an error occurs the conversion terminates and the original name
1664  * is returned in buf.
1665  */
1666 void
1667 smb_vop_catia_v4tov5(char *name, char *buf, int buflen)
1668 {
1669 	int v5_idx, numbytes;
1670 	int space_left = buflen - 1; /* one byte reserved for null */
1671 	uint32_t wc;
1672 	char mbstring[MTS_MB_CHAR_MAX];
1673 	char *src = name, *dst = buf;
1674 
1675 	ASSERT(name);
1676 	ASSERT(buf);
1677 
1678 	if (!buf || !name)
1679 		return;
1680 
1681 	(void) bzero(buf, buflen);
1682 	while (*src) {
1683 		if (smb_isascii(*src)) {
1684 			/* Lookup required */
1685 			v5_idx = (int)*src++;
1686 			numbytes = smb_wctomb(mbstring,
1687 			    smb_catia_v5_lookup[v5_idx]);
1688 			if (space_left < numbytes)
1689 				break;
1690 			(void) strncpy(dst, mbstring, numbytes);
1691 		} else {
1692 			if ((numbytes = smb_mbtowc(&wc, src,
1693 			    MTS_MB_CHAR_MAX)) < 0)
1694 				break;
1695 			if (space_left < numbytes)
1696 				break;
1697 			(void) strncpy(dst, src, numbytes);
1698 			src += numbytes;
1699 		}
1700 
1701 		dst += numbytes;
1702 		space_left -= numbytes;
1703 	}
1704 
1705 	if (*src)
1706 		(void) strlcpy(buf, name, buflen);
1707 }
1708