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