xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_common_open.c (revision 1ed6b69a5ca1ca3ee5e9a4931f74e2237c7e1c9f)
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 /*
23  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
24  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
25  */
26 
27 /*
28  * This module provides the common open functionality to the various
29  * open and create SMB interface functions.
30  */
31 
32 #include <sys/types.h>
33 #include <sys/cmn_err.h>
34 #include <sys/fcntl.h>
35 #include <sys/nbmlock.h>
36 #include <smbsrv/string.h>
37 #include <smbsrv/smb_kproto.h>
38 #include <smbsrv/smb_fsops.h>
39 #include <smbsrv/smbinfo.h>
40 
41 volatile uint32_t smb_fids = 0;
42 
43 static uint32_t smb_open_subr(smb_request_t *);
44 extern uint32_t smb_is_executable(char *);
45 static void smb_delete_new_object(smb_request_t *);
46 static int smb_set_open_timestamps(smb_request_t *, smb_ofile_t *, boolean_t);
47 static void smb_open_oplock_break(smb_request_t *, smb_node_t *);
48 static boolean_t smb_open_attr_only(smb_arg_open_t *);
49 static boolean_t smb_open_overwrite(smb_arg_open_t *);
50 
51 /*
52  * smb_access_generic_to_file
53  *
54  * Search MSDN for IoCreateFile to see following mapping.
55  *
56  * GENERIC_READ		STANDARD_RIGHTS_READ, FILE_READ_DATA,
57  *			FILE_READ_ATTRIBUTES and FILE_READ_EA
58  *
59  * GENERIC_WRITE	STANDARD_RIGHTS_WRITE, FILE_WRITE_DATA,
60  *               FILE_WRITE_ATTRIBUTES, FILE_WRITE_EA, and FILE_APPEND_DATA
61  *
62  * GENERIC_EXECUTE	STANDARD_RIGHTS_EXECUTE, SYNCHRONIZE, and FILE_EXECUTE.
63  *
64  * Careful, we have to emulate some Windows behavior here.
65  * When requested access == zero, you get READ_CONTROL.
66  * MacOS 10.7 depends on this.
67  */
68 uint32_t
69 smb_access_generic_to_file(uint32_t desired_access)
70 {
71 	uint32_t access = READ_CONTROL;
72 
73 	if (desired_access & GENERIC_ALL)
74 		return (FILE_ALL_ACCESS & ~SYNCHRONIZE);
75 
76 	if (desired_access & GENERIC_EXECUTE) {
77 		desired_access &= ~GENERIC_EXECUTE;
78 		access |= (STANDARD_RIGHTS_EXECUTE |
79 		    SYNCHRONIZE | FILE_EXECUTE);
80 	}
81 
82 	if (desired_access & GENERIC_WRITE) {
83 		desired_access &= ~GENERIC_WRITE;
84 		access |= (FILE_GENERIC_WRITE & ~SYNCHRONIZE);
85 	}
86 
87 	if (desired_access & GENERIC_READ) {
88 		desired_access &= ~GENERIC_READ;
89 		access |= FILE_GENERIC_READ;
90 	}
91 
92 	return (access | desired_access);
93 }
94 
95 /*
96  * smb_omode_to_amask
97  *
98  * This function converts open modes used by Open and Open AndX
99  * commands to desired access bits used by NT Create AndX command.
100  */
101 uint32_t
102 smb_omode_to_amask(uint32_t desired_access)
103 {
104 	switch (desired_access & SMB_DA_ACCESS_MASK) {
105 	case SMB_DA_ACCESS_READ:
106 		return (FILE_GENERIC_READ);
107 
108 	case SMB_DA_ACCESS_WRITE:
109 		return (FILE_GENERIC_WRITE);
110 
111 	case SMB_DA_ACCESS_READ_WRITE:
112 		return (FILE_GENERIC_READ | FILE_GENERIC_WRITE);
113 
114 	case SMB_DA_ACCESS_EXECUTE:
115 		return (FILE_GENERIC_EXECUTE);
116 
117 	default:
118 		return (FILE_GENERIC_ALL);
119 	}
120 }
121 
122 /*
123  * smb_denymode_to_sharemode
124  *
125  * This function converts deny modes used by Open and Open AndX
126  * commands to share access bits used by NT Create AndX command.
127  */
128 uint32_t
129 smb_denymode_to_sharemode(uint32_t desired_access, char *fname)
130 {
131 	switch (desired_access & SMB_DA_SHARE_MASK) {
132 	case SMB_DA_SHARE_COMPATIBILITY:
133 		if (smb_is_executable(fname))
134 			return (FILE_SHARE_READ | FILE_SHARE_WRITE);
135 
136 		return (FILE_SHARE_ALL);
137 
138 	case SMB_DA_SHARE_EXCLUSIVE:
139 		return (FILE_SHARE_NONE);
140 
141 	case SMB_DA_SHARE_DENY_WRITE:
142 		return (FILE_SHARE_READ);
143 
144 	case SMB_DA_SHARE_DENY_READ:
145 		return (FILE_SHARE_WRITE);
146 
147 	case SMB_DA_SHARE_DENY_NONE:
148 	default:
149 		return (FILE_SHARE_READ | FILE_SHARE_WRITE);
150 	}
151 }
152 
153 /*
154  * smb_ofun_to_crdisposition
155  *
156  * This function converts open function values used by Open and Open AndX
157  * commands to create disposition values used by NT Create AndX command.
158  */
159 uint32_t
160 smb_ofun_to_crdisposition(uint16_t  ofun)
161 {
162 	static int ofun_cr_map[3][2] =
163 	{
164 		{ -1,			FILE_CREATE },
165 		{ FILE_OPEN,		FILE_OPEN_IF },
166 		{ FILE_OVERWRITE,	FILE_OVERWRITE_IF }
167 	};
168 
169 	int row = ofun & SMB_OFUN_OPEN_MASK;
170 	int col = (ofun & SMB_OFUN_CREATE_MASK) >> 4;
171 
172 	if (row == 3)
173 		return (FILE_MAXIMUM_DISPOSITION + 1);
174 
175 	return (ofun_cr_map[row][col]);
176 }
177 
178 /*
179  * Retry opens to avoid spurious sharing violations, due to timing
180  * issues between closes and opens.  The client that already has the
181  * file open may be in the process of closing it.
182  */
183 uint32_t
184 smb_common_open(smb_request_t *sr)
185 {
186 	smb_arg_open_t	*parg;
187 	uint32_t	status = NT_STATUS_SUCCESS;
188 	int		count;
189 
190 	parg = kmem_alloc(sizeof (*parg), KM_SLEEP);
191 	bcopy(&sr->arg.open, parg, sizeof (*parg));
192 
193 	for (count = 0; count <= 4; count++) {
194 		if (count != 0)
195 			delay(MSEC_TO_TICK(400));
196 
197 		status = smb_open_subr(sr);
198 		if (status != NT_STATUS_SHARING_VIOLATION)
199 			break;
200 
201 		bcopy(parg, &sr->arg.open, sizeof (*parg));
202 	}
203 
204 	if (status == NT_STATUS_SHARING_VIOLATION) {
205 		smbsr_error(sr, NT_STATUS_SHARING_VIOLATION,
206 		    ERRDOS, ERROR_SHARING_VIOLATION);
207 	}
208 
209 	if (status == NT_STATUS_NO_SUCH_FILE) {
210 		smbsr_error(sr, NT_STATUS_OBJECT_NAME_NOT_FOUND,
211 		    ERRDOS, ERROR_FILE_NOT_FOUND);
212 	}
213 
214 	kmem_free(parg, sizeof (*parg));
215 	return (status);
216 }
217 
218 /*
219  * smb_open_subr
220  *
221  * Notes on write-through behaviour. It looks like pre-LM0.12 versions
222  * of the protocol specify the write-through mode when a file is opened,
223  * (SmbOpen, SmbOpenAndX) so the write calls (SmbWrite, SmbWriteAndClose,
224  * SmbWriteAndUnlock) don't need to contain a write-through flag.
225  *
226  * With LM0.12, the open calls (SmbCreateAndX, SmbNtTransactCreate)
227  * don't indicate which write-through mode to use. Instead the write
228  * calls (SmbWriteAndX, SmbWriteRaw) specify the mode on a per call
229  * basis.
230  *
231  * We don't care which open call was used to get us here, we just need
232  * to ensure that the write-through mode flag is copied from the open
233  * parameters to the node. We test the omode write-through flag in all
234  * write functions.
235  *
236  * This function will return NT status codes but it also raises errors,
237  * in which case it won't return to the caller. Be careful how you
238  * handle things in here.
239  *
240  * The following rules apply when processing a file open request:
241  *
242  * - Oplocks must be broken prior to share checking as the break may
243  *   cause other clients to close the file, which would affect sharing
244  *   checks.
245  *
246  * - Share checks must take place prior to access checks for correct
247  * Windows semantics and to prevent unnecessary NFS delegation recalls.
248  *
249  * - Oplocks must be acquired after open to ensure the correct
250  * synchronization with NFS delegation and FEM installation.
251  *
252  * DOS readonly bit rules
253  *
254  * 1. The creator of a readonly file can write to/modify the size of the file
255  * using the original create fid, even though the file will appear as readonly
256  * to all other fids and via a CIFS getattr call.
257  * The readonly bit therefore cannot be set in the filesystem until the file
258  * is closed (smb_ofile_close). It is accounted for via ofile and node flags.
259  *
260  * 2. A setinfo operation (using either an open fid or a path) to set/unset
261  * readonly will be successful regardless of whether a creator of a readonly
262  * file has an open fid (and has the special privilege mentioned in #1,
263  * above).  I.e., the creator of a readonly fid holding that fid will no longer
264  * have a special privilege.
265  *
266  * 3. The DOS readonly bit affects only data and some metadata.
267  * The following metadata can be changed regardless of the readonly bit:
268  * 	- security descriptors
269  *	- DOS attributes
270  *	- timestamps
271  *
272  * In the current implementation, the file size cannot be changed (except for
273  * the exceptions in #1 and #2, above).
274  *
275  *
276  * DOS attribute rules
277  *
278  * These rules are specific to creating / opening files and directories.
279  * How the attribute value (specifically ZERO or FILE_ATTRIBUTE_NORMAL)
280  * should be interpreted may differ in other requests.
281  *
282  * - An attribute value equal to ZERO or FILE_ATTRIBUTE_NORMAL means that the
283  *   file's attributes should be cleared.
284  * - If FILE_ATTRIBUTE_NORMAL is specified with any other attributes,
285  *   FILE_ATTRIBUTE_NORMAL is ignored.
286  *
287  * 1. Creating a new file
288  * - The request attributes + FILE_ATTRIBUTE_ARCHIVE are applied to the file.
289  *
290  * 2. Creating a new directory
291  * - The request attributes + FILE_ATTRIBUTE_DIRECTORY are applied to the file.
292  * - FILE_ATTRIBUTE_ARCHIVE does not get set.
293  *
294  * 3. Overwriting an existing file
295  * - the request attributes are used as search attributes. If the existing
296  *   file does not meet the search criteria access is denied.
297  * - otherwise, applies attributes + FILE_ATTRIBUTE_ARCHIVE.
298  *
299  * 4. Opening an existing file or directory
300  *    The request attributes are ignored.
301  */
302 static uint32_t
303 smb_open_subr(smb_request_t *sr)
304 {
305 	boolean_t	created = B_FALSE;
306 	boolean_t	last_comp_found = B_FALSE;
307 	smb_node_t	*node = NULL;
308 	smb_node_t	*dnode = NULL;
309 	smb_node_t	*cur_node = NULL;
310 	smb_arg_open_t	*op = &sr->sr_open;
311 	int		rc;
312 	smb_ofile_t	*of;
313 	smb_attr_t	new_attr;
314 	int		max_requested = 0;
315 	uint32_t	max_allowed;
316 	uint32_t	status = NT_STATUS_SUCCESS;
317 	int		is_dir;
318 	smb_error_t	err;
319 	boolean_t	is_stream = B_FALSE;
320 	int		lookup_flags = SMB_FOLLOW_LINKS;
321 	uint32_t	uniq_fid;
322 	smb_pathname_t	*pn = &op->fqi.fq_path;
323 	smb_server_t	*sv = sr->sr_server;
324 
325 	is_dir = (op->create_options & FILE_DIRECTORY_FILE) ? 1 : 0;
326 
327 	/*
328 	 * If the object being created or opened is a directory
329 	 * the Disposition parameter must be one of FILE_CREATE,
330 	 * FILE_OPEN, or FILE_OPEN_IF
331 	 */
332 	if (is_dir) {
333 		if ((op->create_disposition != FILE_CREATE) &&
334 		    (op->create_disposition != FILE_OPEN_IF) &&
335 		    (op->create_disposition != FILE_OPEN)) {
336 			smbsr_error(sr, NT_STATUS_INVALID_PARAMETER,
337 			    ERRDOS, ERROR_INVALID_ACCESS);
338 			return (NT_STATUS_INVALID_PARAMETER);
339 		}
340 	}
341 
342 	if (op->desired_access & MAXIMUM_ALLOWED) {
343 		max_requested = 1;
344 		op->desired_access &= ~MAXIMUM_ALLOWED;
345 	}
346 	op->desired_access = smb_access_generic_to_file(op->desired_access);
347 
348 	if (sr->session->s_file_cnt >= SMB_SESSION_OFILE_MAX) {
349 		ASSERT(sr->uid_user);
350 		cmn_err(CE_NOTE, "smbsrv[%s\\%s]: TOO_MANY_OPENED_FILES",
351 		    sr->uid_user->u_domain, sr->uid_user->u_name);
352 
353 		smbsr_error(sr, NT_STATUS_TOO_MANY_OPENED_FILES,
354 		    ERRDOS, ERROR_TOO_MANY_OPEN_FILES);
355 		return (NT_STATUS_TOO_MANY_OPENED_FILES);
356 	}
357 
358 	/* This must be NULL at this point */
359 	sr->fid_ofile = NULL;
360 
361 	op->devstate = 0;
362 
363 	switch (sr->tid_tree->t_res_type & STYPE_MASK) {
364 	case STYPE_DISKTREE:
365 	case STYPE_PRINTQ:
366 		break;
367 
368 	case STYPE_IPC:
369 
370 		if ((rc = smb_threshold_enter(&sv->sv_opipe_ct)) != 0) {
371 			status = RPC_NT_SERVER_TOO_BUSY;
372 			smbsr_error(sr, status, 0, 0);
373 			return (status);
374 		}
375 
376 		/*
377 		 * No further processing for IPC, we need to either
378 		 * raise an exception or return success here.
379 		 */
380 		if ((status = smb_opipe_open(sr)) != NT_STATUS_SUCCESS)
381 			smbsr_error(sr, status, 0, 0);
382 
383 		smb_threshold_exit(&sv->sv_opipe_ct, sv);
384 		return (status);
385 
386 	default:
387 		smbsr_error(sr, NT_STATUS_BAD_DEVICE_TYPE,
388 		    ERRDOS, ERROR_BAD_DEV_TYPE);
389 		return (NT_STATUS_BAD_DEVICE_TYPE);
390 	}
391 
392 	smb_pathname_init(sr, pn, pn->pn_path);
393 	if (!smb_pathname_validate(sr, pn))
394 		return (sr->smb_error.status);
395 
396 	if (strlen(pn->pn_path) >= MAXPATHLEN) {
397 		smbsr_error(sr, 0, ERRSRV, ERRfilespecs);
398 		return (NT_STATUS_NAME_TOO_LONG);
399 	}
400 
401 	if (is_dir) {
402 		if (!smb_validate_dirname(sr, pn))
403 			return (sr->smb_error.status);
404 	} else {
405 		if (!smb_validate_object_name(sr, pn))
406 			return (sr->smb_error.status);
407 	}
408 
409 	cur_node = op->fqi.fq_dnode ?
410 	    op->fqi.fq_dnode : sr->tid_tree->t_snode;
411 
412 	/*
413 	 * if no path or filename are specified the stream should be
414 	 * created on cur_node
415 	 */
416 	if (!is_dir && !pn->pn_pname && !pn->pn_fname && pn->pn_sname) {
417 		/*
418 		 * Can't currently handle a stream on the tree root.
419 		 * If a stream is being opened return "not found", otherwise
420 		 * return "access denied".
421 		 */
422 		if (cur_node == sr->tid_tree->t_snode) {
423 			if (op->create_disposition == FILE_OPEN) {
424 				smbsr_error(sr, NT_STATUS_OBJECT_NAME_NOT_FOUND,
425 				    ERRDOS, ERROR_FILE_NOT_FOUND);
426 				return (NT_STATUS_OBJECT_NAME_NOT_FOUND);
427 			}
428 			smbsr_error(sr, NT_STATUS_ACCESS_DENIED, ERRDOS,
429 			    ERROR_ACCESS_DENIED);
430 			return (NT_STATUS_ACCESS_DENIED);
431 		}
432 
433 		(void) snprintf(op->fqi.fq_last_comp,
434 		    sizeof (op->fqi.fq_last_comp),
435 		    "%s%s", cur_node->od_name, pn->pn_sname);
436 
437 		op->fqi.fq_dnode = cur_node->n_dnode;
438 		smb_node_ref(op->fqi.fq_dnode);
439 	} else {
440 		if (rc = smb_pathname_reduce(sr, sr->user_cr, pn->pn_path,
441 		    sr->tid_tree->t_snode, cur_node, &op->fqi.fq_dnode,
442 		    op->fqi.fq_last_comp)) {
443 			smbsr_errno(sr, rc);
444 			return (sr->smb_error.status);
445 		}
446 	}
447 
448 	/*
449 	 * If the access mask has only DELETE set (ignore
450 	 * FILE_READ_ATTRIBUTES), then assume that this
451 	 * is a request to delete the link (if a link)
452 	 * and do not follow links.  Otherwise, follow
453 	 * the link to the target.
454 	 */
455 	if ((op->desired_access & ~FILE_READ_ATTRIBUTES) == DELETE)
456 		lookup_flags &= ~SMB_FOLLOW_LINKS;
457 
458 	rc = smb_fsop_lookup_name(sr, kcred, lookup_flags,
459 	    sr->tid_tree->t_snode, op->fqi.fq_dnode, op->fqi.fq_last_comp,
460 	    &op->fqi.fq_fnode);
461 
462 	if (rc == 0) {
463 		last_comp_found = B_TRUE;
464 		rc = smb_node_getattr(sr, op->fqi.fq_fnode,
465 		    &op->fqi.fq_fattr);
466 		if (rc != 0) {
467 			smb_node_release(op->fqi.fq_fnode);
468 			smb_node_release(op->fqi.fq_dnode);
469 			smbsr_error(sr, NT_STATUS_INTERNAL_ERROR,
470 			    ERRDOS, ERROR_INTERNAL_ERROR);
471 			return (sr->smb_error.status);
472 		}
473 	} else if (rc == ENOENT) {
474 		last_comp_found = B_FALSE;
475 		op->fqi.fq_fnode = NULL;
476 		rc = 0;
477 	} else {
478 		smb_node_release(op->fqi.fq_dnode);
479 		smbsr_errno(sr, rc);
480 		return (sr->smb_error.status);
481 	}
482 
483 
484 	/*
485 	 * The uniq_fid is a CIFS-server-wide unique identifier for an ofile
486 	 * which is used to uniquely identify open instances for the
487 	 * VFS share reservation and POSIX locks.
488 	 */
489 
490 	uniq_fid = SMB_UNIQ_FID();
491 
492 	if (last_comp_found) {
493 
494 		node = op->fqi.fq_fnode;
495 		dnode = op->fqi.fq_dnode;
496 
497 		if (!smb_node_is_file(node) && !smb_node_is_dir(node) &&
498 		    !smb_node_is_symlink(node)) {
499 			smb_node_release(node);
500 			smb_node_release(dnode);
501 			smbsr_error(sr, NT_STATUS_ACCESS_DENIED, ERRDOS,
502 			    ERRnoaccess);
503 			return (NT_STATUS_ACCESS_DENIED);
504 		}
505 
506 		/*
507 		 * Reject this request if either:
508 		 * - the target IS a directory and the client requires that
509 		 *   it must NOT be (required by Lotus Notes)
510 		 * - the target is NOT a directory and client requires that
511 		 *   it MUST be.
512 		 */
513 		if (smb_node_is_dir(node)) {
514 			if (op->create_options & FILE_NON_DIRECTORY_FILE) {
515 				smb_node_release(node);
516 				smb_node_release(dnode);
517 				smbsr_error(sr, NT_STATUS_FILE_IS_A_DIRECTORY,
518 				    ERRDOS, ERROR_ACCESS_DENIED);
519 				return (NT_STATUS_FILE_IS_A_DIRECTORY);
520 			}
521 		} else {
522 			if ((op->create_options & FILE_DIRECTORY_FILE) ||
523 			    (op->nt_flags & NT_CREATE_FLAG_OPEN_TARGET_DIR)) {
524 				smb_node_release(node);
525 				smb_node_release(dnode);
526 				smbsr_error(sr, NT_STATUS_NOT_A_DIRECTORY,
527 				    ERRDOS, ERROR_DIRECTORY);
528 				return (NT_STATUS_NOT_A_DIRECTORY);
529 			}
530 		}
531 
532 		/*
533 		 * No more open should be accepted when "Delete on close"
534 		 * flag is set.
535 		 */
536 		if (node->flags & NODE_FLAGS_DELETE_ON_CLOSE) {
537 			smb_node_release(node);
538 			smb_node_release(dnode);
539 			smbsr_error(sr, NT_STATUS_DELETE_PENDING,
540 			    ERRDOS, ERROR_ACCESS_DENIED);
541 			return (NT_STATUS_DELETE_PENDING);
542 		}
543 
544 		/*
545 		 * Specified file already exists so the operation should fail.
546 		 */
547 		if (op->create_disposition == FILE_CREATE) {
548 			smb_node_release(node);
549 			smb_node_release(dnode);
550 			smbsr_error(sr, NT_STATUS_OBJECT_NAME_COLLISION,
551 			    ERRDOS, ERROR_FILE_EXISTS);
552 			return (NT_STATUS_OBJECT_NAME_COLLISION);
553 		}
554 
555 		/*
556 		 * Windows seems to check read-only access before file
557 		 * sharing check.
558 		 *
559 		 * Check to see if the file is currently readonly (irrespective
560 		 * of whether this open will make it readonly).
561 		 */
562 		if (SMB_PATHFILE_IS_READONLY(sr, node)) {
563 			/* Files data only */
564 			if (!smb_node_is_dir(node)) {
565 				if (op->desired_access & (FILE_WRITE_DATA |
566 				    FILE_APPEND_DATA)) {
567 					smb_node_release(node);
568 					smb_node_release(dnode);
569 					smbsr_error(sr, NT_STATUS_ACCESS_DENIED,
570 					    ERRDOS, ERRnoaccess);
571 					return (NT_STATUS_ACCESS_DENIED);
572 				}
573 			}
574 		}
575 
576 		/*
577 		 * Oplock break is done prior to sharing checks as the break
578 		 * may cause other clients to close the file which would
579 		 * affect the sharing checks.
580 		 */
581 		smb_node_inc_opening_count(node);
582 		smb_open_oplock_break(sr, node);
583 
584 		smb_node_wrlock(node);
585 
586 		if ((op->create_disposition == FILE_SUPERSEDE) ||
587 		    (op->create_disposition == FILE_OVERWRITE_IF) ||
588 		    (op->create_disposition == FILE_OVERWRITE)) {
589 
590 			if ((!(op->desired_access &
591 			    (FILE_WRITE_DATA | FILE_APPEND_DATA |
592 			    FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA))) ||
593 			    (!smb_sattr_check(op->fqi.fq_fattr.sa_dosattr,
594 			    op->dattr))) {
595 				smb_node_unlock(node);
596 				smb_node_dec_opening_count(node);
597 				smb_node_release(node);
598 				smb_node_release(dnode);
599 				smbsr_error(sr, NT_STATUS_ACCESS_DENIED,
600 				    ERRDOS, ERRnoaccess);
601 				return (NT_STATUS_ACCESS_DENIED);
602 			}
603 		}
604 
605 		status = smb_fsop_shrlock(sr->user_cr, node, uniq_fid,
606 		    op->desired_access, op->share_access);
607 
608 		if (status == NT_STATUS_SHARING_VIOLATION) {
609 			smb_node_unlock(node);
610 			smb_node_dec_opening_count(node);
611 			smb_node_release(node);
612 			smb_node_release(dnode);
613 			return (status);
614 		}
615 
616 		status = smb_fsop_access(sr, sr->user_cr, node,
617 		    op->desired_access);
618 
619 		if (status != NT_STATUS_SUCCESS) {
620 			smb_fsop_unshrlock(sr->user_cr, node, uniq_fid);
621 
622 			smb_node_unlock(node);
623 			smb_node_dec_opening_count(node);
624 			smb_node_release(node);
625 			smb_node_release(dnode);
626 
627 			if (status == NT_STATUS_PRIVILEGE_NOT_HELD) {
628 				smbsr_error(sr, status,
629 				    ERRDOS, ERROR_PRIVILEGE_NOT_HELD);
630 				return (status);
631 			} else {
632 				smbsr_error(sr, NT_STATUS_ACCESS_DENIED,
633 				    ERRDOS, ERROR_ACCESS_DENIED);
634 				return (NT_STATUS_ACCESS_DENIED);
635 			}
636 		}
637 
638 		switch (op->create_disposition) {
639 		case FILE_SUPERSEDE:
640 		case FILE_OVERWRITE_IF:
641 		case FILE_OVERWRITE:
642 			if (smb_node_is_dir(node)) {
643 				smb_fsop_unshrlock(sr->user_cr, node, uniq_fid);
644 				smb_node_unlock(node);
645 				smb_node_dec_opening_count(node);
646 				smb_node_release(node);
647 				smb_node_release(dnode);
648 				smbsr_error(sr, NT_STATUS_ACCESS_DENIED,
649 				    ERRDOS, ERROR_ACCESS_DENIED);
650 				return (NT_STATUS_ACCESS_DENIED);
651 			}
652 
653 			op->dattr |= FILE_ATTRIBUTE_ARCHIVE;
654 			/* Don't apply readonly bit until smb_ofile_close */
655 			if (op->dattr & FILE_ATTRIBUTE_READONLY) {
656 				op->created_readonly = B_TRUE;
657 				op->dattr &= ~FILE_ATTRIBUTE_READONLY;
658 			}
659 
660 			bzero(&new_attr, sizeof (new_attr));
661 			new_attr.sa_dosattr = op->dattr;
662 			new_attr.sa_vattr.va_size = op->dsize;
663 			new_attr.sa_mask = SMB_AT_DOSATTR | SMB_AT_SIZE;
664 			rc = smb_fsop_setattr(sr, sr->user_cr, node, &new_attr);
665 			if (rc != 0) {
666 				smb_fsop_unshrlock(sr->user_cr, node, uniq_fid);
667 				smb_node_unlock(node);
668 				smb_node_dec_opening_count(node);
669 				smb_node_release(node);
670 				smb_node_release(dnode);
671 				smbsr_errno(sr, rc);
672 				return (sr->smb_error.status);
673 			}
674 
675 			/*
676 			 * If file is being replaced, remove existing streams
677 			 */
678 			if (SMB_IS_STREAM(node) == 0) {
679 				rc = smb_fsop_remove_streams(sr, sr->user_cr,
680 				    node);
681 				if (rc != 0) {
682 					smb_fsop_unshrlock(sr->user_cr, node,
683 					    uniq_fid);
684 					smb_node_unlock(node);
685 					smb_node_dec_opening_count(node);
686 					smb_node_release(node);
687 					smb_node_release(dnode);
688 					return (sr->smb_error.status);
689 				}
690 			}
691 
692 			op->action_taken = SMB_OACT_TRUNCATED;
693 			break;
694 
695 		default:
696 			/*
697 			 * FILE_OPEN or FILE_OPEN_IF.
698 			 */
699 			op->action_taken = SMB_OACT_OPENED;
700 			break;
701 		}
702 	} else {
703 		/* Last component was not found. */
704 		dnode = op->fqi.fq_dnode;
705 
706 		if (is_dir == 0)
707 			is_stream = smb_is_stream_name(pn->pn_path);
708 
709 		if ((op->create_disposition == FILE_OPEN) ||
710 		    (op->create_disposition == FILE_OVERWRITE)) {
711 			smb_node_release(dnode);
712 			smbsr_error(sr, NT_STATUS_OBJECT_NAME_NOT_FOUND,
713 			    ERRDOS, ERROR_FILE_NOT_FOUND);
714 			return (NT_STATUS_OBJECT_NAME_NOT_FOUND);
715 		}
716 
717 		if (pn->pn_fname && smb_is_invalid_filename(pn->pn_fname)) {
718 			smb_node_release(dnode);
719 			smbsr_error(sr, NT_STATUS_OBJECT_NAME_INVALID,
720 			    ERRDOS, ERROR_INVALID_NAME);
721 			return (NT_STATUS_OBJECT_NAME_INVALID);
722 		}
723 
724 		/*
725 		 * lock the parent dir node in case another create
726 		 * request to the same parent directory comes in.
727 		 */
728 		smb_node_wrlock(dnode);
729 
730 		/* Don't apply readonly bit until smb_ofile_close */
731 		if (op->dattr & FILE_ATTRIBUTE_READONLY) {
732 			op->dattr &= ~FILE_ATTRIBUTE_READONLY;
733 			op->created_readonly = B_TRUE;
734 		}
735 
736 		bzero(&new_attr, sizeof (new_attr));
737 		if ((op->crtime.tv_sec != 0) &&
738 		    (op->crtime.tv_sec != UINT_MAX)) {
739 
740 			new_attr.sa_mask |= SMB_AT_CRTIME;
741 			new_attr.sa_crtime = op->crtime;
742 		}
743 
744 		if (is_dir == 0) {
745 			op->dattr |= FILE_ATTRIBUTE_ARCHIVE;
746 			new_attr.sa_dosattr = op->dattr;
747 			new_attr.sa_vattr.va_type = VREG;
748 			new_attr.sa_vattr.va_mode = is_stream ? S_IRUSR :
749 			    S_IRUSR | S_IRGRP | S_IROTH |
750 			    S_IWUSR | S_IWGRP | S_IWOTH;
751 			new_attr.sa_mask |=
752 			    SMB_AT_DOSATTR | SMB_AT_TYPE | SMB_AT_MODE;
753 
754 			if (op->dsize) {
755 				new_attr.sa_vattr.va_size = op->dsize;
756 				new_attr.sa_mask |= SMB_AT_SIZE;
757 			}
758 
759 			rc = smb_fsop_create(sr, sr->user_cr, dnode,
760 			    op->fqi.fq_last_comp, &new_attr, &op->fqi.fq_fnode);
761 
762 			if (rc != 0) {
763 				smb_node_unlock(dnode);
764 				smb_node_release(dnode);
765 				smbsr_errno(sr, rc);
766 				return (sr->smb_error.status);
767 			}
768 
769 			node = op->fqi.fq_fnode;
770 			smb_node_inc_opening_count(node);
771 			smb_node_wrlock(node);
772 
773 			status = smb_fsop_shrlock(sr->user_cr, node, uniq_fid,
774 			    op->desired_access, op->share_access);
775 
776 			if (status == NT_STATUS_SHARING_VIOLATION) {
777 				smb_node_unlock(node);
778 				smb_node_dec_opening_count(node);
779 				smb_delete_new_object(sr);
780 				smb_node_release(node);
781 				smb_node_unlock(dnode);
782 				smb_node_release(dnode);
783 				return (status);
784 			}
785 		} else {
786 			op->dattr |= FILE_ATTRIBUTE_DIRECTORY;
787 			new_attr.sa_dosattr = op->dattr;
788 			new_attr.sa_vattr.va_type = VDIR;
789 			new_attr.sa_vattr.va_mode = 0777;
790 			new_attr.sa_mask |=
791 			    SMB_AT_DOSATTR | SMB_AT_TYPE | SMB_AT_MODE;
792 
793 			rc = smb_fsop_mkdir(sr, sr->user_cr, dnode,
794 			    op->fqi.fq_last_comp, &new_attr, &op->fqi.fq_fnode);
795 			if (rc != 0) {
796 				smb_node_unlock(dnode);
797 				smb_node_release(dnode);
798 				smbsr_errno(sr, rc);
799 				return (sr->smb_error.status);
800 			}
801 
802 			node = op->fqi.fq_fnode;
803 			smb_node_inc_opening_count(node);
804 			smb_node_wrlock(node);
805 		}
806 
807 		created = B_TRUE;
808 		op->action_taken = SMB_OACT_CREATED;
809 	}
810 
811 	if (max_requested) {
812 		smb_fsop_eaccess(sr, sr->user_cr, node, &max_allowed);
813 		op->desired_access |= max_allowed;
814 	}
815 
816 	status = NT_STATUS_SUCCESS;
817 
818 	of = smb_ofile_open(sr->tid_tree, node, sr->smb_pid, op, SMB_FTYPE_DISK,
819 	    uniq_fid, &err);
820 	if (of == NULL) {
821 		smbsr_error(sr, err.status, err.errcls, err.errcode);
822 		status = err.status;
823 	}
824 
825 	if (status == NT_STATUS_SUCCESS) {
826 		if (!smb_tree_is_connected(sr->tid_tree)) {
827 			smbsr_error(sr, 0, ERRSRV, ERRinvnid);
828 			status = NT_STATUS_UNSUCCESSFUL;
829 		}
830 	}
831 
832 	/*
833 	 * This MUST be done after ofile creation, so that explicitly
834 	 * set timestamps can be remembered on the ofile.
835 	 */
836 	if (status == NT_STATUS_SUCCESS) {
837 		if ((rc = smb_set_open_timestamps(sr, of, created)) != 0) {
838 			smbsr_errno(sr, rc);
839 			status = sr->smb_error.status;
840 		}
841 	}
842 
843 	if (status == NT_STATUS_SUCCESS) {
844 		if (smb_node_getattr(sr, node,  &op->fqi.fq_fattr) != 0) {
845 			smbsr_error(sr, NT_STATUS_INTERNAL_ERROR,
846 			    ERRDOS, ERROR_INTERNAL_ERROR);
847 			status = NT_STATUS_INTERNAL_ERROR;
848 		}
849 	}
850 
851 	/*
852 	 * smb_fsop_unshrlock is a no-op if node is a directory
853 	 * smb_fsop_unshrlock is done in smb_ofile_close
854 	 */
855 	if (status != NT_STATUS_SUCCESS) {
856 		if (of == NULL) {
857 			smb_fsop_unshrlock(sr->user_cr, node, uniq_fid);
858 		} else {
859 			smb_ofile_close(of, 0);
860 			smb_ofile_release(of);
861 		}
862 		if (created)
863 			smb_delete_new_object(sr);
864 		smb_node_unlock(node);
865 		smb_node_dec_opening_count(node);
866 		smb_node_release(node);
867 		if (created)
868 			smb_node_unlock(dnode);
869 		smb_node_release(dnode);
870 		return (status);
871 	}
872 
873 	/*
874 	 * Propagate the write-through mode from the open params
875 	 * to the node: see the notes in the function header.
876 	 */
877 	if (sr->sr_cfg->skc_sync_enable ||
878 	    (op->create_options & FILE_WRITE_THROUGH))
879 		node->flags |= NODE_FLAGS_WRITE_THROUGH;
880 
881 	/*
882 	 * Set up the fileid and dosattr in open_param for response
883 	 */
884 	op->fileid = op->fqi.fq_fattr.sa_vattr.va_nodeid;
885 	op->dattr = op->fqi.fq_fattr.sa_dosattr;
886 
887 	/*
888 	 * Set up the file type in open_param for the response
889 	 */
890 	op->ftype = SMB_FTYPE_DISK;
891 	sr->smb_fid = of->f_fid;
892 	sr->fid_ofile = of;
893 
894 	if (smb_node_is_file(node)) {
895 		smb_oplock_acquire(sr, node, of);
896 		op->dsize = op->fqi.fq_fattr.sa_vattr.va_size;
897 	} else {
898 		/* directory or symlink */
899 		op->op_oplock_level = SMB_OPLOCK_NONE;
900 		op->dsize = 0;
901 	}
902 
903 	smb_node_dec_opening_count(node);
904 
905 	smb_node_unlock(node);
906 	if (created)
907 		smb_node_unlock(dnode);
908 
909 	smb_node_release(node);
910 	smb_node_release(dnode);
911 
912 	return (NT_STATUS_SUCCESS);
913 }
914 
915 /*
916  * smb_open_oplock_break
917  *
918  * If the node has an ofile opened with share access none,
919  * (smb_node_share_check = FALSE) only break BATCH oplock.
920  * Otherwise:
921  * If overwriting, break to SMB_OPLOCK_NONE, else
922  * If opening for anything other than attribute access,
923  * break oplock to LEVEL_II.
924  */
925 static void
926 smb_open_oplock_break(smb_request_t *sr, smb_node_t *node)
927 {
928 	smb_arg_open_t	*op = &sr->sr_open;
929 	uint32_t	flags = 0;
930 
931 	if (!smb_node_share_check(node))
932 		flags |= SMB_OPLOCK_BREAK_BATCH;
933 
934 	if (smb_open_overwrite(op)) {
935 		flags |= SMB_OPLOCK_BREAK_TO_NONE;
936 		(void) smb_oplock_break(sr, node, flags);
937 	} else if (!smb_open_attr_only(op)) {
938 		flags |= SMB_OPLOCK_BREAK_TO_LEVEL_II;
939 		(void) smb_oplock_break(sr, node, flags);
940 	}
941 }
942 
943 /*
944  * smb_open_attr_only
945  *
946  * Determine if file is being opened for attribute access only.
947  * This is used to determine whether it is necessary to break
948  * existing oplocks on the file.
949  */
950 static boolean_t
951 smb_open_attr_only(smb_arg_open_t *op)
952 {
953 	if (((op->desired_access & ~(FILE_READ_ATTRIBUTES |
954 	    FILE_WRITE_ATTRIBUTES | SYNCHRONIZE)) == 0) &&
955 	    (op->create_disposition != FILE_SUPERSEDE) &&
956 	    (op->create_disposition != FILE_OVERWRITE)) {
957 		return (B_TRUE);
958 	}
959 	return (B_FALSE);
960 }
961 
962 static boolean_t
963 smb_open_overwrite(smb_arg_open_t *op)
964 {
965 	if ((op->create_disposition == FILE_SUPERSEDE) ||
966 	    (op->create_disposition == FILE_OVERWRITE_IF) ||
967 	    (op->create_disposition == FILE_OVERWRITE)) {
968 		return (B_TRUE);
969 	}
970 	return (B_FALSE);
971 }
972 /*
973  * smb_set_open_timestamps
974  *
975  * Last write time:
976  * - If the last_write time specified in the open params is not 0 or -1,
977  *   use it as file's mtime. This will be considered an explicitly set
978  *   timestamps, not reset by subsequent writes.
979  *
980  * Opening existing file (not directory):
981  * - If opening an existing file for overwrite set initial ATIME, MTIME
982  *   & CTIME to now. (This is achieved by setting them as pending then forcing
983  *   an smb_node_setattr() to apply pending times.)
984  *
985  * - Note  If opening an existing file NOT for overwrite, windows would
986  *   set the atime on file close, however setting the atime would cause
987  *   the ARCHIVE attribute to be set, which does not occur on windows,
988  *   so we do not do the atime update.
989  *
990  * Returns: errno
991  */
992 static int
993 smb_set_open_timestamps(smb_request_t *sr, smb_ofile_t *of, boolean_t created)
994 {
995 	int		rc = 0;
996 	smb_arg_open_t	*op = &sr->sr_open;
997 	smb_node_t	*node = of->f_node;
998 	boolean_t	existing_file, set_times;
999 	smb_attr_t	attr;
1000 
1001 	bzero(&attr, sizeof (smb_attr_t));
1002 	set_times = B_FALSE;
1003 
1004 	if ((op->mtime.tv_sec != 0) && (op->mtime.tv_sec != UINT_MAX)) {
1005 		attr.sa_mask = SMB_AT_MTIME;
1006 		attr.sa_vattr.va_mtime = op->mtime;
1007 		set_times = B_TRUE;
1008 	}
1009 
1010 	existing_file = !(created || smb_node_is_dir(node));
1011 	if (existing_file) {
1012 		switch (op->create_disposition) {
1013 		case FILE_SUPERSEDE:
1014 		case FILE_OVERWRITE_IF:
1015 		case FILE_OVERWRITE:
1016 			smb_ofile_set_write_time_pending(of);
1017 			set_times = B_TRUE;
1018 			break;
1019 		default:
1020 			break;
1021 		}
1022 	}
1023 
1024 	if (set_times)
1025 		rc = smb_node_setattr(sr, node, sr->user_cr, of, &attr);
1026 
1027 	return (rc);
1028 }
1029 
1030 /*
1031  * This function is used to delete a newly created object (file or
1032  * directory) if an error occurs after creation of the object.
1033  */
1034 static void
1035 smb_delete_new_object(smb_request_t *sr)
1036 {
1037 	smb_arg_open_t	*op = &sr->sr_open;
1038 	smb_fqi_t	*fqi = &(op->fqi);
1039 	uint32_t	flags = 0;
1040 
1041 	if (SMB_TREE_IS_CASEINSENSITIVE(sr))
1042 		flags |= SMB_IGNORE_CASE;
1043 	if (SMB_TREE_SUPPORTS_CATIA(sr))
1044 		flags |= SMB_CATIA;
1045 
1046 	if (op->create_options & FILE_DIRECTORY_FILE)
1047 		(void) smb_fsop_rmdir(sr, sr->user_cr, fqi->fq_dnode,
1048 		    fqi->fq_last_comp, flags);
1049 	else
1050 		(void) smb_fsop_remove(sr, sr->user_cr, fqi->fq_dnode,
1051 		    fqi->fq_last_comp, flags);
1052 }
1053