--- samba/source/smbd/server.c.orig	2004/02/11 18:51:56	1.9
+++ samba/source/smbd/server.c	2004/06/12 01:59:50
@@ -411,6 +411,11 @@
 					return False;
 				}
 
+#ifdef WITH_BRLM
+				if (lp_BRLM())
+					BRLMInit();
+#endif
+
 				return True; 
 			}
 			/* The parent doesn't need this socket */
@@ -597,6 +602,11 @@
 
 	locking_end();
 	printing_end();
+
+#ifdef WITH_BRLM
+    if(lp_BRLM())
+       BRLMClose();
+#endif
 
 	DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
 	exit(0);
--- samba/source/include/includes.h.orig        Wed Apr 28 16:49:45 2004
+++ samba/source/include/includes.h     Wed Apr 28 16:41:12 2004
@@ -27,6 +27,10 @@
 
 #include "local.h"
 
+#ifdef WITH_BRLM
+#include <ByteRangeLocking/BRLM.h>
+#endif
+
 #ifdef AIX
 #define DEFAULT_PRINTING PRINT_AIX
 #define PRINTCAP_NAME "/etc/qconfig"
--- samba/source/include/smb.h.orig	Thu Jun  3 17:52:16 2004
+++ samba/source/include/smb.h	Thu Jun  3 17:54:43 2004
@@ -379,6 +379,10 @@
 
 #include "fake_file.h"
 
+#ifdef WITH_BRLM
+#include <ByteRangeLocking/BRLM.h>
+#endif
+
 typedef struct files_struct
 {
 	struct files_struct *next, *prev;
@@ -415,6 +419,11 @@
 	BOOL directory_delete_on_close;
 	char *fsp_name;
  	FAKE_FILE_HANDLE *fake_file_handle;
+#ifdef WITH_BRLM
+       BRLMRef brlm_ref;
+       int     brlm_deny_mode;
+       int     brlm_open_mode;
+#endif
 } files_struct;
 
 #include "ntquotas.h"
--- samba/source/locking/posix.c.orig	Thu Jun  3 18:01:38 2004
+++ samba/source/locking/posix.c	Thu Jun  3 19:35:42 2004
@@ -30,6 +30,13 @@
 
 static TDB_CONTEXT *posix_lock_tdb;
 
+#ifdef WITH_BRLM
+struct pending_close {
+       int fd;
+       BRLMRef brlm_ref;
+};
+#endif
+
 /*
  * The pending close database handle.
  */
@@ -97,12 +104,29 @@
 	TDB_DATA kbuf = locking_key_fsp(fsp);
 	TDB_DATA dbuf;
 	char *tp;
+	void *data = NULL;
+#ifdef WITH_BRLM
+	struct pending_close pc;
+	unsigned long datalen = sizeof(struct pending_close);
+#else
+	unsigned long datalen =  sizeof(int);
+#endif
 
 	dbuf.dptr = NULL;
 
 	dbuf = tdb_fetch(posix_pending_close_tdb, kbuf);
+#ifdef WITH_BRLM
+	/*
+	* Add new record.
+	*/
 
-	tp = Realloc(dbuf.dptr, dbuf.dsize + sizeof(int));
+	pc.fd = fsp->fd;
+	pc.brlm_ref = fsp->brlm_ref;
+	data = &pc;
+#else
+	data = &fsp->fd;
+#endif
+	tp = Realloc(dbuf.dptr, dbuf.dsize + datalen);
 	if (!tp) {
 		DEBUG(0,("add_fd_to_close_entry: Realloc fail !\n"));
 		SAFE_FREE(dbuf.dptr);
@@ -110,8 +134,8 @@
 	} else
 		dbuf.dptr = tp;
 
