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 3 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, see <http://www.gnu.org/licenses/>.
20 #define DBGC_CLASS DBGC_LOCKING
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",
96 if (0 == (data
.effective
& (1<<capability
))) {
97 data
.effective
|= (1<<capability
);
99 if (capset(&header
, &data
) == -1) {
100 DEBUG(3,("Unable to set %d capability (%s)\n",
101 capability
, strerror(errno
)));
107 * public function to get linux lease capability. Needed by some VFS modules (eg. gpfs.c)
109 void linux_set_lease_capability(void)
111 set_capability(CAP_LEASE
);
115 * Call to set the kernel lease signal handler
117 int linux_set_lease_sighandler(int fd
)
119 if (fcntl(fd
, F_SETSIG
, RT_SIGNAL_LEASE
) == -1) {
120 DEBUG(3,("Failed to set signal handler for kernel lease\n"));
127 /****************************************************************************
128 Call SETLEASE. If we get EACCES then we try setting up the right capability and
130 Use the SMB_VFS_LINUX_SETLEASE instead of this call directly.
131 ****************************************************************************/
133 int linux_setlease(int fd
, int leasetype
)
137 ret
= fcntl(fd
, F_SETLEASE
, leasetype
);
138 if (ret
== -1 && errno
== EACCES
) {
139 set_capability(CAP_LEASE
);
140 ret
= fcntl(fd
, F_SETLEASE
, leasetype
);
146 /****************************************************************************
147 * Deal with the Linux kernel <--> smbd
148 * oplock break protocol.
149 ****************************************************************************/
151 static files_struct
*linux_oplock_receive_message(fd_set
*fds
)
156 BlockSignals(True
, RT_SIGNAL_LEASE
);
157 fd
= fd_pending_array
[0];
158 fsp
= file_find_fd(fd
);
159 fd_pending_array
[0] = (SIG_ATOMIC_T
)-1;
160 if (signals_received
> 1)
161 memmove(CONST_DISCARD(void *, &fd_pending_array
[0]),
162 CONST_DISCARD(void *, &fd_pending_array
[1]),
163 sizeof(SIG_ATOMIC_T
)*(signals_received
-1));
165 /* now we can receive more signals */
166 BlockSignals(False
, RT_SIGNAL_LEASE
);
171 /****************************************************************************
172 Attempt to set an kernel oplock on a file.
173 ****************************************************************************/
175 static bool linux_set_kernel_oplock(files_struct
*fsp
, int oplock_type
)
177 if ( SMB_VFS_LINUX_SETLEASE(fsp
, F_WRLCK
) == -1) {
178 DEBUG(3,("linux_set_kernel_oplock: Refused oplock on file %s, "
179 "fd = %d, file_id = %s. (%s)\n",
180 fsp
->fsp_name
, fsp
->fh
->fd
,
181 file_id_string_tos(&fsp
->file_id
),
186 DEBUG(3,("linux_set_kernel_oplock: got kernel oplock on file %s, "
187 "file_id = %s gen_id = %lu\n",
188 fsp
->fsp_name
, file_id_string_tos(&fsp
->file_id
),
194 /****************************************************************************
195 Release a kernel oplock on a file.
196 ****************************************************************************/
198 static void linux_release_kernel_oplock(files_struct
*fsp
)
202 * Check and print out the current kernel
203 * oplock state of this file.
205 int state
= fcntl(fsp
->fh
->fd
, F_GETLEASE
, 0);
206 dbgtext("linux_release_kernel_oplock: file %s, file_id = %s "
207 "gen_id = %lu has kernel oplock state "
208 "of %x.\n", fsp
->fsp_name
, file_id_string_tos(&fsp
->file_id
),
209 fsp
->fh
->gen_id
, state
);
213 * Remove the kernel oplock on this file.
215 if ( SMB_VFS_LINUX_SETLEASE(fsp
, F_UNLCK
) == -1) {
217 dbgtext("linux_release_kernel_oplock: Error when "
218 "removing kernel oplock on file " );
219 dbgtext("%s, file_id = %s, gen_id = %lu. "
220 "Error was %s\n", fsp
->fsp_name
,
221 file_id_string_tos(&fsp
->file_id
),
222 fsp
->fh
->gen_id
, strerror(errno
) );
227 /****************************************************************************
228 See if a oplock message is waiting.
229 ****************************************************************************/
231 static bool linux_oplock_msg_waiting(fd_set
*fds
)
233 return signals_received
!= 0;
236 /****************************************************************************
237 See if the kernel supports oplocks.
238 ****************************************************************************/
240 static bool linux_oplocks_available(void)
243 fd
= open("/dev/null", O_RDONLY
);
245 return False
; /* uggh! */
246 ret
= fcntl(fd
, F_GETLEASE
, 0);
248 return ret
== F_UNLCK
;
251 /****************************************************************************
252 Setup kernel oplocks.
253 ****************************************************************************/
255 struct kernel_oplocks
*linux_init_kernel_oplocks(void)
257 static struct kernel_oplocks koplocks
;
258 struct sigaction act
;
260 if (!linux_oplocks_available()) {
261 DEBUG(3,("Linux kernel oplocks not available\n"));
267 act
.sa_handler
= NULL
;
268 act
.sa_sigaction
= signal_handler
;
269 act
.sa_flags
= SA_SIGINFO
;
270 sigemptyset( &act
.sa_mask
);
271 if (sigaction(RT_SIGNAL_LEASE
, &act
, NULL
) != 0) {
272 DEBUG(0,("Failed to setup RT_SIGNAL_LEASE handler\n"));
276 koplocks
.receive_message
= linux_oplock_receive_message
;
277 koplocks
.set_oplock
= linux_set_kernel_oplock
;
278 koplocks
.release_oplock
= linux_release_kernel_oplock
;
279 koplocks
.msg_waiting
= linux_oplock_msg_waiting
;
280 koplocks
.notification_fd
= -1;
282 /* the signal can start off blocked due to a bug in bash */
283 BlockSignals(False
, RT_SIGNAL_LEASE
);
285 DEBUG(3,("Linux kernel oplocks enabled\n"));
290 void oplock_linux_dummy(void);
292 void oplock_linux_dummy(void) {}
293 #endif /* HAVE_KERNEL_OPLOCKS_LINUX */