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  */
21da6c28aaSamw /*
222c2961f8Sjose borrego  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23da6c28aaSamw  * Use is subject to license terms.
240897f7fbSGordon Ross  *
25*93bc28dbSGordon Ross  * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
26da6c28aaSamw  */
27da6c28aaSamw /*
28da6c28aaSamw  * SMB: lock_byte_range
29da6c28aaSamw  *
30da6c28aaSamw  * The lock record message is sent to lock the given byte range.  More than
31da6c28aaSamw  * one non-overlapping byte range may be locked in a given file.  Locks
32da6c28aaSamw  * prevent attempts to lock, read or write the locked portion of the file
33da6c28aaSamw  * by other clients or Pids.  Overlapping locks are not allowed. Offsets
34da6c28aaSamw  * beyond the current end of file may be locked.  Such locks will not cause
35da6c28aaSamw  * allocation of file space.
36da6c28aaSamw  *
37da6c28aaSamw  * Since Offset is a 32 bit quantity, this request is inappropriate for
38da6c28aaSamw  * general locking within a very large file.
39da6c28aaSamw  *
40da6c28aaSamw  * Client Request                     Description
41da6c28aaSamw  * ================================== =================================
42da6c28aaSamw  *
43da6c28aaSamw  * UCHAR WordCount;                   Count of parameter words = 5
44da6c28aaSamw  * USHORT Fid;                        File handle
45da6c28aaSamw  * ULONG Count;                       Count of bytes to lock
46da6c28aaSamw  * ULONG Offset;                      Offset from start of file
47da6c28aaSamw  * USHORT ByteCount;                  Count of data bytes = 0
48da6c28aaSamw  *
49da6c28aaSamw  * Locks may only be unlocked by the Pid that performed the lock.
50da6c28aaSamw  *
51da6c28aaSamw  * Server Response                    Description
52da6c28aaSamw  * ================================== =================================
53da6c28aaSamw  *
54da6c28aaSamw  * UCHAR WordCount;                   Count of parameter words = 0
55da6c28aaSamw  * USHORT ByteCount;                  Count of data bytes = 0
56da6c28aaSamw  *
57da6c28aaSamw  * This client request does not wait for the lock to be granted.  If the
58da6c28aaSamw  * lock can not be immediately granted (within 200-300 ms), the server
59da6c28aaSamw  * should return failure to the client
60da6c28aaSamw  */
61da6c28aaSamw 
62bbf6f00cSJordan Brown #include <smbsrv/smb_kproto.h>
63da6c28aaSamw 
64faa1795aSjb smb_sdrc_t
smb_pre_lock_byte_range(smb_request_t * sr)65faa1795aSjb smb_pre_lock_byte_range(smb_request_t *sr)
66faa1795aSjb {
67*93bc28dbSGordon Ross 	DTRACE_SMB_START(op__LockByteRange, smb_request_t *, sr);
68faa1795aSjb 	return (SDRC_SUCCESS);
69faa1795aSjb }
70faa1795aSjb 
71faa1795aSjb void
smb_post_lock_byte_range(smb_request_t * sr)72faa1795aSjb smb_post_lock_byte_range(smb_request_t *sr)
73faa1795aSjb {
74*93bc28dbSGordon Ross 	DTRACE_SMB_DONE(op__LockByteRange, smb_request_t *, sr);
75faa1795aSjb }
76faa1795aSjb 
770897f7fbSGordon Ross /*
780897f7fbSGordon Ross  * Legacy SMB command; takes an exclusive byte-range lock
790897f7fbSGordon Ross  */
807b59d02dSjb smb_sdrc_t
smb_com_lock_byte_range(struct smb_request * sr)81da6c28aaSamw smb_com_lock_byte_range(struct smb_request *sr)
82da6c28aaSamw {
83da6c28aaSamw 	uint32_t	count;
84da6c28aaSamw 	uint32_t	off;
850897f7fbSGordon Ross 	uint32_t	lk_pid;
86da6c28aaSamw 	DWORD		result;
877b59d02dSjb 	int		rc;
88da6c28aaSamw 
897b59d02dSjb 	if (smbsr_decode_vwv(sr, "wll", &sr->smb_fid, &count, &off) != 0)
90faa1795aSjb 		return (SDRC_ERROR);
91da6c28aaSamw 
922c2961f8Sjose borrego 	smbsr_lookup_file(sr);
93da6c28aaSamw 	if (sr->fid_ofile == NULL) {
94dc20a302Sas 		smbsr_error(sr, NT_STATUS_INVALID_HANDLE,
95da6c28aaSamw 		    ERRDOS, ERRbadfid);
96faa1795aSjb 		return (SDRC_ERROR);
97da6c28aaSamw 	}
98da6c28aaSamw 
990897f7fbSGordon Ross 	/* Note: SMB1 locking uses 16-bit PIDs. */
1000897f7fbSGordon Ross 	lk_pid = sr->smb_pid & 0xFFFF;
1010897f7fbSGordon Ross 
1020897f7fbSGordon Ross 	result = smb_lock_range(sr, (u_offset_t)off, (uint64_t)count,
1030897f7fbSGordon Ross 	    lk_pid, SMB_LOCK_TYPE_READWRITE, 0);
104da6c28aaSamw 	if (result != NT_STATUS_SUCCESS) {
105dc20a302Sas 		smb_lock_range_error(sr, result);
106faa1795aSjb 		return (SDRC_ERROR);
107da6c28aaSamw 	}
108da6c28aaSamw 
1097b59d02dSjb 	rc = smbsr_encode_empty_result(sr);
110faa1795aSjb 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
111da6c28aaSamw }
112