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