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