xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_tree.c (revision 74e7dc986c89efca1f2e4451c7a572e05e4a6e4f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"@(#)smb_tree.c	1.5	08/08/07 SMI"
27 
28 /*
29  * General Structures Layout
30  * -------------------------
31  *
32  * This is a simplified diagram showing the relationship between most of the
33  * main structures.
34  *
35  * +-------------------+
36  * |     SMB_INFO      |
37  * +-------------------+
38  *          |
39  *          |
40  *          v
41  * +-------------------+       +-------------------+      +-------------------+
42  * |     SESSION       |<----->|     SESSION       |......|      SESSION      |
43  * +-------------------+       +-------------------+      +-------------------+
44  *          |
45  *          |
46  *          v
47  * +-------------------+       +-------------------+      +-------------------+
48  * |       USER        |<----->|       USER        |......|       USER        |
49  * +-------------------+       +-------------------+      +-------------------+
50  *          |
51  *          |
52  *          v
53  * +-------------------+       +-------------------+      +-------------------+
54  * |       TREE        |<----->|       TREE        |......|       TREE        |
55  * +-------------------+       +-------------------+      +-------------------+
56  *      |         |
57  *      |         |
58  *      |         v
59  *      |     +-------+       +-------+      +-------+
60  *      |     | OFILE |<----->| OFILE |......| OFILE |
61  *      |     +-------+       +-------+      +-------+
62  *      |
63  *      |
64  *      v
65  *  +-------+       +------+      +------+
66  *  | ODIR  |<----->| ODIR |......| ODIR |
67  *  +-------+       +------+      +------+
68  *
69  *
70  * Tree State Machine
71  * ------------------
72  *
73  *    +-----------------------------+	 T0
74  *    |  SMB_TREE_STATE_CONNECTED   |<----------- Creation/Allocation
75  *    +-----------------------------+
76  *		    |
77  *		    | T1
78  *		    |
79  *		    v
80  *    +------------------------------+
81  *    | SMB_TREE_STATE_DISCONNECTING |
82  *    +------------------------------+
83  *		    |
84  *		    | T2
85  *		    |
86  *		    v
87  *    +-----------------------------+    T3
88  *    | SMB_TREE_STATE_DISCONNECTED |----------> Deletion/Free
89  *    +-----------------------------+
90  *
91  * SMB_TREE_STATE_CONNECTED
92  *
93  *    While in this state:
94  *      - The tree is queued in the list of trees of its user.
95  *      - References will be given out if the tree is looked up.
96  *      - Files under that tree can be accessed.
97  *
98  * SMB_TREE_STATE_DISCONNECTING
99  *
100  *    While in this state:
101  *      - The tree is queued in the list of trees of its user.
102  *      - References will not be given out if the tree is looked up.
103  *      - The files and directories open under the tree are being closed.
104  *      - The resources associated with the tree remain.
105  *
106  * SMB_TREE_STATE_DISCONNECTED
107  *
108  *    While in this state:
109  *      - The tree is queued in the list of trees of its user.
110  *      - References will not be given out if the tree is looked up.
111  *      - The tree has no more files and directories opened.
112  *      - The resources associated with the tree remain.
113  *
114  * Transition T0
115  *
116  *    This transition occurs in smb_tree_connect(). A new tree is created and
117  *    added to the list of trees of a user.
118  *
119  * Transition T1
120  *
121  *    This transition occurs in smb_tree_disconnect().
122  *
123  * Transition T2
124  *
125  *    This transition occurs in smb_tree_release(). The resources associated
126  *    with the tree are freed as well as the tree structure. For the transition
127  *    to occur, the tree must be in the SMB_TREE_STATE_DISCONNECTED state and
128  *    the reference count be zero.
129  *
130  * Comments
131  * --------
132  *
133  *    The state machine of the tree structures is controlled by 3 elements:
134  *      - The list of trees of the user it belongs to.
135  *      - The mutex embedded in the structure itself.
136  *      - The reference count.
137  *
138  *    There's a mutex embedded in the tree structure used to protect its fields
139  *    and there's a lock embedded in the list of trees of a user. To
140  *    increment or to decrement the reference count the mutex must be entered.
141  *    To insert the tree into the list of trees of the user and to remove
142  *    the tree from it, the lock must be entered in RW_WRITER mode.
143  *
144  *    Rules of access to a tree structure:
145  *
146  *    1) In order to avoid deadlocks, when both (mutex and lock of the user
147  *       list) have to be entered, the lock must be entered first.
148  *
149  *    2) All actions applied to a tree require a reference count.
150  *
151  *    3) There are 2 ways of getting a reference count: when a tree is
152  *       connected and when a tree is looked up.
153  *
154  *    It should be noted that the reference count of a tree registers the
155  *    number of references to the tree in other structures (such as an smb
156  *    request). The reference count is not incremented in these 2 instances:
157  *
158  *    1) The tree is connected. An tree is anchored by his state. If there's
159  *       no activity involving a tree currently connected, the reference
160  *       count of that tree is zero.
161  *
162  *    2) The tree is queued in the list of trees of the user. The fact of
163  *       being queued in that list is NOT registered by incrementing the
164  *       reference count.
165  */
166 #include <sys/types.h>
167 #include <sys/refstr_impl.h>
168 #include <sys/feature_tests.h>
169 #include <sys/sunddi.h>
170 #include <sys/fsid.h>
171 #include <sys/vfs.h>
172 #include <sys/stat.h>
173 #include <sys/varargs.h>
174 #include <smbsrv/smb_incl.h>
175 #include <smbsrv/lmerr.h>
176 #include <smbsrv/mlsvc.h>
177 #include <smbsrv/smb_fsops.h>
178 #include <smbsrv/smb_door_svc.h>
179 #include <smbsrv/smb_share.h>
180 
181 int smb_tcon_mute = 0;
182 
183 static smb_tree_t *smb_tree_connect_disk(smb_request_t *, const char *);
184 static smb_tree_t *smb_tree_connect_ipc(smb_request_t *, const char *);
185 static smb_tree_t *smb_tree_alloc(smb_user_t *, const char *, const char *,
186     int32_t, smb_node_t *);
187 static void smb_tree_dealloc(smb_tree_t *);
188 static boolean_t smb_tree_is_connected(smb_tree_t *);
189 static boolean_t smb_tree_is_disconnected(smb_tree_t *);
190 static const char *smb_tree_get_sharename(const char *);
191 static int smb_tree_get_stype(const char *, const char *, int32_t *);
192 static int smb_tree_getattr(smb_node_t *, smb_tree_t *);
193 static void smb_tree_get_volname(vfs_t *, smb_tree_t *);
194 static void smb_tree_get_flags(vfs_t *, smb_tree_t *);
195 static void smb_tree_log(smb_request_t *, const char *, const char *, ...);
196 
197 /*
198  * Extract the share name and share type and connect as appropriate.
199  * Share names are case insensitive so we map the share name to
200  * lower-case as a convenience for internal processing.
201  */
202 smb_tree_t *
203 smb_tree_connect(smb_request_t *sr)
204 {
205 	char *unc_path = sr->arg.tcon.path;
206 	char *service = sr->arg.tcon.service;
207 	smb_tree_t *tree = NULL;
208 	const char *name;
209 	int32_t stype;
210 
211 	(void) utf8_strlwr(unc_path);
212 
213 	if ((name = smb_tree_get_sharename(unc_path)) == NULL) {
214 		smbsr_error(sr, 0, ERRSRV, ERRinvnetname);
215 		return (NULL);
216 	}
217 
218 	if (smb_tree_get_stype(name, service, &stype) != 0) {
219 		smbsr_error(sr, NT_STATUS_BAD_DEVICE_TYPE,
220 		    ERRDOS, ERROR_BAD_DEV_TYPE);
221 		return (NULL);
222 	}
223 
224 	switch (stype & STYPE_MASK) {
225 	case STYPE_DISKTREE:
226 		tree = smb_tree_connect_disk(sr, name);
227 		break;
228 
229 	case STYPE_IPC:
230 		tree = smb_tree_connect_ipc(sr, name);
231 		break;
232 
233 	default:
234 		smbsr_error(sr, NT_STATUS_BAD_DEVICE_TYPE,
235 		    ERRDOS, ERROR_BAD_DEV_TYPE);
236 		break;
237 	}
238 
239 	return (tree);
240 }
241 
242 /*
243  * Disconnect a tree.
244  */
245 void
246 smb_tree_disconnect(
247     smb_tree_t	*tree)
248 {
249 	ASSERT(tree->t_magic == SMB_TREE_MAGIC);
250 
251 	mutex_enter(&tree->t_mutex);
252 	ASSERT(tree->t_refcnt);
253 
254 	if (smb_tree_is_connected(tree)) {
255 		/*
256 		 * Indicate that the disconnect process has started.
257 		 */
258 		tree->t_state = SMB_TREE_STATE_DISCONNECTING;
259 		mutex_exit(&tree->t_mutex);
260 		atomic_dec_32(&tree->t_server->sv_open_trees);
261 
262 		/*
263 		 * The files opened under this tree are closed.
264 		 */
265 		smb_ofile_close_all(tree);
266 		/*
267 		 * The directories opened under this tree are closed.
268 		 */
269 		smb_odir_close_all(tree);
270 		mutex_enter(&tree->t_mutex);
271 		tree->t_state = SMB_TREE_STATE_DISCONNECTED;
272 	}
273 
274 	mutex_exit(&tree->t_mutex);
275 }
276 
277 /*
278  * Take a reference on a tree.
279  */
280 boolean_t
281 smb_tree_hold(
282     smb_tree_t		*tree)
283 {
284 	ASSERT(tree);
285 	ASSERT(tree->t_magic == SMB_TREE_MAGIC);
286 
287 	mutex_enter(&tree->t_mutex);
288 
289 	if (smb_tree_is_connected(tree)) {
290 		tree->t_refcnt++;
291 		mutex_exit(&tree->t_mutex);
292 		return (B_TRUE);
293 	}
294 
295 	mutex_exit(&tree->t_mutex);
296 	return (B_FALSE);
297 }
298 
299 /*
300  * Release a reference on a tree.  If the tree is disconnected and the
301  * reference count falls to zero, the tree will be deallocated.
302  */
303 void
304 smb_tree_release(
305     smb_tree_t		*tree)
306 {
307 	ASSERT(tree);
308 	ASSERT(tree->t_magic == SMB_TREE_MAGIC);
309 
310 	mutex_enter(&tree->t_mutex);
311 	ASSERT(tree->t_refcnt);
312 	tree->t_refcnt--;
313 
314 	if (smb_tree_is_disconnected(tree) && (tree->t_refcnt == 0)) {
315 		mutex_exit(&tree->t_mutex);
316 		smb_tree_dealloc(tree);
317 		return;
318 	}
319 
320 	mutex_exit(&tree->t_mutex);
321 }
322 
323 /*
324  * Close ofiles and odirs that match pid.
325  */
326 void
327 smb_tree_close_pid(
328     smb_tree_t		*tree,
329     uint16_t		pid)
330 {
331 	ASSERT(tree);
332 	ASSERT(tree->t_magic == SMB_TREE_MAGIC);
333 
334 	smb_ofile_close_all_by_pid(tree, pid);
335 	smb_odir_close_all_by_pid(tree, pid);
336 }
337 
338 /*
339  * Check whether or not a tree supports the features identified by flags.
340  */
341 boolean_t
342 smb_tree_has_feature(smb_tree_t *tree, uint32_t flags)
343 {
344 	ASSERT(tree);
345 	ASSERT(tree->t_magic == SMB_TREE_MAGIC);
346 
347 	return ((tree->t_flags & flags) == flags);
348 }
349 
350 /* *************************** Static Functions ***************************** */
351 
352 /*
353  * Connect a share for use with files and directories.
354  */
355 static smb_tree_t *
356 smb_tree_connect_disk(smb_request_t *sr, const char *sharename)
357 {
358 	smb_user_t		*user = sr->uid_user;
359 	smb_node_t		*dir_snode = NULL;
360 	smb_node_t		*snode = NULL;
361 	char			last_component[MAXNAMELEN];
362 	smb_tree_t		*tree;
363 	smb_share_t 		si;
364 	smb_attr_t		attr;
365 	cred_t			*u_cred;
366 	int			rc;
367 
368 	ASSERT(user);
369 	u_cred = user->u_cred;
370 	ASSERT(u_cred);
371 
372 	bzero(&si, sizeof (smb_share_t));
373 
374 	if (user->u_flags & SMB_USER_FLAG_IPC) {
375 		smb_tree_log(sr, sharename, "access denied: IPC only");
376 		smbsr_error(sr, NT_STATUS_ACCESS_DENIED, ERRSRV, ERRaccess);
377 		return (NULL);
378 	}
379 
380 	if (smb_kshare_getinfo(sr->sr_server->sv_lmshrd, sharename, &si) !=
381 	    NERR_Success) {
382 		smb_tree_log(sr, sharename, "share not found");
383 		smbsr_error(sr, 0, ERRSRV, ERRinvnetname);
384 		return (NULL);
385 	}
386 
387 	/*
388 	 * Handle the default administration shares: C$, D$ etc.
389 	 * Only a user with admin rights is allowed to map these
390 	 * shares.
391 	 */
392 	if (si.shr_flags & SMB_SHRF_ADMIN) {
393 		if (!smb_user_is_admin(user)) {
394 			smb_tree_log(sr, sharename, "access denied: not admin");
395 			smbsr_error(sr, NT_STATUS_ACCESS_DENIED,
396 			    ERRSRV, ERRaccess);
397 			return (NULL);
398 		}
399 	}
400 
401 	/*
402 	 * Check that the shared directory exists.
403 	 */
404 	rc = smb_pathname_reduce(sr, u_cred, si.shr_path, 0, 0, &dir_snode,
405 	    last_component);
406 
407 	if (rc == 0) {
408 		rc = smb_fsop_lookup(sr, u_cred, SMB_FOLLOW_LINKS, 0,
409 		    dir_snode, last_component, &snode, &attr, 0, 0);
410 
411 		smb_node_release(dir_snode);
412 	}
413 
414 	if (rc) {
415 		if (snode)
416 			smb_node_release(snode);
417 
418 		smb_tree_log(sr, sharename, "bad path: %s", si.shr_path);
419 		smbsr_error(sr, 0, ERRSRV, ERRinvnetname);
420 		return (NULL);
421 	}
422 
423 	tree = smb_tree_alloc(user, sharename, si.shr_path, STYPE_DISKTREE,
424 	    snode);
425 	if (tree == NULL)
426 		smbsr_error(sr, NT_STATUS_ACCESS_DENIED, ERRSRV, ERRaccess);
427 
428 	smb_node_release(snode);
429 	return (tree);
430 }
431 
432 /*
433  * Connect an IPC share for use with named pipes.
434  */
435 static smb_tree_t *
436 smb_tree_connect_ipc(smb_request_t *sr, const char *name)
437 {
438 	smb_user_t *user = sr->uid_user;
439 	smb_tree_t *tree;
440 
441 	ASSERT(user);
442 
443 	if ((user->u_flags & SMB_USER_FLAG_IPC) &&
444 	    sr->sr_cfg->skc_restrict_anon) {
445 		smb_tree_log(sr, name, "access denied: restrict anonymous");
446 		smbsr_error(sr, NT_STATUS_ACCESS_DENIED, ERRSRV, ERRaccess);
447 		return (NULL);
448 	}
449 
450 	tree = smb_tree_alloc(user, name, name, STYPE_IPC, NULL);
451 	if (tree == NULL) {
452 		smb_tree_log(sr, name, "access denied");
453 		smbsr_error(sr, NT_STATUS_ACCESS_DENIED, ERRSRV, ERRaccess);
454 	}
455 
456 	return (tree);
457 }
458 
459 /*
460  * Allocate a tree.
461  */
462 static smb_tree_t *
463 smb_tree_alloc(
464     smb_user_t		*user,
465     const char		*sharename,
466     const char		*resource,
467     int32_t		stype,
468     smb_node_t		*snode)
469 {
470 	smb_tree_t	*tree;
471 	uint16_t	tid;
472 
473 	if (smb_idpool_alloc(&user->u_tid_pool, &tid))
474 		return (NULL);
475 
476 	tree = kmem_cache_alloc(user->u_server->si_cache_tree, KM_SLEEP);
477 	bzero(tree, sizeof (smb_tree_t));
478 
479 	if (STYPE_ISDSK(stype)) {
480 		if (smb_tree_getattr(snode, tree) != 0) {
481 			smb_idpool_free(&user->u_tid_pool, tid);
482 			kmem_cache_free(user->u_server->si_cache_tree, tree);
483 			return (NULL);
484 		}
485 	}
486 
487 	if (smb_idpool_constructor(&tree->t_fid_pool)) {
488 		smb_idpool_free(&user->u_tid_pool, tid);
489 		kmem_cache_free(user->u_server->si_cache_tree, tree);
490 		return (NULL);
491 	}
492 
493 	if (smb_idpool_constructor(&tree->t_sid_pool)) {
494 		smb_idpool_destructor(&tree->t_fid_pool);
495 		smb_idpool_free(&user->u_tid_pool, tid);
496 		kmem_cache_free(user->u_server->si_cache_tree, tree);
497 		return (NULL);
498 	}
499 
500 	smb_llist_constructor(&tree->t_ofile_list, sizeof (smb_ofile_t),
501 	    offsetof(smb_ofile_t, f_lnd));
502 
503 	smb_llist_constructor(&tree->t_odir_list, sizeof (smb_odir_t),
504 	    offsetof(smb_odir_t, d_lnd));
505 
506 	(void) strlcpy(tree->t_sharename, sharename,
507 	    sizeof (tree->t_sharename));
508 	(void) strlcpy(tree->t_resource, resource, sizeof (tree->t_resource));
509 
510 	mutex_init(&tree->t_mutex, NULL, MUTEX_DEFAULT, NULL);
511 
512 	tree->t_user = user;
513 	tree->t_session = user->u_session;
514 	tree->t_server = user->u_server;
515 	tree->t_refcnt = 1;
516 	tree->t_tid = tid;
517 	tree->t_res_type = stype;
518 	tree->t_state = SMB_TREE_STATE_CONNECTED;
519 	tree->t_magic = SMB_TREE_MAGIC;
520 
521 	if (STYPE_ISDSK(stype)) {
522 		smb_node_ref(snode);
523 		tree->t_snode = snode;
524 		tree->t_acltype = smb_fsop_acltype(snode);
525 	}
526 
527 	smb_llist_enter(&user->u_tree_list, RW_WRITER);
528 	smb_llist_insert_head(&user->u_tree_list, tree);
529 	smb_llist_exit(&user->u_tree_list);
530 	atomic_inc_32(&user->u_session->s_tree_cnt);
531 	atomic_inc_32(&user->u_server->sv_open_trees);
532 
533 	return (tree);
534 }
535 
536 /*
537  * Deallocate a tree: release all resources associated with a tree and
538  * remove the tree from the user's tree list.
539  *
540  * The tree being destroyed must be in the "destroying" state and the
541  * reference count must be zero. This function assumes it's single threaded
542  * i.e. only one thread will attempt to destroy a specific tree, which
543  * should be the case if the tree is in disconnected and has a reference
544  * count of zero.
545  */
546 static void
547 smb_tree_dealloc(smb_tree_t *tree)
548 {
549 	ASSERT(tree);
550 	ASSERT(tree->t_magic == SMB_TREE_MAGIC);
551 	ASSERT(tree->t_state == SMB_TREE_STATE_DISCONNECTED);
552 	ASSERT(tree->t_refcnt == 0);
553 
554 	/*
555 	 * Remove the tree from the user's tree list.  This must be done
556 	 * before any resources associated with the tree are released.
557 	 */
558 	smb_llist_enter(&tree->t_user->u_tree_list, RW_WRITER);
559 	smb_llist_remove(&tree->t_user->u_tree_list, tree);
560 	smb_llist_exit(&tree->t_user->u_tree_list);
561 
562 	tree->t_magic = (uint32_t)~SMB_TREE_MAGIC;
563 	smb_idpool_free(&tree->t_user->u_tid_pool, tree->t_tid);
564 	atomic_dec_32(&tree->t_session->s_tree_cnt);
565 
566 	if (tree->t_snode)
567 		smb_node_release(tree->t_snode);
568 
569 	mutex_destroy(&tree->t_mutex);
570 
571 	/*
572 	 * The list of open files and open directories should be empty.
573 	 */
574 	smb_llist_destructor(&tree->t_ofile_list);
575 	smb_llist_destructor(&tree->t_odir_list);
576 	smb_idpool_destructor(&tree->t_fid_pool);
577 	smb_idpool_destructor(&tree->t_sid_pool);
578 	kmem_cache_free(tree->t_server->si_cache_tree, tree);
579 }
580 
581 /*
582  * Determine whether or not a tree is connected.
583  * This function must be called with the tree mutex held.
584  */
585 static boolean_t
586 smb_tree_is_connected(smb_tree_t *tree)
587 {
588 	switch (tree->t_state) {
589 	case SMB_TREE_STATE_CONNECTED:
590 		return (B_TRUE);
591 
592 	case SMB_TREE_STATE_DISCONNECTING:
593 	case SMB_TREE_STATE_DISCONNECTED:
594 		/*
595 		 * The tree exists but being diconnected or destroyed.
596 		 */
597 		return (B_FALSE);
598 
599 	default:
600 		ASSERT(0);
601 		return (B_FALSE);
602 	}
603 }
604 
605 /*
606  * Determine whether or not a tree is disconnected.
607  * This function must be called with the tree mutex held.
608  */
609 static boolean_t
610 smb_tree_is_disconnected(smb_tree_t *tree)
611 {
612 	switch (tree->t_state) {
613 	case SMB_TREE_STATE_DISCONNECTED:
614 		return (B_TRUE);
615 
616 	case SMB_TREE_STATE_CONNECTED:
617 	case SMB_TREE_STATE_DISCONNECTING:
618 		return (B_FALSE);
619 
620 	default:
621 		ASSERT(0);
622 		return (B_FALSE);
623 	}
624 }
625 
626 /*
627  * Return a pointer to the share name within a share resource path.
628  *
629  * The share path may be a Uniform Naming Convention (UNC) string
630  * (\\server\share) or simply the share name.  We validate the UNC
631  * format but we don't look at the server name.
632  */
633 static const char *
634 smb_tree_get_sharename(const char *unc_path)
635 {
636 	const char *sharename = unc_path;
637 
638 	if (sharename[0] == '\\') {
639 		/*
640 		 * Looks like a UNC path, validate the format.
641 		 */
642 		if (sharename[1] != '\\')
643 			return (NULL);
644 
645 		if ((sharename = strchr(sharename+2, '\\')) == NULL)
646 			return (NULL);
647 
648 		++sharename;
649 	} else if (strchr(sharename, '\\') != NULL) {
650 		/*
651 		 * This should be a share name (no embedded \'s).
652 		 */
653 		return (NULL);
654 	}
655 
656 	return (sharename);
657 }
658 
659 /*
660  * Map the service to a resource type.  Valid values for service are:
661  *
662  *	A:      Disk share
663  *	LPT1:   Printer
664  *	IPC     Named pipe
665  *	COMM    Communications device
666  *	?????   Any type of device (wildcard)
667  *
668  * We support IPC and disk shares; anything else is currently treated
669  * as an error.  IPC$ is reserved as the named pipe share.
670  */
671 static int
672 smb_tree_get_stype(const char *sharename, const char *service,
673     int32_t *stype_ret)
674 {
675 	const char *any = "?????";
676 
677 	if ((strcmp(service, any) == 0) || (strcasecmp(service, "IPC") == 0)) {
678 		if (strcasecmp(sharename, "IPC$") == 0) {
679 			*stype_ret = STYPE_IPC;
680 			return (0);
681 		}
682 	}
683 
684 	if ((strcmp(service, any) == 0) || (strcasecmp(service, "A:") == 0)) {
685 		if (strcasecmp(sharename, "IPC$") == 0)
686 			return (-1);
687 
688 		*stype_ret = STYPE_DISKTREE;
689 		return (0);
690 	}
691 
692 	return (-1);
693 }
694 
695 /*
696  * Obtain the tree attributes: volume name, typename and flags.
697  */
698 static int
699 smb_tree_getattr(smb_node_t *node, smb_tree_t *tree)
700 {
701 	vfs_t *vfsp = SMB_NODE_VFS(node);
702 
703 	ASSERT(vfsp);
704 
705 	if (getvfs(&vfsp->vfs_fsid) != vfsp)
706 		return (ESTALE);
707 
708 	smb_tree_get_volname(vfsp, tree);
709 	smb_tree_get_flags(vfsp, tree);
710 
711 	VFS_RELE(vfsp);
712 	return (0);
713 }
714 
715 /*
716  * Extract the volume name.
717  */
718 static void
719 smb_tree_get_volname(vfs_t *vfsp, smb_tree_t *tree)
720 {
721 	refstr_t *vfs_mntpoint;
722 	const char *s;
723 	char *name;
724 
725 	vfs_mntpoint = vfs_getmntpoint(vfsp);
726 
727 	s = vfs_mntpoint->rs_string;
728 	s += strspn(s, "/");
729 	(void) strlcpy(tree->t_volume, s, SMB_VOLNAMELEN);
730 
731 	refstr_rele(vfs_mntpoint);
732 
733 	name = tree->t_volume;
734 	(void) strsep((char **)&name, "/");
735 }
736 
737 /*
738  * Always set ACL support because the VFS will fake ACLs for file systems
739  * that don't support them.
740  *
741  * Some flags are dependent on the typename, which is also set up here.
742  * File system types are hardcoded in uts/common/os/vfs_conf.c.
743  */
744 static void
745 smb_tree_get_flags(vfs_t *vfsp, smb_tree_t *tree)
746 {
747 	uint32_t flags = SMB_TREE_SUPPORTS_ACLS;
748 	char *name;
749 
750 	if (vfsp->vfs_flag & VFS_RDONLY)
751 		flags |= SMB_TREE_READONLY;
752 
753 	if (vfsp->vfs_flag & VFS_XATTR)
754 		flags |= SMB_TREE_STREAMS;
755 
756 	if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL))
757 		flags |= SMB_TREE_NO_ATIME;
758 
759 	name = vfssw[vfsp->vfs_fstype].vsw_name;
760 
761 	if (strcmp(name, "tmpfs") == 0)
762 		flags |= SMB_TREE_NO_EXPORT;
763 
764 	if (strncasecmp(name, NFS, sizeof (NFS)) == 0)
765 		flags |= SMB_TREE_NFS_MOUNTED;
766 
767 	if (strncasecmp(name, "UFS", sizeof ("UFS")) == 0)
768 		flags |= SMB_TREE_UFS;
769 
770 	(void) strlcpy(tree->t_typename, name, SMB_TYPENAMELEN);
771 	(void) utf8_strupr((char *)tree->t_typename);
772 
773 	if (vfs_has_feature(vfsp, VFSFT_XVATTR))
774 		flags |= SMB_TREE_XVATTR;
775 
776 	if (vfs_has_feature(vfsp, VFSFT_CASEINSENSITIVE))
777 		flags |= SMB_TREE_CASEINSENSITIVE;
778 
779 	if (vfs_has_feature(vfsp, VFSFT_NOCASESENSITIVE))
780 		flags |= SMB_TREE_NO_CASESENSITIVE;
781 
782 	if (vfs_has_feature(vfsp, VFSFT_DIRENTFLAGS))
783 		flags |= SMB_TREE_DIRENTFLAGS;
784 
785 	if (vfs_has_feature(vfsp, VFSFT_ACLONCREATE))
786 		flags |= SMB_TREE_ACLONCREATE;
787 
788 	if (vfs_has_feature(vfsp, VFSFT_ACEMASKONACCESS))
789 		flags |= SMB_TREE_ACEMASKONACCESS;
790 
791 	DTRACE_PROBE1(smb__tree__flags, uint32_t, flags);
792 
793 	tree->t_flags = flags;
794 }
795 
796 /*
797  * Report share access result to syslog.
798  */
799 static void
800 smb_tree_log(smb_request_t *sr, const char *sharename, const char *fmt, ...)
801 {
802 	va_list ap;
803 	char buf[128];
804 	smb_user_t *user = sr->uid_user;
805 
806 	ASSERT(user);
807 
808 	if (smb_tcon_mute)
809 		return;
810 
811 	if ((user->u_name) && (strcasecmp(sharename, "IPC$") == 0)) {
812 		/*
813 		 * Only report normal users, i.e. ignore W2K misuse
814 		 * of the IPC connection by filtering out internal
815 		 * names such as nobody and root.
816 		 */
817 		if ((strcmp(user->u_name, "root") == 0) ||
818 		    (strcmp(user->u_name, "nobody") == 0)) {
819 			return;
820 		}
821 	}
822 
823 	va_start(ap, fmt);
824 	(void) vsnprintf(buf, 128, fmt, ap);
825 	va_end(ap);
826 
827 	cmn_err(CE_NOTE, "smbd[%s\\%s]: %s %s",
828 	    user->u_domain, user->u_name, sharename, buf);
829 }
830