--- samba/source/smbd/server.c.orig	2004-12-15 18:29:03.000000000 -0800
+++ samba/source/smbd/server.c	2004-12-15 18:32:02.000000000 -0800
@@ -46,6 +46,10 @@
  */
 static int server_fd = -1;
 
+#ifdef WITH_BRLM
+BOOL brlm_init = False;
+#endif
+
 int smbd_server_fd(void)
 {
 	return server_fd;
@@ -132,6 +136,13 @@
 	set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
 	set_socket_options(smbd_server_fd(), user_socket_options);
 
+#ifdef WITH_BRLM
+	if (lp_BRLM() && !brlm_init) {
+		BRLMInit();
+		brlm_init = True;
+	}
+#endif
+
 	return True;
 }
 
@@ -387,9 +398,15 @@
 			/* Ensure child is set to blocking mode */
 			set_blocking(smbd_server_fd(),True);
 
-			if (smbd_server_fd() != -1 && interactive)
+			if (smbd_server_fd() != -1 && interactive) {
+#ifdef WITH_BRLM
+				if (lp_BRLM() && !brlm_init) {
+					BRLMInit();
+					brlm_init = True;
+				}
+#endif
 				return True;
-			
+			}
 			if (allowable_number_of_smbd_processes() && smbd_server_fd() != -1 && sys_fork()==0) {
 				/* Child code ... */
 				
@@ -421,6 +438,13 @@
 					smb_panic("tdb_reopen_all failed.");
 				}
 
+#ifdef WITH_BRLM
+				if (lp_BRLM() && !brlm_init) {
+					BRLMInit();
+					brlm_init = True;
+				}
+#endif
+
 				return True; 
 			}
 			/* The parent doesn't need this socket */
@@ -611,6 +635,11 @@
 	locking_end();
 	printing_end();
 
+#ifdef WITH_BRLM
+    if(lp_BRLM() && brlm_init)
+       BRLMClose();
+#endif
+
 	DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
 	exit(0);
 }
--- samba/source/include/smb.h.orig	2004-12-04 15:24:16.000000000 -0800
+++ samba/source/include/smb.h	2004-12-04 15:41:45.000000000 -0800
@@ -380,6 +380,10 @@
 
 #include "fake_file.h"
 
+#ifdef WITH_BRLM
+#include <ByteRangeLocking/BRLM.h>
+#endif
+
 typedef struct files_struct {
 	struct files_struct *next, *prev;
 	int fnum;
@@ -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/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/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/locking/posix.c.orig	2005/01/06 23:22:20	1.1.1.7
+++ samba/source/locking/posix.c	2005/07/21 21:40:48
@@ -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 = SMB_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 = SMB_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,58 @@
 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())
+{
+	if (op == SMB_F_GETLK)
+	{
+		if (type == F_RDLCK) 
+		{
+			brlm_status  = BRLMCanRead(fsp->brlm_ref, offset, count);
+			DEBUG(8,("posix_fcntl_lock:  [%d]BRLMCanRead offset[%d] count[%d]\n",brlm_status, offset, count));
+		} else if (type == F_WRLCK) {
+			brlm_status =  BRLMCanWrite(fsp->brlm_ref, offset, count);
+			DEBUG(8,("posix_fcntl_lock:  [%d]BRLMCanWrite offset[%d] count[%d]\n",brlm_status, offset, count));
+		} else {
+			DEBUG(8,("posix_fcntl_lock:  !!!!! SMB_F_GETLK !!!!! 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) {
+		if (type == F_RDLCK) {
+			brlm_status = BRLMByteRangeLock(fsp->brlm_ref, kBRLMRLock, offset, count);
+			DEBUG(8,("posix_fcntl_lock:  [%d]BRLMByteRangeLock kBRLMRLock\n",brlm_status));		
+		} else if (type == F_WRLCK) {
+			brlm_status = BRLMByteRangeLock(fsp->brlm_ref, kBRLMWLock, offset, count);
+			DEBUG(8,("posix_fcntl_lock:  [%d]BRLMByteRangeLock kBRLMWLock\n",brlm_status));		
+		} else if (type == F_UNLCK) {
+			brlm_status = BRLMByteRangeUnlock(fsp->brlm_ref, kBRLMFree, offset, count);
+			DEBUG(8,("posix_fcntl_lock:  [%d]BRLMByteRangeUnlock kBRLMFree\n",brlm_status));		
+		} else {
+			DEBUG(8,("posix_fcntl_lock:  !!!!! SMB_F_SETLK !!!!! type[0x%X]\n", type));
+			ret = 0;		
+		}
+			
+		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 +778,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/open.c.orig	2005/01/06 23:23:16	1.1.1.15
+++ samba/source/smbd/open.c	2005/07/21 21:42:50
@@ -83,6 +83,59 @@
 }
 
 /****************************************************************************
+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;
+	}
+	DEBUG(4,("map_deny_mode_to_brlm_permissions[0x%X] flags[0x%X]\n",deny_mode, flags));
+	fsp->brlm_deny_mode = deny_mode;
+	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;  
+		   DEBUG(4,("map_open_mode_to_brlm_permissions: DOS_OPEN_RDONLY\n"));
+	} 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.
 ****************************************************************************/
 
@@ -92,6 +145,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;
@@ -172,7 +229,29 @@
 		}
 
 		/* 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 & ~O_ACCMODE/* creatPermissions */, mode, &brlm_ref, 0 /* sessionID */, 0 /* options */);
+		fsp->brlm_ref = brlm_ref;
+		if (BRLMNoErr == brlm_status) {
+			fsp->fd = BRLMGetDescriptor(brlm_ref);
+		} else {
+			if (BRLMOpenDenied == brlm_status || BRLMLockConflict == brlm_status) {
+				unix_ERR_class = ERRDOS;
+				unix_ERR_code = ERRbadshare;
+				unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION;
+			}
+			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 & ~O_ACCMODE, 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));
@@ -1163,7 +1242,10 @@
 			flags2 &= ~O_CREAT;
 		}
 	}
-
+#ifdef WITH_BRLM
+	if (lp_BRLM())
+		map_deny_mode_to_brlm_permissions(fsp, deny_mode); 
+#endif
 	if (file_existed) {
 
 		dev = psbuf->st_dev;
