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