2 Unix SMB/CIFS implementation.
3 kernel oplock processing for Linux
4 Copyright (C) Andrew Tridgell 2000
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #if HAVE_KERNEL_OPLOCKS_LINUX
25 /* these can be removed when they are in glibc headers */
26 struct cap_user_header
{
30 struct cap_user_data
{
36 extern int capget(struct cap_user_header
* hdrp
,
37 struct cap_user_data
* datap
);
38 extern int capset(struct cap_user_header
* hdrp
,
39 const struct cap_user_data
* datap
);
41 static SIG_ATOMIC_T signals_received
;
42 #define FD_PENDING_SIZE 100
43 static SIG_ATOMIC_T fd_pending_array
[FD_PENDING_SIZE
];
46 #define F_SETLEASE 1024
50 #define F_GETLEASE 1025
57 #ifndef RT_SIGNAL_LEASE
58 #define RT_SIGNAL_LEASE (SIGRTMIN+1)
65 /****************************************************************************
66 Handle a LEASE signal, incrementing the signals_received and blocking the signal.
67 ****************************************************************************/
69 static void signal_handler(int sig
, siginfo_t
*info
, void *unused
)
71 if (signals_received
< FD_PENDING_SIZE
- 1) {
72 fd_pending_array
[signals_received
] = (SIG_ATOMIC_T
)info
->si_fd
;
74 } /* Else signal is lost. */
75 sys_select_signal(RT_SIGNAL_LEASE
);
78 /****************************************************************************
79 Try to gain a linux capability.
80 ****************************************************************************/
82 static void set_capability(unsigned capability
)
84 #ifndef _LINUX_CAPABILITY_VERSION
85 #define _LINUX_CAPABILITY_VERSION 0x19980330
87 header
.version
= _LINUX_CAPABILITY_VERSION
;
90 if (capget(&header
, &data
) == -1) {
91 DEBUG(3,("Unable to get kernel capabilities (%s)\n", strerror(errno
)));
95 data
.effective
|= (1<<capability
);
97 if (capset(&header
, &data
) == -1) {
98 DEBUG(3,("Unable to set %d capability (%s)\n",
99 capability
, strerror(errno
)));
103 /****************************************************************************
104 Call SETLEASE. If we get EACCES then we try setting up the right capability and
106 ****************************************************************************/
108 static int linux_setlease(int fd
, int leasetype
)
112 if (fcntl(fd
, F_SETSIG
, RT_SIGNAL_LEASE
) == -1) {
113 DEBUG(3,("Failed to set signal handler for kernel lease\n"));
117 ret
= fcntl(fd
, F_SETLEASE
, leasetype
);
118 if (ret
== -1 && errno
== EACCES
) {
119 set_capability(CAP_LEASE
);
120 ret
= fcntl(fd
, F_SETLEASE
, leasetype
);
126 /****************************************************************************
127 * Deal with the Linux kernel <--> smbd
128 * oplock break protocol.
129 ****************************************************************************/
131 static files_struct
*linux_oplock_receive_message(fd_set
*fds
)
136 BlockSignals(True
, RT_SIGNAL_LEASE
);
137 fd
= fd_pending_array
[0];
138 fsp
= file_find_fd(fd
);
139 fd_pending_array
[0] = (SIG_ATOMIC_T
)-1;
140 if (signals_received
> 1)
141 memmove(CONST_DISCARD(void *, &fd_pending_array
[0]),
142 CONST_DISCARD(void *, &fd_pending_array
[1]),
143 sizeof(SIG_ATOMIC_T
)*(signals_received
-1));
145 /* now we can receive more signals */
146 BlockSignals(False
, RT_SIGNAL_LEASE
);
151 /****************************************************************************
152 Attempt to set an kernel oplock on a file.
153 ****************************************************************************/
155 static BOOL
linux_set_kernel_oplock(files_struct
*fsp
, int oplock_type
)
157 if (linux_setlease(fsp
->fh
->fd
, F_WRLCK
) == -1) {
158 DEBUG(3,("linux_set_kernel_oplock: Refused oplock on file %s, fd = %d, dev = %x, \
159 inode = %.0f. (%s)\n",
160 fsp
->fsp_name
, fsp
->fh
->fd
,
161 (unsigned int)fsp
->dev
, (double)fsp
->inode
, strerror(errno
)));
165 DEBUG(3,("linux_set_kernel_oplock: got kernel oplock on file %s, dev = %x, inode = %.0f, file_id = %lu\n",
166 fsp
->fsp_name
, (unsigned int)fsp
->dev
, (double)fsp
->inode
, fsp
->file_id
));
171 /****************************************************************************
172 Release a kernel oplock on a file.
173 ****************************************************************************/
175 static void linux_release_kernel_oplock(files_struct
*fsp
)
179 * Check and print out the current kernel
180 * oplock state of this file.
182 int state
= fcntl(fsp
->fh
->fd
, F_GETLEASE
, 0);
183 dbgtext("linux_release_kernel_oplock: file %s, dev = %x, inode = %.0f file_id = %lu has kernel \
184 oplock state of %x.\n", fsp
->fsp_name
, (unsigned int)fsp
->dev
,
185 (double)fsp
->inode
, fsp
->file_id
, state
);
189 * Remove the kernel oplock on this file.
191 if (linux_setlease(fsp
->fh
->fd
, F_UNLCK
) == -1) {
193 dbgtext("linux_release_kernel_oplock: Error when removing kernel oplock on file " );
194 dbgtext("%s, dev = %x, inode = %.0f, file_id = %lu. Error was %s\n",
195 fsp
->fsp_name
, (unsigned int)fsp
->dev
,
196 (double)fsp
->inode
, fsp
->file_id
, strerror(errno
) );
201 /****************************************************************************
202 See if a oplock message is waiting.
203 ****************************************************************************/
205 static BOOL
linux_oplock_msg_waiting(fd_set
*fds
)
207 return signals_received
!= 0;
210 /****************************************************************************
211 See if the kernel supports oplocks.
212 ****************************************************************************/
214 static BOOL
linux_oplocks_available(void)
217 fd
= open("/dev/null", O_RDONLY
);
219 return False
; /* uggh! */
220 ret
= fcntl(fd
, F_GETLEASE
, 0);
222 return ret
== F_UNLCK
;
225 /****************************************************************************
226 Setup kernel oplocks.
227 ****************************************************************************/
229 struct kernel_oplocks
*linux_init_kernel_oplocks(void)
231 static struct kernel_oplocks koplocks
;
232 struct sigaction act
;
234 if (!linux_oplocks_available()) {
235 DEBUG(3,("Linux kernel oplocks not available\n"));
241 act
.sa_handler
= NULL
;
242 act
.sa_sigaction
= signal_handler
;
243 act
.sa_flags
= SA_SIGINFO
;
244 sigemptyset( &act
.sa_mask
);
245 if (sigaction(RT_SIGNAL_LEASE
, &act
, NULL
) != 0) {
246 DEBUG(0,("Failed to setup RT_SIGNAL_LEASE handler\n"));
250 koplocks
.receive_message
= linux_oplock_receive_message
;
251 koplocks
.set_oplock
= linux_set_kernel_oplock
;
252 koplocks
.release_oplock
= linux_release_kernel_oplock
;
253 koplocks
.msg_waiting
= linux_oplock_msg_waiting
;
254 koplocks
.notification_fd
= -1;
256 /* the signal can start off blocked due to a bug in bash */
257 BlockSignals(False
, RT_SIGNAL_LEASE
);
259 DEBUG(3,("Linux kernel oplocks enabled\n"));
264 void oplock_linux_dummy(void);
266 void oplock_linux_dummy(void) {}
267 #endif /* HAVE_KERNEL_OPLOCKS_LINUX */