xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_locking_andx.c (revision a90cf9f29973990687fa61de9f1f6ea22e924e40)
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.
249c787c76SGordon Ross  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
25da6c28aaSamw  */
26148c5f43SAlan Wright 
27da6c28aaSamw /*
28da6c28aaSamw  * SMB: locking_andx
29da6c28aaSamw  *
30da6c28aaSamw  * SMB_COM_LOCKING_ANDX allows both locking and/or unlocking of file range(s).
31da6c28aaSamw  *
32da6c28aaSamw  *  Client Request                     Description
33da6c28aaSamw  *  ================================== =================================
34da6c28aaSamw  *
35da6c28aaSamw  *  UCHAR WordCount;                   Count of parameter words = 8
36da6c28aaSamw  *  UCHAR AndXCommand;                 Secondary (X) command;  0xFF = none
37da6c28aaSamw  *  UCHAR AndXReserved;                Reserved (must be 0)
38da6c28aaSamw  *  USHORT AndXOffset;                 Offset to next command WordCount
39da6c28aaSamw  *  USHORT Fid;                        File handle
40da6c28aaSamw  *  UCHAR LockType;                    See LockType table below
41da6c28aaSamw  *  UCHAR OplockLevel;                 The new oplock level
42da6c28aaSamw  *  ULONG Timeout;                     Milliseconds to wait for unlock
43da6c28aaSamw  *  USHORT NumberOfUnlocks;            Num. unlock range structs following
44da6c28aaSamw  *  USHORT NumberOfLocks;              Num. lock range structs following
45da6c28aaSamw  *  USHORT ByteCount;                  Count of data bytes
46da6c28aaSamw  *  LOCKING_ANDX_RANGE Unlocks[];      Unlock ranges
47da6c28aaSamw  *  LOCKING_ANDX_RANGE Locks[];        Lock ranges
48da6c28aaSamw  *
49da6c28aaSamw  *  LockType Flag Name            Value Description
50da6c28aaSamw  *  ============================  ===== ================================
51da6c28aaSamw  *
52da6c28aaSamw  *  LOCKING_ANDX_SHARED_LOCK      0x01  Read-only lock
53da6c28aaSamw  *  LOCKING_ANDX_OPLOCK_RELEASE   0x02  Oplock break notification
54da6c28aaSamw  *  LOCKING_ANDX_CHANGE_LOCKTYPE  0x04  Change lock type
55da6c28aaSamw  *  LOCKING_ANDX_CANCEL_LOCK      0x08  Cancel outstanding request
56da6c28aaSamw  *  LOCKING_ANDX_LARGE_FILES      0x10  Large file locking format
57da6c28aaSamw  *
58da6c28aaSamw  *  LOCKING_ANDX_RANGE Format
59da6c28aaSamw  *  =====================================================================
60da6c28aaSamw  *
61da6c28aaSamw  *  USHORT Pid;                        PID of process "owning" lock
62da6c28aaSamw  *  ULONG Offset;                      Offset to bytes to [un]lock
63da6c28aaSamw  *  ULONG Length;                      Number of bytes to [un]lock
64da6c28aaSamw  *
65da6c28aaSamw  *  Large File LOCKING_ANDX_RANGE Format
66da6c28aaSamw  *  =====================================================================
67da6c28aaSamw  *
68da6c28aaSamw  *  USHORT Pid;                        PID of process "owning" lock
69da6c28aaSamw  *  USHORT Pad;                        Pad to DWORD align (mbz)
70da6c28aaSamw  *  ULONG OffsetHigh;                  Offset to bytes to [un]lock
71da6c28aaSamw  *                                      (high)
72da6c28aaSamw  *  ULONG OffsetLow;                   Offset to bytes to [un]lock (low)
73da6c28aaSamw  *  ULONG LengthHigh;                  Number of bytes to [un]lock
74da6c28aaSamw  *                                      (high)
75da6c28aaSamw  *  ULONG LengthLow;                   Number of bytes to [un]lock (low)
76da6c28aaSamw  *
77da6c28aaSamw  *  Server Response                    Description
78da6c28aaSamw  *  ================================== =================================
79da6c28aaSamw  *
80da6c28aaSamw  *  UCHAR WordCount;                   Count of parameter words = 2
81da6c28aaSamw  *  UCHAR AndXCommand;                 Secondary (X) command;  0xFF =
82da6c28aaSamw  *                                      none
83da6c28aaSamw  *  UCHAR AndXReserved;                Reserved (must be 0)
84da6c28aaSamw  *  USHORT AndXOffset;                 Offset to next command WordCount
85da6c28aaSamw  *  USHORT ByteCount;                  Count of data bytes = 0
86da6c28aaSamw  *
87da6c28aaSamw  * Locking is a simple mechanism for excluding other processes read/write
88da6c28aaSamw  * access to regions of a file.  The locked regions can be anywhere in the
89da6c28aaSamw  * logical file.  Locking beyond end-of-file is permitted.  Any process
90da6c28aaSamw  * using the Fid specified in this request's Fid has access to the locked
91da6c28aaSamw  * bytes, other processes will be denied the locking of the same bytes.
92da6c28aaSamw  *
93da6c28aaSamw  * The proper method for using locks is not to rely on being denied read or
94da6c28aaSamw  * write access on any of the read/write protocols but rather to attempt
95da6c28aaSamw  * the locking protocol and proceed with the read/write only if the locks
96da6c28aaSamw  * succeeded.
97da6c28aaSamw  *
98da6c28aaSamw  * Locking a range of bytes will fail if any subranges or overlapping
99da6c28aaSamw  * ranges are locked.  In other words, if any of the specified bytes are
100da6c28aaSamw  * already locked, the lock will fail.
101da6c28aaSamw  *
102da6c28aaSamw  * If NumberOfUnlocks is non-zero, the Unlocks vector contains
103da6c28aaSamw  * NumberOfUnlocks elements.  Each element requests that a lock at Offset
104da6c28aaSamw  * of Length be released.  If NumberOfLocks is nonzero, the Locks vector
105da6c28aaSamw  * contains NumberOfLocks elements.  Each element requests the acquisition
106da6c28aaSamw  * of a lock at Offset of Length.
107da6c28aaSamw  *
108da6c28aaSamw  * Timeout is the maximum amount of time to wait for the byte range(s)
109da6c28aaSamw  * specified to become unlocked.  A timeout value of 0 indicates that the
110da6c28aaSamw  * server should fail immediately if any lock range specified is locked.  A
111da6c28aaSamw  *
112da6c28aaSamw  * timeout value of -1 indicates that the server should wait as long as it
113da6c28aaSamw  * takes for each byte range specified to become unlocked so that it may be
114da6c28aaSamw  * again locked by this protocol.  Any other value of smb_timeout specifies
115da6c28aaSamw  * the maximum number of milliseconds to wait for all lock range(s)
116da6c28aaSamw  * specified to become available.
117da6c28aaSamw  *
118da6c28aaSamw  * If any of the lock ranges timeout because of the area to be locked is
119da6c28aaSamw  * already locked (or the lock fails), the other ranges in the protocol
120da6c28aaSamw  * request which were successfully locked as a result of this protocol will
121da6c28aaSamw  * be unlocked (either all requested ranges will be locked when this
122da6c28aaSamw  * protocol returns to the client or none).
123da6c28aaSamw  *
124da6c28aaSamw  * If LockType has the LOCKING_ANDX_SHARED_LOCK flag set, the lock is
125da6c28aaSamw  * specified as a shared lock.  Locks for both read and write (where
126da6c28aaSamw  * LOCKING_ANDX_SHARED_LOCK is clear) should be prohibited, but other
127da6c28aaSamw  * shared locks should be permitted.  If shared locks can not be supported
128da6c28aaSamw  * by a server, the server should map the lock to a lock for both read and
129da6c28aaSamw  * write.  Closing a file with locks still in force causes the locks to be
130da6c28aaSamw  * released in no defined order.
131da6c28aaSamw  *
132da6c28aaSamw  * If LockType has the LOCKING_ANDX_LARGE_FILES flag set and if the
133da6c28aaSamw  * negotiated protocol is NT LM 0.12 or later, then the Locks and Unlocks
134da6c28aaSamw  * vectors are in the Large File LOCKING_ANDX_RANGE format.  This allows
135da6c28aaSamw  * specification of 64 bit offsets for very large files.
136da6c28aaSamw  *
137da6c28aaSamw  * If the one and only member of the Locks vector has the
138da6c28aaSamw  * LOCKING_ANDX_CANCEL_LOCK flag set in the LockType field, the client is
139da6c28aaSamw  * requesting the server to cancel a previously requested, but not yet
140da6c28aaSamw  * responded to, lock.
141da6c28aaSamw  *
142da6c28aaSamw  * If LockType has the LOCKING_ANDX_CHANGE_LOCKTYPE flag set, the client is
143da6c28aaSamw  * requesting that the server atomically change the lock type from a shared
144da6c28aaSamw  * lock to an exclusive lock or vice versa.  If the server can not do this
145da6c28aaSamw  * in an atomic fashion, the server must reject this request.  NT and W95
146da6c28aaSamw  * servers do not support this capability.
147da6c28aaSamw  *
148da6c28aaSamw  * Oplocks are described in the "Opportunistic Locks" section elsewhere in
149da6c28aaSamw  * this document.  A client requests an oplock by setting the appropriate
150da6c28aaSamw  * bit in the SMB_COM_OPEN_ANDX request when the file is being opened in a
151da6c28aaSamw  * mode which is not exclusive.  The server responds by setting the
152da6c28aaSamw  * appropriate bit in the response SMB indicating whether or not the oplock
153da6c28aaSamw  * was granted.  By granting the oplock, the server tells the client the
154da6c28aaSamw  * file is currently only being used by this one client process at the
155da6c28aaSamw  * current time.  The client can therefore safely do read ahead and write
156da6c28aaSamw  * behind as well as local caching of file locks knowing that the file will
157da6c28aaSamw  * not be accessed/changed in any way by another process while the oplock
158da6c28aaSamw  * is in effect.  The client will be notified when any other process
159da6c28aaSamw  * attempts to open or modify the oplocked file.
160da6c28aaSamw  *
161da6c28aaSamw  * When another user attempts to open or otherwise modify the file which a
162da6c28aaSamw  * client has oplocked, the server delays the second attempt and notifies
163da6c28aaSamw  * the client via an SMB_LOCKING_ANDX SMB asynchronously sent from the
164da6c28aaSamw  * server to the client.  This message has the LOCKING_ANDX_OPLOCK_RELEASE
165da6c28aaSamw  * flag set indicating to the client that the oplock is being broken.
166da6c28aaSamw  *
167da6c28aaSamw  * OplockLevel indicates the type of oplock the client now owns. If
168da6c28aaSamw  * OplockLevel is 0, the client possesses no oplocks on the file at all, if
169da6c28aaSamw  * OplockLevel is 1 the client possesses a Level II oplock.  The client is
170da6c28aaSamw  * expected to flush any dirty buffers to the server, submit any file locks
171da6c28aaSamw  * and respond to the server with either an SMB_LOCKING_ANDX SMB having the
172da6c28aaSamw  * LOCKING_ANDX_OPLOCK_RELEASE flag set, or with a file close if the file
173da6c28aaSamw  * is no longer in use by the client.  If the client sends an
174da6c28aaSamw  * SMB_LOCKING_ANDX SMB with the LOCKING_ANDX_OPLOCK_RELEASE flag set and
175da6c28aaSamw  * NumberOfLocks is zero, the server does not send a response.  Since a
176da6c28aaSamw  * close being sent to the server and break oplock notification from the
177da6c28aaSamw  * server could cross on the wire, if the client gets an oplock
178da6c28aaSamw  * notification on a file which it does not have open, that notification
179da6c28aaSamw  * should be ignored.
180da6c28aaSamw  *
181da6c28aaSamw  * Due to timing, the client could get an "oplock broken" notification in a
182da6c28aaSamw  * user's data buffer as a result of this notification crossing on the wire
183da6c28aaSamw  * with a SMB_COM_READ_RAW request.  The client must detect this (use
184da6c28aaSamw  * length of msg, "FFSMB", MID of -1 and Command of SMB_COM_LOCKING_ANDX)
185da6c28aaSamw  * and honor the "oplock broken" notification as usual.  The server must
186da6c28aaSamw  * also note on receipt of an SMB_COM_READ_RAW request that there is an
187da6c28aaSamw  * outstanding (unanswered) "oplock broken" notification to the client and
188da6c28aaSamw  * return a zero length response denoting failure of the read raw request.
189da6c28aaSamw  * The client should (after responding to the "oplock broken"
190da6c28aaSamw  * notification), use a standard read protocol to redo the read request.
191da6c28aaSamw  * This allows a file to actually contain data matching an "oplock broken"
192da6c28aaSamw  * notification and still be read correctly.
193da6c28aaSamw  *
194da6c28aaSamw  * The entire message sent and received including the optional second
195da6c28aaSamw  * protocol must fit in the negotiated maximum transfer size.  The
196da6c28aaSamw  * following are the only valid SMB commands for AndXCommand for
197da6c28aaSamw  * SMB_COM_LOCKING_ANDX:
198da6c28aaSamw  *
199da6c28aaSamw  *     SMB_COM_READ       SMB_COM_READ_ANDX
200da6c28aaSamw  *     SMB_COM_WRITE      SMB_COM_WRITE_ANDX
201da6c28aaSamw  *     SMB_COM_FLUSH
202da6c28aaSamw  *
203da6c28aaSamw  * 4.2.6.1   Errors
204da6c28aaSamw  *
205da6c28aaSamw  * ERRDOS/ERRbadfile
206da6c28aaSamw  * ERRDOS/ERRbadfid
207da6c28aaSamw  * ERRDOS/ERRlock
208da6c28aaSamw  * ERRDOS/ERRinvdevice
209da6c28aaSamw  * ERRSRV/ERRinvid
210da6c28aaSamw  * ERRSRV/ERRbaduid
211da6c28aaSamw  */
212da6c28aaSamw 
213bbf6f00cSJordan Brown #include <smbsrv/smb_kproto.h>
214da6c28aaSamw 
2157b59d02dSjb150015 smb_sdrc_t
216faa1795aSjb150015 smb_pre_locking_andx(smb_request_t *sr)
217faa1795aSjb150015 {
218faa1795aSjb150015 	DTRACE_SMB_1(op__LockingX__start, smb_request_t *, sr);
219faa1795aSjb150015 	return (SDRC_SUCCESS);
220faa1795aSjb150015 }
221faa1795aSjb150015 
222faa1795aSjb150015 void
223faa1795aSjb150015 smb_post_locking_andx(smb_request_t *sr)
224faa1795aSjb150015 {
225faa1795aSjb150015 	DTRACE_SMB_1(op__LockingX__done, smb_request_t *, sr);
226faa1795aSjb150015 }
227faa1795aSjb150015 
228faa1795aSjb150015 smb_sdrc_t
229faa1795aSjb150015 smb_com_locking_andx(smb_request_t *sr)
230da6c28aaSamw {
231da6c28aaSamw 	unsigned short	i;
232da6c28aaSamw 	unsigned char	lock_type;	/* See lock_type table above */
233da6c28aaSamw 	unsigned char	oplock_level;	/* The new oplock level */
234da6c28aaSamw 	uint32_t	timeout;	/* Milliseconds to wait for lock */
235da6c28aaSamw 	unsigned short	unlock_num;	/* # unlock range structs */
236da6c28aaSamw 	unsigned short	lock_num;	/* # lock range structs */
237*a90cf9f2SGordon Ross 	uint32_t	save_pid;	/* Process Id of owner */
238da6c28aaSamw 	uint32_t	offset32, length32;
239da6c28aaSamw 	uint64_t	offset64;
240da6c28aaSamw 	uint64_t	length64;
241da6c28aaSamw 	DWORD		result;
242da6c28aaSamw 	int 		rc;
243da6c28aaSamw 	uint32_t	ltype;
244cb174861Sjoyce mcintosh 	smb_ofile_t	*ofile;
245*a90cf9f2SGordon Ross 	uint16_t	tmp_pid;	/* locking uses 16-bit pids */
246cb174861Sjoyce mcintosh 	uint8_t		brk;
247da6c28aaSamw 
248da6c28aaSamw 	rc = smbsr_decode_vwv(sr, "4.wbblww", &sr->smb_fid, &lock_type,
249da6c28aaSamw 	    &oplock_level, &timeout, &unlock_num, &lock_num);
2507b59d02dSjb150015 	if (rc != 0)
251faa1795aSjb150015 		return (SDRC_ERROR);
252da6c28aaSamw 
2532c2961f8Sjose borrego 	smbsr_lookup_file(sr);
254da6c28aaSamw 	if (sr->fid_ofile == NULL) {
255dc20a302Sas200622 		smbsr_error(sr, NT_STATUS_INVALID_HANDLE, ERRDOS, ERRbadfid);
256faa1795aSjb150015 		return (SDRC_ERROR);
257da6c28aaSamw 	}
258cb174861Sjoyce mcintosh 	ofile = sr->fid_ofile;
2599c787c76SGordon Ross 	if (ofile->f_node == NULL) {
2609c787c76SGordon Ross 		smbsr_error(sr, NT_STATUS_INVALID_PARAMETER,
2619c787c76SGordon Ross 		    ERRDOS, ERROR_INVALID_PARAMETER);
2629c787c76SGordon Ross 		return (SDRC_ERROR);
2639c787c76SGordon Ross 	}
264da6c28aaSamw 
265da6c28aaSamw 	if (lock_type & LOCKING_ANDX_SHARED_LOCK)
266da6c28aaSamw 		ltype = SMB_LOCK_TYPE_READONLY;
267da6c28aaSamw 	else
268da6c28aaSamw 		ltype = SMB_LOCK_TYPE_READWRITE;
269da6c28aaSamw 
270*a90cf9f2SGordon Ross 	save_pid = sr->smb_pid;	/* Save the original pid */
271da6c28aaSamw 
272da6c28aaSamw 	if (lock_type & LOCKING_ANDX_OPLOCK_RELEASE) {
273cb174861Sjoyce mcintosh 		if (oplock_level == 0)
274cb174861Sjoyce mcintosh 			brk = SMB_OPLOCK_BREAK_TO_NONE;
275cb174861Sjoyce mcintosh 		else
276cb174861Sjoyce mcintosh 			brk = SMB_OPLOCK_BREAK_TO_LEVEL_II;
277cb174861Sjoyce mcintosh 		smb_oplock_ack(ofile->f_node, ofile, brk);
278da6c28aaSamw 		if (unlock_num == 0 && lock_num == 0)
279da6c28aaSamw 			return (SDRC_NO_REPLY);
280da6c28aaSamw 	}
281da6c28aaSamw 
282da6c28aaSamw 	/*
283da6c28aaSamw 	 * No support for changing locktype (although we could probably
284da6c28aaSamw 	 * implement this)
285da6c28aaSamw 	 */
286da6c28aaSamw 	if (lock_type & LOCKING_ANDX_CHANGE_LOCK_TYPE) {
287148c5f43SAlan Wright 		smbsr_error(sr, 0, ERRDOS,
288148c5f43SAlan Wright 		    ERROR_ATOMIC_LOCKS_NOT_SUPPORTED);
289faa1795aSjb150015 		return (SDRC_ERROR);
290da6c28aaSamw 	}
291da6c28aaSamw 
292da6c28aaSamw 	/*
293da6c28aaSamw 	 * No support for cancel lock (smbtorture expects this)
294da6c28aaSamw 	 */
295da6c28aaSamw 	if (lock_type & LOCKING_ANDX_CANCEL_LOCK) {
296dc20a302Sas200622 		smbsr_error(sr, NT_STATUS_INVALID_PARAMETER,
297da6c28aaSamw 		    ERRDOS, ERROR_INVALID_PARAMETER);
298faa1795aSjb150015 		return (SDRC_ERROR);
299da6c28aaSamw 	}
300da6c28aaSamw 
301da6c28aaSamw 	if (lock_type & LOCKING_ANDX_LARGE_FILES) {
302da6c28aaSamw 		/*
303da6c28aaSamw 		 * negotiated protocol should be NT LM 0.12 or later
304da6c28aaSamw 		 */
305da6c28aaSamw 		if (sr->session->dialect < NT_LM_0_12) {
306dc20a302Sas200622 			smbsr_error(sr, NT_STATUS_INVALID_PARAMETER,
307da6c28aaSamw 			    ERRDOS, ERROR_INVALID_PARAMETER);
308faa1795aSjb150015 			return (SDRC_ERROR);
309da6c28aaSamw 		}
310da6c28aaSamw 
311da6c28aaSamw 		for (i = 0; i < unlock_num; i++) {
3123db3f65cSamw 			rc = smb_mbc_decodef(&sr->smb_data, "w2.QQ",
313*a90cf9f2SGordon Ross 			    &tmp_pid, &offset64, &length64);
314da6c28aaSamw 			if (rc) {
315da6c28aaSamw 				/*
316dc20a302Sas200622 				 * This is the error returned by Windows 2000
317dc20a302Sas200622 				 * even when STATUS32 has been negotiated.
318da6c28aaSamw 				 */
319dc20a302Sas200622 				smbsr_error(sr, 0, ERRSRV, ERRerror);
320faa1795aSjb150015 				return (SDRC_ERROR);
321da6c28aaSamw 			}
322*a90cf9f2SGordon Ross 			sr->smb_pid = tmp_pid;	/* NB: 16-bit */
323da6c28aaSamw 
324da6c28aaSamw 			result = smb_unlock_range(sr, sr->fid_ofile->f_node,
325da6c28aaSamw 			    offset64, length64);
326da6c28aaSamw 			if (result != NT_STATUS_SUCCESS) {
327dc20a302Sas200622 				smbsr_error(sr, NT_STATUS_RANGE_NOT_LOCKED,
328148c5f43SAlan Wright 				    ERRDOS, ERROR_NOT_LOCKED);
329faa1795aSjb150015 				return (SDRC_ERROR);
330da6c28aaSamw 			}
331da6c28aaSamw 		}
332da6c28aaSamw 
333da6c28aaSamw 		for (i = 0; i < lock_num; i++) {
3343db3f65cSamw 			rc = smb_mbc_decodef(&sr->smb_data, "w2.QQ",
335*a90cf9f2SGordon Ross 			    &tmp_pid, &offset64, &length64);
336da6c28aaSamw 			if (rc) {
337dc20a302Sas200622 				smbsr_error(sr, 0, ERRSRV, ERRerror);
338faa1795aSjb150015 				return (SDRC_ERROR);
339da6c28aaSamw 			}
340*a90cf9f2SGordon Ross 			sr->smb_pid = tmp_pid;	/* NB: 16-bit */
341da6c28aaSamw 
3426537f381Sas200622 			result = smb_lock_range(sr, offset64, length64, timeout,
3436537f381Sas200622 			    ltype);
344da6c28aaSamw 			if (result != NT_STATUS_SUCCESS) {
345dc20a302Sas200622 				smb_lock_range_error(sr, result);
346faa1795aSjb150015 				return (SDRC_ERROR);
347da6c28aaSamw 			}
348da6c28aaSamw 		}
349da6c28aaSamw 	} else {
350da6c28aaSamw 		for (i = 0; i < unlock_num; i++) {
351*a90cf9f2SGordon Ross 			rc = smb_mbc_decodef(&sr->smb_data, "wll", &tmp_pid,
352da6c28aaSamw 			    &offset32, &length32);
353da6c28aaSamw 			if (rc) {
354dc20a302Sas200622 				smbsr_error(sr, 0, ERRSRV, ERRerror);
355faa1795aSjb150015 				return (SDRC_ERROR);
356da6c28aaSamw 			}
357*a90cf9f2SGordon Ross 			sr->smb_pid = tmp_pid;	/* NB: 16-bit */
358da6c28aaSamw 
359da6c28aaSamw 			result = smb_unlock_range(sr, sr->fid_ofile->f_node,
360da6c28aaSamw 			    (uint64_t)offset32, (uint64_t)length32);
361da6c28aaSamw 			if (result != NT_STATUS_SUCCESS) {
362dc20a302Sas200622 				smbsr_error(sr, NT_STATUS_RANGE_NOT_LOCKED,
363148c5f43SAlan Wright 				    ERRDOS, ERROR_NOT_LOCKED);
364faa1795aSjb150015 				return (SDRC_ERROR);
365da6c28aaSamw 			}
366da6c28aaSamw 		}
367da6c28aaSamw 
368da6c28aaSamw 		for (i = 0; i < lock_num; i++) {
369*a90cf9f2SGordon Ross 			rc = smb_mbc_decodef(&sr->smb_data, "wll", &tmp_pid,
370da6c28aaSamw 			    &offset32, &length32);
371da6c28aaSamw 			if (rc) {
372dc20a302Sas200622 				smbsr_error(sr, 0, ERRSRV, ERRerror);
373faa1795aSjb150015 				return (SDRC_ERROR);
374da6c28aaSamw 			}
375*a90cf9f2SGordon Ross 			sr->smb_pid = tmp_pid;	/* NB: 16-bit */
376da6c28aaSamw 
3776537f381Sas200622 			result = smb_lock_range(sr, (uint64_t)offset32,
3786537f381Sas200622 			    (uint64_t)length32, timeout, ltype);
379da6c28aaSamw 			if (result != NT_STATUS_SUCCESS) {
380dc20a302Sas200622 				smb_lock_range_error(sr, result);
381faa1795aSjb150015 				return (SDRC_ERROR);
382da6c28aaSamw 			}
383da6c28aaSamw 		}
384da6c28aaSamw 	}
385da6c28aaSamw 
386*a90cf9f2SGordon Ross 	sr->smb_pid = save_pid;
3877b59d02dSjb150015 	if (smbsr_encode_result(sr, 2, 0, "bb.ww", 2, sr->andx_com, 7, 0))
388faa1795aSjb150015 		return (SDRC_ERROR);
389faa1795aSjb150015 	return (SDRC_SUCCESS);
390da6c28aaSamw }
391*a90cf9f2SGordon Ross 
392*a90cf9f2SGordon Ross /*
393*a90cf9f2SGordon Ross  * Compose an SMB1 Oplock Break Notification packet, including
394*a90cf9f2SGordon Ross  * the SMB1 header and everything, in sr->reply.
395*a90cf9f2SGordon Ross  * The caller will send it and free the request.
396*a90cf9f2SGordon Ross  */
397*a90cf9f2SGordon Ross void
398*a90cf9f2SGordon Ross smb1_oplock_break_notification(smb_request_t *sr, uint8_t brk)
399*a90cf9f2SGordon Ross {
400*a90cf9f2SGordon Ross 	smb_ofile_t *ofile = sr->fid_ofile;
401*a90cf9f2SGordon Ross 	uint16_t fid;
402*a90cf9f2SGordon Ross 	uint8_t lock_type;
403*a90cf9f2SGordon Ross 	uint8_t oplock_level;
404*a90cf9f2SGordon Ross 
405*a90cf9f2SGordon Ross 	switch (brk) {
406*a90cf9f2SGordon Ross 	default:
407*a90cf9f2SGordon Ross 		ASSERT(0);
408*a90cf9f2SGordon Ross 		/* FALLTHROUGH */
409*a90cf9f2SGordon Ross 	case SMB_OPLOCK_BREAK_TO_NONE:
410*a90cf9f2SGordon Ross 		oplock_level = 0;
411*a90cf9f2SGordon Ross 		break;
412*a90cf9f2SGordon Ross 	case SMB_OPLOCK_BREAK_TO_LEVEL_II:
413*a90cf9f2SGordon Ross 		oplock_level = 1;
414*a90cf9f2SGordon Ross 		break;
415*a90cf9f2SGordon Ross 	}
416*a90cf9f2SGordon Ross 
417*a90cf9f2SGordon Ross 	sr->smb_com = SMB_COM_LOCKING_ANDX;
418*a90cf9f2SGordon Ross 	sr->smb_tid = ofile->f_tree->t_tid;
419*a90cf9f2SGordon Ross 	sr->smb_pid = 0xFFFF;
420*a90cf9f2SGordon Ross 	sr->smb_uid = 0;
421*a90cf9f2SGordon Ross 	sr->smb_mid = 0xFFFF;
422*a90cf9f2SGordon Ross 	fid = ofile->f_fid;
423*a90cf9f2SGordon Ross 	lock_type = LOCKING_ANDX_OPLOCK_RELEASE;
424*a90cf9f2SGordon Ross 
425*a90cf9f2SGordon Ross 	(void) smb_mbc_encodef(
426*a90cf9f2SGordon Ross 	    &sr->reply, "Mb19.wwwwbb3.wbb10.",
427*a90cf9f2SGordon Ross 	    /*  "\xffSMB"		   M */
428*a90cf9f2SGordon Ross 	    sr->smb_com,		/* b */
429*a90cf9f2SGordon Ross 	    /* status, flags, signature	 19. */
430*a90cf9f2SGordon Ross 	    sr->smb_tid,		/* w */
431*a90cf9f2SGordon Ross 	    sr->smb_pid,		/* w */
432*a90cf9f2SGordon Ross 	    sr->smb_uid,		/* w */
433*a90cf9f2SGordon Ross 	    sr->smb_mid,		/* w */
434*a90cf9f2SGordon Ross 	    8,		/* word count	   b */
435*a90cf9f2SGordon Ross 	    0xFF,	/* AndX cmd	   b */
436*a90cf9f2SGordon Ross 	    /*  AndX reserved, offset	  3. */
437*a90cf9f2SGordon Ross 	    fid,
438*a90cf9f2SGordon Ross 	    lock_type,
439*a90cf9f2SGordon Ross 	    oplock_level);
440*a90cf9f2SGordon Ross }
441