| 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 2 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, write to the Free Software
|
|---|
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 | #define DBGC_CLASS DBGC_LOCKING
|
|---|
| 22 | #include "includes.h"
|
|---|
| 23 |
|
|---|
| 24 | #if HAVE_KERNEL_OPLOCKS_LINUX
|
|---|
| 25 |
|
|---|
| 26 | /* these can be removed when they are in glibc headers */
|
|---|
| 27 | struct cap_user_header {
|
|---|
| 28 | uint32 version;
|
|---|
| 29 | int pid;
|
|---|
| 30 | } header;
|
|---|
| 31 | struct cap_user_data {
|
|---|
| 32 | uint32 effective;
|
|---|
| 33 | uint32 permitted;
|
|---|
| 34 | uint32 inheritable;
|
|---|
| 35 | } data;
|
|---|
| 36 |
|
|---|
| 37 | extern int capget(struct cap_user_header * hdrp,
|
|---|
| 38 | struct cap_user_data * datap);
|
|---|
| 39 | extern int capset(struct cap_user_header * hdrp,
|
|---|
| 40 | const struct cap_user_data * datap);
|
|---|
| 41 |
|
|---|
| 42 | static SIG_ATOMIC_T signals_received;
|
|---|
| 43 | #define FD_PENDING_SIZE 100
|
|---|
| 44 | static SIG_ATOMIC_T fd_pending_array[FD_PENDING_SIZE];
|
|---|
| 45 |
|
|---|
| 46 | #ifndef F_SETLEASE
|
|---|
| 47 | #define F_SETLEASE 1024
|
|---|
| 48 | #endif
|
|---|
| 49 |
|
|---|
| 50 | #ifndef F_GETLEASE
|
|---|
| 51 | #define F_GETLEASE 1025
|
|---|
| 52 | #endif
|
|---|
| 53 |
|
|---|
| 54 | #ifndef CAP_LEASE
|
|---|
| 55 | #define CAP_LEASE 28
|
|---|
| 56 | #endif
|
|---|
| 57 |
|
|---|
| 58 | #ifndef RT_SIGNAL_LEASE
|
|---|
| 59 | #define RT_SIGNAL_LEASE (SIGRTMIN+1)
|
|---|
| 60 | #endif
|
|---|
| 61 |
|
|---|
| 62 | #ifndef F_SETSIG
|
|---|
| 63 | #define F_SETSIG 10
|
|---|
| 64 | #endif
|
|---|
| 65 |
|
|---|
| 66 | /****************************************************************************
|
|---|
| 67 | Handle a LEASE signal, incrementing the signals_received and blocking the signal.
|
|---|
| 68 | ****************************************************************************/
|
|---|
| 69 |
|
|---|
| 70 | static void signal_handler(int sig, siginfo_t *info, void *unused)
|
|---|
| 71 | {
|
|---|
| 72 | if (signals_received < FD_PENDING_SIZE - 1) {
|
|---|
| 73 | fd_pending_array[signals_received] = (SIG_ATOMIC_T)info->si_fd;
|
|---|
| 74 | signals_received++;
|
|---|
| 75 | } /* Else signal is lost. */
|
|---|
| 76 | sys_select_signal(RT_SIGNAL_LEASE);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /****************************************************************************
|
|---|
| 80 | Try to gain a linux capability.
|
|---|
| 81 | ****************************************************************************/
|
|---|
| 82 |
|
|---|
| 83 | static void set_capability(unsigned capability)
|
|---|
| 84 | {
|
|---|
| 85 | #ifndef _LINUX_CAPABILITY_VERSION
|
|---|
| 86 | #define _LINUX_CAPABILITY_VERSION 0x19980330
|
|---|
| 87 | #endif
|
|---|
| 88 | header.version = _LINUX_CAPABILITY_VERSION;
|
|---|
| 89 | header.pid = 0;
|
|---|
| 90 |
|
|---|
| 91 | if (capget(&header, &data) == -1) {
|
|---|
| 92 | DEBUG(3,("Unable to get kernel capabilities (%s)\n",
|
|---|
| 93 | strerror(errno)));
|
|---|
| 94 | return;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | data.effective |= (1<<capability);
|
|---|
| 98 |
|
|---|
| 99 | if (capset(&header, &data) == -1) {
|
|---|
| 100 | DEBUG(3,("Unable to set %d capability (%s)\n",
|
|---|
| 101 | capability, strerror(errno)));
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | /*
|
|---|
| 106 | Call to set the kernel lease signal handler
|
|---|
| 107 | */
|
|---|
| 108 | int linux_set_lease_sighandler(int fd)
|
|---|
| 109 | {
|
|---|
| 110 | if (fcntl(fd, F_SETSIG, RT_SIGNAL_LEASE) == -1) {
|
|---|
| 111 | DEBUG(3,("Failed to set signal handler for kernel lease\n"));
|
|---|
| 112 | return -1;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | return 0;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | /****************************************************************************
|
|---|
| 119 | Call SETLEASE. If we get EACCES then we try setting up the right capability and
|
|---|
| 120 | try again.
|
|---|
| 121 | Use the SMB_VFS_LINUX_SETLEASE instead of this call directly.
|
|---|
| 122 | ****************************************************************************/
|
|---|
| 123 |
|
|---|
| 124 | int linux_setlease(int fd, int leasetype)
|
|---|
| 125 | {
|
|---|
| 126 | int ret;
|
|---|
| 127 |
|
|---|
| 128 | ret = fcntl(fd, F_SETLEASE, leasetype);
|
|---|
| 129 | if (ret == -1 && errno == EACCES) {
|
|---|
| 130 | set_capability(CAP_LEASE);
|
|---|
| 131 | ret = fcntl(fd, F_SETLEASE, leasetype);
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | return ret;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | /****************************************************************************
|
|---|
| 138 | * Deal with the Linux kernel <--> smbd
|
|---|
| 139 | * oplock break protocol.
|
|---|
| 140 | ****************************************************************************/
|
|---|
| 141 |
|
|---|
| 142 | static files_struct *linux_oplock_receive_message(fd_set *fds)
|
|---|
| 143 | {
|
|---|
| 144 | int fd;
|
|---|
| 145 | files_struct *fsp;
|
|---|
| 146 |
|
|---|
| 147 | BlockSignals(True, RT_SIGNAL_LEASE);
|
|---|
| 148 | fd = fd_pending_array[0];
|
|---|
| 149 | fsp = file_find_fd(fd);
|
|---|
| 150 | fd_pending_array[0] = (SIG_ATOMIC_T)-1;
|
|---|
| 151 | if (signals_received > 1)
|
|---|
| 152 | memmove(CONST_DISCARD(void *, &fd_pending_array[0]),
|
|---|
| 153 | CONST_DISCARD(void *, &fd_pending_array[1]),
|
|---|
| 154 | sizeof(SIG_ATOMIC_T)*(signals_received-1));
|
|---|
| 155 | signals_received--;
|
|---|
| 156 | /* now we can receive more signals */
|
|---|
| 157 | BlockSignals(False, RT_SIGNAL_LEASE);
|
|---|
| 158 |
|
|---|
| 159 | return fsp;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | /****************************************************************************
|
|---|
| 163 | Attempt to set an kernel oplock on a file.
|
|---|
| 164 | ****************************************************************************/
|
|---|
| 165 |
|
|---|
| 166 | static BOOL linux_set_kernel_oplock(files_struct *fsp, int oplock_type)
|
|---|
| 167 | {
|
|---|
| 168 | if ( SMB_VFS_LINUX_SETLEASE(fsp,fsp->fh->fd, F_WRLCK) == -1) {
|
|---|
| 169 | DEBUG(3,("linux_set_kernel_oplock: Refused oplock on file %s, "
|
|---|
| 170 | "fd = %d, dev = %x, inode = %.0f. (%s)\n",
|
|---|
| 171 | fsp->fsp_name, fsp->fh->fd,
|
|---|
| 172 | (unsigned int)fsp->dev, (double)fsp->inode,
|
|---|
| 173 | strerror(errno)));
|
|---|
| 174 | return False;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | DEBUG(3,("linux_set_kernel_oplock: got kernel oplock on file %s, "
|
|---|
| 178 | "dev = %x, inode = %.0f, file_id = %lu\n",
|
|---|
| 179 | fsp->fsp_name, (unsigned int)fsp->dev, (double)fsp->inode,
|
|---|
| 180 | fsp->fh->file_id));
|
|---|
| 181 |
|
|---|
| 182 | return True;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | /****************************************************************************
|
|---|
| 186 | Release a kernel oplock on a file.
|
|---|
| 187 | ****************************************************************************/
|
|---|
| 188 |
|
|---|
| 189 | static void linux_release_kernel_oplock(files_struct *fsp)
|
|---|
| 190 | {
|
|---|
| 191 | if (DEBUGLVL(10)) {
|
|---|
| 192 | /*
|
|---|
| 193 | * Check and print out the current kernel
|
|---|
| 194 | * oplock state of this file.
|
|---|
| 195 | */
|
|---|
| 196 | int state = fcntl(fsp->fh->fd, F_GETLEASE, 0);
|
|---|
| 197 | dbgtext("linux_release_kernel_oplock: file %s, dev = %x, "
|
|---|
| 198 | "inode = %.0f file_id = %lu has kernel oplock state "
|
|---|
| 199 | "of %x.\n", fsp->fsp_name, (unsigned int)fsp->dev,
|
|---|
| 200 | (double)fsp->inode, fsp->fh->file_id, state );
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | /*
|
|---|
| 204 | * Remove the kernel oplock on this file.
|
|---|
| 205 | */
|
|---|
| 206 | if ( SMB_VFS_LINUX_SETLEASE(fsp,fsp->fh->fd, F_UNLCK) == -1) {
|
|---|
| 207 | if (DEBUGLVL(0)) {
|
|---|
| 208 | dbgtext("linux_release_kernel_oplock: Error when "
|
|---|
| 209 | "removing kernel oplock on file " );
|
|---|
| 210 | dbgtext("%s, dev = %x, inode = %.0f, file_id = %lu. "
|
|---|
| 211 | "Error was %s\n", fsp->fsp_name,
|
|---|
| 212 | (unsigned int)fsp->dev, (double)fsp->inode,
|
|---|
| 213 | fsp->fh->file_id, strerror(errno) );
|
|---|
| 214 | }
|
|---|
| 215 | }
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | /****************************************************************************
|
|---|
| 219 | See if a oplock message is waiting.
|
|---|
| 220 | ****************************************************************************/
|
|---|
| 221 |
|
|---|
| 222 | static BOOL linux_oplock_msg_waiting(fd_set *fds)
|
|---|
| 223 | {
|
|---|
| 224 | return signals_received != 0;
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | /****************************************************************************
|
|---|
| 228 | See if the kernel supports oplocks.
|
|---|
| 229 | ****************************************************************************/
|
|---|
| 230 |
|
|---|
| 231 | static BOOL linux_oplocks_available(void)
|
|---|
| 232 | {
|
|---|
| 233 | int fd, ret;
|
|---|
| 234 | fd = open("/dev/null", O_RDONLY);
|
|---|
| 235 | if (fd == -1)
|
|---|
| 236 | return False; /* uggh! */
|
|---|
| 237 | ret = fcntl(fd, F_GETLEASE, 0);
|
|---|
| 238 | close(fd);
|
|---|
| 239 | return ret == F_UNLCK;
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | /****************************************************************************
|
|---|
| 243 | Setup kernel oplocks.
|
|---|
| 244 | ****************************************************************************/
|
|---|
| 245 |
|
|---|
| 246 | struct kernel_oplocks *linux_init_kernel_oplocks(void)
|
|---|
| 247 | {
|
|---|
| 248 | static struct kernel_oplocks koplocks;
|
|---|
| 249 | struct sigaction act;
|
|---|
| 250 |
|
|---|
| 251 | if (!linux_oplocks_available()) {
|
|---|
| 252 | DEBUG(3,("Linux kernel oplocks not available\n"));
|
|---|
| 253 | return NULL;
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | ZERO_STRUCT(act);
|
|---|
| 257 |
|
|---|
| 258 | act.sa_handler = NULL;
|
|---|
| 259 | act.sa_sigaction = signal_handler;
|
|---|
| 260 | act.sa_flags = SA_SIGINFO;
|
|---|
| 261 | sigemptyset( &act.sa_mask );
|
|---|
| 262 | if (sigaction(RT_SIGNAL_LEASE, &act, NULL) != 0) {
|
|---|
| 263 | DEBUG(0,("Failed to setup RT_SIGNAL_LEASE handler\n"));
|
|---|
| 264 | return NULL;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | koplocks.receive_message = linux_oplock_receive_message;
|
|---|
| 268 | koplocks.set_oplock = linux_set_kernel_oplock;
|
|---|
| 269 | koplocks.release_oplock = linux_release_kernel_oplock;
|
|---|
| 270 | koplocks.msg_waiting = linux_oplock_msg_waiting;
|
|---|
| 271 | koplocks.notification_fd = -1;
|
|---|
| 272 |
|
|---|
| 273 | /* the signal can start off blocked due to a bug in bash */
|
|---|
| 274 | BlockSignals(False, RT_SIGNAL_LEASE);
|
|---|
| 275 |
|
|---|
| 276 | DEBUG(3,("Linux kernel oplocks enabled\n"));
|
|---|
| 277 |
|
|---|
| 278 | return &koplocks;
|
|---|
| 279 | }
|
|---|
| 280 | #else
|
|---|
| 281 | void oplock_linux_dummy(void);
|
|---|
| 282 |
|
|---|
| 283 | void oplock_linux_dummy(void) {}
|
|---|
| 284 | #endif /* HAVE_KERNEL_OPLOCKS_LINUX */
|
|---|