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