xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_ofile.c (revision 11eb49180ece0464cd4d507dc5c8e61131e3096a)
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 2011-2020 Tintri by DDN, Inc. All rights reserved.
24  * Copyright 2016 Syneto S.R.L. All rights reserved.
25  * Copyright (c) 2016 by Delphix. All rights reserved.
26  * Copyright 2021-2023 RackTop Systems, Inc.
27  */
28 
29 /*
30  * General Structures Layout
31  * -------------------------
32  *
33  * This is a simplified diagram showing the relationship between most of the
34  * main structures.
35  *
36  * +-------------------+
37  * |     SMB_INFO      |
38  * +-------------------+
39  *          |
40  *          |
41  *          v
42  * +-------------------+       +-------------------+      +-------------------+
43  * |     SESSION       |<----->|     SESSION       |......|      SESSION      |
44  * +-------------------+       +-------------------+      +-------------------+
45  *   |          |
46  *   |          |
47  *   |          v
48  *   |  +-------------------+     +-------------------+   +-------------------+
49  *   |  |       USER        |<--->|       USER        |...|       USER        |
50  *   |  +-------------------+     +-------------------+   +-------------------+
51  *   |
52  *   |
53  *   v
54  * +-------------------+       +-------------------+      +-------------------+
55  * |       TREE        |<----->|       TREE        |......|       TREE        |
56  * +-------------------+       +-------------------+      +-------------------+
57  *      |         |
58  *      |         |
59  *      |         v
60  *      |     +-------+       +-------+      +-------+
61  *      |     | OFILE |<----->| OFILE |......| OFILE |
62  *      |     +-------+       +-------+      +-------+
63  *      |
64  *      |
65  *      v
66  *  +-------+       +------+      +------+
67  *  | ODIR  |<----->| ODIR |......| ODIR |
68  *  +-------+       +------+      +------+
69  *
70  *
71  * Ofile State Machine
72  * ------------------
73  *
74  *    +-------------------------+	 T0
75  *    |  SMB_OFILE_STATE_OPEN   |<--+-------- Creation/Allocation
76  *    +-------------------------+   |
77  *	    |		|	    | T5
78  *	    |		|	+---------------------------+
79  *	    |		|	| SMB_OFILE_STATE_RECONNECT |
80  *	    |		|	+---------------------------+
81  *	    |		|	    ^
82  *	    |		v	    |
83  *	    |   +---------------+   |
84  *	    |   | STATE_SAVE_DH |   |
85  *	    |   | STATE_SAVING  |   |
86  *	    |   +---------------+   |
87  *	    |		|	    | T4
88  *	    | T1	| T3	+--------------------------+
89  *	    |		+------>| SMB_OFILE_STATE_ORPHANED |
90  *	    v			+--------------------------+
91  *    +-------------------------+   |		|
92  *    | SMB_OFILE_STATE_CLOSING |<--+ T6	| T7
93  *    +-------------------------+		|
94  *	    |		^			v
95  *	    | T2	| T8	+-------------------------+
96  *	    |		+-------| SMB_OFILE_STATE_EXPIRED |
97  *	    v			+-------------------------+
98  *    +-------------------------+
99  *    | SMB_OFILE_STATE_CLOSED  |----------> Deletion/Free
100  *    +-------------------------+    T9
101  *
102  * SMB_OFILE_STATE_OPEN
103  *
104  *    While in this state:
105  *      - The ofile is queued in the list of ofiles of its tree.
106  *      - References will be given out if the ofile is looked up.
107  *
108  * SMB_OFILE_STATE_SAVE_DH
109  *
110  *    Similar to state _CLOSING, but instead of deleting the ofile,
111  *    it leaves the ofile in state _ORPHANED (for later reclaim).
112  *    Will move to _SAVING after last ref, then _ORPHANED.
113  *
114  *    While in this state:
115  *	- The ofile has been marked for preservation during a
116  *	  walk of the tree ofile list to close multiple files.
117  *	- References will not be given out if the ofile is looked up,
118  *	  except for oplock break processing.
119  *	- Still affects Sharing Violation rules
120  *
121  * SMB_OFILE_STATE_SAVING
122  *
123  *    Transient state used to keep oplock break processing out
124  *    while the ofile moves to state _ORPHANED.
125  *
126  *    While in this state:
127  *	- References will not be given out if the ofile is looked up,
128  *	  except for oplock break processing.
129  *	- Still affects Sharing Violation rules
130  *
131  * SMB_OFILE_STATE_CLOSING
132  *
133  *    Close has been requested.  Stay in this state until the last
134  *    ref. is gone, then move to state _CLOSED
135  *
136  *    While in this state:
137  *      - The ofile is queued in the list of ofiles of its tree.
138  *      - References will not be given out if the ofile is looked up.
139  *      - The file is closed and the locks held are being released.
140  *      - The resources associated with the ofile remain.
141  *
142  * SMB_OFILE_STATE_CLOSED
143  *
144  *    While in this state:
145  *      - The ofile is queued in the list of ofiles of its tree.
146  *      - References will not be given out if the ofile is looked up.
147  *      - The resources associated with the ofile remain.
148  *
149  * SMB_OFILE_STATE_ORPHANED
150  *
151  *    While in this state:
152  *      - The ofile is queued in the list of ofiles of its tree.
153  *      - Can be reclaimed by the original owner
154  *      - References will not be given out if the ofile is looked up.
155  *      - All the tree, user, and session "up" pointers are NULL!
156  *      - Will eventually be "expired" if not reclaimed
157  *      - Can be closed if its oplock is broken
158  *      - Still affects Sharing Violation rules
159  *
160  * SMB_OFILE_STATE_EXPIRED
161  *
162  *    While in this state:
163  *      - The ofile is queued in the list of ofiles of its tree.
164  *      - References will not be given out if the ofile is looked up.
165  *      - The ofile has not been reclaimed and will soon be closed,
166  *        due to, for example, the durable handle timer expiring, or its
167  *        oplock being broken.
168  *      - Cannot be reclaimed at this point
169  *
170  * SMB_OFILE_STATE_RECONNECT
171  *
172  *    Transient state used to keep oplock break processing out
173  *    while the ofile moves from state _ORPHANED to _OPEN.
174  *
175  *    While in this state:
176  *      - The ofile is being reclaimed; do not touch it.
177  *      - References will not be given out if the ofile is looked up.
178  *      - Still affects Sharing Violation rules
179  *	- see smb2_dh_reconnect() for which members need to be avoided
180  *
181  * Transition T0
182  *
183  *    This transition occurs in smb_ofile_open(). A new ofile is created and
184  *    added to the list of ofiles of a tree.
185  *
186  * Transition T1
187  *
188  *    This transition occurs in smb_ofile_close(). Note that this only happens
189  *    when we determine that an ofile should be closed in spite of its durable
190  *    handle properties.
191  *
192  * Transition T2
193  *
194  *    This transition occurs in smb_ofile_release(). The resources associated
195  *    with the ofile are freed as well as the ofile structure. For the
196  *    transition to occur, the ofile must be in the SMB_OFILE_STATE_CLOSED
197  *    state and the reference count be zero.
198  *
199  * Transition T3
200  *
201  *    This transition occurs in smb_ofile_orphan_dh(). It happens during an
202  *    smb2 logoff, or during a session disconnect when certain conditions are
203  *    met. The ofile and structures above it will be kept around until the ofile
204  *    either gets reclaimed, expires after f_timeout_offset nanoseconds, or its
205  *    oplock is broken.
206  *
207  * Transition T4
208  *
209  *    This transition occurs in smb2_dh_reconnect(). An smb2 create request
210  *    with a DURABLE_HANDLE_RECONNECT(_V2) create context has been
211  *    recieved from the original owner. If leases are supported or it's
212  *    RECONNECT_V2, reconnect is subject to additional conditions. The ofile
213  *    will be unwired from the old, disconnected session, tree, and user,
214  *    and wired up to its new context.
215  *
216  * Transition T5
217  *
218  *    This transition occurs in smb2_dh_reconnect(). The ofile has been
219  *    successfully reclaimed.
220  *
221  * Transition T6
222  *
223  *    This transition occurs in smb_ofile_close(). The ofile has been orphaned
224  *    while some thread was blocked, and that thread closes the ofile. Can only
225  *    happen when the ofile is orphaned due to an SMB2 LOGOFF request.
226  *
227  * Transition T7
228  *
229  *    This transition occurs in smb_session_durable_timers() and
230  *    smb_oplock_send_break(). The ofile will soon be closed.
231  *    In the former case, f_timeout_offset nanoseconds have passed since
232  *    the ofile was orphaned. In the latter, an oplock break occured
233  *    on the ofile while it was orphaned.
234  *
235  * Transition T8
236  *
237  *    This transition occurs in smb_ofile_close().
238  *
239  * Transition T9
240  *
241  *    This transition occurs in smb_ofile_delete().
242  *
243  * Comments
244  * --------
245  *
246  *    The state machine of the ofile structures is controlled by 3 elements:
247  *      - The list of ofiles of the tree it belongs to.
248  *      - The mutex embedded in the structure itself.
249  *      - The reference count.
250  *
251  *    There's a mutex embedded in the ofile structure used to protect its fields
252  *    and there's a lock embedded in the list of ofiles of a tree. To
253  *    increment or to decrement the reference count the mutex must be entered.
254  *    To insert the ofile into the list of ofiles of the tree and to remove
255  *    the ofile from it, the lock must be entered in RW_WRITER mode.
256  *
257  *    Rules of access to a ofile structure:
258  *
259  *    1) In order to avoid deadlocks, when both (mutex and lock of the ofile
260  *       list) have to be entered, the lock must be entered first. Additionally,
261  *       f_mutex must not be held when removing the ofile from sv_persistid_ht.
262  *
263  *    2) All actions applied to an ofile require a reference count.
264  *
265  *    3) There are 2 ways of getting a reference count. One is when the ofile
266  *       is opened. The other one when the ofile is looked up. This translates
267  *       into 2 functions: smb_ofile_open() and smb_ofile_lookup_by_fid().
268  *
269  *    It should be noted that the reference count of an ofile registers the
270  *    number of references to the ofile in other structures (such as an smb
271  *    request). The reference count is not incremented in these 2 instances:
272  *
273  *    1) The ofile is open. An ofile is anchored by its state. If there's
274  *       no activity involving an ofile currently open, the reference count
275  *       of that ofile is zero.
276  *
277  *    2) The ofile is queued in the list of ofiles of its tree. The fact of
278  *       being queued in that list is NOT registered by incrementing the
279  *       reference count.
280  */
281 #include <smbsrv/smb2_kproto.h>
282 #include <smbsrv/smb_fsops.h>
283 #include <sys/time.h>
284 #include <sys/random.h>
285 
286 static boolean_t smb_ofile_is_open_locked(smb_ofile_t *);
287 static void smb_ofile_delete(void *arg);
288 static void smb_ofile_save_dh(void *arg);
289 
290 static int smb_ofile_netinfo_encode(smb_ofile_t *, uint8_t *, size_t,
291     uint32_t *);
292 static int smb_ofile_netinfo_init(smb_ofile_t *, smb_netfileinfo_t *);
293 static void smb_ofile_netinfo_fini(smb_netfileinfo_t *);
294 
295 /*
296  * The uniq_fid is a CIFS-server-wide unique identifier for an ofile
297  * which is used to uniquely identify open instances for the
298  * VFS share reservation and POSIX locks.
299  */
300 static volatile uint32_t smb_fids = 0;
301 #define	SMB_UNIQ_FID()	atomic_inc_32_nv(&smb_fids)
302 
303 /*
304  * AVL Comparator for t_ofile_list
305  *
306  * NB: can only access f_fid because smb_ofile_lookup_by_fid
307  * doesn't pass in an entire smb_ofile_t.
308  */
309 int
smb_ofile_avl_compare(const void * v1,const void * v2)310 smb_ofile_avl_compare(const void *v1, const void *v2)
311 {
312 	const smb_ofile_t *of1 = v1;
313 	const smb_ofile_t *of2 = v2;
314 
315 	return (AVL_CMP(of1->f_fid, of2->f_fid));
316 }
317 
318 /*
319  * smb_ofile_alloc
320  * Allocate an ofile and fill in it's "up" pointers, but
321  * do NOT link it into the tree's list of ofiles or the
322  * node's list of ofiles.  An ofile in this state is a
323  * "proposed" open passed to the oplock break code.
324  *
325  * If we don't get as far as smb_ofile_open with this OF,
326  * call smb_ofile_free() to free this object.
327  *
328  * Note: The following sr members may be null during
329  * persistent handle import: session, uid_usr, tid_tree
330  */
331 smb_ofile_t *
smb_ofile_alloc(smb_request_t * sr,smb_arg_open_t * op,smb_node_t * node,uint16_t ftype,uint16_t tree_fid)332 smb_ofile_alloc(
333     smb_request_t	*sr,
334     smb_arg_open_t	*op,
335     smb_node_t		*node, /* optional (may be NULL) */
336     uint16_t		ftype,
337     uint16_t		tree_fid)
338 {
339 	smb_user_t	*user = sr->uid_user;	/* optional */
340 	smb_tree_t	*tree = sr->tid_tree;	/* optional */
341 	smb_ofile_t	*of;
342 
343 	of = kmem_cache_alloc(smb_cache_ofile, KM_SLEEP);
344 	bzero(of, sizeof (smb_ofile_t));
345 	of->f_magic = SMB_OFILE_MAGIC;
346 
347 	mutex_init(&of->f_mutex, NULL, MUTEX_DEFAULT, NULL);
348 	list_create(&of->f_notify.nc_waiters, sizeof (smb_request_t),
349 	    offsetof(smb_request_t, sr_waiters));
350 	mutex_init(&of->dh_nvlock, NULL, MUTEX_DEFAULT, NULL);
351 
352 	of->f_state = SMB_OFILE_STATE_ALLOC;
353 	of->f_refcnt = 1;
354 	of->f_ftype = ftype;
355 	of->f_fid = tree_fid;
356 	/* of->f_persistid see smb2_create */
357 	of->f_uniqid = SMB_UNIQ_FID();
358 	of->f_opened_by_pid = sr->smb_pid;
359 	of->f_granted_access = op->desired_access;
360 	of->f_share_access = op->share_access;
361 	of->f_create_options = op->create_options;
362 	if (user != NULL) {
363 		if ((op->create_options & FILE_OPEN_FOR_BACKUP_INTENT) != 0)
364 			of->f_cr = smb_user_getprivcred(user);
365 		else
366 			of->f_cr = user->u_cred;
367 		crhold(of->f_cr);
368 	}
369 	of->f_server = sr->sr_server;
370 	of->f_session = sr->session;	/* may be NULL */
371 
372 	(void) memset(of->f_lock_seq, -1, SMB_OFILE_LSEQ_MAX);
373 
374 	of->f_mode = smb_fsop_amask_to_omode(of->f_granted_access);
375 	if ((of->f_granted_access & FILE_DATA_ALL) == FILE_EXECUTE)
376 		of->f_flags |= SMB_OFLAGS_EXECONLY;
377 
378 	/*
379 	 * In case a lease is requested, copy the lease keys now so
380 	 * any oplock breaks during open don't break those on our
381 	 * other handles that might have the same lease.
382 	 */
383 	bcopy(op->lease_key, of->TargetOplockKey, SMB_LEASE_KEY_SZ);
384 	bcopy(op->parent_lease_key, of->ParentOplockKey, SMB_LEASE_KEY_SZ);
385 
386 	/*
387 	 * grab a ref for of->f_user and of->f_tree
388 	 * We know the user and tree must be "live" because
389 	 * this SR holds references to them.  The node ref. is
390 	 * held by our caller, until smb_ofile_open puts this
391 	 * ofile on the node ofile list with smb_node_add_ofile.
392 	 */
393 	if (user != NULL) {
394 		smb_user_hold_internal(user);
395 		of->f_user = user;
396 	}
397 	if (tree != NULL) {
398 		smb_tree_hold_internal(tree);
399 		of->f_tree = tree;
400 	}
401 	of->f_node = node;	/* may be NULL */
402 
403 	return (of);
404 }
405 
406 /*
407  * smb_ofile_open
408  *
409  * Complete an open on an ofile that was previously allocated by
410  * smb_ofile_alloc, by putting it on the tree ofile list and
411  * (if it's a file) the node ofile list.
412  */
413 void
smb_ofile_open(smb_request_t * sr,smb_arg_open_t * op,smb_ofile_t * of)414 smb_ofile_open(
415     smb_request_t	*sr,
416     smb_arg_open_t	*op,
417     smb_ofile_t		*of)
418 {
419 	smb_tree_t	*tree = sr->tid_tree;
420 	smb_node_t	*node = of->f_node;
421 
422 	ASSERT(of->f_state == SMB_OFILE_STATE_ALLOC);
423 	ASSERT(of->f_fid != 0);
424 
425 	of->f_state = SMB_OFILE_STATE_OPEN;
426 
427 	switch (of->f_ftype) {
428 	case SMB_FTYPE_BYTE_PIPE:
429 	case SMB_FTYPE_MESG_PIPE:
430 		/* See smb_opipe_open. */
431 		of->f_pipe = op->pipe;
432 		smb_server_inc_pipes(of->f_server);
433 		break;
434 	case SMB_FTYPE_DISK:
435 	case SMB_FTYPE_PRINTER:
436 		/* Regular file, not a pipe */
437 		ASSERT(node != NULL);
438 
439 		smb_node_inc_open_ofiles(node);
440 		smb_node_add_ofile(node, of);
441 		smb_node_ref(node);
442 		smb_server_inc_files(of->f_server);
443 		break;
444 	default:
445 		ASSERT(0);
446 	}
447 	ASSERT(of->f_fid != 0);
448 	smb_lavl_enter(&tree->t_ofile_list, RW_WRITER);
449 	smb_lavl_insert(&tree->t_ofile_list, of);
450 	smb_lavl_exit(&tree->t_ofile_list);
451 	atomic_inc_32(&tree->t_open_files);
452 	atomic_inc_32(&of->f_session->s_file_cnt);
453 
454 }
455 
456 /*
457  * smb_ofile_close
458  *
459  * Incoming states: (where from)
460  *   SMB_OFILE_STATE_OPEN  protocol close, smb_ofile_drop
461  *   SMB_OFILE_STATE_EXPIRED  called via smb2_dh_expire
462  *   SMB_OFILE_STATE_ORPHANED  smb2_dh_shutdown
463  *
464  * Not that this enters the of->node->n_ofile_list rwlock as reader,
465  * (via smb_oplock_close) so this must not be called while holding
466  * that rwlock.
467  */
468 void
smb_ofile_close(smb_ofile_t * of,int32_t mtime_sec)469 smb_ofile_close(smb_ofile_t *of, int32_t mtime_sec)
470 {
471 	smb_attr_t *pa;
472 
473 	SMB_OFILE_VALID(of);
474 
475 	if (of->f_ftype == SMB_FTYPE_DISK) {
476 		smb_oplock_close(of);
477 	}
478 
479 	mutex_enter(&of->f_mutex);
480 	ASSERT(of->f_refcnt);
481 
482 	switch (of->f_state) {
483 	case SMB_OFILE_STATE_OPEN:
484 	case SMB_OFILE_STATE_ORPHANED:
485 	case SMB_OFILE_STATE_EXPIRED:
486 		of->f_state = SMB_OFILE_STATE_CLOSING;
487 		mutex_exit(&of->f_mutex);
488 		break;
489 	default:
490 		mutex_exit(&of->f_mutex);
491 		return;
492 	}
493 
494 	/*
495 	 * Only one thread here (the one that that set f_state closing)
496 	 */
497 	switch (of->f_ftype) {
498 	case SMB_FTYPE_BYTE_PIPE:
499 	case SMB_FTYPE_MESG_PIPE:
500 		smb_opipe_close(of);
501 		smb_server_dec_pipes(of->f_server);
502 		break;
503 
504 	case SMB_FTYPE_DISK:
505 		if (of->dh_persist)
506 			smb2_dh_close_persistent(of);
507 		if (of->f_persistid != 0)
508 			smb_ofile_del_persistid(of);
509 		/* FALLTHROUGH */
510 
511 	case SMB_FTYPE_PRINTER: /* or FTYPE_DISK */
512 		/*
513 		 * In here we make changes to of->f_pending_attr
514 		 * while not holding of->f_mutex.  This is OK
515 		 * because we've changed f_state to CLOSING,
516 		 * so no more threads will take this path.
517 		 */
518 		pa = &of->f_pending_attr;
519 		if (mtime_sec != 0) {
520 			pa->sa_vattr.va_mtime.tv_sec = mtime_sec;
521 			pa->sa_mask |= SMB_AT_MTIME;
522 		}
523 
524 		if (of->f_flags & SMB_OFLAGS_SET_DELETE_ON_CLOSE) {
525 			/* We delete using the on-disk name. */
526 			uint32_t flags = SMB_CASE_SENSITIVE;
527 			(void) smb_node_set_delete_on_close(of->f_node,
528 			    of->f_cr, flags);
529 		}
530 		smb_fsop_unshrlock(of->f_cr, of->f_node, of->f_uniqid);
531 		smb_node_destroy_lock_by_ofile(of->f_node, of);
532 
533 		if (smb_node_is_file(of->f_node)) {
534 			(void) smb_fsop_close(of->f_node, of->f_mode,
535 			    of->f_cr);
536 		} else {
537 			/*
538 			 * If there was an odir, close it.
539 			 */
540 			if (of->f_odir != NULL)
541 				smb_odir_close(of->f_odir);
542 			/*
543 			 * Cancel any notify change requests that
544 			 * might be watching this open file (dir),
545 			 * and unsubscribe it from node events.
546 			 *
547 			 * Can't hold f_mutex when calling smb_notify_ofile.
548 			 * Don't really need it when unsubscribing, but
549 			 * harmless, and consistent with subscribing.
550 			 */
551 			if (of->f_notify.nc_subscribed)
552 				smb_notify_ofile(of,
553 				    FILE_ACTION_HANDLE_CLOSED, NULL);
554 			mutex_enter(&of->f_mutex);
555 			if (of->f_notify.nc_subscribed) {
556 				of->f_notify.nc_subscribed = B_FALSE;
557 				smb_node_fcn_unsubscribe(of->f_node);
558 				of->f_notify.nc_filter = 0;
559 			}
560 			mutex_exit(&of->f_mutex);
561 		}
562 		if (smb_node_dec_open_ofiles(of->f_node) == 0) {
563 			/*
564 			 * Last close.  If we're not deleting
565 			 * the file, apply any pending attrs.
566 			 * Leave allocsz zero when no open files,
567 			 * just to avoid confusion, because it's
568 			 * only updated when there are opens.
569 			 * XXX: Just do this on _every_ close.
570 			 */
571 			mutex_enter(&of->f_node->n_mutex);
572 			if (of->f_node->flags & NODE_FLAGS_DELETE_ON_CLOSE) {
573 				smb_node_delete_on_close(of->f_node);
574 				pa->sa_mask = 0;
575 			}
576 			of->f_node->n_allocsz = 0;
577 			mutex_exit(&of->f_node->n_mutex);
578 		}
579 		if (pa->sa_mask != 0) {
580 			/*
581 			 * Commit any pending attributes from
582 			 * the ofile we're closing.  Note that
583 			 * we pass NULL as the ofile to setattr
584 			 * so it will write to the file system
585 			 * and not keep anything on the ofile.
586 			 */
587 			(void) smb_node_setattr(NULL, of->f_node,
588 			    of->f_cr, NULL, pa);
589 		}
590 		/*
591 		 * Don't want the performance cost of generating
592 		 * change notify events on every write.  Instead:
593 		 * Keep track of the fact that we have written
594 		 * data via this handle, and do change notify
595 		 * work on the first write, and during close.
596 		 */
597 		if (of->f_written) {
598 			smb_node_notify_modified(of->f_node);
599 		}
600 
601 		smb_server_dec_files(of->f_server);
602 		break;
603 	}
604 
605 	/*
606 	 * Keep f_state == SMB_OFILE_STATE_CLOSING
607 	 * until the last ref. is dropped, in
608 	 * smb_ofile_release()
609 	 */
610 }
611 
612 uint32_t smb2_savedh_timeout = 60;	/* sec. */
613 
614 /*
615  * "Destructor" function for smb_ofile_close_all, and
616  * smb_ofile_close_all_by_pid, called after the llist lock
617  * for tree list has been exited.  Our job is to either
618  * close this ofile, or (if durable) set state _SAVE_DH.
619  *
620  * The next interesting thing happens when the last ref.
621  * on this ofile calls smb_ofile_release(), where we
622  * eihter delete the ofile, or (if durable) leave it
623  * in the persistid hash table for possible reclaim.
624  * (state becomes "orphaned").
625  *
626  * In case references dont't ever leave (eg. ref leak bug)
627  * arrange for smb2_durable_timers() to move this ofile from
628  * state SAVE_DH to state CLOSING.  The dh_expire_time set
629  * on SAVE_DH ofiles here is temporary, and replaced with
630  * the negotiated expiration time when it becomes ORPHANED.
631  *
632  * This is run via smb_llist_post (after smb_llist_exit)
633  * because smb_ofile_close can block, and we'd rather not
634  * block while holding the ofile list as reader.
635  */
636 static void
smb_ofile_drop(void * arg)637 smb_ofile_drop(void *arg)
638 {
639 	smb_ofile_t	*of = arg;
640 
641 	SMB_OFILE_VALID(of);
642 
643 	mutex_enter(&of->f_mutex);
644 	switch (of->f_state) {
645 	case SMB_OFILE_STATE_OPEN:
646 		/* DH checks under mutex. */
647 		if (of->f_ftype == SMB_FTYPE_DISK &&
648 		    of->dh_vers != SMB2_NOT_DURABLE &&
649 		    smb_dh_should_save(of)) {
650 			/*
651 			 * Tell smb_ofile_release() to
652 			 * make this an _ORPHANED DH.
653 			 */
654 			of->f_state = SMB_OFILE_STATE_SAVE_DH;
655 			of->dh_expire_time = gethrtime() +
656 			    SEC2NSEC(smb2_savedh_timeout);
657 			mutex_exit(&of->f_mutex);
658 			break;
659 		}
660 		/* OK close it. */
661 		mutex_exit(&of->f_mutex);
662 		smb_ofile_close(of, 0);
663 		break;
664 
665 	default:
666 		/* Something else closed it already. */
667 		mutex_exit(&of->f_mutex);
668 		break;
669 	}
670 
671 	/*
672 	 * Release the ref acquired during the traversal loop.
673 	 * Note that on the last ref, this ofile will be
674 	 * removed from the tree list etc.
675 	 * See: smb_llist_post, smb_ofile_delete
676 	 */
677 	smb_ofile_release(of);
678 }
679 
680 /*
681  * smb_ofile_close_all
682  *
683  *
684  */
685 void
smb_ofile_close_all(smb_tree_t * tree,uint32_t pid)686 smb_ofile_close_all(
687     smb_tree_t		*tree,
688     uint32_t		pid)
689 {
690 	smb_ofile_t	*of;
691 	smb_lavl_t	*la;
692 
693 	ASSERT(tree);
694 	ASSERT(tree->t_magic == SMB_TREE_MAGIC);
695 
696 	la = &tree->t_ofile_list;
697 
698 	smb_lavl_enter(la, RW_READER);
699 	for (of = smb_lavl_first(la);
700 	    of != NULL;
701 	    of = smb_lavl_next(la, of)) {
702 		ASSERT(of->f_magic == SMB_OFILE_MAGIC);
703 		ASSERT(of->f_tree == tree);
704 		if (pid != 0 && of->f_opened_by_pid != pid)
705 			continue;
706 		if (smb_ofile_hold(of)) {
707 			smb_lavl_post(la, of, smb_ofile_drop);
708 		}
709 	}
710 
711 	/*
712 	 * Drop the lock and process the llist dtor queue.
713 	 * Calls smb_ofile_drop on ofiles that were open.
714 	 */
715 	smb_lavl_exit(la);
716 }
717 
718 /*
719  * If the enumeration request is for ofile data, handle it here.
720  * Otherwise, return.
721  *
722  * This function should be called with a hold on the ofile.
723  */
724 int
smb_ofile_enum(smb_ofile_t * of,smb_svcenum_t * svcenum)725 smb_ofile_enum(smb_ofile_t *of, smb_svcenum_t *svcenum)
726 {
727 	uint8_t *pb;
728 	uint_t nbytes;
729 	int rc;
730 
731 	ASSERT(of);
732 	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
733 	ASSERT(of->f_refcnt);
734 
735 	if (svcenum->se_type != SMB_SVCENUM_TYPE_FILE)
736 		return (0);
737 
738 	if (svcenum->se_nskip > 0) {
739 		svcenum->se_nskip--;
740 		return (0);
741 	}
742 
743 	if (svcenum->se_nitems >= svcenum->se_nlimit) {
744 		svcenum->se_nitems = svcenum->se_nlimit;
745 		return (0);
746 	}
747 
748 	pb = &svcenum->se_buf[svcenum->se_bused];
749 
750 	rc = smb_ofile_netinfo_encode(of, pb, svcenum->se_bavail,
751 	    &nbytes);
752 	if (rc == 0) {
753 		svcenum->se_bavail -= nbytes;
754 		svcenum->se_bused += nbytes;
755 		svcenum->se_nitems++;
756 	}
757 
758 	return (rc);
759 }
760 
761 /*
762  * Take a reference on an open file, in any of the states:
763  *   RECONNECT, SAVE_DH, OPEN, ORPHANED, EXPIRED.
764  * Return TRUE if ref taken.  Used for oplock breaks.
765  *
766  * Note: When the oplock break code calls this, it holds the
767  * node ofile list lock and node oplock mutex.  When we see
768  * an ofile in states RECONNECT or SAVING, we know the ofile
769  * is gaining or losing it's tree, and that happens quickly,
770  * so we just wait for that work to finish.  However, the
771  * waiting for state transitions here means we have to be
772  * careful not to re-enter the node list lock or otherwise
773  * block on things that could cause a deadlock.  Waiting
774  * just on of->f_mutex here is OK.
775  */
776 boolean_t
smb_ofile_hold_olbrk(smb_ofile_t * of)777 smb_ofile_hold_olbrk(smb_ofile_t *of)
778 {
779 	boolean_t ret = B_FALSE;
780 
781 	ASSERT(of);
782 	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
783 
784 	mutex_enter(&of->f_mutex);
785 
786 again:
787 	switch (of->f_state) {
788 	case SMB_OFILE_STATE_RECONNECT:
789 	case SMB_OFILE_STATE_SAVING:
790 		cv_wait(&of->f_cv, &of->f_mutex);
791 		goto again;
792 
793 	case SMB_OFILE_STATE_OPEN:
794 	case SMB_OFILE_STATE_SAVE_DH:
795 	case SMB_OFILE_STATE_ORPHANED:
796 	case SMB_OFILE_STATE_EXPIRED:
797 		of->f_refcnt++;
798 		ret = B_TRUE;
799 		break;
800 
801 	/*
802 	 * This is called only when an ofile has an oplock,
803 	 * so if we come across states that should not have
804 	 * an oplock, let's debug how that happened.
805 	 */
806 	default:
807 		ASSERT(0);
808 		break;
809 	}
810 	mutex_exit(&of->f_mutex);
811 
812 	return (ret);
813 }
814 
815 /*
816  * Take a reference on an open file.
817  */
818 boolean_t
smb_ofile_hold(smb_ofile_t * of)819 smb_ofile_hold(smb_ofile_t *of)
820 {
821 	ASSERT(of);
822 	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
823 
824 	mutex_enter(&of->f_mutex);
825 
826 	if (of->f_state != SMB_OFILE_STATE_OPEN) {
827 		mutex_exit(&of->f_mutex);
828 		return (B_FALSE);
829 	}
830 	of->f_refcnt++;
831 
832 	mutex_exit(&of->f_mutex);
833 	return (B_TRUE);
834 }
835 
836 /*
837  * Void arg variant of smb_ofile_release for use with smb_llist_post.
838  * This is needed because smb_ofile_release may need to enter the
839  * smb_llist as writer when it drops the last reference, so when
840  * we're in the llist as reader, use smb_llist_post with this
841  * function to arrange for the release call at llist_exit.
842  */
843 void
smb_ofile_release_LL(void * arg)844 smb_ofile_release_LL(void *arg)
845 {
846 	smb_ofile_t	*of = arg;
847 
848 	SMB_OFILE_VALID(of);
849 	smb_ofile_release(of);
850 }
851 
852 /*
853  * Release a reference on a file.  If the reference count falls to
854  * zero and the file has been closed, post the object for deletion.
855  * Object deletion is deferred to avoid modifying a list while an
856  * iteration may be in progress.
857  *
858  * We're careful to avoid dropping f_session etc. until the last
859  * reference goes away.  The oplock break code depends on that
860  * not changing while it holds a ref. on an ofile.
861  */
862 void
smb_ofile_release(smb_ofile_t * of)863 smb_ofile_release(smb_ofile_t *of)
864 {
865 	smb_tree_t *tree = of->f_tree;
866 	boolean_t delete = B_FALSE;
867 
868 	SMB_OFILE_VALID(of);
869 
870 	mutex_enter(&of->f_mutex);
871 	ASSERT(of->f_refcnt > 0);
872 	of->f_refcnt--;
873 
874 	switch (of->f_state) {
875 	case SMB_OFILE_STATE_OPEN:
876 	case SMB_OFILE_STATE_ORPHANED:
877 	case SMB_OFILE_STATE_EXPIRED:
878 		break;
879 
880 	case SMB_OFILE_STATE_SAVE_DH:
881 		ASSERT(tree != NULL);
882 		if (of->f_refcnt == 0) {
883 			of->f_state = SMB_OFILE_STATE_SAVING;
884 			smb_lavl_post(&tree->t_ofile_list, of,
885 			    smb_ofile_save_dh);
886 		}
887 		break;
888 
889 	case SMB_OFILE_STATE_CLOSING:
890 		/* Note, tree == NULL on _ORPHANED */
891 		if (of->f_refcnt == 0) {
892 			of->f_state = SMB_OFILE_STATE_CLOSED;
893 			if (tree == NULL) {
894 				/* Skip smb_llist_post */
895 				delete = B_TRUE;
896 				break;
897 			}
898 			smb_lavl_post(&tree->t_ofile_list, of,
899 			    smb_ofile_delete);
900 		}
901 		break;
902 
903 	default:
904 		ASSERT(0);
905 		break;
906 	}
907 	mutex_exit(&of->f_mutex);
908 
909 	/*
910 	 * When we drop the last ref. on an expired DH, it's no longer
911 	 * in any tree, so skip the smb_llist_post and just call
912 	 * smb_ofile_delete directly.
913 	 */
914 	if (delete) {
915 		smb_ofile_delete(of);
916 	}
917 }
918 
919 /*
920  * smb_ofile_lookup_by_fid
921  *
922  * Find the open file whose fid matches the one specified in the request.
923  * If we can't find the fid or the shares (trees) don't match, we have a
924  * bad fid.
925  */
926 smb_ofile_t *
smb_ofile_lookup_by_fid(smb_request_t * sr,uint16_t fid)927 smb_ofile_lookup_by_fid(
928     smb_request_t	*sr,
929     uint16_t		fid)
930 {
931 	smb_tree_t	*tree = sr->tid_tree;
932 	smb_lavl_t	*lavl;
933 	smb_ofile_t	*of;
934 	uint16_t	cmp_fid;
935 
936 	CTASSERT(offsetof(smb_ofile_t, f_fid) == 0);
937 	ASSERT(tree->t_magic == SMB_TREE_MAGIC);
938 
939 	cmp_fid = fid;
940 
941 	lavl = &tree->t_ofile_list;
942 
943 	smb_lavl_enter(lavl, RW_READER);
944 	of = avl_find(&lavl->la_tree, &cmp_fid, NULL);
945 	if (of == NULL)
946 		goto out;
947 
948 	/*
949 	 * Only allow use of a given FID with the same UID that
950 	 * was used to open it.  MS-CIFS 3.3.5.14
951 	 */
952 	if (of->f_user != sr->uid_user) {
953 		of = NULL;
954 		goto out;
955 	}
956 
957 	/* inline smb_ofile_hold() */
958 	mutex_enter(&of->f_mutex);
959 	if (of->f_state != SMB_OFILE_STATE_OPEN) {
960 		mutex_exit(&of->f_mutex);
961 		of = NULL;
962 		goto out;
963 	}
964 	of->f_refcnt++;
965 	mutex_exit(&of->f_mutex);
966 
967 out:
968 	smb_lavl_exit(lavl);
969 	return (of);
970 }
971 
972 /*
973  * smb_ofile_lookup_by_uniqid
974  *
975  * Find the open file whose uniqid matches the one specified in the request.
976  */
977 smb_ofile_t *
smb_ofile_lookup_by_uniqid(smb_tree_t * tree,uint32_t uniqid)978 smb_ofile_lookup_by_uniqid(smb_tree_t *tree, uint32_t uniqid)
979 {
980 	smb_lavl_t	*lavl;
981 	smb_ofile_t	*of;
982 
983 	ASSERT(tree->t_magic == SMB_TREE_MAGIC);
984 
985 	lavl = &tree->t_ofile_list;
986 	smb_lavl_enter(lavl, RW_READER);
987 	of = smb_lavl_first(lavl);
988 
989 	while (of) {
990 		ASSERT(of->f_magic == SMB_OFILE_MAGIC);
991 		ASSERT(of->f_tree == tree);
992 
993 		if (of->f_uniqid == uniqid) {
994 			if (smb_ofile_hold(of)) {
995 				smb_lavl_exit(lavl);
996 				return (of);
997 			}
998 		}
999 
1000 		of = smb_lavl_next(lavl, of);
1001 	}
1002 
1003 	smb_lavl_exit(lavl);
1004 	return (NULL);
1005 }
1006 
1007 /*
1008  * Durable ID (or persistent ID)
1009  */
1010 
1011 static smb_ofile_t *
smb_ofile_hold_cb(smb_ofile_t * of)1012 smb_ofile_hold_cb(smb_ofile_t *of)
1013 {
1014 	smb_ofile_t *ret = of;
1015 
1016 	mutex_enter(&of->f_mutex);
1017 	if (of->f_state == SMB_OFILE_STATE_ORPHANED)
1018 		/* inline smb_ofile_hold() */
1019 		of->f_refcnt++;
1020 	else
1021 		ret = NULL;
1022 
1023 	mutex_exit(&of->f_mutex);
1024 	return (ret);
1025 }
1026 
1027 /*
1028  * Lookup an ofile by persistent ID, and return ONLY if in state ORPHANED
1029  * This is used by SMB2 create "reclaim".
1030  */
1031 smb_ofile_t *
smb_ofile_lookup_by_persistid(smb_request_t * sr,uint64_t persistid)1032 smb_ofile_lookup_by_persistid(smb_request_t *sr, uint64_t persistid)
1033 {
1034 	smb_hash_t *hash;
1035 	smb_bucket_t *bucket;
1036 	smb_llist_t *ll;
1037 	smb_ofile_t *of;
1038 	uint_t idx;
1039 
1040 	if (persistid == 0)
1041 		return (NULL);
1042 
1043 	hash = sr->sr_server->sv_persistid_ht;
1044 	idx = smb_hash_uint64(hash, persistid);
1045 	bucket = &hash->buckets[idx];
1046 	ll = &bucket->b_list;
1047 
1048 	smb_llist_enter(ll, RW_READER);
1049 	of = smb_llist_head(ll);
1050 	while (of != NULL) {
1051 		if (of->f_persistid == persistid)
1052 			break;
1053 		of = smb_llist_next(ll, of);
1054 	}
1055 	if (of != NULL)
1056 		of = smb_ofile_hold_cb(of);
1057 	smb_llist_exit(ll);
1058 
1059 	return (of);
1060 }
1061 
1062 /*
1063  * Create a (unique) durable/persistent ID for a new ofile,
1064  * and add this ofile to the persistid hash table.  This ID
1065  * is referred to as the persistent ID in the protocol spec,
1066  * so that's what we call it too, though the persistence may
1067  * vary.  "Durable" handles are persistent across reconnects
1068  * but not server reboots.  Persistent handles are persistent
1069  * across server reboots too.
1070  *
1071  * Note that persistent IDs need to be unique for the lifetime of
1072  * any given ofile.  For normal (non-persistent) ofiles we can just
1073  * use a persistent ID derived from the ofile memory address, as
1074  * these don't ever live beyond the current OS boot lifetime.
1075  *
1076  * Persistent handles are re-imported after server restart, and
1077  * generally have a different memory address after import than
1078  * they had in the previous OS boot lifetime, so for these we
1079  * use a randomly assigned value that won't conflict with any
1080  * non-persistent (durable) handles.  Ensuring that a randomly
1081  * generated ID is unique requires a search of the ofiles in one
1082  * hash bucket, which we'd rather avoid for non-persistent opens.
1083  *
1084  * The solution used here is to divide the persistent ID space
1085  * in half (odd and even values) where durable opens use an ID
1086  * derived from the ofile address (which is always even), and
1087  * persistent opens use an ID generated randomly (always odd).
1088  *
1089  * smb_ofile_set_persistid_dh() sets a durable handle ID and
1090  * smb_ofile_set_persistid_ph() sets a persistent handle ID.
1091  */
1092 void
smb_ofile_set_persistid_dh(smb_ofile_t * of)1093 smb_ofile_set_persistid_dh(smb_ofile_t *of)
1094 {
1095 	smb_hash_t *hash = of->f_server->sv_persistid_ht;
1096 	smb_bucket_t *bucket;
1097 	smb_llist_t *ll;
1098 	uint64_t persistid;
1099 	uint_t idx;
1100 
1101 	persistid = (uintptr_t)of;
1102 	/* Avoid showing object addresses */
1103 	persistid ^= ((uintptr_t)&smb_cache_ofile);
1104 	/* make sure it's even */
1105 	persistid &= ~((uint64_t)1);
1106 
1107 	idx = smb_hash_uint64(hash, persistid);
1108 	bucket = &hash->buckets[idx];
1109 	ll = &bucket->b_list;
1110 	smb_llist_enter(ll, RW_WRITER);
1111 	if (of->f_persistid == 0) {
1112 		of->f_persistid = persistid;
1113 		smb_llist_insert_tail(ll, of);
1114 	}
1115 	smb_llist_exit(ll);
1116 }
1117 
1118 void
smb_ofile_set_persistid_ph(smb_ofile_t * of)1119 smb_ofile_set_persistid_ph(smb_ofile_t *of)
1120 {
1121 	uint64_t persistid;
1122 	int rc;
1123 
1124 top:
1125 	(void) random_get_pseudo_bytes((uint8_t *)&persistid,
1126 	    sizeof (persistid));
1127 	if (persistid == 0) {
1128 		cmn_err(CE_NOTE, "random gave all zeros!");
1129 		goto top;
1130 	}
1131 	/* make sure it's odd */
1132 	persistid |= (uint64_t)1;
1133 
1134 	/*
1135 	 * Try inserting with this persistent ID.
1136 	 */
1137 	rc = smb_ofile_insert_persistid(of, persistid);
1138 	if (rc == EEXIST)
1139 		goto top;
1140 	if (rc != 0) {
1141 		cmn_err(CE_NOTE, "set persistid rc=%d", rc);
1142 	}
1143 }
1144 
1145 /*
1146  * Insert an ofile into the persistid hash table.
1147  * If the persistent ID is in use, error.
1148  */
1149 int
smb_ofile_insert_persistid(smb_ofile_t * new_of,uint64_t persistid)1150 smb_ofile_insert_persistid(smb_ofile_t *new_of, uint64_t persistid)
1151 {
1152 	smb_hash_t *hash = new_of->f_server->sv_persistid_ht;
1153 	smb_bucket_t *bucket;
1154 	smb_llist_t *ll;
1155 	smb_ofile_t *of;
1156 	uint_t idx;
1157 
1158 	ASSERT(persistid != 0);
1159 
1160 	/*
1161 	 * Look to see if this key alreay exists.
1162 	 */
1163 	idx = smb_hash_uint64(hash, persistid);
1164 	bucket = &hash->buckets[idx];
1165 	ll = &bucket->b_list;
1166 
1167 	smb_llist_enter(ll, RW_WRITER);
1168 	of = smb_llist_head(ll);
1169 	while (of != NULL) {
1170 		if (of->f_persistid == persistid) {
1171 			/* already in use */
1172 			smb_llist_exit(ll);
1173 			return (EEXIST);
1174 		}
1175 		of = smb_llist_next(ll, of);
1176 	}
1177 
1178 	/* Not found, so OK to insert. */
1179 	if (new_of->f_persistid == 0) {
1180 		new_of->f_persistid = persistid;
1181 		smb_llist_insert_tail(ll, new_of);
1182 	}
1183 	smb_llist_exit(ll);
1184 
1185 	return (0);
1186 }
1187 
1188 void
smb_ofile_del_persistid(smb_ofile_t * of)1189 smb_ofile_del_persistid(smb_ofile_t *of)
1190 {
1191 	smb_hash_t *hash = of->f_server->sv_persistid_ht;
1192 	smb_bucket_t *bucket;
1193 	smb_llist_t *ll;
1194 	uint_t idx;
1195 
1196 	idx = smb_hash_uint64(hash, of->f_persistid);
1197 	bucket = &hash->buckets[idx];
1198 	ll = &bucket->b_list;
1199 	smb_llist_enter(ll, RW_WRITER);
1200 	if (of->f_persistid != 0) {
1201 		smb_llist_remove(ll, of);
1202 		of->f_persistid = 0;
1203 	}
1204 	smb_llist_exit(ll);
1205 }
1206 
1207 /*
1208  * Disallow NetFileClose on certain ofiles to avoid side-effects.
1209  * Closing a tree root is not allowed: use NetSessionDel or NetShareDel.
1210  * Closing SRVSVC connections is not allowed because this NetFileClose
1211  * request may depend on this ofile.
1212  */
1213 boolean_t
smb_ofile_disallow_fclose(smb_ofile_t * of)1214 smb_ofile_disallow_fclose(smb_ofile_t *of)
1215 {
1216 	ASSERT(of);
1217 	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
1218 	ASSERT(of->f_refcnt);
1219 
1220 	switch (of->f_ftype) {
1221 	case SMB_FTYPE_DISK:
1222 		ASSERT(of->f_tree);
1223 		return (of->f_node == of->f_tree->t_snode);
1224 
1225 	case SMB_FTYPE_MESG_PIPE:
1226 		ASSERT(of->f_pipe);
1227 		if (smb_strcasecmp(of->f_pipe->p_name, "SRVSVC", 0) == 0)
1228 			return (B_TRUE);
1229 		break;
1230 	default:
1231 		break;
1232 	}
1233 
1234 	return (B_FALSE);
1235 }
1236 
1237 /*
1238  * smb_ofile_set_flags
1239  *
1240  * Return value:
1241  *
1242  *	Current flags value
1243  *
1244  */
1245 void
smb_ofile_set_flags(smb_ofile_t * of,uint32_t flags)1246 smb_ofile_set_flags(
1247     smb_ofile_t		*of,
1248     uint32_t		flags)
1249 {
1250 	ASSERT(of);
1251 	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
1252 	ASSERT(of->f_refcnt);
1253 
1254 	mutex_enter(&of->f_mutex);
1255 	of->f_flags |= flags;
1256 	mutex_exit(&of->f_mutex);
1257 }
1258 
1259 /*
1260  * smb_ofile_seek
1261  *
1262  * Return value:
1263  *
1264  *	0		Success
1265  *	EINVAL		Unknown mode
1266  *	EOVERFLOW	offset too big
1267  *
1268  */
1269 int
smb_ofile_seek(smb_ofile_t * of,ushort_t mode,int32_t off,uint32_t * retoff)1270 smb_ofile_seek(
1271     smb_ofile_t		*of,
1272     ushort_t		mode,
1273     int32_t		off,
1274     uint32_t		*retoff)
1275 {
1276 	u_offset_t	newoff = 0;
1277 	int		rc = 0;
1278 	smb_attr_t	attr;
1279 
1280 	ASSERT(of);
1281 	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
1282 	ASSERT(of->f_refcnt);
1283 
1284 	mutex_enter(&of->f_mutex);
1285 	switch (mode) {
1286 	case SMB_SEEK_SET:
1287 		if (off < 0)
1288 			newoff = 0;
1289 		else
1290 			newoff = (u_offset_t)off;
1291 		break;
1292 
1293 	case SMB_SEEK_CUR:
1294 		if (off < 0 && (-off) > of->f_seek_pos)
1295 			newoff = 0;
1296 		else
1297 			newoff = of->f_seek_pos + (u_offset_t)off;
1298 		break;
1299 
1300 	case SMB_SEEK_END:
1301 		bzero(&attr, sizeof (smb_attr_t));
1302 		attr.sa_mask |= SMB_AT_SIZE;
1303 		rc = smb_fsop_getattr(NULL, zone_kcred(), of->f_node, &attr);
1304 		if (rc != 0) {
1305 			mutex_exit(&of->f_mutex);
1306 			return (rc);
1307 		}
1308 		if (off < 0 && (-off) > attr.sa_vattr.va_size)
1309 			newoff = 0;
1310 		else
1311 			newoff = attr.sa_vattr.va_size + (u_offset_t)off;
1312 		break;
1313 
1314 	default:
1315 		mutex_exit(&of->f_mutex);
1316 		return (EINVAL);
1317 	}
1318 
1319 	/*
1320 	 * See comments at the beginning of smb_seek.c.
1321 	 * If the offset is greater than UINT_MAX, we will return an error.
1322 	 */
1323 
1324 	if (newoff > UINT_MAX) {
1325 		rc = EOVERFLOW;
1326 	} else {
1327 		of->f_seek_pos = newoff;
1328 		*retoff = (uint32_t)newoff;
1329 	}
1330 	mutex_exit(&of->f_mutex);
1331 	return (rc);
1332 }
1333 
1334 /*
1335  * smb_ofile_flush
1336  *
1337  * If writes on this file are not synchronous, flush it using the NFSv3
1338  * commit interface.
1339  *
1340  * XXX - todo: Flush named pipe should drain writes.
1341  */
1342 void
smb_ofile_flush(struct smb_request * sr,struct smb_ofile * of)1343 smb_ofile_flush(struct smb_request *sr, struct smb_ofile *of)
1344 {
1345 	switch (of->f_ftype) {
1346 	case SMB_FTYPE_DISK:
1347 		if ((of->f_node->flags & NODE_FLAGS_WRITE_THROUGH) == 0)
1348 			(void) smb_fsop_commit(sr, of->f_cr, of->f_node);
1349 		break;
1350 	default:
1351 		break;
1352 	}
1353 }
1354 
1355 /*
1356  * smb_ofile_is_open
1357  */
1358 boolean_t
smb_ofile_is_open(smb_ofile_t * of)1359 smb_ofile_is_open(smb_ofile_t *of)
1360 {
1361 	boolean_t	rc;
1362 
1363 	SMB_OFILE_VALID(of);
1364 
1365 	mutex_enter(&of->f_mutex);
1366 	rc = smb_ofile_is_open_locked(of);
1367 	mutex_exit(&of->f_mutex);
1368 	return (rc);
1369 }
1370 
1371 /* *************************** Static Functions ***************************** */
1372 
1373 /*
1374  * Determine whether or not an ofile is open.
1375  * This function must be called with the mutex held.
1376  */
1377 static boolean_t
smb_ofile_is_open_locked(smb_ofile_t * of)1378 smb_ofile_is_open_locked(smb_ofile_t *of)
1379 {
1380 	ASSERT(MUTEX_HELD(&of->f_mutex));
1381 
1382 	switch (of->f_state) {
1383 	case SMB_OFILE_STATE_OPEN:
1384 	case SMB_OFILE_STATE_SAVE_DH:
1385 	case SMB_OFILE_STATE_SAVING:
1386 	case SMB_OFILE_STATE_ORPHANED:
1387 	case SMB_OFILE_STATE_RECONNECT:
1388 		return (B_TRUE);
1389 
1390 	case SMB_OFILE_STATE_CLOSING:
1391 	case SMB_OFILE_STATE_CLOSED:
1392 	case SMB_OFILE_STATE_EXPIRED:
1393 		return (B_FALSE);
1394 
1395 	default:
1396 		ASSERT(0);
1397 		return (B_FALSE);
1398 	}
1399 }
1400 
1401 /*
1402  * smb_ofile_save_dh
1403  *
1404  * Called via smb_llist_post (after smb_llist_exit) when the last ref.
1405  * on this ofile has gone, and this ofile is a "durable handle" (DH)
1406  * that has state we've decided to save.
1407  *
1408  * This does parts of what smb_ofile_delete would do, including:
1409  * remove the ofile from the tree ofile list and related.
1410  *
1411  * We leave the ofile in state ORPHANED, ready for reconnect
1412  * or expiration via smb2_dh_expire (see smb_ofile_delete).
1413  */
1414 static void
smb_ofile_save_dh(void * arg)1415 smb_ofile_save_dh(void *arg)
1416 {
1417 	smb_ofile_t	*of = (smb_ofile_t *)arg;
1418 	smb_tree_t	*tree = of->f_tree;
1419 
1420 	SMB_OFILE_VALID(of);
1421 	ASSERT(of->f_refcnt == 0);
1422 	ASSERT(of->f_ftype == SMB_FTYPE_DISK);
1423 	ASSERT(of->f_state == SMB_OFILE_STATE_SAVING);
1424 
1425 	atomic_dec_32(&of->f_session->s_file_cnt);
1426 	atomic_dec_32(&of->f_tree->t_open_files);
1427 	smb_lavl_enter(&tree->t_ofile_list, RW_WRITER);
1428 	smb_lavl_remove(&tree->t_ofile_list, of);
1429 	smb_lavl_exit(&tree->t_ofile_list);
1430 
1431 	/*
1432 	 * This ofile is no longer on t_ofile_list, however...
1433 	 *
1434 	 * This is called via smb_llist_post, which means it may run
1435 	 * BEFORE smb_ofile_release drops f_mutex (if another thread
1436 	 * flushes the delete queue before we do).  Synchronize.
1437 	 */
1438 	mutex_enter(&of->f_mutex);
1439 	DTRACE_PROBE1(ofile__exit, smb_ofile_t *, of);
1440 	mutex_exit(&of->f_mutex);
1441 
1442 	/*
1443 	 * Keep f_notify state, lease, and
1444 	 * keep on node ofile list.
1445 	 * Keep of->f_cr until reclaim.
1446 	 */
1447 
1448 	ASSERT(of->f_fid != 0);
1449 	smb_idpool_free(&tree->t_fid_pool, of->f_fid);
1450 	of->f_fid = 0;
1451 	smb_tree_release(of->f_tree);
1452 	of->f_tree = NULL;
1453 	smb_user_release(of->f_user);
1454 	of->f_user = NULL;
1455 	of->f_session = NULL;
1456 
1457 	/*
1458 	 * Make it "orphaned" so it can now be reclaimed.
1459 	 * Note that smb_ofile_hold_olbrk() may have blocked
1460 	 * for state SMB_OFILE_STATE_SAVING, so wake it.
1461 	 */
1462 	mutex_enter(&of->f_mutex);
1463 	of->dh_expire_time = gethrtime() + of->dh_timeout_offset;
1464 	of->f_state = SMB_OFILE_STATE_ORPHANED;
1465 	cv_broadcast(&of->f_cv);
1466 	mutex_exit(&of->f_mutex);
1467 }
1468 
1469 /*
1470  * Delete an ofile.
1471  *
1472  * Approximately the inverse of smb_ofile_alloc()
1473  * Called via smb_llist_post (after smb_llist_exit)
1474  * when the last ref. on this ofile has gone.
1475  *
1476  * Normally,this removes the ofile from the tree list and
1477  * then frees resources held on the ofile.  However, when
1478  * we're expiring an orphaned durable handle, the linkage
1479  * into the tree lists etc. have already been destroyed.
1480  * This case is distinguished by of->f_tree == NULL.
1481  */
1482 static void
smb_ofile_delete(void * arg)1483 smb_ofile_delete(void *arg)
1484 {
1485 	smb_ofile_t	*of = (smb_ofile_t *)arg;
1486 	smb_tree_t	*tree = of->f_tree;
1487 
1488 	SMB_OFILE_VALID(of);
1489 	ASSERT(of->f_refcnt == 0);
1490 	ASSERT(of->f_state == SMB_OFILE_STATE_CLOSED);
1491 
1492 	if (tree != NULL) {
1493 		ASSERT(of->f_user != NULL);
1494 		ASSERT(of->f_session != NULL);
1495 		atomic_dec_32(&of->f_session->s_file_cnt);
1496 		atomic_dec_32(&of->f_tree->t_open_files);
1497 		smb_lavl_enter(&tree->t_ofile_list, RW_WRITER);
1498 		smb_lavl_remove(&tree->t_ofile_list, of);
1499 		smb_lavl_exit(&tree->t_ofile_list);
1500 	}
1501 
1502 	/*
1503 	 * Remove this ofile from the node's n_ofile_list so it
1504 	 * can't be found by list walkers like notify or oplock.
1505 	 * Keep the node ref. until later in this function so
1506 	 * of->f_node remains valid while we destroy the ofile.
1507 	 */
1508 	if (of->f_ftype == SMB_FTYPE_DISK ||
1509 	    of->f_ftype == SMB_FTYPE_PRINTER) {
1510 		smb_node_t *node = of->f_node;
1511 
1512 		/*
1513 		 * Oplock cleanup should have made sure that
1514 		 * excl_open does not point to this ofile.
1515 		 */
1516 		VERIFY(node->n_oplock.excl_open != of);
1517 
1518 		/*
1519 		 * Note smb_ofile_close did smb_node_dec_open_ofiles()
1520 		 */
1521 		smb_node_rem_ofile(node, of);
1522 	}
1523 
1524 	/*
1525 	 * This ofile is no longer on any lists, however...
1526 	 *
1527 	 * This is called via smb_llist_post, which means it may run
1528 	 * BEFORE smb_ofile_release drops f_mutex (if another thread
1529 	 * flushes the delete queue before we do).  Synchronize.
1530 	 */
1531 	mutex_enter(&of->f_mutex);
1532 	of->f_state = SMB_OFILE_STATE_ALLOC;
1533 	DTRACE_PROBE1(ofile__exit, smb_ofile_t *, of);
1534 	mutex_exit(&of->f_mutex);
1535 
1536 	switch (of->f_ftype) {
1537 	case SMB_FTYPE_BYTE_PIPE:
1538 	case SMB_FTYPE_MESG_PIPE:
1539 		smb_opipe_dealloc(of->f_pipe);
1540 		of->f_pipe = NULL;
1541 		break;
1542 	case SMB_FTYPE_DISK:
1543 		ASSERT(of->f_notify.nc_subscribed == B_FALSE);
1544 		MBC_FLUSH(&of->f_notify.nc_buffer);
1545 		if (of->f_odir != NULL)
1546 			smb_odir_release(of->f_odir);
1547 		if (of->f_lease != NULL) {
1548 			smb2_lease_rele(of->f_lease);
1549 			of->f_lease = NULL;
1550 		}
1551 		/* FALLTHROUGH */
1552 	case SMB_FTYPE_PRINTER:
1553 		/*
1554 		 * Did smb_node_rem_ofile above.
1555 		 */
1556 		ASSERT(of->f_node != NULL);
1557 		smb_node_release(of->f_node);
1558 		break;
1559 	default:
1560 		ASSERT(!"f_ftype");
1561 		break;
1562 	}
1563 
1564 	smb_ofile_free(of);
1565 }
1566 
1567 void
smb_ofile_free(smb_ofile_t * of)1568 smb_ofile_free(smb_ofile_t *of)
1569 {
1570 	smb_tree_t	*tree = of->f_tree;
1571 
1572 	ASSERT(of->f_state == SMB_OFILE_STATE_ALLOC);
1573 
1574 	/* Make sure it's not in the persistid hash. */
1575 	ASSERT(of->f_persistid == 0);
1576 
1577 	if (tree != NULL) {
1578 		if (of->f_fid != 0)
1579 			smb_idpool_free(&tree->t_fid_pool, of->f_fid);
1580 		smb_tree_release(of->f_tree);
1581 		smb_user_release(of->f_user);
1582 	}
1583 
1584 	if (of->f_cr != NULL)
1585 		crfree(of->f_cr);
1586 
1587 	of->f_magic = (uint32_t)~SMB_OFILE_MAGIC;
1588 	list_destroy(&of->f_notify.nc_waiters);
1589 	mutex_destroy(&of->dh_nvlock);
1590 	mutex_destroy(&of->f_mutex);
1591 	kmem_cache_free(smb_cache_ofile, of);
1592 }
1593 
1594 /*
1595  * smb_ofile_access
1596  *
1597  * This function will check to see if the access requested is granted.
1598  * Returns NT status codes.
1599  */
1600 uint32_t
smb_ofile_access(smb_ofile_t * of,cred_t * cr,uint32_t access)1601 smb_ofile_access(smb_ofile_t *of, cred_t *cr, uint32_t access)
1602 {
1603 
1604 	if ((of == NULL) || (cr == zone_kcred()))
1605 		return (NT_STATUS_SUCCESS);
1606 
1607 	/*
1608 	 * If the request is for something
1609 	 * I don't grant it is an error
1610 	 */
1611 	if (~(of->f_granted_access) & access) {
1612 		if (!(of->f_granted_access & ACCESS_SYSTEM_SECURITY) &&
1613 		    (access & ACCESS_SYSTEM_SECURITY)) {
1614 			return (NT_STATUS_PRIVILEGE_NOT_HELD);
1615 		}
1616 		return (NT_STATUS_ACCESS_DENIED);
1617 	}
1618 
1619 	return (NT_STATUS_SUCCESS);
1620 }
1621 
1622 /*
1623  * smb_ofile_share_check
1624  *
1625  * Check if ofile was opened with share access NONE (0).
1626  * Returns: B_TRUE  - share access non-zero
1627  *          B_FALSE - share access NONE
1628  */
1629 boolean_t
smb_ofile_share_check(smb_ofile_t * of)1630 smb_ofile_share_check(smb_ofile_t *of)
1631 {
1632 	return (!SMB_DENY_ALL(of->f_share_access));
1633 }
1634 
1635 /*
1636  * check file sharing rules for current open request
1637  * against existing open instances of the same file
1638  *
1639  * Returns NT_STATUS_SHARING_VIOLATION if there is any
1640  * sharing conflict, otherwise returns NT_STATUS_SUCCESS.
1641  */
1642 uint32_t
smb_ofile_open_check(smb_ofile_t * of,uint32_t desired_access,uint32_t share_access)1643 smb_ofile_open_check(smb_ofile_t *of, uint32_t desired_access,
1644     uint32_t share_access)
1645 {
1646 	uint32_t ret;
1647 
1648 	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
1649 
1650 	mutex_enter(&of->f_mutex);
1651 
1652 	if (!smb_ofile_is_open_locked(of)) {
1653 		ret = NT_STATUS_INVALID_HANDLE;
1654 		goto out;
1655 	}
1656 
1657 	/* if it's just meta data */
1658 	if ((of->f_granted_access & FILE_DATA_ALL) == 0) {
1659 		ret = NT_STATUS_SUCCESS;
1660 		goto out;
1661 	}
1662 
1663 	/*
1664 	 * Check requested share access against the
1665 	 * open granted (desired) access
1666 	 */
1667 	if (SMB_DENY_DELETE(share_access) && (of->f_granted_access & DELETE)) {
1668 		ret = NT_STATUS_SHARING_VIOLATION;
1669 		goto out;
1670 	}
1671 
1672 	if (SMB_DENY_READ(share_access) &&
1673 	    (of->f_granted_access & (FILE_READ_DATA | FILE_EXECUTE))) {
1674 		ret = NT_STATUS_SHARING_VIOLATION;
1675 		goto out;
1676 	}
1677 
1678 	if (SMB_DENY_WRITE(share_access) &&
1679 	    (of->f_granted_access & (FILE_WRITE_DATA | FILE_APPEND_DATA))) {
1680 		ret = NT_STATUS_SHARING_VIOLATION;
1681 		goto out;
1682 	}
1683 
1684 	/* check requested desired access against the open share access */
1685 	if (SMB_DENY_DELETE(of->f_share_access) && (desired_access & DELETE)) {
1686 		ret = NT_STATUS_SHARING_VIOLATION;
1687 		goto out;
1688 	}
1689 
1690 	if (SMB_DENY_READ(of->f_share_access) &&
1691 	    (desired_access & (FILE_READ_DATA | FILE_EXECUTE))) {
1692 		ret = NT_STATUS_SHARING_VIOLATION;
1693 		goto out;
1694 	}
1695 
1696 	if (SMB_DENY_WRITE(of->f_share_access) &&
1697 	    (desired_access & (FILE_WRITE_DATA | FILE_APPEND_DATA))) {
1698 		ret = NT_STATUS_SHARING_VIOLATION;
1699 		goto out;
1700 	}
1701 
1702 	ret = NT_STATUS_SUCCESS;
1703 out:
1704 	mutex_exit(&of->f_mutex);
1705 	return (ret);
1706 }
1707 
1708 /*
1709  * smb_ofile_rename_check
1710  *
1711  * This does the work described in MS-FSA 2.1.5.1.2.2 (Algorithm
1712  * to Check Sharing Access to an Existing Stream or Directory),
1713  * where the "open in-progress" has DesiredAccess = DELETE and
1714  * SharingMode = SHARE_READ | SHARE_WRITE | SHARE_DELETE.
1715  */
1716 
1717 uint32_t
smb_ofile_rename_check(smb_ofile_t * of)1718 smb_ofile_rename_check(smb_ofile_t *of)
1719 {
1720 	uint32_t ret;
1721 
1722 	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
1723 
1724 	mutex_enter(&of->f_mutex);
1725 
1726 	if (!smb_ofile_is_open_locked(of)) {
1727 		ret = NT_STATUS_INVALID_HANDLE;
1728 		goto out;
1729 	}
1730 
1731 	if ((of->f_granted_access & FILE_DATA_ALL) == 0) {
1732 		ret = NT_STATUS_SUCCESS;
1733 		goto out;
1734 	}
1735 
1736 	if ((of->f_share_access & FILE_SHARE_DELETE) == 0) {
1737 		ret = NT_STATUS_SHARING_VIOLATION;
1738 		goto out;
1739 	}
1740 
1741 	ret = NT_STATUS_SUCCESS;
1742 out:
1743 	mutex_exit(&of->f_mutex);
1744 	return (ret);
1745 }
1746 
1747 /*
1748  * smb_ofile_delete_check
1749  *
1750  * An open file can be deleted only if opened for
1751  * accessing meta data. Share modes aren't important
1752  * in this case.
1753  *
1754  * NOTE: there is another mechanism for deleting an
1755  * open file that NT clients usually use.
1756  * That's setting "Delete on close" flag for an open
1757  * file.  In this way the file will be deleted after
1758  * last close. This flag can be set by SmbTrans2SetFileInfo
1759  * with FILE_DISPOSITION_INFO information level.
1760  * For setting this flag, the file should be opened by
1761  * DELETE access in the FID that is passed in the Trans2
1762  * request.
1763  */
1764 
1765 uint32_t
smb_ofile_delete_check(smb_ofile_t * of)1766 smb_ofile_delete_check(smb_ofile_t *of)
1767 {
1768 	uint32_t ret;
1769 
1770 	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
1771 
1772 	mutex_enter(&of->f_mutex);
1773 
1774 	if (!smb_ofile_is_open_locked(of)) {
1775 		ret = NT_STATUS_INVALID_HANDLE;
1776 		goto out;
1777 	}
1778 
1779 	if (of->f_granted_access &
1780 	    (FILE_READ_DATA | FILE_WRITE_DATA |
1781 	    FILE_APPEND_DATA | FILE_EXECUTE | DELETE)) {
1782 		ret = NT_STATUS_SHARING_VIOLATION;
1783 		goto out;
1784 	}
1785 
1786 	ret = NT_STATUS_SUCCESS;
1787 out:
1788 	mutex_exit(&of->f_mutex);
1789 	return (ret);
1790 }
1791 
1792 cred_t *
smb_ofile_getcred(smb_ofile_t * of)1793 smb_ofile_getcred(smb_ofile_t *of)
1794 {
1795 	return (of->f_cr);
1796 }
1797 
1798 /*
1799  * smb_ofile_set_delete_on_close
1800  *
1801  * Set the DeleteOnClose flag on the smb file. When the file is closed,
1802  * the flag will be transferred to the smb node, which will commit the
1803  * delete operation and inhibit subsequent open requests.
1804  *
1805  * When DeleteOnClose is set on an smb_node, the common open code will
1806  * reject subsequent open requests for the file. Observation of Windows
1807  * 2000 indicates that subsequent opens should be allowed (assuming
1808  * there would be no sharing violation) until the file is closed using
1809  * the fid on which the DeleteOnClose was requested.
1810  */
1811 void
smb_ofile_set_delete_on_close(smb_request_t * sr,smb_ofile_t * of)1812 smb_ofile_set_delete_on_close(smb_request_t *sr, smb_ofile_t *of)
1813 {
1814 	uint32_t	status;
1815 
1816 	/*
1817 	 * Break any oplock handle caching.
1818 	 */
1819 	status = smb_oplock_break_SETINFO(of->f_node, of,
1820 	    FileDispositionInformation);
1821 	if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) {
1822 		if (sr->session->dialect >= SMB_VERS_2_BASE)
1823 			(void) smb2sr_go_async(sr);
1824 		(void) smb_oplock_wait_break(sr, of->f_node, 0);
1825 	}
1826 
1827 	mutex_enter(&of->f_mutex);
1828 	of->f_flags |= SMB_OFLAGS_SET_DELETE_ON_CLOSE;
1829 	mutex_exit(&of->f_mutex);
1830 }
1831 
1832 /*
1833  * Encode open file information into a buffer; needed in user space to
1834  * support RPC requests.
1835  */
1836 static int
smb_ofile_netinfo_encode(smb_ofile_t * of,uint8_t * buf,size_t buflen,uint32_t * nbytes)1837 smb_ofile_netinfo_encode(smb_ofile_t *of, uint8_t *buf, size_t buflen,
1838     uint32_t *nbytes)
1839 {
1840 	smb_netfileinfo_t	fi;
1841 	int			rc;
1842 
1843 	rc = smb_ofile_netinfo_init(of, &fi);
1844 	if (rc == 0) {
1845 		rc = smb_netfileinfo_encode(&fi, buf, buflen, nbytes);
1846 		smb_ofile_netinfo_fini(&fi);
1847 	}
1848 
1849 	return (rc);
1850 }
1851 
1852 static int
smb_ofile_netinfo_init(smb_ofile_t * of,smb_netfileinfo_t * fi)1853 smb_ofile_netinfo_init(smb_ofile_t *of, smb_netfileinfo_t *fi)
1854 {
1855 	smb_user_t	*user;
1856 	smb_tree_t	*tree;
1857 	smb_node_t	*node;
1858 	char		*path;
1859 	char		*buf;
1860 	int		rc;
1861 
1862 	ASSERT(of);
1863 	user = of->f_user;
1864 	tree = of->f_tree;
1865 	ASSERT(user);
1866 	ASSERT(tree);
1867 
1868 	buf = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1869 
1870 	switch (of->f_ftype) {
1871 	case SMB_FTYPE_DISK:
1872 		node = of->f_node;
1873 		ASSERT(node);
1874 
1875 		fi->fi_permissions = of->f_granted_access;
1876 		fi->fi_numlocks = smb_lock_get_lock_count(node, of);
1877 
1878 		path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1879 
1880 		if (node != tree->t_snode) {
1881 			rc = smb_node_getshrpath(node, tree, path, MAXPATHLEN);
1882 			if (rc != 0)
1883 				(void) strlcpy(path, node->od_name, MAXPATHLEN);
1884 		}
1885 
1886 		(void) snprintf(buf, MAXPATHLEN, "%s:%s", tree->t_sharename,
1887 		    path);
1888 		kmem_free(path, MAXPATHLEN);
1889 		break;
1890 
1891 	case SMB_FTYPE_MESG_PIPE:
1892 		ASSERT(of->f_pipe);
1893 
1894 		fi->fi_permissions = FILE_READ_DATA | FILE_WRITE_DATA |
1895 		    FILE_EXECUTE;
1896 		fi->fi_numlocks = 0;
1897 		(void) snprintf(buf, MAXPATHLEN, "\\PIPE\\%s",
1898 		    of->f_pipe->p_name);
1899 		break;
1900 
1901 	default:
1902 		kmem_free(buf, MAXPATHLEN);
1903 		return (-1);
1904 	}
1905 
1906 	fi->fi_fid = of->f_fid;
1907 	fi->fi_uniqid = of->f_uniqid;
1908 	fi->fi_pathlen = strlen(buf) + 1;
1909 	fi->fi_path = smb_mem_strdup(buf);
1910 	kmem_free(buf, MAXPATHLEN);
1911 
1912 	fi->fi_namelen = user->u_domain_len + user->u_name_len + 2;
1913 	fi->fi_username = kmem_alloc(fi->fi_namelen, KM_SLEEP);
1914 	(void) snprintf(fi->fi_username, fi->fi_namelen, "%s\\%s",
1915 	    user->u_domain, user->u_name);
1916 	return (0);
1917 }
1918 
1919 static void
smb_ofile_netinfo_fini(smb_netfileinfo_t * fi)1920 smb_ofile_netinfo_fini(smb_netfileinfo_t *fi)
1921 {
1922 	if (fi == NULL)
1923 		return;
1924 
1925 	if (fi->fi_path)
1926 		smb_mem_free(fi->fi_path);
1927 	if (fi->fi_username)
1928 		kmem_free(fi->fi_username, fi->fi_namelen);
1929 
1930 	bzero(fi, sizeof (smb_netfileinfo_t));
1931 }
1932 
1933 /*
1934  * A query of user and group quotas may span multiple requests.
1935  * f_quota_resume is used to determine where the query should
1936  * be resumed, in a subsequent request. f_quota_resume contains
1937  * the SID of the last quota entry returned to the client.
1938  */
1939 void
smb_ofile_set_quota_resume(smb_ofile_t * ofile,char * resume)1940 smb_ofile_set_quota_resume(smb_ofile_t *ofile, char *resume)
1941 {
1942 	ASSERT(ofile);
1943 	mutex_enter(&ofile->f_mutex);
1944 	if (resume == NULL)
1945 		bzero(ofile->f_quota_resume, SMB_SID_STRSZ);
1946 	else
1947 		(void) strlcpy(ofile->f_quota_resume, resume, SMB_SID_STRSZ);
1948 	mutex_exit(&ofile->f_mutex);
1949 }
1950 
1951 void
smb_ofile_get_quota_resume(smb_ofile_t * ofile,char * buf,int bufsize)1952 smb_ofile_get_quota_resume(smb_ofile_t *ofile, char *buf, int bufsize)
1953 {
1954 	ASSERT(ofile);
1955 	mutex_enter(&ofile->f_mutex);
1956 	(void) strlcpy(buf, ofile->f_quota_resume, bufsize);
1957 	mutex_exit(&ofile->f_mutex);
1958 }
1959