xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_common_open.c (revision 525641e8e46b2b8beffe3f067f910178ffa06377)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2020 Tintri by DDN, Inc. All rights reserved.
25  */
26 
27 /*
28  * This module provides the common open functionality to the various
29  * open and create SMB interface functions.
30  */
31 
32 #include <sys/types.h>
33 #include <sys/cmn_err.h>
34 #include <sys/fcntl.h>
35 #include <sys/nbmlock.h>
36 #include <smbsrv/string.h>
37 #include <smbsrv/smb2_kproto.h>
38 #include <smbsrv/smb_fsops.h>
39 #include <smbsrv/smbinfo.h>
40 
41 int smb_session_ofile_max = 32768;
42 
43 extern uint32_t smb_is_executable(char *);
44 static void smb_delete_new_object(smb_request_t *);
45 static int smb_set_open_attributes(smb_request_t *, smb_ofile_t *);
46 
47 /*
48  * smb_access_generic_to_file
49  *
50  * Search MSDN for IoCreateFile to see following mapping.
51  *
52  * GENERIC_READ		STANDARD_RIGHTS_READ, FILE_READ_DATA,
53  *			FILE_READ_ATTRIBUTES and FILE_READ_EA
54  *
55  * GENERIC_WRITE	STANDARD_RIGHTS_WRITE, FILE_WRITE_DATA,
56  *               FILE_WRITE_ATTRIBUTES, FILE_WRITE_EA, and FILE_APPEND_DATA
57  *
58  * GENERIC_EXECUTE	STANDARD_RIGHTS_EXECUTE, SYNCHRONIZE, and FILE_EXECUTE.
59  */
60 static uint32_t
61 smb_access_generic_to_file(uint32_t desired_access)
62 {
63 	uint32_t access = 0;
64 
65 	if (desired_access & GENERIC_ALL)
66 		return (FILE_ALL_ACCESS & ~SYNCHRONIZE);
67 
68 	if (desired_access & GENERIC_EXECUTE) {
69 		desired_access &= ~GENERIC_EXECUTE;
70 		access |= (STANDARD_RIGHTS_EXECUTE |
71 		    SYNCHRONIZE | FILE_EXECUTE);
72 	}
73 
74 	if (desired_access & GENERIC_WRITE) {
75 		desired_access &= ~GENERIC_WRITE;
76 		access |= (FILE_GENERIC_WRITE & ~SYNCHRONIZE);
77 	}
78 
79 	if (desired_access & GENERIC_READ) {
80 		desired_access &= ~GENERIC_READ;
81 		access |= FILE_GENERIC_READ;
82 	}
83 
84 	return (access | desired_access);
85 }
86 
87 /*
88  * smb_omode_to_amask
89  *
90  * This function converts open modes used by Open and Open AndX
91  * commands to desired access bits used by NT Create AndX command.
92  */
93 uint32_t
94 smb_omode_to_amask(uint32_t desired_access)
95 {
96 	switch (desired_access & SMB_DA_ACCESS_MASK) {
97 	case SMB_DA_ACCESS_READ:
98 		return (FILE_GENERIC_READ);
99 
100 	case SMB_DA_ACCESS_WRITE:
101 		return (FILE_GENERIC_WRITE);
102 
103 	case SMB_DA_ACCESS_READ_WRITE:
104 		return (FILE_GENERIC_READ | FILE_GENERIC_WRITE);
105 
106 	case SMB_DA_ACCESS_EXECUTE:
107 		return (FILE_GENERIC_READ | FILE_GENERIC_EXECUTE);
108 
109 	default:
110 		return (FILE_GENERIC_ALL);
111 	}
112 }
113 
114 /*
115  * smb_denymode_to_sharemode
116  *
117  * This function converts deny modes used by Open and Open AndX
118  * commands to share access bits used by NT Create AndX command.
119  */
120 uint32_t
121 smb_denymode_to_sharemode(uint32_t desired_access, char *fname)
122 {
123 	switch (desired_access & SMB_DA_SHARE_MASK) {
124 	case SMB_DA_SHARE_COMPATIBILITY:
125 		if (smb_is_executable(fname))
126 			return (FILE_SHARE_READ | FILE_SHARE_WRITE);
127 
128 		return (FILE_SHARE_ALL);
129 
130 	case SMB_DA_SHARE_EXCLUSIVE:
131 		return (FILE_SHARE_NONE);
132 
133 	case SMB_DA_SHARE_DENY_WRITE:
134 		return (FILE_SHARE_READ);
135 
136 	case SMB_DA_SHARE_DENY_READ:
137 		return (FILE_SHARE_WRITE);
138 
139 	case SMB_DA_SHARE_DENY_NONE:
140 	default:
141 		return (FILE_SHARE_READ | FILE_SHARE_WRITE);
142 	}
143 }
144 
145 /*
146  * smb_ofun_to_crdisposition
147  *
148  * This function converts open function values used by Open and Open AndX
149  * commands to create disposition values used by NT Create AndX command.
150  */
151 uint32_t
152 smb_ofun_to_crdisposition(uint16_t  ofun)
153 {
154 	static int ofun_cr_map[3][2] =
155 	{
156 		{ -1,			FILE_CREATE },
157 		{ FILE_OPEN,		FILE_OPEN_IF },
158 		{ FILE_OVERWRITE,	FILE_OVERWRITE_IF }
159 	};
160 
161 	int row = ofun & SMB_OFUN_OPEN_MASK;
162 	int col = (ofun & SMB_OFUN_CREATE_MASK) >> 4;
163 
164 	if (row == 3)
165 		return (FILE_MAXIMUM_DISPOSITION + 1);
166 
167 	return (ofun_cr_map[row][col]);
168 }
169 
170 /*
171  * smb_common_open
172  *
173  * Notes on write-through behaviour. It looks like pre-LM0.12 versions
174  * of the protocol specify the write-through mode when a file is opened,
175  * (SmbOpen, SmbOpenAndX) so the write calls (SmbWrite, SmbWriteAndClose,
176  * SmbWriteAndUnlock) don't need to contain a write-through flag.
177  *
178  * With LM0.12, the open calls (SmbCreateAndX, SmbNtTransactCreate)
179  * don't indicate which write-through mode to use. Instead the write
180  * calls (SmbWriteAndX, SmbWriteRaw) specify the mode on a per call
181  * basis.
182  *
183  * We don't care which open call was used to get us here, we just need
184  * to ensure that the write-through mode flag is copied from the open
185  * parameters to the node. We test the omode write-through flag in all
186  * write functions.
187  *
188  * This function returns NT status codes.
189  *
190  * The following rules apply when processing a file open request:
191  *
192  * - Oplocks must be broken prior to share checking as the break may
193  *   cause other clients to close the file, which would affect sharing
194  *   checks.
195  *
196  * - Share checks must take place prior to access checks for correct
197  * Windows semantics and to prevent unnecessary NFS delegation recalls.
198  *
199  * - Oplocks must be acquired after open to ensure the correct
200  * synchronization with NFS delegation and FEM installation.
201  *
202  * DOS readonly bit rules
203  *
204  * 1. The creator of a readonly file can write to/modify the size of the file
205  * using the original create fid, even though the file will appear as readonly
206  * to all other fids and via a CIFS getattr call.
207  *
208  * 2. A setinfo operation (using either an open fid or a path) to set/unset
209  * readonly will be successful regardless of whether a creator of a readonly
210  * file has an open fid.
211  *
212  * 3. The DOS readonly bit affects only data and some metadata.
213  * The following metadata can be changed regardless of the readonly bit:
214  *	- security descriptors
215  *	- DOS attributes
216  *	- timestamps
217  *
218  * In the current implementation, the file size cannot be changed (except for
219  * the exceptions in #1 and #2, above).
220  *
221  *
222  * DOS attribute rules
223  *
224  * These rules are specific to creating / opening files and directories.
225  * How the attribute value (specifically ZERO or FILE_ATTRIBUTE_NORMAL)
226  * should be interpreted may differ in other requests.
227  *
228  * - An attribute value equal to ZERO or FILE_ATTRIBUTE_NORMAL means that the
229  *   file's attributes should be cleared.
230  * - If FILE_ATTRIBUTE_NORMAL is specified with any other attributes,
231  *   FILE_ATTRIBUTE_NORMAL is ignored.
232  *
233  * 1. Creating a new file
234  * - The request attributes + FILE_ATTRIBUTE_ARCHIVE are applied to the file.
235  *
236  * 2. Creating a new directory
237  * - The request attributes + FILE_ATTRIBUTE_DIRECTORY are applied to the file.
238  * - FILE_ATTRIBUTE_ARCHIVE does not get set.
239  *
240  * 3. Overwriting an existing file
241  * - the request attributes are used as search attributes. If the existing
242  *   file does not meet the search criteria access is denied.
243  * - otherwise, applies attributes + FILE_ATTRIBUTE_ARCHIVE.
244  *
245  * 4. Opening an existing file or directory
246  *    The request attributes are ignored.
247  */
248 uint32_t
249 smb_common_open(smb_request_t *sr)
250 {
251 	smb_server_t	*sv = sr->sr_server;
252 	smb_tree_t	*tree = sr->tid_tree;
253 	smb_node_t	*fnode = NULL;
254 	smb_node_t	*dnode = NULL;
255 	smb_node_t	*cur_node = NULL;
256 	smb_node_t	*tmp_node = NULL;
257 	smb_arg_open_t	*op = &sr->sr_open;
258 	smb_pathname_t	*pn = &op->fqi.fq_path;
259 	smb_ofile_t	*of = NULL;
260 	smb_attr_t	new_attr;
261 	hrtime_t	shrlock_t0;
262 	int		max_requested = 0;
263 	uint32_t	max_allowed;
264 	uint32_t	status = NT_STATUS_SUCCESS;
265 	int		is_dir;
266 	int		rc;
267 	boolean_t	is_stream = B_FALSE;
268 	int		lookup_flags = SMB_FOLLOW_LINKS;
269 	uint32_t	uniq_fid = 0;
270 	uint16_t	tree_fid = 0;
271 	boolean_t	created = B_FALSE;
272 	boolean_t	last_comp_found = B_FALSE;
273 	boolean_t	stream_found = B_FALSE;
274 	boolean_t	opening_incr = B_FALSE;
275 	boolean_t	dnode_held = B_FALSE;
276 	boolean_t	dnode_wlock = B_FALSE;
277 	boolean_t	fnode_held = B_FALSE;
278 	boolean_t	fnode_wlock = B_FALSE;
279 	boolean_t	fnode_shrlk = B_FALSE;
280 	boolean_t	did_open = B_FALSE;
281 	boolean_t	did_break_handle = B_FALSE;
282 	boolean_t	did_cleanup_orphans = B_FALSE;
283 	char		*sname = NULL;
284 
285 	/* Get out now if we've been cancelled. */
286 	mutex_enter(&sr->sr_mutex);
287 	if (sr->sr_state != SMB_REQ_STATE_ACTIVE) {
288 		mutex_exit(&sr->sr_mutex);
289 		return (NT_STATUS_CANCELLED);
290 	}
291 	mutex_exit(&sr->sr_mutex);
292 
293 	is_dir = (op->create_options & FILE_DIRECTORY_FILE) ? 1 : 0;
294 
295 	/*
296 	 * If the object being created or opened is a directory
297 	 * the Disposition parameter must be one of FILE_CREATE,
298 	 * FILE_OPEN, or FILE_OPEN_IF
299 	 */
300 	if (is_dir) {
301 		if ((op->create_disposition != FILE_CREATE) &&
302 		    (op->create_disposition != FILE_OPEN_IF) &&
303 		    (op->create_disposition != FILE_OPEN)) {
304 			return (NT_STATUS_INVALID_PARAMETER);
305 		}
306 	}
307 
308 	if (op->desired_access & MAXIMUM_ALLOWED) {
309 		max_requested = 1;
310 		op->desired_access &= ~MAXIMUM_ALLOWED;
311 	}
312 	op->desired_access = smb_access_generic_to_file(op->desired_access);
313 
314 	if (sr->session->s_file_cnt >= smb_session_ofile_max) {
315 		ASSERT(sr->uid_user);
316 		cmn_err(CE_NOTE, "smbsrv[%s\\%s]: TOO_MANY_OPENED_FILES",
317 		    sr->uid_user->u_domain, sr->uid_user->u_name);
318 		return (NT_STATUS_TOO_MANY_OPENED_FILES);
319 	}
320 
321 	if (smb_idpool_alloc(&tree->t_fid_pool, &tree_fid))
322 		return (NT_STATUS_TOO_MANY_OPENED_FILES);
323 
324 	/* This must be NULL at this point */
325 	sr->fid_ofile = NULL;
326 
327 	op->devstate = 0;
328 
329 	switch (sr->tid_tree->t_res_type & STYPE_MASK) {
330 	case STYPE_DISKTREE:
331 	case STYPE_PRINTQ:
332 		break;
333 
334 	case STYPE_IPC:
335 		/*
336 		 * Security descriptors for pipes are not implemented,
337 		 * so just setup a reasonable access mask.
338 		 */
339 		op->desired_access = (READ_CONTROL | SYNCHRONIZE |
340 		    FILE_READ_DATA | FILE_READ_ATTRIBUTES |
341 		    FILE_WRITE_DATA | FILE_APPEND_DATA);
342 
343 		/*
344 		 * Limit the number of open pipe instances.
345 		 */
346 		if ((rc = smb_threshold_enter(&sv->sv_opipe_ct)) != 0) {
347 			status = RPC_NT_SERVER_TOO_BUSY;
348 			goto errout;
349 		}
350 
351 		/*
352 		 * Most of IPC open is handled in smb_opipe_open()
353 		 */
354 		op->create_options = 0;
355 		of = smb_ofile_alloc(sr, op, NULL, SMB_FTYPE_MESG_PIPE,
356 		    tree_fid);
357 		tree_fid = 0; // given to the ofile
358 		status = smb_opipe_open(sr, of);
359 		smb_threshold_exit(&sv->sv_opipe_ct);
360 		if (status != NT_STATUS_SUCCESS)
361 			goto errout;
362 		return (NT_STATUS_SUCCESS);
363 
364 	default:
365 		status = NT_STATUS_BAD_DEVICE_TYPE;
366 		goto errout;
367 	}
368 
369 	smb_pathname_init(sr, pn, pn->pn_path);
370 	if (!smb_pathname_validate(sr, pn)) {
371 		status = sr->smb_error.status;
372 		goto errout;
373 	}
374 
375 	if (strlen(pn->pn_path) >= SMB_MAXPATHLEN) {
376 		status = NT_STATUS_OBJECT_PATH_INVALID;
377 		goto errout;
378 	}
379 
380 	if (is_dir) {
381 		if (!smb_validate_dirname(sr, pn)) {
382 			status = sr->smb_error.status;
383 			goto errout;
384 		}
385 	} else {
386 		if (!smb_validate_object_name(sr, pn)) {
387 			status = sr->smb_error.status;
388 			goto errout;
389 		}
390 	}
391 
392 	cur_node = op->fqi.fq_dnode ?
393 	    op->fqi.fq_dnode : sr->tid_tree->t_snode;
394 
395 	rc = smb_pathname_reduce(sr, sr->user_cr, pn->pn_path,
396 	    sr->tid_tree->t_snode, cur_node, &op->fqi.fq_dnode,
397 	    op->fqi.fq_last_comp);
398 	if (rc != 0) {
399 		status = smb_errno2status(rc);
400 		goto errout;
401 	}
402 	dnode = op->fqi.fq_dnode;
403 	dnode_held = B_TRUE;
404 
405 	/*
406 	 * Lock the parent dir node in case another create
407 	 * request to the same parent directory comes in.
408 	 * Drop this once either lookup succeeds, or we've
409 	 * created the object in this directory.
410 	 */
411 	smb_node_wrlock(dnode);
412 	dnode_wlock = B_TRUE;
413 
414 	/*
415 	 * If the access mask has only DELETE set (ignore
416 	 * FILE_READ_ATTRIBUTES), then assume that this
417 	 * is a request to delete the link (if a link)
418 	 * and do not follow links.  Otherwise, follow
419 	 * the link to the target.
420 	 */
421 	if ((op->desired_access & ~FILE_READ_ATTRIBUTES) == DELETE)
422 		lookup_flags &= ~SMB_FOLLOW_LINKS;
423 
424 	/*
425 	 * Lookup *just* the file portion of the name.
426 	 * Returns stream name in sname, which this allocates
427 	 */
428 	rc = smb_fsop_lookup_file(sr, zone_kcred(), lookup_flags,
429 	    sr->tid_tree->t_snode, op->fqi.fq_dnode, op->fqi.fq_last_comp,
430 	    &sname, &op->fqi.fq_fnode);
431 
432 	if (rc == 0) {
433 		last_comp_found = B_TRUE;
434 		fnode_held = B_TRUE;
435 
436 		/*
437 		 * Need the DOS attributes below, where we
438 		 * check the search attributes (sattr).
439 		 * Also UID, for owner check below.
440 		 */
441 		op->fqi.fq_fattr.sa_mask = SMB_AT_DOSATTR | SMB_AT_UID;
442 		rc = smb_node_getattr(sr, op->fqi.fq_fnode, zone_kcred(),
443 		    NULL, &op->fqi.fq_fattr);
444 		if (rc != 0) {
445 			status = NT_STATUS_INTERNAL_ERROR;
446 			goto errout;
447 		}
448 	} else if (rc == ENOENT) {
449 		last_comp_found = B_FALSE;
450 		op->fqi.fq_fnode = NULL;
451 		rc = 0;
452 	} else {
453 		status = smb_errno2status(rc);
454 		goto errout;
455 	}
456 
457 	if (last_comp_found) {
458 
459 		fnode = op->fqi.fq_fnode;
460 		dnode = op->fqi.fq_dnode;
461 
462 		if (!smb_node_is_file(fnode) &&
463 		    !smb_node_is_dir(fnode) &&
464 		    !smb_node_is_symlink(fnode)) {
465 			status = NT_STATUS_ACCESS_DENIED;
466 			goto errout;
467 		}
468 
469 		/*
470 		 * Reject this request if either:
471 		 * - the target IS a directory and the client requires that
472 		 *   it must NOT be (required by Lotus Notes)
473 		 * - the target is NOT a directory and client requires that
474 		 *   it MUST be.
475 		 * Streams are never directories.
476 		 */
477 		if (smb_node_is_dir(fnode) && sname == NULL) {
478 			if (op->create_options & FILE_NON_DIRECTORY_FILE) {
479 				status = NT_STATUS_FILE_IS_A_DIRECTORY;
480 				goto errout;
481 			}
482 		} else {
483 			if ((op->create_options & FILE_DIRECTORY_FILE) ||
484 			    (op->nt_flags & NT_CREATE_FLAG_OPEN_TARGET_DIR)) {
485 				status = NT_STATUS_NOT_A_DIRECTORY;
486 				goto errout;
487 			}
488 		}
489 
490 		/* If we're given a stream name, look it up now */
491 		if (sname != NULL) {
492 			tmp_node = fnode;
493 			rc = smb_fsop_lookup_stream(sr, zone_kcred(),
494 			    lookup_flags, sr->tid_tree->t_snode, fnode, sname,
495 			    &fnode);
496 		} else {
497 			rc = 0;
498 		}
499 
500 		if (rc == 0) { /* Stream Exists (including unnamed stream) */
501 			stream_found = B_TRUE;
502 			smb_node_unlock(dnode);
503 			dnode_wlock = B_FALSE;
504 
505 			if (tmp_node != NULL)
506 				smb_node_release(tmp_node);
507 
508 			/*
509 			 * No more open should be accepted when
510 			 * "Delete on close" flag is set.
511 			 */
512 			if (fnode->flags & NODE_FLAGS_DELETE_ON_CLOSE) {
513 				status = NT_STATUS_DELETE_PENDING;
514 				goto errout;
515 			}
516 
517 			/*
518 			 * Specified file already exists
519 			 * so the operation should fail.
520 			 */
521 			if (op->create_disposition == FILE_CREATE) {
522 				status = NT_STATUS_OBJECT_NAME_COLLISION;
523 				goto errout;
524 			}
525 
526 			if ((op->create_disposition == FILE_SUPERSEDE) ||
527 			    (op->create_disposition == FILE_OVERWRITE_IF) ||
528 			    (op->create_disposition == FILE_OVERWRITE)) {
529 
530 				if (sname == NULL) {
531 					if (!smb_sattr_check(
532 					    op->fqi.fq_fattr.sa_dosattr,
533 					    op->dattr)) {
534 						status =
535 						    NT_STATUS_ACCESS_DENIED;
536 						goto errout;
537 					}
538 					op->desired_access |=
539 					    FILE_WRITE_ATTRIBUTES;
540 				}
541 
542 				if (smb_node_is_dir(fnode)) {
543 					status = NT_STATUS_ACCESS_DENIED;
544 					goto errout;
545 				}
546 			}
547 
548 			/* MS-FSA 2.1.5.1.2 */
549 			if (op->create_disposition == FILE_SUPERSEDE)
550 				op->desired_access |= DELETE;
551 			if ((op->create_disposition == FILE_OVERWRITE_IF) ||
552 			    (op->create_disposition == FILE_OVERWRITE))
553 				op->desired_access |= FILE_WRITE_DATA;
554 		} else if (rc == ENOENT) { /* File Exists, but Stream doesn't */
555 			if (op->create_disposition == FILE_OPEN ||
556 			    op->create_disposition == FILE_OVERWRITE) {
557 				status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
558 				goto errout;
559 			}
560 
561 			op->desired_access |= FILE_WRITE_DATA;
562 		} else { /* Error looking up stream */
563 			status = smb_errno2status(rc);
564 			fnode = tmp_node;
565 			goto errout;
566 		}
567 
568 		/*
569 		 * Windows seems to check read-only access before file
570 		 * sharing check.
571 		 *
572 		 * Check to see if the file is currently readonly (regardless
573 		 * of whether this open will make it readonly).
574 		 * Readonly is ignored on directories.
575 		 */
576 		if (SMB_PATHFILE_IS_READONLY(sr, fnode) &&
577 		    !smb_node_is_dir(fnode)) {
578 			if (op->desired_access &
579 			    (FILE_WRITE_DATA | FILE_APPEND_DATA)) {
580 				status = NT_STATUS_ACCESS_DENIED;
581 				goto errout;
582 			}
583 			if (op->create_options & FILE_DELETE_ON_CLOSE) {
584 				status = NT_STATUS_CANNOT_DELETE;
585 				goto errout;
586 			}
587 		}
588 
589 		/* Dataset roots can't be deleted, so don't set DOC */
590 		if ((op->create_options & FILE_DELETE_ON_CLOSE) != 0 &&
591 		    (fnode->flags & NODE_FLAGS_VFSROOT) != 0) {
592 			status = NT_STATUS_CANNOT_DELETE;
593 			goto errout;
594 		}
595 
596 		status = smb_fsop_access(sr, sr->user_cr, fnode,
597 		    op->desired_access);
598 
599 		if (status != NT_STATUS_SUCCESS)
600 			goto errout;
601 
602 		if (max_requested) {
603 			smb_fsop_eaccess(sr, sr->user_cr, fnode, &max_allowed);
604 			op->desired_access |= max_allowed;
605 		}
606 
607 		/*
608 		 * File owner should always get read control + read attr.
609 		 */
610 		if (crgetuid(sr->user_cr) == op->fqi.fq_fattr.sa_vattr.va_uid)
611 			op->desired_access |=
612 			    (READ_CONTROL | FILE_READ_ATTRIBUTES);
613 
614 		/*
615 		 * According to MS "dochelp" mail in Mar 2015, any handle
616 		 * on which read or write access is granted implicitly
617 		 * gets "read attributes", even if it was not requested.
618 		 */
619 		if ((op->desired_access & FILE_DATA_ALL) != 0)
620 			op->desired_access |= FILE_READ_ATTRIBUTES;
621 
622 		/* If the stream didn't exist, create it now */
623 		if (!stream_found) {
624 			smb_node_t *tmp_node = fnode;
625 
626 			bzero(&new_attr, sizeof (new_attr));
627 			new_attr.sa_vattr.va_type = VREG;
628 			new_attr.sa_vattr.va_mode = S_IRUSR;
629 			new_attr.sa_mask |= SMB_AT_TYPE | SMB_AT_MODE;
630 
631 			rc = smb_fsop_create_stream(sr, sr->user_cr, dnode,
632 			    fnode, sname, lookup_flags, &new_attr, &fnode);
633 			smb_node_release(tmp_node);
634 
635 			if (rc != 0) {
636 				status = smb_errno2status(rc);
637 				fnode_held = B_FALSE;
638 				goto errout;
639 			}
640 			op->action_taken = SMB_OACT_CREATED;
641 			created = B_TRUE;
642 
643 			smb_node_unlock(dnode);
644 			dnode_wlock = B_FALSE;
645 		}
646 
647 		/*
648 		 * Oplock break is done prior to sharing checks as the break
649 		 * may cause other clients to close the file which would
650 		 * affect the sharing checks, and may delete the file due to
651 		 * DELETE_ON_CLOSE. This may block, so set the file opening
652 		 * count before oplock stuff.
653 		 *
654 		 * Need the "proposed" ofile (and its TargetOplockKey) for
655 		 * correct oplock break semantics.
656 		 */
657 		of = smb_ofile_alloc(sr, op, fnode, SMB_FTYPE_DISK,
658 		    tree_fid);
659 		tree_fid = 0; // given to the ofile
660 		uniq_fid = of->f_uniqid;
661 
662 		smb_node_inc_opening_count(fnode);
663 		opening_incr = B_TRUE;
664 
665 		if (!stream_found) {
666 			/*
667 			 * Stake our Share Access claim.
668 			 */
669 			smb_node_wrlock(fnode);
670 			fnode_wlock = B_TRUE;
671 
672 			status = smb_fsop_shrlock(sr->user_cr, fnode, uniq_fid,
673 			    op->desired_access, op->share_access);
674 			if (status != 0)
675 				goto errout;
676 
677 			fnode_shrlk = B_TRUE;
678 			smb_node_unlock(fnode);
679 			fnode_wlock = B_FALSE;
680 			goto stream_created;
681 		}
682 
683 		/*
684 		 * XXX Supposed to do share access checks next.
685 		 * [MS-FSA] describes that as part of access check:
686 		 * 2.1.5.1.2.1 Alg... Check Access to an Existing File
687 		 *
688 		 * If CreateDisposition is FILE_OPEN or FILE_OPEN_IF:
689 		 *   If Open.Stream.Oplock is not empty and
690 		 *   Open.Stream.Oplock.State contains BATCH_OPLOCK,
691 		 *   the object store MUST check for an oplock
692 		 *   break according to the algorithm in section 2.1.4.12,
693 		 *   with input values as follows:
694 		 *	Open equal to this operation's Open
695 		 *	Oplock equal to Open.Stream.Oplock
696 		 *	Operation equal to "OPEN"
697 		 *	OpParams containing two members:
698 		 *	  DesiredAccess, CreateDisposition
699 		 *
700 		 * It's not clear how Windows would ask the FS layer if
701 		 * the file has a BATCH oplock.  We'll use a call to the
702 		 * common oplock code, which calls smb_oplock_break_OPEN
703 		 * only if the oplock state contains BATCH_OPLOCK.
704 		 * See: smb_oplock_break_BATCH()
705 		 *
706 		 * Also note: There's a nearly identical section in the
707 		 * spec. at the start of the "else" part of the above
708 		 * "if (disposition is overwrite, overwrite_if)" so this
709 		 * section (oplock break, the share mode check, and the
710 		 * next oplock_break_HANDLE) are all factored out to be
711 		 * in all cases above that if/else from the spec.
712 		 */
713 		status = smb_oplock_break_BATCH(fnode, of,
714 		    op->desired_access, op->create_disposition);
715 		if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) {
716 			if (sr->session->dialect >= SMB_VERS_2_BASE)
717 				(void) smb2sr_go_async(sr);
718 			status = smb_oplock_wait_break(sr, fnode, 0);
719 		}
720 		if (status != NT_STATUS_SUCCESS)
721 			goto errout;
722 
723 		/*
724 		 * Check for sharing violations, and if any,
725 		 * do oplock break of handle caching.
726 		 *
727 		 * Need node_wrlock during shrlock checks,
728 		 * and not locked during oplock breaks etc.
729 		 */
730 		shrlock_t0 = gethrtime();
731 	shrlock_again:
732 		smb_node_wrlock(fnode);
733 		fnode_wlock = B_TRUE;
734 		status = smb_fsop_shrlock(sr->user_cr, fnode, uniq_fid,
735 		    op->desired_access, op->share_access);
736 		smb_node_unlock(fnode);
737 		fnode_wlock = B_FALSE;
738 
739 		/*
740 		 * [MS-FSA] "OPEN_BREAK_H"
741 		 * If the (proposed) new open would violate sharing rules,
742 		 * indicate an oplock break with OPEN_BREAK_H (to break
743 		 * handle level caching rights) then try again.
744 		 */
745 		if (status == NT_STATUS_SHARING_VIOLATION &&
746 		    did_break_handle == B_FALSE) {
747 			did_break_handle = B_TRUE;
748 
749 			status = smb_oplock_break_HANDLE(fnode, of);
750 			if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) {
751 				if (sr->session->dialect >= SMB_VERS_2_BASE)
752 					(void) smb2sr_go_async(sr);
753 				status = smb_oplock_wait_break(sr, fnode, 0);
754 			} else {
755 				/*
756 				 * Even when the oplock layer does NOT
757 				 * give us the special status indicating
758 				 * we should wait, it may have scheduled
759 				 * taskq jobs that may close handles.
760 				 * Give those a chance to run before we
761 				 * check again for sharing violations.
762 				 */
763 				delay(MSEC_TO_TICK(10));
764 			}
765 			if (status != NT_STATUS_SUCCESS)
766 				goto errout;
767 
768 			goto shrlock_again;
769 		}
770 
771 		/*
772 		 * If we still have orphaned durable handles on this file,
773 		 * let's assume the client has lost interest in those and
774 		 * close them so they don't cause sharing violations.
775 		 * See longer comment at smb2_dh_close_my_orphans().
776 		 */
777 		if (status == NT_STATUS_SHARING_VIOLATION &&
778 		    sr->session->dialect >= SMB_VERS_2_BASE &&
779 		    did_cleanup_orphans == B_FALSE) {
780 
781 			did_cleanup_orphans = B_TRUE;
782 			smb2_dh_close_my_orphans(sr, of);
783 
784 			goto shrlock_again;
785 		}
786 
787 		/*
788 		 * SMB1 expects a 1 sec. delay before returning a
789 		 * sharing violation error.  If breaking oplocks
790 		 * above took less than a sec, wait some more.
791 		 * See: smbtorture base.defer_open
792 		 */
793 		if (status == NT_STATUS_SHARING_VIOLATION &&
794 		    sr->session->dialect < SMB_VERS_2_BASE) {
795 			hrtime_t t1 = shrlock_t0 + NANOSEC;
796 			hrtime_t now = gethrtime();
797 			if (now < t1) {
798 				delay(NSEC_TO_TICK_ROUNDUP(t1 - now));
799 			}
800 		}
801 
802 		if (status != NT_STATUS_SUCCESS)
803 			goto errout;
804 		fnode_shrlk = B_TRUE;
805 
806 		/*
807 		 * The [MS-FSA] spec. describes this oplock break as
808 		 * part of the sharing access checks.  See:
809 		 * 2.1.5.1.2.2 Algorithm to Check Sharing Access...
810 		 * At the end of the share mode tests described there,
811 		 * if it has not returned "sharing violation", it
812 		 * specifies a call to the alg. in sec. 2.1.4.12,
813 		 * that boils down to: smb_oplock_break_OPEN()
814 		 */
815 		status = smb_oplock_break_OPEN(fnode, of,
816 		    op->desired_access,
817 		    op->create_disposition);
818 		if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) {
819 			if (sr->session->dialect >= SMB_VERS_2_BASE)
820 				(void) smb2sr_go_async(sr);
821 			status = smb_oplock_wait_break(sr, fnode, 0);
822 		}
823 		if (status != NT_STATUS_SUCCESS)
824 			goto errout;
825 
826 		if ((fnode->flags & NODE_FLAGS_DELETE_COMMITTED) != 0) {
827 			/*
828 			 * Breaking the oplock caused the file to be deleted,
829 			 * so let's bail and pretend the file wasn't found.
830 			 * Have to duplicate much of the logic found a the
831 			 * "errout" label here.
832 			 *
833 			 * This code path is exercised by smbtorture
834 			 * smb2.durable-open.delete_on_close1
835 			 */
836 			DTRACE_PROBE1(node_deleted, smb_node_t, fnode);
837 			smb_ofile_free(of);
838 			of = NULL;
839 			last_comp_found = B_FALSE;
840 
841 			/*
842 			 * Get all the holds and locks into the state
843 			 * they would have if lookup had failed.
844 			 */
845 			fnode_shrlk = B_FALSE;
846 			smb_fsop_unshrlock(sr->user_cr, fnode, uniq_fid);
847 
848 			opening_incr = B_FALSE;
849 			smb_node_dec_opening_count(fnode);
850 
851 			fnode_held = B_FALSE;
852 			smb_node_release(fnode);
853 
854 			dnode_wlock = B_TRUE;
855 			smb_node_wrlock(dnode);
856 
857 			goto create;
858 		}
859 
860 		/*
861 		 * Go ahead with modifications as necessary.
862 		 */
863 		switch (op->create_disposition) {
864 		case FILE_SUPERSEDE:
865 		case FILE_OVERWRITE_IF:
866 		case FILE_OVERWRITE:
867 			bzero(&new_attr, sizeof (new_attr));
868 			if (sname == NULL) {
869 				op->dattr |= FILE_ATTRIBUTE_ARCHIVE;
870 				/*
871 				 * Don't apply readonly until
872 				 * smb_set_open_attributes
873 				 */
874 				if (op->dattr & FILE_ATTRIBUTE_READONLY) {
875 					op->dattr &= ~FILE_ATTRIBUTE_READONLY;
876 					op->created_readonly = B_TRUE;
877 				}
878 				new_attr.sa_dosattr = op->dattr;
879 			} else {
880 				new_attr.sa_dosattr = FILE_ATTRIBUTE_ARCHIVE;
881 			}
882 
883 			/*
884 			 * Truncate the file data here.
885 			 * We set alloc_size = op->dsize later,
886 			 * after we have an ofile.  See:
887 			 * smb_set_open_attributes
888 			 */
889 			new_attr.sa_vattr.va_size = 0;
890 			new_attr.sa_mask = SMB_AT_DOSATTR | SMB_AT_SIZE;
891 			rc = smb_fsop_setattr(sr, sr->user_cr, fnode,
892 			    &new_attr);
893 			if (rc != 0) {
894 				status = smb_errno2status(rc);
895 				goto errout;
896 			}
897 
898 			/*
899 			 * If file is being replaced, remove existing streams
900 			 */
901 			if (SMB_IS_STREAM(fnode) == 0) {
902 				status = smb_fsop_remove_streams(sr,
903 				    sr->user_cr, fnode);
904 				if (status != 0)
905 					goto errout;
906 			}
907 
908 			op->action_taken = SMB_OACT_TRUNCATED;
909 			break;
910 
911 		default:
912 			/*
913 			 * FILE_OPEN or FILE_OPEN_IF.
914 			 */
915 			/*
916 			 * Ignore any user-specified alloc_size for
917 			 * existing files, to avoid truncation in
918 			 * smb_set_open_attributes
919 			 */
920 			op->dsize = 0L;
921 			op->action_taken = SMB_OACT_OPENED;
922 			break;
923 		}
924 	} else {
925 create:
926 		/* Last component was not found. */
927 		dnode = op->fqi.fq_dnode;
928 
929 		if (is_dir == 0)
930 			is_stream = smb_is_stream_name(pn->pn_path);
931 
932 		if ((op->create_disposition == FILE_OPEN) ||
933 		    (op->create_disposition == FILE_OVERWRITE)) {
934 			status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
935 			goto errout;
936 		}
937 
938 		if ((op->desired_access & ACCESS_SYSTEM_SECURITY) != 0 &&
939 		    !smb_user_has_security_priv(sr->uid_user, sr->user_cr)) {
940 			status = NT_STATUS_ACCESS_DENIED;
941 			goto errout;
942 		}
943 
944 		if (pn->pn_fname && smb_is_invalid_filename(pn->pn_fname)) {
945 			status = NT_STATUS_OBJECT_NAME_INVALID;
946 			goto errout;
947 		}
948 
949 		/*
950 		 * Don't create in directories marked "Delete on close".
951 		 */
952 		if (dnode->flags & NODE_FLAGS_DELETE_ON_CLOSE) {
953 			status = NT_STATUS_DELETE_PENDING;
954 			goto errout;
955 		}
956 
957 		/*
958 		 * Create always sets the DOS attributes, type, and mode
959 		 * in the if/else below (different for file vs directory).
960 		 * Don't set the readonly bit until smb_set_open_attributes
961 		 * or that would prevent this open.  Note that op->dattr
962 		 * needs to be what smb_set_open_attributes will use,
963 		 * except for the readonly bit.
964 		 */
965 		bzero(&new_attr, sizeof (new_attr));
966 		new_attr.sa_mask = SMB_AT_DOSATTR | SMB_AT_TYPE | SMB_AT_MODE;
967 		if (op->dattr & FILE_ATTRIBUTE_READONLY) {
968 			op->dattr &= ~FILE_ATTRIBUTE_READONLY;
969 			op->created_readonly = B_TRUE;
970 		}
971 
972 		/*
973 		 * SMB create can specify the create time.
974 		 */
975 		if ((op->crtime.tv_sec != 0) &&
976 		    (op->crtime.tv_sec != UINT_MAX)) {
977 			new_attr.sa_mask |= SMB_AT_CRTIME;
978 			new_attr.sa_crtime = op->crtime;
979 		}
980 
981 		if (is_dir == 0) {
982 			op->dattr |= FILE_ATTRIBUTE_ARCHIVE;
983 			new_attr.sa_dosattr = op->dattr;
984 			new_attr.sa_vattr.va_type = VREG;
985 			if (is_stream)
986 				new_attr.sa_vattr.va_mode = S_IRUSR | S_IWUSR;
987 			else
988 				new_attr.sa_vattr.va_mode =
989 				    S_IRUSR | S_IRGRP | S_IROTH |
990 				    S_IWUSR | S_IWGRP | S_IWOTH;
991 
992 			/*
993 			 * We set alloc_size = op->dsize later,
994 			 * (in smb_set_open_attributes) after we
995 			 * have an ofile on which to save that.
996 			 *
997 			 * Legacy Open&X sets size to alloc_size
998 			 * when creating a new file.
999 			 */
1000 			if (sr->smb_com == SMB_COM_OPEN_ANDX) {
1001 				new_attr.sa_vattr.va_size = op->dsize;
1002 				new_attr.sa_mask |= SMB_AT_SIZE;
1003 			}
1004 
1005 			rc = smb_fsop_create(sr, sr->user_cr, dnode,
1006 			    op->fqi.fq_last_comp, &new_attr, &op->fqi.fq_fnode);
1007 		} else {
1008 			op->dattr |= FILE_ATTRIBUTE_DIRECTORY;
1009 			new_attr.sa_dosattr = op->dattr;
1010 			new_attr.sa_vattr.va_type = VDIR;
1011 			new_attr.sa_vattr.va_mode = 0777;
1012 
1013 			rc = smb_fsop_mkdir(sr, sr->user_cr, dnode,
1014 			    op->fqi.fq_last_comp, &new_attr, &op->fqi.fq_fnode);
1015 		}
1016 		if (rc != 0) {
1017 			status = smb_errno2status(rc);
1018 			goto errout;
1019 		}
1020 
1021 		/* Create done. */
1022 		smb_node_unlock(dnode);
1023 		dnode_wlock = B_FALSE;
1024 
1025 		created = B_TRUE;
1026 		op->action_taken = SMB_OACT_CREATED;
1027 
1028 		/* Note: hold from create */
1029 		fnode = op->fqi.fq_fnode;
1030 		fnode_held = B_TRUE;
1031 
1032 		if (max_requested) {
1033 			smb_fsop_eaccess(sr, sr->user_cr, fnode, &max_allowed);
1034 			op->desired_access |= max_allowed;
1035 		}
1036 		/*
1037 		 * We created this object (we own it) so grant
1038 		 * read_control + read_attributes on this handle,
1039 		 * even if that was not requested.  This avoids
1040 		 * unexpected access failures later.
1041 		 */
1042 		op->desired_access |= (READ_CONTROL | FILE_READ_ATTRIBUTES);
1043 
1044 		/* Allocate the ofile and fill in most of it. */
1045 		of = smb_ofile_alloc(sr, op, fnode, SMB_FTYPE_DISK,
1046 		    tree_fid);
1047 		tree_fid = 0; // given to the ofile
1048 		uniq_fid = of->f_uniqid;
1049 
1050 		smb_node_inc_opening_count(fnode);
1051 		opening_incr = B_TRUE;
1052 
1053 		/*
1054 		 * Share access checks...
1055 		 */
1056 		smb_node_wrlock(fnode);
1057 		fnode_wlock = B_TRUE;
1058 
1059 		status = smb_fsop_shrlock(sr->user_cr, fnode, uniq_fid,
1060 		    op->desired_access, op->share_access);
1061 		if (status != 0)
1062 			goto errout;
1063 		fnode_shrlk = B_TRUE;
1064 
1065 		/*
1066 		 * MS-FSA 2.1.5.1.1
1067 		 * If the Oplock member of the DirectoryStream in
1068 		 * Link.ParentFile.StreamList (ParentOplock) is
1069 		 * not empty ... oplock break on the parent...
1070 		 * (dnode is the parent directory)
1071 		 *
1072 		 * This compares of->ParentOplockKey with each
1073 		 * oplock of->TargetOplockKey and breaks...
1074 		 * so it's OK that we're passing an OF that's
1075 		 * NOT a member of dnode->n_ofile_list
1076 		 *
1077 		 * The break never blocks, so ignore the return.
1078 		 */
1079 		(void) smb_oplock_break_PARENT(dnode, of);
1080 	}
1081 
1082 stream_created:
1083 	/*
1084 	 * We might have blocked in smb_oplock_break_OPEN long enough
1085 	 * so a tree disconnect might have happened.  In that case,
1086 	 * we would be adding an ofile to a tree that's disconnecting,
1087 	 * which would interfere with tear-down.  If so, error out.
1088 	 */
1089 	if (!smb_tree_is_connected(sr->tid_tree)) {
1090 		status = NT_STATUS_INVALID_PARAMETER;
1091 		goto errout;
1092 	}
1093 
1094 	/*
1095 	 * Moved this up from smb_ofile_open()
1096 	 */
1097 	if ((rc = smb_fsop_open(fnode, of->f_mode, of->f_cr)) != 0) {
1098 		status = smb_errno2status(rc);
1099 		goto errout;
1100 	}
1101 
1102 	/*
1103 	 * Complete this open (add to ofile lists)
1104 	 */
1105 	smb_ofile_open(sr, op, of);
1106 	did_open = B_TRUE;
1107 
1108 	/*
1109 	 * This MUST be done after ofile creation, so that explicitly
1110 	 * set timestamps can be remembered on the ofile, and setting
1111 	 * the readonly flag won't affect access via this open.
1112 	 */
1113 	if ((rc = smb_set_open_attributes(sr, of)) != 0) {
1114 		status = smb_errno2status(rc);
1115 		goto errout;
1116 	}
1117 
1118 	/*
1119 	 * We've already done access checks above,
1120 	 * and want this call to succeed even when
1121 	 * !(desired_access & FILE_READ_ATTRIBUTES),
1122 	 * so pass kcred here.
1123 	 */
1124 	op->fqi.fq_fattr.sa_mask = SMB_AT_ALL;
1125 	(void) smb_node_getattr(sr, fnode, zone_kcred(), of,
1126 	    &op->fqi.fq_fattr);
1127 
1128 	/*
1129 	 * Propagate the write-through mode from the open params
1130 	 * to the node: see the notes in the function header.
1131 	 * XXX: write_through should be a flag on the ofile.
1132 	 */
1133 	if (sr->sr_cfg->skc_sync_enable ||
1134 	    (op->create_options & FILE_WRITE_THROUGH))
1135 		fnode->flags |= NODE_FLAGS_WRITE_THROUGH;
1136 
1137 	/*
1138 	 * Set up the fileid and dosattr in open_param for response
1139 	 */
1140 	op->fileid = op->fqi.fq_fattr.sa_vattr.va_nodeid;
1141 	op->dattr = op->fqi.fq_fattr.sa_dosattr;
1142 
1143 	/*
1144 	 * Set up the file type in open_param for the response
1145 	 */
1146 	op->ftype = SMB_FTYPE_DISK;
1147 	sr->smb_fid = of->f_fid;
1148 	sr->fid_ofile = of;
1149 
1150 	if (smb_node_is_file(fnode)) {
1151 		op->dsize = op->fqi.fq_fattr.sa_vattr.va_size;
1152 	} else {
1153 		/* directory or symlink */
1154 		op->dsize = 0;
1155 	}
1156 
1157 	/*
1158 	 * Note: oplock_acquire happens in callers, because
1159 	 * how that happens is protocol-specific.
1160 	 */
1161 
1162 	if (sname != NULL)
1163 		kmem_free(sname, MAXNAMELEN);
1164 	if (fnode_wlock)
1165 		smb_node_unlock(fnode);
1166 	if (opening_incr)
1167 		smb_node_dec_opening_count(fnode);
1168 	if (fnode_held)
1169 		smb_node_release(fnode);
1170 	if (dnode_wlock)
1171 		smb_node_unlock(dnode);
1172 	if (dnode_held)
1173 		smb_node_release(dnode);
1174 
1175 	return (NT_STATUS_SUCCESS);
1176 
1177 errout:
1178 	if (did_open) {
1179 		smb_ofile_close(of, 0);
1180 		/* rele via sr->fid_ofile */
1181 	} else if (of != NULL) {
1182 		/* No other refs possible */
1183 		smb_ofile_free(of);
1184 	}
1185 
1186 	if (fnode_shrlk)
1187 		smb_fsop_unshrlock(sr->user_cr, fnode, uniq_fid);
1188 
1189 	if (created) {
1190 		/* Try to roll-back create. */
1191 		smb_delete_new_object(sr);
1192 	}
1193 
1194 	if (sname != NULL)
1195 		kmem_free(sname, MAXNAMELEN);
1196 	if (fnode_wlock)
1197 		smb_node_unlock(fnode);
1198 	if (opening_incr)
1199 		smb_node_dec_opening_count(fnode);
1200 	if (fnode_held)
1201 		smb_node_release(fnode);
1202 	if (dnode_wlock)
1203 		smb_node_unlock(dnode);
1204 	if (dnode_held)
1205 		smb_node_release(dnode);
1206 
1207 	if (tree_fid != 0)
1208 		smb_idpool_free(&tree->t_fid_pool, tree_fid);
1209 
1210 	return (status);
1211 }
1212 
1213 /*
1214  * smb_set_open_attributes
1215  *
1216  * Last write time:
1217  * - If the last_write time specified in the open params is not 0 or -1,
1218  *   use it as file's mtime. This will be considered an explicitly set
1219  *   timestamps, not reset by subsequent writes.
1220  *
1221  * DOS attributes
1222  * - If we created_readonly, we now store the real DOS attributes
1223  *   (including the readonly bit) so subsequent opens will see it.
1224  *
1225  * Returns: errno
1226  */
1227 static int
1228 smb_set_open_attributes(smb_request_t *sr, smb_ofile_t *of)
1229 {
1230 	smb_attr_t	attr;
1231 	smb_arg_open_t	*op = &sr->sr_open;
1232 	smb_node_t	*node = of->f_node;
1233 	int		rc = 0;
1234 
1235 	bzero(&attr, sizeof (smb_attr_t));
1236 
1237 	if (op->created_readonly) {
1238 		attr.sa_dosattr = op->dattr | FILE_ATTRIBUTE_READONLY;
1239 		attr.sa_mask |= SMB_AT_DOSATTR;
1240 	}
1241 
1242 	if (op->dsize != 0) {
1243 		attr.sa_allocsz = op->dsize;
1244 		attr.sa_mask |= SMB_AT_ALLOCSZ;
1245 	}
1246 
1247 	if ((op->mtime.tv_sec != 0) && (op->mtime.tv_sec != UINT_MAX)) {
1248 		attr.sa_vattr.va_mtime = op->mtime;
1249 		attr.sa_mask |= SMB_AT_MTIME;
1250 	}
1251 
1252 	if (attr.sa_mask != 0)
1253 		rc = smb_node_setattr(sr, node, of->f_cr, of, &attr);
1254 
1255 	return (rc);
1256 }
1257 
1258 /*
1259  * This function is used to delete a newly created object (file or
1260  * directory) if an error occurs after creation of the object.
1261  */
1262 static void
1263 smb_delete_new_object(smb_request_t *sr)
1264 {
1265 	smb_arg_open_t	*op = &sr->sr_open;
1266 	smb_fqi_t	*fqi = &(op->fqi);
1267 	uint32_t	flags = 0;
1268 
1269 	if (SMB_TREE_IS_CASEINSENSITIVE(sr))
1270 		flags |= SMB_IGNORE_CASE;
1271 	if (SMB_TREE_SUPPORTS_CATIA(sr))
1272 		flags |= SMB_CATIA;
1273 
1274 	if (op->create_options & FILE_DIRECTORY_FILE)
1275 		(void) smb_fsop_rmdir(sr, sr->user_cr, fqi->fq_dnode,
1276 		    fqi->fq_last_comp, flags);
1277 	else
1278 		(void) smb_fsop_remove(sr, sr->user_cr, fqi->fq_dnode,
1279 		    fqi->fq_last_comp, flags);
1280 }
1281