xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_common_open.c (revision 94047d49916b669576decf2f622a1ee718646882)
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 
4368b2bbf2SGordon Ross static volatile uint32_t smb_fids = 0;
44b819cea2SGordon Ross #define	SMB_UNIQ_FID()	atomic_inc_32_nv(&smb_fids)
457b59d02dSjb150015 
46faa1795aSjb150015 extern uint32_t smb_is_executable(char *);
478b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States static void smb_delete_new_object(smb_request_t *);
485fd03bc0SGordon Ross static int smb_set_open_attributes(smb_request_t *, smb_ofile_t *);
49da6c28aaSamw 
50da6c28aaSamw /*
51da6c28aaSamw  * smb_access_generic_to_file
52da6c28aaSamw  *
53da6c28aaSamw  * Search MSDN for IoCreateFile to see following mapping.
54da6c28aaSamw  *
55da6c28aaSamw  * GENERIC_READ		STANDARD_RIGHTS_READ, FILE_READ_DATA,
56da6c28aaSamw  *			FILE_READ_ATTRIBUTES and FILE_READ_EA
57da6c28aaSamw  *
58da6c28aaSamw  * GENERIC_WRITE	STANDARD_RIGHTS_WRITE, FILE_WRITE_DATA,
59da6c28aaSamw  *               FILE_WRITE_ATTRIBUTES, FILE_WRITE_EA, and FILE_APPEND_DATA
60da6c28aaSamw  *
61da6c28aaSamw  * GENERIC_EXECUTE	STANDARD_RIGHTS_EXECUTE, SYNCHRONIZE, and FILE_EXECUTE.
62da6c28aaSamw  */
63a90cf9f2SGordon Ross static uint32_t
64da6c28aaSamw smb_access_generic_to_file(uint32_t desired_access)
65da6c28aaSamw {
66a90cf9f2SGordon Ross 	uint32_t access = 0;
67da6c28aaSamw 
68da6c28aaSamw 	if (desired_access & GENERIC_ALL)
69da6c28aaSamw 		return (FILE_ALL_ACCESS & ~SYNCHRONIZE);
70da6c28aaSamw 
71da6c28aaSamw 	if (desired_access & GENERIC_EXECUTE) {
72da6c28aaSamw 		desired_access &= ~GENERIC_EXECUTE;
73da6c28aaSamw 		access |= (STANDARD_RIGHTS_EXECUTE |
74da6c28aaSamw 		    SYNCHRONIZE | FILE_EXECUTE);
75da6c28aaSamw 	}
76da6c28aaSamw 
77da6c28aaSamw 	if (desired_access & GENERIC_WRITE) {
78da6c28aaSamw 		desired_access &= ~GENERIC_WRITE;
79da6c28aaSamw 		access |= (FILE_GENERIC_WRITE & ~SYNCHRONIZE);
80da6c28aaSamw 	}
81da6c28aaSamw 
82da6c28aaSamw 	if (desired_access & GENERIC_READ) {
83da6c28aaSamw 		desired_access &= ~GENERIC_READ;
84da6c28aaSamw 		access |= FILE_GENERIC_READ;
85da6c28aaSamw 	}
86da6c28aaSamw 
87da6c28aaSamw 	return (access | desired_access);
88da6c28aaSamw }
89da6c28aaSamw 
90da6c28aaSamw /*
91da6c28aaSamw  * smb_omode_to_amask
92da6c28aaSamw  *
93da6c28aaSamw  * This function converts open modes used by Open and Open AndX
94da6c28aaSamw  * commands to desired access bits used by NT Create AndX command.
95da6c28aaSamw  */
96da6c28aaSamw uint32_t
97da6c28aaSamw smb_omode_to_amask(uint32_t desired_access)
98da6c28aaSamw {
99da6c28aaSamw 	switch (desired_access & SMB_DA_ACCESS_MASK) {
100da6c28aaSamw 	case SMB_DA_ACCESS_READ:
101da6c28aaSamw 		return (FILE_GENERIC_READ);
102da6c28aaSamw 
103da6c28aaSamw 	case SMB_DA_ACCESS_WRITE:
104da6c28aaSamw 		return (FILE_GENERIC_WRITE);
105da6c28aaSamw 
106da6c28aaSamw 	case SMB_DA_ACCESS_READ_WRITE:
107da6c28aaSamw 		return (FILE_GENERIC_READ | FILE_GENERIC_WRITE);
108da6c28aaSamw 
109da6c28aaSamw 	case SMB_DA_ACCESS_EXECUTE:
110c5f48fa5SGordon Ross 		return (FILE_GENERIC_READ | FILE_GENERIC_EXECUTE);
111da6c28aaSamw 
1122c2961f8Sjose borrego 	default:
1132c2961f8Sjose borrego 		return (FILE_GENERIC_ALL);
1142c2961f8Sjose borrego 	}
115da6c28aaSamw }
116da6c28aaSamw 
117da6c28aaSamw /*
118da6c28aaSamw  * smb_denymode_to_sharemode
119da6c28aaSamw  *
120da6c28aaSamw  * This function converts deny modes used by Open and Open AndX
121da6c28aaSamw  * commands to share access bits used by NT Create AndX command.
122da6c28aaSamw  */
123da6c28aaSamw uint32_t
124da6c28aaSamw smb_denymode_to_sharemode(uint32_t desired_access, char *fname)
125da6c28aaSamw {
126da6c28aaSamw 	switch (desired_access & SMB_DA_SHARE_MASK) {
127da6c28aaSamw 	case SMB_DA_SHARE_COMPATIBILITY:
128da6c28aaSamw 		if (smb_is_executable(fname))
129da6c28aaSamw 			return (FILE_SHARE_READ | FILE_SHARE_WRITE);
130c8ec8eeaSjose borrego 
131c8ec8eeaSjose borrego 		return (FILE_SHARE_ALL);
132da6c28aaSamw 
133da6c28aaSamw 	case SMB_DA_SHARE_EXCLUSIVE:
134da6c28aaSamw 		return (FILE_SHARE_NONE);
135da6c28aaSamw 
136da6c28aaSamw 	case SMB_DA_SHARE_DENY_WRITE:
137da6c28aaSamw 		return (FILE_SHARE_READ);
138da6c28aaSamw 
139da6c28aaSamw 	case SMB_DA_SHARE_DENY_READ:
140da6c28aaSamw 		return (FILE_SHARE_WRITE);
141da6c28aaSamw 
142da6c28aaSamw 	case SMB_DA_SHARE_DENY_NONE:
1432c2961f8Sjose borrego 	default:
144da6c28aaSamw 		return (FILE_SHARE_READ | FILE_SHARE_WRITE);
145da6c28aaSamw 	}
146da6c28aaSamw }
147da6c28aaSamw 
148da6c28aaSamw /*
149da6c28aaSamw  * smb_ofun_to_crdisposition
150da6c28aaSamw  *
151da6c28aaSamw  * This function converts open function values used by Open and Open AndX
152da6c28aaSamw  * commands to create disposition values used by NT Create AndX command.
153da6c28aaSamw  */
154da6c28aaSamw uint32_t
155da6c28aaSamw smb_ofun_to_crdisposition(uint16_t  ofun)
156da6c28aaSamw {
157da6c28aaSamw 	static int ofun_cr_map[3][2] =
158da6c28aaSamw 	{
159da6c28aaSamw 		{ -1,			FILE_CREATE },
160da6c28aaSamw 		{ FILE_OPEN,		FILE_OPEN_IF },
161da6c28aaSamw 		{ FILE_OVERWRITE,	FILE_OVERWRITE_IF }
162da6c28aaSamw 	};
163da6c28aaSamw 
164da6c28aaSamw 	int row = ofun & SMB_OFUN_OPEN_MASK;
165da6c28aaSamw 	int col = (ofun & SMB_OFUN_CREATE_MASK) >> 4;
166da6c28aaSamw 
167da6c28aaSamw 	if (row == 3)
1682c2961f8Sjose borrego 		return (FILE_MAXIMUM_DISPOSITION + 1);
169da6c28aaSamw 
170da6c28aaSamw 	return (ofun_cr_map[row][col]);
171da6c28aaSamw }
172da6c28aaSamw 
173da6c28aaSamw /*
174*94047d49SGordon Ross  * smb_common_open
175da6c28aaSamw  *
176da6c28aaSamw  * Notes on write-through behaviour. It looks like pre-LM0.12 versions
177da6c28aaSamw  * of the protocol specify the write-through mode when a file is opened,
178da6c28aaSamw  * (SmbOpen, SmbOpenAndX) so the write calls (SmbWrite, SmbWriteAndClose,
179da6c28aaSamw  * SmbWriteAndUnlock) don't need to contain a write-through flag.
180da6c28aaSamw  *
181da6c28aaSamw  * With LM0.12, the open calls (SmbCreateAndX, SmbNtTransactCreate)
182da6c28aaSamw  * don't indicate which write-through mode to use. Instead the write
183da6c28aaSamw  * calls (SmbWriteAndX, SmbWriteRaw) specify the mode on a per call
184da6c28aaSamw  * basis.
185da6c28aaSamw  *
186da6c28aaSamw  * We don't care which open call was used to get us here, we just need
187da6c28aaSamw  * to ensure that the write-through mode flag is copied from the open
188da6c28aaSamw  * parameters to the node. We test the omode write-through flag in all
189da6c28aaSamw  * write functions.
190da6c28aaSamw  *
191a90cf9f2SGordon Ross  * This function returns NT status codes.
1928c10a865Sas200622  *
1938c10a865Sas200622  * The following rules apply when processing a file open request:
1948c10a865Sas200622  *
195cb174861Sjoyce mcintosh  * - Oplocks must be broken prior to share checking as the break may
196cb174861Sjoyce mcintosh  *   cause other clients to close the file, which would affect sharing
197cb174861Sjoyce mcintosh  *   checks.
1988c10a865Sas200622  *
1998c10a865Sas200622  * - Share checks must take place prior to access checks for correct
2008c10a865Sas200622  * Windows semantics and to prevent unnecessary NFS delegation recalls.
2018c10a865Sas200622  *
2028c10a865Sas200622  * - Oplocks must be acquired after open to ensure the correct
2038c10a865Sas200622  * synchronization with NFS delegation and FEM installation.
204c8ec8eeaSjose borrego  *
205c8ec8eeaSjose borrego  * DOS readonly bit rules
206c8ec8eeaSjose borrego  *
207c8ec8eeaSjose borrego  * 1. The creator of a readonly file can write to/modify the size of the file
208c8ec8eeaSjose borrego  * using the original create fid, even though the file will appear as readonly
209c8ec8eeaSjose borrego  * to all other fids and via a CIFS getattr call.
210c8ec8eeaSjose borrego  *
211c8ec8eeaSjose borrego  * 2. A setinfo operation (using either an open fid or a path) to set/unset
212c8ec8eeaSjose borrego  * readonly will be successful regardless of whether a creator of a readonly
2135cb2894aSGordon Ross  * file has an open fid.
214c8ec8eeaSjose borrego  *
215c8ec8eeaSjose borrego  * 3. The DOS readonly bit affects only data and some metadata.
216c8ec8eeaSjose borrego  * The following metadata can be changed regardless of the readonly bit:
217c8ec8eeaSjose borrego  *	- security descriptors
218c8ec8eeaSjose borrego  *	- DOS attributes
219c8ec8eeaSjose borrego  *	- timestamps
220c8ec8eeaSjose borrego  *
221c8ec8eeaSjose borrego  * In the current implementation, the file size cannot be changed (except for
222c8ec8eeaSjose borrego  * the exceptions in #1 and #2, above).
2232c1b14e5Sjose borrego  *
2242c1b14e5Sjose borrego  *
2252c1b14e5Sjose borrego  * DOS attribute rules
2262c1b14e5Sjose borrego  *
2272c1b14e5Sjose borrego  * These rules are specific to creating / opening files and directories.
2282c1b14e5Sjose borrego  * How the attribute value (specifically ZERO or FILE_ATTRIBUTE_NORMAL)
2292c1b14e5Sjose borrego  * should be interpreted may differ in other requests.
2302c1b14e5Sjose borrego  *
2312c1b14e5Sjose borrego  * - An attribute value equal to ZERO or FILE_ATTRIBUTE_NORMAL means that the
2322c1b14e5Sjose borrego  *   file's attributes should be cleared.
2332c1b14e5Sjose borrego  * - If FILE_ATTRIBUTE_NORMAL is specified with any other attributes,
2342c1b14e5Sjose borrego  *   FILE_ATTRIBUTE_NORMAL is ignored.
2352c1b14e5Sjose borrego  *
2362c1b14e5Sjose borrego  * 1. Creating a new file
2372c1b14e5Sjose borrego  * - The request attributes + FILE_ATTRIBUTE_ARCHIVE are applied to the file.
2382c1b14e5Sjose borrego  *
2392c1b14e5Sjose borrego  * 2. Creating a new directory
2402c1b14e5Sjose borrego  * - The request attributes + FILE_ATTRIBUTE_DIRECTORY are applied to the file.
2412c1b14e5Sjose borrego  * - FILE_ATTRIBUTE_ARCHIVE does not get set.
2422c1b14e5Sjose borrego  *
2432c1b14e5Sjose borrego  * 3. Overwriting an existing file
2442c1b14e5Sjose borrego  * - the request attributes are used as search attributes. If the existing
2452c1b14e5Sjose borrego  *   file does not meet the search criteria access is denied.
2462c1b14e5Sjose borrego  * - otherwise, applies attributes + FILE_ATTRIBUTE_ARCHIVE.
2472c1b14e5Sjose borrego  *
2482c1b14e5Sjose borrego  * 4. Opening an existing file or directory
2492c1b14e5Sjose borrego  *    The request attributes are ignored.
250da6c28aaSamw  */
251*94047d49SGordon Ross uint32_t
252*94047d49SGordon Ross smb_common_open(smb_request_t *sr)
253da6c28aaSamw {
254*94047d49SGordon Ross 	smb_server_t	*sv = sr->sr_server;
255*94047d49SGordon Ross 	smb_tree_t	*tree = sr->tid_tree;
256*94047d49SGordon Ross 	smb_node_t	*fnode = NULL;
2572c2961f8Sjose borrego 	smb_node_t	*dnode = NULL;
258eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	smb_node_t	*cur_node = NULL;
259148c5f43SAlan Wright 	smb_arg_open_t	*op = &sr->sr_open;
260*94047d49SGordon Ross 	smb_pathname_t	*pn = &op->fqi.fq_path;
261*94047d49SGordon Ross 	smb_ofile_t	*of = NULL;
262da6c28aaSamw 	smb_attr_t	new_attr;
263*94047d49SGordon Ross 	hrtime_t	shrlock_t0;
264da6c28aaSamw 	int		max_requested = 0;
265da6c28aaSamw 	uint32_t	max_allowed;
266da6c28aaSamw 	uint32_t	status = NT_STATUS_SUCCESS;
267da6c28aaSamw 	int		is_dir;
268*94047d49SGordon Ross 	int		rc;
2692c2961f8Sjose borrego 	boolean_t	is_stream = B_FALSE;
270da6c28aaSamw 	int		lookup_flags = SMB_FOLLOW_LINKS;
271*94047d49SGordon Ross 	uint32_t	uniq_fid = 0;
272*94047d49SGordon Ross 	uint16_t	tree_fid = 0;
273*94047d49SGordon Ross 	boolean_t	created = B_FALSE;
274*94047d49SGordon Ross 	boolean_t	last_comp_found = B_FALSE;
275*94047d49SGordon Ross 	boolean_t	opening_incr = B_FALSE;
276*94047d49SGordon Ross 	boolean_t	dnode_held = B_FALSE;
277*94047d49SGordon Ross 	boolean_t	dnode_wlock = B_FALSE;
278*94047d49SGordon Ross 	boolean_t	fnode_held = B_FALSE;
279*94047d49SGordon Ross 	boolean_t	fnode_wlock = B_FALSE;
280*94047d49SGordon Ross 	boolean_t	fnode_shrlk = B_FALSE;
281*94047d49SGordon Ross 	boolean_t	did_open = B_FALSE;
282*94047d49SGordon Ross 	boolean_t	did_break_handle = B_FALSE;
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 
31391ca6bffSGordon Ross 	if (sr->session->s_file_cnt >= smb_session_ofile_max) {
314da6c28aaSamw 		ASSERT(sr->uid_user);
315cb174861Sjoyce mcintosh 		cmn_err(CE_NOTE, "smbsrv[%s\\%s]: TOO_MANY_OPENED_FILES",
316148c5f43SAlan Wright 		    sr->uid_user->u_domain, sr->uid_user->u_name);
3177b59d02dSjb150015 		return (NT_STATUS_TOO_MANY_OPENED_FILES);
318da6c28aaSamw 	}
319da6c28aaSamw 
320*94047d49SGordon Ross 	if (smb_idpool_alloc(&tree->t_fid_pool, &tree_fid))
321*94047d49SGordon Ross 		return (NT_STATUS_TOO_MANY_OPENED_FILES);
322*94047d49SGordon Ross 
323da6c28aaSamw 	/* This must be NULL at this point */
324da6c28aaSamw 	sr->fid_ofile = NULL;
325da6c28aaSamw 
326da6c28aaSamw 	op->devstate = 0;
327da6c28aaSamw 
328da6c28aaSamw 	switch (sr->tid_tree->t_res_type & STYPE_MASK) {
329da6c28aaSamw 	case STYPE_DISKTREE:
330f96bd5c8SAlan Wright 	case STYPE_PRINTQ:
331da6c28aaSamw 		break;
332da6c28aaSamw 
333da6c28aaSamw 	case STYPE_IPC:
334a90cf9f2SGordon Ross 		/*
335a90cf9f2SGordon Ross 		 * Security descriptors for pipes are not implemented,
336a90cf9f2SGordon Ross 		 * so just setup a reasonable access mask.
337a90cf9f2SGordon Ross 		 */
338a90cf9f2SGordon Ross 		op->desired_access = (READ_CONTROL | SYNCHRONIZE |
339a90cf9f2SGordon Ross 		    FILE_READ_DATA | FILE_READ_ATTRIBUTES |
340a90cf9f2SGordon Ross 		    FILE_WRITE_DATA | FILE_APPEND_DATA);
341cb174861Sjoyce mcintosh 
342a90cf9f2SGordon Ross 		/*
343a90cf9f2SGordon Ross 		 * Limit the number of open pipe instances.
344a90cf9f2SGordon Ross 		 */
345cb174861Sjoyce mcintosh 		if ((rc = smb_threshold_enter(&sv->sv_opipe_ct)) != 0) {
346cb174861Sjoyce mcintosh 			status = RPC_NT_SERVER_TOO_BUSY;
347*94047d49SGordon Ross 			goto errout;
348cb174861Sjoyce mcintosh 		}
349cb174861Sjoyce mcintosh 
350da6c28aaSamw 		/*
351*94047d49SGordon Ross 		 * Most of IPC open is handled in smb_opipe_open()
352da6c28aaSamw 		 */
35368b2bbf2SGordon Ross 		uniq_fid = SMB_UNIQ_FID();
354*94047d49SGordon Ross 		op->create_options = 0;
355*94047d49SGordon Ross 		of = smb_ofile_alloc(sr, op, NULL, SMB_FTYPE_MESG_PIPE,
356*94047d49SGordon Ross 		    tree_fid, uniq_fid);
357*94047d49SGordon Ross 		tree_fid = 0; // given to the ofile
358*94047d49SGordon Ross 		status = smb_opipe_open(sr, of);
359856399cfSGordon Ross 		smb_threshold_exit(&sv->sv_opipe_ct);
360*94047d49SGordon Ross 		if (status != NT_STATUS_SUCCESS)
361*94047d49SGordon Ross 			goto errout;
362*94047d49SGordon Ross 		return (NT_STATUS_SUCCESS);
363da6c28aaSamw 
364da6c28aaSamw 	default:
365*94047d49SGordon Ross 		status = NT_STATUS_BAD_DEVICE_TYPE;
366*94047d49SGordon Ross 		goto errout;
367da6c28aaSamw 	}
368da6c28aaSamw 
369fe1c642dSBill Krier 	smb_pathname_init(sr, pn, pn->pn_path);
370*94047d49SGordon Ross 	if (!smb_pathname_validate(sr, pn)) {
371*94047d49SGordon Ross 		status = sr->smb_error.status;
372*94047d49SGordon Ross 		goto errout;
373*94047d49SGordon Ross 	}
374fe1c642dSBill Krier 
375b24e356bSPeer Dampmann 	if (strlen(pn->pn_path) >= SMB_MAXPATHLEN) {
376*94047d49SGordon Ross 		status = NT_STATUS_OBJECT_PATH_INVALID;
377*94047d49SGordon Ross 		goto errout;
378da6c28aaSamw 	}
379da6c28aaSamw 
380fe1c642dSBill Krier 	if (is_dir) {
381*94047d49SGordon Ross 		if (!smb_validate_dirname(sr, pn)) {
382*94047d49SGordon Ross 			status = sr->smb_error.status;
383*94047d49SGordon Ross 			goto errout;
384*94047d49SGordon Ross 		}
385fe1c642dSBill Krier 	} else {
386*94047d49SGordon Ross 		if (!smb_validate_object_name(sr, pn)) {
387*94047d49SGordon Ross 			status = sr->smb_error.status;
388*94047d49SGordon Ross 			goto errout;
389*94047d49SGordon 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) {
399*94047d49SGordon Ross 		status = smb_errno2status(rc);
400*94047d49SGordon Ross 		goto errout;
401da6c28aaSamw 	}
402*94047d49SGordon Ross 	dnode = op->fqi.fq_dnode;
403*94047d49SGordon Ross 	dnode_held = B_TRUE;
404*94047d49SGordon Ross 
405*94047d49SGordon Ross 	/*
406*94047d49SGordon Ross 	 * Lock the parent dir node in case another create
407*94047d49SGordon Ross 	 * request to the same parent directory comes in.
408*94047d49SGordon Ross 	 * Drop this once either lookup succeeds, or we've
409*94047d49SGordon Ross 	 * created the object in this directory.
410*94047d49SGordon Ross 	 */
411*94047d49SGordon Ross 	smb_node_wrlock(dnode);
412*94047d49SGordon 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 
4248622ec45SGordon Ross 	rc = smb_fsop_lookup_name(sr, zone_kcred(), lookup_flags,
425eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	    sr->tid_tree->t_snode, op->fqi.fq_dnode, op->fqi.fq_last_comp,
426037cac00Sjoyce mcintosh 	    &op->fqi.fq_fnode);
427da6c28aaSamw 
428da6c28aaSamw 	if (rc == 0) {
429eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		last_comp_found = B_TRUE;
430*94047d49SGordon Ross 		fnode_held = B_TRUE;
431*94047d49SGordon Ross 
4325fd03bc0SGordon Ross 		/*
4335fd03bc0SGordon Ross 		 * Need the DOS attributes below, where we
4345fd03bc0SGordon Ross 		 * check the search attributes (sattr).
435*94047d49SGordon Ross 		 * Also UID, for owner check below.
4365fd03bc0SGordon Ross 		 */
437*94047d49SGordon Ross 		op->fqi.fq_fattr.sa_mask = SMB_AT_DOSATTR | SMB_AT_UID;
4388622ec45SGordon Ross 		rc = smb_node_getattr(sr, op->fqi.fq_fnode, zone_kcred(),
4395fd03bc0SGordon Ross 		    NULL, &op->fqi.fq_fattr);
440037cac00Sjoyce mcintosh 		if (rc != 0) {
441*94047d49SGordon Ross 			status = NT_STATUS_INTERNAL_ERROR;
442*94047d49SGordon Ross 			goto errout;
443037cac00Sjoyce mcintosh 		}
444da6c28aaSamw 	} else if (rc == ENOENT) {
445eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		last_comp_found = B_FALSE;
446eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		op->fqi.fq_fnode = NULL;
447da6c28aaSamw 		rc = 0;
448da6c28aaSamw 	} else {
449*94047d49SGordon Ross 		status = smb_errno2status(rc);
450*94047d49SGordon Ross 		goto errout;
451da6c28aaSamw 	}
452da6c28aaSamw 
453dc20a302Sas200622 	/*
454dc20a302Sas200622 	 * The uniq_fid is a CIFS-server-wide unique identifier for an ofile
455dc20a302Sas200622 	 * which is used to uniquely identify open instances for the
456c8ec8eeaSjose borrego 	 * VFS share reservation and POSIX locks.
457dc20a302Sas200622 	 */
458dc20a302Sas200622 	uniq_fid = SMB_UNIQ_FID();
459dc20a302Sas200622 
460eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (last_comp_found) {
4616537f381Sas200622 
462*94047d49SGordon Ross 		smb_node_unlock(dnode);
463*94047d49SGordon Ross 		dnode_wlock = B_FALSE;
464*94047d49SGordon Ross 
465*94047d49SGordon Ross 		fnode = op->fqi.fq_fnode;
4669fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		dnode = op->fqi.fq_dnode;
4676537f381Sas200622 
468*94047d49SGordon Ross 		if (!smb_node_is_file(fnode) &&
469*94047d49SGordon Ross 		    !smb_node_is_dir(fnode) &&
470*94047d49SGordon Ross 		    !smb_node_is_symlink(fnode)) {
471*94047d49SGordon Ross 			status = NT_STATUS_ACCESS_DENIED;
472*94047d49SGordon Ross 			goto errout;
4736537f381Sas200622 		}
4746537f381Sas200622 
475da6c28aaSamw 		/*
4762c1b14e5Sjose borrego 		 * Reject this request if either:
4772c1b14e5Sjose borrego 		 * - the target IS a directory and the client requires that
4782c1b14e5Sjose borrego 		 *   it must NOT be (required by Lotus Notes)
4792c1b14e5Sjose borrego 		 * - the target is NOT a directory and client requires that
4802c1b14e5Sjose borrego 		 *   it MUST be.
481da6c28aaSamw 		 */
482*94047d49SGordon Ross 		if (smb_node_is_dir(fnode)) {
4832c1b14e5Sjose borrego 			if (op->create_options & FILE_NON_DIRECTORY_FILE) {
484*94047d49SGordon Ross 				status = NT_STATUS_FILE_IS_A_DIRECTORY;
485*94047d49SGordon Ross 				goto errout;
486da6c28aaSamw 			}
4872c1b14e5Sjose borrego 		} else {
4882c1b14e5Sjose borrego 			if ((op->create_options & FILE_DIRECTORY_FILE) ||
4892c2961f8Sjose borrego 			    (op->nt_flags & NT_CREATE_FLAG_OPEN_TARGET_DIR)) {
490*94047d49SGordon Ross 				status = NT_STATUS_NOT_A_DIRECTORY;
491*94047d49SGordon Ross 				goto errout;
492da6c28aaSamw 			}
4932c1b14e5Sjose borrego 		}
494da6c28aaSamw 
495da6c28aaSamw 		/*
496da6c28aaSamw 		 * No more open should be accepted when "Delete on close"
497da6c28aaSamw 		 * flag is set.
498da6c28aaSamw 		 */
499*94047d49SGordon Ross 		if (fnode->flags & NODE_FLAGS_DELETE_ON_CLOSE) {
500*94047d49SGordon Ross 			status = NT_STATUS_DELETE_PENDING;
501*94047d49SGordon Ross 			goto errout;
502da6c28aaSamw 		}
503da6c28aaSamw 
504da6c28aaSamw 		/*
505da6c28aaSamw 		 * Specified file already exists so the operation should fail.
506da6c28aaSamw 		 */
507da6c28aaSamw 		if (op->create_disposition == FILE_CREATE) {
508*94047d49SGordon Ross 			status = NT_STATUS_OBJECT_NAME_COLLISION;
509*94047d49SGordon Ross 			goto errout;
510da6c28aaSamw 		}
511da6c28aaSamw 
512da6c28aaSamw 		/*
513da6c28aaSamw 		 * Windows seems to check read-only access before file
514da6c28aaSamw 		 * sharing check.
515c8ec8eeaSjose borrego 		 *
516*94047d49SGordon Ross 		 * Check to see if the file is currently readonly (regardless
517c8ec8eeaSjose borrego 		 * of whether this open will make it readonly).
518*94047d49SGordon Ross 		 * Readonly is ignored on directories.
519da6c28aaSamw 		 */
520*94047d49SGordon Ross 		if (SMB_PATHFILE_IS_READONLY(sr, fnode) &&
521*94047d49SGordon Ross 		    !smb_node_is_dir(fnode)) {
522*94047d49SGordon Ross 			if (op->desired_access &
523*94047d49SGordon Ross 			    (FILE_WRITE_DATA | FILE_APPEND_DATA)) {
524*94047d49SGordon Ross 				status = NT_STATUS_ACCESS_DENIED;
525*94047d49SGordon Ross 				goto errout;
526da6c28aaSamw 			}
527b5b772b0SGordon Ross 			if (op->create_options & FILE_DELETE_ON_CLOSE) {
528*94047d49SGordon Ross 				status = NT_STATUS_CANNOT_DELETE;
529*94047d49SGordon Ross 				goto errout;
530da6c28aaSamw 			}
531da6c28aaSamw 		}
532da6c28aaSamw 
533dc20a302Sas200622 		if ((op->create_disposition == FILE_SUPERSEDE) ||
534dc20a302Sas200622 		    (op->create_disposition == FILE_OVERWRITE_IF) ||
535dc20a302Sas200622 		    (op->create_disposition == FILE_OVERWRITE)) {
536dc20a302Sas200622 
537fb699f1eSAlek Pinchuk 			if (!smb_sattr_check(op->fqi.fq_fattr.sa_dosattr,
538fb699f1eSAlek Pinchuk 			    op->dattr)) {
539*94047d49SGordon Ross 				status = NT_STATUS_ACCESS_DENIED;
540*94047d49SGordon Ross 				goto errout;
541dc20a302Sas200622 			}
542dc20a302Sas200622 
543*94047d49SGordon Ross 			if (smb_node_is_dir(fnode)) {
544*94047d49SGordon Ross 				status = NT_STATUS_ACCESS_DENIED;
545*94047d49SGordon Ross 				goto errout;
546da6c28aaSamw 			}
547fb699f1eSAlek Pinchuk 		}
548fb699f1eSAlek Pinchuk 
549fb699f1eSAlek Pinchuk 		/* MS-FSA 2.1.5.1.2 */
550fb699f1eSAlek Pinchuk 		if (op->create_disposition == FILE_SUPERSEDE)
551fb699f1eSAlek Pinchuk 			op->desired_access |= DELETE;
552fb699f1eSAlek Pinchuk 		if ((op->create_disposition == FILE_OVERWRITE_IF) ||
553fb699f1eSAlek Pinchuk 		    (op->create_disposition == FILE_OVERWRITE))
554fb699f1eSAlek Pinchuk 			op->desired_access |= FILE_WRITE_DATA;
555da6c28aaSamw 
556*94047d49SGordon Ross 		status = smb_fsop_access(sr, sr->user_cr, fnode,
557da6c28aaSamw 		    op->desired_access);
558*94047d49SGordon Ross 		if (status != NT_STATUS_SUCCESS)
559*94047d49SGordon Ross 			goto errout;
560da6c28aaSamw 
561fb699f1eSAlek Pinchuk 		if (max_requested) {
562*94047d49SGordon Ross 			smb_fsop_eaccess(sr, sr->user_cr, fnode, &max_allowed);
563fb699f1eSAlek Pinchuk 			op->desired_access |= max_allowed;
564fb699f1eSAlek Pinchuk 		}
565*94047d49SGordon Ross 
566*94047d49SGordon Ross 		/*
567*94047d49SGordon Ross 		 * File owner should always get read control + read attr.
568*94047d49SGordon Ross 		 */
569*94047d49SGordon Ross 		if (crgetuid(sr->user_cr) == op->fqi.fq_fattr.sa_vattr.va_uid)
570*94047d49SGordon Ross 			op->desired_access |=
571*94047d49SGordon Ross 			    (READ_CONTROL | FILE_READ_ATTRIBUTES);
572*94047d49SGordon Ross 
573a90cf9f2SGordon Ross 		/*
574a90cf9f2SGordon Ross 		 * According to MS "dochelp" mail in Mar 2015, any handle
575a90cf9f2SGordon Ross 		 * on which read or write access is granted implicitly
576a90cf9f2SGordon Ross 		 * gets "read attributes", even if it was not requested.
577a90cf9f2SGordon Ross 		 */
578*94047d49SGordon Ross 		if ((op->desired_access & FILE_DATA_ALL) != 0)
579*94047d49SGordon Ross 			op->desired_access |= FILE_READ_ATTRIBUTES;
580fb699f1eSAlek Pinchuk 
581fb699f1eSAlek Pinchuk 		/*
582fb699f1eSAlek Pinchuk 		 * Oplock break is done prior to sharing checks as the break
583fb699f1eSAlek Pinchuk 		 * may cause other clients to close the file which would
58449d83597SMatt Barden 		 * affect the sharing checks, and may delete the file due to
58549d83597SMatt Barden 		 * DELETE_ON_CLOSE. This may block, so set the file opening
58649d83597SMatt Barden 		 * count before oplock stuff.
587fb699f1eSAlek Pinchuk 		 */
588*94047d49SGordon Ross 		of = smb_ofile_alloc(sr, op, fnode, SMB_FTYPE_DISK,
589*94047d49SGordon Ross 		    tree_fid, uniq_fid);
590*94047d49SGordon Ross 		tree_fid = 0; // given to the ofile
591fb699f1eSAlek Pinchuk 
592*94047d49SGordon Ross 		smb_node_inc_opening_count(fnode);
593*94047d49SGordon Ross 		opening_incr = B_TRUE;
594*94047d49SGordon Ross 
59549d83597SMatt Barden 		/*
596*94047d49SGordon Ross 		 * XXX Supposed to do share access checks next.
597*94047d49SGordon Ross 		 * [MS-FSA] describes that as part of access check:
598*94047d49SGordon Ross 		 * 2.1.5.1.2.1 Alg... Check Access to an Existing File
599*94047d49SGordon Ross 		 *
600*94047d49SGordon Ross 		 * If CreateDisposition is FILE_OPEN or FILE_OPEN_IF:
601*94047d49SGordon Ross 		 *   If Open.Stream.Oplock is not empty and
602*94047d49SGordon Ross 		 *   Open.Stream.Oplock.State contains BATCH_OPLOCK,
603*94047d49SGordon Ross 		 *   the object store MUST check for an oplock
604*94047d49SGordon Ross 		 *   break according to the algorithm in section 2.1.4.12,
605*94047d49SGordon Ross 		 *   with input values as follows:
606*94047d49SGordon Ross 		 *	Open equal to this operation's Open
607*94047d49SGordon Ross 		 *	Oplock equal to Open.Stream.Oplock
608*94047d49SGordon Ross 		 *	Operation equal to "OPEN"
609*94047d49SGordon Ross 		 *	OpParams containing two members:
610*94047d49SGordon Ross 		 *	  DesiredAccess, CreateDisposition
611*94047d49SGordon Ross 		 *
612*94047d49SGordon Ross 		 * It's not clear how Windows would ask the FS layer if
613*94047d49SGordon Ross 		 * the file has a BATCH oplock.  We'll use a call to the
614*94047d49SGordon Ross 		 * common oplock code, which calls smb_oplock_break_OPEN
615*94047d49SGordon Ross 		 * only if the oplock state contains BATCH_OPLOCK.
616*94047d49SGordon Ross 		 * See: smb_oplock_break_BATCH()
617*94047d49SGordon Ross 		 *
618*94047d49SGordon Ross 		 * Also note: There's a nearly identical section in the
619*94047d49SGordon Ross 		 * spec. at the start of the "else" part of the above
620*94047d49SGordon Ross 		 * "if (disposition is overwrite, overwrite_if)" so this
621*94047d49SGordon Ross 		 * section (oplock break, the share mode check, and the
622*94047d49SGordon Ross 		 * next oplock_break_HANDLE) are all factored out to be
623*94047d49SGordon Ross 		 * in all cases above that if/else from the spec.
62449d83597SMatt Barden 		 */
625*94047d49SGordon Ross 		status = smb_oplock_break_BATCH(fnode, of,
626*94047d49SGordon Ross 		    op->desired_access, op->create_disposition);
627*94047d49SGordon Ross 		if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) {
628*94047d49SGordon Ross 			if (sr->session->dialect >= SMB_VERS_2_BASE)
629*94047d49SGordon Ross 				(void) smb2sr_go_async(sr);
630*94047d49SGordon Ross 			(void) smb_oplock_wait_break(fnode, 0);
631*94047d49SGordon Ross 			status = 0;
632*94047d49SGordon Ross 		}
633*94047d49SGordon Ross 		if (status != NT_STATUS_SUCCESS)
634*94047d49SGordon Ross 			goto errout;
635*94047d49SGordon Ross 
636*94047d49SGordon Ross 		/*
637*94047d49SGordon Ross 		 * Check for sharing violations, and if any,
638*94047d49SGordon Ross 		 * do oplock break of handle caching.
639*94047d49SGordon Ross 		 *
640*94047d49SGordon Ross 		 * Need node_wrlock during shrlock checks,
641*94047d49SGordon Ross 		 * and not locked during oplock breaks etc.
642*94047d49SGordon Ross 		 */
643*94047d49SGordon Ross 		shrlock_t0 = gethrtime();
644*94047d49SGordon Ross 	shrlock_again:
645*94047d49SGordon Ross 		smb_node_wrlock(fnode);
646*94047d49SGordon Ross 		fnode_wlock = B_TRUE;
647*94047d49SGordon Ross 		status = smb_fsop_shrlock(sr->user_cr, fnode, uniq_fid,
648*94047d49SGordon Ross 		    op->desired_access, op->share_access);
649*94047d49SGordon Ross 		smb_node_unlock(fnode);
650*94047d49SGordon Ross 		fnode_wlock = B_FALSE;
651*94047d49SGordon Ross 
652*94047d49SGordon Ross 		/*
653*94047d49SGordon Ross 		 * [MS-FSA] "OPEN_BREAK_H"
654*94047d49SGordon Ross 		 * If the (proposed) new open would violate sharing rules,
655*94047d49SGordon Ross 		 * indicate an oplock break with OPEN_BREAK_H (to break
656*94047d49SGordon Ross 		 * handle level caching rights) then try again.
657*94047d49SGordon Ross 		 */
658*94047d49SGordon Ross 		if (status == NT_STATUS_SHARING_VIOLATION &&
659*94047d49SGordon Ross 		    did_break_handle == B_FALSE) {
660*94047d49SGordon Ross 			did_break_handle = B_TRUE;
661*94047d49SGordon Ross 
662*94047d49SGordon Ross 			status = smb_oplock_break_HANDLE(fnode, of);
663*94047d49SGordon Ross 			if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) {
664*94047d49SGordon Ross 				if (sr->session->dialect >= SMB_VERS_2_BASE)
665*94047d49SGordon Ross 					(void) smb2sr_go_async(sr);
666*94047d49SGordon Ross 				(void) smb_oplock_wait_break(fnode, 0);
667*94047d49SGordon Ross 				status = 0;
668*94047d49SGordon Ross 			} else {
669*94047d49SGordon Ross 				/*
670*94047d49SGordon Ross 				 * Even when the oplock layer does NOT
671*94047d49SGordon Ross 				 * give us the special status indicating
672*94047d49SGordon Ross 				 * we should wait, it may have scheduled
673*94047d49SGordon Ross 				 * taskq jobs that may close handles.
674*94047d49SGordon Ross 				 * Give those a chance to run before we
675*94047d49SGordon Ross 				 * check again for sharing violations.
676*94047d49SGordon Ross 				 */
677*94047d49SGordon Ross 				delay(MSEC_TO_TICK(10));
678*94047d49SGordon Ross 			}
679*94047d49SGordon Ross 			if (status != NT_STATUS_SUCCESS)
680*94047d49SGordon Ross 				goto errout;
681*94047d49SGordon Ross 
682*94047d49SGordon Ross 			goto shrlock_again;
68349d83597SMatt Barden 		}
68449d83597SMatt Barden 
685*94047d49SGordon Ross 		/*
686*94047d49SGordon Ross 		 * SMB1 expects a 1 sec. delay before returning a
687*94047d49SGordon Ross 		 * sharing violation error.  If breaking oplocks
688*94047d49SGordon Ross 		 * above took less than a sec, wait some more.
689*94047d49SGordon Ross 		 * See: smbtorture base.defer_open
690*94047d49SGordon Ross 		 */
691*94047d49SGordon Ross 		if (status == NT_STATUS_SHARING_VIOLATION &&
692*94047d49SGordon Ross 		    sr->session->dialect < SMB_VERS_2_BASE) {
693*94047d49SGordon Ross 			hrtime_t t1 = shrlock_t0 + NANOSEC;
694*94047d49SGordon Ross 			hrtime_t now = gethrtime();
695*94047d49SGordon Ross 			if (now < t1) {
696*94047d49SGordon Ross 				delay(NSEC_TO_TICK_ROUNDUP(t1 - now));
697*94047d49SGordon Ross 			}
698*94047d49SGordon Ross 		}
699*94047d49SGordon Ross 
700*94047d49SGordon Ross 		if (status != NT_STATUS_SUCCESS)
701*94047d49SGordon Ross 			goto errout;
702*94047d49SGordon Ross 		fnode_shrlk = B_TRUE;
703fb699f1eSAlek Pinchuk 
704fb699f1eSAlek Pinchuk 		/*
705*94047d49SGordon Ross 		 * The [MS-FSA] spec. describes this oplock break as
706*94047d49SGordon Ross 		 * part of the sharing access checks.  See:
707*94047d49SGordon Ross 		 * 2.1.5.1.2.2 Algorithm to Check Sharing Access...
708*94047d49SGordon Ross 		 * At the end of the share mode tests described there,
709*94047d49SGordon Ross 		 * if it has not returned "sharing violation", it
710*94047d49SGordon Ross 		 * specifies a call to the alg. in sec. 2.1.4.12,
711*94047d49SGordon Ross 		 * that boils down to: smb_oplock_break_OPEN()
712fb699f1eSAlek Pinchuk 		 */
713*94047d49SGordon Ross 		status = smb_oplock_break_OPEN(fnode, of,
714*94047d49SGordon Ross 		    op->desired_access,
715*94047d49SGordon Ross 		    op->create_disposition);
716*94047d49SGordon Ross 		if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) {
717*94047d49SGordon Ross 			if (sr->session->dialect >= SMB_VERS_2_BASE)
718*94047d49SGordon Ross 				(void) smb2sr_go_async(sr);
719*94047d49SGordon Ross 			(void) smb_oplock_wait_break(fnode, 0);
720*94047d49SGordon Ross 			status = 0;
721*94047d49SGordon Ross 		}
722*94047d49SGordon Ross 		if (status != NT_STATUS_SUCCESS)
723*94047d49SGordon Ross 			goto errout;
724*94047d49SGordon Ross 
725*94047d49SGordon Ross 		if ((fnode->flags & NODE_FLAGS_DELETE_COMMITTED) != 0) {
726*94047d49SGordon Ross 			/*
727*94047d49SGordon Ross 			 * Breaking the oplock caused the file to be deleted,
728*94047d49SGordon Ross 			 * so let's bail and pretend the file wasn't found.
729*94047d49SGordon Ross 			 * Have to duplicate much of the logic found a the
730*94047d49SGordon Ross 			 * "errout" label here.
731*94047d49SGordon Ross 			 *
732*94047d49SGordon Ross 			 * This code path is exercised by smbtorture
733*94047d49SGordon Ross 			 * smb2.durable-open.delete_on_close1
734*94047d49SGordon Ross 			 */
735*94047d49SGordon Ross 			DTRACE_PROBE1(node_deleted, smb_node_t, fnode);
736*94047d49SGordon Ross 			smb_ofile_free(of);
737*94047d49SGordon Ross 			of = NULL;
738*94047d49SGordon Ross 			last_comp_found = B_FALSE;
739*94047d49SGordon Ross 
740*94047d49SGordon Ross 			/*
741*94047d49SGordon Ross 			 * Get all the holds and locks into the state
742*94047d49SGordon Ross 			 * they would have if lookup had failed.
743*94047d49SGordon Ross 			 */
744*94047d49SGordon Ross 			fnode_shrlk = B_FALSE;
745*94047d49SGordon Ross 			smb_fsop_unshrlock(sr->user_cr, fnode, uniq_fid);
746*94047d49SGordon Ross 
747*94047d49SGordon Ross 			opening_incr = B_FALSE;
748*94047d49SGordon Ross 			smb_node_dec_opening_count(fnode);
749*94047d49SGordon Ross 
750*94047d49SGordon Ross 			fnode_held = B_FALSE;
751*94047d49SGordon Ross 			smb_node_release(fnode);
752*94047d49SGordon Ross 
753*94047d49SGordon Ross 			dnode_wlock = B_TRUE;
754*94047d49SGordon Ross 			smb_node_wrlock(dnode);
755*94047d49SGordon Ross 
756*94047d49SGordon Ross 			goto create;
757da6c28aaSamw 		}
758da6c28aaSamw 
759fb699f1eSAlek Pinchuk 		/*
760fb699f1eSAlek Pinchuk 		 * Go ahead with modifications as necessary.
761fb699f1eSAlek Pinchuk 		 */
762fb699f1eSAlek Pinchuk 		switch (op->create_disposition) {
763fb699f1eSAlek Pinchuk 		case FILE_SUPERSEDE:
764fb699f1eSAlek Pinchuk 		case FILE_OVERWRITE_IF:
765fb699f1eSAlek Pinchuk 		case FILE_OVERWRITE:
766037cac00Sjoyce mcintosh 			op->dattr |= FILE_ATTRIBUTE_ARCHIVE;
7675cb2894aSGordon Ross 			/* Don't apply readonly until smb_set_open_attributes */
768037cac00Sjoyce mcintosh 			if (op->dattr & FILE_ATTRIBUTE_READONLY) {
769037cac00Sjoyce mcintosh 				op->dattr &= ~FILE_ATTRIBUTE_READONLY;
7705cb2894aSGordon Ross 				op->created_readonly = B_TRUE;
771037cac00Sjoyce mcintosh 			}
772037cac00Sjoyce mcintosh 
773a90cf9f2SGordon Ross 			/*
774a90cf9f2SGordon Ross 			 * Truncate the file data here.
775a90cf9f2SGordon Ross 			 * We set alloc_size = op->dsize later,
776a90cf9f2SGordon Ross 			 * after we have an ofile.  See:
777a90cf9f2SGordon Ross 			 * smb_set_open_attributes
778a90cf9f2SGordon Ross 			 */
779dc20a302Sas200622 			bzero(&new_attr, sizeof (new_attr));
780037cac00Sjoyce mcintosh 			new_attr.sa_dosattr = op->dattr;
781a90cf9f2SGordon Ross 			new_attr.sa_vattr.va_size = 0;
782037cac00Sjoyce mcintosh 			new_attr.sa_mask = SMB_AT_DOSATTR | SMB_AT_SIZE;
783*94047d49SGordon Ross 			rc = smb_fsop_setattr(sr, sr->user_cr, fnode,
784*94047d49SGordon Ross 			    &new_attr);
785037cac00Sjoyce mcintosh 			if (rc != 0) {
786*94047d49SGordon Ross 				status = smb_errno2status(rc);
787*94047d49SGordon Ross 				goto errout;
788da6c28aaSamw 			}
789da6c28aaSamw 
790da6c28aaSamw 			/*
791037cac00Sjoyce mcintosh 			 * If file is being replaced, remove existing streams
792da6c28aaSamw 			 */
793*94047d49SGordon Ross 			if (SMB_IS_STREAM(fnode) == 0) {
794a90cf9f2SGordon Ross 				status = smb_fsop_remove_streams(sr,
795*94047d49SGordon Ross 				    sr->user_cr, fnode);
796*94047d49SGordon Ross 				if (status != 0)
797*94047d49SGordon Ross 					goto errout;
798eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 			}
799da6c28aaSamw 
800da6c28aaSamw 			op->action_taken = SMB_OACT_TRUNCATED;
801da6c28aaSamw 			break;
802da6c28aaSamw 
803da6c28aaSamw 		default:
804da6c28aaSamw 			/*
805da6c28aaSamw 			 * FILE_OPEN or FILE_OPEN_IF.
806da6c28aaSamw 			 */
807a90cf9f2SGordon Ross 			/*
808a90cf9f2SGordon Ross 			 * Ignore any user-specified alloc_size for
809a90cf9f2SGordon Ross 			 * existing files, to avoid truncation in
810a90cf9f2SGordon Ross 			 * smb_set_open_attributes
811a90cf9f2SGordon Ross 			 */
812a90cf9f2SGordon Ross 			op->dsize = 0L;
813da6c28aaSamw 			op->action_taken = SMB_OACT_OPENED;
814da6c28aaSamw 			break;
815da6c28aaSamw 		}
816da6c28aaSamw 	} else {
81749d83597SMatt Barden create:
818da6c28aaSamw 		/* Last component was not found. */
819eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		dnode = op->fqi.fq_dnode;
820da6c28aaSamw 
8217b59d02dSjb150015 		if (is_dir == 0)
822eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 			is_stream = smb_is_stream_name(pn->pn_path);
8237b59d02dSjb150015 
824da6c28aaSamw 		if ((op->create_disposition == FILE_OPEN) ||
825da6c28aaSamw 		    (op->create_disposition == FILE_OVERWRITE)) {
826*94047d49SGordon Ross 			status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
827*94047d49SGordon Ross 			goto errout;
828da6c28aaSamw 		}
829da6c28aaSamw 
8309fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		if (pn->pn_fname && smb_is_invalid_filename(pn->pn_fname)) {
831*94047d49SGordon Ross 			status = NT_STATUS_OBJECT_NAME_INVALID;
832*94047d49SGordon Ross 			goto errout;
8332c2961f8Sjose borrego 		}
8342c2961f8Sjose borrego 
835da6c28aaSamw 		/*
836a1096253SGordon Ross 		 * Don't create in directories marked "Delete on close".
837a1096253SGordon Ross 		 */
838a1096253SGordon Ross 		if (dnode->flags & NODE_FLAGS_DELETE_ON_CLOSE) {
839*94047d49SGordon Ross 			status = NT_STATUS_DELETE_PENDING;
840*94047d49SGordon Ross 			goto errout;
841a1096253SGordon Ross 		}
842a1096253SGordon Ross 
843a1096253SGordon Ross 		/*
8445cb2894aSGordon Ross 		 * Create always sets the DOS attributes, type, and mode
8455cb2894aSGordon Ross 		 * in the if/else below (different for file vs directory).
8465cb2894aSGordon Ross 		 * Don't set the readonly bit until smb_set_open_attributes
8475cb2894aSGordon Ross 		 * or that would prevent this open.  Note that op->dattr
8485cb2894aSGordon Ross 		 * needs to be what smb_set_open_attributes will use,
8495cb2894aSGordon Ross 		 * except for the readonly bit.
8505cb2894aSGordon Ross 		 */
8515cb2894aSGordon Ross 		bzero(&new_attr, sizeof (new_attr));
8525cb2894aSGordon Ross 		new_attr.sa_mask = SMB_AT_DOSATTR | SMB_AT_TYPE | SMB_AT_MODE;
853037cac00Sjoyce mcintosh 		if (op->dattr & FILE_ATTRIBUTE_READONLY) {
854037cac00Sjoyce mcintosh 			op->dattr &= ~FILE_ATTRIBUTE_READONLY;
855037cac00Sjoyce mcintosh 			op->created_readonly = B_TRUE;
856037cac00Sjoyce mcintosh 		}
857037cac00Sjoyce mcintosh 
8585cb2894aSGordon Ross 		/*
8595cb2894aSGordon Ross 		 * SMB create can specify the create time.
8605cb2894aSGordon Ross 		 */
861c8ec8eeaSjose borrego 		if ((op->crtime.tv_sec != 0) &&
862c8ec8eeaSjose borrego 		    (op->crtime.tv_sec != UINT_MAX)) {
863c8ec8eeaSjose borrego 			new_attr.sa_mask |= SMB_AT_CRTIME;
864c8ec8eeaSjose borrego 			new_attr.sa_crtime = op->crtime;
865c8ec8eeaSjose borrego 		}
866c8ec8eeaSjose borrego 
867da6c28aaSamw 		if (is_dir == 0) {
868037cac00Sjoyce mcintosh 			op->dattr |= FILE_ATTRIBUTE_ARCHIVE;
869037cac00Sjoyce mcintosh 			new_attr.sa_dosattr = op->dattr;
870da6c28aaSamw 			new_attr.sa_vattr.va_type = VREG;
8715cb2894aSGordon Ross 			if (is_stream)
8725cb2894aSGordon Ross 				new_attr.sa_vattr.va_mode = S_IRUSR | S_IWUSR;
8735cb2894aSGordon Ross 			else
8745cb2894aSGordon Ross 				new_attr.sa_vattr.va_mode =
8757b59d02dSjb150015 				    S_IRUSR | S_IRGRP | S_IROTH |
8767b59d02dSjb150015 				    S_IWUSR | S_IWGRP | S_IWOTH;
877dc20a302Sas200622 
878a90cf9f2SGordon Ross 			/*
879a90cf9f2SGordon Ross 			 * We set alloc_size = op->dsize later,
880c5f48fa5SGordon Ross 			 * (in smb_set_open_attributes) after we
881c5f48fa5SGordon Ross 			 * have an ofile on which to save that.
882c5f48fa5SGordon Ross 			 *
883c5f48fa5SGordon Ross 			 * Legacy Open&X sets size to alloc_size
884c5f48fa5SGordon Ross 			 * when creating a new file.
885a90cf9f2SGordon Ross 			 */
886c5f48fa5SGordon Ross 			if (sr->smb_com == SMB_COM_OPEN_ANDX) {
887c5f48fa5SGordon Ross 				new_attr.sa_vattr.va_size = op->dsize;
888c5f48fa5SGordon Ross 				new_attr.sa_mask |= SMB_AT_SIZE;
889c5f48fa5SGordon Ross 			}
890dc20a302Sas200622 
891da6c28aaSamw 			rc = smb_fsop_create(sr, sr->user_cr, dnode,
892037cac00Sjoyce mcintosh 			    op->fqi.fq_last_comp, &new_attr, &op->fqi.fq_fnode);
893da6c28aaSamw 		} else {
8943db3f65cSamw 			op->dattr |= FILE_ATTRIBUTE_DIRECTORY;
895037cac00Sjoyce mcintosh 			new_attr.sa_dosattr = op->dattr;
896da6c28aaSamw 			new_attr.sa_vattr.va_type = VDIR;
897da6c28aaSamw 			new_attr.sa_vattr.va_mode = 0777;
898c8ec8eeaSjose borrego 
899da6c28aaSamw 			rc = smb_fsop_mkdir(sr, sr->user_cr, dnode,
900037cac00Sjoyce mcintosh 			    op->fqi.fq_last_comp, &new_attr, &op->fqi.fq_fnode);
901*94047d49SGordon Ross 		}
902da6c28aaSamw 		if (rc != 0) {
903*94047d49SGordon Ross 			status = smb_errno2status(rc);
904*94047d49SGordon Ross 			goto errout;
905da6c28aaSamw 		}
906dc20a302Sas200622 
907*94047d49SGordon Ross 		smb_node_unlock(dnode);
908*94047d49SGordon Ross 		dnode_wlock = B_FALSE;
909da6c28aaSamw 
910eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		created = B_TRUE;
911da6c28aaSamw 		op->action_taken = SMB_OACT_CREATED;
912c8ec8eeaSjose borrego 
913*94047d49SGordon Ross 		fnode = op->fqi.fq_fnode;
914*94047d49SGordon Ross 		fnode_held = B_TRUE;
915*94047d49SGordon Ross 
916*94047d49SGordon Ross 		smb_node_inc_opening_count(fnode);
917*94047d49SGordon Ross 		opening_incr = B_TRUE;
918*94047d49SGordon Ross 
919*94047d49SGordon Ross 		smb_node_wrlock(fnode);
920*94047d49SGordon Ross 		fnode_wlock = B_TRUE;
921*94047d49SGordon Ross 
922*94047d49SGordon Ross 		status = smb_fsop_shrlock(sr->user_cr, fnode, uniq_fid,
923*94047d49SGordon Ross 		    op->desired_access, op->share_access);
924*94047d49SGordon Ross 		if (status != 0)
925*94047d49SGordon Ross 			goto errout;
926*94047d49SGordon Ross 		fnode_shrlk = B_TRUE;
927*94047d49SGordon Ross 
9282c1b14e5Sjose borrego 		if (max_requested) {
929*94047d49SGordon 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*94047d49SGordon Ross 		/*
941*94047d49SGordon Ross 		 * MS-FSA 2.1.5.1.1
942*94047d49SGordon Ross 		 * If the Oplock member of the DirectoryStream in
943*94047d49SGordon Ross 		 * Link.ParentFile.StreamList (ParentOplock) is
944*94047d49SGordon Ross 		 * not empty ... oplock break on the parent...
945*94047d49SGordon Ross 		 * (dnode is the parent directory)
946*94047d49SGordon Ross 		 *
947*94047d49SGordon Ross 		 * This compares of->ParentOplockKey with each
948*94047d49SGordon Ross 		 * oplock of->TargetOplockKey and breaks...
949*94047d49SGordon Ross 		 * so it's OK that we're passing an OF that's
950*94047d49SGordon Ross 		 * NOT a member of dnode->n_ofile_list
951*94047d49SGordon Ross 		 *
952*94047d49SGordon Ross 		 * The break never blocks, so ignore the return.
953*94047d49SGordon Ross 		 */
954*94047d49SGordon Ross 		of = smb_ofile_alloc(sr, op, fnode, SMB_FTYPE_DISK,
955*94047d49SGordon Ross 		    tree_fid, uniq_fid);
956*94047d49SGordon Ross 		tree_fid = 0; // given to the ofile
957*94047d49SGordon Ross 		(void) smb_oplock_break_PARENT(dnode, of);
958da6c28aaSamw 	}
959da6c28aaSamw 
96068b2bbf2SGordon Ross 	/*
961*94047d49SGordon Ross 	 * We might have blocked in smb_oplock_break_OPEN long enough
962*94047d49SGordon Ross 	 * so a tree disconnect might have happened.  In that case,
963*94047d49SGordon Ross 	 * we would be adding an ofile to a tree that's disconnecting,
964*94047d49SGordon Ross 	 * which would interfere with tear-down.  If so, error out.
96568b2bbf2SGordon Ross 	 */
966*94047d49SGordon Ross 	if (!smb_tree_is_connected(sr->tid_tree)) {
96768b2bbf2SGordon Ross 		status = NT_STATUS_INVALID_PARAMETER;
968*94047d49SGordon Ross 		goto errout;
969037cac00Sjoyce mcintosh 	}
970037cac00Sjoyce mcintosh 
971037cac00Sjoyce mcintosh 	/*
972*94047d49SGordon Ross 	 * Moved this up from smb_ofile_open()
973*94047d49SGordon Ross 	 */
974*94047d49SGordon Ross 	if ((rc = smb_fsop_open(fnode, of->f_mode, of->f_cr)) != 0) {
975*94047d49SGordon Ross 		status = smb_errno2status(rc);
976*94047d49SGordon Ross 		goto errout;
977*94047d49SGordon Ross 	}
978*94047d49SGordon Ross 
979*94047d49SGordon Ross 	/*
980*94047d49SGordon Ross 	 * Complete this open (add to ofile lists)
981*94047d49SGordon Ross 	 */
982*94047d49SGordon Ross 	smb_ofile_open(sr, op, of);
983*94047d49SGordon Ross 	did_open = B_TRUE;
984*94047d49SGordon Ross 
985*94047d49SGordon Ross 	/*
986037cac00Sjoyce mcintosh 	 * This MUST be done after ofile creation, so that explicitly
9875cb2894aSGordon Ross 	 * set timestamps can be remembered on the ofile, and setting
9885cb2894aSGordon Ross 	 * the readonly flag won't affect access via this open.
989037cac00Sjoyce mcintosh 	 */
9905fd03bc0SGordon Ross 	if ((rc = smb_set_open_attributes(sr, of)) != 0) {
991a90cf9f2SGordon Ross 		status = smb_errno2status(rc);
992*94047d49SGordon Ross 		goto errout;
993037cac00Sjoyce mcintosh 	}
994037cac00Sjoyce mcintosh 
9955fd03bc0SGordon Ross 	/*
9965fd03bc0SGordon Ross 	 * We've already done access checks above,
9975fd03bc0SGordon Ross 	 * and want this call to succeed even when
9985fd03bc0SGordon Ross 	 * !(desired_access & FILE_READ_ATTRIBUTES),
9995fd03bc0SGordon Ross 	 * so pass kcred here.
10005fd03bc0SGordon Ross 	 */
10015fd03bc0SGordon Ross 	op->fqi.fq_fattr.sa_mask = SMB_AT_ALL;
1002*94047d49SGordon Ross 	(void) smb_node_getattr(sr, fnode, zone_kcred(), of,
10035fd03bc0SGordon Ross 	    &op->fqi.fq_fattr);
10048b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 
10058c10a865Sas200622 	/*
10068c10a865Sas200622 	 * Propagate the write-through mode from the open params
10078c10a865Sas200622 	 * to the node: see the notes in the function header.
1008*94047d49SGordon Ross 	 * XXX: write_through should be a flag on the ofile.
10098c10a865Sas200622 	 */
10108c10a865Sas200622 	if (sr->sr_cfg->skc_sync_enable ||
10118c10a865Sas200622 	    (op->create_options & FILE_WRITE_THROUGH))
1012*94047d49SGordon Ross 		fnode->flags |= NODE_FLAGS_WRITE_THROUGH;
10138c10a865Sas200622 
1014037cac00Sjoyce mcintosh 	/*
1015037cac00Sjoyce mcintosh 	 * Set up the fileid and dosattr in open_param for response
1016037cac00Sjoyce mcintosh 	 */
1017eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 	op->fileid = op->fqi.fq_fattr.sa_vattr.va_nodeid;
1018037cac00Sjoyce mcintosh 	op->dattr = op->fqi.fq_fattr.sa_dosattr;
10198c10a865Sas200622 
1020da6c28aaSamw 	/*
1021da6c28aaSamw 	 * Set up the file type in open_param for the response
1022da6c28aaSamw 	 */
1023da6c28aaSamw 	op->ftype = SMB_FTYPE_DISK;
1024da6c28aaSamw 	sr->smb_fid = of->f_fid;
1025da6c28aaSamw 	sr->fid_ofile = of;
1026da6c28aaSamw 
1027*94047d49SGordon Ross 	if (smb_node_is_file(fnode)) {
1028eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		op->dsize = op->fqi.fq_fattr.sa_vattr.va_size;
10299fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	} else {
10309fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		/* directory or symlink */
10312c2961f8Sjose borrego 		op->dsize = 0;
10322c2961f8Sjose borrego 	}
1033dc20a302Sas200622 
1034*94047d49SGordon Ross 	/*
1035*94047d49SGordon Ross 	 * Note: oplock_acquire happens in callers, because
1036*94047d49SGordon Ross 	 * how that happens is protocol-specific.
1037*94047d49SGordon Ross 	 */
1038cb174861Sjoyce mcintosh 
1039*94047d49SGordon Ross 	if (fnode_wlock)
1040*94047d49SGordon Ross 		smb_node_unlock(fnode);
1041*94047d49SGordon Ross 	if (opening_incr)
1042*94047d49SGordon Ross 		smb_node_dec_opening_count(fnode);
1043*94047d49SGordon Ross 	if (fnode_held)
1044*94047d49SGordon Ross 		smb_node_release(fnode);
1045*94047d49SGordon Ross 	if (dnode_wlock)
1046cb174861Sjoyce mcintosh 		smb_node_unlock(dnode);
1047*94047d49SGordon Ross 	if (dnode_held)
1048da6c28aaSamw 		smb_node_release(dnode);
1049da6c28aaSamw 
1050da6c28aaSamw 	return (NT_STATUS_SUCCESS);
1051*94047d49SGordon Ross 
1052*94047d49SGordon Ross errout:
1053*94047d49SGordon Ross 	if (did_open) {
1054*94047d49SGordon Ross 		smb_ofile_close(of, 0);
1055*94047d49SGordon Ross 		/* Don't also ofile_free */
1056*94047d49SGordon Ross 	} else if (of != NULL) {
1057*94047d49SGordon Ross 		smb_ofile_free(of);
1058da6c28aaSamw 	}
1059da6c28aaSamw 
1060*94047d49SGordon Ross 	if (fnode_shrlk)
1061*94047d49SGordon Ross 		smb_fsop_unshrlock(sr->user_cr, fnode, uniq_fid);
1062148d1a41SMatt Barden 
1063*94047d49SGordon Ross 	if (created) {
1064*94047d49SGordon Ross 		/* Try to roll-back create. */
1065*94047d49SGordon Ross 		smb_delete_new_object(sr);
1066148d1a41SMatt Barden 	}
1067148d1a41SMatt Barden 
1068*94047d49SGordon Ross 	if (fnode_wlock)
1069*94047d49SGordon Ross 		smb_node_unlock(fnode);
1070*94047d49SGordon Ross 	if (opening_incr)
1071*94047d49SGordon Ross 		smb_node_dec_opening_count(fnode);
1072*94047d49SGordon Ross 	if (fnode_held)
1073*94047d49SGordon Ross 		smb_node_release(fnode);
1074*94047d49SGordon Ross 	if (dnode_wlock)
1075*94047d49SGordon Ross 		smb_node_unlock(dnode);
1076*94047d49SGordon Ross 	if (dnode_held)
1077*94047d49SGordon Ross 		smb_node_release(dnode);
1078cb174861Sjoyce mcintosh 
1079*94047d49SGordon Ross 	if (tree_fid != 0)
1080*94047d49SGordon Ross 		smb_idpool_free(&tree->t_fid_pool, tree_fid);
1081cb174861Sjoyce mcintosh 
1082*94047d49SGordon Ross 	return (status);
1083cb174861Sjoyce mcintosh }
10845fd03bc0SGordon Ross 
1085cb174861Sjoyce mcintosh /*
10865fd03bc0SGordon Ross  * smb_set_open_attributes
1087037cac00Sjoyce mcintosh  *
1088037cac00Sjoyce mcintosh  * Last write time:
1089037cac00Sjoyce mcintosh  * - If the last_write time specified in the open params is not 0 or -1,
1090037cac00Sjoyce mcintosh  *   use it as file's mtime. This will be considered an explicitly set
1091037cac00Sjoyce mcintosh  *   timestamps, not reset by subsequent writes.
1092037cac00Sjoyce mcintosh  *
10935fd03bc0SGordon Ross  * DOS attributes
10945fd03bc0SGordon Ross  * - If we created_readonly, we now store the real DOS attributes
10955fd03bc0SGordon Ross  *   (including the readonly bit) so subsequent opens will see it.
1096037cac00Sjoyce mcintosh  *
1097037cac00Sjoyce mcintosh  * Returns: errno
1098037cac00Sjoyce mcintosh  */
1099037cac00Sjoyce mcintosh static int
11005fd03bc0SGordon Ross smb_set_open_attributes(smb_request_t *sr, smb_ofile_t *of)
1101037cac00Sjoyce mcintosh {
11025fd03bc0SGordon Ross 	smb_attr_t	attr;
1103148c5f43SAlan Wright 	smb_arg_open_t	*op = &sr->sr_open;
1104037cac00Sjoyce mcintosh 	smb_node_t	*node = of->f_node;
11055fd03bc0SGordon Ross 	int		rc = 0;
1106037cac00Sjoyce mcintosh 
1107037cac00Sjoyce mcintosh 	bzero(&attr, sizeof (smb_attr_t));
11085fd03bc0SGordon Ross 
11095fd03bc0SGordon Ross 	if (op->created_readonly) {
11105fd03bc0SGordon Ross 		attr.sa_dosattr = op->dattr | FILE_ATTRIBUTE_READONLY;
11115fd03bc0SGordon Ross 		attr.sa_mask |= SMB_AT_DOSATTR;
11125fd03bc0SGordon Ross 	}
1113037cac00Sjoyce mcintosh 
1114a90cf9f2SGordon Ross 	if (op->dsize != 0) {
1115a90cf9f2SGordon Ross 		attr.sa_allocsz = op->dsize;
1116a90cf9f2SGordon Ross 		attr.sa_mask |= SMB_AT_ALLOCSZ;
1117a90cf9f2SGordon Ross 	}
1118a90cf9f2SGordon Ross 
1119037cac00Sjoyce mcintosh 	if ((op->mtime.tv_sec != 0) && (op->mtime.tv_sec != UINT_MAX)) {
1120037cac00Sjoyce mcintosh 		attr.sa_vattr.va_mtime = op->mtime;
11215fd03bc0SGordon Ross 		attr.sa_mask |= SMB_AT_MTIME;
1122037cac00Sjoyce mcintosh 	}
1123037cac00Sjoyce mcintosh 
11245fd03bc0SGordon Ross 	/*
11255fd03bc0SGordon Ross 	 * Used to have code here to set mtime, ctime, atime
11265fd03bc0SGordon Ross 	 * when the open op->create_disposition is any of:
11275fd03bc0SGordon Ross 	 * FILE_SUPERSEDE, FILE_OVERWRITE_IF, FILE_OVERWRITE.
11285fd03bc0SGordon Ross 	 * We know that in those cases we will have set the
11295fd03bc0SGordon Ross 	 * file size, in which case the file system will
11305fd03bc0SGordon Ross 	 * update those times, so we don't have to.
11315fd03bc0SGordon Ross 	 *
11325fd03bc0SGordon Ross 	 * However, keep track of the fact that we modified
11335fd03bc0SGordon Ross 	 * the file via this handle, so we can do the evil,
11345fd03bc0SGordon Ross 	 * gratuitious mtime update on close that Windows
11355cb2894aSGordon Ross 	 * clients expect.
11365fd03bc0SGordon Ross 	 */
11375fd03bc0SGordon Ross 	if (op->action_taken == SMB_OACT_TRUNCATED)
11385fd03bc0SGordon Ross 		of->f_written = B_TRUE;
1139037cac00Sjoyce mcintosh 
11405fd03bc0SGordon Ross 	if (attr.sa_mask != 0)
11415fd03bc0SGordon Ross 		rc = smb_node_setattr(sr, node, of->f_cr, of, &attr);
1142037cac00Sjoyce mcintosh 
1143037cac00Sjoyce mcintosh 	return (rc);
1144037cac00Sjoyce mcintosh }
1145037cac00Sjoyce mcintosh 
1146037cac00Sjoyce mcintosh /*
11478b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States  * This function is used to delete a newly created object (file or
11488b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States  * directory) if an error occurs after creation of the object.
1149da6c28aaSamw  */
11508b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States static void
11518b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States smb_delete_new_object(smb_request_t *sr)
1152da6c28aaSamw {
1153148c5f43SAlan Wright 	smb_arg_open_t	*op = &sr->sr_open;
11548b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	smb_fqi_t	*fqi = &(op->fqi);
11558b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	uint32_t	flags = 0;
11568b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 
11578b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (SMB_TREE_IS_CASEINSENSITIVE(sr))
11588b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		flags |= SMB_IGNORE_CASE;
11598b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (SMB_TREE_SUPPORTS_CATIA(sr))
11608b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 		flags |= SMB_CATIA;
11618b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 
11628b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	if (op->create_options & FILE_DIRECTORY_FILE)
1163eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		(void) smb_fsop_rmdir(sr, sr->user_cr, fqi->fq_dnode,
1164eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		    fqi->fq_last_comp, flags);
11658b2cc8acSafshin salek ardakani - Sun Microsystems - Irvine United States 	else
1166eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		(void) smb_fsop_remove(sr, sr->user_cr, fqi->fq_dnode,
1167eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States 		    fqi->fq_last_comp, flags);
1168eb1d736bSafshin salek ardakani - Sun Microsystems - Irvine United States }
1169