da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * CDDL HEADER START
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * The contents of this file are subject to the terms of the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Common Development and Distribution License (the "License").
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * You may not use this file except in compliance with the License.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * See the License for the specific language governing permissions
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * and limitations under the License.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * When distributing Covered Code, include this CDDL HEADER in each
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If applicable, add the following below this CDDL HEADER, with the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * fields enclosed by brackets "[]" replaced with your own identifying
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * information: Portions Copyright [yyyy] [name of copyright owner]
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * CDDL HEADER END
148c5f43199ca0b43fc8e3b643aab11cd66ea327Alan Wright * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
9c787c76803e57736d13413909945366ce3a9448Gordon Ross * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * SMB: locking_andx
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * SMB_COM_LOCKING_ANDX allows both locking and/or unlocking of file range(s).
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Client Request Description
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * ================================== =================================
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * UCHAR WordCount; Count of parameter words = 8
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * UCHAR AndXCommand; Secondary (X) command; 0xFF = none
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * UCHAR AndXReserved; Reserved (must be 0)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * USHORT AndXOffset; Offset to next command WordCount
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * USHORT Fid; File handle
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * UCHAR LockType; See LockType table below
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * UCHAR OplockLevel; The new oplock level
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * ULONG Timeout; Milliseconds to wait for unlock
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * USHORT NumberOfUnlocks; Num. unlock range structs following
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * USHORT NumberOfLocks; Num. lock range structs following
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * USHORT ByteCount; Count of data bytes
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * LOCKING_ANDX_RANGE Unlocks[]; Unlock ranges
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * LOCKING_ANDX_RANGE Locks[]; Lock ranges
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * LockType Flag Name Value Description
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * ============================ ===== ================================
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * LOCKING_ANDX_SHARED_LOCK 0x01 Read-only lock
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * LOCKING_ANDX_OPLOCK_RELEASE 0x02 Oplock break notification
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * LOCKING_ANDX_CHANGE_LOCKTYPE 0x04 Change lock type
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * LOCKING_ANDX_CANCEL_LOCK 0x08 Cancel outstanding request
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * LOCKING_ANDX_LARGE_FILES 0x10 Large file locking format
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * LOCKING_ANDX_RANGE Format
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * =====================================================================
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * USHORT Pid; PID of process "owning" lock
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * ULONG Offset; Offset to bytes to [un]lock
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * ULONG Length; Number of bytes to [un]lock
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Large File LOCKING_ANDX_RANGE Format
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * =====================================================================
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * USHORT Pid; PID of process "owning" lock
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * USHORT Pad; Pad to DWORD align (mbz)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * ULONG OffsetHigh; Offset to bytes to [un]lock
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * ULONG OffsetLow; Offset to bytes to [un]lock (low)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * ULONG LengthHigh; Number of bytes to [un]lock
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * ULONG LengthLow; Number of bytes to [un]lock (low)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Server Response Description
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * ================================== =================================
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * UCHAR WordCount; Count of parameter words = 2
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * UCHAR AndXCommand; Secondary (X) command; 0xFF =
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * UCHAR AndXReserved; Reserved (must be 0)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * USHORT AndXOffset; Offset to next command WordCount
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * USHORT ByteCount; Count of data bytes = 0
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Locking is a simple mechanism for excluding other processes read/write
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * access to regions of a file. The locked regions can be anywhere in the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * logical file. Locking beyond end-of-file is permitted. Any process
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * using the Fid specified in this request's Fid has access to the locked
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * bytes, other processes will be denied the locking of the same bytes.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * The proper method for using locks is not to rely on being denied read or
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * write access on any of the read/write protocols but rather to attempt
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * the locking protocol and proceed with the read/write only if the locks
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * succeeded.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Locking a range of bytes will fail if any subranges or overlapping
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * ranges are locked. In other words, if any of the specified bytes are
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * already locked, the lock will fail.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If NumberOfUnlocks is non-zero, the Unlocks vector contains
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * NumberOfUnlocks elements. Each element requests that a lock at Offset
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * of Length be released. If NumberOfLocks is nonzero, the Locks vector
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * contains NumberOfLocks elements. Each element requests the acquisition
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * of a lock at Offset of Length.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Timeout is the maximum amount of time to wait for the byte range(s)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * specified to become unlocked. A timeout value of 0 indicates that the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * server should fail immediately if any lock range specified is locked. A
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * timeout value of -1 indicates that the server should wait as long as it
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * takes for each byte range specified to become unlocked so that it may be
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * again locked by this protocol. Any other value of smb_timeout specifies
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * the maximum number of milliseconds to wait for all lock range(s)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * specified to become available.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If any of the lock ranges timeout because of the area to be locked is
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * already locked (or the lock fails), the other ranges in the protocol
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * request which were successfully locked as a result of this protocol will
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * be unlocked (either all requested ranges will be locked when this
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * protocol returns to the client or none).
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If LockType has the LOCKING_ANDX_SHARED_LOCK flag set, the lock is
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * specified as a shared lock. Locks for both read and write (where
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * LOCKING_ANDX_SHARED_LOCK is clear) should be prohibited, but other
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * shared locks should be permitted. If shared locks can not be supported
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * by a server, the server should map the lock to a lock for both read and
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * write. Closing a file with locks still in force causes the locks to be
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * released in no defined order.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If LockType has the LOCKING_ANDX_LARGE_FILES flag set and if the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * negotiated protocol is NT LM 0.12 or later, then the Locks and Unlocks
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * vectors are in the Large File LOCKING_ANDX_RANGE format. This allows
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * specification of 64 bit offsets for very large files.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If the one and only member of the Locks vector has the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * LOCKING_ANDX_CANCEL_LOCK flag set in the LockType field, the client is
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * requesting the server to cancel a previously requested, but not yet
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * responded to, lock.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If LockType has the LOCKING_ANDX_CHANGE_LOCKTYPE flag set, the client is
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * requesting that the server atomically change the lock type from a shared
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * lock to an exclusive lock or vice versa. If the server can not do this
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * in an atomic fashion, the server must reject this request. NT and W95
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * servers do not support this capability.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Oplocks are described in the "Opportunistic Locks" section elsewhere in
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * this document. A client requests an oplock by setting the appropriate
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * bit in the SMB_COM_OPEN_ANDX request when the file is being opened in a
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * mode which is not exclusive. The server responds by setting the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * appropriate bit in the response SMB indicating whether or not the oplock
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * was granted. By granting the oplock, the server tells the client the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * file is currently only being used by this one client process at the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * current time. The client can therefore safely do read ahead and write
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * behind as well as local caching of file locks knowing that the file will
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * not be accessed/changed in any way by another process while the oplock
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * is in effect. The client will be notified when any other process
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * attempts to open or modify the oplocked file.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * When another user attempts to open or otherwise modify the file which a
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * client has oplocked, the server delays the second attempt and notifies
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * the client via an SMB_LOCKING_ANDX SMB asynchronously sent from the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * server to the client. This message has the LOCKING_ANDX_OPLOCK_RELEASE
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * flag set indicating to the client that the oplock is being broken.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * OplockLevel indicates the type of oplock the client now owns. If
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * OplockLevel is 0, the client possesses no oplocks on the file at all, if
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * OplockLevel is 1 the client possesses a Level II oplock. The client is
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * expected to flush any dirty buffers to the server, submit any file locks
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * and respond to the server with either an SMB_LOCKING_ANDX SMB having the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * LOCKING_ANDX_OPLOCK_RELEASE flag set, or with a file close if the file
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * is no longer in use by the client. If the client sends an
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * SMB_LOCKING_ANDX SMB with the LOCKING_ANDX_OPLOCK_RELEASE flag set and
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * NumberOfLocks is zero, the server does not send a response. Since a
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * close being sent to the server and break oplock notification from the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * server could cross on the wire, if the client gets an oplock
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * notification on a file which it does not have open, that notification
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * should be ignored.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Due to timing, the client could get an "oplock broken" notification in a
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * user's data buffer as a result of this notification crossing on the wire
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * with a SMB_COM_READ_RAW request. The client must detect this (use
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * length of msg, "FFSMB", MID of -1 and Command of SMB_COM_LOCKING_ANDX)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * and honor the "oplock broken" notification as usual. The server must
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * also note on receipt of an SMB_COM_READ_RAW request that there is an
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * outstanding (unanswered) "oplock broken" notification to the client and
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * return a zero length response denoting failure of the read raw request.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * The client should (after responding to the "oplock broken"
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * notification), use a standard read protocol to redo the read request.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * This allows a file to actually contain data matching an "oplock broken"
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * notification and still be read correctly.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * The entire message sent and received including the optional second
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * protocol must fit in the negotiated maximum transfer size. The
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * following are the only valid SMB commands for AndXCommand for
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * SMB_COM_LOCKING_ANDX:
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * SMB_COM_READ SMB_COM_READ_ANDX
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * SMB_COM_WRITE SMB_COM_WRITE_ANDX
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * SMB_COM_FLUSH
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * 4.2.6.1 Errors
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw unsigned short i;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw unsigned char lock_type; /* See lock_type table above */
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross uint16_t tmp_pid; /* locking uses 16-bit pids */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw rc = smbsr_decode_vwv(sr, "4.wbblww", &sr->smb_fid, &lock_type,
7b59d02d2a384be9a08087b14defadd214b3c1ddjb if (rc != 0)
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as smbsr_error(sr, NT_STATUS_INVALID_HANDLE, ERRDOS, ERRbadfid);
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross save_pid = sr->smb_pid; /* Save the original pid */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * No support for changing locktype (although we could probably
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * implement this)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * No support for cancel lock (smbtorture expects this)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * negotiated protocol should be NT LM 0.12 or later
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw for (i = 0; i < unlock_num; i++) {
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as * This is the error returned by Windows 2000
dc20a3024900c47dd2ee44b9707e6df38f7d62a5as * even when STATUS32 has been negotiated.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw for (i = 0; i < lock_num; i++) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw for (i = 0; i < unlock_num; i++) {
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross rc = smb_mbc_decodef(&sr->smb_data, "wll", &tmp_pid,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw for (i = 0; i < lock_num; i++) {
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross rc = smb_mbc_decodef(&sr->smb_data, "wll", &tmp_pid,
7b59d02d2a384be9a08087b14defadd214b3c1ddjb if (smbsr_encode_result(sr, 2, 0, "bb.ww", 2, sr->andx_com, 7, 0))
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross * Compose an SMB1 Oplock Break Notification packet, including
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross * the SMB1 header and everything, in sr->reply.
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross * The caller will send it and free the request.
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Rosssmb1_oplock_break_notification(smb_request_t *sr, uint8_t brk)
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross /* FALLTHROUGH */
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross /* "\xffSMB" M */
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross /* status, flags, signature 19. */
a90cf9f29973990687fa61de9f1f6ea22e924e40Gordon Ross /* AndX reserved, offset 3. */