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 (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 /*
26  * General Structures Layout
27  * -------------------------
28  *
29  * This is a simplified diagram showing the relationship between most of the
30  * main structures.
31  *
32  * +-------------------+
33  * |     SMB_INFO      |
34  * +-------------------+
35  *          |
36  *          |
37  *          v
38  * +-------------------+       +-------------------+      +-------------------+
39  * |     SESSION       |<----->|     SESSION       |......|      SESSION      |
40  * +-------------------+       +-------------------+      +-------------------+
41  *          |
42  *          |
43  *          v
44  * +-------------------+       +-------------------+      +-------------------+
45  * |       USER        |<----->|       USER        |......|       USER        |
46  * +-------------------+       +-------------------+      +-------------------+
47  *          |
48  *          |
49  *          v
50  * +-------------------+       +-------------------+      +-------------------+
51  * |       TREE        |<----->|       TREE        |......|       TREE        |
52  * +-------------------+       +-------------------+      +-------------------+
53  *      |         |
54  *      |         |
55  *      |         v
56  *      |     +-------+       +-------+      +-------+
57  *      |     | OFILE |<----->| OFILE |......| OFILE |
58  *      |     +-------+       +-------+      +-------+
59  *      |
60  *      |
61  *      v
62  *  +-------+       +------+      +------+
63  *  | ODIR  |<----->| ODIR |......| ODIR |
64  *  +-------+       +------+      +------+
65  *
66  *
67  * Ofile State Machine
68  * ------------------
69  *
70  *    +-------------------------+	 T0
71  *    |  SMB_OFILE_STATE_OPEN   |<----------- Creation/Allocation
72  *    +-------------------------+
73  *		    |
74  *		    | T1
75  *		    |
76  *		    v
77  *    +-------------------------+
78  *    | SMB_OFILE_STATE_CLOSING |
79  *    +-------------------------+
80  *		    |
81  *		    | T2
82  *		    |
83  *		    v
84  *    +-------------------------+    T3
85  *    | SMB_OFILE_STATE_CLOSED  |----------> Deletion/Free
86  *    +-------------------------+
87  *
88  * SMB_OFILE_STATE_OPEN
89  *
90  *    While in this state:
91  *      - The ofile is queued in the list of ofiles of its tree.
92  *      - References will be given out if the ofile is looked up.
93  *
94  * SMB_OFILE_STATE_CLOSING
95  *
96  *    While in this state:
97  *      - The ofile is queued in the list of ofiles of its tree.
98  *      - References will not be given out if the ofile is looked up.
99  *      - The file is closed and the locks held are being released.
100  *      - The resources associated with the ofile remain.
101  *
102  * SMB_OFILE_STATE_CLOSED
103  *
104  *    While in this state:
105  *      - The ofile is queued in the list of ofiles of its tree.
106  *      - References will not be given out if the ofile is looked up.
107  *      - The resources associated with the ofile remain.
108  *
109  * Transition T0
110  *
111  *    This transition occurs in smb_ofile_open(). A new ofile is created and
112  *    added to the list of ofiles of a tree.
113  *
114  * Transition T1
115  *
116  *    This transition occurs in smb_ofile_close().
117  *
118  * Transition T2
119  *
120  *    This transition occurs in smb_ofile_release(). The resources associated
121  *    with the ofile are freed as well as the ofile structure. For the
122  *    transition to occur, the ofile must be in the SMB_OFILE_STATE_CLOSED
123  *    state and the reference count be zero.
124  *
125  * Comments
126  * --------
127  *
128  *    The state machine of the ofile structures is controlled by 3 elements:
129  *      - The list of ofiles of the tree it belongs to.
130  *      - The mutex embedded in the structure itself.
131  *      - The reference count.
132  *
133  *    There's a mutex embedded in the ofile structure used to protect its fields
134  *    and there's a lock embedded in the list of ofiles of a tree. To
135  *    increment or to decrement the reference count the mutex must be entered.
136  *    To insert the ofile into the list of ofiles of the tree and to remove
137  *    the ofile from it, the lock must be entered in RW_WRITER mode.
138  *
139  *    Rules of access to a ofile structure:
140  *
141  *    1) In order to avoid deadlocks, when both (mutex and lock of the ofile
142  *       list) have to be entered, the lock must be entered first.
143  *
144  *    2) All actions applied to an ofile require a reference count.
145  *
146  *    3) There are 2 ways of getting a reference count. One is when the ofile
147  *       is opened. The other one when the ofile is looked up. This translates
148  *       into 2 functions: smb_ofile_open() and smb_ofile_lookup_by_fid().
149  *
150  *    It should be noted that the reference count of an ofile registers the
151  *    number of references to the ofile in other structures (such as an smb
152  *    request). The reference count is not incremented in these 2 instances:
153  *
154  *    1) The ofile is open. An ofile is anchored by his state. If there's
155  *       no activity involving an ofile currently open, the reference count
156  *       of that ofile is zero.
157  *
158  *    2) The ofile is queued in the list of ofiles of its tree. The fact of
159  *       being queued in that list is NOT registered by incrementing the
160  *       reference count.
161  */
162 #include <smbsrv/smb_kproto.h>
163 #include <smbsrv/smb_fsops.h>
164 
165 static boolean_t smb_ofile_is_open_locked(smb_ofile_t *);
166 static smb_ofile_t *smb_ofile_close_and_next(smb_ofile_t *);
167 static void smb_ofile_set_close_attrs(smb_ofile_t *, uint32_t);
168 static int smb_ofile_netinfo_encode(smb_ofile_t *, uint8_t *, size_t,
169     uint32_t *);
170 static int smb_ofile_netinfo_init(smb_ofile_t *, smb_netfileinfo_t *);
171 static void smb_ofile_netinfo_fini(smb_netfileinfo_t *);
172 
173 /*
174  * smb_ofile_open
175  */
176 smb_ofile_t *
177 smb_ofile_open(
178     smb_tree_t		*tree,
179     smb_node_t		*node,
180     uint16_t		pid,
181     struct open_param	*op,
182     uint16_t		ftype,
183     uint32_t		uniqid,
184     smb_error_t		*err)
185 {
186 	smb_ofile_t	*of;
187 	uint16_t	fid;
188 	smb_attr_t	attr;
189 
190 	if (smb_idpool_alloc(&tree->t_fid_pool, &fid)) {
191 		err->status = NT_STATUS_TOO_MANY_OPENED_FILES;
192 		err->errcls = ERRDOS;
193 		err->errcode = ERROR_TOO_MANY_OPEN_FILES;
194 		return (NULL);
195 	}
196 
197 	of = kmem_cache_alloc(tree->t_server->si_cache_ofile, KM_SLEEP);
198 	bzero(of, sizeof (smb_ofile_t));
199 	of->f_magic = SMB_OFILE_MAGIC;
200 	of->f_refcnt = 1;
201 	of->f_fid = fid;
202 	of->f_uniqid = uniqid;
203 	of->f_opened_by_pid = pid;
204 	of->f_granted_access = op->desired_access;
205 	of->f_share_access = op->share_access;
206 	of->f_create_options = op->create_options;
207 	of->f_cr = (op->create_options & FILE_OPEN_FOR_BACKUP_INTENT) ?
208 	    smb_user_getprivcred(tree->t_user) : tree->t_user->u_cred;
209 	crhold(of->f_cr);
210 	of->f_ftype = ftype;
211 	of->f_server = tree->t_server;
212 	of->f_session = tree->t_user->u_session;
213 	of->f_user = tree->t_user;
214 	of->f_tree = tree;
215 	of->f_node = node;
216 	of->f_explicit_times = 0;
217 	mutex_init(&of->f_mutex, NULL, MUTEX_DEFAULT, NULL);
218 	of->f_state = SMB_OFILE_STATE_OPEN;
219 
220 
221 	if (ftype == SMB_FTYPE_MESG_PIPE) {
222 		of->f_pipe = smb_opipe_alloc(tree->t_server);
223 	} else {
224 		ASSERT(ftype == SMB_FTYPE_DISK); /* Regular file, not a pipe */
225 		ASSERT(node);
226 
227 		if (of->f_granted_access == FILE_EXECUTE)
228 			of->f_flags |= SMB_OFLAGS_EXECONLY;
229 
230 		bzero(&attr, sizeof (smb_attr_t));
231 		attr.sa_mask |= SMB_AT_UID;
232 		if (smb_fsop_getattr(NULL, kcred, node, &attr) != 0) {
233 			of->f_magic = 0;
234 			mutex_destroy(&of->f_mutex);
235 			crfree(of->f_cr);
236 			smb_idpool_free(&tree->t_fid_pool, of->f_fid);
237 			kmem_cache_free(tree->t_server->si_cache_ofile, of);
238 			err->status = NT_STATUS_INTERNAL_ERROR;
239 			err->errcls = ERRDOS;
240 			err->errcode = ERROR_INTERNAL_ERROR;
241 			return (NULL);
242 		}
243 		if (crgetuid(of->f_cr) == attr.sa_vattr.va_uid) {
244 			/*
245 			 * Add this bit for the file's owner even if it's not
246 			 * specified in the request (Windows behavior).
247 			 */
248 			of->f_granted_access |= FILE_READ_ATTRIBUTES;
249 		}
250 
251 		if (smb_node_is_file(node)) {
252 			of->f_mode =
253 			    smb_fsop_amask_to_omode(of->f_granted_access);
254 			if (smb_fsop_open(node, of->f_mode, of->f_cr) != 0) {
255 				of->f_magic = 0;
256 				mutex_destroy(&of->f_mutex);
257 				crfree(of->f_cr);
258 				smb_idpool_free(&tree->t_fid_pool, of->f_fid);
259 				kmem_cache_free(tree->t_server->si_cache_ofile,
260 				    of);
261 				err->status = NT_STATUS_ACCESS_DENIED;
262 				err->errcls = ERRDOS;
263 				err->errcode = ERROR_ACCESS_DENIED;
264 				return (NULL);
265 			}
266 		}
267 
268 		if (tree->t_flags & SMB_TREE_READONLY)
269 			of->f_flags |= SMB_OFLAGS_READONLY;
270 
271 		if (op->created_readonly)
272 			node->readonly_creator = of;
273 
274 		smb_node_inc_open_ofiles(node);
275 		smb_node_add_ofile(node, of);
276 		smb_node_ref(node);
277 	}
278 	smb_llist_enter(&tree->t_ofile_list, RW_WRITER);
279 	smb_llist_insert_tail(&tree->t_ofile_list, of);
280 	smb_llist_exit(&tree->t_ofile_list);
281 	atomic_inc_32(&tree->t_open_files);
282 	atomic_inc_32(&tree->t_server->sv_open_files);
283 	atomic_inc_32(&of->f_session->s_file_cnt);
284 
285 	return (of);
286 }
287 
288 /*
289  * smb_ofile_close
290  */
291 void
292 smb_ofile_close(smb_ofile_t *of, uint32_t last_wtime)
293 {
294 	ASSERT(of);
295 	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
296 	uint32_t flags = 0;
297 
298 	mutex_enter(&of->f_mutex);
299 	ASSERT(of->f_refcnt);
300 	switch (of->f_state) {
301 	case SMB_OFILE_STATE_OPEN: {
302 
303 		of->f_state = SMB_OFILE_STATE_CLOSING;
304 		mutex_exit(&of->f_mutex);
305 
306 		if (of->f_ftype == SMB_FTYPE_MESG_PIPE) {
307 			smb_opipe_close(of);
308 		} else {
309 			smb_ofile_set_close_attrs(of, last_wtime);
310 
311 			if (of->f_flags & SMB_OFLAGS_SET_DELETE_ON_CLOSE) {
312 				if (smb_tree_has_feature(of->f_tree,
313 				    SMB_TREE_CATIA)) {
314 					flags |= SMB_CATIA;
315 				}
316 				(void) smb_node_set_delete_on_close(of->f_node,
317 				    of->f_cr, flags);
318 			}
319 			smb_fsop_unshrlock(of->f_cr, of->f_node, of->f_uniqid);
320 			smb_node_destroy_lock_by_ofile(of->f_node, of);
321 
322 			if (smb_node_is_file(of->f_node))
323 				(void) smb_fsop_close(of->f_node, of->f_mode,
324 				    of->f_cr);
325 
326 			/*
327 			 * Cancel any notify change requests related
328 			 * to this open instance.
329 			 */
330 			if (of->f_node->flags & NODE_FLAGS_NOTIFY_CHANGE)
331 				smb_process_file_notify_change_queue(of);
332 		}
333 		atomic_dec_32(&of->f_tree->t_open_files);
334 		atomic_dec_32(&of->f_tree->t_server->sv_open_files);
335 
336 		mutex_enter(&of->f_mutex);
337 		ASSERT(of->f_refcnt);
338 		ASSERT(of->f_state == SMB_OFILE_STATE_CLOSING);
339 		of->f_state = SMB_OFILE_STATE_CLOSED;
340 		mutex_exit(&of->f_mutex);
341 		if (of->f_node != NULL) {
342 			smb_node_dec_open_ofiles(of->f_node);
343 			if (of->f_oplock_granted) {
344 				smb_oplock_release(of->f_node, of);
345 				of->f_oplock_granted = B_FALSE;
346 			}
347 		}
348 		return;
349 	}
350 	case SMB_OFILE_STATE_CLOSED:
351 	case SMB_OFILE_STATE_CLOSING:
352 		break;
353 
354 	default:
355 		ASSERT(0);
356 		break;
357 	}
358 	mutex_exit(&of->f_mutex);
359 }
360 
361 /*
362  * smb_ofile_close_all
363  *
364  *
365  */
366 void
367 smb_ofile_close_all(
368     smb_tree_t		*tree)
369 {
370 	smb_ofile_t	*of;
371 
372 	ASSERT(tree);
373 	ASSERT(tree->t_magic == SMB_TREE_MAGIC);
374 
375 	smb_llist_enter(&tree->t_ofile_list, RW_READER);
376 	of = smb_llist_head(&tree->t_ofile_list);
377 	while (of) {
378 		ASSERT(of->f_magic == SMB_OFILE_MAGIC);
379 		ASSERT(of->f_tree == tree);
380 		of = smb_ofile_close_and_next(of);
381 	}
382 	smb_llist_exit(&tree->t_ofile_list);
383 }
384 
385 /*
386  * smb_ofiles_close_by_pid
387  *
388  *
389  */
390 void
391 smb_ofile_close_all_by_pid(
392     smb_tree_t		*tree,
393     uint16_t		pid)
394 {
395 	smb_ofile_t	*of;
396 
397 	ASSERT(tree);
398 	ASSERT(tree->t_magic == SMB_TREE_MAGIC);
399 
400 	smb_llist_enter(&tree->t_ofile_list, RW_READER);
401 	of = smb_llist_head(&tree->t_ofile_list);
402 	while (of) {
403 		ASSERT(of->f_magic == SMB_OFILE_MAGIC);
404 		ASSERT(of->f_tree == tree);
405 		if (of->f_opened_by_pid == pid) {
406 			of = smb_ofile_close_and_next(of);
407 		} else {
408 			of = smb_llist_next(&tree->t_ofile_list, of);
409 		}
410 	}
411 	smb_llist_exit(&tree->t_ofile_list);
412 }
413 
414 /*
415  * If the enumeration request is for ofile data, handle it here.
416  * Otherwise, return.
417  *
418  * This function should be called with a hold on the ofile.
419  */
420 int
421 smb_ofile_enum(smb_ofile_t *of, smb_svcenum_t *svcenum)
422 {
423 	uint8_t *pb;
424 	uint_t nbytes;
425 	int rc;
426 
427 	ASSERT(of);
428 	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
429 	ASSERT(of->f_refcnt);
430 
431 	if (svcenum->se_type != SMB_SVCENUM_TYPE_FILE)
432 		return (0);
433 
434 	if (svcenum->se_nskip > 0) {
435 		svcenum->se_nskip--;
436 		return (0);
437 	}
438 
439 	if (svcenum->se_nitems >= svcenum->se_nlimit) {
440 		svcenum->se_nitems = svcenum->se_nlimit;
441 		return (0);
442 	}
443 
444 	pb = &svcenum->se_buf[svcenum->se_bused];
445 
446 	rc = smb_ofile_netinfo_encode(of, pb, svcenum->se_bavail,
447 	    &nbytes);
448 	if (rc == 0) {
449 		svcenum->se_bavail -= nbytes;
450 		svcenum->se_bused += nbytes;
451 		svcenum->se_nitems++;
452 	}
453 
454 	return (rc);
455 }
456 
457 /*
458  * Take a reference on an open file.
459  */
460 boolean_t
461 smb_ofile_hold(smb_ofile_t *of)
462 {
463 	ASSERT(of);
464 	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
465 
466 	mutex_enter(&of->f_mutex);
467 
468 	if (smb_ofile_is_open_locked(of)) {
469 		of->f_refcnt++;
470 		mutex_exit(&of->f_mutex);
471 		return (B_TRUE);
472 	}
473 
474 	mutex_exit(&of->f_mutex);
475 	return (B_FALSE);
476 }
477 
478 /*
479  * Release a reference on a file.  If the reference count falls to
480  * zero and the file has been closed, post the object for deletion.
481  * Object deletion is deferred to avoid modifying a list while an
482  * iteration may be in progress.
483  */
484 void
485 smb_ofile_release(smb_ofile_t	*of)
486 {
487 	boolean_t	rb;
488 
489 	SMB_OFILE_VALID(of);
490 
491 	mutex_enter(&of->f_mutex);
492 	if (of->f_oplock_exit) {
493 		mutex_exit(&of->f_mutex);
494 		rb = smb_oplock_broadcast(of->f_node);
495 		mutex_enter(&of->f_mutex);
496 		if (rb)
497 			of->f_oplock_exit = B_FALSE;
498 	}
499 	ASSERT(of->f_refcnt);
500 	of->f_refcnt--;
501 	switch (of->f_state) {
502 	case SMB_OFILE_STATE_OPEN:
503 	case SMB_OFILE_STATE_CLOSING:
504 		break;
505 
506 	case SMB_OFILE_STATE_CLOSED:
507 		if (of->f_refcnt == 0)
508 			smb_tree_post_ofile(of->f_tree, of);
509 		break;
510 
511 	default:
512 		ASSERT(0);
513 		break;
514 	}
515 	mutex_exit(&of->f_mutex);
516 }
517 
518 /*
519  * smb_ofile_lookup_by_fid
520  *
521  * Find the open file whose fid matches the one specified in the request.
522  * If we can't find the fid or the shares (trees) don't match, we have a
523  * bad fid.
524  */
525 smb_ofile_t *
526 smb_ofile_lookup_by_fid(
527     smb_tree_t		*tree,
528     uint16_t		fid)
529 {
530 	smb_llist_t	*of_list;
531 	smb_ofile_t	*of;
532 
533 	ASSERT(tree->t_magic == SMB_TREE_MAGIC);
534 
535 	of_list = &tree->t_ofile_list;
536 
537 	smb_llist_enter(of_list, RW_READER);
538 	of = smb_llist_head(of_list);
539 	while (of) {
540 		ASSERT(of->f_magic == SMB_OFILE_MAGIC);
541 		ASSERT(of->f_tree == tree);
542 		if (of->f_fid == fid) {
543 			mutex_enter(&of->f_mutex);
544 			if (of->f_state != SMB_OFILE_STATE_OPEN) {
545 				mutex_exit(&of->f_mutex);
546 				smb_llist_exit(of_list);
547 				return (NULL);
548 			}
549 			of->f_refcnt++;
550 			mutex_exit(&of->f_mutex);
551 			break;
552 		}
553 		of = smb_llist_next(of_list, of);
554 	}
555 	smb_llist_exit(of_list);
556 	return (of);
557 }
558 
559 /*
560  * smb_ofile_lookup_by_uniqid
561  *
562  * Find the open file whose uniqid matches the one specified in the request.
563  */
564 smb_ofile_t *
565 smb_ofile_lookup_by_uniqid(smb_tree_t *tree, uint32_t uniqid)
566 {
567 	smb_llist_t	*of_list;
568 	smb_ofile_t	*of;
569 
570 	ASSERT(tree->t_magic == SMB_TREE_MAGIC);
571 
572 	of_list = &tree->t_ofile_list;
573 	smb_llist_enter(of_list, RW_READER);
574 	of = smb_llist_head(of_list);
575 
576 	while (of) {
577 		ASSERT(of->f_magic == SMB_OFILE_MAGIC);
578 		ASSERT(of->f_tree == tree);
579 
580 		if (of->f_uniqid == uniqid) {
581 			if (smb_ofile_hold(of)) {
582 				smb_llist_exit(of_list);
583 				return (of);
584 			}
585 		}
586 
587 		of = smb_llist_next(of_list, of);
588 	}
589 
590 	smb_llist_exit(of_list);
591 	return (NULL);
592 }
593 
594 /*
595  * Disallow NetFileClose on certain ofiles to avoid side-effects.
596  * Closing a tree root is not allowed: use NetSessionDel or NetShareDel.
597  * Closing SRVSVC connections is not allowed because this NetFileClose
598  * request may depend on this ofile.
599  */
600 boolean_t
601 smb_ofile_disallow_fclose(smb_ofile_t *of)
602 {
603 	ASSERT(of);
604 	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
605 	ASSERT(of->f_refcnt);
606 
607 	switch (of->f_ftype) {
608 	case SMB_FTYPE_DISK:
609 		ASSERT(of->f_tree);
610 		return (of->f_node == of->f_tree->t_snode);
611 
612 	case SMB_FTYPE_MESG_PIPE:
613 		ASSERT(of->f_pipe);
614 		if (smb_strcasecmp(of->f_pipe->p_name, "SRVSVC", 0) == 0)
615 			return (B_TRUE);
616 		break;
617 	default:
618 		break;
619 	}
620 
621 	return (B_FALSE);
622 }
623 
624 /*
625  * smb_ofile_set_flags
626  *
627  * Return value:
628  *
629  *	Current flags value
630  *
631  */
632 void
633 smb_ofile_set_flags(
634     smb_ofile_t		*of,
635     uint32_t		flags)
636 {
637 	ASSERT(of);
638 	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
639 	ASSERT(of->f_refcnt);
640 
641 	mutex_enter(&of->f_mutex);
642 	of->f_flags |= flags;
643 	mutex_exit(&of->f_mutex);
644 }
645 
646 /*
647  * smb_ofile_seek
648  *
649  * Return value:
650  *
651  *	0		Success
652  *	EINVAL		Unknown mode
653  *	EOVERFLOW	offset too big
654  *
655  */
656 int
657 smb_ofile_seek(
658     smb_ofile_t		*of,
659     ushort_t		mode,
660     int32_t		off,
661     uint32_t		*retoff)
662 {
663 	u_offset_t	newoff = 0;
664 	int		rc = 0;
665 	smb_attr_t	attr;
666 
667 	ASSERT(of);
668 	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
669 	ASSERT(of->f_refcnt);
670 
671 	mutex_enter(&of->f_mutex);
672 	switch (mode) {
673 	case SMB_SEEK_SET:
674 		if (off < 0)
675 			newoff = 0;
676 		else
677 			newoff = (u_offset_t)off;
678 		break;
679 
680 	case SMB_SEEK_CUR:
681 		if (off < 0 && (-off) > of->f_seek_pos)
682 			newoff = 0;
683 		else
684 			newoff = of->f_seek_pos + (u_offset_t)off;
685 		break;
686 
687 	case SMB_SEEK_END:
688 		bzero(&attr, sizeof (smb_attr_t));
689 		attr.sa_mask |= SMB_AT_SIZE;
690 		rc = smb_fsop_getattr(NULL, kcred, of->f_node, &attr);
691 		if (rc != 0) {
692 			mutex_exit(&of->f_mutex);
693 			return (rc);
694 		}
695 		if (off < 0 && (-off) > attr.sa_vattr.va_size)
696 			newoff = 0;
697 		else
698 			newoff = attr.sa_vattr.va_size + (u_offset_t)off;
699 		break;
700 
701 	default:
702 		mutex_exit(&of->f_mutex);
703 		return (EINVAL);
704 	}
705 
706 	/*
707 	 * See comments at the beginning of smb_seek.c.
708 	 * If the offset is greater than UINT_MAX, we will return an error.
709 	 */
710 
711 	if (newoff > UINT_MAX) {
712 		rc = EOVERFLOW;
713 	} else {
714 		of->f_seek_pos = newoff;
715 		*retoff = (uint32_t)newoff;
716 	}
717 	mutex_exit(&of->f_mutex);
718 	return (rc);
719 }
720 
721 /*
722  * smb_ofile_is_open
723  */
724 boolean_t
725 smb_ofile_is_open(smb_ofile_t *of)
726 {
727 	boolean_t	rc;
728 
729 	SMB_OFILE_VALID(of);
730 
731 	mutex_enter(&of->f_mutex);
732 	rc = smb_ofile_is_open_locked(of);
733 	mutex_exit(&of->f_mutex);
734 	return (rc);
735 }
736 
737 void
738 smb_ofile_set_oplock_granted(smb_ofile_t *of)
739 {
740 	SMB_OFILE_VALID(of);
741 	mutex_enter(&of->f_mutex);
742 	ASSERT(!of->f_oplock_granted);
743 	of->f_oplock_granted = B_TRUE;
744 	of->f_oplock_exit = B_TRUE;
745 	mutex_exit(&of->f_mutex);
746 }
747 
748 /*
749  * smb_ofile_pending_write_time
750  *
751  * Flag write times as pending - to be set on close, setattr
752  * or delayed write timer.
753  */
754 void
755 smb_ofile_set_write_time_pending(smb_ofile_t *of)
756 {
757 	SMB_OFILE_VALID(of);
758 	mutex_enter(&of->f_mutex);
759 	of->f_flags |= SMB_OFLAGS_TIMESTAMPS_PENDING;
760 	mutex_exit(&of->f_mutex);
761 }
762 
763 /*
764  * smb_ofile_write_time_pending
765  *
766  * Get and reset the write times pending flag.
767  */
768 boolean_t
769 smb_ofile_write_time_pending(smb_ofile_t *of)
770 {
771 	boolean_t rc = B_FALSE;
772 
773 	SMB_OFILE_VALID(of);
774 	mutex_enter(&of->f_mutex);
775 	if (of->f_flags & SMB_OFLAGS_TIMESTAMPS_PENDING) {
776 		rc = B_TRUE;
777 		of->f_flags &= ~SMB_OFLAGS_TIMESTAMPS_PENDING;
778 	}
779 	mutex_exit(&of->f_mutex);
780 
781 	return (rc);
782 }
783 
784 /*
785  * smb_ofile_set_explicit_time_flag
786  *
787  * Note the timestamps specified in "what", as having been
788  * explicity set for the ofile.
789  */
790 void
791 smb_ofile_set_explicit_times(smb_ofile_t *of, uint32_t what)
792 {
793 	SMB_OFILE_VALID(of);
794 	mutex_enter(&of->f_mutex);
795 	of->f_explicit_times |= (what & SMB_AT_TIMES);
796 	mutex_exit(&of->f_mutex);
797 }
798 
799 uint32_t
800 smb_ofile_explicit_times(smb_ofile_t *of)
801 {
802 	uint32_t rc;
803 
804 	SMB_OFILE_VALID(of);
805 	mutex_enter(&of->f_mutex);
806 	rc = of->f_explicit_times;
807 	mutex_exit(&of->f_mutex);
808 
809 	return (rc);
810 }
811 
812 /* *************************** Static Functions ***************************** */
813 
814 /*
815  * Determine whether or not an ofile is open.
816  * This function must be called with the mutex held.
817  */
818 static boolean_t
819 smb_ofile_is_open_locked(smb_ofile_t *of)
820 {
821 	switch (of->f_state) {
822 	case SMB_OFILE_STATE_OPEN:
823 		return (B_TRUE);
824 
825 	case SMB_OFILE_STATE_CLOSING:
826 	case SMB_OFILE_STATE_CLOSED:
827 		return (B_FALSE);
828 
829 	default:
830 		ASSERT(0);
831 		return (B_FALSE);
832 	}
833 }
834 
835 /*
836  * smb_ofile_set_close_attrs
837  *
838  * Updates timestamps, size and readonly bit.
839  * The last_wtime is specified in the request received
840  * from the client. If it is neither 0 nor -1, this time
841  * should be used as the file's mtime. It must first be
842  * converted from the server's localtime (as received in
843  * the client's request) to GMT.
844  *
845  * Call smb_node_setattr even if no attributes are being
846  * explicitly set, to set any pending attributes.
847  */
848 static void
849 smb_ofile_set_close_attrs(smb_ofile_t *of, uint32_t last_wtime)
850 {
851 	smb_node_t *node = of->f_node;
852 	smb_attr_t attr;
853 
854 	bzero(&attr, sizeof (smb_attr_t));
855 
856 	/* For files created readonly, propagate readonly bit */
857 	if (node->readonly_creator == of) {
858 		attr.sa_mask |= SMB_AT_DOSATTR;
859 		if (smb_fsop_getattr(NULL, kcred, node, &attr) &&
860 		    (attr.sa_dosattr & FILE_ATTRIBUTE_READONLY)) {
861 			attr.sa_mask = 0;
862 		} else {
863 			attr.sa_dosattr |= FILE_ATTRIBUTE_READONLY;
864 		}
865 
866 		node->readonly_creator = NULL;
867 	}
868 
869 	/* apply last_wtime if specified */
870 	if (last_wtime != 0 && last_wtime != 0xFFFFFFFF) {
871 		attr.sa_vattr.va_mtime.tv_sec =
872 		    last_wtime + of->f_server->si_gmtoff;
873 		attr.sa_mask |= SMB_AT_MTIME;
874 	}
875 
876 	(void) smb_node_setattr(NULL, node, of->f_cr, of, &attr);
877 }
878 
879 /*
880  * This function closes the file passed in (if appropriate) and returns the
881  * next open file in the list of open files of the tree of the open file passed
882  * in. It requires that the list of open files of the tree be entered in
883  * RW_READER mode before being called.
884  */
885 static smb_ofile_t *
886 smb_ofile_close_and_next(smb_ofile_t *of)
887 {
888 	smb_ofile_t	*next_of;
889 	smb_tree_t	*tree;
890 
891 	ASSERT(of);
892 	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
893 
894 	mutex_enter(&of->f_mutex);
895 	switch (of->f_state) {
896 	case SMB_OFILE_STATE_OPEN:
897 		/* The file is still open. */
898 		of->f_refcnt++;
899 		ASSERT(of->f_refcnt);
900 		tree = of->f_tree;
901 		mutex_exit(&of->f_mutex);
902 		smb_llist_exit(&of->f_tree->t_ofile_list);
903 		smb_ofile_close(of, 0);
904 		smb_ofile_release(of);
905 		smb_llist_enter(&tree->t_ofile_list, RW_READER);
906 		next_of = smb_llist_head(&tree->t_ofile_list);
907 		break;
908 	case SMB_OFILE_STATE_CLOSING:
909 	case SMB_OFILE_STATE_CLOSED:
910 		/*
911 		 * The ofile exists but is closed or
912 		 * in the process being closed.
913 		 */
914 		mutex_exit(&of->f_mutex);
915 		next_of = smb_llist_next(&of->f_tree->t_ofile_list, of);
916 		break;
917 	default:
918 		ASSERT(0);
919 		mutex_exit(&of->f_mutex);
920 		next_of = smb_llist_next(&of->f_tree->t_ofile_list, of);
921 		break;
922 	}
923 	return (next_of);
924 }
925 
926 /*
927  * Delete an ofile.
928  *
929  * Remove the ofile from the tree list before freeing resources
930  * associated with the ofile.
931  */
932 void
933 smb_ofile_delete(void *arg)
934 {
935 	smb_tree_t	*tree;
936 	smb_ofile_t	*of = (smb_ofile_t *)arg;
937 
938 	SMB_OFILE_VALID(of);
939 	ASSERT(of->f_refcnt == 0);
940 	ASSERT(of->f_state == SMB_OFILE_STATE_CLOSED);
941 
942 	tree = of->f_tree;
943 	smb_llist_enter(&tree->t_ofile_list, RW_WRITER);
944 	smb_llist_remove(&tree->t_ofile_list, of);
945 	smb_idpool_free(&tree->t_fid_pool, of->f_fid);
946 	atomic_dec_32(&tree->t_session->s_file_cnt);
947 	smb_llist_exit(&tree->t_ofile_list);
948 
949 	mutex_enter(&of->f_mutex);
950 	mutex_exit(&of->f_mutex);
951 
952 	if (of->f_ftype == SMB_FTYPE_MESG_PIPE) {
953 		smb_opipe_dealloc(of->f_pipe);
954 		of->f_pipe = NULL;
955 	} else {
956 		ASSERT(of->f_ftype == SMB_FTYPE_DISK);
957 		ASSERT(of->f_node != NULL);
958 		smb_node_rem_ofile(of->f_node, of);
959 		smb_node_release(of->f_node);
960 	}
961 
962 	of->f_magic = (uint32_t)~SMB_OFILE_MAGIC;
963 	mutex_destroy(&of->f_mutex);
964 	crfree(of->f_cr);
965 	kmem_cache_free(of->f_tree->t_server->si_cache_ofile, of);
966 }
967 
968 /*
969  * smb_ofile_access
970  *
971  * This function will check to see if the access requested is granted.
972  * Returns NT status codes.
973  */
974 uint32_t
975 smb_ofile_access(smb_ofile_t *of, cred_t *cr, uint32_t access)
976 {
977 
978 	if ((of == NULL) || (cr == kcred))
979 		return (NT_STATUS_SUCCESS);
980 
981 	/*
982 	 * If the request is for something
983 	 * I don't grant it is an error
984 	 */
985 	if (~(of->f_granted_access) & access) {
986 		if (!(of->f_granted_access & ACCESS_SYSTEM_SECURITY) &&
987 		    (access & ACCESS_SYSTEM_SECURITY)) {
988 			return (NT_STATUS_PRIVILEGE_NOT_HELD);
989 		}
990 		return (NT_STATUS_ACCESS_DENIED);
991 	}
992 
993 	return (NT_STATUS_SUCCESS);
994 }
995 
996 
997 /*
998  * check file sharing rules for current open request
999  * against existing open instances of the same file
1000  *
1001  * Returns NT_STATUS_SHARING_VIOLATION if there is any
1002  * sharing conflict, otherwise returns NT_STATUS_SUCCESS.
1003  */
1004 uint32_t
1005 smb_ofile_open_check(smb_ofile_t *of, uint32_t desired_access,
1006     uint32_t share_access)
1007 {
1008 	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
1009 
1010 	mutex_enter(&of->f_mutex);
1011 
1012 	if (of->f_state != SMB_OFILE_STATE_OPEN) {
1013 		mutex_exit(&of->f_mutex);
1014 		return (NT_STATUS_INVALID_HANDLE);
1015 	}
1016 
1017 	/* if it's just meta data */
1018 	if ((of->f_granted_access & FILE_DATA_ALL) == 0) {
1019 		mutex_exit(&of->f_mutex);
1020 		return (NT_STATUS_SUCCESS);
1021 	}
1022 
1023 	/*
1024 	 * Check requested share access against the
1025 	 * open granted (desired) access
1026 	 */
1027 	if (SMB_DENY_DELETE(share_access) && (of->f_granted_access & DELETE)) {
1028 		mutex_exit(&of->f_mutex);
1029 		return (NT_STATUS_SHARING_VIOLATION);
1030 	}
1031 
1032 	if (SMB_DENY_READ(share_access) &&
1033 	    (of->f_granted_access & (FILE_READ_DATA | FILE_EXECUTE))) {
1034 		mutex_exit(&of->f_mutex);
1035 		return (NT_STATUS_SHARING_VIOLATION);
1036 	}
1037 
1038 	if (SMB_DENY_WRITE(share_access) &&
1039 	    (of->f_granted_access & (FILE_WRITE_DATA | FILE_APPEND_DATA))) {
1040 		mutex_exit(&of->f_mutex);
1041 		return (NT_STATUS_SHARING_VIOLATION);
1042 	}
1043 
1044 	/* check requested desired access against the open share access */
1045 	if (SMB_DENY_DELETE(of->f_share_access) && (desired_access & DELETE)) {
1046 		mutex_exit(&of->f_mutex);
1047 		return (NT_STATUS_SHARING_VIOLATION);
1048 	}
1049 
1050 	if (SMB_DENY_READ(of->f_share_access) &&
1051 	    (desired_access & (FILE_READ_DATA | FILE_EXECUTE))) {
1052 		mutex_exit(&of->f_mutex);
1053 		return (NT_STATUS_SHARING_VIOLATION);
1054 	}
1055 
1056 	if (SMB_DENY_WRITE(of->f_share_access) &&
1057 	    (desired_access & (FILE_WRITE_DATA | FILE_APPEND_DATA))) {
1058 		mutex_exit(&of->f_mutex);
1059 		return (NT_STATUS_SHARING_VIOLATION);
1060 	}
1061 
1062 	mutex_exit(&of->f_mutex);
1063 	return (NT_STATUS_SUCCESS);
1064 }
1065 
1066 /*
1067  * smb_ofile_rename_check
1068  *
1069  * An open file can be renamed if
1070  *
1071  *  1. isn't opened for data writing or deleting
1072  *
1073  *  2. Opened with "Deny Delete" share mode
1074  *         But not opened for data reading or executing
1075  *         (opened for accessing meta data)
1076  */
1077 
1078 uint32_t
1079 smb_ofile_rename_check(smb_ofile_t *of)
1080 {
1081 	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
1082 
1083 	mutex_enter(&of->f_mutex);
1084 
1085 	if (of->f_state != SMB_OFILE_STATE_OPEN) {
1086 		mutex_exit(&of->f_mutex);
1087 		return (NT_STATUS_INVALID_HANDLE);
1088 	}
1089 
1090 	if (of->f_granted_access &
1091 	    (FILE_WRITE_DATA | FILE_APPEND_DATA | DELETE)) {
1092 		mutex_exit(&of->f_mutex);
1093 		return (NT_STATUS_SHARING_VIOLATION);
1094 	}
1095 
1096 	if ((of->f_share_access & FILE_SHARE_DELETE) == 0) {
1097 		if (of->f_granted_access &
1098 		    (FILE_READ_DATA | FILE_EXECUTE)) {
1099 			mutex_exit(&of->f_mutex);
1100 			return (NT_STATUS_SHARING_VIOLATION);
1101 		}
1102 	}
1103 
1104 	mutex_exit(&of->f_mutex);
1105 	return (NT_STATUS_SUCCESS);
1106 }
1107 
1108 /*
1109  * smb_ofile_delete_check
1110  *
1111  * An open file can be deleted only if opened for
1112  * accessing meta data. Share modes aren't important
1113  * in this case.
1114  *
1115  * NOTE: there is another mechanism for deleting an
1116  * open file that NT clients usually use.
1117  * That's setting "Delete on close" flag for an open
1118  * file.  In this way the file will be deleted after
1119  * last close. This flag can be set by SmbTrans2SetFileInfo
1120  * with FILE_DISPOSITION_INFO information level.
1121  * For setting this flag, the file should be opened by
1122  * DELETE access in the FID that is passed in the Trans2
1123  * request.
1124  */
1125 
1126 uint32_t
1127 smb_ofile_delete_check(smb_ofile_t *of)
1128 {
1129 	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
1130 
1131 	mutex_enter(&of->f_mutex);
1132 
1133 	if (of->f_state != SMB_OFILE_STATE_OPEN) {
1134 		mutex_exit(&of->f_mutex);
1135 		return (NT_STATUS_INVALID_HANDLE);
1136 	}
1137 
1138 	if (of->f_granted_access &
1139 	    (FILE_READ_DATA | FILE_WRITE_DATA |
1140 	    FILE_APPEND_DATA | FILE_EXECUTE | DELETE)) {
1141 		mutex_exit(&of->f_mutex);
1142 		return (NT_STATUS_SHARING_VIOLATION);
1143 	}
1144 
1145 	mutex_exit(&of->f_mutex);
1146 	return (NT_STATUS_SUCCESS);
1147 }
1148 
1149 cred_t *
1150 smb_ofile_getcred(smb_ofile_t *of)
1151 {
1152 	return (of->f_cr);
1153 }
1154 
1155 /*
1156  * smb_ofile_set_delete_on_close
1157  *
1158  * Set the DeleteOnClose flag on the smb file. When the file is closed,
1159  * the flag will be transferred to the smb node, which will commit the
1160  * delete operation and inhibit subsequent open requests.
1161  *
1162  * When DeleteOnClose is set on an smb_node, the common open code will
1163  * reject subsequent open requests for the file. Observation of Windows
1164  * 2000 indicates that subsequent opens should be allowed (assuming
1165  * there would be no sharing violation) until the file is closed using
1166  * the fid on which the DeleteOnClose was requested.
1167  */
1168 void
1169 smb_ofile_set_delete_on_close(smb_ofile_t *of)
1170 {
1171 	mutex_enter(&of->f_mutex);
1172 	of->f_flags |= SMB_OFLAGS_SET_DELETE_ON_CLOSE;
1173 	mutex_exit(&of->f_mutex);
1174 }
1175 
1176 /*
1177  * Encode open file information into a buffer; needed in user space to
1178  * support RPC requests.
1179  */
1180 static int
1181 smb_ofile_netinfo_encode(smb_ofile_t *of, uint8_t *buf, size_t buflen,
1182     uint32_t *nbytes)
1183 {
1184 	smb_netfileinfo_t	fi;
1185 	int			rc;
1186 
1187 	rc = smb_ofile_netinfo_init(of, &fi);
1188 	if (rc == 0) {
1189 		rc = smb_netfileinfo_encode(&fi, buf, buflen, nbytes);
1190 		smb_ofile_netinfo_fini(&fi);
1191 	}
1192 
1193 	return (rc);
1194 }
1195 
1196 static int
1197 smb_ofile_netinfo_init(smb_ofile_t *of, smb_netfileinfo_t *fi)
1198 {
1199 	smb_user_t	*user;
1200 	smb_tree_t	*tree;
1201 	smb_node_t	*node;
1202 	char		*path;
1203 	char		*buf;
1204 	int		rc;
1205 
1206 	ASSERT(of);
1207 	user = of->f_user;
1208 	tree = of->f_tree;
1209 	ASSERT(user);
1210 	ASSERT(tree);
1211 
1212 	buf = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1213 
1214 	switch (of->f_ftype) {
1215 	case SMB_FTYPE_DISK:
1216 		node = of->f_node;
1217 		ASSERT(node);
1218 
1219 		fi->fi_permissions = of->f_granted_access;
1220 		fi->fi_numlocks = smb_lock_get_lock_count(node);
1221 
1222 		path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1223 
1224 		if (node != tree->t_snode) {
1225 			rc = vnodetopath(tree->t_snode->vp, node->vp, path,
1226 			    MAXPATHLEN, kcred);
1227 			if (rc == 0)
1228 				(void) strsubst(path, '/', '\\');
1229 			else
1230 				(void) strlcpy(path, node->od_name, MAXPATHLEN);
1231 		}
1232 
1233 		(void) snprintf(buf, MAXPATHLEN, "%s:%s", tree->t_sharename,
1234 		    path);
1235 		kmem_free(path, MAXPATHLEN);
1236 		break;
1237 
1238 	case SMB_FTYPE_MESG_PIPE:
1239 		ASSERT(of->f_pipe);
1240 
1241 		fi->fi_permissions = FILE_READ_DATA | FILE_WRITE_DATA |
1242 		    FILE_EXECUTE;
1243 		fi->fi_numlocks = 0;
1244 		(void) snprintf(buf, MAXPATHLEN, "\\PIPE\\%s",
1245 		    of->f_pipe->p_name);
1246 		break;
1247 
1248 	default:
1249 		kmem_free(buf, MAXPATHLEN);
1250 		return (-1);
1251 	}
1252 
1253 	fi->fi_fid = of->f_fid;
1254 	fi->fi_uniqid = of->f_uniqid;
1255 	fi->fi_pathlen = strlen(buf) + 1;
1256 	fi->fi_path = smb_mem_strdup(buf);
1257 	kmem_free(buf, MAXPATHLEN);
1258 
1259 	fi->fi_namelen = user->u_domain_len + user->u_name_len + 2;
1260 	fi->fi_username = kmem_alloc(fi->fi_namelen, KM_SLEEP);
1261 	(void) snprintf(fi->fi_username, fi->fi_namelen, "%s\\%s",
1262 	    user->u_domain, user->u_name);
1263 	return (0);
1264 }
1265 
1266 static void
1267 smb_ofile_netinfo_fini(smb_netfileinfo_t *fi)
1268 {
1269 	if (fi == NULL)
1270 		return;
1271 
1272 	if (fi->fi_path)
1273 		smb_mem_free(fi->fi_path);
1274 	if (fi->fi_username)
1275 		kmem_free(fi->fi_username, fi->fi_namelen);
1276 
1277 	bzero(fi, sizeof (smb_netfileinfo_t));
1278 }
1279 
1280 /*
1281  * A query of user and group quotas may span multiple requests.
1282  * f_quota_resume is used to determine where the query should
1283  * be resumed, in a subsequent request. f_quota_resume contains
1284  * the SID of the last quota entry returned to the client.
1285  */
1286 void
1287 smb_ofile_set_quota_resume(smb_ofile_t *ofile, char *resume)
1288 {
1289 	ASSERT(ofile);
1290 	mutex_enter(&ofile->f_mutex);
1291 	if (resume == NULL)
1292 		bzero(ofile->f_quota_resume, SMB_SID_STRSZ);
1293 	else
1294 		(void) strlcpy(ofile->f_quota_resume, resume, SMB_SID_STRSZ);
1295 	mutex_exit(&ofile->f_mutex);
1296 }
1297 
1298 void
1299 smb_ofile_get_quota_resume(smb_ofile_t *ofile, char *buf, int bufsize)
1300 {
1301 	ASSERT(ofile);
1302 	mutex_enter(&ofile->f_mutex);
1303 	(void) strlcpy(buf, ofile->f_quota_resume, bufsize);
1304 	mutex_exit(&ofile->f_mutex);
1305 }
1306