2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1992-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 12 aug 96: Erik.Devriendt@te6.siemens.be
24 added support for shared memory implementation of share mode locking
26 May 1997. Jeremy Allison (jallison@whistle.com). Modified share mode
27 locking to deal with multiple share modes per open file.
29 September 1997. Jeremy Allison (jallison@whistle.com). Added oplock
35 extern int DEBUGLEVEL;
37 static struct share_ops *share_ops;
39 /****************************************************************************
40 Utility function to map a lock type correctly depending on the real open
42 ****************************************************************************/
44 static int map_lock_type( files_struct *fsp, int lock_type)
46 if((lock_type == F_WRLCK) && (fsp->fd_ptr->real_open_flags == O_RDONLY)) {
48 * Many UNIX's cannot get a write lock on a file opened read-only.
49 * Win32 locking semantics allow this.
50 * Do the best we can and attempt a read-only lock.
52 DEBUG(10,("map_lock_type: Downgrading write lock to read due to read-only file.\n"));
54 } else if( (lock_type == F_RDLCK) && (fsp->fd_ptr->real_open_flags == O_WRONLY)) {
56 * Ditto for read locks on write only files.
58 DEBUG(10,("map_lock_type: Changing read lock to write due to write-only file.\n"));
63 * This return should be the most normal, as we attempt
64 * to always open files read/write.
70 /****************************************************************************
71 Utility function called to see if a file region is locked.
72 ****************************************************************************/
73 BOOL is_locked(files_struct *fsp,connection_struct *conn,
74 uint32 count,uint32 offset, int lock_type)
76 int snum = SNUM(conn);
81 if (!lp_locking(snum) || !lp_strict_locking(snum))
85 * Note that most UNIX's can *test* for a write lock on
86 * a read-only fd, just not *set* a write lock on a read-only
87 * fd. So we don't need to use map_lock_type here.
90 return(fcntl_lock(fsp->fd_ptr->fd,F_GETLK,offset,count,lock_type));
94 /****************************************************************************
95 Utility function called by locking requests.
96 ****************************************************************************/
97 BOOL do_lock(files_struct *fsp,connection_struct *conn,
98 uint32 count,uint32 offset,int lock_type,
99 int *eclass,uint32 *ecode)
103 if (!lp_locking(SNUM(conn)))
108 *ecode = ERRnoaccess;
112 if (OPEN_FSP(fsp) && fsp->can_lock && (fsp->conn == conn))
113 ok = fcntl_lock(fsp->fd_ptr->fd,F_SETLK,offset,count,
114 map_lock_type(fsp,lock_type));
121 return True; /* Got lock */
125 /****************************************************************************
126 Utility function called by unlocking requests.
127 ****************************************************************************/
128 BOOL do_unlock(files_struct *fsp,connection_struct *conn,
129 uint32 count,uint32 offset,int *eclass,uint32 *ecode)
133 if (!lp_locking(SNUM(conn)))
136 if (OPEN_FSP(fsp) && fsp->can_lock && (fsp->conn == conn))
137 ok = fcntl_lock(fsp->fd_ptr->fd,F_SETLK,offset,count,F_UNLCK);
144 return True; /* Did unlock */
149 /****************************************************************************
150 Initialise the locking functions.
151 ****************************************************************************/
153 BOOL locking_init(int read_only)
155 if (share_ops) return True;
157 #ifdef FAST_SHARE_MODES
158 share_ops = locking_shm_init(read_only);
160 share_ops = locking_slow_init(read_only);
164 DEBUG(0,("ERROR: Failed to initialise share modes!\n"));
171 /*******************************************************************
172 Deinitialize the share_mode management.
173 ******************************************************************/
175 BOOL locking_end(void)
178 return share_ops->stop_mgmt();
183 /*******************************************************************
184 Lock a hash bucket entry.
185 ******************************************************************/
186 BOOL lock_share_entry(connection_struct *conn,
187 uint32 dev, uint32 inode, int *ptok)
189 return share_ops->lock_entry(conn, dev, inode, ptok);
192 /*******************************************************************
193 Unlock a hash bucket entry.
194 ******************************************************************/
195 BOOL unlock_share_entry(connection_struct *conn,
196 uint32 dev, uint32 inode, int token)
198 return share_ops->unlock_entry(conn, dev, inode, token);
201 /*******************************************************************
202 Get all share mode entries for a dev/inode pair.
203 ********************************************************************/
204 int get_share_modes(connection_struct *conn,
205 int token, uint32 dev, uint32 inode,
206 share_mode_entry **shares)
208 return share_ops->get_entries(conn, token, dev, inode, shares);
211 /*******************************************************************
212 Del the share mode of a file.
213 ********************************************************************/
214 void del_share_mode(int token, files_struct *fsp)
216 share_ops->del_entry(token, fsp);
219 /*******************************************************************
220 Set the share mode of a file. Return False on fail, True on success.
221 ********************************************************************/
222 BOOL set_share_mode(int token, files_struct *fsp, uint16 port, uint16 op_type)
224 return share_ops->set_entry(token, fsp, port, op_type);
227 /*******************************************************************
228 Remove an oplock port and mode entry from a share mode.
229 ********************************************************************/
230 BOOL remove_share_oplock(files_struct *fsp, int token)
232 return share_ops->remove_oplock(fsp, token);
235 /*******************************************************************
236 Call the specified function on each entry under management by the
238 ********************************************************************/
240 int share_mode_forall(void (*fn)(share_mode_entry *, char *))
242 return share_ops->forall(fn);
245 /*******************************************************************
246 Dump the state of the system.
247 ********************************************************************/
249 void share_status(FILE *f)
251 share_ops->status(f);