-	memcpy(dbuf.dptr + dbuf.dsize, &fsp->fd, sizeof(int));
-	dbuf.dsize += sizeof(int);
+	memcpy(dbuf.dptr + dbuf.dsize, data, datalen);
+	dbuf.dsize += datalen;
 
 	if (tdb_store(posix_pending_close_tdb, kbuf, dbuf, TDB_REPLACE) == -1) {
 		DEBUG(0,("add_fd_to_close_entry: tdb_store fail !\n"));
@@ -138,7 +162,11 @@
  free. Returns number of entries.
 ****************************************************************************/
 
+#ifdef WITH_BRLM
+static size_t get_posix_pending_close_entries(files_struct *fsp, struct pending_close **entries)
+#else
 static size_t get_posix_pending_close_entries(files_struct *fsp, int **entries)
+#endif
 {
 	TDB_DATA kbuf = locking_key_fsp(fsp);
 	TDB_DATA dbuf;
@@ -153,9 +181,13 @@
 		return 0;
 	}
 
+#ifdef WITH_BRLM
+	*entries = (struct pending_close *)dbuf.dptr;
+	count = (size_t)(dbuf.dsize / sizeof(struct pending_close));
+#else
 	*entries = (int *)dbuf.dptr;
 	count = (size_t)(dbuf.dsize / sizeof(int));
-
+#endif
 	return count;
 }
 
@@ -198,7 +230,12 @@
 	int ret;
 	size_t count, i;
 	struct posix_lock *entries = NULL;
+#ifdef WITH_BRLM
+	BRLMStatus brlm_status = BRLMMiscErr;
+	struct pending_close *fd_array = NULL;
+#else
 	int *fd_array = NULL;
+#endif
 	BOOL locks_on_other_fds = False;
 
 	if (!lp_posix_locking(SNUM(conn))) {
@@ -259,9 +296,25 @@
 		DEBUG(10,("fd_close_posix: doing close on %u fd's.\n", (unsigned int)count ));
 
 		for(i = 0; i < count; i++) {
+#ifdef WITH_BRLM
+			if (lp_BRLM())
+			{
+				if (fd_array[i].brlm_ref) {
+					brlm_status = BRLMCloseRef(fd_array[i].brlm_ref);
+					if (BRLMNoErr != brlm_status)
+						saved_errno = brlm_status;
+				}
+				DEBUG(4,("fd_close_posix: [%d]BRLMCloseRef fd(%d) ref(%X)\n", brlm_status, fd_array[i].fd, fd_array[i].brlm_ref));
+			} else {
+				if (SMB_VFS_CLOSE(fsp,fd_array[i].fd) == -1) {
+				saved_errno = errno;
+				}
+			}
+#else
 			if (SMB_VFS_CLOSE(fsp,fd_array[i]) == -1) {
 				saved_errno = errno;
 			}
+#endif
 		}
 
 		/*
@@ -646,9 +699,61 @@
 static BOOL posix_fcntl_lock(files_struct *fsp, int op, SMB_OFF_T offset, SMB_OFF_T count, int type)
 {
 	int ret;
+#ifdef WITH_BRLM
+	BRLMStatus brlm_status = BRLMParamErr;
+	BRLMLockType brlm_lock_type = 0;
+#endif
 
 	DEBUG(8,("posix_fcntl_lock %d %d %.0f %.0f %d\n",fsp->fd,op,(double)offset,(double)count,type));
 
+#ifdef WITH_BRLM
+if (lp_BRLM())
+{
+	switch (type) {
+		case F_RDLCK: 
+			brlm_lock_type = kBRLMRLock; 
+		break;
+		case F_WRLCK: 
+			brlm_lock_type = kBRLMWLock; 
+		break;
+		case F_UNLCK: 
+			brlm_lock_type = kBRLMFree; 
+		break;
+		default: 
+			brlm_lock_type = 0; 
+		break;
+	}
+	
+	if (op == SMB_F_GETLK)
+	{
+		if (type == F_RDLCK) 
+		{
+			brlm_status  = BRLMCanRead(fsp->brlm_ref, offset, count);
+			DEBUG(8,("posix_fcntl_lock:  [%d]BRLMCanRead lock_type[0x%X]\n",brlm_status, brlm_lock_type));
+		} else if (type == F_WRLCK) {
+			brlm_status =  BRLMCanWrite(fsp->brlm_ref, offset, count);
+			DEBUG(8,("posix_fcntl_lock:  [%d]BRLMCanWrite lock_type[0x%X]\n",brlm_status, brlm_lock_type));
+		} else {
+			DEBUG(8,("posix_fcntl_lock:  !!!!! SMB_F_GETLK !!!!! lock_type[0x%X]\n", type));
+			ret = 0;
+		}
+		if (BRLMNoErr == brlm_status) 
+			ret = 0; /* can read | write */
+		else
+			ret = 1;
+	} else if (op == SMB_F_SETLK) {
+		brlm_status = BRLMByteRangeLock(fsp->brlm_ref, brlm_lock_type, offset, count);
+		DEBUG(8,("posix_fcntl_lock:  [%d]BRLMByteRangeLock lock_type[0x%X]\n",brlm_status, brlm_lock_type));
+		if (BRLMNoErr == brlm_status) 
+		ret = 1;
+		else
+		ret = 0;
+	} else {
+		DEBUG(8,("posix_fcntl_lock:  !!!!! OP !!!!! op[0x%X] lock_type[0x%X]\n", op, type));
+		ret = 0;
+	}
+} else {
+#endif
 	ret = SMB_VFS_LOCK(fsp,fsp->fd,op,offset,count,type);
 
 	if (!ret && ((errno == EFBIG) || (errno == ENOLCK) || (errno ==  EINVAL))) {
@@ -676,7 +781,9 @@
 			ret = SMB_VFS_LOCK(fsp,fsp->fd,op,offset,count,type);
 		}
 	}
