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