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