-
+#ifdef WITH_BRLM
+} /* lp_BRLM */
+#endif
 	DEBUG(8,("posix_fcntl_lock: Lock call %s\n", ret ? "successful" : "failed"));
 
 	return ret;
--- samba/source/smbd/vfs-wrap.c.orig	Fri Jun  4 14:11:02 2004
+++ samba/source/smbd/vfs-wrap.c	Fri Jun  4 14:14:58 2004
@@ -172,10 +172,31 @@
 int vfswrap_close(vfs_handle_struct *handle, files_struct *fsp, int fd)
 {
 	int result;
+#ifdef WITH_BRLM
+	BRLMStatus brlm_status = BRLMMiscErr;
+#endif
 
 	START_PROFILE(syscall_close);
 
+#ifdef WITH_BRLM
+if (lp_BRLM())
+{
+	if (fsp->brlm_ref) {
+		brlm_status = BRLMCloseRef(fsp->brlm_ref);
+	fsp->brlm_ref = 0;
+	if (BRLMNoErr == brlm_status)
+		result = 0;
+	else
+		result = -1;
+}
+	DEBUG(6,("vfswrap_close: [%d]BRLMCloseRef file(%s) ref(%X)\n", brlm_status, fsp->fsp_name, fsp->brlm_ref));
+} else {
+#endif
 	result = close(fd);
+#ifdef WITH_BRLM
+       }       /* lp_BRLM */   
+#endif         
+	
 	END_PROFILE(syscall_close);
 	return result;
 }
--- samba/source/param/loadparm.c.orig	Fri Jun  4 14:17:36 2004
+++ samba/source/param/loadparm.c	Fri Jun  4 14:23:05 2004
@@ -288,6 +288,9 @@
 #ifdef WITH_OPENDIRECTORY
 	BOOL bOpenDirectory;
 #endif
+#ifdef WITH_BRLM
+	BOOL bBRLM;
+#endif
 	int restrict_anonymous;
 	int name_cache_timeout;
 	int client_signing;
@@ -1169,6 +1172,9 @@
 #ifdef WITH_OPENDIRECTORY
 	{"opendirectory", P_BOOL, P_GLOBAL, &Globals.bOpenDirectory, NULL, NULL, FLAG_ADVANCED}, 
 #endif
+#ifdef WITH_BRLM
+	{"brlm", P_BOOL, P_GLOBAL, &Globals.bBRLM, NULL, NULL, FLAG_ADVANCED}, 
+#endif
 
 	{NULL,  P_BOOL,  P_NONE,  NULL,  NULL,  NULL,  0}
 };
@@ -1529,6 +1535,9 @@
 #ifdef WITH_OPENDIRECTORY
 	Globals.bOpenDirectory = True;
 #endif
+#ifdef WITH_BRLM
+	Globals.bBRLM = False;
+#endif
 }
 
 static TALLOC_CTX *lp_talloc;
@@ -1905,6 +1914,9 @@
 FN_GLOBAL_INTEGER(lp_server_signing, &Globals.server_signing)
 #ifdef WITH_OPENDIRECTORY
 FN_GLOBAL_BOOL(lp_opendirectory, &Globals.bOpenDirectory)
+#endif
+#ifdef WITH_BRLM
+FN_GLOBAL_BOOL(lp_BRLM, &Globals.bBRLM)
 #endif
 
 /* local prototypes */
