1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | kernel oplock processing for Linux
|
---|
4 | Copyright (C) Andrew Tridgell 2000
|
---|
5 |
|
---|
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.
|
---|
10 |
|
---|
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.
|
---|
15 |
|
---|
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/>.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #define DBGC_CLASS DBGC_LOCKING
|
---|
21 | #include "includes.h"
|
---|
22 | #include "smbd/globals.h"
|
---|
23 |
|
---|
24 | #if HAVE_KERNEL_OPLOCKS_LINUX
|
---|
25 |
|
---|
26 | #ifndef F_SETLEASE
|
---|
27 | #define F_SETLEASE 1024
|
---|
28 | #endif
|
---|
29 |
|
---|
30 | #ifndef F_GETLEASE
|
---|
31 | #define F_GETLEASE 1025
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #ifndef CAP_LEASE
|
---|
35 | #define CAP_LEASE 28
|
---|
36 | #endif
|
---|
37 |
|
---|
38 | #ifndef RT_SIGNAL_LEASE
|
---|
39 | #define RT_SIGNAL_LEASE (SIGRTMIN+1)
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | #ifndef F_SETSIG
|
---|
43 | #define F_SETSIG 10
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | /*
|
---|
47 | * public function to get linux lease capability. Needed by some VFS modules (eg. gpfs.c)
|
---|
48 | */
|
---|
49 | void linux_set_lease_capability(void)
|
---|
50 | {
|
---|
51 | set_effective_capability(LEASE_CAPABILITY);
|
---|
52 | }
|
---|
53 |
|
---|
54 | /*
|
---|
55 | * Call to set the kernel lease signal handler
|
---|
56 | */
|
---|
57 | int linux_set_lease_sighandler(int fd)
|
---|
58 | {
|
---|
59 | if (fcntl(fd, F_SETSIG, RT_SIGNAL_LEASE) == -1) {
|
---|
60 | DEBUG(3,("Failed to set signal handler for kernel lease\n"));
|
---|
61 | return -1;
|
---|
62 | }
|
---|
63 |
|
---|
64 | return 0;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /****************************************************************************
|
---|
68 | Call SETLEASE. If we get EACCES then we try setting up the right capability and
|
---|
69 | try again.
|
---|
70 | Use the SMB_VFS_LINUX_SETLEASE instead of this call directly.
|
---|
71 | ****************************************************************************/
|
---|
72 |
|
---|
73 | int linux_setlease(int fd, int leasetype)
|
---|
74 | {
|
---|
75 | int ret;
|
---|
76 |
|
---|
77 | /* First set the signal handler. */
|
---|
78 | if (linux_set_lease_sighandler(fd) == -1) {
|
---|
79 | return -1;
|
---|
80 | }
|
---|
81 | ret = fcntl(fd, F_SETLEASE, leasetype);
|
---|
82 | if (ret == -1 && errno == EACCES) {
|
---|
83 | set_effective_capability(LEASE_CAPABILITY);
|
---|
84 | /*
|
---|
85 | * Bug 8974 - work around Linux kernel bug
|
---|
86 | * https://bugzilla.kernel.org/show_bug.cgi?id=43336.
|
---|
87 | * "fcntl(F_SETLEASE) resets signal number when
|
---|
88 | * called multiple times"
|
---|
89 | */
|
---|
90 | if (linux_set_lease_sighandler(fd) == -1) {
|
---|
91 | return -1;
|
---|
92 | }
|
---|
93 | ret = fcntl(fd, F_SETLEASE, leasetype);
|
---|
94 | }
|
---|
95 |
|
---|
96 | return ret;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /****************************************************************************
|
---|
100 | * Deal with the Linux kernel <--> smbd
|
---|
101 | * oplock break protocol.
|
---|
102 | ****************************************************************************/
|
---|
103 |
|
---|
104 | static void linux_oplock_signal_handler(struct tevent_context *ev_ctx,
|
---|
105 | struct tevent_signal *se,
|
---|
106 | int signum, int count,
|
---|
107 | void *_info, void *private_data)
|
---|
108 | {
|
---|
109 | siginfo_t *info = (siginfo_t *)_info;
|
---|
110 | int fd = info->si_fd;
|
---|
111 | files_struct *fsp;
|
---|
112 |
|
---|
113 | fsp = file_find_fd(fd);
|
---|
114 | if (fsp == NULL) {
|
---|
115 | DEBUG(0,("linux_oplock_signal_handler: failed to find fsp for file fd=%d (file was closed ?)\n", fd ));
|
---|
116 | return;
|
---|
117 | }
|
---|
118 | break_kernel_oplock(smbd_messaging_context(), fsp);
|
---|
119 | }
|
---|
120 |
|
---|
121 | /****************************************************************************
|
---|
122 | Attempt to set an kernel oplock on a file.
|
---|
123 | ****************************************************************************/
|
---|
124 |
|
---|
125 | static bool linux_set_kernel_oplock(struct kernel_oplocks *ctx,
|
---|
126 | files_struct *fsp, int oplock_type)
|
---|
127 | {
|
---|
128 | if ( SMB_VFS_LINUX_SETLEASE(fsp, F_WRLCK) == -1) {
|
---|
129 | DEBUG(3,("linux_set_kernel_oplock: Refused oplock on file %s, "
|
---|
130 | "fd = %d, file_id = %s. (%s)\n",
|
---|
131 | fsp_str_dbg(fsp), fsp->fh->fd,
|
---|
132 | file_id_string_tos(&fsp->file_id),
|
---|
133 | strerror(errno)));
|
---|
134 | return False;
|
---|
135 | }
|
---|
136 |
|
---|
137 | DEBUG(3,("linux_set_kernel_oplock: got kernel oplock on file %s, "
|
---|
138 | "file_id = %s gen_id = %lu\n",
|
---|
139 | fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id),
|
---|
140 | fsp->fh->gen_id));
|
---|
141 |
|
---|
142 | return True;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /****************************************************************************
|
---|
146 | Release a kernel oplock on a file.
|
---|
147 | ****************************************************************************/
|
---|
148 |
|
---|
149 | static void linux_release_kernel_oplock(struct kernel_oplocks *ctx,
|
---|
150 | files_struct *fsp, int oplock_type)
|
---|
151 | {
|
---|
152 | if (DEBUGLVL(10)) {
|
---|
153 | /*
|
---|
154 | * Check and print out the current kernel
|
---|
155 | * oplock state of this file.
|
---|
156 | */
|
---|
157 | int state = fcntl(fsp->fh->fd, F_GETLEASE, 0);
|
---|
158 | dbgtext("linux_release_kernel_oplock: file %s, file_id = %s "
|
---|
159 | "gen_id = %lu has kernel oplock state "
|
---|
160 | "of %x.\n", fsp_str_dbg(fsp),
|
---|
161 | file_id_string_tos(&fsp->file_id),
|
---|
162 | fsp->fh->gen_id, state );
|
---|
163 | }
|
---|
164 |
|
---|
165 | /*
|
---|
166 | * Remove the kernel oplock on this file.
|
---|
167 | */
|
---|
168 | if ( SMB_VFS_LINUX_SETLEASE(fsp, F_UNLCK) == -1) {
|
---|
169 | if (DEBUGLVL(0)) {
|
---|
170 | dbgtext("linux_release_kernel_oplock: Error when "
|
---|
171 | "removing kernel oplock on file " );
|
---|
172 | dbgtext("%s, file_id = %s, gen_id = %lu. "
|
---|
173 | "Error was %s\n", fsp_str_dbg(fsp),
|
---|
174 | file_id_string_tos(&fsp->file_id),
|
---|
175 | fsp->fh->gen_id, strerror(errno) );
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | /****************************************************************************
|
---|
181 | See if the kernel supports oplocks.
|
---|
182 | ****************************************************************************/
|
---|
183 |
|
---|
184 | static bool linux_oplocks_available(void)
|
---|
185 | {
|
---|
186 | int fd, ret;
|
---|
187 | fd = open("/dev/null", O_RDONLY);
|
---|
188 | if (fd == -1)
|
---|
189 | return False; /* uggh! */
|
---|
190 | ret = fcntl(fd, F_GETLEASE, 0);
|
---|
191 | close(fd);
|
---|
192 | return ret == F_UNLCK;
|
---|
193 | }
|
---|
194 |
|
---|
195 | /****************************************************************************
|
---|
196 | Setup kernel oplocks.
|
---|
197 | ****************************************************************************/
|
---|
198 |
|
---|
199 | static const struct kernel_oplocks_ops linux_koplocks = {
|
---|
200 | .set_oplock = linux_set_kernel_oplock,
|
---|
201 | .release_oplock = linux_release_kernel_oplock,
|
---|
202 | .contend_level2_oplocks_begin = NULL,
|
---|
203 | .contend_level2_oplocks_end = NULL,
|
---|
204 | };
|
---|
205 |
|
---|
206 | struct kernel_oplocks *linux_init_kernel_oplocks(TALLOC_CTX *mem_ctx)
|
---|
207 | {
|
---|
208 | struct kernel_oplocks *ctx;
|
---|
209 | struct tevent_signal *se;
|
---|
210 |
|
---|
211 | if (!linux_oplocks_available()) {
|
---|
212 | DEBUG(3,("Linux kernel oplocks not available\n"));
|
---|
213 | return NULL;
|
---|
214 | }
|
---|
215 |
|
---|
216 | ctx = talloc_zero(mem_ctx, struct kernel_oplocks);
|
---|
217 | if (!ctx) {
|
---|
218 | DEBUG(0,("Linux Kernel oplocks talloc_Zero failed\n"));
|
---|
219 | return NULL;
|
---|
220 | }
|
---|
221 |
|
---|
222 | ctx->ops = &linux_koplocks;
|
---|
223 |
|
---|
224 | se = tevent_add_signal(smbd_event_context(),
|
---|
225 | ctx,
|
---|
226 | RT_SIGNAL_LEASE, SA_SIGINFO,
|
---|
227 | linux_oplock_signal_handler,
|
---|
228 | ctx);
|
---|
229 | if (!se) {
|
---|
230 | DEBUG(0,("Failed to setup RT_SIGNAL_LEASE handler"));
|
---|
231 | TALLOC_FREE(ctx);
|
---|
232 | return NULL;
|
---|
233 | }
|
---|
234 |
|
---|
235 | ctx->private_data = se;
|
---|
236 |
|
---|
237 | DEBUG(3,("Linux kernel oplocks enabled\n"));
|
---|
238 |
|
---|
239 | return ctx;
|
---|
240 | }
|
---|
241 | #else
|
---|
242 | void oplock_linux_dummy(void);
|
---|
243 |
|
---|
244 | void oplock_linux_dummy(void) {}
|
---|
245 | #endif /* HAVE_KERNEL_OPLOCKS_LINUX */
|
---|