RIP BOOL. Convert BOOL -> bool. I found a few interesting
[nivanova/samba-autobuild/.git] / source3 / locking / posix.c
index 8bb7f605e3521c7a703e3d1ce1fdba3af60a0886..135e204f014e082ea460a748965631bb28201d67 100644 (file)
@@ -5,7 +5,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
    Revision History:
 
@@ -79,7 +78,7 @@ static const char *posix_lock_type_name(int lock_type)
  False if not.
 ****************************************************************************/
 
-static BOOL posix_lock_in_range(SMB_OFF_T *offset_out, SMB_OFF_T *count_out,
+static bool posix_lock_in_range(SMB_OFF_T *offset_out, SMB_OFF_T *count_out,
                                SMB_BIG_UINT u_offset, SMB_BIG_UINT u_count)
 {
        SMB_OFF_T offset = (SMB_OFF_T)u_offset;
@@ -183,9 +182,9 @@ static BOOL posix_lock_in_range(SMB_OFF_T *offset_out, SMB_OFF_T *count_out,
  broken NFS implementations.
 ****************************************************************************/
 
-static BOOL posix_fcntl_lock(files_struct *fsp, int op, SMB_OFF_T offset, SMB_OFF_T count, int type)
+static bool posix_fcntl_lock(files_struct *fsp, int op, SMB_OFF_T offset, SMB_OFF_T count, int type)
 {
-       BOOL ret;
+       bool ret;
 
        DEBUG(8,("posix_fcntl_lock %d %d %.0f %.0f %d\n",fsp->fh->fd,op,(double)offset,(double)count,type));
 
@@ -195,8 +194,8 @@ static BOOL posix_fcntl_lock(files_struct *fsp, int op, SMB_OFF_T offset, SMB_OF
 
                DEBUG(0,("posix_fcntl_lock: WARNING: lock request at offset %.0f, length %.0f returned\n",
                                        (double)offset,(double)count));
-               DEBUG(0,("an %s error. This can happen when using 64 bit lock offsets\n", strerror(errno)));
-               DEBUG(0,("on 32 bit NFS mounted file systems.\n"));
+               DEBUGADD(0,("an %s error. This can happen when using 64 bit lock offsets\n", strerror(errno)));
+               DEBUGADD(0,("on 32 bit NFS mounted file systems.\n"));
 
                /*
                 * If the offset is > 0x7FFFFFFF then this will cause problems on
@@ -226,10 +225,10 @@ static BOOL posix_fcntl_lock(files_struct *fsp, int op, SMB_OFF_T offset, SMB_OF
  broken NFS implementations.
 ****************************************************************************/
 
-static BOOL posix_fcntl_getlock(files_struct *fsp, SMB_OFF_T *poffset, SMB_OFF_T *pcount, int *ptype)
+static bool posix_fcntl_getlock(files_struct *fsp, SMB_OFF_T *poffset, SMB_OFF_T *pcount, int *ptype)
 {
        pid_t pid;
-       BOOL ret;
+       bool ret;
 
        DEBUG(8,("posix_fcntl_getlock %d %.0f %.0f %d\n",
                fsp->fh->fd,(double)*poffset,(double)*pcount,*ptype));
@@ -240,8 +239,8 @@ static BOOL posix_fcntl_getlock(files_struct *fsp, SMB_OFF_T *poffset, SMB_OFF_T
 
                DEBUG(0,("posix_fcntl_getlock: WARNING: lock request at offset %.0f, length %.0f returned\n",
                                        (double)*poffset,(double)*pcount));
-               DEBUG(0,("an %s error. This can happen when using 64 bit lock offsets\n", strerror(errno)));
-               DEBUG(0,("on 32 bit NFS mounted file systems.\n"));
+               DEBUGADD(0,("an %s error. This can happen when using 64 bit lock offsets\n", strerror(errno)));
+               DEBUGADD(0,("on 32 bit NFS mounted file systems.\n"));
 
                /*
                 * If the offset is > 0x7FFFFFFF then this will cause problems on
@@ -271,7 +270,7 @@ static BOOL posix_fcntl_getlock(files_struct *fsp, SMB_OFF_T *poffset, SMB_OFF_T
  region is locked, False otherwise.
 ****************************************************************************/
 
-BOOL is_posix_locked(files_struct *fsp,
+bool is_posix_locked(files_struct *fsp,
                        SMB_BIG_UINT *pu_offset,
                        SMB_BIG_UINT *pu_count,
                        enum brl_type *plock_type,
@@ -318,28 +317,19 @@ BOOL is_posix_locked(files_struct *fsp,
 /* The key used in the in-memory POSIX databases. */
 
 struct lock_ref_count_key {
-       SMB_DEV_T device;
-       SMB_INO_T inode;
+       struct file_id id;
        char r;
 }; 
 
-struct fd_key {
-       SMB_DEV_T device;
-       SMB_INO_T inode;
-}; 
-
 /*******************************************************************
  Form a static locking key for a dev/inode pair for the fd array.
 ******************************************************************/
 
-static TDB_DATA fd_array_key(SMB_DEV_T dev, SMB_INO_T inode)
+static TDB_DATA fd_array_key(struct file_id id)
 {
-       static struct fd_key key;
+       static struct file_id key;
        TDB_DATA kbuf;
-
-       memset(&key, '\0', sizeof(key));
-       key.device = dev;
-       key.inode = inode;
+       key = id;
        kbuf.dptr = (uint8 *)&key;
        kbuf.dsize = sizeof(key);
        return kbuf;
@@ -349,14 +339,13 @@ static TDB_DATA fd_array_key(SMB_DEV_T dev, SMB_INO_T inode)
  Form a static locking key for a dev/inode pair for the lock ref count
 ******************************************************************/
 
-static TDB_DATA locking_ref_count_key(SMB_DEV_T dev, SMB_INO_T inode)
+static TDB_DATA locking_ref_count_key(struct file_id id)
 {
        static struct lock_ref_count_key key;
        TDB_DATA kbuf;
 
        memset(&key, '\0', sizeof(key));
-       key.device = dev;
-       key.inode = inode;
+       key.id = id;
        key.r = 'r';
        kbuf.dptr = (uint8 *)&key;
        kbuf.dsize = sizeof(key);
@@ -369,7 +358,7 @@ static TDB_DATA locking_ref_count_key(SMB_DEV_T dev, SMB_INO_T inode)
 
 static TDB_DATA fd_array_key_fsp(files_struct *fsp)
 {
-       return fd_array_key(fsp->dev, fsp->inode);
+       return fd_array_key(fsp->file_id);
 }
 
 /*******************************************************************
@@ -378,14 +367,14 @@ static TDB_DATA fd_array_key_fsp(files_struct *fsp)
 
 static TDB_DATA locking_ref_count_key_fsp(files_struct *fsp)
 {
-       return locking_ref_count_key(fsp->dev, fsp->inode);
+       return locking_ref_count_key(fsp->file_id);
 }
 
 /*******************************************************************
  Create the in-memory POSIX lock databases.
 ********************************************************************/
 
-BOOL posix_locking_init(int read_only)
+bool posix_locking_init(int read_only)
 {
        if (posix_pending_close_tdb) {
                return True;
@@ -407,7 +396,7 @@ BOOL posix_locking_init(int read_only)
  Delete the in-memory POSIX lock databases.
 ********************************************************************/
 
-BOOL posix_locking_end(void)
+bool posix_locking_end(void)
 {
        if (posix_pending_close_tdb && tdb_close(posix_pending_close_tdb) != 0) {
                return False;
@@ -444,7 +433,7 @@ static void increment_windows_lock_ref_count(files_struct *fsp)
        if (dbuf.dptr == NULL) {
                dbuf.dptr = (uint8 *)SMB_MALLOC_P(int);
                if (!dbuf.dptr) {
-                       smb_panic("increment_windows_lock_ref_count: malloc fail.\n");
+                       smb_panic("increment_windows_lock_ref_count: malloc fail");
                }
                memset(dbuf.dptr, '\0', sizeof(int));
                dbuf.dsize = sizeof(int);
@@ -455,7 +444,7 @@ static void increment_windows_lock_ref_count(files_struct *fsp)
        memcpy(dbuf.dptr, &lock_ref_count, sizeof(int));
        
        if (tdb_store(posix_pending_close_tdb, kbuf, dbuf, TDB_REPLACE) == -1) {
-               smb_panic("increment_windows_lock_ref_count: tdb_store_fail.\n");
+               smb_panic("increment_windows_lock_ref_count: tdb_store_fail");
        }
        SAFE_FREE(dbuf.dptr);
 
@@ -471,7 +460,7 @@ static void decrement_windows_lock_ref_count(files_struct *fsp)
 
        dbuf = tdb_fetch(posix_pending_close_tdb, kbuf);
        if (!dbuf.dptr) {
-               smb_panic("decrement_windows_lock_ref_count: logic error.\n");
+               smb_panic("decrement_windows_lock_ref_count: logic error");
        }
 
        memcpy(&lock_ref_count, dbuf.dptr, sizeof(int));
@@ -479,11 +468,11 @@ static void decrement_windows_lock_ref_count(files_struct *fsp)
        memcpy(dbuf.dptr, &lock_ref_count, sizeof(int));
 
        if (lock_ref_count < 0) {
-               smb_panic("decrement_windows_lock_ref_count: lock_count logic error.\n");
+               smb_panic("decrement_windows_lock_ref_count: lock_count logic error");
        }
 
        if (tdb_store(posix_pending_close_tdb, kbuf, dbuf, TDB_REPLACE) == -1) {
-               smb_panic("decrement_windows_lock_ref_count: tdb_store_fail.\n");
+               smb_panic("decrement_windows_lock_ref_count: tdb_store_fail");
        }
        SAFE_FREE(dbuf.dptr);
 
@@ -510,12 +499,12 @@ void reduce_windows_lock_ref_count(files_struct *fsp, unsigned int dcount)
        lock_ref_count -= dcount;
 
        if (lock_ref_count < 0) {
-               smb_panic("reduce_windows_lock_ref_count: lock_count logic error.\n");
+               smb_panic("reduce_windows_lock_ref_count: lock_count logic error");
        }
        memcpy(dbuf.dptr, &lock_ref_count, sizeof(int));
        
        if (tdb_store(posix_pending_close_tdb, kbuf, dbuf, TDB_REPLACE) == -1) {
-               smb_panic("reduce_windows_lock_ref_count: tdb_store_fail.\n");
+               smb_panic("reduce_windows_lock_ref_count: tdb_store_fail");
        }
        SAFE_FREE(dbuf.dptr);
 
@@ -575,14 +564,14 @@ static void add_fd_to_close_entry(files_struct *fsp)
 
        dbuf.dptr = (uint8 *)SMB_REALLOC(dbuf.dptr, dbuf.dsize + sizeof(int));
        if (!dbuf.dptr) {
-               smb_panic("add_fd_to_close_entry: Realloc fail !\n");
+               smb_panic("add_fd_to_close_entry: SMB_REALLOC failed");
        }
 
        memcpy(dbuf.dptr + dbuf.dsize, &fsp->fh->fd, sizeof(int));
        dbuf.dsize += sizeof(int);
 
        if (tdb_store(posix_pending_close_tdb, kbuf, dbuf, TDB_REPLACE) == -1) {
-               smb_panic("add_fd_to_close_entry: tdb_store_fail.\n");
+               smb_panic("add_fd_to_close_entry: tdb_store_fail");
        }
 
        DEBUG(10,("add_fd_to_close_entry: added fd %d file %s\n",
@@ -600,7 +589,7 @@ static void delete_close_entries(files_struct *fsp)
        TDB_DATA kbuf = fd_array_key_fsp(fsp);
 
        if (tdb_delete(posix_pending_close_tdb, kbuf) == -1) {
-               smb_panic("delete_close_entries: tdb_delete fail !\n");
+               smb_panic("delete_close_entries: tdb_delete failed");
        }
 }
 
@@ -651,7 +640,10 @@ NTSTATUS fd_close_posix(struct connection_struct *conn, files_struct *fsp)
                 */
                ret = SMB_VFS_CLOSE(fsp,fsp->fh->fd);
                fsp->fh->fd = -1;
-               return map_nt_error_from_unix(errno);
+               if (ret == -1) {
+                       return map_nt_error_from_unix(errno);
+               }
+               return NT_STATUS_OK;
        }
 
        if (get_windows_lock_ref_count(fsp)) {
@@ -944,7 +936,7 @@ new: start=%.0f,size=%.0f\n", (double)l_curr->start, (double)l_curr->size,
                                pstring msg;
 
                                slprintf(msg, sizeof(msg)-1, "logic flaw in cases: l_curr: start = %.0f, size = %.0f : \
-lock: start = %.0f, size = %.0f\n", (double)l_curr->start, (double)l_curr->size, (double)lock->start, (double)lock->size );
+lock: start = %.0f, size = %.0f", (double)l_curr->start, (double)l_curr->size, (double)lock->start, (double)lock->size );
 
                                smb_panic(msg);
                        }
@@ -959,7 +951,7 @@ lock: start = %.0f, size = %.0f\n", (double)l_curr->start, (double)l_curr->size,
  lock could be granted, False if not.
 ****************************************************************************/
 
-BOOL set_posix_lock_windows_flavour(files_struct *fsp,
+bool set_posix_lock_windows_flavour(files_struct *fsp,
                        SMB_BIG_UINT u_offset,
                        SMB_BIG_UINT u_count,
                        enum brl_type lock_type,
@@ -971,7 +963,7 @@ BOOL set_posix_lock_windows_flavour(files_struct *fsp,
        SMB_OFF_T offset;
        SMB_OFF_T count;
        int posix_lock_type = map_posix_lock_type(fsp,lock_type);
-       BOOL ret = True;
+       bool ret = True;
        size_t lock_count;
        TALLOC_CTX *l_ctx = NULL;
        struct lock_list *llist = NULL;
@@ -1096,7 +1088,7 @@ BOOL set_posix_lock_windows_flavour(files_struct *fsp,
  lock could be released, False if not.
 ****************************************************************************/
 
-BOOL release_posix_lock_windows_flavour(files_struct *fsp,
+bool release_posix_lock_windows_flavour(files_struct *fsp,
                                SMB_BIG_UINT u_offset,
                                SMB_BIG_UINT u_count,
                                enum brl_type deleted_lock_type,
@@ -1106,7 +1098,7 @@ BOOL release_posix_lock_windows_flavour(files_struct *fsp,
 {
        SMB_OFF_T offset;
        SMB_OFF_T count;
-       BOOL ret = True;
+       bool ret = True;
        TALLOC_CTX *ul_ctx = NULL;
        struct lock_list *ulist = NULL;
        struct lock_list *ul = NULL;
@@ -1219,7 +1211,7 @@ BOOL release_posix_lock_windows_flavour(files_struct *fsp,
  upper layer would have refused it.
 ****************************************************************************/
 
-BOOL set_posix_lock_posix_flavour(files_struct *fsp,
+bool set_posix_lock_posix_flavour(files_struct *fsp,
                        SMB_BIG_UINT u_offset,
                        SMB_BIG_UINT u_count,
                        enum brl_type lock_type,
@@ -1259,14 +1251,14 @@ BOOL set_posix_lock_posix_flavour(files_struct *fsp,
  have a different lock context.
 ****************************************************************************/
 
-BOOL release_posix_lock_posix_flavour(files_struct *fsp,
+bool release_posix_lock_posix_flavour(files_struct *fsp,
                                SMB_BIG_UINT u_offset,
                                SMB_BIG_UINT u_count,
                                const struct lock_context *lock_ctx,
                                const struct lock_struct *plocks,
                                int num_locks)
 {
-       BOOL ret = True;
+       bool ret = True;
        SMB_OFF_T offset;
        SMB_OFF_T count;
        TALLOC_CTX *ul_ctx = NULL;