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