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