xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_common_open.c (revision 2cf6b79f28bc1e3f5631e49f3995194cc93991d4)
1da6c28aaSamw /*
2da6c28aaSamw  * CDDL HEADER START
3da6c28aaSamw  *
4da6c28aaSamw  * The contents of this file are subject to the terms of the
5da6c28aaSamw  * Common Development and Distribution License (the "License").
6da6c28aaSamw  * You may not use this file except in compliance with the License.
7da6c28aaSamw  *
8da6c28aaSamw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9da6c28aaSamw  * or http://www.opensolaris.org/os/licensing.
10da6c28aaSamw  * See the License for the specific language governing permissions
11da6c28aaSamw  * and limitations under the License.
12da6c28aaSamw  *
13da6c28aaSamw  * When distributing Covered Code, include this CDDL HEADER in each
14da6c28aaSamw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15da6c28aaSamw  * If applicable, add the following below this CDDL HEADER, with the
16da6c28aaSamw  * fields enclosed by brackets "[]" replaced with your own identifying
17da6c28aaSamw  * information: Portions Copyright [yyyy] [name of copyright owner]
18da6c28aaSamw  *
19da6c28aaSamw  * CDDL HEADER END
20da6c28aaSamw  */
21148c5f43SAlan Wright 
22da6c28aaSamw /*
23148c5f43SAlan Wright  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24a8e9db1cSGordon Ross  * Copyright 2020 Tintri by DDN, Inc. All rights reserved.
250cab3dcdSGordon Ross  * Copyright 2022 RackTop Systems, Inc.
26da6c28aaSamw  */
27da6c28aaSamw 
28da6c28aaSamw /*
29da6c28aaSamw  * This module provides the common open functionality to the various
30da6c28aaSamw  * open and create SMB interface functions.
31da6c28aaSamw  */
32da6c28aaSamw 
33bbf6f00cSJordan Brown #include <sys/types.h>
34bbf6f00cSJordan Brown #include <sys/cmn_err.h>
35da6c28aaSamw #include <sys/fcntl.h>
36dc20a302Sas200622 #include <sys/nbmlock.h>
37bbf6f00cSJordan Brown #include <smbsrv/string.h>
38148d1a41SMatt Barden #include <smbsrv/smb2_kproto.h>
39bbf6f00cSJordan Brown #include <smbsrv/smb_fsops.h>
40bbf6f00cSJordan Brown #include <smbsrv/smbinfo.h>
41da6c28aaSamw 
42faa1795aSjb150015 extern uint32_t smb_is_executable(char *);
438b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States static void smb_delete_new_object(smb_request_t *);
445fd03bc0SGordon Ross static int smb_set_open_attributes(smb_request_t *, smb_ofile_t *);
45da6c28aaSamw 
46da6c28aaSamw /*
47da6c28aaSamw  * smb_access_generic_to_file
48da6c28aaSamw  *
49da6c28aaSamw  * Search MSDN for IoCreateFile to see following mapping.
50da6c28aaSamw  *
51da6c28aaSamw  * GENERIC_READ		STANDARD_RIGHTS_READ, FILE_READ_DATA,
52da6c28aaSamw  *			FILE_READ_ATTRIBUTES and FILE_READ_EA
53da6c28aaSamw  *
54da6c28aaSamw  * GENERIC_WRITE	STANDARD_RIGHTS_WRITE, FILE_WRITE_DATA,
55da6c28aaSamw  *               FILE_WRITE_ATTRIBUTES, FILE_WRITE_EA, and FILE_APPEND_DATA
56da6c28aaSamw  *
57da6c28aaSamw  * GENERIC_EXECUTE	STANDARD_RIGHTS_EXECUTE, SYNCHRONIZE, and FILE_EXECUTE.
58da6c28aaSamw  */
59a90cf9f2SGordon Ross static uint32_t
smb_access_generic_to_file(uint32_t desired_access)60da6c28aaSamw smb_access_generic_to_file(uint32_t desired_access)
61da6c28aaSamw {
62a90cf9f2SGordon Ross 	uint32_t access = 0;
63da6c28aaSamw 
64da6c28aaSamw 	if (desired_access & GENERIC_ALL)
65da6c28aaSamw 		return (FILE_ALL_ACCESS & ~SYNCHRONIZE);
66da6c28aaSamw 
67da6c28aaSamw 	if (desired_access & GENERIC_EXECUTE) {
68da6c28aaSamw 		desired_access &= ~GENERIC_EXECUTE;
69da6c28aaSamw 		access |= (STANDARD_RIGHTS_EXECUTE |
70da6c28aaSamw 		    SYNCHRONIZE | FILE_EXECUTE);
71da6c28aaSamw 	}
72da6c28aaSamw 
73da6c28aaSamw 	if (desired_access & GENERIC_WRITE) {
74da6c28aaSamw 		desired_access &= ~GENERIC_WRITE;
75da6c28aaSamw 		access |= (FILE_GENERIC_WRITE & ~SYNCHRONIZE);
76da6c28aaSamw 	}
77da6c28aaSamw 
78da6c28aaSamw 	if (desired_access & GENERIC_READ) {
79da6c28aaSamw 		desired_access &= ~GENERIC_READ;
80da6c28aaSamw 		access |= FILE_GENERIC_READ;
81da6c28aaSamw 	}
82da6c28aaSamw 
83da6c28aaSamw 	return (access | desired_access);
84da6c28aaSamw }
85da6c28aaSamw 
86da6c28aaSamw /*
87da6c28aaSamw  * smb_omode_to_amask
88da6c28aaSamw  *
89da6c28aaSamw  * This function converts open modes used by Open and Open AndX
90da6c28aaSamw  * commands to desired access bits used by NT Create AndX command.
91da6c28aaSamw  */
92da6c28aaSamw uint32_t
smb_omode_to_amask(uint32_t desired_access)93da6c28aaSamw smb_omode_to_amask(uint32_t desired_access)
94da6c28aaSamw {
95da6c28aaSamw 	switch (desired_access & SMB_DA_ACCESS_MASK) {
96da6c28aaSamw 	case SMB_DA_ACCESS_READ:
97da6c28aaSamw 		return (FILE_GENERIC_READ);
98da6c28aaSamw 
99da6c28aaSamw 	case SMB_DA_ACCESS_WRITE:
100da6c28aaSamw 		return (FILE_GENERIC_WRITE);
101da6c28aaSamw 
102da6c28aaSamw 	case SMB_DA_ACCESS_READ_WRITE:
103da6c28aaSamw 		return (FILE_GENERIC_READ | FILE_GENERIC_WRITE);
104da6c28aaSamw 
105da6c28aaSamw 	case SMB_DA_ACCESS_EXECUTE:
106c5f48fa5SGordon Ross 		return (FILE_GENERIC_READ | FILE_GENERIC_EXECUTE);
107da6c28aaSamw 
1082c2961f8Sjose borrego 	default:
1092c2961f8Sjose borrego 		return (FILE_GENERIC_ALL);
1102c2961f8Sjose borrego 	}
111da6c28aaSamw }
112da6c28aaSamw 
113da6c28aaSamw /*
114da6c28aaSamw  * smb_denymode_to_sharemode
115da6c28aaSamw  *
116da6c28aaSamw  * This function converts deny modes used by Open and Open AndX
117da6c28aaSamw  * commands to share access bits used by NT Create AndX command.
118da6c28aaSamw  */
119da6c28aaSamw uint32_t
smb_denymode_to_sharemode(uint32_t desired_access,char * fname)120da6c28aaSamw smb_denymode_to_sharemode(uint32_t desired_access, char *fname)
121da6c28aaSamw {
122da6c28aaSamw 	switch (desired_access & SMB_DA_SHARE_MASK) {
123da6c28aaSamw 	case SMB_DA_SHARE_COMPATIBILITY:
124da6c28aaSamw 		if (smb_is_executable(fname))
125da6c28aaSamw 			return (FILE_SHARE_READ | FILE_SHARE_WRITE);
126c8ec8eeaSjose borrego 
127c8ec8eeaSjose borrego 		return (FILE_SHARE_ALL);
128da6c28aaSamw 
129da6c28aaSamw 	case SMB_DA_SHARE_EXCLUSIVE:
130da6c28aaSamw 		return (FILE_SHARE_NONE);
131da6c28aaSamw 
132da6c28aaSamw 	case SMB_DA_SHARE_DENY_WRITE:
133da6c28aaSamw 		return (FILE_SHARE_READ);
134da6c28aaSamw 
135da6c28aaSamw 	case SMB_DA_SHARE_DENY_READ:
136da6c28aaSamw 		return (FILE_SHARE_WRITE);
137da6c28aaSamw 
138da6c28aaSamw 	case SMB_DA_SHARE_DENY_NONE:
1392c2961f8Sjose borrego 	default:
140da6c28aaSamw 		return (FILE_SHARE_READ | FILE_SHARE_WRITE);
141da6c28aaSamw 	}
142da6c28aaSamw }
143da6c28aaSamw 
144da6c28aaSamw /*
145da6c28aaSamw  * smb_ofun_to_crdisposition
146da6c28aaSamw  *
147da6c28aaSamw  * This function converts open function values used by Open and Open AndX
148da6c28aaSamw  * commands to create disposition values used by NT Create AndX command.
149da6c28aaSamw  */
150da6c28aaSamw uint32_t
smb_ofun_to_crdisposition(uint16_t ofun)151da6c28aaSamw smb_ofun_to_crdisposition(uint16_t  ofun)
152da6c28aaSamw {
153da6c28aaSamw 	static int ofun_cr_map[3][2] =
154da6c28aaSamw 	{
155da6c28aaSamw 		{ -1,			FILE_CREATE },
156da6c28aaSamw 		{ FILE_OPEN,		FILE_OPEN_IF },
157da6c28aaSamw 		{ FILE_OVERWRITE,	FILE_OVERWRITE_IF }
158da6c28aaSamw 	};
159da6c28aaSamw 
160da6c28aaSamw 	int row = ofun & SMB_OFUN_OPEN_MASK;
161da6c28aaSamw 	int col = (ofun & SMB_OFUN_CREATE_MASK) >> 4;
162da6c28aaSamw 
163da6c28aaSamw 	if (row == 3)
1642c2961f8Sjose borrego 		return (FILE_MAXIMUM_DISPOSITION + 1);
165da6c28aaSamw 
166da6c28aaSamw 	return (ofun_cr_map[row][col]);
167da6c28aaSamw }
168da6c28aaSamw 
169da6c28aaSamw /*
17094047d49SGordon Ross  * smb_common_open
171da6c28aaSamw  *
172da6c28aaSamw  * Notes on write-through behaviour. It looks like pre-LM0.12 versions
173da6c28aaSamw  * of the protocol specify the write-through mode when a file is opened,
174da6c28aaSamw  * (SmbOpen, SmbOpenAndX) so the write calls (SmbWrite, SmbWriteAndClose,
175da6c28aaSamw  * SmbWriteAndUnlock) don't need to contain a write-through flag.
176da6c28aaSamw  *
177da6c28aaSamw  * With LM0.12, the open calls (SmbCreateAndX, SmbNtTransactCreate)
178da6c28aaSamw  * don't indicate which write-through mode to use. Instead the write
179da6c28aaSamw  * calls (SmbWriteAndX, SmbWriteRaw) specify the mode on a per call
180da6c28aaSamw  * basis.
181da6c28aaSamw  *
182da6c28aaSamw  * We don't care which open call was used to get us here, we just need
183da6c28aaSamw  * to ensure that the write-through mode flag is copied from the open
184da6c28aaSamw  * parameters to the node. We test the omode write-through flag in all
185da6c28aaSamw  * write functions.
186da6c28aaSamw  *
187a90cf9f2SGordon Ross  * This function returns NT status codes.
1888c10a865Sas200622  *
1898c10a865Sas200622  * The following rules apply when processing a file open request:
1908c10a865Sas200622  *
191cb174861Sjoyce mcintosh  * - Oplocks must be broken prior to share checking as the break may
192cb174861Sjoyce mcintosh  *   cause other clients to close the file, which would affect sharing
193cb174861Sjoyce mcintosh  *   checks.
1948c10a865Sas200622  *
1958c10a865Sas200622  * - Share checks must take place prior to access checks for correct
1968c10a865Sas200622  * Windows semantics and to prevent unnecessary NFS delegation recalls.
1978c10a865Sas200622  *
1988c10a865Sas200622  * - Oplocks must be acquired after open to ensure the correct
1998c10a865Sas200622  * synchronization with NFS delegation and FEM installation.
200c8ec8eeaSjose borrego  *
201c8ec8eeaSjose borrego  * DOS readonly bit rules
202c8ec8eeaSjose borrego  *
203c8ec8eeaSjose borrego  * 1. The creator of a readonly file can write to/modify the size of the file
204c8ec8eeaSjose borrego  * using the original create fid, even though the file will appear as readonly
205c8ec8eeaSjose borrego  * to all other fids and via a CIFS getattr call.
206c8ec8eeaSjose borrego  *
207c8ec8eeaSjose borrego  * 2. A setinfo operation (using either an open fid or a path) to set/unset
208c8ec8eeaSjose borrego  * readonly will be successful regardless of whether a creator of a readonly
2095cb2894aSGordon Ross  * file has an open fid.
210c8ec8eeaSjose borrego  *
211c8ec8eeaSjose borrego  * 3. The DOS readonly bit affects only data and some metadata.
212c8ec8eeaSjose borrego  * The following metadata can be changed regardless of the readonly bit:
213c8ec8eeaSjose borrego  *	- security descriptors
214c8ec8eeaSjose borrego  *	- DOS attributes
215c8ec8eeaSjose borrego  *	- timestamps
216c8ec8eeaSjose borrego  *
217c8ec8eeaSjose borrego  * In the current implementation, the file size cannot be changed (except for
218c8ec8eeaSjose borrego  * the exceptions in #1 and #2, above).
2192c1b14e5Sjose borrego  *
2202c1b14e5Sjose borrego  *
2212c1b14e5Sjose borrego  * DOS attribute rules
2222c1b14e5Sjose borrego  *
2232c1b14e5Sjose borrego  * These rules are specific to creating / opening files and directories.
2242c1b14e5Sjose borrego  * How the attribute value (specifically ZERO or FILE_ATTRIBUTE_NORMAL)
2252c1b14e5Sjose borrego  * should be interpreted may differ in other requests.
2262c1b14e5Sjose borrego  *
2272c1b14e5Sjose borrego  * - An attribute value equal to ZERO or FILE_ATTRIBUTE_NORMAL means that the
2282c1b14e5Sjose borrego  *   file's attributes should be cleared.
2292c1b14e5Sjose borrego  * - If FILE_ATTRIBUTE_NORMAL is specified with any other attributes,
2302c1b14e5Sjose borrego  *   FILE_ATTRIBUTE_NORMAL is ignored.
2312c1b14e5Sjose borrego  *
2322c1b14e5Sjose borrego  * 1. Creating a new file
2332c1b14e5Sjose borrego  * - The request attributes + FILE_ATTRIBUTE_ARCHIVE are applied to the file.
2342c1b14e5Sjose borrego  *
2352c1b14e5Sjose borrego  * 2. Creating a new directory
2362c1b14e5Sjose borrego  * - The request attributes + FILE_ATTRIBUTE_DIRECTORY are applied to the file.
2372c1b14e5Sjose borrego  * - FILE_ATTRIBUTE_ARCHIVE does not get set.
2382c1b14e5Sjose borrego  *
2392c1b14e5Sjose borrego  * 3. Overwriting an existing file
2402c1b14e5Sjose borrego  * - the request attributes are used as search attributes. If the existing
2412c1b14e5Sjose borrego  *   file does not meet the search criteria access is denied.
2422c1b14e5Sjose borrego  * - otherwise, applies attributes + FILE_ATTRIBUTE_ARCHIVE.
2432c1b14e5Sjose borrego  *
2442c1b14e5Sjose borrego  * 4. Opening an existing file or directory
2452c1b14e5Sjose borrego  *    The request attributes are ignored.
246da6c28aaSamw  */
24794047d49SGordon Ross uint32_t
smb_common_open(smb_request_t * sr)24894047d49SGordon Ross smb_common_open(smb_request_t *sr)
249da6c28aaSamw {
25094047d49SGordon Ross 	smb_server_t	*sv = sr->sr_server;
25194047d49SGordon Ross 	smb_tree_t	*tree = sr->tid_tree;
25294047d49SGordon Ross 	smb_node_t	*fnode = NULL;
2532c2961f8Sjose borrego 	smb_node_t	*dnode = NULL;
254eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	smb_node_t	*cur_node = NULL;
2550a73e6f9SMatt Barden 	smb_node_t	*tmp_node = NULL;
256148c5f43SAlan Wright 	smb_arg_open_t	*op = &sr->sr_open;
25794047d49SGordon Ross 	smb_pathname_t	*pn = &op->fqi.fq_path;
25894047d49SGordon Ross 	smb_ofile_t	*of = NULL;
259da6c28aaSamw 	smb_attr_t	new_attr;
26094047d49SGordon Ross 	hrtime_t	shrlock_t0;
261da6c28aaSamw 	int		max_requested = 0;
262da6c28aaSamw 	uint32_t	max_allowed;
263da6c28aaSamw 	uint32_t	status = NT_STATUS_SUCCESS;
264da6c28aaSamw 	int		is_dir;
26594047d49SGordon Ross 	int		rc;
2662c2961f8Sjose borrego 	boolean_t	is_stream = B_FALSE;
267da6c28aaSamw 	int		lookup_flags = SMB_FOLLOW_LINKS;
26894047d49SGordon Ross 	uint32_t	uniq_fid = 0;
26994047d49SGordon Ross 	uint16_t	tree_fid = 0;
27094047d49SGordon Ross 	boolean_t	created = B_FALSE;
27194047d49SGordon Ross 	boolean_t	last_comp_found = B_FALSE;
2720a73e6f9SMatt Barden 	boolean_t	stream_found = B_FALSE;
27394047d49SGordon Ross 	boolean_t	opening_incr = B_FALSE;
27494047d49SGordon Ross 	boolean_t	dnode_held = B_FALSE;
27594047d49SGordon Ross 	boolean_t	dnode_wlock = B_FALSE;
27694047d49SGordon Ross 	boolean_t	fnode_held = B_FALSE;
27794047d49SGordon Ross 	boolean_t	fnode_wlock = B_FALSE;
27894047d49SGordon Ross 	boolean_t	fnode_shrlk = B_FALSE;
27994047d49SGordon Ross 	boolean_t	did_open = B_FALSE;
28094047d49SGordon Ross 	boolean_t	did_break_handle = B_FALSE;
2818d94f651SGordon Ross 	boolean_t	did_cleanup_orphans = B_FALSE;
2820a73e6f9SMatt Barden 	char		*sname = NULL;
283da6c28aaSamw 
2845677e049SGordon Ross 	/* Get out now if we've been cancelled. */
2855677e049SGordon Ross 	mutex_enter(&sr->sr_mutex);
2865677e049SGordon Ross 	if (sr->sr_state != SMB_REQ_STATE_ACTIVE) {
2875677e049SGordon Ross 		mutex_exit(&sr->sr_mutex);
2885677e049SGordon Ross 		return (NT_STATUS_CANCELLED);
2895677e049SGordon Ross 	}
2905677e049SGordon Ross 	mutex_exit(&sr->sr_mutex);
2915677e049SGordon Ross 
292da6c28aaSamw 	is_dir = (op->create_options & FILE_DIRECTORY_FILE) ? 1 : 0;
293da6c28aaSamw 
294da6c28aaSamw 	/*
295037cac00Sjoyce mcintosh 	 * If the object being created or opened is a directory
296037cac00Sjoyce mcintosh 	 * the Disposition parameter must be one of FILE_CREATE,
297037cac00Sjoyce mcintosh 	 * FILE_OPEN, or FILE_OPEN_IF
298da6c28aaSamw 	 */
299037cac00Sjoyce mcintosh 	if (is_dir) {
300da6c28aaSamw 		if ((op->create_disposition != FILE_CREATE) &&
301da6c28aaSamw 		    (op->create_disposition != FILE_OPEN_IF) &&
302da6c28aaSamw 		    (op->create_disposition != FILE_OPEN)) {
3037b59d02dSjb150015 			return (NT_STATUS_INVALID_PARAMETER);
304da6c28aaSamw 		}
305da6c28aaSamw 	}
306da6c28aaSamw 
307da6c28aaSamw 	if (op->desired_access & MAXIMUM_ALLOWED) {
308da6c28aaSamw 		max_requested = 1;
309da6c28aaSamw 		op->desired_access &= ~MAXIMUM_ALLOWED;
310da6c28aaSamw 	}
311da6c28aaSamw 	op->desired_access = smb_access_generic_to_file(op->desired_access);
312da6c28aaSamw 
313*2cf6b79fSGordon Ross 	if (sr->session->s_cfg.skc_max_opens != 0 &&
314*2cf6b79fSGordon Ross 	    sr->session->s_file_cnt >= sr->session->s_cfg.skc_max_opens) {
315da6c28aaSamw 		ASSERT(sr->uid_user);
316cb174861Sjoyce mcintosh 		cmn_err(CE_NOTE, "smbsrv[%s\\%s]: TOO_MANY_OPENED_FILES",
317148c5f43SAlan Wright 		    sr->uid_user->u_domain, sr->uid_user->u_name);
3187b59d02dSjb150015 		return (NT_STATUS_TOO_MANY_OPENED_FILES);
319da6c28aaSamw 	}
320da6c28aaSamw 
32194047d49SGordon Ross 	if (smb_idpool_alloc(&tree->t_fid_pool, &tree_fid))
32294047d49SGordon Ross 		return (NT_STATUS_TOO_MANY_OPENED_FILES);
32394047d49SGordon Ross 
324da6c28aaSamw 	/* This must be NULL at this point */
325da6c28aaSamw 	sr->fid_ofile = NULL;
326da6c28aaSamw 
327da6c28aaSamw 	op->devstate = 0;
328da6c28aaSamw 
329da6c28aaSamw 	switch (sr->tid_tree->t_res_type & STYPE_MASK) {
330da6c28aaSamw 	case STYPE_DISKTREE:
331f96bd5c8SAlan Wright 	case STYPE_PRINTQ:
332da6c28aaSamw 		break;
333da6c28aaSamw 
334da6c28aaSamw 	case STYPE_IPC:
335a90cf9f2SGordon Ross 		/*
336a90cf9f2SGordon Ross 		 * Security descriptors for pipes are not implemented,
337a90cf9f2SGordon Ross 		 * so just setup a reasonable access mask.
338a90cf9f2SGordon Ross 		 */
339a90cf9f2SGordon Ross 		op->desired_access = (READ_CONTROL | SYNCHRONIZE |
340a90cf9f2SGordon Ross 		    FILE_READ_DATA | FILE_READ_ATTRIBUTES |
341a90cf9f2SGordon Ross 		    FILE_WRITE_DATA | FILE_APPEND_DATA);
342cb174861Sjoyce mcintosh 
343a90cf9f2SGordon Ross 		/*
344a90cf9f2SGordon Ross 		 * Limit the number of open pipe instances.
345a90cf9f2SGordon Ross 		 */
346cb174861Sjoyce mcintosh 		if ((rc = smb_threshold_enter(&sv->sv_opipe_ct)) != 0) {
347cb174861Sjoyce mcintosh 			status = RPC_NT_SERVER_TOO_BUSY;
34894047d49SGordon Ross 			goto errout;
349cb174861Sjoyce mcintosh 		}
350cb174861Sjoyce mcintosh 
351da6c28aaSamw 		/*
35294047d49SGordon Ross 		 * Most of IPC open is handled in smb_opipe_open()
353da6c28aaSamw 		 */
35494047d49SGordon Ross 		op->create_options = 0;
35594047d49SGordon Ross 		of = smb_ofile_alloc(sr, op, NULL, SMB_FTYPE_MESG_PIPE,
3568d94f651SGordon Ross 		    tree_fid);
35794047d49SGordon Ross 		tree_fid = 0; // given to the ofile
35894047d49SGordon Ross 		status = smb_opipe_open(sr, of);
359856399cfSGordon Ross 		smb_threshold_exit(&sv->sv_opipe_ct);
36094047d49SGordon Ross 		if (status != NT_STATUS_SUCCESS)
36194047d49SGordon Ross 			goto errout;
36294047d49SGordon Ross 		return (NT_STATUS_SUCCESS);
363da6c28aaSamw 
364da6c28aaSamw 	default:
36594047d49SGordon Ross 		status = NT_STATUS_BAD_DEVICE_TYPE;
36694047d49SGordon Ross 		goto errout;
367da6c28aaSamw 	}
368da6c28aaSamw 
369fe1c642dSBill Krier 	smb_pathname_init(sr, pn, pn->pn_path);
37094047d49SGordon Ross 	if (!smb_pathname_validate(sr, pn)) {
37194047d49SGordon Ross 		status = sr->smb_error.status;
37294047d49SGordon Ross 		goto errout;
37394047d49SGordon Ross 	}
374fe1c642dSBill Krier 
375b24e356bSPeer Dampmann 	if (strlen(pn->pn_path) >= SMB_MAXPATHLEN) {
37694047d49SGordon Ross 		status = NT_STATUS_OBJECT_PATH_INVALID;
37794047d49SGordon Ross 		goto errout;
378da6c28aaSamw 	}
379da6c28aaSamw 
380fe1c642dSBill Krier 	if (is_dir) {
38194047d49SGordon Ross 		if (!smb_validate_dirname(sr, pn)) {
38294047d49SGordon Ross 			status = sr->smb_error.status;
38394047d49SGordon Ross 			goto errout;
38494047d49SGordon Ross 		}
385fe1c642dSBill Krier 	} else {
38694047d49SGordon Ross 		if (!smb_validate_object_name(sr, pn)) {
38794047d49SGordon Ross 			status = sr->smb_error.status;
38894047d49SGordon Ross 			goto errout;
38994047d49SGordon Ross 		}
390da6c28aaSamw 	}
391da6c28aaSamw 
392eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	cur_node = op->fqi.fq_dnode ?
393eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	    op->fqi.fq_dnode : sr->tid_tree->t_snode;
394da6c28aaSamw 
395b819cea2SGordon Ross 	rc = smb_pathname_reduce(sr, sr->user_cr, pn->pn_path,
396eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	    sr->tid_tree->t_snode, cur_node, &op->fqi.fq_dnode,
397b819cea2SGordon Ross 	    op->fqi.fq_last_comp);
398b819cea2SGordon Ross 	if (rc != 0) {
39994047d49SGordon Ross 		status = smb_errno2status(rc);
40094047d49SGordon Ross 		goto errout;
401da6c28aaSamw 	}
40294047d49SGordon Ross 	dnode = op->fqi.fq_dnode;
40394047d49SGordon Ross 	dnode_held = B_TRUE;
40494047d49SGordon Ross 
40594047d49SGordon Ross 	/*
40694047d49SGordon Ross 	 * Lock the parent dir node in case another create
40794047d49SGordon Ross 	 * request to the same parent directory comes in.
40894047d49SGordon Ross 	 * Drop this once either lookup succeeds, or we've
40994047d49SGordon Ross 	 * created the object in this directory.
41094047d49SGordon Ross 	 */
41194047d49SGordon Ross 	smb_node_wrlock(dnode);
41294047d49SGordon Ross 	dnode_wlock = B_TRUE;
413da6c28aaSamw 
414da6c28aaSamw 	/*
415da6c28aaSamw 	 * If the access mask has only DELETE set (ignore
416da6c28aaSamw 	 * FILE_READ_ATTRIBUTES), then assume that this
417da6c28aaSamw 	 * is a request to delete the link (if a link)
418da6c28aaSamw 	 * and do not follow links.  Otherwise, follow
419da6c28aaSamw 	 * the link to the target.
420da6c28aaSamw 	 */
421037cac00Sjoyce mcintosh 	if ((op->desired_access & ~FILE_READ_ATTRIBUTES) == DELETE)
422da6c28aaSamw 		lookup_flags &= ~SMB_FOLLOW_LINKS;
423da6c28aaSamw 
4240a73e6f9SMatt Barden 	/*
4250a73e6f9SMatt Barden 	 * Lookup *just* the file portion of the name.
4260a73e6f9SMatt Barden 	 * Returns stream name in sname, which this allocates
4270a73e6f9SMatt Barden 	 */
4280a73e6f9SMatt Barden 	rc = smb_fsop_lookup_file(sr, zone_kcred(), lookup_flags,
429eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	    sr->tid_tree->t_snode, op->fqi.fq_dnode, op->fqi.fq_last_comp,
4300a73e6f9SMatt Barden 	    &sname, &op->fqi.fq_fnode);
431da6c28aaSamw 
432da6c28aaSamw 	if (rc == 0) {
433eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		last_comp_found = B_TRUE;
43448e2dbe9SGordon Ross 		fnode = op->fqi.fq_fnode;
43594047d49SGordon Ross 		fnode_held = B_TRUE;
43694047d49SGordon Ross 
4375fd03bc0SGordon Ross 		/*
4385fd03bc0SGordon Ross 		 * Need the DOS attributes below, where we
4395fd03bc0SGordon Ross 		 * check the search attributes (sattr).
44094047d49SGordon Ross 		 * Also UID, for owner check below.
4415fd03bc0SGordon Ross 		 */
44294047d49SGordon Ross 		op->fqi.fq_fattr.sa_mask = SMB_AT_DOSATTR | SMB_AT_UID;
4438622ec45SGordon Ross 		rc = smb_node_getattr(sr, op->fqi.fq_fnode, zone_kcred(),
4445fd03bc0SGordon Ross 		    NULL, &op->fqi.fq_fattr);
445037cac00Sjoyce mcintosh 		if (rc != 0) {
44694047d49SGordon Ross 			status = NT_STATUS_INTERNAL_ERROR;
44794047d49SGordon Ross 			goto errout;
448037cac00Sjoyce mcintosh 		}
449da6c28aaSamw 	} else if (rc == ENOENT) {
450eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		last_comp_found = B_FALSE;
451eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		op->fqi.fq_fnode = NULL;
452da6c28aaSamw 		rc = 0;
453da6c28aaSamw 	} else {
45494047d49SGordon Ross 		status = smb_errno2status(rc);
45594047d49SGordon Ross 		goto errout;
456da6c28aaSamw 	}
457da6c28aaSamw 
458eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (last_comp_found) {
4596537f381Sas200622 
46094047d49SGordon Ross 		fnode = op->fqi.fq_fnode;
4619fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		dnode = op->fqi.fq_dnode;
4626537f381Sas200622 
46394047d49SGordon Ross 		if (!smb_node_is_file(fnode) &&
46494047d49SGordon Ross 		    !smb_node_is_dir(fnode) &&
46594047d49SGordon Ross 		    !smb_node_is_symlink(fnode)) {
46694047d49SGordon Ross 			status = NT_STATUS_ACCESS_DENIED;
46794047d49SGordon Ross 			goto errout;
4686537f381Sas200622 		}
4696537f381Sas200622 
470da6c28aaSamw 		/*
4712c1b14e5Sjose borrego 		 * Reject this request if either:
4722c1b14e5Sjose borrego 		 * - the target IS a directory and the client requires that
4732c1b14e5Sjose borrego 		 *   it must NOT be (required by Lotus Notes)
4742c1b14e5Sjose borrego 		 * - the target is NOT a directory and client requires that
4752c1b14e5Sjose borrego 		 *   it MUST be.
4760a73e6f9SMatt Barden 		 * Streams are never directories.
477da6c28aaSamw 		 */
4780a73e6f9SMatt Barden 		if (smb_node_is_dir(fnode) && sname == NULL) {
4792c1b14e5Sjose borrego 			if (op->create_options & FILE_NON_DIRECTORY_FILE) {
48094047d49SGordon Ross 				status = NT_STATUS_FILE_IS_A_DIRECTORY;
48194047d49SGordon Ross 				goto errout;
482da6c28aaSamw 			}
4832c1b14e5Sjose borrego 		} else {
4842c1b14e5Sjose borrego 			if ((op->create_options & FILE_DIRECTORY_FILE) ||
4852c2961f8Sjose borrego 			    (op->nt_flags & NT_CREATE_FLAG_OPEN_TARGET_DIR)) {
48694047d49SGordon Ross 				status = NT_STATUS_NOT_A_DIRECTORY;
48794047d49SGordon Ross 				goto errout;
488da6c28aaSamw 			}
4892c1b14e5Sjose borrego 		}
490da6c28aaSamw 
4910a73e6f9SMatt Barden 		/* If we're given a stream name, look it up now */
4920a73e6f9SMatt Barden 		if (sname != NULL) {
4930a73e6f9SMatt Barden 			tmp_node = fnode;
4940a73e6f9SMatt Barden 			rc = smb_fsop_lookup_stream(sr, zone_kcred(),
4950a73e6f9SMatt Barden 			    lookup_flags, sr->tid_tree->t_snode, fnode, sname,
4960a73e6f9SMatt Barden 			    &fnode);
4970a73e6f9SMatt Barden 		} else {
4980a73e6f9SMatt Barden 			rc = 0;
4990a73e6f9SMatt Barden 		}
5000a73e6f9SMatt Barden 
5010a73e6f9SMatt Barden 		if (rc == 0) { /* Stream Exists (including unnamed stream) */
5020a73e6f9SMatt Barden 			stream_found = B_TRUE;
5030a73e6f9SMatt Barden 			smb_node_unlock(dnode);
5040a73e6f9SMatt Barden 			dnode_wlock = B_FALSE;
5050a73e6f9SMatt Barden 
5060a73e6f9SMatt Barden 			if (tmp_node != NULL)
5070a73e6f9SMatt Barden 				smb_node_release(tmp_node);
5080a73e6f9SMatt Barden 
509da6c28aaSamw 			/*
5100a73e6f9SMatt Barden 			 * No more open should be accepted when
5110a73e6f9SMatt Barden 			 * "Delete on close" flag is set.
512da6c28aaSamw 			 */
51394047d49SGordon Ross 			if (fnode->flags & NODE_FLAGS_DELETE_ON_CLOSE) {
51494047d49SGordon Ross 				status = NT_STATUS_DELETE_PENDING;
51594047d49SGordon Ross 				goto errout;
516da6c28aaSamw 			}
517da6c28aaSamw 
518da6c28aaSamw 			/*
5190a73e6f9SMatt Barden 			 * Specified file already exists
5200a73e6f9SMatt Barden 			 * so the operation should fail.
521da6c28aaSamw 			 */
522da6c28aaSamw 			if (op->create_disposition == FILE_CREATE) {
52394047d49SGordon Ross 				status = NT_STATUS_OBJECT_NAME_COLLISION;
52494047d49SGordon Ross 				goto errout;
525da6c28aaSamw 			}
526da6c28aaSamw 
5270a73e6f9SMatt Barden 			if ((op->create_disposition == FILE_SUPERSEDE) ||
5280a73e6f9SMatt Barden 			    (op->create_disposition == FILE_OVERWRITE_IF) ||
5290a73e6f9SMatt Barden 			    (op->create_disposition == FILE_OVERWRITE)) {
5300a73e6f9SMatt Barden 
5310a73e6f9SMatt Barden 				if (sname == NULL) {
5320a73e6f9SMatt Barden 					if (!smb_sattr_check(
5330a73e6f9SMatt Barden 					    op->fqi.fq_fattr.sa_dosattr,
5340a73e6f9SMatt Barden 					    op->dattr)) {
5350a73e6f9SMatt Barden 						status =
5360a73e6f9SMatt Barden 						    NT_STATUS_ACCESS_DENIED;
5370a73e6f9SMatt Barden 						goto errout;
5380a73e6f9SMatt Barden 					}
5390a73e6f9SMatt Barden 					op->desired_access |=
5400a73e6f9SMatt Barden 					    FILE_WRITE_ATTRIBUTES;
5410a73e6f9SMatt Barden 				}
5420a73e6f9SMatt Barden 
5430a73e6f9SMatt Barden 				if (smb_node_is_dir(fnode)) {
5440a73e6f9SMatt Barden 					status = NT_STATUS_ACCESS_DENIED;
5450a73e6f9SMatt Barden 					goto errout;
5460a73e6f9SMatt Barden 				}
5470a73e6f9SMatt Barden 			}
5480a73e6f9SMatt Barden 
5490a73e6f9SMatt Barden 			/* MS-FSA 2.1.5.1.2 */
5500a73e6f9SMatt Barden 			if (op->create_disposition == FILE_SUPERSEDE)
5510a73e6f9SMatt Barden 				op->desired_access |= DELETE;
5520a73e6f9SMatt Barden 			if ((op->create_disposition == FILE_OVERWRITE_IF) ||
5530a73e6f9SMatt Barden 			    (op->create_disposition == FILE_OVERWRITE))
5540a73e6f9SMatt Barden 				op->desired_access |= FILE_WRITE_DATA;
5550a73e6f9SMatt Barden 		} else if (rc == ENOENT) { /* File Exists, but Stream doesn't */
5560a73e6f9SMatt Barden 			if (op->create_disposition == FILE_OPEN ||
5570a73e6f9SMatt Barden 			    op->create_disposition == FILE_OVERWRITE) {
5580a73e6f9SMatt Barden 				status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
5590a73e6f9SMatt Barden 				goto errout;
5600a73e6f9SMatt Barden 			}
5610a73e6f9SMatt Barden 
5620a73e6f9SMatt Barden 			op->desired_access |= FILE_WRITE_DATA;
5630a73e6f9SMatt Barden 		} else { /* Error looking up stream */
5640a73e6f9SMatt Barden 			status = smb_errno2status(rc);
5650a73e6f9SMatt Barden 			fnode = tmp_node;
5660a73e6f9SMatt Barden 			goto errout;
5670a73e6f9SMatt Barden 		}
5680a73e6f9SMatt Barden 
569da6c28aaSamw 		/*
570da6c28aaSamw 		 * Windows seems to check read-only access before file
571da6c28aaSamw 		 * sharing check.
572c8ec8eeaSjose borrego 		 *
57394047d49SGordon Ross 		 * Check to see if the file is currently readonly (regardless
574c8ec8eeaSjose borrego 		 * of whether this open will make it readonly).
57594047d49SGordon Ross 		 * Readonly is ignored on directories.
576da6c28aaSamw 		 */
57794047d49SGordon Ross 		if (SMB_PATHFILE_IS_READONLY(sr, fnode) &&
57894047d49SGordon Ross 		    !smb_node_is_dir(fnode)) {
57994047d49SGordon Ross 			if (op->desired_access &
58094047d49SGordon Ross 			    (FILE_WRITE_DATA | FILE_APPEND_DATA)) {
58194047d49SGordon Ross 				status = NT_STATUS_ACCESS_DENIED;
58294047d49SGordon Ross 				goto errout;
583da6c28aaSamw 			}
584b5b772b0SGordon Ross 			if (op->create_options & FILE_DELETE_ON_CLOSE) {
58594047d49SGordon Ross 				status = NT_STATUS_CANNOT_DELETE;
58694047d49SGordon Ross 				goto errout;
587da6c28aaSamw 			}
588da6c28aaSamw 		}
589da6c28aaSamw 
59006721c88SMatt Barden 		/* Dataset roots can't be deleted, so don't set DOC */
59106721c88SMatt Barden 		if ((op->create_options & FILE_DELETE_ON_CLOSE) != 0 &&
59206721c88SMatt Barden 		    (fnode->flags & NODE_FLAGS_VFSROOT) != 0) {
59306721c88SMatt Barden 			status = NT_STATUS_CANNOT_DELETE;
59406721c88SMatt Barden 			goto errout;
59506721c88SMatt Barden 		}
59606721c88SMatt Barden 
59794047d49SGordon Ross 		status = smb_fsop_access(sr, sr->user_cr, fnode,
598da6c28aaSamw 		    op->desired_access);
5990a73e6f9SMatt Barden 
60094047d49SGordon Ross 		if (status != NT_STATUS_SUCCESS)
60194047d49SGordon Ross 			goto errout;
602da6c28aaSamw 
603fb699f1eSAlek Pinchuk 		if (max_requested) {
60494047d49SGordon Ross 			smb_fsop_eaccess(sr, sr->user_cr, fnode, &max_allowed);
605fb699f1eSAlek Pinchuk 			op->desired_access |= max_allowed;
606fb699f1eSAlek Pinchuk 		}
60794047d49SGordon Ross 
60894047d49SGordon Ross 		/*
60994047d49SGordon Ross 		 * File owner should always get read control + read attr.
61094047d49SGordon Ross 		 */
61194047d49SGordon Ross 		if (crgetuid(sr->user_cr) == op->fqi.fq_fattr.sa_vattr.va_uid)
61294047d49SGordon Ross 			op->desired_access |=
61394047d49SGordon Ross 			    (READ_CONTROL | FILE_READ_ATTRIBUTES);
61494047d49SGordon Ross 
615a90cf9f2SGordon Ross 		/*
616a90cf9f2SGordon Ross 		 * According to MS "dochelp" mail in Mar 2015, any handle
617a90cf9f2SGordon Ross 		 * on which read or write access is granted implicitly
618a90cf9f2SGordon Ross 		 * gets "read attributes", even if it was not requested.
619a90cf9f2SGordon Ross 		 */
62094047d49SGordon Ross 		if ((op->desired_access & FILE_DATA_ALL) != 0)
62194047d49SGordon Ross 			op->desired_access |= FILE_READ_ATTRIBUTES;
622fb699f1eSAlek Pinchuk 
6230a73e6f9SMatt Barden 		/* If the stream didn't exist, create it now */
6240a73e6f9SMatt Barden 		if (!stream_found) {
6250a73e6f9SMatt Barden 			smb_node_t *tmp_node = fnode;
6260a73e6f9SMatt Barden 
6270a73e6f9SMatt Barden 			bzero(&new_attr, sizeof (new_attr));
6280a73e6f9SMatt Barden 			new_attr.sa_vattr.va_type = VREG;
6290a73e6f9SMatt Barden 			new_attr.sa_vattr.va_mode = S_IRUSR;
6300a73e6f9SMatt Barden 			new_attr.sa_mask |= SMB_AT_TYPE | SMB_AT_MODE;
6310a73e6f9SMatt Barden 
6320a73e6f9SMatt Barden 			rc = smb_fsop_create_stream(sr, sr->user_cr, dnode,
6330a73e6f9SMatt Barden 			    fnode, sname, lookup_flags, &new_attr, &fnode);
6340a73e6f9SMatt Barden 			smb_node_release(tmp_node);
6350a73e6f9SMatt Barden 
6360a73e6f9SMatt Barden 			if (rc != 0) {
6370a73e6f9SMatt Barden 				status = smb_errno2status(rc);
6380a73e6f9SMatt Barden 				fnode_held = B_FALSE;
6390a73e6f9SMatt Barden 				goto errout;
6400a73e6f9SMatt Barden 			}
6410a73e6f9SMatt Barden 			op->action_taken = SMB_OACT_CREATED;
6420a73e6f9SMatt Barden 			created = B_TRUE;
6430a73e6f9SMatt Barden 
6440a73e6f9SMatt Barden 			smb_node_unlock(dnode);
6450a73e6f9SMatt Barden 			dnode_wlock = B_FALSE;
6460a73e6f9SMatt Barden 		}
6470a73e6f9SMatt Barden 
648fb699f1eSAlek Pinchuk 		/*
649fb699f1eSAlek Pinchuk 		 * Oplock break is done prior to sharing checks as the break
650fb699f1eSAlek Pinchuk 		 * may cause other clients to close the file which would
65149d83597SMatt Barden 		 * affect the sharing checks, and may delete the file due to
65249d83597SMatt Barden 		 * DELETE_ON_CLOSE. This may block, so set the file opening
65349d83597SMatt Barden 		 * count before oplock stuff.
6548d94f651SGordon Ross 		 *
6558d94f651SGordon Ross 		 * Need the "proposed" ofile (and its TargetOplockKey) for
6568d94f651SGordon Ross 		 * correct oplock break semantics.
657fb699f1eSAlek Pinchuk 		 */
65894047d49SGordon Ross 		of = smb_ofile_alloc(sr, op, fnode, SMB_FTYPE_DISK,
6598d94f651SGordon Ross 		    tree_fid);
66094047d49SGordon Ross 		tree_fid = 0; // given to the ofile
6618d94f651SGordon Ross 		uniq_fid = of->f_uniqid;
662fb699f1eSAlek Pinchuk 
66394047d49SGordon Ross 		smb_node_inc_opening_count(fnode);
66494047d49SGordon Ross 		opening_incr = B_TRUE;
66594047d49SGordon Ross 
6660a73e6f9SMatt Barden 		if (!stream_found) {
6670a73e6f9SMatt Barden 			/*
6680a73e6f9SMatt Barden 			 * Stake our Share Access claim.
6690a73e6f9SMatt Barden 			 */
6700a73e6f9SMatt Barden 			smb_node_wrlock(fnode);
6710a73e6f9SMatt Barden 			fnode_wlock = B_TRUE;
6720a73e6f9SMatt Barden 
6730a73e6f9SMatt Barden 			status = smb_fsop_shrlock(sr->user_cr, fnode, uniq_fid,
6740a73e6f9SMatt Barden 			    op->desired_access, op->share_access);
6750a73e6f9SMatt Barden 			if (status != 0)
6760a73e6f9SMatt Barden 				goto errout;
6770a73e6f9SMatt Barden 
6780a73e6f9SMatt Barden 			fnode_shrlk = B_TRUE;
6790a73e6f9SMatt Barden 			smb_node_unlock(fnode);
6800a73e6f9SMatt Barden 			fnode_wlock = B_FALSE;
6810a73e6f9SMatt Barden 			goto stream_created;
6820a73e6f9SMatt Barden 		}
6830a73e6f9SMatt Barden 
68449d83597SMatt Barden 		/*
68594047d49SGordon Ross 		 * XXX Supposed to do share access checks next.
68694047d49SGordon Ross 		 * [MS-FSA] describes that as part of access check:
68794047d49SGordon Ross 		 * 2.1.5.1.2.1 Alg... Check Access to an Existing File
68894047d49SGordon Ross 		 *
68994047d49SGordon Ross 		 * If CreateDisposition is FILE_OPEN or FILE_OPEN_IF:
69094047d49SGordon Ross 		 *   If Open.Stream.Oplock is not empty and
69194047d49SGordon Ross 		 *   Open.Stream.Oplock.State contains BATCH_OPLOCK,
69294047d49SGordon Ross 		 *   the object store MUST check for an oplock
69394047d49SGordon Ross 		 *   break according to the algorithm in section 2.1.4.12,
69494047d49SGordon Ross 		 *   with input values as follows:
69594047d49SGordon Ross 		 *	Open equal to this operation's Open
69694047d49SGordon Ross 		 *	Oplock equal to Open.Stream.Oplock
69794047d49SGordon Ross 		 *	Operation equal to "OPEN"
69894047d49SGordon Ross 		 *	OpParams containing two members:
69994047d49SGordon Ross 		 *	  DesiredAccess, CreateDisposition
70094047d49SGordon Ross 		 *
70194047d49SGordon Ross 		 * It's not clear how Windows would ask the FS layer if
70294047d49SGordon Ross 		 * the file has a BATCH oplock.  We'll use a call to the
70394047d49SGordon Ross 		 * common oplock code, which calls smb_oplock_break_OPEN
70494047d49SGordon Ross 		 * only if the oplock state contains BATCH_OPLOCK.
70594047d49SGordon Ross 		 * See: smb_oplock_break_BATCH()
70694047d49SGordon Ross 		 *
70794047d49SGordon Ross 		 * Also note: There's a nearly identical section in the
70894047d49SGordon Ross 		 * spec. at the start of the "else" part of the above
70994047d49SGordon Ross 		 * "if (disposition is overwrite, overwrite_if)" so this
71094047d49SGordon Ross 		 * section (oplock break, the share mode check, and the
71194047d49SGordon Ross 		 * next oplock_break_HANDLE) are all factored out to be
71294047d49SGordon Ross 		 * in all cases above that if/else from the spec.
71349d83597SMatt Barden 		 */
71494047d49SGordon Ross 		status = smb_oplock_break_BATCH(fnode, of,
71594047d49SGordon Ross 		    op->desired_access, op->create_disposition);
71694047d49SGordon Ross 		if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) {
71794047d49SGordon Ross 			if (sr->session->dialect >= SMB_VERS_2_BASE)
71894047d49SGordon Ross 				(void) smb2sr_go_async(sr);
719525641e8SGordon Ross 			status = smb_oplock_wait_break(sr, fnode, 0);
72094047d49SGordon Ross 		}
72194047d49SGordon Ross 		if (status != NT_STATUS_SUCCESS)
72294047d49SGordon Ross 			goto errout;
72394047d49SGordon Ross 
72494047d49SGordon Ross 		/*
72594047d49SGordon Ross 		 * Check for sharing violations, and if any,
72694047d49SGordon Ross 		 * do oplock break of handle caching.
72794047d49SGordon Ross 		 *
72894047d49SGordon Ross 		 * Need node_wrlock during shrlock checks,
72994047d49SGordon Ross 		 * and not locked during oplock breaks etc.
73094047d49SGordon Ross 		 */
73194047d49SGordon Ross 		shrlock_t0 = gethrtime();
73294047d49SGordon Ross 	shrlock_again:
73394047d49SGordon Ross 		smb_node_wrlock(fnode);
73494047d49SGordon Ross 		fnode_wlock = B_TRUE;
73594047d49SGordon Ross 		status = smb_fsop_shrlock(sr->user_cr, fnode, uniq_fid,
73694047d49SGordon Ross 		    op->desired_access, op->share_access);
73794047d49SGordon Ross 		smb_node_unlock(fnode);
73894047d49SGordon Ross 		fnode_wlock = B_FALSE;
73994047d49SGordon Ross 
74094047d49SGordon Ross 		/*
74194047d49SGordon Ross 		 * [MS-FSA] "OPEN_BREAK_H"
74294047d49SGordon Ross 		 * If the (proposed) new open would violate sharing rules,
74394047d49SGordon Ross 		 * indicate an oplock break with OPEN_BREAK_H (to break
74494047d49SGordon Ross 		 * handle level caching rights) then try again.
74594047d49SGordon Ross 		 */
74694047d49SGordon Ross 		if (status == NT_STATUS_SHARING_VIOLATION &&
74794047d49SGordon Ross 		    did_break_handle == B_FALSE) {
74894047d49SGordon Ross 			did_break_handle = B_TRUE;
74994047d49SGordon Ross 
75094047d49SGordon Ross 			status = smb_oplock_break_HANDLE(fnode, of);
75194047d49SGordon Ross 			if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) {
75294047d49SGordon Ross 				if (sr->session->dialect >= SMB_VERS_2_BASE)
75394047d49SGordon Ross 					(void) smb2sr_go_async(sr);
754525641e8SGordon Ross 				status = smb_oplock_wait_break(sr, fnode, 0);
75594047d49SGordon Ross 			} else {
75694047d49SGordon Ross 				/*
75794047d49SGordon Ross 				 * Even when the oplock layer does NOT
75894047d49SGordon Ross 				 * give us the special status indicating
75994047d49SGordon Ross 				 * we should wait, it may have scheduled
76094047d49SGordon Ross 				 * taskq jobs that may close handles.
76194047d49SGordon Ross 				 * Give those a chance to run before we
76294047d49SGordon Ross 				 * check again for sharing violations.
76394047d49SGordon Ross 				 */
76494047d49SGordon Ross 				delay(MSEC_TO_TICK(10));
76594047d49SGordon Ross 			}
76694047d49SGordon Ross 			if (status != NT_STATUS_SUCCESS)
76794047d49SGordon Ross 				goto errout;
76894047d49SGordon Ross 
76994047d49SGordon Ross 			goto shrlock_again;
77049d83597SMatt Barden 		}
77149d83597SMatt Barden 
77294047d49SGordon Ross 		/*
7738d94f651SGordon Ross 		 * If we still have orphaned durable handles on this file,
7748d94f651SGordon Ross 		 * let's assume the client has lost interest in those and
7758d94f651SGordon Ross 		 * close them so they don't cause sharing violations.
7768d94f651SGordon Ross 		 * See longer comment at smb2_dh_close_my_orphans().
7778d94f651SGordon Ross 		 */
7788d94f651SGordon Ross 		if (status == NT_STATUS_SHARING_VIOLATION &&
7798d94f651SGordon Ross 		    sr->session->dialect >= SMB_VERS_2_BASE &&
7808d94f651SGordon Ross 		    did_cleanup_orphans == B_FALSE) {
7818d94f651SGordon Ross 
7828d94f651SGordon Ross 			did_cleanup_orphans = B_TRUE;
7838d94f651SGordon Ross 			smb2_dh_close_my_orphans(sr, of);
7848d94f651SGordon Ross 
7858d94f651SGordon Ross 			goto shrlock_again;
7868d94f651SGordon Ross 		}
7878d94f651SGordon Ross 
7888d94f651SGordon Ross 		/*
78994047d49SGordon Ross 		 * SMB1 expects a 1 sec. delay before returning a
79094047d49SGordon Ross 		 * sharing violation error.  If breaking oplocks
79194047d49SGordon Ross 		 * above took less than a sec, wait some more.
79294047d49SGordon Ross 		 * See: smbtorture base.defer_open
79394047d49SGordon Ross 		 */
79494047d49SGordon Ross 		if (status == NT_STATUS_SHARING_VIOLATION &&
79594047d49SGordon Ross 		    sr->session->dialect < SMB_VERS_2_BASE) {
79694047d49SGordon Ross 			hrtime_t t1 = shrlock_t0 + NANOSEC;
79794047d49SGordon Ross 			hrtime_t now = gethrtime();
79894047d49SGordon Ross 			if (now < t1) {
79994047d49SGordon Ross 				delay(NSEC_TO_TICK_ROUNDUP(t1 - now));
80094047d49SGordon Ross 			}
80194047d49SGordon Ross 		}
80294047d49SGordon Ross 
80394047d49SGordon Ross 		if (status != NT_STATUS_SUCCESS)
80494047d49SGordon Ross 			goto errout;
80594047d49SGordon Ross 		fnode_shrlk = B_TRUE;
806fb699f1eSAlek Pinchuk 
807fb699f1eSAlek Pinchuk 		/*
80894047d49SGordon Ross 		 * The [MS-FSA] spec. describes this oplock break as
80994047d49SGordon Ross 		 * part of the sharing access checks.  See:
81094047d49SGordon Ross 		 * 2.1.5.1.2.2 Algorithm to Check Sharing Access...
81194047d49SGordon Ross 		 * At the end of the share mode tests described there,
81294047d49SGordon Ross 		 * if it has not returned "sharing violation", it
81394047d49SGordon Ross 		 * specifies a call to the alg. in sec. 2.1.4.12,
81494047d49SGordon Ross 		 * that boils down to: smb_oplock_break_OPEN()
815fb699f1eSAlek Pinchuk 		 */
81694047d49SGordon Ross 		status = smb_oplock_break_OPEN(fnode, of,
81794047d49SGordon Ross 		    op->desired_access,
81894047d49SGordon Ross 		    op->create_disposition);
81994047d49SGordon Ross 		if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) {
82094047d49SGordon Ross 			if (sr->session->dialect >= SMB_VERS_2_BASE)
82194047d49SGordon Ross 				(void) smb2sr_go_async(sr);
822525641e8SGordon Ross 			status = smb_oplock_wait_break(sr, fnode, 0);
82394047d49SGordon Ross 		}
82494047d49SGordon Ross 		if (status != NT_STATUS_SUCCESS)
82594047d49SGordon Ross 			goto errout;
82694047d49SGordon Ross 
82794047d49SGordon Ross 		if ((fnode->flags & NODE_FLAGS_DELETE_COMMITTED) != 0) {
82894047d49SGordon Ross 			/*
82994047d49SGordon Ross 			 * Breaking the oplock caused the file to be deleted,
83094047d49SGordon Ross 			 * so let's bail and pretend the file wasn't found.
83194047d49SGordon Ross 			 * Have to duplicate much of the logic found a the
83294047d49SGordon Ross 			 * "errout" label here.
83394047d49SGordon Ross 			 *
83494047d49SGordon Ross 			 * This code path is exercised by smbtorture
83594047d49SGordon Ross 			 * smb2.durable-open.delete_on_close1
83694047d49SGordon Ross 			 */
837a5a9a6bbSGordon Ross 			DTRACE_PROBE1(node_deleted, smb_node_t *, fnode);
838088ae41eSGordon Ross 			tree_fid = of->f_fid;
839088ae41eSGordon Ross 			of->f_fid = 0;
84094047d49SGordon Ross 			smb_ofile_free(of);
84194047d49SGordon Ross 			of = NULL;
84294047d49SGordon Ross 			last_comp_found = B_FALSE;
84394047d49SGordon Ross 
84494047d49SGordon Ross 			/*
84594047d49SGordon Ross 			 * Get all the holds and locks into the state
84694047d49SGordon Ross 			 * they would have if lookup had failed.
84794047d49SGordon Ross 			 */
84894047d49SGordon Ross 			fnode_shrlk = B_FALSE;
84994047d49SGordon Ross 			smb_fsop_unshrlock(sr->user_cr, fnode, uniq_fid);
85094047d49SGordon Ross 
85194047d49SGordon Ross 			opening_incr = B_FALSE;
85294047d49SGordon Ross 			smb_node_dec_opening_count(fnode);
85394047d49SGordon Ross 
85494047d49SGordon Ross 			fnode_held = B_FALSE;
85594047d49SGordon Ross 			smb_node_release(fnode);
85694047d49SGordon Ross 
85794047d49SGordon Ross 			dnode_wlock = B_TRUE;
85894047d49SGordon Ross 			smb_node_wrlock(dnode);
85994047d49SGordon Ross 
86094047d49SGordon Ross 			goto create;
861da6c28aaSamw 		}
862da6c28aaSamw 
863fb699f1eSAlek Pinchuk 		/*
864fb699f1eSAlek Pinchuk 		 * Go ahead with modifications as necessary.
865fb699f1eSAlek Pinchuk 		 */
866fb699f1eSAlek Pinchuk 		switch (op->create_disposition) {
867fb699f1eSAlek Pinchuk 		case FILE_SUPERSEDE:
868fb699f1eSAlek Pinchuk 		case FILE_OVERWRITE_IF:
869fb699f1eSAlek Pinchuk 		case FILE_OVERWRITE:
8700a73e6f9SMatt Barden 			bzero(&new_attr, sizeof (new_attr));
8710a73e6f9SMatt Barden 			if (sname == NULL) {
872037cac00Sjoyce mcintosh 				op->dattr |= FILE_ATTRIBUTE_ARCHIVE;
8730a73e6f9SMatt Barden 				/*
8740a73e6f9SMatt Barden 				 * Don't apply readonly until
8750a73e6f9SMatt Barden 				 * smb_set_open_attributes
8760a73e6f9SMatt Barden 				 */
877037cac00Sjoyce mcintosh 				if (op->dattr & FILE_ATTRIBUTE_READONLY) {
878037cac00Sjoyce mcintosh 					op->dattr &= ~FILE_ATTRIBUTE_READONLY;
8795cb2894aSGordon Ross 					op->created_readonly = B_TRUE;
880037cac00Sjoyce mcintosh 				}
8810a73e6f9SMatt Barden 				new_attr.sa_dosattr = op->dattr;
8820a73e6f9SMatt Barden 			} else {
8830a73e6f9SMatt Barden 				new_attr.sa_dosattr = FILE_ATTRIBUTE_ARCHIVE;
8840a73e6f9SMatt Barden 			}
885037cac00Sjoyce mcintosh 
886a90cf9f2SGordon Ross 			/*
887a90cf9f2SGordon Ross 			 * Truncate the file data here.
888a90cf9f2SGordon Ross 			 * We set alloc_size = op->dsize later,
889a90cf9f2SGordon Ross 			 * after we have an ofile.  See:
890a90cf9f2SGordon Ross 			 * smb_set_open_attributes
891a90cf9f2SGordon Ross 			 */
892a90cf9f2SGordon Ross 			new_attr.sa_vattr.va_size = 0;
893037cac00Sjoyce mcintosh 			new_attr.sa_mask = SMB_AT_DOSATTR | SMB_AT_SIZE;
89494047d49SGordon Ross 			rc = smb_fsop_setattr(sr, sr->user_cr, fnode,
89594047d49SGordon Ross 			    &new_attr);
896037cac00Sjoyce mcintosh 			if (rc != 0) {
89794047d49SGordon Ross 				status = smb_errno2status(rc);
89894047d49SGordon Ross 				goto errout;
899da6c28aaSamw 			}
900da6c28aaSamw 
901da6c28aaSamw 			/*
902037cac00Sjoyce mcintosh 			 * If file is being replaced, remove existing streams
903da6c28aaSamw 			 */
90494047d49SGordon Ross 			if (SMB_IS_STREAM(fnode) == 0) {
905a90cf9f2SGordon Ross 				status = smb_fsop_remove_streams(sr,
90694047d49SGordon Ross 				    sr->user_cr, fnode);
90794047d49SGordon Ross 				if (status != 0)
90894047d49SGordon Ross 					goto errout;
909eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 			}
910da6c28aaSamw 
911da6c28aaSamw 			op->action_taken = SMB_OACT_TRUNCATED;
912da6c28aaSamw 			break;
913da6c28aaSamw 
914da6c28aaSamw 		default:
915da6c28aaSamw 			/*
916da6c28aaSamw 			 * FILE_OPEN or FILE_OPEN_IF.
917da6c28aaSamw 			 */
918a90cf9f2SGordon Ross 			/*
919a90cf9f2SGordon Ross 			 * Ignore any user-specified alloc_size for
920a90cf9f2SGordon Ross 			 * existing files, to avoid truncation in
921a90cf9f2SGordon Ross 			 * smb_set_open_attributes
922a90cf9f2SGordon Ross 			 */
923a90cf9f2SGordon Ross 			op->dsize = 0L;
924da6c28aaSamw 			op->action_taken = SMB_OACT_OPENED;
925da6c28aaSamw 			break;
926da6c28aaSamw 		}
927da6c28aaSamw 	} else {
92849d83597SMatt Barden create:
929da6c28aaSamw 		/* Last component was not found. */
930eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		dnode = op->fqi.fq_dnode;
931da6c28aaSamw 
9327b59d02dSjb150015 		if (is_dir == 0)
933eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 			is_stream = smb_is_stream_name(pn->pn_path);
9347b59d02dSjb150015 
935da6c28aaSamw 		if ((op->create_disposition == FILE_OPEN) ||
936da6c28aaSamw 		    (op->create_disposition == FILE_OVERWRITE)) {
93794047d49SGordon Ross 			status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
93894047d49SGordon Ross 			goto errout;
939da6c28aaSamw 		}
940da6c28aaSamw 
9410cab3dcdSGordon Ross 		if (is_dir != 0 &&
9420cab3dcdSGordon Ross 		    (op->dattr & FILE_ATTRIBUTE_TEMPORARY) != 0) {
9430cab3dcdSGordon Ross 			status = NT_STATUS_INVALID_PARAMETER;
9440cab3dcdSGordon Ross 			goto errout;
9450cab3dcdSGordon Ross 		}
9460cab3dcdSGordon Ross 
947abd30308SGordon Ross 		if ((op->dattr & FILE_ATTRIBUTE_READONLY) != 0 &&
948abd30308SGordon Ross 		    (op->create_options & FILE_DELETE_ON_CLOSE) != 0) {
949abd30308SGordon Ross 			status = NT_STATUS_CANNOT_DELETE;
950abd30308SGordon Ross 			goto errout;
951abd30308SGordon Ross 		}
952abd30308SGordon Ross 
9539e3ab9e9SMatt Barden 		if ((op->desired_access & ACCESS_SYSTEM_SECURITY) != 0 &&
9549e3ab9e9SMatt Barden 		    !smb_user_has_security_priv(sr->uid_user, sr->user_cr)) {
9559e3ab9e9SMatt Barden 			status = NT_STATUS_ACCESS_DENIED;
9569e3ab9e9SMatt Barden 			goto errout;
9579e3ab9e9SMatt Barden 		}
9589e3ab9e9SMatt Barden 
9599fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		if (pn->pn_fname && smb_is_invalid_filename(pn->pn_fname)) {
96094047d49SGordon Ross 			status = NT_STATUS_OBJECT_NAME_INVALID;
96194047d49SGordon Ross 			goto errout;
9622c2961f8Sjose borrego 		}
9632c2961f8Sjose borrego 
964da6c28aaSamw 		/*
965a1096253SGordon Ross 		 * Don't create in directories marked "Delete on close".
966a1096253SGordon Ross 		 */
967a1096253SGordon Ross 		if (dnode->flags & NODE_FLAGS_DELETE_ON_CLOSE) {
96894047d49SGordon Ross 			status = NT_STATUS_DELETE_PENDING;
96994047d49SGordon Ross 			goto errout;
970a1096253SGordon Ross 		}
971a1096253SGordon Ross 
972a1096253SGordon Ross 		/*
9735cb2894aSGordon Ross 		 * Create always sets the DOS attributes, type, and mode
9745cb2894aSGordon Ross 		 * in the if/else below (different for file vs directory).
9755cb2894aSGordon Ross 		 * Don't set the readonly bit until smb_set_open_attributes
9765cb2894aSGordon Ross 		 * or that would prevent this open.  Note that op->dattr
9775cb2894aSGordon Ross 		 * needs to be what smb_set_open_attributes will use,
9785cb2894aSGordon Ross 		 * except for the readonly bit.
9795cb2894aSGordon Ross 		 */
9805cb2894aSGordon Ross 		bzero(&new_attr, sizeof (new_attr));
9815cb2894aSGordon Ross 		new_attr.sa_mask = SMB_AT_DOSATTR | SMB_AT_TYPE | SMB_AT_MODE;
982037cac00Sjoyce mcintosh 		if (op->dattr & FILE_ATTRIBUTE_READONLY) {
983037cac00Sjoyce mcintosh 			op->dattr &= ~FILE_ATTRIBUTE_READONLY;
984037cac00Sjoyce mcintosh 			op->created_readonly = B_TRUE;
985037cac00Sjoyce mcintosh 		}
986037cac00Sjoyce mcintosh 
9875cb2894aSGordon Ross 		/*
9885cb2894aSGordon Ross 		 * SMB create can specify the create time.
9895cb2894aSGordon Ross 		 */
990c8ec8eeaSjose borrego 		if ((op->crtime.tv_sec != 0) &&
991c8ec8eeaSjose borrego 		    (op->crtime.tv_sec != UINT_MAX)) {
992c8ec8eeaSjose borrego 			new_attr.sa_mask |= SMB_AT_CRTIME;
993c8ec8eeaSjose borrego 			new_attr.sa_crtime = op->crtime;
994c8ec8eeaSjose borrego 		}
995c8ec8eeaSjose borrego 
996da6c28aaSamw 		if (is_dir == 0) {
997037cac00Sjoyce mcintosh 			op->dattr |= FILE_ATTRIBUTE_ARCHIVE;
998037cac00Sjoyce mcintosh 			new_attr.sa_dosattr = op->dattr;
999da6c28aaSamw 			new_attr.sa_vattr.va_type = VREG;
10005cb2894aSGordon Ross 			if (is_stream)
10015cb2894aSGordon Ross 				new_attr.sa_vattr.va_mode = S_IRUSR | S_IWUSR;
10025cb2894aSGordon Ross 			else
10035cb2894aSGordon Ross 				new_attr.sa_vattr.va_mode =
10047b59d02dSjb150015 				    S_IRUSR | S_IRGRP | S_IROTH |
10057b59d02dSjb150015 				    S_IWUSR | S_IWGRP | S_IWOTH;
1006dc20a302Sas200622 
1007a90cf9f2SGordon Ross 			/*
1008a90cf9f2SGordon Ross 			 * We set alloc_size = op->dsize later,
1009c5f48fa5SGordon Ross 			 * (in smb_set_open_attributes) after we
1010c5f48fa5SGordon Ross 			 * have an ofile on which to save that.
1011c5f48fa5SGordon Ross 			 *
1012c5f48fa5SGordon Ross 			 * Legacy Open&X sets size to alloc_size
1013c5f48fa5SGordon Ross 			 * when creating a new file.
1014a90cf9f2SGordon Ross 			 */
1015c5f48fa5SGordon Ross 			if (sr->smb_com == SMB_COM_OPEN_ANDX) {
1016c5f48fa5SGordon Ross 				new_attr.sa_vattr.va_size = op->dsize;
1017c5f48fa5SGordon Ross 				new_attr.sa_mask |= SMB_AT_SIZE;
1018c5f48fa5SGordon Ross 			}
1019dc20a302Sas200622 
1020da6c28aaSamw 			rc = smb_fsop_create(sr, sr->user_cr, dnode,
1021037cac00Sjoyce mcintosh 			    op->fqi.fq_last_comp, &new_attr, &op->fqi.fq_fnode);
1022da6c28aaSamw 		} else {
10233db3f65cSamw 			op->dattr |= FILE_ATTRIBUTE_DIRECTORY;
1024037cac00Sjoyce mcintosh 			new_attr.sa_dosattr = op->dattr;
1025da6c28aaSamw 			new_attr.sa_vattr.va_type = VDIR;
1026da6c28aaSamw 			new_attr.sa_vattr.va_mode = 0777;
1027c8ec8eeaSjose borrego 
1028da6c28aaSamw 			rc = smb_fsop_mkdir(sr, sr->user_cr, dnode,
1029037cac00Sjoyce mcintosh 			    op->fqi.fq_last_comp, &new_attr, &op->fqi.fq_fnode);
103094047d49SGordon Ross 		}
1031da6c28aaSamw 		if (rc != 0) {
103294047d49SGordon Ross 			status = smb_errno2status(rc);
103394047d49SGordon Ross 			goto errout;
1034da6c28aaSamw 		}
1035dc20a302Sas200622 
10368d94f651SGordon Ross 		/* Create done. */
103794047d49SGordon Ross 		smb_node_unlock(dnode);
103894047d49SGordon Ross 		dnode_wlock = B_FALSE;
1039da6c28aaSamw 
1040eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		created = B_TRUE;
1041da6c28aaSamw 		op->action_taken = SMB_OACT_CREATED;
1042c8ec8eeaSjose borrego 
10438d94f651SGordon Ross 		/* Note: hold from create */
104494047d49SGordon Ross 		fnode = op->fqi.fq_fnode;
104594047d49SGordon Ross 		fnode_held = B_TRUE;
104694047d49SGordon Ross 
10472c1b14e5Sjose borrego 		if (max_requested) {
104894047d49SGordon Ross 			smb_fsop_eaccess(sr, sr->user_cr, fnode, &max_allowed);
10492c1b14e5Sjose borrego 			op->desired_access |= max_allowed;
10502c1b14e5Sjose borrego 		}
1051a90cf9f2SGordon Ross 		/*
1052245d1332SMatt Barden 		 * We created this object (we own it) so grant
1053245d1332SMatt Barden 		 * read_control + read_attributes on this handle,
1054a90cf9f2SGordon Ross 		 * even if that was not requested.  This avoids
1055245d1332SMatt Barden 		 * unexpected access failures later.
1056a90cf9f2SGordon Ross 		 */
1057245d1332SMatt Barden 		op->desired_access |= (READ_CONTROL | FILE_READ_ATTRIBUTES);
10582c1b14e5Sjose borrego 
10598d94f651SGordon Ross 		/* Allocate the ofile and fill in most of it. */
10608d94f651SGordon Ross 		of = smb_ofile_alloc(sr, op, fnode, SMB_FTYPE_DISK,
10618d94f651SGordon Ross 		    tree_fid);
10628d94f651SGordon Ross 		tree_fid = 0; // given to the ofile
10638d94f651SGordon Ross 		uniq_fid = of->f_uniqid;
10648d94f651SGordon Ross 
10658d94f651SGordon Ross 		smb_node_inc_opening_count(fnode);
10668d94f651SGordon Ross 		opening_incr = B_TRUE;
10678d94f651SGordon Ross 
10688d94f651SGordon Ross 		/*
10698d94f651SGordon Ross 		 * Share access checks...
10708d94f651SGordon Ross 		 */
10718d94f651SGordon Ross 		smb_node_wrlock(fnode);
10728d94f651SGordon Ross 		fnode_wlock = B_TRUE;
10738d94f651SGordon Ross 
10748d94f651SGordon Ross 		status = smb_fsop_shrlock(sr->user_cr, fnode, uniq_fid,
10758d94f651SGordon Ross 		    op->desired_access, op->share_access);
10768d94f651SGordon Ross 		if (status != 0)
10778d94f651SGordon Ross 			goto errout;
10788d94f651SGordon Ross 		fnode_shrlk = B_TRUE;
10798d94f651SGordon Ross 
108094047d49SGordon Ross 		/*
108194047d49SGordon Ross 		 * MS-FSA 2.1.5.1.1
108294047d49SGordon Ross 		 * If the Oplock member of the DirectoryStream in
108394047d49SGordon Ross 		 * Link.ParentFile.StreamList (ParentOplock) is
108494047d49SGordon Ross 		 * not empty ... oplock break on the parent...
108594047d49SGordon Ross 		 * (dnode is the parent directory)
108694047d49SGordon Ross 		 *
108794047d49SGordon Ross 		 * This compares of->ParentOplockKey with each
108894047d49SGordon Ross 		 * oplock of->TargetOplockKey and breaks...
108994047d49SGordon Ross 		 * so it's OK that we're passing an OF that's
109094047d49SGordon Ross 		 * NOT a member of dnode->n_ofile_list
109194047d49SGordon Ross 		 *
109294047d49SGordon Ross 		 * The break never blocks, so ignore the return.
109394047d49SGordon Ross 		 */
109494047d49SGordon Ross 		(void) smb_oplock_break_PARENT(dnode, of);
1095da6c28aaSamw 	}
1096da6c28aaSamw 
10970a73e6f9SMatt Barden stream_created:
109868b2bbf2SGordon Ross 	/*
109994047d49SGordon Ross 	 * We might have blocked in smb_oplock_break_OPEN long enough
110094047d49SGordon Ross 	 * so a tree disconnect might have happened.  In that case,
110194047d49SGordon Ross 	 * we would be adding an ofile to a tree that's disconnecting,
110294047d49SGordon Ross 	 * which would interfere with tear-down.  If so, error out.
110368b2bbf2SGordon Ross 	 */
110494047d49SGordon Ross 	if (!smb_tree_is_connected(sr->tid_tree)) {
110568b2bbf2SGordon Ross 		status = NT_STATUS_INVALID_PARAMETER;
110694047d49SGordon Ross 		goto errout;
1107037cac00Sjoyce mcintosh 	}
1108037cac00Sjoyce mcintosh 
1109037cac00Sjoyce mcintosh 	/*
111094047d49SGordon Ross 	 * Moved this up from smb_ofile_open()
111194047d49SGordon Ross 	 */
111294047d49SGordon Ross 	if ((rc = smb_fsop_open(fnode, of->f_mode, of->f_cr)) != 0) {
111394047d49SGordon Ross 		status = smb_errno2status(rc);
111494047d49SGordon Ross 		goto errout;
111594047d49SGordon Ross 	}
111694047d49SGordon Ross 
111794047d49SGordon Ross 	/*
111894047d49SGordon Ross 	 * Complete this open (add to ofile lists)
111994047d49SGordon Ross 	 */
112094047d49SGordon Ross 	smb_ofile_open(sr, op, of);
112194047d49SGordon Ross 	did_open = B_TRUE;
112294047d49SGordon Ross 
112394047d49SGordon Ross 	/*
1124037cac00Sjoyce mcintosh 	 * This MUST be done after ofile creation, so that explicitly
11255cb2894aSGordon Ross 	 * set timestamps can be remembered on the ofile, and setting
11265cb2894aSGordon Ross 	 * the readonly flag won't affect access via this open.
1127037cac00Sjoyce mcintosh 	 */
11285fd03bc0SGordon Ross 	if ((rc = smb_set_open_attributes(sr, of)) != 0) {
1129a90cf9f2SGordon Ross 		status = smb_errno2status(rc);
113094047d49SGordon Ross 		goto errout;
1131037cac00Sjoyce mcintosh 	}
1132037cac00Sjoyce mcintosh 
11335fd03bc0SGordon Ross 	/*
11345fd03bc0SGordon Ross 	 * We've already done access checks above,
11355fd03bc0SGordon Ross 	 * and want this call to succeed even when
11365fd03bc0SGordon Ross 	 * !(desired_access & FILE_READ_ATTRIBUTES),
11375fd03bc0SGordon Ross 	 * so pass kcred here.
11385fd03bc0SGordon Ross 	 */
11395fd03bc0SGordon Ross 	op->fqi.fq_fattr.sa_mask = SMB_AT_ALL;
114094047d49SGordon Ross 	(void) smb_node_getattr(sr, fnode, zone_kcred(), of,
11415fd03bc0SGordon Ross 	    &op->fqi.fq_fattr);
11428b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 
11438c10a865Sas200622 	/*
11448c10a865Sas200622 	 * Propagate the write-through mode from the open params
11458c10a865Sas200622 	 * to the node: see the notes in the function header.
114694047d49SGordon Ross 	 * XXX: write_through should be a flag on the ofile.
11478c10a865Sas200622 	 */
11488c10a865Sas200622 	if (sr->sr_cfg->skc_sync_enable ||
11498c10a865Sas200622 	    (op->create_options & FILE_WRITE_THROUGH))
115094047d49SGordon Ross 		fnode->flags |= NODE_FLAGS_WRITE_THROUGH;
11518c10a865Sas200622 
1152037cac00Sjoyce mcintosh 	/*
1153037cac00Sjoyce mcintosh 	 * Set up the fileid and dosattr in open_param for response
1154037cac00Sjoyce mcintosh 	 */
1155eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	op->fileid = op->fqi.fq_fattr.sa_vattr.va_nodeid;
1156037cac00Sjoyce mcintosh 	op->dattr = op->fqi.fq_fattr.sa_dosattr;
11578c10a865Sas200622 
1158da6c28aaSamw 	/*
1159da6c28aaSamw 	 * Set up the file type in open_param for the response
1160da6c28aaSamw 	 */
1161da6c28aaSamw 	op->ftype = SMB_FTYPE_DISK;
1162da6c28aaSamw 	sr->smb_fid = of->f_fid;
1163da6c28aaSamw 	sr->fid_ofile = of;
1164da6c28aaSamw 
116594047d49SGordon Ross 	if (smb_node_is_file(fnode)) {
1166eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		op->dsize = op->fqi.fq_fattr.sa_vattr.va_size;
11679fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	} else {
11689fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		/* directory or symlink */
11692c2961f8Sjose borrego 		op->dsize = 0;
11702c2961f8Sjose borrego 	}
1171dc20a302Sas200622 
117294047d49SGordon Ross 	/*
117394047d49SGordon Ross 	 * Note: oplock_acquire happens in callers, because
117494047d49SGordon Ross 	 * how that happens is protocol-specific.
117594047d49SGordon Ross 	 */
1176cb174861Sjoyce mcintosh 
11770a73e6f9SMatt Barden 	if (sname != NULL)
11780a73e6f9SMatt Barden 		kmem_free(sname, MAXNAMELEN);
117994047d49SGordon Ross 	if (fnode_wlock)
118094047d49SGordon Ross 		smb_node_unlock(fnode);
118194047d49SGordon Ross 	if (opening_incr)
118294047d49SGordon Ross 		smb_node_dec_opening_count(fnode);
118394047d49SGordon Ross 	if (fnode_held)
118494047d49SGordon Ross 		smb_node_release(fnode);
118594047d49SGordon Ross 	if (dnode_wlock)
1186cb174861Sjoyce mcintosh 		smb_node_unlock(dnode);
118794047d49SGordon Ross 	if (dnode_held)
1188da6c28aaSamw 		smb_node_release(dnode);
1189da6c28aaSamw 
1190da6c28aaSamw 	return (NT_STATUS_SUCCESS);
119194047d49SGordon Ross 
119294047d49SGordon Ross errout:
119394047d49SGordon Ross 	if (did_open) {
119494047d49SGordon Ross 		smb_ofile_close(of, 0);
11958d94f651SGordon Ross 		/* rele via sr->fid_ofile */
119694047d49SGordon Ross 	} else if (of != NULL) {
11978d94f651SGordon Ross 		/* No other refs possible */
119894047d49SGordon Ross 		smb_ofile_free(of);
1199da6c28aaSamw 	}
1200da6c28aaSamw 
120194047d49SGordon Ross 	if (fnode_shrlk)
120294047d49SGordon Ross 		smb_fsop_unshrlock(sr->user_cr, fnode, uniq_fid);
1203148d1a41SMatt Barden 
120494047d49SGordon Ross 	if (created) {
120594047d49SGordon Ross 		/* Try to roll-back create. */
120694047d49SGordon Ross 		smb_delete_new_object(sr);
1207148d1a41SMatt Barden 	}
1208148d1a41SMatt Barden 
12090a73e6f9SMatt Barden 	if (sname != NULL)
12100a73e6f9SMatt Barden 		kmem_free(sname, MAXNAMELEN);
121194047d49SGordon Ross 	if (fnode_wlock)
121294047d49SGordon Ross 		smb_node_unlock(fnode);
121394047d49SGordon Ross 	if (opening_incr)
121494047d49SGordon Ross 		smb_node_dec_opening_count(fnode);
121594047d49SGordon Ross 	if (fnode_held)
121694047d49SGordon Ross 		smb_node_release(fnode);
121794047d49SGordon Ross 	if (dnode_wlock)
121894047d49SGordon Ross 		smb_node_unlock(dnode);
121994047d49SGordon Ross 	if (dnode_held)
122094047d49SGordon Ross 		smb_node_release(dnode);
1221cb174861Sjoyce mcintosh 
122294047d49SGordon Ross 	if (tree_fid != 0)
122394047d49SGordon Ross 		smb_idpool_free(&tree->t_fid_pool, tree_fid);
1224cb174861Sjoyce mcintosh 
122594047d49SGordon Ross 	return (status);
1226cb174861Sjoyce mcintosh }
12275fd03bc0SGordon Ross 
1228cb174861Sjoyce mcintosh /*
12295fd03bc0SGordon Ross  * smb_set_open_attributes
1230037cac00Sjoyce mcintosh  *
1231037cac00Sjoyce mcintosh  * Last write time:
1232037cac00Sjoyce mcintosh  * - If the last_write time specified in the open params is not 0 or -1,
1233037cac00Sjoyce mcintosh  *   use it as file's mtime. This will be considered an explicitly set
1234037cac00Sjoyce mcintosh  *   timestamps, not reset by subsequent writes.
1235037cac00Sjoyce mcintosh  *
12365fd03bc0SGordon Ross  * DOS attributes
12375fd03bc0SGordon Ross  * - If we created_readonly, we now store the real DOS attributes
12385fd03bc0SGordon Ross  *   (including the readonly bit) so subsequent opens will see it.
1239037cac00Sjoyce mcintosh  *
1240037cac00Sjoyce mcintosh  * Returns: errno
1241037cac00Sjoyce mcintosh  */
1242037cac00Sjoyce mcintosh static int
smb_set_open_attributes(smb_request_t * sr,smb_ofile_t * of)12435fd03bc0SGordon Ross smb_set_open_attributes(smb_request_t *sr, smb_ofile_t *of)
1244037cac00Sjoyce mcintosh {
12455fd03bc0SGordon Ross 	smb_attr_t	attr;
1246148c5f43SAlan Wright 	smb_arg_open_t	*op = &sr->sr_open;
1247037cac00Sjoyce mcintosh 	smb_node_t	*node = of->f_node;
12485fd03bc0SGordon Ross 	int		rc = 0;
1249037cac00Sjoyce mcintosh 
1250037cac00Sjoyce mcintosh 	bzero(&attr, sizeof (smb_attr_t));
12515fd03bc0SGordon Ross 
12525fd03bc0SGordon Ross 	if (op->created_readonly) {
12535fd03bc0SGordon Ross 		attr.sa_dosattr = op->dattr | FILE_ATTRIBUTE_READONLY;
12545fd03bc0SGordon Ross 		attr.sa_mask |= SMB_AT_DOSATTR;
12555fd03bc0SGordon Ross 	}
1256037cac00Sjoyce mcintosh 
1257a90cf9f2SGordon Ross 	if (op->dsize != 0) {
1258a90cf9f2SGordon Ross 		attr.sa_allocsz = op->dsize;
1259a90cf9f2SGordon Ross 		attr.sa_mask |= SMB_AT_ALLOCSZ;
1260a90cf9f2SGordon Ross 	}
1261a90cf9f2SGordon Ross 
1262037cac00Sjoyce mcintosh 	if ((op->mtime.tv_sec != 0) && (op->mtime.tv_sec != UINT_MAX)) {
1263037cac00Sjoyce mcintosh 		attr.sa_vattr.va_mtime = op->mtime;
12645fd03bc0SGordon Ross 		attr.sa_mask |= SMB_AT_MTIME;
1265037cac00Sjoyce mcintosh 	}
1266037cac00Sjoyce mcintosh 
12675fd03bc0SGordon Ross 	if (attr.sa_mask != 0)
12685fd03bc0SGordon Ross 		rc = smb_node_setattr(sr, node, of->f_cr, of, &attr);
1269037cac00Sjoyce mcintosh 
1270037cac00Sjoyce mcintosh 	return (rc);
1271037cac00Sjoyce mcintosh }
1272037cac00Sjoyce mcintosh 
1273037cac00Sjoyce mcintosh /*
12748b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States  * This function is used to delete a newly created object (file or
12758b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States  * directory) if an error occurs after creation of the object.
1276da6c28aaSamw  */
12778b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States static void
smb_delete_new_object(smb_request_t * sr)12788b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States smb_delete_new_object(smb_request_t *sr)
1279da6c28aaSamw {
1280148c5f43SAlan Wright 	smb_arg_open_t	*op = &sr->sr_open;
12818b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	smb_fqi_t	*fqi = &(op->fqi);
12828b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	uint32_t	flags = 0;
12838b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 
12848b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (SMB_TREE_IS_CASEINSENSITIVE(sr))
12858b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		flags |= SMB_IGNORE_CASE;
12868b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (SMB_TREE_SUPPORTS_CATIA(sr))
12878b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		flags |= SMB_CATIA;
12888b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 
12898b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (op->create_options & FILE_DIRECTORY_FILE)
1290eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		(void) smb_fsop_rmdir(sr, sr->user_cr, fqi->fq_dnode,
1291eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		    fqi->fq_last_comp, flags);
12928b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	else
1293eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		(void) smb_fsop_remove(sr, sr->user_cr, fqi->fq_dnode,
1294eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		    fqi->fq_last_comp, flags);
1295eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States }
1296