xref: /titanic_52/usr/src/uts/common/fs/smbsrv/smb_ofile.c (revision aff4bce51ecc47df7e5a6351b7cee6bc20408c63)
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 		if (of->f_node != NULL) {
344 			smb_node_dec_open_ofiles(of->f_node);
345 			if (of->f_oplock_granted) {
346 				smb_oplock_release(of->f_node, of);
347 				of->f_oplock_granted = B_FALSE;
348 			}
349 		}
350 		mutex_exit(&of->f_mutex);
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(
487     smb_ofile_t		*of)
488 {
489 	ASSERT(of);
490 	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
491 
492 	mutex_enter(&of->f_mutex);
493 	if (of->f_oplock_exit)
494 		if (smb_oplock_broadcast(of->f_node))
495 			of->f_oplock_exit = B_FALSE;
496 	ASSERT(of->f_refcnt);
497 	of->f_refcnt--;
498 	switch (of->f_state) {
499 	case SMB_OFILE_STATE_OPEN:
500 	case SMB_OFILE_STATE_CLOSING:
501 		break;
502 
503 	case SMB_OFILE_STATE_CLOSED:
504 		if (of->f_refcnt == 0) {
505 			mutex_exit(&of->f_mutex);
506 			smb_ofile_delete(of);
507 			return;
508 		}
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 (utf8_strcasecmp(of->f_pipe->p_name, "SRVSVC") == 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  * smb_ofile_seek
647  *
648  * Return value:
649  *
650  *	0		Success
651  *	EINVAL		Unknown mode
652  *	EOVERFLOW	offset too big
653  *
654  */
655 int
656 smb_ofile_seek(
657     smb_ofile_t		*of,
658     ushort_t		mode,
659     int32_t		off,
660     uint32_t		*retoff)
661 {
662 	u_offset_t	newoff = 0;
663 	int		rc = 0;
664 	smb_attr_t	attr;
665 
666 	ASSERT(of);
667 	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
668 	ASSERT(of->f_refcnt);
669 
670 	mutex_enter(&of->f_mutex);
671 	switch (mode) {
672 	case SMB_SEEK_SET:
673 		if (off < 0)
674 			newoff = 0;
675 		else
676 			newoff = (u_offset_t)off;
677 		break;
678 
679 	case SMB_SEEK_CUR:
680 		if (off < 0 && (-off) > of->f_seek_pos)
681 			newoff = 0;
682 		else
683 			newoff = of->f_seek_pos + (u_offset_t)off;
684 		break;
685 
686 	case SMB_SEEK_END:
687 		bzero(&attr, sizeof (smb_attr_t));
688 		attr.sa_mask |= SMB_AT_SIZE;
689 		rc = smb_fsop_getattr(NULL, kcred, of->f_node, &attr);
690 		if (rc != 0) {
691 			mutex_exit(&of->f_mutex);
692 			return (rc);
693 		}
694 		if (off < 0 && (-off) > attr.sa_vattr.va_size)
695 			newoff = 0;
696 		else
697 			newoff = attr.sa_vattr.va_size + (u_offset_t)off;
698 		break;
699 
700 	default:
701 		mutex_exit(&of->f_mutex);
702 		return (EINVAL);
703 	}
704 
705 	/*
706 	 * See comments at the beginning of smb_seek.c.
707 	 * If the offset is greater than UINT_MAX, we will return an error.
708 	 */
709 
710 	if (newoff > UINT_MAX) {
711 		rc = EOVERFLOW;
712 	} else {
713 		of->f_seek_pos = newoff;
714 		*retoff = (uint32_t)newoff;
715 	}
716 	mutex_exit(&of->f_mutex);
717 	return (rc);
718 }
719 
720 /*
721  * smb_ofile_is_open
722  */
723 boolean_t
724 smb_ofile_is_open(smb_ofile_t *of)
725 {
726 	boolean_t	rc;
727 
728 	SMB_OFILE_VALID(of);
729 
730 	mutex_enter(&of->f_mutex);
731 	rc = smb_ofile_is_open_locked(of);
732 	mutex_exit(&of->f_mutex);
733 	return (rc);
734 }
735 
736 void
737 smb_ofile_set_oplock_granted(smb_ofile_t *of)
738 {
739 	SMB_OFILE_VALID(of);
740 	mutex_enter(&of->f_mutex);
741 	ASSERT(!of->f_oplock_granted);
742 	of->f_oplock_granted = B_TRUE;
743 	of->f_oplock_exit = B_TRUE;
744 	mutex_exit(&of->f_mutex);
745 }
746 
747 /*
748  * smb_ofile_pending_write_time
749  *
750  * Flag write times as pending - to be set on close, setattr
751  * or delayed write timer.
752  */
753 void
754 smb_ofile_set_write_time_pending(smb_ofile_t *of)
755 {
756 	SMB_OFILE_VALID(of);
757 	mutex_enter(&of->f_mutex);
758 	of->f_flags |= SMB_OFLAGS_TIMESTAMPS_PENDING;
759 	mutex_exit(&of->f_mutex);
760 }
761 
762 boolean_t
763 smb_ofile_write_time_pending(smb_ofile_t *of)
764 {
765 	boolean_t rc = B_FALSE;
766 
767 	SMB_OFILE_VALID(of);
768 	mutex_enter(&of->f_mutex);
769 	if (of->f_flags & SMB_OFLAGS_TIMESTAMPS_PENDING)
770 		rc = B_TRUE;
771 	mutex_exit(&of->f_mutex);
772 
773 	return (rc);
774 }
775 
776 /*
777  * smb_ofile_set_explicit_time_flag
778  *
779  * Note the timestamps specified in "what", as having been
780  * explicity set for the ofile. Also clear the flag for pending
781  * timestamps as the pending timestamps will have been applied
782  * by the explicit set.
783  */
784 void
785 smb_ofile_set_explicit_times(smb_ofile_t *of, uint32_t what)
786 {
787 	SMB_OFILE_VALID(of);
788 	mutex_enter(&of->f_mutex);
789 	of->f_flags &= ~SMB_OFLAGS_TIMESTAMPS_PENDING;
790 	of->f_explicit_times |= (what & SMB_AT_TIMES);
791 	mutex_exit(&of->f_mutex);
792 }
793 
794 uint32_t
795 smb_ofile_explicit_times(smb_ofile_t *of)
796 {
797 	uint32_t rc;
798 
799 	SMB_OFILE_VALID(of);
800 	mutex_enter(&of->f_mutex);
801 	rc = of->f_explicit_times;
802 	mutex_exit(&of->f_mutex);
803 
804 	return (rc);
805 }
806 
807 /* *************************** Static Functions ***************************** */
808 
809 /*
810  * Determine whether or not an ofile is open.
811  * This function must be called with the mutex held.
812  */
813 static boolean_t
814 smb_ofile_is_open_locked(smb_ofile_t *of)
815 {
816 	switch (of->f_state) {
817 	case SMB_OFILE_STATE_OPEN:
818 		return (B_TRUE);
819 
820 	case SMB_OFILE_STATE_CLOSING:
821 	case SMB_OFILE_STATE_CLOSED:
822 		return (B_FALSE);
823 
824 	default:
825 		ASSERT(0);
826 		return (B_FALSE);
827 	}
828 }
829 
830 /*
831  * smb_ofile_set_close_attrs
832  *
833  * Updates timestamps, size and readonly bit.
834  * The last_wtime is specified in the request received
835  * from the client. If it is neither 0 nor -1, this time
836  * should be used as the file's mtime. It must first be
837  * converted from the server's localtime (as received in
838  * the client's request) to GMT.
839  *
840  * Call smb_node_setattr even if no attributes are being
841  * explicitly set, to set any pending attributes.
842  */
843 static void
844 smb_ofile_set_close_attrs(smb_ofile_t *of, uint32_t last_wtime)
845 {
846 	smb_node_t *node = of->f_node;
847 	smb_attr_t attr;
848 
849 	bzero(&attr, sizeof (smb_attr_t));
850 
851 	/* For files created readonly, propagate readonly bit */
852 	if (node->readonly_creator == of) {
853 		attr.sa_mask |= SMB_AT_DOSATTR;
854 		if (smb_fsop_getattr(NULL, kcred, node, &attr) &&
855 		    (attr.sa_dosattr & FILE_ATTRIBUTE_READONLY)) {
856 			attr.sa_mask = 0;
857 		} else {
858 			attr.sa_dosattr |= FILE_ATTRIBUTE_READONLY;
859 		}
860 
861 		node->readonly_creator = NULL;
862 	}
863 
864 	/* apply last_wtime if specified */
865 	if (last_wtime != 0 && last_wtime != 0xFFFFFFFF) {
866 		attr.sa_vattr.va_mtime.tv_sec =
867 		    last_wtime + of->f_server->si_gmtoff;
868 		attr.sa_mask |= SMB_AT_MTIME;
869 	}
870 
871 	(void) smb_node_setattr(NULL, node, of->f_cr, of, &attr);
872 }
873 
874 /*
875  * smb_ofile_close_and_next
876  *
877  * This function closes the file passed in (if appropriate) and returns the
878  * next open file in the list of open files of the tree of the open file passed
879  * in. It requires that the list of open files of the tree be entered in
880  * RW_READER mode before being called.
881  */
882 static smb_ofile_t *
883 smb_ofile_close_and_next(smb_ofile_t *of)
884 {
885 	smb_ofile_t	*next_of;
886 	smb_tree_t	*tree;
887 
888 	ASSERT(of);
889 	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
890 
891 	mutex_enter(&of->f_mutex);
892 	switch (of->f_state) {
893 	case SMB_OFILE_STATE_OPEN:
894 		/* The file is still open. */
895 		of->f_refcnt++;
896 		ASSERT(of->f_refcnt);
897 		tree = of->f_tree;
898 		mutex_exit(&of->f_mutex);
899 		smb_llist_exit(&of->f_tree->t_ofile_list);
900 		smb_ofile_close(of, 0);
901 		smb_ofile_release(of);
902 		smb_llist_enter(&tree->t_ofile_list, RW_READER);
903 		next_of = smb_llist_head(&tree->t_ofile_list);
904 		break;
905 	case SMB_OFILE_STATE_CLOSING:
906 	case SMB_OFILE_STATE_CLOSED:
907 		/*
908 		 * The ofile exists but is closed or
909 		 * in the process being closed.
910 		 */
911 		mutex_exit(&of->f_mutex);
912 		next_of = smb_llist_next(&of->f_tree->t_ofile_list, of);
913 		break;
914 	default:
915 		ASSERT(0);
916 		mutex_exit(&of->f_mutex);
917 		next_of = smb_llist_next(&of->f_tree->t_ofile_list, of);
918 		break;
919 	}
920 	return (next_of);
921 }
922 
923 /*
924  * smb_ofile_delete
925  *
926  *
927  */
928 static void
929 smb_ofile_delete(smb_ofile_t *of)
930 {
931 	ASSERT(of);
932 	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
933 	ASSERT(of->f_refcnt == 0);
934 	ASSERT(of->f_state == SMB_OFILE_STATE_CLOSED);
935 
936 	/*
937 	 * Let's remove the ofile from the list of ofiles of the tree. This has
938 	 * to be done before any resources associated with the ofile are
939 	 * released.
940 	 */
941 	smb_llist_enter(&of->f_tree->t_ofile_list, RW_WRITER);
942 	smb_llist_remove(&of->f_tree->t_ofile_list, of);
943 	smb_llist_exit(&of->f_tree->t_ofile_list);
944 	atomic_dec_32(&of->f_session->s_file_cnt);
945 
946 	if (of->f_ftype == SMB_FTYPE_MESG_PIPE) {
947 		kmem_free(of->f_pipe, sizeof (smb_opipe_t));
948 		of->f_pipe = NULL;
949 	} else {
950 		ASSERT(of->f_ftype == SMB_FTYPE_DISK);
951 		ASSERT(of->f_node != NULL);
952 		smb_node_rem_ofile(of->f_node, of);
953 		smb_node_release(of->f_node);
954 	}
955 
956 	of->f_magic = (uint32_t)~SMB_OFILE_MAGIC;
957 	mutex_destroy(&of->f_mutex);
958 	crfree(of->f_cr);
959 	smb_idpool_free(&of->f_tree->t_fid_pool, of->f_fid);
960 	kmem_cache_free(of->f_tree->t_server->si_cache_ofile, of);
961 }
962 
963 /*
964  * smb_ofile_access
965  *
966  * This function will check to see if the access requested is granted.
967  * Returns NT status codes.
968  */
969 uint32_t
970 smb_ofile_access(smb_ofile_t *of, cred_t *cr, uint32_t access)
971 {
972 
973 	if ((of == NULL) || (cr == kcred))
974 		return (NT_STATUS_SUCCESS);
975 
976 	/*
977 	 * If the request is for something
978 	 * I don't grant it is an error
979 	 */
980 	if (~(of->f_granted_access) & access) {
981 		if (!(of->f_granted_access & ACCESS_SYSTEM_SECURITY) &&
982 		    (access & ACCESS_SYSTEM_SECURITY)) {
983 			return (NT_STATUS_PRIVILEGE_NOT_HELD);
984 		}
985 		return (NT_STATUS_ACCESS_DENIED);
986 	}
987 
988 	return (NT_STATUS_SUCCESS);
989 }
990 
991 
992 /*
993  * smb_ofile_open_check
994  *
995  * check file sharing rules for current open request
996  * against existing open instances of the same file
997  *
998  * Returns NT_STATUS_SHARING_VIOLATION if there is any
999  * sharing conflict, otherwise returns NT_STATUS_SUCCESS.
1000  */
1001 uint32_t
1002 smb_ofile_open_check(
1003     smb_ofile_t *of,
1004     cred_t *cr,
1005     uint32_t desired_access,
1006     uint32_t share_access)
1007 {
1008 	smb_node_t *node;
1009 
1010 	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
1011 
1012 	node = of->f_node;
1013 
1014 	mutex_enter(&of->f_mutex);
1015 
1016 	if (of->f_state !=  SMB_OFILE_STATE_OPEN) {
1017 		mutex_exit(&of->f_mutex);
1018 		return (NT_STATUS_INVALID_HANDLE);
1019 	}
1020 
1021 	/*
1022 	 * It appears that share modes are not relevant to
1023 	 * directories, but this check will remain as it is not
1024 	 * clear whether it was originally put here for a reason.
1025 	 */
1026 	if (smb_node_is_dir(node)) {
1027 		if (SMB_DENY_RW(of->f_share_access) &&
1028 		    (node->n_orig_uid != crgetuid(cr))) {
1029 			mutex_exit(&of->f_mutex);
1030 			return (NT_STATUS_SHARING_VIOLATION);
1031 		}
1032 
1033 		mutex_exit(&of->f_mutex);
1034 		return (NT_STATUS_SUCCESS);
1035 	}
1036 
1037 	/* if it's just meta data */
1038 	if ((of->f_granted_access & FILE_DATA_ALL) == 0) {
1039 		mutex_exit(&of->f_mutex);
1040 		return (NT_STATUS_SUCCESS);
1041 	}
1042 
1043 	/*
1044 	 * Check requested share access against the
1045 	 * open granted (desired) access
1046 	 */
1047 	if (SMB_DENY_DELETE(share_access) && (of->f_granted_access & DELETE)) {
1048 		mutex_exit(&of->f_mutex);
1049 		return (NT_STATUS_SHARING_VIOLATION);
1050 	}
1051 
1052 	if (SMB_DENY_READ(share_access) &&
1053 	    (of->f_granted_access & (FILE_READ_DATA | FILE_EXECUTE))) {
1054 		mutex_exit(&of->f_mutex);
1055 		return (NT_STATUS_SHARING_VIOLATION);
1056 	}
1057 
1058 	if (SMB_DENY_WRITE(share_access) &&
1059 	    (of->f_granted_access & (FILE_WRITE_DATA | FILE_APPEND_DATA))) {
1060 		mutex_exit(&of->f_mutex);
1061 		return (NT_STATUS_SHARING_VIOLATION);
1062 	}
1063 
1064 	/* check requested desired access against the open share access */
1065 	if (SMB_DENY_DELETE(of->f_share_access) && (desired_access & DELETE)) {
1066 		mutex_exit(&of->f_mutex);
1067 		return (NT_STATUS_SHARING_VIOLATION);
1068 	}
1069 
1070 	if (SMB_DENY_READ(of->f_share_access) &&
1071 	    (desired_access & (FILE_READ_DATA | FILE_EXECUTE))) {
1072 		mutex_exit(&of->f_mutex);
1073 		return (NT_STATUS_SHARING_VIOLATION);
1074 	}
1075 
1076 	if (SMB_DENY_WRITE(of->f_share_access) &&
1077 	    (desired_access & (FILE_WRITE_DATA | FILE_APPEND_DATA))) {
1078 		mutex_exit(&of->f_mutex);
1079 		return (NT_STATUS_SHARING_VIOLATION);
1080 	}
1081 
1082 	mutex_exit(&of->f_mutex);
1083 	return (NT_STATUS_SUCCESS);
1084 }
1085 
1086 /*
1087  * smb_ofile_rename_check
1088  *
1089  * An open file can be renamed if
1090  *
1091  *  1. isn't opened for data writing or deleting
1092  *
1093  *  2. Opened with "Deny Delete" share mode
1094  *         But not opened for data reading or executing
1095  *         (opened for accessing meta data)
1096  */
1097 
1098 uint32_t
1099 smb_ofile_rename_check(smb_ofile_t *of)
1100 {
1101 	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
1102 
1103 	mutex_enter(&of->f_mutex);
1104 
1105 	if (of->f_state != SMB_OFILE_STATE_OPEN) {
1106 		mutex_exit(&of->f_mutex);
1107 		return (NT_STATUS_INVALID_HANDLE);
1108 	}
1109 
1110 	if (of->f_granted_access &
1111 	    (FILE_WRITE_DATA | FILE_APPEND_DATA | DELETE)) {
1112 		mutex_exit(&of->f_mutex);
1113 		return (NT_STATUS_SHARING_VIOLATION);
1114 	}
1115 
1116 	if ((of->f_share_access & FILE_SHARE_DELETE) == 0) {
1117 		if (of->f_granted_access &
1118 		    (FILE_READ_DATA | FILE_EXECUTE)) {
1119 			mutex_exit(&of->f_mutex);
1120 			return (NT_STATUS_SHARING_VIOLATION);
1121 		}
1122 	}
1123 
1124 	mutex_exit(&of->f_mutex);
1125 	return (NT_STATUS_SUCCESS);
1126 }
1127 
1128 /*
1129  * smb_ofile_delete_check
1130  *
1131  * An open file can be deleted only if opened for
1132  * accessing meta data. Share modes aren't important
1133  * in this case.
1134  *
1135  * NOTE: there is another mechanism for deleting an
1136  * open file that NT clients usually use.
1137  * That's setting "Delete on close" flag for an open
1138  * file.  In this way the file will be deleted after
1139  * last close. This flag can be set by SmbTrans2SetFileInfo
1140  * with FILE_DISPOSITION_INFO information level.
1141  * For setting this flag, the file should be opened by
1142  * DELETE access in the FID that is passed in the Trans2
1143  * request.
1144  */
1145 
1146 uint32_t
1147 smb_ofile_delete_check(smb_ofile_t *of)
1148 {
1149 	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
1150 
1151 	mutex_enter(&of->f_mutex);
1152 
1153 	if (of->f_state != SMB_OFILE_STATE_OPEN) {
1154 		mutex_exit(&of->f_mutex);
1155 		return (NT_STATUS_INVALID_HANDLE);
1156 	}
1157 
1158 	if (of->f_granted_access &
1159 	    (FILE_READ_DATA | FILE_WRITE_DATA |
1160 	    FILE_APPEND_DATA | FILE_EXECUTE | DELETE)) {
1161 		mutex_exit(&of->f_mutex);
1162 		return (NT_STATUS_SHARING_VIOLATION);
1163 	}
1164 
1165 	mutex_exit(&of->f_mutex);
1166 	return (NT_STATUS_SUCCESS);
1167 }
1168 
1169 cred_t *
1170 smb_ofile_getcred(smb_ofile_t *of)
1171 {
1172 	return (of->f_cr);
1173 }
1174 
1175 /*
1176  * smb_ofile_set_delete_on_close
1177  *
1178  * Set the DeleteOnClose flag on the smb file. When the file is closed,
1179  * the flag will be transferred to the smb node, which will commit the
1180  * delete operation and inhibit subsequent open requests.
1181  *
1182  * When DeleteOnClose is set on an smb_node, the common open code will
1183  * reject subsequent open requests for the file. Observation of Windows
1184  * 2000 indicates that subsequent opens should be allowed (assuming
1185  * there would be no sharing violation) until the file is closed using
1186  * the fid on which the DeleteOnClose was requested.
1187  */
1188 void
1189 smb_ofile_set_delete_on_close(smb_ofile_t *of)
1190 {
1191 	mutex_enter(&of->f_mutex);
1192 	of->f_flags |= SMB_OFLAGS_SET_DELETE_ON_CLOSE;
1193 	mutex_exit(&of->f_mutex);
1194 }
1195 
1196 /*
1197  * Encode open file information into a buffer; needed in user space to
1198  * support RPC requests.
1199  */
1200 static int
1201 smb_ofile_netinfo_encode(smb_ofile_t *of, uint8_t *buf, size_t buflen,
1202     uint32_t *nbytes)
1203 {
1204 	smb_netfileinfo_t	fi;
1205 	int			rc;
1206 
1207 	rc = smb_ofile_netinfo_init(of, &fi);
1208 	if (rc == 0) {
1209 		rc = smb_netfileinfo_encode(&fi, buf, buflen, nbytes);
1210 		smb_ofile_netinfo_fini(&fi);
1211 	}
1212 
1213 	return (rc);
1214 }
1215 
1216 static int
1217 smb_ofile_netinfo_init(smb_ofile_t *of, smb_netfileinfo_t *fi)
1218 {
1219 	smb_user_t	*user;
1220 	smb_tree_t	*tree;
1221 	smb_node_t	*node;
1222 	char		*path;
1223 	char		*buf;
1224 	int		rc;
1225 
1226 	ASSERT(of);
1227 	user = of->f_user;
1228 	tree = of->f_tree;
1229 	ASSERT(user);
1230 	ASSERT(tree);
1231 
1232 	buf = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1233 
1234 	switch (of->f_ftype) {
1235 	case SMB_FTYPE_DISK:
1236 		node = of->f_node;
1237 		ASSERT(node);
1238 
1239 		fi->fi_permissions = of->f_granted_access;
1240 		fi->fi_numlocks = smb_lock_get_lock_count(node);
1241 
1242 		path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1243 
1244 		if (node != tree->t_snode) {
1245 			rc = vnodetopath(tree->t_snode->vp, node->vp, path,
1246 			    MAXPATHLEN, kcred);
1247 			if (rc == 0)
1248 				(void) strsubst(path, '/', '\\');
1249 			else
1250 				(void) strlcpy(path, node->od_name, MAXPATHLEN);
1251 		}
1252 
1253 		(void) snprintf(buf, MAXPATHLEN, "%s:%s", tree->t_sharename,
1254 		    path);
1255 		kmem_free(path, MAXPATHLEN);
1256 		break;
1257 
1258 	case SMB_FTYPE_MESG_PIPE:
1259 		ASSERT(of->f_pipe);
1260 
1261 		fi->fi_permissions = FILE_READ_DATA | FILE_WRITE_DATA |
1262 		    FILE_EXECUTE;
1263 		fi->fi_numlocks = 0;
1264 		(void) snprintf(buf, MAXPATHLEN, "\\PIPE\\%s",
1265 		    of->f_pipe->p_name);
1266 		break;
1267 
1268 	default:
1269 		kmem_free(buf, MAXPATHLEN);
1270 		return (-1);
1271 	}
1272 
1273 	fi->fi_fid = of->f_fid;
1274 	fi->fi_uniqid = of->f_uniqid;
1275 	fi->fi_pathlen = strlen(buf) + 1;
1276 	fi->fi_path = smb_kstrdup(buf, fi->fi_pathlen);
1277 	kmem_free(buf, MAXPATHLEN);
1278 
1279 	fi->fi_namelen = user->u_domain_len + user->u_name_len + 2;
1280 	fi->fi_username = kmem_alloc(fi->fi_namelen, KM_SLEEP);
1281 	(void) snprintf(fi->fi_username, fi->fi_namelen, "%s\\%s",
1282 	    user->u_domain, user->u_name);
1283 	return (0);
1284 }
1285 
1286 static void
1287 smb_ofile_netinfo_fini(smb_netfileinfo_t *fi)
1288 {
1289 	if (fi == NULL)
1290 		return;
1291 
1292 	if (fi->fi_path)
1293 		kmem_free(fi->fi_path, fi->fi_pathlen);
1294 	if (fi->fi_username)
1295 		kmem_free(fi->fi_username, fi->fi_namelen);
1296 
1297 	bzero(fi, sizeof (smb_netfileinfo_t));
1298 }
1299