xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_common_open.c (revision 8d94f651a44d41a7147253bb5dad1a53941e8f50)
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.
245677e049SGordon Ross  * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
25da6c28aaSamw  */
26da6c28aaSamw 
27da6c28aaSamw /*
28da6c28aaSamw  * This module provides the common open functionality to the various
29da6c28aaSamw  * open and create SMB interface functions.
30da6c28aaSamw  */
31da6c28aaSamw 
32bbf6f00cSJordan Brown #include <sys/types.h>
33bbf6f00cSJordan Brown #include <sys/cmn_err.h>
34da6c28aaSamw #include <sys/fcntl.h>
35dc20a302Sas200622 #include <sys/nbmlock.h>
36bbf6f00cSJordan Brown #include <smbsrv/string.h>
37148d1a41SMatt Barden #include <smbsrv/smb2_kproto.h>
38bbf6f00cSJordan Brown #include <smbsrv/smb_fsops.h>
39bbf6f00cSJordan Brown #include <smbsrv/smbinfo.h>
40da6c28aaSamw 
4191ca6bffSGordon Ross int smb_session_ofile_max = 32768;
4291ca6bffSGordon Ross 
43faa1795aSjb150015 extern uint32_t smb_is_executable(char *);
448b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States static void smb_delete_new_object(smb_request_t *);
455fd03bc0SGordon Ross static int smb_set_open_attributes(smb_request_t *, smb_ofile_t *);
46da6c28aaSamw 
47da6c28aaSamw /*
48da6c28aaSamw  * smb_access_generic_to_file
49da6c28aaSamw  *
50da6c28aaSamw  * Search MSDN for IoCreateFile to see following mapping.
51da6c28aaSamw  *
52da6c28aaSamw  * GENERIC_READ		STANDARD_RIGHTS_READ, FILE_READ_DATA,
53da6c28aaSamw  *			FILE_READ_ATTRIBUTES and FILE_READ_EA
54da6c28aaSamw  *
55da6c28aaSamw  * GENERIC_WRITE	STANDARD_RIGHTS_WRITE, FILE_WRITE_DATA,
56da6c28aaSamw  *               FILE_WRITE_ATTRIBUTES, FILE_WRITE_EA, and FILE_APPEND_DATA
57da6c28aaSamw  *
58da6c28aaSamw  * GENERIC_EXECUTE	STANDARD_RIGHTS_EXECUTE, SYNCHRONIZE, and FILE_EXECUTE.
59da6c28aaSamw  */
60a90cf9f2SGordon Ross static uint32_t
61da6c28aaSamw smb_access_generic_to_file(uint32_t desired_access)
62da6c28aaSamw {
63a90cf9f2SGordon Ross 	uint32_t access = 0;
64da6c28aaSamw 
65da6c28aaSamw 	if (desired_access & GENERIC_ALL)
66da6c28aaSamw 		return (FILE_ALL_ACCESS & ~SYNCHRONIZE);
67da6c28aaSamw 
68da6c28aaSamw 	if (desired_access & GENERIC_EXECUTE) {
69da6c28aaSamw 		desired_access &= ~GENERIC_EXECUTE;
70da6c28aaSamw 		access |= (STANDARD_RIGHTS_EXECUTE |
71da6c28aaSamw 		    SYNCHRONIZE | FILE_EXECUTE);
72da6c28aaSamw 	}
73da6c28aaSamw 
74da6c28aaSamw 	if (desired_access & GENERIC_WRITE) {
75da6c28aaSamw 		desired_access &= ~GENERIC_WRITE;
76da6c28aaSamw 		access |= (FILE_GENERIC_WRITE & ~SYNCHRONIZE);
77da6c28aaSamw 	}
78da6c28aaSamw 
79da6c28aaSamw 	if (desired_access & GENERIC_READ) {
80da6c28aaSamw 		desired_access &= ~GENERIC_READ;
81da6c28aaSamw 		access |= FILE_GENERIC_READ;
82da6c28aaSamw 	}
83da6c28aaSamw 
84da6c28aaSamw 	return (access | desired_access);
85da6c28aaSamw }
86da6c28aaSamw 
87da6c28aaSamw /*
88da6c28aaSamw  * smb_omode_to_amask
89da6c28aaSamw  *
90da6c28aaSamw  * This function converts open modes used by Open and Open AndX
91da6c28aaSamw  * commands to desired access bits used by NT Create AndX command.
92da6c28aaSamw  */
93da6c28aaSamw uint32_t
94da6c28aaSamw smb_omode_to_amask(uint32_t desired_access)
95da6c28aaSamw {
96da6c28aaSamw 	switch (desired_access & SMB_DA_ACCESS_MASK) {
97da6c28aaSamw 	case SMB_DA_ACCESS_READ:
98da6c28aaSamw 		return (FILE_GENERIC_READ);
99da6c28aaSamw 
100da6c28aaSamw 	case SMB_DA_ACCESS_WRITE:
101da6c28aaSamw 		return (FILE_GENERIC_WRITE);
102da6c28aaSamw 
103da6c28aaSamw 	case SMB_DA_ACCESS_READ_WRITE:
104da6c28aaSamw 		return (FILE_GENERIC_READ | FILE_GENERIC_WRITE);
105da6c28aaSamw 
106da6c28aaSamw 	case SMB_DA_ACCESS_EXECUTE:
107c5f48fa5SGordon Ross 		return (FILE_GENERIC_READ | FILE_GENERIC_EXECUTE);
108da6c28aaSamw 
1092c2961f8Sjose borrego 	default:
1102c2961f8Sjose borrego 		return (FILE_GENERIC_ALL);
1112c2961f8Sjose borrego 	}
112da6c28aaSamw }
113da6c28aaSamw 
114da6c28aaSamw /*
115da6c28aaSamw  * smb_denymode_to_sharemode
116da6c28aaSamw  *
117da6c28aaSamw  * This function converts deny modes used by Open and Open AndX
118da6c28aaSamw  * commands to share access bits used by NT Create AndX command.
119da6c28aaSamw  */
120da6c28aaSamw uint32_t
121da6c28aaSamw smb_denymode_to_sharemode(uint32_t desired_access, char *fname)
122da6c28aaSamw {
123da6c28aaSamw 	switch (desired_access & SMB_DA_SHARE_MASK) {
124da6c28aaSamw 	case SMB_DA_SHARE_COMPATIBILITY:
125da6c28aaSamw 		if (smb_is_executable(fname))
126da6c28aaSamw 			return (FILE_SHARE_READ | FILE_SHARE_WRITE);
127c8ec8eeaSjose borrego 
128c8ec8eeaSjose borrego 		return (FILE_SHARE_ALL);
129da6c28aaSamw 
130da6c28aaSamw 	case SMB_DA_SHARE_EXCLUSIVE:
131da6c28aaSamw 		return (FILE_SHARE_NONE);
132da6c28aaSamw 
133da6c28aaSamw 	case SMB_DA_SHARE_DENY_WRITE:
134da6c28aaSamw 		return (FILE_SHARE_READ);
135da6c28aaSamw 
136da6c28aaSamw 	case SMB_DA_SHARE_DENY_READ:
137da6c28aaSamw 		return (FILE_SHARE_WRITE);
138da6c28aaSamw 
139da6c28aaSamw 	case SMB_DA_SHARE_DENY_NONE:
1402c2961f8Sjose borrego 	default:
141da6c28aaSamw 		return (FILE_SHARE_READ | FILE_SHARE_WRITE);
142da6c28aaSamw 	}
143da6c28aaSamw }
144da6c28aaSamw 
145da6c28aaSamw /*
146da6c28aaSamw  * smb_ofun_to_crdisposition
147da6c28aaSamw  *
148da6c28aaSamw  * This function converts open function values used by Open and Open AndX
149da6c28aaSamw  * commands to create disposition values used by NT Create AndX command.
150da6c28aaSamw  */
151da6c28aaSamw uint32_t
152da6c28aaSamw smb_ofun_to_crdisposition(uint16_t  ofun)
153da6c28aaSamw {
154da6c28aaSamw 	static int ofun_cr_map[3][2] =
155da6c28aaSamw 	{
156da6c28aaSamw 		{ -1,			FILE_CREATE },
157da6c28aaSamw 		{ FILE_OPEN,		FILE_OPEN_IF },
158da6c28aaSamw 		{ FILE_OVERWRITE,	FILE_OVERWRITE_IF }
159da6c28aaSamw 	};
160da6c28aaSamw 
161da6c28aaSamw 	int row = ofun & SMB_OFUN_OPEN_MASK;
162da6c28aaSamw 	int col = (ofun & SMB_OFUN_CREATE_MASK) >> 4;
163da6c28aaSamw 
164da6c28aaSamw 	if (row == 3)
1652c2961f8Sjose borrego 		return (FILE_MAXIMUM_DISPOSITION + 1);
166da6c28aaSamw 
167da6c28aaSamw 	return (ofun_cr_map[row][col]);
168da6c28aaSamw }
169da6c28aaSamw 
170da6c28aaSamw /*
17194047d49SGordon Ross  * smb_common_open
172da6c28aaSamw  *
173da6c28aaSamw  * Notes on write-through behaviour. It looks like pre-LM0.12 versions
174da6c28aaSamw  * of the protocol specify the write-through mode when a file is opened,
175da6c28aaSamw  * (SmbOpen, SmbOpenAndX) so the write calls (SmbWrite, SmbWriteAndClose,
176da6c28aaSamw  * SmbWriteAndUnlock) don't need to contain a write-through flag.
177da6c28aaSamw  *
178da6c28aaSamw  * With LM0.12, the open calls (SmbCreateAndX, SmbNtTransactCreate)
179da6c28aaSamw  * don't indicate which write-through mode to use. Instead the write
180da6c28aaSamw  * calls (SmbWriteAndX, SmbWriteRaw) specify the mode on a per call
181da6c28aaSamw  * basis.
182da6c28aaSamw  *
183da6c28aaSamw  * We don't care which open call was used to get us here, we just need
184da6c28aaSamw  * to ensure that the write-through mode flag is copied from the open
185da6c28aaSamw  * parameters to the node. We test the omode write-through flag in all
186da6c28aaSamw  * write functions.
187da6c28aaSamw  *
188a90cf9f2SGordon Ross  * This function returns NT status codes.
1898c10a865Sas200622  *
1908c10a865Sas200622  * The following rules apply when processing a file open request:
1918c10a865Sas200622  *
192cb174861Sjoyce mcintosh  * - Oplocks must be broken prior to share checking as the break may
193cb174861Sjoyce mcintosh  *   cause other clients to close the file, which would affect sharing
194cb174861Sjoyce mcintosh  *   checks.
1958c10a865Sas200622  *
1968c10a865Sas200622  * - Share checks must take place prior to access checks for correct
1978c10a865Sas200622  * Windows semantics and to prevent unnecessary NFS delegation recalls.
1988c10a865Sas200622  *
1998c10a865Sas200622  * - Oplocks must be acquired after open to ensure the correct
2008c10a865Sas200622  * synchronization with NFS delegation and FEM installation.
201c8ec8eeaSjose borrego  *
202c8ec8eeaSjose borrego  * DOS readonly bit rules
203c8ec8eeaSjose borrego  *
204c8ec8eeaSjose borrego  * 1. The creator of a readonly file can write to/modify the size of the file
205c8ec8eeaSjose borrego  * using the original create fid, even though the file will appear as readonly
206c8ec8eeaSjose borrego  * to all other fids and via a CIFS getattr call.
207c8ec8eeaSjose borrego  *
208c8ec8eeaSjose borrego  * 2. A setinfo operation (using either an open fid or a path) to set/unset
209c8ec8eeaSjose borrego  * readonly will be successful regardless of whether a creator of a readonly
2105cb2894aSGordon Ross  * file has an open fid.
211c8ec8eeaSjose borrego  *
212c8ec8eeaSjose borrego  * 3. The DOS readonly bit affects only data and some metadata.
213c8ec8eeaSjose borrego  * The following metadata can be changed regardless of the readonly bit:
214c8ec8eeaSjose borrego  *	- security descriptors
215c8ec8eeaSjose borrego  *	- DOS attributes
216c8ec8eeaSjose borrego  *	- timestamps
217c8ec8eeaSjose borrego  *
218c8ec8eeaSjose borrego  * In the current implementation, the file size cannot be changed (except for
219c8ec8eeaSjose borrego  * the exceptions in #1 and #2, above).
2202c1b14e5Sjose borrego  *
2212c1b14e5Sjose borrego  *
2222c1b14e5Sjose borrego  * DOS attribute rules
2232c1b14e5Sjose borrego  *
2242c1b14e5Sjose borrego  * These rules are specific to creating / opening files and directories.
2252c1b14e5Sjose borrego  * How the attribute value (specifically ZERO or FILE_ATTRIBUTE_NORMAL)
2262c1b14e5Sjose borrego  * should be interpreted may differ in other requests.
2272c1b14e5Sjose borrego  *
2282c1b14e5Sjose borrego  * - An attribute value equal to ZERO or FILE_ATTRIBUTE_NORMAL means that the
2292c1b14e5Sjose borrego  *   file's attributes should be cleared.
2302c1b14e5Sjose borrego  * - If FILE_ATTRIBUTE_NORMAL is specified with any other attributes,
2312c1b14e5Sjose borrego  *   FILE_ATTRIBUTE_NORMAL is ignored.
2322c1b14e5Sjose borrego  *
2332c1b14e5Sjose borrego  * 1. Creating a new file
2342c1b14e5Sjose borrego  * - The request attributes + FILE_ATTRIBUTE_ARCHIVE are applied to the file.
2352c1b14e5Sjose borrego  *
2362c1b14e5Sjose borrego  * 2. Creating a new directory
2372c1b14e5Sjose borrego  * - The request attributes + FILE_ATTRIBUTE_DIRECTORY are applied to the file.
2382c1b14e5Sjose borrego  * - FILE_ATTRIBUTE_ARCHIVE does not get set.
2392c1b14e5Sjose borrego  *
2402c1b14e5Sjose borrego  * 3. Overwriting an existing file
2412c1b14e5Sjose borrego  * - the request attributes are used as search attributes. If the existing
2422c1b14e5Sjose borrego  *   file does not meet the search criteria access is denied.
2432c1b14e5Sjose borrego  * - otherwise, applies attributes + FILE_ATTRIBUTE_ARCHIVE.
2442c1b14e5Sjose borrego  *
2452c1b14e5Sjose borrego  * 4. Opening an existing file or directory
2462c1b14e5Sjose borrego  *    The request attributes are ignored.
247da6c28aaSamw  */
24894047d49SGordon Ross uint32_t
24994047d49SGordon Ross smb_common_open(smb_request_t *sr)
250da6c28aaSamw {
25194047d49SGordon Ross 	smb_server_t	*sv = sr->sr_server;
25294047d49SGordon Ross 	smb_tree_t	*tree = sr->tid_tree;
25394047d49SGordon Ross 	smb_node_t	*fnode = NULL;
2542c2961f8Sjose borrego 	smb_node_t	*dnode = NULL;
255eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	smb_node_t	*cur_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;
27294047d49SGordon Ross 	boolean_t	opening_incr = B_FALSE;
27394047d49SGordon Ross 	boolean_t	dnode_held = B_FALSE;
27494047d49SGordon Ross 	boolean_t	dnode_wlock = B_FALSE;
27594047d49SGordon Ross 	boolean_t	fnode_held = B_FALSE;
27694047d49SGordon Ross 	boolean_t	fnode_wlock = B_FALSE;
27794047d49SGordon Ross 	boolean_t	fnode_shrlk = B_FALSE;
27894047d49SGordon Ross 	boolean_t	did_open = B_FALSE;
27994047d49SGordon Ross 	boolean_t	did_break_handle = B_FALSE;
280*8d94f651SGordon Ross 	boolean_t	did_cleanup_orphans = B_FALSE;
281da6c28aaSamw 
2825677e049SGordon Ross 	/* Get out now if we've been cancelled. */
2835677e049SGordon Ross 	mutex_enter(&sr->sr_mutex);
2845677e049SGordon Ross 	if (sr->sr_state != SMB_REQ_STATE_ACTIVE) {
2855677e049SGordon Ross 		mutex_exit(&sr->sr_mutex);
2865677e049SGordon Ross 		return (NT_STATUS_CANCELLED);
2875677e049SGordon Ross 	}
2885677e049SGordon Ross 	mutex_exit(&sr->sr_mutex);
2895677e049SGordon Ross 
290da6c28aaSamw 	is_dir = (op->create_options & FILE_DIRECTORY_FILE) ? 1 : 0;
291da6c28aaSamw 
292da6c28aaSamw 	/*
293037cac00Sjoyce mcintosh 	 * If the object being created or opened is a directory
294037cac00Sjoyce mcintosh 	 * the Disposition parameter must be one of FILE_CREATE,
295037cac00Sjoyce mcintosh 	 * FILE_OPEN, or FILE_OPEN_IF
296da6c28aaSamw 	 */
297037cac00Sjoyce mcintosh 	if (is_dir) {
298da6c28aaSamw 		if ((op->create_disposition != FILE_CREATE) &&
299da6c28aaSamw 		    (op->create_disposition != FILE_OPEN_IF) &&
300da6c28aaSamw 		    (op->create_disposition != FILE_OPEN)) {
3017b59d02dSjb150015 			return (NT_STATUS_INVALID_PARAMETER);
302da6c28aaSamw 		}
303da6c28aaSamw 	}
304da6c28aaSamw 
305da6c28aaSamw 	if (op->desired_access & MAXIMUM_ALLOWED) {
306da6c28aaSamw 		max_requested = 1;
307da6c28aaSamw 		op->desired_access &= ~MAXIMUM_ALLOWED;
308da6c28aaSamw 	}
309da6c28aaSamw 	op->desired_access = smb_access_generic_to_file(op->desired_access);
310da6c28aaSamw 
31191ca6bffSGordon Ross 	if (sr->session->s_file_cnt >= smb_session_ofile_max) {
312da6c28aaSamw 		ASSERT(sr->uid_user);
313cb174861Sjoyce mcintosh 		cmn_err(CE_NOTE, "smbsrv[%s\\%s]: TOO_MANY_OPENED_FILES",
314148c5f43SAlan Wright 		    sr->uid_user->u_domain, sr->uid_user->u_name);
3157b59d02dSjb150015 		return (NT_STATUS_TOO_MANY_OPENED_FILES);
316da6c28aaSamw 	}
317da6c28aaSamw 
31894047d49SGordon Ross 	if (smb_idpool_alloc(&tree->t_fid_pool, &tree_fid))
31994047d49SGordon Ross 		return (NT_STATUS_TOO_MANY_OPENED_FILES);
32094047d49SGordon Ross 
321da6c28aaSamw 	/* This must be NULL at this point */
322da6c28aaSamw 	sr->fid_ofile = NULL;
323da6c28aaSamw 
324da6c28aaSamw 	op->devstate = 0;
325da6c28aaSamw 
326da6c28aaSamw 	switch (sr->tid_tree->t_res_type & STYPE_MASK) {
327da6c28aaSamw 	case STYPE_DISKTREE:
328f96bd5c8SAlan Wright 	case STYPE_PRINTQ:
329da6c28aaSamw 		break;
330da6c28aaSamw 
331da6c28aaSamw 	case STYPE_IPC:
332a90cf9f2SGordon Ross 		/*
333a90cf9f2SGordon Ross 		 * Security descriptors for pipes are not implemented,
334a90cf9f2SGordon Ross 		 * so just setup a reasonable access mask.
335a90cf9f2SGordon Ross 		 */
336a90cf9f2SGordon Ross 		op->desired_access = (READ_CONTROL | SYNCHRONIZE |
337a90cf9f2SGordon Ross 		    FILE_READ_DATA | FILE_READ_ATTRIBUTES |
338a90cf9f2SGordon Ross 		    FILE_WRITE_DATA | FILE_APPEND_DATA);
339cb174861Sjoyce mcintosh 
340a90cf9f2SGordon Ross 		/*
341a90cf9f2SGordon Ross 		 * Limit the number of open pipe instances.
342a90cf9f2SGordon Ross 		 */
343cb174861Sjoyce mcintosh 		if ((rc = smb_threshold_enter(&sv->sv_opipe_ct)) != 0) {
344cb174861Sjoyce mcintosh 			status = RPC_NT_SERVER_TOO_BUSY;
34594047d49SGordon Ross 			goto errout;
346cb174861Sjoyce mcintosh 		}
347cb174861Sjoyce mcintosh 
348da6c28aaSamw 		/*
34994047d49SGordon Ross 		 * Most of IPC open is handled in smb_opipe_open()
350da6c28aaSamw 		 */
35194047d49SGordon Ross 		op->create_options = 0;
35294047d49SGordon Ross 		of = smb_ofile_alloc(sr, op, NULL, SMB_FTYPE_MESG_PIPE,
353*8d94f651SGordon Ross 		    tree_fid);
35494047d49SGordon Ross 		tree_fid = 0; // given to the ofile
35594047d49SGordon Ross 		status = smb_opipe_open(sr, of);
356856399cfSGordon Ross 		smb_threshold_exit(&sv->sv_opipe_ct);
35794047d49SGordon Ross 		if (status != NT_STATUS_SUCCESS)
35894047d49SGordon Ross 			goto errout;
35994047d49SGordon Ross 		return (NT_STATUS_SUCCESS);
360da6c28aaSamw 
361da6c28aaSamw 	default:
36294047d49SGordon Ross 		status = NT_STATUS_BAD_DEVICE_TYPE;
36394047d49SGordon Ross 		goto errout;
364da6c28aaSamw 	}
365da6c28aaSamw 
366fe1c642dSBill Krier 	smb_pathname_init(sr, pn, pn->pn_path);
36794047d49SGordon Ross 	if (!smb_pathname_validate(sr, pn)) {
36894047d49SGordon Ross 		status = sr->smb_error.status;
36994047d49SGordon Ross 		goto errout;
37094047d49SGordon Ross 	}
371fe1c642dSBill Krier 
372b24e356bSPeer Dampmann 	if (strlen(pn->pn_path) >= SMB_MAXPATHLEN) {
37394047d49SGordon Ross 		status = NT_STATUS_OBJECT_PATH_INVALID;
37494047d49SGordon Ross 		goto errout;
375da6c28aaSamw 	}
376da6c28aaSamw 
377fe1c642dSBill Krier 	if (is_dir) {
37894047d49SGordon Ross 		if (!smb_validate_dirname(sr, pn)) {
37994047d49SGordon Ross 			status = sr->smb_error.status;
38094047d49SGordon Ross 			goto errout;
38194047d49SGordon Ross 		}
382fe1c642dSBill Krier 	} else {
38394047d49SGordon Ross 		if (!smb_validate_object_name(sr, pn)) {
38494047d49SGordon Ross 			status = sr->smb_error.status;
38594047d49SGordon Ross 			goto errout;
38694047d49SGordon Ross 		}
387da6c28aaSamw 	}
388da6c28aaSamw 
389eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	cur_node = op->fqi.fq_dnode ?
390eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	    op->fqi.fq_dnode : sr->tid_tree->t_snode;
391da6c28aaSamw 
392b819cea2SGordon Ross 	rc = smb_pathname_reduce(sr, sr->user_cr, pn->pn_path,
393eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	    sr->tid_tree->t_snode, cur_node, &op->fqi.fq_dnode,
394b819cea2SGordon Ross 	    op->fqi.fq_last_comp);
395b819cea2SGordon Ross 	if (rc != 0) {
39694047d49SGordon Ross 		status = smb_errno2status(rc);
39794047d49SGordon Ross 		goto errout;
398da6c28aaSamw 	}
39994047d49SGordon Ross 	dnode = op->fqi.fq_dnode;
40094047d49SGordon Ross 	dnode_held = B_TRUE;
40194047d49SGordon Ross 
40294047d49SGordon Ross 	/*
40394047d49SGordon Ross 	 * Lock the parent dir node in case another create
40494047d49SGordon Ross 	 * request to the same parent directory comes in.
40594047d49SGordon Ross 	 * Drop this once either lookup succeeds, or we've
40694047d49SGordon Ross 	 * created the object in this directory.
40794047d49SGordon Ross 	 */
40894047d49SGordon Ross 	smb_node_wrlock(dnode);
40994047d49SGordon Ross 	dnode_wlock = B_TRUE;
410da6c28aaSamw 
411da6c28aaSamw 	/*
412da6c28aaSamw 	 * If the access mask has only DELETE set (ignore
413da6c28aaSamw 	 * FILE_READ_ATTRIBUTES), then assume that this
414da6c28aaSamw 	 * is a request to delete the link (if a link)
415da6c28aaSamw 	 * and do not follow links.  Otherwise, follow
416da6c28aaSamw 	 * the link to the target.
417da6c28aaSamw 	 */
418037cac00Sjoyce mcintosh 	if ((op->desired_access & ~FILE_READ_ATTRIBUTES) == DELETE)
419da6c28aaSamw 		lookup_flags &= ~SMB_FOLLOW_LINKS;
420da6c28aaSamw 
4218622ec45SGordon Ross 	rc = smb_fsop_lookup_name(sr, zone_kcred(), lookup_flags,
422eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	    sr->tid_tree->t_snode, op->fqi.fq_dnode, op->fqi.fq_last_comp,
423037cac00Sjoyce mcintosh 	    &op->fqi.fq_fnode);
424da6c28aaSamw 
425da6c28aaSamw 	if (rc == 0) {
426eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		last_comp_found = B_TRUE;
42794047d49SGordon Ross 		fnode_held = B_TRUE;
42894047d49SGordon Ross 
4295fd03bc0SGordon Ross 		/*
4305fd03bc0SGordon Ross 		 * Need the DOS attributes below, where we
4315fd03bc0SGordon Ross 		 * check the search attributes (sattr).
43294047d49SGordon Ross 		 * Also UID, for owner check below.
4335fd03bc0SGordon Ross 		 */
43494047d49SGordon Ross 		op->fqi.fq_fattr.sa_mask = SMB_AT_DOSATTR | SMB_AT_UID;
4358622ec45SGordon Ross 		rc = smb_node_getattr(sr, op->fqi.fq_fnode, zone_kcred(),
4365fd03bc0SGordon Ross 		    NULL, &op->fqi.fq_fattr);
437037cac00Sjoyce mcintosh 		if (rc != 0) {
43894047d49SGordon Ross 			status = NT_STATUS_INTERNAL_ERROR;
43994047d49SGordon Ross 			goto errout;
440037cac00Sjoyce mcintosh 		}
441da6c28aaSamw 	} else if (rc == ENOENT) {
442eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		last_comp_found = B_FALSE;
443eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		op->fqi.fq_fnode = NULL;
444da6c28aaSamw 		rc = 0;
445da6c28aaSamw 	} else {
44694047d49SGordon Ross 		status = smb_errno2status(rc);
44794047d49SGordon Ross 		goto errout;
448da6c28aaSamw 	}
449da6c28aaSamw 
450eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (last_comp_found) {
4516537f381Sas200622 
45294047d49SGordon Ross 		smb_node_unlock(dnode);
45394047d49SGordon Ross 		dnode_wlock = B_FALSE;
45494047d49SGordon Ross 
45594047d49SGordon Ross 		fnode = op->fqi.fq_fnode;
4569fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		dnode = op->fqi.fq_dnode;
4576537f381Sas200622 
45894047d49SGordon Ross 		if (!smb_node_is_file(fnode) &&
45994047d49SGordon Ross 		    !smb_node_is_dir(fnode) &&
46094047d49SGordon Ross 		    !smb_node_is_symlink(fnode)) {
46194047d49SGordon Ross 			status = NT_STATUS_ACCESS_DENIED;
46294047d49SGordon Ross 			goto errout;
4636537f381Sas200622 		}
4646537f381Sas200622 
465da6c28aaSamw 		/*
4662c1b14e5Sjose borrego 		 * Reject this request if either:
4672c1b14e5Sjose borrego 		 * - the target IS a directory and the client requires that
4682c1b14e5Sjose borrego 		 *   it must NOT be (required by Lotus Notes)
4692c1b14e5Sjose borrego 		 * - the target is NOT a directory and client requires that
4702c1b14e5Sjose borrego 		 *   it MUST be.
471da6c28aaSamw 		 */
47294047d49SGordon Ross 		if (smb_node_is_dir(fnode)) {
4732c1b14e5Sjose borrego 			if (op->create_options & FILE_NON_DIRECTORY_FILE) {
47494047d49SGordon Ross 				status = NT_STATUS_FILE_IS_A_DIRECTORY;
47594047d49SGordon Ross 				goto errout;
476da6c28aaSamw 			}
4772c1b14e5Sjose borrego 		} else {
4782c1b14e5Sjose borrego 			if ((op->create_options & FILE_DIRECTORY_FILE) ||
4792c2961f8Sjose borrego 			    (op->nt_flags & NT_CREATE_FLAG_OPEN_TARGET_DIR)) {
48094047d49SGordon Ross 				status = NT_STATUS_NOT_A_DIRECTORY;
48194047d49SGordon Ross 				goto errout;
482da6c28aaSamw 			}
4832c1b14e5Sjose borrego 		}
484da6c28aaSamw 
485da6c28aaSamw 		/*
486da6c28aaSamw 		 * No more open should be accepted when "Delete on close"
487da6c28aaSamw 		 * flag is set.
488da6c28aaSamw 		 */
48994047d49SGordon Ross 		if (fnode->flags & NODE_FLAGS_DELETE_ON_CLOSE) {
49094047d49SGordon Ross 			status = NT_STATUS_DELETE_PENDING;
49194047d49SGordon Ross 			goto errout;
492da6c28aaSamw 		}
493da6c28aaSamw 
494da6c28aaSamw 		/*
495da6c28aaSamw 		 * Specified file already exists so the operation should fail.
496da6c28aaSamw 		 */
497da6c28aaSamw 		if (op->create_disposition == FILE_CREATE) {
49894047d49SGordon Ross 			status = NT_STATUS_OBJECT_NAME_COLLISION;
49994047d49SGordon Ross 			goto errout;
500da6c28aaSamw 		}
501da6c28aaSamw 
502da6c28aaSamw 		/*
503da6c28aaSamw 		 * Windows seems to check read-only access before file
504da6c28aaSamw 		 * sharing check.
505c8ec8eeaSjose borrego 		 *
50694047d49SGordon Ross 		 * Check to see if the file is currently readonly (regardless
507c8ec8eeaSjose borrego 		 * of whether this open will make it readonly).
50894047d49SGordon Ross 		 * Readonly is ignored on directories.
509da6c28aaSamw 		 */
51094047d49SGordon Ross 		if (SMB_PATHFILE_IS_READONLY(sr, fnode) &&
51194047d49SGordon Ross 		    !smb_node_is_dir(fnode)) {
51294047d49SGordon Ross 			if (op->desired_access &
51394047d49SGordon Ross 			    (FILE_WRITE_DATA | FILE_APPEND_DATA)) {
51494047d49SGordon Ross 				status = NT_STATUS_ACCESS_DENIED;
51594047d49SGordon Ross 				goto errout;
516da6c28aaSamw 			}
517b5b772b0SGordon Ross 			if (op->create_options & FILE_DELETE_ON_CLOSE) {
51894047d49SGordon Ross 				status = NT_STATUS_CANNOT_DELETE;
51994047d49SGordon Ross 				goto errout;
520da6c28aaSamw 			}
521da6c28aaSamw 		}
522da6c28aaSamw 
523dc20a302Sas200622 		if ((op->create_disposition == FILE_SUPERSEDE) ||
524dc20a302Sas200622 		    (op->create_disposition == FILE_OVERWRITE_IF) ||
525dc20a302Sas200622 		    (op->create_disposition == FILE_OVERWRITE)) {
526dc20a302Sas200622 
527fb699f1eSAlek Pinchuk 			if (!smb_sattr_check(op->fqi.fq_fattr.sa_dosattr,
528fb699f1eSAlek Pinchuk 			    op->dattr)) {
52994047d49SGordon Ross 				status = NT_STATUS_ACCESS_DENIED;
53094047d49SGordon Ross 				goto errout;
531dc20a302Sas200622 			}
532dc20a302Sas200622 
53394047d49SGordon Ross 			if (smb_node_is_dir(fnode)) {
53494047d49SGordon Ross 				status = NT_STATUS_ACCESS_DENIED;
53594047d49SGordon Ross 				goto errout;
536da6c28aaSamw 			}
537fb699f1eSAlek Pinchuk 		}
538fb699f1eSAlek Pinchuk 
539fb699f1eSAlek Pinchuk 		/* MS-FSA 2.1.5.1.2 */
540fb699f1eSAlek Pinchuk 		if (op->create_disposition == FILE_SUPERSEDE)
541fb699f1eSAlek Pinchuk 			op->desired_access |= DELETE;
542fb699f1eSAlek Pinchuk 		if ((op->create_disposition == FILE_OVERWRITE_IF) ||
543fb699f1eSAlek Pinchuk 		    (op->create_disposition == FILE_OVERWRITE))
544fb699f1eSAlek Pinchuk 			op->desired_access |= FILE_WRITE_DATA;
545da6c28aaSamw 
54694047d49SGordon Ross 		status = smb_fsop_access(sr, sr->user_cr, fnode,
547da6c28aaSamw 		    op->desired_access);
54894047d49SGordon Ross 		if (status != NT_STATUS_SUCCESS)
54994047d49SGordon Ross 			goto errout;
550da6c28aaSamw 
551fb699f1eSAlek Pinchuk 		if (max_requested) {
55294047d49SGordon Ross 			smb_fsop_eaccess(sr, sr->user_cr, fnode, &max_allowed);
553fb699f1eSAlek Pinchuk 			op->desired_access |= max_allowed;
554fb699f1eSAlek Pinchuk 		}
55594047d49SGordon Ross 
55694047d49SGordon Ross 		/*
55794047d49SGordon Ross 		 * File owner should always get read control + read attr.
55894047d49SGordon Ross 		 */
55994047d49SGordon Ross 		if (crgetuid(sr->user_cr) == op->fqi.fq_fattr.sa_vattr.va_uid)
56094047d49SGordon Ross 			op->desired_access |=
56194047d49SGordon Ross 			    (READ_CONTROL | FILE_READ_ATTRIBUTES);
56294047d49SGordon Ross 
563a90cf9f2SGordon Ross 		/*
564a90cf9f2SGordon Ross 		 * According to MS "dochelp" mail in Mar 2015, any handle
565a90cf9f2SGordon Ross 		 * on which read or write access is granted implicitly
566a90cf9f2SGordon Ross 		 * gets "read attributes", even if it was not requested.
567a90cf9f2SGordon Ross 		 */
56894047d49SGordon Ross 		if ((op->desired_access & FILE_DATA_ALL) != 0)
56994047d49SGordon Ross 			op->desired_access |= FILE_READ_ATTRIBUTES;
570fb699f1eSAlek Pinchuk 
571fb699f1eSAlek Pinchuk 		/*
572fb699f1eSAlek Pinchuk 		 * Oplock break is done prior to sharing checks as the break
573fb699f1eSAlek Pinchuk 		 * may cause other clients to close the file which would
57449d83597SMatt Barden 		 * affect the sharing checks, and may delete the file due to
57549d83597SMatt Barden 		 * DELETE_ON_CLOSE. This may block, so set the file opening
57649d83597SMatt Barden 		 * count before oplock stuff.
577*8d94f651SGordon Ross 		 *
578*8d94f651SGordon Ross 		 * Need the "proposed" ofile (and its TargetOplockKey) for
579*8d94f651SGordon Ross 		 * correct oplock break semantics.
580fb699f1eSAlek Pinchuk 		 */
58194047d49SGordon Ross 		of = smb_ofile_alloc(sr, op, fnode, SMB_FTYPE_DISK,
582*8d94f651SGordon Ross 		    tree_fid);
58394047d49SGordon Ross 		tree_fid = 0; // given to the ofile
584*8d94f651SGordon Ross 		uniq_fid = of->f_uniqid;
585fb699f1eSAlek Pinchuk 
58694047d49SGordon Ross 		smb_node_inc_opening_count(fnode);
58794047d49SGordon Ross 		opening_incr = B_TRUE;
58894047d49SGordon Ross 
58949d83597SMatt Barden 		/*
59094047d49SGordon Ross 		 * XXX Supposed to do share access checks next.
59194047d49SGordon Ross 		 * [MS-FSA] describes that as part of access check:
59294047d49SGordon Ross 		 * 2.1.5.1.2.1 Alg... Check Access to an Existing File
59394047d49SGordon Ross 		 *
59494047d49SGordon Ross 		 * If CreateDisposition is FILE_OPEN or FILE_OPEN_IF:
59594047d49SGordon Ross 		 *   If Open.Stream.Oplock is not empty and
59694047d49SGordon Ross 		 *   Open.Stream.Oplock.State contains BATCH_OPLOCK,
59794047d49SGordon Ross 		 *   the object store MUST check for an oplock
59894047d49SGordon Ross 		 *   break according to the algorithm in section 2.1.4.12,
59994047d49SGordon Ross 		 *   with input values as follows:
60094047d49SGordon Ross 		 *	Open equal to this operation's Open
60194047d49SGordon Ross 		 *	Oplock equal to Open.Stream.Oplock
60294047d49SGordon Ross 		 *	Operation equal to "OPEN"
60394047d49SGordon Ross 		 *	OpParams containing two members:
60494047d49SGordon Ross 		 *	  DesiredAccess, CreateDisposition
60594047d49SGordon Ross 		 *
60694047d49SGordon Ross 		 * It's not clear how Windows would ask the FS layer if
60794047d49SGordon Ross 		 * the file has a BATCH oplock.  We'll use a call to the
60894047d49SGordon Ross 		 * common oplock code, which calls smb_oplock_break_OPEN
60994047d49SGordon Ross 		 * only if the oplock state contains BATCH_OPLOCK.
61094047d49SGordon Ross 		 * See: smb_oplock_break_BATCH()
61194047d49SGordon Ross 		 *
61294047d49SGordon Ross 		 * Also note: There's a nearly identical section in the
61394047d49SGordon Ross 		 * spec. at the start of the "else" part of the above
61494047d49SGordon Ross 		 * "if (disposition is overwrite, overwrite_if)" so this
61594047d49SGordon Ross 		 * section (oplock break, the share mode check, and the
61694047d49SGordon Ross 		 * next oplock_break_HANDLE) are all factored out to be
61794047d49SGordon Ross 		 * in all cases above that if/else from the spec.
61849d83597SMatt Barden 		 */
61994047d49SGordon Ross 		status = smb_oplock_break_BATCH(fnode, of,
62094047d49SGordon Ross 		    op->desired_access, op->create_disposition);
62194047d49SGordon Ross 		if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) {
62294047d49SGordon Ross 			if (sr->session->dialect >= SMB_VERS_2_BASE)
62394047d49SGordon Ross 				(void) smb2sr_go_async(sr);
62494047d49SGordon Ross 			(void) smb_oplock_wait_break(fnode, 0);
62594047d49SGordon Ross 			status = 0;
62694047d49SGordon Ross 		}
62794047d49SGordon Ross 		if (status != NT_STATUS_SUCCESS)
62894047d49SGordon Ross 			goto errout;
62994047d49SGordon Ross 
63094047d49SGordon Ross 		/*
63194047d49SGordon Ross 		 * Check for sharing violations, and if any,
63294047d49SGordon Ross 		 * do oplock break of handle caching.
63394047d49SGordon Ross 		 *
63494047d49SGordon Ross 		 * Need node_wrlock during shrlock checks,
63594047d49SGordon Ross 		 * and not locked during oplock breaks etc.
63694047d49SGordon Ross 		 */
63794047d49SGordon Ross 		shrlock_t0 = gethrtime();
63894047d49SGordon Ross 	shrlock_again:
63994047d49SGordon Ross 		smb_node_wrlock(fnode);
64094047d49SGordon Ross 		fnode_wlock = B_TRUE;
64194047d49SGordon Ross 		status = smb_fsop_shrlock(sr->user_cr, fnode, uniq_fid,
64294047d49SGordon Ross 		    op->desired_access, op->share_access);
64394047d49SGordon Ross 		smb_node_unlock(fnode);
64494047d49SGordon Ross 		fnode_wlock = B_FALSE;
64594047d49SGordon Ross 
64694047d49SGordon Ross 		/*
64794047d49SGordon Ross 		 * [MS-FSA] "OPEN_BREAK_H"
64894047d49SGordon Ross 		 * If the (proposed) new open would violate sharing rules,
64994047d49SGordon Ross 		 * indicate an oplock break with OPEN_BREAK_H (to break
65094047d49SGordon Ross 		 * handle level caching rights) then try again.
65194047d49SGordon Ross 		 */
65294047d49SGordon Ross 		if (status == NT_STATUS_SHARING_VIOLATION &&
65394047d49SGordon Ross 		    did_break_handle == B_FALSE) {
65494047d49SGordon Ross 			did_break_handle = B_TRUE;
65594047d49SGordon Ross 
65694047d49SGordon Ross 			status = smb_oplock_break_HANDLE(fnode, of);
65794047d49SGordon Ross 			if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) {
65894047d49SGordon Ross 				if (sr->session->dialect >= SMB_VERS_2_BASE)
65994047d49SGordon Ross 					(void) smb2sr_go_async(sr);
66094047d49SGordon Ross 				(void) smb_oplock_wait_break(fnode, 0);
66194047d49SGordon Ross 				status = 0;
66294047d49SGordon Ross 			} else {
66394047d49SGordon Ross 				/*
66494047d49SGordon Ross 				 * Even when the oplock layer does NOT
66594047d49SGordon Ross 				 * give us the special status indicating
66694047d49SGordon Ross 				 * we should wait, it may have scheduled
66794047d49SGordon Ross 				 * taskq jobs that may close handles.
66894047d49SGordon Ross 				 * Give those a chance to run before we
66994047d49SGordon Ross 				 * check again for sharing violations.
67094047d49SGordon Ross 				 */
67194047d49SGordon Ross 				delay(MSEC_TO_TICK(10));
67294047d49SGordon Ross 			}
67394047d49SGordon Ross 			if (status != NT_STATUS_SUCCESS)
67494047d49SGordon Ross 				goto errout;
67594047d49SGordon Ross 
67694047d49SGordon Ross 			goto shrlock_again;
67749d83597SMatt Barden 		}
67849d83597SMatt Barden 
67994047d49SGordon Ross 		/*
680*8d94f651SGordon Ross 		 * If we still have orphaned durable handles on this file,
681*8d94f651SGordon Ross 		 * let's assume the client has lost interest in those and
682*8d94f651SGordon Ross 		 * close them so they don't cause sharing violations.
683*8d94f651SGordon Ross 		 * See longer comment at smb2_dh_close_my_orphans().
684*8d94f651SGordon Ross 		 */
685*8d94f651SGordon Ross 		if (status == NT_STATUS_SHARING_VIOLATION &&
686*8d94f651SGordon Ross 		    sr->session->dialect >= SMB_VERS_2_BASE &&
687*8d94f651SGordon Ross 		    did_cleanup_orphans == B_FALSE) {
688*8d94f651SGordon Ross 
689*8d94f651SGordon Ross 			did_cleanup_orphans = B_TRUE;
690*8d94f651SGordon Ross 			smb2_dh_close_my_orphans(sr, of);
691*8d94f651SGordon Ross 
692*8d94f651SGordon Ross 			goto shrlock_again;
693*8d94f651SGordon Ross 		}
694*8d94f651SGordon Ross 
695*8d94f651SGordon Ross 		/*
69694047d49SGordon Ross 		 * SMB1 expects a 1 sec. delay before returning a
69794047d49SGordon Ross 		 * sharing violation error.  If breaking oplocks
69894047d49SGordon Ross 		 * above took less than a sec, wait some more.
69994047d49SGordon Ross 		 * See: smbtorture base.defer_open
70094047d49SGordon Ross 		 */
70194047d49SGordon Ross 		if (status == NT_STATUS_SHARING_VIOLATION &&
70294047d49SGordon Ross 		    sr->session->dialect < SMB_VERS_2_BASE) {
70394047d49SGordon Ross 			hrtime_t t1 = shrlock_t0 + NANOSEC;
70494047d49SGordon Ross 			hrtime_t now = gethrtime();
70594047d49SGordon Ross 			if (now < t1) {
70694047d49SGordon Ross 				delay(NSEC_TO_TICK_ROUNDUP(t1 - now));
70794047d49SGordon Ross 			}
70894047d49SGordon Ross 		}
70994047d49SGordon Ross 
71094047d49SGordon Ross 		if (status != NT_STATUS_SUCCESS)
71194047d49SGordon Ross 			goto errout;
71294047d49SGordon Ross 		fnode_shrlk = B_TRUE;
713fb699f1eSAlek Pinchuk 
714fb699f1eSAlek Pinchuk 		/*
71594047d49SGordon Ross 		 * The [MS-FSA] spec. describes this oplock break as
71694047d49SGordon Ross 		 * part of the sharing access checks.  See:
71794047d49SGordon Ross 		 * 2.1.5.1.2.2 Algorithm to Check Sharing Access...
71894047d49SGordon Ross 		 * At the end of the share mode tests described there,
71994047d49SGordon Ross 		 * if it has not returned "sharing violation", it
72094047d49SGordon Ross 		 * specifies a call to the alg. in sec. 2.1.4.12,
72194047d49SGordon Ross 		 * that boils down to: smb_oplock_break_OPEN()
722fb699f1eSAlek Pinchuk 		 */
72394047d49SGordon Ross 		status = smb_oplock_break_OPEN(fnode, of,
72494047d49SGordon Ross 		    op->desired_access,
72594047d49SGordon Ross 		    op->create_disposition);
72694047d49SGordon Ross 		if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) {
72794047d49SGordon Ross 			if (sr->session->dialect >= SMB_VERS_2_BASE)
72894047d49SGordon Ross 				(void) smb2sr_go_async(sr);
72994047d49SGordon Ross 			(void) smb_oplock_wait_break(fnode, 0);
73094047d49SGordon Ross 			status = 0;
73194047d49SGordon Ross 		}
73294047d49SGordon Ross 		if (status != NT_STATUS_SUCCESS)
73394047d49SGordon Ross 			goto errout;
73494047d49SGordon Ross 
73594047d49SGordon Ross 		if ((fnode->flags & NODE_FLAGS_DELETE_COMMITTED) != 0) {
73694047d49SGordon Ross 			/*
73794047d49SGordon Ross 			 * Breaking the oplock caused the file to be deleted,
73894047d49SGordon Ross 			 * so let's bail and pretend the file wasn't found.
73994047d49SGordon Ross 			 * Have to duplicate much of the logic found a the
74094047d49SGordon Ross 			 * "errout" label here.
74194047d49SGordon Ross 			 *
74294047d49SGordon Ross 			 * This code path is exercised by smbtorture
74394047d49SGordon Ross 			 * smb2.durable-open.delete_on_close1
74494047d49SGordon Ross 			 */
74594047d49SGordon Ross 			DTRACE_PROBE1(node_deleted, smb_node_t, fnode);
74694047d49SGordon Ross 			smb_ofile_free(of);
74794047d49SGordon Ross 			of = NULL;
74894047d49SGordon Ross 			last_comp_found = B_FALSE;
74994047d49SGordon Ross 
75094047d49SGordon Ross 			/*
75194047d49SGordon Ross 			 * Get all the holds and locks into the state
75294047d49SGordon Ross 			 * they would have if lookup had failed.
75394047d49SGordon Ross 			 */
75494047d49SGordon Ross 			fnode_shrlk = B_FALSE;
75594047d49SGordon Ross 			smb_fsop_unshrlock(sr->user_cr, fnode, uniq_fid);
75694047d49SGordon Ross 
75794047d49SGordon Ross 			opening_incr = B_FALSE;
75894047d49SGordon Ross 			smb_node_dec_opening_count(fnode);
75994047d49SGordon Ross 
76094047d49SGordon Ross 			fnode_held = B_FALSE;
76194047d49SGordon Ross 			smb_node_release(fnode);
76294047d49SGordon Ross 
76394047d49SGordon Ross 			dnode_wlock = B_TRUE;
76494047d49SGordon Ross 			smb_node_wrlock(dnode);
76594047d49SGordon Ross 
76694047d49SGordon Ross 			goto create;
767da6c28aaSamw 		}
768da6c28aaSamw 
769fb699f1eSAlek Pinchuk 		/*
770fb699f1eSAlek Pinchuk 		 * Go ahead with modifications as necessary.
771fb699f1eSAlek Pinchuk 		 */
772fb699f1eSAlek Pinchuk 		switch (op->create_disposition) {
773fb699f1eSAlek Pinchuk 		case FILE_SUPERSEDE:
774fb699f1eSAlek Pinchuk 		case FILE_OVERWRITE_IF:
775fb699f1eSAlek Pinchuk 		case FILE_OVERWRITE:
776037cac00Sjoyce mcintosh 			op->dattr |= FILE_ATTRIBUTE_ARCHIVE;
7775cb2894aSGordon Ross 			/* Don't apply readonly until smb_set_open_attributes */
778037cac00Sjoyce mcintosh 			if (op->dattr & FILE_ATTRIBUTE_READONLY) {
779037cac00Sjoyce mcintosh 				op->dattr &= ~FILE_ATTRIBUTE_READONLY;
7805cb2894aSGordon Ross 				op->created_readonly = B_TRUE;
781037cac00Sjoyce mcintosh 			}
782037cac00Sjoyce mcintosh 
783a90cf9f2SGordon Ross 			/*
784a90cf9f2SGordon Ross 			 * Truncate the file data here.
785a90cf9f2SGordon Ross 			 * We set alloc_size = op->dsize later,
786a90cf9f2SGordon Ross 			 * after we have an ofile.  See:
787a90cf9f2SGordon Ross 			 * smb_set_open_attributes
788a90cf9f2SGordon Ross 			 */
789dc20a302Sas200622 			bzero(&new_attr, sizeof (new_attr));
790037cac00Sjoyce mcintosh 			new_attr.sa_dosattr = op->dattr;
791a90cf9f2SGordon Ross 			new_attr.sa_vattr.va_size = 0;
792037cac00Sjoyce mcintosh 			new_attr.sa_mask = SMB_AT_DOSATTR | SMB_AT_SIZE;
79394047d49SGordon Ross 			rc = smb_fsop_setattr(sr, sr->user_cr, fnode,
79494047d49SGordon Ross 			    &new_attr);
795037cac00Sjoyce mcintosh 			if (rc != 0) {
79694047d49SGordon Ross 				status = smb_errno2status(rc);
79794047d49SGordon Ross 				goto errout;
798da6c28aaSamw 			}
799da6c28aaSamw 
800da6c28aaSamw 			/*
801037cac00Sjoyce mcintosh 			 * If file is being replaced, remove existing streams
802da6c28aaSamw 			 */
80394047d49SGordon Ross 			if (SMB_IS_STREAM(fnode) == 0) {
804a90cf9f2SGordon Ross 				status = smb_fsop_remove_streams(sr,
80594047d49SGordon Ross 				    sr->user_cr, fnode);
80694047d49SGordon Ross 				if (status != 0)
80794047d49SGordon Ross 					goto errout;
808eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 			}
809da6c28aaSamw 
810da6c28aaSamw 			op->action_taken = SMB_OACT_TRUNCATED;
811da6c28aaSamw 			break;
812da6c28aaSamw 
813da6c28aaSamw 		default:
814da6c28aaSamw 			/*
815da6c28aaSamw 			 * FILE_OPEN or FILE_OPEN_IF.
816da6c28aaSamw 			 */
817a90cf9f2SGordon Ross 			/*
818a90cf9f2SGordon Ross 			 * Ignore any user-specified alloc_size for
819a90cf9f2SGordon Ross 			 * existing files, to avoid truncation in
820a90cf9f2SGordon Ross 			 * smb_set_open_attributes
821a90cf9f2SGordon Ross 			 */
822a90cf9f2SGordon Ross 			op->dsize = 0L;
823da6c28aaSamw 			op->action_taken = SMB_OACT_OPENED;
824da6c28aaSamw 			break;
825da6c28aaSamw 		}
826da6c28aaSamw 	} else {
82749d83597SMatt Barden create:
828da6c28aaSamw 		/* Last component was not found. */
829eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		dnode = op->fqi.fq_dnode;
830da6c28aaSamw 
8317b59d02dSjb150015 		if (is_dir == 0)
832eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 			is_stream = smb_is_stream_name(pn->pn_path);
8337b59d02dSjb150015 
834da6c28aaSamw 		if ((op->create_disposition == FILE_OPEN) ||
835da6c28aaSamw 		    (op->create_disposition == FILE_OVERWRITE)) {
83694047d49SGordon Ross 			status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
83794047d49SGordon Ross 			goto errout;
838da6c28aaSamw 		}
839da6c28aaSamw 
8409fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		if (pn->pn_fname && smb_is_invalid_filename(pn->pn_fname)) {
84194047d49SGordon Ross 			status = NT_STATUS_OBJECT_NAME_INVALID;
84294047d49SGordon Ross 			goto errout;
8432c2961f8Sjose borrego 		}
8442c2961f8Sjose borrego 
845da6c28aaSamw 		/*
846a1096253SGordon Ross 		 * Don't create in directories marked "Delete on close".
847a1096253SGordon Ross 		 */
848a1096253SGordon Ross 		if (dnode->flags & NODE_FLAGS_DELETE_ON_CLOSE) {
84994047d49SGordon Ross 			status = NT_STATUS_DELETE_PENDING;
85094047d49SGordon Ross 			goto errout;
851a1096253SGordon Ross 		}
852a1096253SGordon Ross 
853a1096253SGordon Ross 		/*
8545cb2894aSGordon Ross 		 * Create always sets the DOS attributes, type, and mode
8555cb2894aSGordon Ross 		 * in the if/else below (different for file vs directory).
8565cb2894aSGordon Ross 		 * Don't set the readonly bit until smb_set_open_attributes
8575cb2894aSGordon Ross 		 * or that would prevent this open.  Note that op->dattr
8585cb2894aSGordon Ross 		 * needs to be what smb_set_open_attributes will use,
8595cb2894aSGordon Ross 		 * except for the readonly bit.
8605cb2894aSGordon Ross 		 */
8615cb2894aSGordon Ross 		bzero(&new_attr, sizeof (new_attr));
8625cb2894aSGordon Ross 		new_attr.sa_mask = SMB_AT_DOSATTR | SMB_AT_TYPE | SMB_AT_MODE;
863037cac00Sjoyce mcintosh 		if (op->dattr & FILE_ATTRIBUTE_READONLY) {
864037cac00Sjoyce mcintosh 			op->dattr &= ~FILE_ATTRIBUTE_READONLY;
865037cac00Sjoyce mcintosh 			op->created_readonly = B_TRUE;
866037cac00Sjoyce mcintosh 		}
867037cac00Sjoyce mcintosh 
8685cb2894aSGordon Ross 		/*
8695cb2894aSGordon Ross 		 * SMB create can specify the create time.
8705cb2894aSGordon Ross 		 */
871c8ec8eeaSjose borrego 		if ((op->crtime.tv_sec != 0) &&
872c8ec8eeaSjose borrego 		    (op->crtime.tv_sec != UINT_MAX)) {
873c8ec8eeaSjose borrego 			new_attr.sa_mask |= SMB_AT_CRTIME;
874c8ec8eeaSjose borrego 			new_attr.sa_crtime = op->crtime;
875c8ec8eeaSjose borrego 		}
876c8ec8eeaSjose borrego 
877da6c28aaSamw 		if (is_dir == 0) {
878037cac00Sjoyce mcintosh 			op->dattr |= FILE_ATTRIBUTE_ARCHIVE;
879037cac00Sjoyce mcintosh 			new_attr.sa_dosattr = op->dattr;
880da6c28aaSamw 			new_attr.sa_vattr.va_type = VREG;
8815cb2894aSGordon Ross 			if (is_stream)
8825cb2894aSGordon Ross 				new_attr.sa_vattr.va_mode = S_IRUSR | S_IWUSR;
8835cb2894aSGordon Ross 			else
8845cb2894aSGordon Ross 				new_attr.sa_vattr.va_mode =
8857b59d02dSjb150015 				    S_IRUSR | S_IRGRP | S_IROTH |
8867b59d02dSjb150015 				    S_IWUSR | S_IWGRP | S_IWOTH;
887dc20a302Sas200622 
888a90cf9f2SGordon Ross 			/*
889a90cf9f2SGordon Ross 			 * We set alloc_size = op->dsize later,
890c5f48fa5SGordon Ross 			 * (in smb_set_open_attributes) after we
891c5f48fa5SGordon Ross 			 * have an ofile on which to save that.
892c5f48fa5SGordon Ross 			 *
893c5f48fa5SGordon Ross 			 * Legacy Open&X sets size to alloc_size
894c5f48fa5SGordon Ross 			 * when creating a new file.
895a90cf9f2SGordon Ross 			 */
896c5f48fa5SGordon Ross 			if (sr->smb_com == SMB_COM_OPEN_ANDX) {
897c5f48fa5SGordon Ross 				new_attr.sa_vattr.va_size = op->dsize;
898c5f48fa5SGordon Ross 				new_attr.sa_mask |= SMB_AT_SIZE;
899c5f48fa5SGordon Ross 			}
900dc20a302Sas200622 
901da6c28aaSamw 			rc = smb_fsop_create(sr, sr->user_cr, dnode,
902037cac00Sjoyce mcintosh 			    op->fqi.fq_last_comp, &new_attr, &op->fqi.fq_fnode);
903da6c28aaSamw 		} else {
9043db3f65cSamw 			op->dattr |= FILE_ATTRIBUTE_DIRECTORY;
905037cac00Sjoyce mcintosh 			new_attr.sa_dosattr = op->dattr;
906da6c28aaSamw 			new_attr.sa_vattr.va_type = VDIR;
907da6c28aaSamw 			new_attr.sa_vattr.va_mode = 0777;
908c8ec8eeaSjose borrego 
909da6c28aaSamw 			rc = smb_fsop_mkdir(sr, sr->user_cr, dnode,
910037cac00Sjoyce mcintosh 			    op->fqi.fq_last_comp, &new_attr, &op->fqi.fq_fnode);
91194047d49SGordon Ross 		}
912da6c28aaSamw 		if (rc != 0) {
91394047d49SGordon Ross 			status = smb_errno2status(rc);
91494047d49SGordon Ross 			goto errout;
915da6c28aaSamw 		}
916dc20a302Sas200622 
917*8d94f651SGordon Ross 		/* Create done. */
91894047d49SGordon Ross 		smb_node_unlock(dnode);
91994047d49SGordon Ross 		dnode_wlock = B_FALSE;
920da6c28aaSamw 
921eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		created = B_TRUE;
922da6c28aaSamw 		op->action_taken = SMB_OACT_CREATED;
923c8ec8eeaSjose borrego 
924*8d94f651SGordon Ross 		/* Note: hold from create */
92594047d49SGordon Ross 		fnode = op->fqi.fq_fnode;
92694047d49SGordon Ross 		fnode_held = B_TRUE;
92794047d49SGordon Ross 
9282c1b14e5Sjose borrego 		if (max_requested) {
92994047d49SGordon Ross 			smb_fsop_eaccess(sr, sr->user_cr, fnode, &max_allowed);
9302c1b14e5Sjose borrego 			op->desired_access |= max_allowed;
9312c1b14e5Sjose borrego 		}
932a90cf9f2SGordon Ross 		/*
933245d1332SMatt Barden 		 * We created this object (we own it) so grant
934245d1332SMatt Barden 		 * read_control + read_attributes on this handle,
935a90cf9f2SGordon Ross 		 * even if that was not requested.  This avoids
936245d1332SMatt Barden 		 * unexpected access failures later.
937a90cf9f2SGordon Ross 		 */
938245d1332SMatt Barden 		op->desired_access |= (READ_CONTROL | FILE_READ_ATTRIBUTES);
9392c1b14e5Sjose borrego 
940*8d94f651SGordon Ross 		/* Allocate the ofile and fill in most of it. */
941*8d94f651SGordon Ross 		of = smb_ofile_alloc(sr, op, fnode, SMB_FTYPE_DISK,
942*8d94f651SGordon Ross 		    tree_fid);
943*8d94f651SGordon Ross 		tree_fid = 0; // given to the ofile
944*8d94f651SGordon Ross 		uniq_fid = of->f_uniqid;
945*8d94f651SGordon Ross 
946*8d94f651SGordon Ross 		smb_node_inc_opening_count(fnode);
947*8d94f651SGordon Ross 		opening_incr = B_TRUE;
948*8d94f651SGordon Ross 
949*8d94f651SGordon Ross 		/*
950*8d94f651SGordon Ross 		 * Share access checks...
951*8d94f651SGordon Ross 		 */
952*8d94f651SGordon Ross 		smb_node_wrlock(fnode);
953*8d94f651SGordon Ross 		fnode_wlock = B_TRUE;
954*8d94f651SGordon Ross 
955*8d94f651SGordon Ross 		status = smb_fsop_shrlock(sr->user_cr, fnode, uniq_fid,
956*8d94f651SGordon Ross 		    op->desired_access, op->share_access);
957*8d94f651SGordon Ross 		if (status != 0)
958*8d94f651SGordon Ross 			goto errout;
959*8d94f651SGordon Ross 		fnode_shrlk = B_TRUE;
960*8d94f651SGordon Ross 
96194047d49SGordon Ross 		/*
96294047d49SGordon Ross 		 * MS-FSA 2.1.5.1.1
96394047d49SGordon Ross 		 * If the Oplock member of the DirectoryStream in
96494047d49SGordon Ross 		 * Link.ParentFile.StreamList (ParentOplock) is
96594047d49SGordon Ross 		 * not empty ... oplock break on the parent...
96694047d49SGordon Ross 		 * (dnode is the parent directory)
96794047d49SGordon Ross 		 *
96894047d49SGordon Ross 		 * This compares of->ParentOplockKey with each
96994047d49SGordon Ross 		 * oplock of->TargetOplockKey and breaks...
97094047d49SGordon Ross 		 * so it's OK that we're passing an OF that's
97194047d49SGordon Ross 		 * NOT a member of dnode->n_ofile_list
97294047d49SGordon Ross 		 *
97394047d49SGordon Ross 		 * The break never blocks, so ignore the return.
97494047d49SGordon Ross 		 */
97594047d49SGordon Ross 		(void) smb_oplock_break_PARENT(dnode, of);
976da6c28aaSamw 	}
977da6c28aaSamw 
97868b2bbf2SGordon Ross 	/*
97994047d49SGordon Ross 	 * We might have blocked in smb_oplock_break_OPEN long enough
98094047d49SGordon Ross 	 * so a tree disconnect might have happened.  In that case,
98194047d49SGordon Ross 	 * we would be adding an ofile to a tree that's disconnecting,
98294047d49SGordon Ross 	 * which would interfere with tear-down.  If so, error out.
98368b2bbf2SGordon Ross 	 */
98494047d49SGordon Ross 	if (!smb_tree_is_connected(sr->tid_tree)) {
98568b2bbf2SGordon Ross 		status = NT_STATUS_INVALID_PARAMETER;
98694047d49SGordon Ross 		goto errout;
987037cac00Sjoyce mcintosh 	}
988037cac00Sjoyce mcintosh 
989037cac00Sjoyce mcintosh 	/*
99094047d49SGordon Ross 	 * Moved this up from smb_ofile_open()
99194047d49SGordon Ross 	 */
99294047d49SGordon Ross 	if ((rc = smb_fsop_open(fnode, of->f_mode, of->f_cr)) != 0) {
99394047d49SGordon Ross 		status = smb_errno2status(rc);
99494047d49SGordon Ross 		goto errout;
99594047d49SGordon Ross 	}
99694047d49SGordon Ross 
99794047d49SGordon Ross 	/*
99894047d49SGordon Ross 	 * Complete this open (add to ofile lists)
99994047d49SGordon Ross 	 */
100094047d49SGordon Ross 	smb_ofile_open(sr, op, of);
100194047d49SGordon Ross 	did_open = B_TRUE;
100294047d49SGordon Ross 
100394047d49SGordon Ross 	/*
1004037cac00Sjoyce mcintosh 	 * This MUST be done after ofile creation, so that explicitly
10055cb2894aSGordon Ross 	 * set timestamps can be remembered on the ofile, and setting
10065cb2894aSGordon Ross 	 * the readonly flag won't affect access via this open.
1007037cac00Sjoyce mcintosh 	 */
10085fd03bc0SGordon Ross 	if ((rc = smb_set_open_attributes(sr, of)) != 0) {
1009a90cf9f2SGordon Ross 		status = smb_errno2status(rc);
101094047d49SGordon Ross 		goto errout;
1011037cac00Sjoyce mcintosh 	}
1012037cac00Sjoyce mcintosh 
10135fd03bc0SGordon Ross 	/*
10145fd03bc0SGordon Ross 	 * We've already done access checks above,
10155fd03bc0SGordon Ross 	 * and want this call to succeed even when
10165fd03bc0SGordon Ross 	 * !(desired_access & FILE_READ_ATTRIBUTES),
10175fd03bc0SGordon Ross 	 * so pass kcred here.
10185fd03bc0SGordon Ross 	 */
10195fd03bc0SGordon Ross 	op->fqi.fq_fattr.sa_mask = SMB_AT_ALL;
102094047d49SGordon Ross 	(void) smb_node_getattr(sr, fnode, zone_kcred(), of,
10215fd03bc0SGordon Ross 	    &op->fqi.fq_fattr);
10228b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 
10238c10a865Sas200622 	/*
10248c10a865Sas200622 	 * Propagate the write-through mode from the open params
10258c10a865Sas200622 	 * to the node: see the notes in the function header.
102694047d49SGordon Ross 	 * XXX: write_through should be a flag on the ofile.
10278c10a865Sas200622 	 */
10288c10a865Sas200622 	if (sr->sr_cfg->skc_sync_enable ||
10298c10a865Sas200622 	    (op->create_options & FILE_WRITE_THROUGH))
103094047d49SGordon Ross 		fnode->flags |= NODE_FLAGS_WRITE_THROUGH;
10318c10a865Sas200622 
1032037cac00Sjoyce mcintosh 	/*
1033037cac00Sjoyce mcintosh 	 * Set up the fileid and dosattr in open_param for response
1034037cac00Sjoyce mcintosh 	 */
1035eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	op->fileid = op->fqi.fq_fattr.sa_vattr.va_nodeid;
1036037cac00Sjoyce mcintosh 	op->dattr = op->fqi.fq_fattr.sa_dosattr;
10378c10a865Sas200622 
1038da6c28aaSamw 	/*
1039da6c28aaSamw 	 * Set up the file type in open_param for the response
1040da6c28aaSamw 	 */
1041da6c28aaSamw 	op->ftype = SMB_FTYPE_DISK;
1042da6c28aaSamw 	sr->smb_fid = of->f_fid;
1043da6c28aaSamw 	sr->fid_ofile = of;
1044da6c28aaSamw 
104594047d49SGordon Ross 	if (smb_node_is_file(fnode)) {
1046eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		op->dsize = op->fqi.fq_fattr.sa_vattr.va_size;
10479fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	} else {
10489fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		/* directory or symlink */
10492c2961f8Sjose borrego 		op->dsize = 0;
10502c2961f8Sjose borrego 	}
1051dc20a302Sas200622 
105294047d49SGordon Ross 	/*
105394047d49SGordon Ross 	 * Note: oplock_acquire happens in callers, because
105494047d49SGordon Ross 	 * how that happens is protocol-specific.
105594047d49SGordon Ross 	 */
1056cb174861Sjoyce mcintosh 
105794047d49SGordon Ross 	if (fnode_wlock)
105894047d49SGordon Ross 		smb_node_unlock(fnode);
105994047d49SGordon Ross 	if (opening_incr)
106094047d49SGordon Ross 		smb_node_dec_opening_count(fnode);
106194047d49SGordon Ross 	if (fnode_held)
106294047d49SGordon Ross 		smb_node_release(fnode);
106394047d49SGordon Ross 	if (dnode_wlock)
1064cb174861Sjoyce mcintosh 		smb_node_unlock(dnode);
106594047d49SGordon Ross 	if (dnode_held)
1066da6c28aaSamw 		smb_node_release(dnode);
1067da6c28aaSamw 
1068da6c28aaSamw 	return (NT_STATUS_SUCCESS);
106994047d49SGordon Ross 
107094047d49SGordon Ross errout:
107194047d49SGordon Ross 	if (did_open) {
107294047d49SGordon Ross 		smb_ofile_close(of, 0);
1073*8d94f651SGordon Ross 		/* rele via sr->fid_ofile */
107494047d49SGordon Ross 	} else if (of != NULL) {
1075*8d94f651SGordon Ross 		/* No other refs possible */
107694047d49SGordon Ross 		smb_ofile_free(of);
1077da6c28aaSamw 	}
1078da6c28aaSamw 
107994047d49SGordon Ross 	if (fnode_shrlk)
108094047d49SGordon Ross 		smb_fsop_unshrlock(sr->user_cr, fnode, uniq_fid);
1081148d1a41SMatt Barden 
108294047d49SGordon Ross 	if (created) {
108394047d49SGordon Ross 		/* Try to roll-back create. */
108494047d49SGordon Ross 		smb_delete_new_object(sr);
1085148d1a41SMatt Barden 	}
1086148d1a41SMatt Barden 
108794047d49SGordon Ross 	if (fnode_wlock)
108894047d49SGordon Ross 		smb_node_unlock(fnode);
108994047d49SGordon Ross 	if (opening_incr)
109094047d49SGordon Ross 		smb_node_dec_opening_count(fnode);
109194047d49SGordon Ross 	if (fnode_held)
109294047d49SGordon Ross 		smb_node_release(fnode);
109394047d49SGordon Ross 	if (dnode_wlock)
109494047d49SGordon Ross 		smb_node_unlock(dnode);
109594047d49SGordon Ross 	if (dnode_held)
109694047d49SGordon Ross 		smb_node_release(dnode);
1097cb174861Sjoyce mcintosh 
109894047d49SGordon Ross 	if (tree_fid != 0)
109994047d49SGordon Ross 		smb_idpool_free(&tree->t_fid_pool, tree_fid);
1100cb174861Sjoyce mcintosh 
110194047d49SGordon Ross 	return (status);
1102cb174861Sjoyce mcintosh }
11035fd03bc0SGordon Ross 
1104cb174861Sjoyce mcintosh /*
11055fd03bc0SGordon Ross  * smb_set_open_attributes
1106037cac00Sjoyce mcintosh  *
1107037cac00Sjoyce mcintosh  * Last write time:
1108037cac00Sjoyce mcintosh  * - If the last_write time specified in the open params is not 0 or -1,
1109037cac00Sjoyce mcintosh  *   use it as file's mtime. This will be considered an explicitly set
1110037cac00Sjoyce mcintosh  *   timestamps, not reset by subsequent writes.
1111037cac00Sjoyce mcintosh  *
11125fd03bc0SGordon Ross  * DOS attributes
11135fd03bc0SGordon Ross  * - If we created_readonly, we now store the real DOS attributes
11145fd03bc0SGordon Ross  *   (including the readonly bit) so subsequent opens will see it.
1115037cac00Sjoyce mcintosh  *
1116037cac00Sjoyce mcintosh  * Returns: errno
1117037cac00Sjoyce mcintosh  */
1118037cac00Sjoyce mcintosh static int
11195fd03bc0SGordon Ross smb_set_open_attributes(smb_request_t *sr, smb_ofile_t *of)
1120037cac00Sjoyce mcintosh {
11215fd03bc0SGordon Ross 	smb_attr_t	attr;
1122148c5f43SAlan Wright 	smb_arg_open_t	*op = &sr->sr_open;
1123037cac00Sjoyce mcintosh 	smb_node_t	*node = of->f_node;
11245fd03bc0SGordon Ross 	int		rc = 0;
1125037cac00Sjoyce mcintosh 
1126037cac00Sjoyce mcintosh 	bzero(&attr, sizeof (smb_attr_t));
11275fd03bc0SGordon Ross 
11285fd03bc0SGordon Ross 	if (op->created_readonly) {
11295fd03bc0SGordon Ross 		attr.sa_dosattr = op->dattr | FILE_ATTRIBUTE_READONLY;
11305fd03bc0SGordon Ross 		attr.sa_mask |= SMB_AT_DOSATTR;
11315fd03bc0SGordon Ross 	}
1132037cac00Sjoyce mcintosh 
1133a90cf9f2SGordon Ross 	if (op->dsize != 0) {
1134a90cf9f2SGordon Ross 		attr.sa_allocsz = op->dsize;
1135a90cf9f2SGordon Ross 		attr.sa_mask |= SMB_AT_ALLOCSZ;
1136a90cf9f2SGordon Ross 	}
1137a90cf9f2SGordon Ross 
1138037cac00Sjoyce mcintosh 	if ((op->mtime.tv_sec != 0) && (op->mtime.tv_sec != UINT_MAX)) {
1139037cac00Sjoyce mcintosh 		attr.sa_vattr.va_mtime = op->mtime;
11405fd03bc0SGordon Ross 		attr.sa_mask |= SMB_AT_MTIME;
1141037cac00Sjoyce mcintosh 	}
1142037cac00Sjoyce mcintosh 
11435fd03bc0SGordon Ross 	/*
11445fd03bc0SGordon Ross 	 * Used to have code here to set mtime, ctime, atime
11455fd03bc0SGordon Ross 	 * when the open op->create_disposition is any of:
11465fd03bc0SGordon Ross 	 * FILE_SUPERSEDE, FILE_OVERWRITE_IF, FILE_OVERWRITE.
11475fd03bc0SGordon Ross 	 * We know that in those cases we will have set the
11485fd03bc0SGordon Ross 	 * file size, in which case the file system will
11495fd03bc0SGordon Ross 	 * update those times, so we don't have to.
11505fd03bc0SGordon Ross 	 *
11515fd03bc0SGordon Ross 	 * However, keep track of the fact that we modified
11525fd03bc0SGordon Ross 	 * the file via this handle, so we can do the evil,
11535fd03bc0SGordon Ross 	 * gratuitious mtime update on close that Windows
11545cb2894aSGordon Ross 	 * clients expect.
11555fd03bc0SGordon Ross 	 */
11565fd03bc0SGordon Ross 	if (op->action_taken == SMB_OACT_TRUNCATED)
11575fd03bc0SGordon Ross 		of->f_written = B_TRUE;
1158037cac00Sjoyce mcintosh 
11595fd03bc0SGordon Ross 	if (attr.sa_mask != 0)
11605fd03bc0SGordon Ross 		rc = smb_node_setattr(sr, node, of->f_cr, of, &attr);
1161037cac00Sjoyce mcintosh 
1162037cac00Sjoyce mcintosh 	return (rc);
1163037cac00Sjoyce mcintosh }
1164037cac00Sjoyce mcintosh 
1165037cac00Sjoyce mcintosh /*
11668b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States  * This function is used to delete a newly created object (file or
11678b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States  * directory) if an error occurs after creation of the object.
1168da6c28aaSamw  */
11698b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States static void
11708b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States smb_delete_new_object(smb_request_t *sr)
1171da6c28aaSamw {
1172148c5f43SAlan Wright 	smb_arg_open_t	*op = &sr->sr_open;
11738b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	smb_fqi_t	*fqi = &(op->fqi);
11748b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	uint32_t	flags = 0;
11758b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 
11768b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (SMB_TREE_IS_CASEINSENSITIVE(sr))
11778b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		flags |= SMB_IGNORE_CASE;
11788b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (SMB_TREE_SUPPORTS_CATIA(sr))
11798b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		flags |= SMB_CATIA;
11808b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 
11818b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (op->create_options & FILE_DIRECTORY_FILE)
1182eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		(void) smb_fsop_rmdir(sr, sr->user_cr, fqi->fq_dnode,
1183eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		    fqi->fq_last_comp, flags);
11848b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	else
1185eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		(void) smb_fsop_remove(sr, sr->user_cr, fqi->fq_dnode,
1186eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		    fqi->fq_last_comp, flags);
1187eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States }
1188