xref: /titanic_50/usr/src/uts/common/fs/smbsrv/smb_vops.c (revision 5832075c3d504be66f389f3bc46573d7c21ee3fa)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/uio.h>
31 #include <sys/statvfs.h>
32 #include <sys/vnode.h>
33 #include <sys/thread.h>
34 #include <sys/pathname.h>
35 #include <sys/cred.h>
36 #include <sys/extdirent.h>
37 #include <sys/nbmlock.h>
38 #include <sys/share.h>
39 #include <sys/fcntl.h>
40 
41 #include <smbsrv/smb_vops.h>
42 #include <smbsrv/string.h>
43 
44 #include <smbsrv/smbtrans.h>
45 #include <smbsrv/smb_fsops.h>
46 #include <smbsrv/smb_kproto.h>
47 #include <smbsrv/smb_incl.h>
48 
49 void
50 smb_vop_setup_xvattr(smb_attr_t *smb_attr, xvattr_t *xvattr);
51 
52 static int
53 smb_vop_readdir_readpage(vnode_t *vp, void *buf, uint32_t offset, int *count,
54     cred_t *cr, int flags);
55 
56 static int
57 smb_vop_readdir_entry(vnode_t *dvp, uint32_t *cookiep, char *name, int *namelen,
58     ino64_t *inop, vnode_t **vpp, char *od_name, int flags, cred_t *cr,
59     char *dirbuf, int num_bytes);
60 
61 static int
62 smb_vop_getdents_entries(smb_node_t *dir_snode, uint32_t *cookiep,
63     int32_t *dircountp, char *arg, uint32_t flags, struct smb_request *sr,
64     cred_t *cr, char *dirbuf, int *maxentries, int num_bytes, char *);
65 
66 extern int
67 smb_gather_dents_info(char *args, ino_t fileid, int namelen,
68     char *name, uint32_t cookie, int32_t *countp,
69     smb_attr_t *attr, struct smb_node *snode,
70     char *shortname, char *name83);
71 
72 static void
73 smb_sa_to_va_mask(uint_t sa_mask, uint_t *va_maskp);
74 
75 extern sysid_t lm_alloc_sysidt();
76 
77 #define	SMB_AT_MAX	16
78 static uint_t smb_attrmap[SMB_AT_MAX] = {
79 	0,
80 	AT_TYPE,
81 	AT_MODE,
82 	AT_UID,
83 	AT_GID,
84 	AT_FSID,
85 	AT_NODEID,
86 	AT_NLINK,
87 	AT_SIZE,
88 	AT_ATIME,
89 	AT_MTIME,
90 	AT_CTIME,
91 	AT_RDEV,
92 	AT_BLKSIZE,
93 	AT_NBLOCKS,
94 	AT_SEQ
95 };
96 
97 /*
98  * The smb_ct will be used primarily for range locking.
99  * Since the CIFS server is mapping its locks to POSIX locks,
100  * only one pid is used for operations originating from the
101  * CIFS server (to represent CIFS in the VOP_FRLOCK routines).
102  */
103 
104 caller_context_t smb_ct;
105 
106 /*
107  * smb_vop_start()
108  *
109  * Initialize the smb caller context.  This function must be called
110  * before any other smb_vop calls.
111  */
112 
113 void
114 smb_vop_start(void)
115 {
116 	static boolean_t initialized = B_FALSE;
117 
118 	if (!initialized) {
119 		smb_ct.cc_caller_id = fs_new_caller_id();
120 		smb_ct.cc_pid = ttoproc(curthread)->p_pid;
121 		smb_ct.cc_sysid = lm_alloc_sysidt();
122 		initialized = B_TRUE;
123 	}
124 }
125 
126 int
127 smb_vop_open(vnode_t **vpp, int mode, cred_t *cred)
128 {
129 	return (VOP_OPEN(vpp, mode, cred, &smb_ct));
130 }
131 
132 int
133 smb_vop_close(vnode_t *vp, int mode, cred_t *cred)
134 {
135 	return (VOP_CLOSE(vp, mode, 1, (offset_t)0, cred, &smb_ct));
136 }
137 
138 /*
139  * The smb_vop_* functions have minimal knowledge of CIFS semantics and
140  * serve as an interface to the VFS layer.
141  *
142  * Only smb_fsop_* layer functions should call smb_vop_* layer functions.
143  * (Higher-level CIFS service code should never skip the smb_fsop_* layer
144  * to call smb_vop_* layer functions directly.)
145  */
146 
147 /*
148  * XXX - Extended attributes support in the file system assumed.
149  * This is needed for full NT Streams functionality.
150  */
151 
152 int
153 smb_vop_read(vnode_t *vp, uio_t *uiop, cred_t *cr)
154 {
155 	int error;
156 
157 	(void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, NULL);
158 	error = VOP_READ(vp, uiop, 0, cr, &smb_ct);
159 	VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, NULL);
160 	return (error);
161 }
162 
163 int
164 smb_vop_write(vnode_t *vp, uio_t *uiop, uint32_t *flag, uint32_t *lcount,
165     cred_t *cr)
166 {
167 	int error;
168 	int ioflag = 0;
169 
170 	*lcount = uiop->uio_resid;
171 
172 	if (*flag == FSSTAB_FILE_SYNC)
173 		ioflag = FSYNC;
174 
175 	uiop->uio_llimit = MAXOFFSET_T;
176 
177 	(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
178 	error = VOP_WRITE(vp, uiop, ioflag, cr, &smb_ct);
179 	VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
180 
181 	*lcount -= uiop->uio_resid;
182 
183 	return (error);
184 }
185 
186 /*
187  * smb_vop_getattr()
188  *
189  * smb_fsop_getattr()/smb_vop_getattr() should always be called from the CIFS
190  * service (instead of calling VOP_GETATTR directly) to retrieve attributes
191  * due to special processing needed for streams files.
192  *
193  * All attributes are retrieved.
194  *
195  * A named stream's attributes (as far as CIFS is concerned) are those of the
196  * unnamed (i.e. data) stream (minus the size attribute), and the size of the
197  * named stream.  Though the file system may store attributes other than size
198  * with the named stream, these should not be used by CIFS for any purpose.
199  *
200  * When vp denotes a named stream, then unnamed_vp should be passed in (denoting
201  * the corresponding unnamed stream).
202  */
203 
204 int
205 smb_vop_getattr(vnode_t *vp, vnode_t *unnamed_vp, smb_attr_t *ret_attr,
206     int flags, cred_t *cr)
207 {
208 	int error;
209 	vnode_t *use_vp;
210 	smb_attr_t tmp_attr;
211 	xvattr_t tmp_xvattr;
212 	xoptattr_t *xoap = NULL;
213 
214 	if (unnamed_vp)
215 		use_vp = unnamed_vp;
216 	else
217 		use_vp = vp;
218 
219 	if (vfs_has_feature(use_vp->v_vfsp, VFSFT_XVATTR)) {
220 		xva_init(&tmp_xvattr);
221 		xoap = xva_getxoptattr(&tmp_xvattr);
222 
223 		ASSERT(xoap);
224 
225 		smb_sa_to_va_mask(ret_attr->sa_mask,
226 		    &tmp_xvattr.xva_vattr.va_mask);
227 
228 		XVA_SET_REQ(&tmp_xvattr, XAT_READONLY);
229 		XVA_SET_REQ(&tmp_xvattr, XAT_HIDDEN);
230 		XVA_SET_REQ(&tmp_xvattr, XAT_SYSTEM);
231 		XVA_SET_REQ(&tmp_xvattr, XAT_ARCHIVE);
232 		XVA_SET_REQ(&tmp_xvattr, XAT_CREATETIME);
233 
234 		if ((error = VOP_GETATTR(use_vp, (vattr_t *)&tmp_xvattr, flags,
235 		    cr, &smb_ct)) != 0)
236 			return (error);
237 
238 		ret_attr->sa_vattr = tmp_xvattr.xva_vattr;
239 
240 		/*
241 		 * Copy special attributes to ret_attr parameter
242 		 */
243 
244 		ret_attr->sa_dosattr = 0;
245 
246 		ASSERT(tmp_xvattr.xva_vattr.va_mask & AT_XVATTR);
247 
248 		xoap = xva_getxoptattr(&tmp_xvattr);
249 		ASSERT(xoap);
250 
251 		if (XVA_ISSET_RTN(&tmp_xvattr, XAT_READONLY)) {
252 			if (xoap->xoa_readonly)
253 				ret_attr->sa_dosattr |= FILE_ATTRIBUTE_READONLY;
254 		}
255 
256 		if (XVA_ISSET_RTN(&tmp_xvattr, XAT_HIDDEN)) {
257 			if (xoap->xoa_hidden)
258 				ret_attr->sa_dosattr |= FILE_ATTRIBUTE_HIDDEN;
259 		}
260 
261 		if (XVA_ISSET_RTN(&tmp_xvattr, XAT_SYSTEM)) {
262 			if (xoap->xoa_system)
263 				ret_attr->sa_dosattr |= FILE_ATTRIBUTE_SYSTEM;
264 		}
265 
266 		if (XVA_ISSET_RTN(&tmp_xvattr, XAT_ARCHIVE)) {
267 			if (xoap->xoa_archive)
268 				ret_attr->sa_dosattr |= FILE_ATTRIBUTE_ARCHIVE;
269 		}
270 
271 		ret_attr->sa_crtime = xoap->xoa_createtime;
272 
273 		if (unnamed_vp && (ret_attr->sa_mask & SMB_AT_SIZE)) {
274 			/*
275 			 * Retrieve stream size attribute into temporary
276 			 * structure, in case the underlying file system
277 			 * returns attributes other than the size (we do not
278 			 * want to have ret_attr's other fields get
279 			 * overwritten).
280 			 *
281 			 * Note that vp is used here, and not use_vp.
282 			 * Also, only AT_SIZE is needed.
283 			 */
284 
285 			tmp_xvattr.xva_vattr.va_mask = AT_SIZE;
286 
287 			if ((error = VOP_GETATTR(vp, (vattr_t *)&tmp_xvattr,
288 			    flags, cr, &smb_ct)) != 0)
289 				return (error);
290 
291 			ret_attr->sa_vattr.va_size =
292 			    tmp_xvattr.xva_vattr.va_size;
293 
294 		}
295 
296 		if (ret_attr->sa_vattr.va_type == VDIR) {
297 			ret_attr->sa_dosattr |= FILE_ATTRIBUTE_DIRECTORY;
298 		}
299 
300 		return (error);
301 	}
302 
303 	/*
304 	 * Support for file systems without VFSFT_XVATTR
305 	 */
306 
307 	smb_sa_to_va_mask(ret_attr->sa_mask,
308 	    &ret_attr->sa_vattr.va_mask);
309 
310 	error = VOP_GETATTR(use_vp, &ret_attr->sa_vattr, flags, cr, &smb_ct);
311 
312 	if (error != 0)
313 		return (error);
314 
315 	/*
316 	 * "Fake" DOS attributes and create time, filesystem doesn't support
317 	 * them.
318 	 */
319 
320 	ret_attr->sa_dosattr = 0;
321 	ret_attr->sa_crtime = ret_attr->sa_vattr.va_ctime;
322 
323 	if (unnamed_vp && (ret_attr->sa_mask & SMB_AT_SIZE)) {
324 		/*
325 		 * Retrieve stream size attribute into temporary structure,
326 		 * in case the underlying file system returns attributes
327 		 * other than the size (we do not want to have ret_attr's
328 		 * other fields get overwritten).
329 		 *
330 		 * Note that vp is used here, and not use_vp.
331 		 * Also, only AT_SIZE is needed.
332 		 */
333 
334 		tmp_attr.sa_vattr.va_mask = AT_SIZE;
335 		error = VOP_GETATTR(vp, &tmp_attr.sa_vattr, flags, cr, &smb_ct);
336 
337 		if (error != 0)
338 			return (error);
339 
340 
341 		ret_attr->sa_vattr.va_size = tmp_attr.sa_vattr.va_size;
342 	}
343 
344 	if (ret_attr->sa_vattr.va_type == VDIR) {
345 		ret_attr->sa_dosattr |= FILE_ATTRIBUTE_DIRECTORY;
346 	}
347 
348 	return (error);
349 }
350 
351 /*
352  * smb_vop_setattr()
353  *
354  * smb_fsop_setattr()/smb_vop_setattr() should always be called from the CIFS
355  * service to set attributes due to special processing for streams files.
356  *
357  * When smb_vop_setattr() is called on a named stream file, all indicated
358  * attributes except the size are set on the unnamed stream file.  The size
359  * (if indicated) is set on the named stream file.
360  */
361 
362 int
363 smb_vop_setattr(vnode_t *vp, vnode_t *unnamed_vp, smb_attr_t *set_attr,
364     int flags, cred_t *cr, boolean_t no_xvattr)
365 {
366 	int error = 0;
367 	int at_size = 0;
368 	vnode_t *use_vp;
369 	xvattr_t xvattr;
370 	vattr_t *vap;
371 
372 	if (unnamed_vp) {
373 		use_vp = unnamed_vp;
374 		if (set_attr->sa_mask & SMB_AT_SIZE) {
375 			at_size = 1;
376 			set_attr->sa_mask &= ~SMB_AT_SIZE;
377 		}
378 	} else {
379 		use_vp = vp;
380 	}
381 
382 	/*
383 	 * The caller should not be setting sa_vattr.va_mask,
384 	 * but rather sa_mask.
385 	 */
386 
387 	set_attr->sa_vattr.va_mask = 0;
388 
389 	if ((no_xvattr == B_FALSE) &&
390 	    vfs_has_feature(use_vp->v_vfsp, VFSFT_XVATTR)) {
391 
392 		smb_vop_setup_xvattr(set_attr, &xvattr);
393 		vap = (vattr_t *)&xvattr;
394 	} else {
395 		smb_sa_to_va_mask(set_attr->sa_mask,
396 		    &set_attr->sa_vattr.va_mask);
397 		vap = &set_attr->sa_vattr;
398 	}
399 
400 	if ((error = VOP_SETATTR(use_vp, vap, flags, cr, &smb_ct)) != 0)
401 		return (error);
402 
403 	/*
404 	 * If the size of the stream needs to be set, set it on
405 	 * the stream file directly.  (All other indicated attributes
406 	 * are set on the stream's unnamed stream, above.)
407 	 */
408 
409 	if (at_size) {
410 		/*
411 		 * set_attr->sa_vattr.va_size already contains the
412 		 * size as set by the caller
413 		 *
414 		 * Note that vp is used here, and not use_vp.
415 		 * Also, only AT_SIZE is needed.
416 		 */
417 
418 		set_attr->sa_vattr.va_mask = AT_SIZE;
419 		error = VOP_SETATTR(vp, &set_attr->sa_vattr, flags, cr,
420 		    &smb_ct);
421 	}
422 
423 	return (error);
424 }
425 
426 /*
427  * smb_vop_access
428  *
429  * This is a wrapper round VOP_ACCESS. VOP_ACCESS checks the given mode
430  * against file's ACL or Unix permissions. CIFS on the other hand needs to
431  * know if the requested operation can succeed for the given object, this
432  * requires more checks in case of DELETE bit since permissions on the parent
433  * directory are important as well. Based on Windows rules if parent's ACL
434  * grant FILE_DELETE_CHILD a file can be delete regardless of the file's
435  * permissions.
436  */
437 int
438 smb_vop_access(vnode_t *vp, int mode, int flags, vnode_t *dir_vp, cred_t *cr)
439 {
440 	int error = 0;
441 
442 	if (mode == 0)
443 		return (0);
444 
445 	if ((flags == V_ACE_MASK) && (mode & ACE_DELETE)) {
446 		if (dir_vp) {
447 			error = VOP_ACCESS(dir_vp, ACE_DELETE_CHILD, flags,
448 			    cr, NULL);
449 
450 			if (error == 0)
451 				mode &= ~ACE_DELETE;
452 		}
453 	}
454 
455 	if (mode) {
456 		error = VOP_ACCESS(vp, mode, flags, cr, NULL);
457 	}
458 
459 	return (error);
460 }
461 
462 /*
463  * smb_vop_lookup
464  *
465  * dvp:		directory vnode (in)
466  * name:	name of file to be looked up (in)
467  * vpp:		looked-up vnode (out)
468  * od_name:	on-disk name of file (out).
469  *		This parameter is optional.  If a pointer is passed in, it
470  * 		must be allocated with MAXNAMELEN bytes
471  * rootvp:	vnode of the tree root (in)
472  *		This parameter is always passed in non-NULL except at the time
473  *		of share set up.
474  */
475 
476 int
477 smb_vop_lookup(vnode_t *dvp, char *name, vnode_t **vpp, char *od_name,
478     int flags, vnode_t *rootvp, cred_t *cr)
479 {
480 	int error = 0;
481 	int option_flags = 0;
482 	pathname_t rpn;
483 
484 	if (*name == '\0')
485 		return (EINVAL);
486 
487 	ASSERT(vpp);
488 	*vpp = NULL;
489 
490 	if ((name[0] == '.') && (name[1] == '.') && (name[2] == 0)) {
491 		if (rootvp && (dvp == rootvp)) {
492 			VN_HOLD(dvp);
493 			*vpp = dvp;
494 			return (0);
495 		}
496 
497 		if (dvp->v_flag & VROOT) {
498 			vfs_t *vfsp;
499 			vnode_t *cvp = dvp;
500 
501 			/*
502 			 * Set dvp and check for races with forced unmount
503 			 * (see lookuppnvp())
504 			 */
505 
506 			vfsp = cvp->v_vfsp;
507 			vfs_rlock_wait(vfsp);
508 			if (((dvp = cvp->v_vfsp->vfs_vnodecovered) == NULL) ||
509 			    (cvp->v_vfsp->vfs_flag & VFS_UNMOUNTED)) {
510 				vfs_unlock(vfsp);
511 				return (EIO);
512 			}
513 			vfs_unlock(vfsp);
514 		}
515 	}
516 
517 
518 
519 	if (flags & SMB_IGNORE_CASE)
520 		option_flags = FIGNORECASE;
521 
522 	pn_alloc(&rpn);
523 
524 	error = VOP_LOOKUP(dvp, name, vpp, NULL, option_flags, NULL, cr,
525 	    &smb_ct, NULL, &rpn);
526 
527 	if ((error == 0) && od_name) {
528 		bzero(od_name, MAXNAMELEN);
529 		if (option_flags == FIGNORECASE)
530 			(void) strlcpy(od_name, rpn.pn_buf, MAXNAMELEN);
531 		else
532 			(void) strlcpy(od_name, name, MAXNAMELEN);
533 	}
534 
535 	pn_free(&rpn);
536 	return (error);
537 }
538 
539 int
540 smb_vop_create(vnode_t *dvp, char *name, smb_attr_t *attr, vnode_t **vpp,
541     int flags, cred_t *cr, vsecattr_t *vsap)
542 {
543 	int error;
544 	int option_flags = 0;
545 	xvattr_t xvattr;
546 	vattr_t *vap;
547 
548 	if (flags & SMB_IGNORE_CASE)
549 		option_flags = FIGNORECASE;
550 
551 	attr->sa_vattr.va_mask = 0;
552 
553 	if (vfs_has_feature(dvp->v_vfsp, VFSFT_XVATTR)) {
554 		smb_vop_setup_xvattr(attr, &xvattr);
555 		vap = (vattr_t *)&xvattr;
556 	} else {
557 		smb_sa_to_va_mask(attr->sa_mask, &attr->sa_vattr.va_mask);
558 		vap = &attr->sa_vattr;
559 	}
560 
561 	error = VOP_CREATE(dvp, name, vap, EXCL, attr->sa_vattr.va_mode,
562 	    vpp, cr, option_flags, &smb_ct, vsap);
563 
564 	return (error);
565 }
566 
567 int
568 smb_vop_remove(vnode_t *dvp, char *name, int flags, cred_t *cr)
569 {
570 	int error;
571 	int option_flags = 0;
572 
573 	if (flags & SMB_IGNORE_CASE)
574 		option_flags = FIGNORECASE;
575 
576 	error = VOP_REMOVE(dvp, name, cr, &smb_ct, option_flags);
577 
578 	return (error);
579 }
580 
581 /*
582  * smb_vop_rename()
583  *
584  * The rename is for files in the same tree (identical TID) only.
585  */
586 
587 int
588 smb_vop_rename(vnode_t *from_dvp, char *from_name, vnode_t *to_dvp,
589     char *to_name, int flags, cred_t *cr)
590 {
591 	int error;
592 	int option_flags = 0;
593 
594 
595 	if (flags & SMB_IGNORE_CASE)
596 		option_flags = FIGNORECASE;
597 
598 	error = VOP_RENAME(from_dvp, from_name, to_dvp, to_name, cr,
599 	    &smb_ct, option_flags);
600 
601 	return (error);
602 }
603 
604 int
605 smb_vop_mkdir(vnode_t *dvp, char *name, smb_attr_t *attr, vnode_t **vpp,
606     int flags, cred_t *cr, vsecattr_t *vsap)
607 {
608 	int error;
609 	int option_flags = 0;
610 
611 
612 
613 	if (flags & SMB_IGNORE_CASE)
614 		option_flags = FIGNORECASE;
615 
616 	smb_sa_to_va_mask(attr->sa_mask, &attr->sa_vattr.va_mask);
617 
618 	error = VOP_MKDIR(dvp, name, &attr->sa_vattr, vpp, cr, &smb_ct,
619 	    option_flags, vsap);
620 
621 	return (error);
622 }
623 
624 /*
625  * smb_vop_rmdir()
626  *
627  * Only simple rmdir supported, consistent with NT semantics
628  * (can only remove an empty directory).
629  *
630  */
631 
632 int
633 smb_vop_rmdir(vnode_t *dvp, char *name, int flags, cred_t *cr)
634 {
635 	int error;
636 	int option_flags = 0;
637 
638 	if (flags & SMB_IGNORE_CASE)
639 		option_flags = FIGNORECASE;
640 
641 	/*
642 	 * Comments adapted from rfs_rmdir().
643 	 *
644 	 * VOP_RMDIR now takes a new third argument (the current
645 	 * directory of the process).  That's because rmdir
646 	 * wants to return EINVAL if one tries to remove ".".
647 	 * Of course, SMB servers do not know what their
648 	 * clients' current directories are.  We fake it by
649 	 * supplying a vnode known to exist and illegal to
650 	 * remove.
651 	 */
652 
653 	error = VOP_RMDIR(dvp, name, rootdir, cr, &smb_ct, option_flags);
654 	return (error);
655 }
656 
657 int
658 smb_vop_commit(vnode_t *vp, cred_t *cr)
659 {
660 	return (VOP_FSYNC(vp, 1, cr, &smb_ct));
661 }
662 
663 void
664 smb_vop_setup_xvattr(smb_attr_t *smb_attr, xvattr_t *xvattr)
665 {
666 	xoptattr_t *xoap = NULL;
667 	uint_t xva_mask;
668 
669 	/*
670 	 * Initialize xvattr, including bzero
671 	 */
672 	xva_init(xvattr);
673 	xoap = xva_getxoptattr(xvattr);
674 
675 	ASSERT(xoap);
676 
677 	/*
678 	 * Copy caller-specified classic attributes to xvattr.
679 	 * First save xvattr's mask (set in xva_init()), which
680 	 * contains AT_XVATTR.  This is |'d in later if needed.
681 	 */
682 
683 	xva_mask = xvattr->xva_vattr.va_mask;
684 	xvattr->xva_vattr = smb_attr->sa_vattr;
685 
686 	smb_sa_to_va_mask(smb_attr->sa_mask, &xvattr->xva_vattr.va_mask);
687 
688 	/*
689 	 * Do not set ctime (only the file system can do it)
690 	 */
691 
692 	xvattr->xva_vattr.va_mask &= ~AT_CTIME;
693 
694 	if (smb_attr->sa_mask & SMB_AT_DOSATTR) {
695 
696 		/*
697 		 * "|" in the original xva_mask, which contains
698 		 * AT_XVATTR
699 		 */
700 
701 		xvattr->xva_vattr.va_mask |= xva_mask;
702 
703 		XVA_SET_REQ(xvattr, XAT_ARCHIVE);
704 		XVA_SET_REQ(xvattr, XAT_SYSTEM);
705 		XVA_SET_REQ(xvattr, XAT_READONLY);
706 		XVA_SET_REQ(xvattr, XAT_HIDDEN);
707 
708 		/*
709 		 * smb_attr->sa_dosattr: If a given bit is not set,
710 		 * that indicates that the corresponding field needs
711 		 * to be updated with a "0" value.  This is done
712 		 * implicitly as the xoap->xoa_* fields were bzero'd.
713 		 */
714 
715 		if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_ARCHIVE)
716 			xoap->xoa_archive = 1;
717 
718 		if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_SYSTEM)
719 			xoap->xoa_system = 1;
720 
721 		if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_READONLY)
722 			xoap->xoa_readonly = 1;
723 
724 		if (smb_attr->sa_dosattr & FILE_ATTRIBUTE_HIDDEN)
725 			xoap->xoa_hidden = 1;
726 	}
727 
728 	if (smb_attr->sa_mask & SMB_AT_CRTIME) {
729 		/*
730 		 * "|" in the original xva_mask, which contains
731 		 * AT_XVATTR
732 		 */
733 
734 		xvattr->xva_vattr.va_mask |= xva_mask;
735 		XVA_SET_REQ(xvattr, XAT_CREATETIME);
736 		xoap->xoa_createtime = smb_attr->sa_crtime;
737 	}
738 }
739 
740 
741 /*
742  * smb_vop_readdir()
743  *
744  * Upon return, the "name" field will contain either the on-disk name or, if
745  * it needs mangling or has a case-insensitive collision, the mangled
746  * "shortname."
747  *
748  * vpp is an optional parameter.  If non-NULL, it will contain a pointer to
749  * the vnode for the name that is looked up (the vnode will be returned held).
750  *
751  * od_name is an optional parameter (NULL can be passed if the on-disk name
752  * is not needed by the caller).
753  */
754 
755 int
756 smb_vop_readdir(vnode_t *dvp, uint32_t *cookiep, char *name, int *namelen,
757     ino64_t *inop, vnode_t **vpp, char *od_name, int flags, cred_t *cr)
758 {
759 	int num_bytes;
760 	int error = 0;
761 	char *dirbuf = NULL;
762 
763 	ASSERT(dvp);
764 	ASSERT(cookiep);
765 	ASSERT(name);
766 	ASSERT(namelen);
767 	ASSERT(inop);
768 	ASSERT(cr);
769 
770 	if (dvp->v_type != VDIR) {
771 		*namelen = 0;
772 		return (ENOTDIR);
773 	}
774 
775 	if (vpp)
776 		*vpp = NULL;
777 
778 	dirbuf = kmem_zalloc(SMB_MINLEN_RDDIR_BUF, KM_SLEEP);
779 	num_bytes = SMB_MINLEN_RDDIR_BUF;
780 
781 	/*
782 	 * The goal is to retrieve the first valid entry from *cookiep
783 	 * forward.  smb_vop_readdir_readpage() collects an
784 	 * SMB_MINLEN_RDDIR_BUF-size "page" of directory entry information.
785 	 * smb_vop_readdir_entry() attempts to find the first valid entry
786 	 * in that page.
787 	 */
788 
789 	while ((error = smb_vop_readdir_readpage(dvp, dirbuf, *cookiep,
790 	    &num_bytes, cr, flags)) == 0) {
791 
792 		if (num_bytes <= 0)
793 			break;
794 
795 		name[0] = '\0';
796 
797 		error = smb_vop_readdir_entry(dvp, cookiep, name, namelen,
798 		    inop, vpp, od_name, flags, cr, dirbuf,
799 		    num_bytes);
800 
801 		if (error)
802 			break;
803 
804 		if (*name)
805 			break;
806 
807 		bzero(dirbuf, SMB_MINLEN_RDDIR_BUF);
808 		num_bytes = SMB_MINLEN_RDDIR_BUF;
809 	}
810 
811 
812 	if (error) {
813 		kmem_free(dirbuf, SMB_MINLEN_RDDIR_BUF);
814 		*namelen = 0;
815 		return (error);
816 	}
817 
818 	if (num_bytes == 0) { /* EOF */
819 		kmem_free(dirbuf, SMB_MINLEN_RDDIR_BUF);
820 		*cookiep = SMB_EOF;
821 		*namelen = 0;
822 		return (0);
823 	}
824 
825 	kmem_free(dirbuf, SMB_MINLEN_RDDIR_BUF);
826 	return (0);
827 }
828 
829 /*
830  * smb_vop_readdir_readpage()
831  *
832  * Collects an SMB_MINLEN_RDDIR_BUF "page" of directory entries.  (The
833  * directory entries are returned in an fs-independent format by the
834  * underlying file system.  That is, the "page" of information returned is
835  * not literally stored on-disk in the format returned.)
836  *
837  * Much of the following is borrowed from getdents64()
838  *
839  * MAXGETDENTS_SIZE is defined in getdents.c
840  */
841 
842 #define	MAXGETDENTS_SIZE	(64 * 1024)
843 
844 static int
845 smb_vop_readdir_readpage(vnode_t *vp, void *buf, uint32_t offset, int *count,
846     cred_t *cr, int flags)
847 {
848 	int error = 0;
849 	int rdirent_flags = 0;
850 	int sink;
851 	struct uio auio;
852 	struct iovec aiov;
853 
854 	if (vp->v_type != VDIR)
855 		return (ENOTDIR);
856 
857 	/* entflags not working for streams so don't try to use them */
858 	if (!(flags & SMB_STREAM_RDDIR) &&
859 	    (vfs_has_feature(vp->v_vfsp, VFSFT_DIRENTFLAGS))) {
860 		/*
861 		 * Setting V_RDDIR_ENTFLAGS will cause the buffer to
862 		 * be filled with edirent_t structures (instead of
863 		 * dirent64_t structures).
864 		 */
865 		rdirent_flags = V_RDDIR_ENTFLAGS;
866 
867 		if (*count < sizeof (edirent_t))
868 			return (EINVAL);
869 	} else {
870 		if (*count < sizeof (dirent64_t))
871 			return (EINVAL);
872 	}
873 
874 	if (*count > MAXGETDENTS_SIZE)
875 		*count = MAXGETDENTS_SIZE;
876 
877 	aiov.iov_base = buf;
878 	aiov.iov_len = *count;
879 	auio.uio_iov = &aiov;
880 	auio.uio_iovcnt = 1;
881 	auio.uio_loffset = (uint64_t)offset;
882 	auio.uio_segflg = UIO_SYSSPACE;
883 	auio.uio_resid = *count;
884 	auio.uio_fmode = 0;
885 
886 	(void) VOP_RWLOCK(vp, V_WRITELOCK_FALSE, NULL);
887 	error = VOP_READDIR(vp, &auio, cr, &sink, &smb_ct, rdirent_flags);
888 	VOP_RWUNLOCK(vp, V_WRITELOCK_FALSE, NULL);
889 
890 	if (error) {
891 		if (error == ENOENT) {
892 			/* Fake EOF if offset is bad due to dropping of lock */
893 			*count = 0;
894 			return (0);
895 		} else {
896 			return (error);
897 		}
898 	}
899 
900 	/*
901 	 * Windows cannot handle an offset > SMB_EOF.
902 	 * Pretend we are at EOF.
903 	 */
904 
905 	if (auio.uio_loffset > SMB_EOF) {
906 		*count = 0;
907 		return (0);
908 	}
909 
910 	*count = *count - auio.uio_resid;
911 	return (0);
912 }
913 
914 /*
915  * smb_vop_readdir_entry()
916  *
917  * This function retrieves the first valid entry from the
918  * SMB_MINLEN_RDDIR_BUF-sized buffer returned by smb_vop_readdir_readpage()
919  * to smb_vop_readdir().
920  *
921  * Both dirent64_t and edirent_t structures need to be handled.  The former is
922  * needed for file systems that do not support VFSFT_DIRENTFLAGS.  The latter
923  * is required for proper handling of case collisions on file systems that
924  * support case-insensitivity.  edirent_t structures are also used for
925  * case-sensitive file systems if VFSFT_DIRENTFLAGS is supported.
926  */
927 
928 static int
929 smb_vop_readdir_entry(vnode_t *dvp, uint32_t *cookiep, char *name, int *namelen,
930     ino64_t *inop, vnode_t **vpp, char *od_name, int flags, cred_t *cr,
931     char *dirbuf, int num_bytes)
932 {
933 	uint32_t next_cookie;
934 	int ebufsize;
935 	int error = 0;
936 	int len;
937 	int rc;
938 	char shortname[MANGLE_NAMELEN];
939 	char name83[MANGLE_NAMELEN];
940 	char *ebuf = NULL;
941 	edirent_t *edp;
942 	dirent64_t *dp = NULL;
943 	vnode_t *vp = NULL;
944 
945 	ASSERT(dirbuf);
946 
947 	/*
948 	 * Use edirent_t structure for both
949 	 * entflags not working for streams so don't try to use them
950 	 */
951 	if (!(flags & SMB_STREAM_RDDIR) &&
952 	    (vfs_has_feature(dvp->v_vfsp, VFSFT_DIRENTFLAGS))) {
953 		/*LINTED E_BAD_PTR_CAST_ALIGN*/
954 		edp = (edirent_t *)dirbuf;
955 	} else {
956 		/*LINTED E_BAD_PTR_CAST_ALIGN*/
957 		dp = (dirent64_t *)dirbuf;
958 		ebufsize = EDIRENT_RECLEN(MAXNAMELEN);
959 		ebuf = kmem_zalloc(ebufsize, KM_SLEEP);
960 		/*LINTED E_BAD_PTR_CAST_ALIGN*/
961 		edp = (edirent_t *)ebuf;
962 	}
963 
964 	while (edp) {
965 		if (dp)
966 			DP_TO_EDP(dp, edp);
967 
968 		next_cookie = (uint32_t)edp->ed_off;
969 		if (edp->ed_ino == 0) {
970 			*cookiep = next_cookie;
971 
972 			if (dp) {
973 				/*LINTED E_BAD_PTR_CAST_ALIGN*/
974 				DP_ADVANCE(dp, dirbuf, num_bytes);
975 				if (dp == NULL)
976 					edp = NULL;
977 			} else {
978 				/*LINTED E_BAD_PTR_CAST_ALIGN*/
979 				EDP_ADVANCE(edp, dirbuf, num_bytes);
980 			}
981 			continue;
982 		}
983 
984 		len = strlen(edp->ed_name);
985 
986 		if (*namelen < len) {
987 			*namelen = 0;
988 
989 			if (ebuf)
990 				kmem_free(ebuf, ebufsize);
991 
992 			return (EOVERFLOW);
993 		}
994 
995 		/*
996 		 * Do not pass SMB_IGNORE_CASE to smb_vop_lookup
997 		 */
998 
999 		error = smb_vop_lookup(dvp, edp->ed_name, vpp ? vpp : &vp,
1000 		    od_name, 0, NULL, cr);
1001 
1002 		if (error) {
1003 			if (error == ENOENT) {
1004 				*cookiep = (uint32_t)next_cookie;
1005 
1006 				if (dp) {
1007 					/*LINTED E_BAD_PTR_CAST_ALIGN*/
1008 					DP_ADVANCE(dp, dirbuf, num_bytes);
1009 					if (dp == NULL)
1010 						edp = NULL;
1011 				} else {
1012 					/*LINTED E_BAD_PTR_CAST_ALIGN*/
1013 					EDP_ADVANCE(edp, dirbuf, num_bytes);
1014 				}
1015 				continue;
1016 			}
1017 
1018 
1019 			*namelen = 0;
1020 
1021 			if (ebuf)
1022 				kmem_free(ebuf, ebufsize);
1023 
1024 			return (error);
1025 		}
1026 
1027 		if ((flags & SMB_IGNORE_CASE) && ED_CASE_CONFLICTS(edp)) {
1028 			rc = smb_mangle_name(edp->ed_ino, edp->ed_name,
1029 			    shortname, name83, 1);
1030 
1031 			if (rc == 1) { /* success */
1032 				(void) strlcpy(name, shortname, *namelen + 1);
1033 				*namelen = strlen(shortname);
1034 			} else {
1035 				(void) strlcpy(name, edp->ed_name,
1036 				    *namelen + 1);
1037 				name[*namelen] = '\0';
1038 			}
1039 
1040 		} else {
1041 			(void) strlcpy(name, edp->ed_name, *namelen + 1);
1042 				*namelen = len;
1043 		}
1044 
1045 		if (vpp == NULL)
1046 			VN_RELE(vp);
1047 
1048 		if (inop)
1049 			*inop = edp->ed_ino;
1050 
1051 		*cookiep = (uint32_t)next_cookie;
1052 		break;
1053 	}
1054 
1055 	if (ebuf)
1056 		kmem_free(ebuf, ebufsize);
1057 
1058 	return (error);
1059 }
1060 
1061 /*
1062  * smb_sa_to_va_mask
1063  *
1064  * Set va_mask by running through the SMB_AT_* #define's and
1065  * setting those bits that correspond to the SMB_AT_* bits
1066  * set in sa_mask.
1067  */
1068 
1069 void
1070 smb_sa_to_va_mask(uint_t sa_mask, uint_t *va_maskp)
1071 {
1072 	int i;
1073 	uint_t smask;
1074 
1075 	smask = (sa_mask);
1076 	for (i = SMB_AT_TYPE; (i < SMB_AT_MAX) && (smask != 0); ++i) {
1077 		if (smask & 1)
1078 			*(va_maskp) |= smb_attrmap[i];
1079 
1080 		smask >>= 1;
1081 	}
1082 }
1083 
1084 /*
1085  * smb_vop_getdents()
1086  *
1087  * Upon success, the smb_node corresponding to each entry returned will
1088  * have a reference taken on it.  These will be released in
1089  * smb_trans2_find_get_dents().
1090  *
1091  * If an error is returned from this routine, a list of already processed
1092  * entries will be returned.  The smb_nodes corresponding to these entries
1093  * will be referenced, and will be released in smb_trans2_find_get_dents().
1094  *
1095  * The returned dp->d_name field will contain either the on-disk name or, if
1096  * it needs mangling or has a case-insensitive collision, the mangled
1097  * "shortname."  In this case, the on-disk name can be retrieved from the
1098  * smb_node's od_name (the smb_node is passed to smb_gather_dents_info()).
1099  */
1100 
1101 int /*ARGSUSED*/
1102 smb_vop_getdents(
1103     smb_node_t		*dir_snode,
1104     uint32_t		*cookiep,
1105     uint64_t		*verifierp,
1106     int32_t		*dircountp,
1107     char		*arg,
1108     char		*pattern,
1109     uint32_t		flags,
1110     smb_request_t	*sr,
1111     cred_t		*cr)
1112 {
1113 	int		error = 0;
1114 	int		maxentries;
1115 	int		num_bytes;
1116 	int		resid;
1117 	char		*dirbuf = NULL;
1118 	vnode_t		*dvp;
1119 	/*LINTED E_BAD_PTR_CAST_ALIGN*/
1120 	smb_dent_info_hdr_t *ihdr = (smb_dent_info_hdr_t *)arg;
1121 
1122 	dvp = dir_snode->vp;
1123 
1124 	resid = ihdr->uio.uio_resid;
1125 	maxentries = resid / SMB_MAX_DENT_INFO_SIZE;
1126 
1127 	bzero(ihdr->iov->iov_base, resid);
1128 
1129 	dirbuf = kmem_alloc(SMB_MINLEN_RDDIR_BUF, KM_SLEEP);
1130 
1131 	while (maxentries) {
1132 
1133 		bzero(dirbuf, SMB_MINLEN_RDDIR_BUF);
1134 
1135 		num_bytes = SMB_MINLEN_RDDIR_BUF;
1136 		error = smb_vop_readdir_readpage(dvp, dirbuf, *cookiep,
1137 		    &num_bytes, cr, flags);
1138 
1139 		if (error || (num_bytes <= 0))
1140 			break;
1141 
1142 		error = smb_vop_getdents_entries(dir_snode, cookiep, dircountp,
1143 		    arg, flags, sr, cr, dirbuf, &maxentries, num_bytes,
1144 		    pattern);
1145 
1146 		if (error)
1147 			goto out;
1148 	}
1149 
1150 	if (num_bytes < 0) {
1151 		error = -1;
1152 	} else if (num_bytes == 0) {
1153 		*cookiep = SMB_EOF;
1154 		error = 0;
1155 	} else {
1156 		error = 0;
1157 	}
1158 
1159 out:
1160 	if (dirbuf)
1161 		kmem_free(dirbuf, SMB_MINLEN_RDDIR_BUF);
1162 
1163 	return (error);
1164 }
1165 
1166 /*
1167  * smb_vop_getdents_entries()
1168  *
1169  * This function retrieves names from the SMB_MINLEN_RDDIR_BUF-sized buffer
1170  * returned by smb_vop_readdir_readpage() to smb_vop_getdents().
1171  *
1172  * Both dirent64_t and edirent_t structures need to be handled.  The former is
1173  * needed for file systems that do not support VFSFT_DIRENTFLAGS.  The latter
1174  * is required for properly handling case collisions on file systems that
1175  * support case-insensitivity.  edirent_t is also used on case-sensitive
1176  * file systems where VFSFT_DIRENTFLAGS is available.
1177  */
1178 
1179 static int
1180 smb_vop_getdents_entries(
1181     smb_node_t		*dir_snode,
1182     uint32_t		*cookiep,
1183     int32_t		*dircountp,
1184     char		*arg,
1185     uint32_t		flags,
1186     struct smb_request	*sr,
1187     cred_t		*cr,
1188     char		*dirbuf,
1189     int			*maxentries,
1190     int			num_bytes,
1191     char		*pattern)
1192 {
1193 	uint32_t	next_cookie;
1194 	int		ebufsize;
1195 	char		*tmp_name;
1196 	int		error;
1197 	int		rc;
1198 	char		shortname[MANGLE_NAMELEN];
1199 	char		name83[MANGLE_NAMELEN];
1200 	char		*ebuf = NULL;
1201 	dirent64_t	*dp = NULL;
1202 	edirent_t	*edp;
1203 	smb_node_t	*ret_snode;
1204 	smb_attr_t	ret_attr;
1205 	vnode_t		*dvp;
1206 	vnode_t		*fvp;
1207 
1208 	ASSERT(dirbuf);
1209 
1210 	dvp = dir_snode->vp;
1211 
1212 	if (vfs_has_feature(dvp->v_vfsp, VFSFT_DIRENTFLAGS)) {
1213 		/*LINTED E_BAD_PTR_CAST_ALIGN*/
1214 		edp = (edirent_t *)dirbuf;
1215 	} else {
1216 		/*LINTED E_BAD_PTR_CAST_ALIGN*/
1217 		dp = (dirent64_t *)dirbuf;
1218 		ebufsize = EDIRENT_RECLEN(MAXNAMELEN);
1219 		ebuf = kmem_zalloc(ebufsize, KM_SLEEP);
1220 		/*LINTED E_BAD_PTR_CAST_ALIGN*/
1221 		edp = (edirent_t *)ebuf;
1222 	}
1223 
1224 	while (edp) {
1225 		if (dp)
1226 			DP_TO_EDP(dp, edp);
1227 
1228 		if (*maxentries == 0)
1229 			break;
1230 
1231 		next_cookie = (uint32_t)edp->ed_off;
1232 
1233 		if (edp->ed_ino == 0) {
1234 			*cookiep = next_cookie;
1235 			if (dp) {
1236 				/*LINTED E_BAD_PTR_CAST_ALIGN*/
1237 				DP_ADVANCE(dp, dirbuf, num_bytes);
1238 				if (dp == NULL)
1239 					edp = NULL;
1240 			} else {
1241 				/*LINTED E_BAD_PTR_CAST_ALIGN*/
1242 				EDP_ADVANCE(edp, dirbuf, num_bytes);
1243 			}
1244 			continue;
1245 		}
1246 
1247 		error = smb_vop_lookup(dvp, edp->ed_name, &fvp,
1248 		    NULL, 0, NULL, cr);
1249 
1250 		if (error) {
1251 			if (error == ENOENT) {
1252 				*cookiep = next_cookie;
1253 				if (dp) {
1254 					/*LINTED E_BAD_PTR_CAST_ALIGN*/
1255 					DP_ADVANCE(dp, dirbuf,
1256 					    num_bytes);
1257 					if (dp == NULL)
1258 						edp = NULL;
1259 				} else {
1260 					/*LINTED E_BAD_PTR_CAST_ALIGN*/
1261 					EDP_ADVANCE(edp, dirbuf,
1262 					    num_bytes);
1263 				}
1264 				continue;
1265 			}
1266 			if (ebuf)
1267 				kmem_free(ebuf, ebufsize);
1268 
1269 			return (error);
1270 		}
1271 
1272 		ret_snode = smb_node_lookup(sr, NULL, cr, fvp,
1273 		    edp->ed_name, dir_snode, NULL, &ret_attr);
1274 
1275 		if (ret_snode == NULL) {
1276 			VN_RELE(fvp);
1277 
1278 			if (ebuf)
1279 				kmem_free(ebuf, ebufsize);
1280 
1281 			return (ENOMEM);
1282 		}
1283 
1284 		if (smb_match_name(edp->ed_ino, edp->ed_name, shortname,
1285 		    name83, pattern, (flags & SMB_IGNORE_CASE))) {
1286 
1287 			tmp_name = edp->ed_name;
1288 
1289 			if ((flags & SMB_IGNORE_CASE) &&
1290 			    ED_CASE_CONFLICTS(edp)) {
1291 				rc = smb_mangle_name(edp->ed_ino, edp->ed_name,
1292 				    shortname, name83, 1);
1293 				if (rc == 1)
1294 					tmp_name = shortname;
1295 			} else {
1296 				rc = smb_mangle_name(edp->ed_ino, edp->ed_name,
1297 				    shortname, name83, 0);
1298 			}
1299 
1300 			if (rc != 1) {
1301 				(void) strlcpy(shortname, edp->ed_name,
1302 				    MANGLE_NAMELEN);
1303 				(void) strlcpy(name83, edp->ed_name,
1304 				    MANGLE_NAMELEN);
1305 				shortname[MANGLE_NAMELEN - 1] = '\0';
1306 				name83[MANGLE_NAMELEN - 1] = '\0';
1307 			}
1308 
1309 			error = smb_gather_dents_info(arg, edp->ed_ino,
1310 			    strlen(tmp_name), tmp_name, next_cookie, dircountp,
1311 			    &ret_attr, ret_snode, shortname, name83);
1312 
1313 			if (error > 0) {
1314 				if (ebuf)
1315 					kmem_free(ebuf, ebufsize);
1316 				return (error);
1317 			}
1318 
1319 			/*
1320 			 * Treat errors from smb_gather_dents_info() that are
1321 			 * < 0 the same as EOF.
1322 			 */
1323 			if (error < 0) {
1324 				if (ebuf)
1325 					kmem_free(ebuf, ebufsize);
1326 				*maxentries = 0;
1327 				return (0);
1328 			}
1329 			(*maxentries)--;
1330 		} else {
1331 			smb_node_release(ret_snode);
1332 		}
1333 
1334 		*cookiep = next_cookie;
1335 
1336 		if (dp) {
1337 			/*LINTED E_BAD_PTR_CAST_ALIGN*/
1338 			DP_ADVANCE(dp, dirbuf, num_bytes);
1339 			if (dp == NULL)
1340 				edp = NULL;
1341 		} else {
1342 			/*LINTED E_BAD_PTR_CAST_ALIGN*/
1343 			EDP_ADVANCE(edp, dirbuf, num_bytes);
1344 		}
1345 	}
1346 
1347 	if (ebuf)
1348 		kmem_free(ebuf, ebufsize);
1349 
1350 	return (0);
1351 }
1352 
1353 /*
1354  * smb_vop_stream_lookup()
1355  *
1356  * The name returned in od_name is the on-disk name of the stream with the
1357  * SMB_STREAM_PREFIX stripped off.  od_name should be allocated to MAXNAMELEN
1358  * by the caller.
1359  */
1360 
1361 int
1362 smb_vop_stream_lookup(vnode_t *fvp, char *stream_name, vnode_t **vpp,
1363     char *od_name, vnode_t **xattrdirvpp, int flags, vnode_t *rootvp,
1364     cred_t *cr)
1365 {
1366 	char *solaris_stream_name;
1367 	char *name;
1368 	int error;
1369 
1370 	if ((error = smb_vop_lookup_xattrdir(fvp, xattrdirvpp,
1371 	    LOOKUP_XATTR | CREATE_XATTR_DIR, cr)) != 0)
1372 		return (error);
1373 
1374 	/*
1375 	 * Prepend SMB_STREAM_PREFIX to stream name
1376 	 */
1377 
1378 	solaris_stream_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1379 	(void) sprintf(solaris_stream_name, "%s%s", SMB_STREAM_PREFIX,
1380 	    stream_name);
1381 
1382 	/*
1383 	 * "name" will hold the on-disk name returned from smb_vop_lookup
1384 	 * for the stream, including the SMB_STREAM_PREFIX.
1385 	 */
1386 
1387 	name = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
1388 
1389 	if ((error = smb_vop_lookup(*xattrdirvpp, solaris_stream_name, vpp,
1390 	    name, flags, rootvp, cr)) != 0) {
1391 		VN_RELE(*xattrdirvpp);
1392 	} else {
1393 		(void) strlcpy(od_name, &(name[SMB_STREAM_PREFIX_LEN]),
1394 		    MAXNAMELEN);
1395 	}
1396 
1397 	kmem_free(solaris_stream_name, MAXNAMELEN);
1398 	kmem_free(name, MAXNAMELEN);
1399 
1400 	return (error);
1401 }
1402 
1403 int
1404 smb_vop_stream_create(vnode_t *fvp, char *stream_name, smb_attr_t *attr,
1405     vnode_t **vpp, vnode_t **xattrdirvpp, int flags, cred_t *cr)
1406 {
1407 	char *solaris_stream_name;
1408 	int error;
1409 
1410 	if ((error = smb_vop_lookup_xattrdir(fvp, xattrdirvpp,
1411 	    LOOKUP_XATTR | CREATE_XATTR_DIR, cr)) != 0)
1412 		return (error);
1413 
1414 	/*
1415 	 * Prepend SMB_STREAM_PREFIX to stream name
1416 	 */
1417 
1418 	solaris_stream_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1419 	(void) sprintf(solaris_stream_name, "%s%s", SMB_STREAM_PREFIX,
1420 	    stream_name);
1421 
1422 	if ((error = smb_vop_create(*xattrdirvpp, solaris_stream_name, attr,
1423 	    vpp, flags, cr, NULL)) != 0)
1424 		VN_RELE(*xattrdirvpp);
1425 
1426 	kmem_free(solaris_stream_name, MAXNAMELEN);
1427 
1428 	return (error);
1429 }
1430 
1431 int
1432 smb_vop_stream_remove(vnode_t *vp, char *stream_name, int flags, cred_t *cr)
1433 {
1434 	char *solaris_stream_name;
1435 	vnode_t *xattrdirvp;
1436 	int error;
1437 
1438 	if ((error = smb_vop_lookup_xattrdir(vp, &xattrdirvp, LOOKUP_XATTR, cr))
1439 	    != 0)
1440 		return (error);
1441 
1442 	/*
1443 	 * Prepend SMB_STREAM_PREFIX to stream name
1444 	 */
1445 
1446 	solaris_stream_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
1447 	(void) sprintf(solaris_stream_name, "%s%s", SMB_STREAM_PREFIX,
1448 	    stream_name);
1449 
1450 	/* XXX might have to use kcred */
1451 	error = smb_vop_remove(xattrdirvp, solaris_stream_name, flags, cr);
1452 
1453 	kmem_free(solaris_stream_name, MAXNAMELEN);
1454 
1455 	return (error);
1456 }
1457 
1458 /*
1459  * smb_vop_stream_readdir()
1460  *
1461  * Note: stream_info.size is not filled in in this routine.
1462  * It needs to be filled in by the caller due to the parameters for getattr.
1463  *
1464  * stream_info.name is set to the on-disk stream name with the SMB_STREAM_PREFIX
1465  * removed.
1466  */
1467 
1468 int
1469 smb_vop_stream_readdir(vnode_t *fvp, uint32_t *cookiep,
1470     struct fs_stream_info *stream_info, vnode_t **vpp, vnode_t **xattrdirvpp,
1471     int flags, cred_t *cr)
1472 {
1473 	int nsize = MAXNAMELEN-1;
1474 	int error = 0;
1475 	ino64_t ino;
1476 	char *tmp_name;
1477 	vnode_t *xattrdirvp;
1478 	vnode_t *vp;
1479 
1480 	if ((error = smb_vop_lookup_xattrdir(fvp, &xattrdirvp, LOOKUP_XATTR,
1481 	    cr)) != 0)
1482 		return (error);
1483 
1484 	bzero(stream_info->name, sizeof (stream_info->name));
1485 	stream_info->size = 0;
1486 
1487 	tmp_name = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
1488 
1489 	for (;;) {
1490 		error = smb_vop_readdir(xattrdirvp, cookiep, tmp_name, &nsize,
1491 		    &ino, &vp, NULL, flags | SMB_STREAM_RDDIR, cr);
1492 
1493 		if (error || (*cookiep == SMB_EOF))
1494 			break;
1495 
1496 		if (strncmp(tmp_name, SMB_STREAM_PREFIX,
1497 		    SMB_STREAM_PREFIX_LEN)) {
1498 			VN_RELE(vp);
1499 			continue;
1500 		}
1501 
1502 		tmp_name[nsize] = '\0';
1503 		(void) strlcpy(stream_info->name,
1504 		    &(tmp_name[SMB_STREAM_PREFIX_LEN]),
1505 		    sizeof (stream_info->name));
1506 
1507 		nsize -= SMB_STREAM_PREFIX_LEN;
1508 		break;
1509 	}
1510 
1511 	if ((error == 0) && nsize) {
1512 		if (vpp)
1513 			*vpp = vp;
1514 		else
1515 			VN_RELE(vp);
1516 
1517 		if (xattrdirvpp)
1518 			*xattrdirvpp = xattrdirvp;
1519 		else
1520 			VN_RELE(xattrdirvp);
1521 
1522 	} else {
1523 		VN_RELE(xattrdirvp);
1524 	}
1525 
1526 	kmem_free(tmp_name, MAXNAMELEN);
1527 
1528 	return (error);
1529 }
1530 
1531 int
1532 smb_vop_lookup_xattrdir(vnode_t *fvp, vnode_t **xattrdirvpp, int flags,
1533     cred_t *cr)
1534 {
1535 	int error;
1536 
1537 	error = VOP_LOOKUP(fvp, "", xattrdirvpp, NULL, flags, NULL, cr,
1538 	    &smb_ct, NULL, NULL);
1539 	return (error);
1540 }
1541 
1542 /*
1543  * smb_vop_traverse_check()
1544  *
1545  * This function checks to see if the passed-in vnode has a file system
1546  * mounted on it.  If it does, the mount point is "traversed" and the
1547  * vnode for the root of the file system is returned.
1548  */
1549 
1550 int
1551 smb_vop_traverse_check(vnode_t **vpp)
1552 {
1553 	int error;
1554 
1555 	if (vn_mountedvfs(*vpp) == 0)
1556 		return (0);
1557 
1558 	/*
1559 	 * traverse() may return a different held vnode, even in the error case.
1560 	 * If it returns a different vnode, it will have released the original.
1561 	 */
1562 
1563 	error = traverse(vpp);
1564 
1565 	return (error);
1566 }
1567 
1568 int /*ARGSUSED*/
1569 smb_vop_statfs(vnode_t *vp, struct statvfs64 *statp, cred_t *cr)
1570 {
1571 	int error;
1572 
1573 	error = VFS_STATVFS(vp->v_vfsp, statp);
1574 
1575 	return (error);
1576 }
1577 
1578 /*
1579  * smb_vop_acl_read
1580  *
1581  * Reads the ACL of the specified file into 'aclp'.
1582  * acl_type is the type of ACL which the filesystem supports.
1583  *
1584  * Caller has to free the allocated memory for aclp by calling
1585  * acl_free().
1586  */
1587 int
1588 smb_vop_acl_read(vnode_t *vp, acl_t **aclp, int flags, acl_type_t acl_type,
1589     cred_t *cr)
1590 {
1591 	int error;
1592 	vsecattr_t vsecattr;
1593 
1594 	ASSERT(vp);
1595 	ASSERT(aclp);
1596 
1597 	*aclp = NULL;
1598 	bzero(&vsecattr, sizeof (vsecattr_t));
1599 
1600 	switch (acl_type) {
1601 	case ACLENT_T:
1602 		vsecattr.vsa_mask = VSA_ACL | VSA_ACLCNT | VSA_DFACL |
1603 		    VSA_DFACLCNT;
1604 		break;
1605 
1606 	case ACE_T:
1607 		vsecattr.vsa_mask = VSA_ACE | VSA_ACECNT | VSA_ACE_ACLFLAGS;
1608 		break;
1609 
1610 	default:
1611 		return (EINVAL);
1612 	}
1613 
1614 	if (error = VOP_GETSECATTR(vp, &vsecattr, flags, cr, &smb_ct))
1615 		return (error);
1616 
1617 	*aclp = smb_fsacl_from_vsa(&vsecattr, acl_type);
1618 	if (vp->v_type == VDIR)
1619 		(*aclp)->acl_flags |= ACL_IS_DIR;
1620 
1621 	return (0);
1622 }
1623 
1624 /*
1625  * smb_vop_acl_write
1626  *
1627  * Writes the given ACL in aclp for the specified file.
1628  */
1629 int
1630 smb_vop_acl_write(vnode_t *vp, acl_t *aclp, int flags, cred_t *cr)
1631 {
1632 	int error;
1633 	vsecattr_t vsecattr;
1634 	int aclbsize;
1635 
1636 	ASSERT(vp);
1637 	ASSERT(aclp);
1638 
1639 	error = smb_fsacl_to_vsa(aclp, &vsecattr, &aclbsize);
1640 
1641 	if (error == 0) {
1642 		(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
1643 		error = VOP_SETSECATTR(vp, &vsecattr, flags, cr, &smb_ct);
1644 		VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
1645 	}
1646 
1647 	if (aclbsize && vsecattr.vsa_aclentp)
1648 		kmem_free(vsecattr.vsa_aclentp, aclbsize);
1649 
1650 	return (error);
1651 }
1652 
1653 /*
1654  * smb_vop_acl_type
1655  *
1656  * Determines the ACL type for the given vnode.
1657  * ACLENT_T is a Posix ACL and ACE_T is a ZFS ACL.
1658  */
1659 acl_type_t
1660 smb_vop_acl_type(vnode_t *vp)
1661 {
1662 	int error;
1663 	ulong_t whichacl;
1664 
1665 	error = VOP_PATHCONF(vp, _PC_ACL_ENABLED, &whichacl, kcred, NULL);
1666 	if (error != 0) {
1667 		/*
1668 		 * If we got an error, then the filesystem
1669 		 * likely does not understand the _PC_ACL_ENABLED
1670 		 * pathconf.  In this case, we fall back to trying
1671 		 * POSIX-draft (aka UFS-style) ACLs.
1672 		 */
1673 		whichacl = _ACL_ACLENT_ENABLED;
1674 	}
1675 
1676 	if (!(whichacl & (_ACL_ACE_ENABLED | _ACL_ACLENT_ENABLED))) {
1677 		/*
1678 		 * If the file system supports neither ACE nor
1679 		 * ACLENT ACLs we will fall back to UFS-style ACLs
1680 		 * like we did above if there was an error upon
1681 		 * calling VOP_PATHCONF.
1682 		 *
1683 		 * ACE and ACLENT type ACLs are the only interfaces
1684 		 * supported thus far.  If any other bits are set on
1685 		 * 'whichacl' upon return from VOP_PATHCONF, we will
1686 		 * ignore them.
1687 		 */
1688 		whichacl = _ACL_ACLENT_ENABLED;
1689 	}
1690 
1691 	if (whichacl == _ACL_ACLENT_ENABLED)
1692 		return (ACLENT_T);
1693 
1694 	return (ACE_T);
1695 }
1696 
1697 static int zfs_perms[] = {
1698 	ACE_READ_DATA, ACE_WRITE_DATA, ACE_APPEND_DATA, ACE_READ_NAMED_ATTRS,
1699 	ACE_WRITE_NAMED_ATTRS, ACE_EXECUTE, ACE_DELETE_CHILD,
1700 	ACE_READ_ATTRIBUTES, ACE_WRITE_ATTRIBUTES, ACE_DELETE, ACE_READ_ACL,
1701 	ACE_WRITE_ACL, ACE_WRITE_OWNER, ACE_SYNCHRONIZE
1702 };
1703 
1704 static int unix_perms[] = { VREAD, VWRITE, VEXEC };
1705 /*
1706  * smb_vop_eaccess
1707  *
1708  * Returns the effective permission of the given credential for the
1709  * specified object.
1710  *
1711  * This is just a workaround. We need VFS/FS support for this.
1712  */
1713 void
1714 smb_vop_eaccess(vnode_t *vp, int *mode, int flags, vnode_t *dir_vp, cred_t *cr)
1715 {
1716 	int error, i;
1717 	int pnum;
1718 
1719 	*mode = 0;
1720 
1721 	if (flags == V_ACE_MASK) {
1722 		pnum = sizeof (zfs_perms) / sizeof (int);
1723 
1724 		for (i = 0; i < pnum; i++) {
1725 			error = smb_vop_access(vp, zfs_perms[i], flags,
1726 			    dir_vp, cr);
1727 			if (error == 0)
1728 				*mode |= zfs_perms[i];
1729 		}
1730 	} else {
1731 		pnum = sizeof (unix_perms) / sizeof (int);
1732 
1733 		for (i = 0; i < pnum; i++) {
1734 			error = smb_vop_access(vp, unix_perms[i], flags,
1735 			    dir_vp, cr);
1736 			if (error == 0)
1737 				*mode |= unix_perms[i];
1738 		}
1739 	}
1740 }
1741 
1742 /*
1743  * smb_vop_shrlock()
1744  *
1745  * See comments for smb_fsop_shrlock()
1746  */
1747 
1748 int
1749 smb_vop_shrlock(vnode_t *vp, uint32_t uniq_fid, uint32_t desired_access,
1750     uint32_t share_access, cred_t *cr)
1751 {
1752 	struct shrlock shr;
1753 	struct shr_locowner shr_own;
1754 	short new_access = 0;
1755 	short deny = 0;
1756 	int flag = 0;
1757 	int cmd;
1758 
1759 	cmd = (nbl_need_check(vp)) ? F_SHARE_NBMAND : F_SHARE;
1760 
1761 	/*
1762 	 * Check if this is a metadata access
1763 	 */
1764 
1765 	if ((desired_access & FILE_DATA_ALL) == 0) {
1766 		new_access |= F_MDACC;
1767 	} else {
1768 		if (desired_access & (ACE_READ_DATA | ACE_EXECUTE)) {
1769 			new_access |= F_RDACC;
1770 			flag |= FREAD;
1771 		}
1772 
1773 		if (desired_access & (ACE_WRITE_DATA | ACE_APPEND_DATA |
1774 		    ACE_ADD_FILE)) {
1775 			new_access |= F_WRACC;
1776 			flag |= FWRITE;
1777 		}
1778 
1779 		if (SMB_DENY_READ(share_access)) {
1780 			deny |= F_RDDNY;
1781 		}
1782 
1783 		if (SMB_DENY_WRITE(share_access)) {
1784 			deny |= F_WRDNY;
1785 		}
1786 
1787 		if (cmd == F_SHARE_NBMAND) {
1788 			if (desired_access & ACE_DELETE)
1789 				new_access |= F_RMACC;
1790 
1791 			if (SMB_DENY_DELETE(share_access)) {
1792 				deny |= F_RMDNY;
1793 			}
1794 		}
1795 	}
1796 
1797 	shr.s_access = new_access;
1798 	shr.s_deny = deny;
1799 	shr.s_sysid = smb_ct.cc_sysid;
1800 	shr.s_pid = uniq_fid;
1801 	shr.s_own_len = sizeof (shr_own);
1802 	shr.s_owner = (caddr_t)&shr_own;
1803 	shr_own.sl_id = shr.s_sysid;
1804 	shr_own.sl_pid = shr.s_pid;
1805 
1806 	return (VOP_SHRLOCK(vp, cmd, &shr, flag, cr, NULL));
1807 }
1808 
1809 int
1810 smb_vop_unshrlock(vnode_t *vp, uint32_t uniq_fid, cred_t *cr)
1811 {
1812 	struct shrlock shr;
1813 	struct shr_locowner shr_own;
1814 
1815 	/*
1816 	 * For s_access and s_deny, we do not need to pass in the original
1817 	 * values.
1818 	 */
1819 
1820 	shr.s_access = 0;
1821 	shr.s_deny = 0;
1822 	shr.s_sysid = smb_ct.cc_sysid;
1823 	shr.s_pid = uniq_fid;
1824 	shr.s_own_len = sizeof (shr_own);
1825 	shr.s_owner = (caddr_t)&shr_own;
1826 	shr_own.sl_id = shr.s_sysid;
1827 	shr_own.sl_pid = shr.s_pid;
1828 
1829 	return (VOP_SHRLOCK(vp, F_UNSHARE, &shr, 0, cr, NULL));
1830 }
1831