--- samba/source/smbd/open.c.orig	Fri Jun  4 14:55:59 2004
+++ samba/source/smbd/open.c	Fri Jun  4 14:55:05 2004
@@ -78,6 +78,58 @@
 }
 
 /****************************************************************************
+Open a file.
+****************************************************************************/
+#ifdef WITH_BRLM
+int map_deny_mode_to_brlm_permissions(files_struct *fsp, int flags)
+{
+	int deny_mode = 0;
+	
+	switch (flags) {
+		   case DENY_ALL: 
+				   deny_mode = kBRLMDenyRead | kBRLMDenyWrite; 
+				   break;
+		   case DENY_WRITE: 
+				   deny_mode = kBRLMDenyWrite; 
+				   break;
+		   case DENY_READ: 
+				   deny_mode = kBRLMDenyRead; 
+				   break;
+		   case DENY_DOS: 
+		   case DENY_FCB: 
+		   default: 
+				   deny_mode = 0; 
+				   break;
+	}
+	fsp->brlm_deny_mode = deny_mode;
+	DEBUG(4,("map_deny_mode_to_brlm_permissions[0x%X] flags[0x%X]\n",deny_mode, flags));
+	return deny_mode;
+}
+
+int map_open_mode_to_brlm_permissions(files_struct *fsp, int flags)
+{
+	int open_mode = 0;
+	
+	if (flags & DOS_OPEN_RDONLY) {
+		   open_mode = kBRLMRead;  
+	} else if ( flags & DOS_OPEN_WRONLY) {
+		   open_mode = kBRLMWrite;         
+	} else if ( flags & DOS_OPEN_RDWR) {
+		   open_mode = kBRLMRead | kBRLMWrite;     
+	} else if ( flags & DOS_OPEN_FCB) {
+		   open_mode = kBRLMRead; 
+	} else {
+		   open_mode = kBRLMRead;          
+		   DEBUG(4,("map_open_mode_to_brlm_permissions: !!! default open permissions - kBRLMRead  flags[0x%X]\n", flags));
+	}
+	
+	fsp->brlm_open_mode = open_mode;
+	DEBUG(4,("map_open_mode_to_brlm_permissions[0x%X] flags[0x%X]\n",open_mode, flags));
+	
+	return open_mode;
+}
+#endif
+/****************************************************************************
  Open a file.
 ****************************************************************************/
 
@@ -87,6 +139,10 @@
 	extern struct current_user current_user;
 	int accmode = (flags & O_ACCMODE);
 	int local_flags = flags;
+#ifdef WITH_BRLM
+	BRLMStatus brlm_status = BRLMNoErr;
+	BRLMRef brlm_ref = 0;
+#endif
 
 	fsp->fd = -1;
 	fsp->oplock_type = NO_OPLOCK;
@@ -167,7 +223,23 @@
 		}
 
 		/* Actually do the open */
+#ifdef WITH_BRLM
+	if (lp_BRLM())
+	{
+		map_open_mode_to_brlm_permissions(fsp, local_flags); 
+		brlm_status = BRLMPosixOpen(fname, kBRLMDataFork, fsp->brlm_deny_mode | fsp->brlm_open_mode, local_flags /* creatPermissions */, mode, &brlm_ref, 0 /* sessionID */);
+		fsp->brlm_ref = brlm_ref;
+		if (BRLMNoErr == brlm_status)
+			fsp->fd = BRLMGetDescriptor(brlm_ref);
+		else 
+			fsp->fd = -1;
+		DEBUG(3,("open_file: [%d]BRLMPosixOpen file (%s) (brlm_perms=0x%X) (local_flags=0x%X) (mode=%d) (fd=%d) (brlm_ref=%X)\n", brlm_status, fname, fsp->brlm_deny_mode | fsp->brlm_open_mode, local_flags, mode, fsp->fd, brlm_ref));
+	} else {
+#endif
 		fsp->fd = fd_open(conn, fname, local_flags, mode);
+#ifdef WITH_BRLM
+	} /* lp_BRLM */   
+#endif         
 		if (fsp->fd == -1)  {
 			DEBUG(3,("Error opening file %s (%s) (local_flags=%d) (flags=%d)\n",
 				 fname,strerror(errno),local_flags,flags));
@@ -941,6 +1013,10 @@
 		errno = EINVAL;
 		return NULL;
 	}
+
+#ifdef WITH_BRLM
+	map_deny_mode_to_brlm_permissions(fsp, deny_mode); 
+#endif
 
 	if (file_existed) {
 
