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