s3:smbd: move pending_auth_data list to struct smbd_server_connection
[kai/samba.git] / source3 / smbd / oplock_irix.c
index 788cd04c17d7274988213a464248e2f69124863f..89b8e0f7b58489c738c8d1f693c24e06f222bd72 100644 (file)
 
 #define DBGC_CLASS DBGC_LOCKING
 #include "includes.h"
+#include "smbd/globals.h"
 
 #if HAVE_KERNEL_OPLOCKS_IRIX
 
-static int oplock_pipe_write = -1;
-static int oplock_pipe_read = -1;
+struct irix_oplocks_context {
+       struct kernel_oplocks *ctx;
+       int write_fd;
+       int read_fd;
+       struct fd_event *read_fde;
+       bool pending;
+};
 
 /****************************************************************************
  Test to see if IRIX kernel oplocks work.
@@ -97,27 +103,50 @@ static bool irix_oplocks_available(void)
        return True;
 }
 
+/*
+ * This is bad because the file_id should always be created through the vfs
+ * layer!  Unfortunately, a conn struct isn't available here.
+ */
+static struct file_id file_id_create_dev(SMB_DEV_T dev, SMB_INO_T inode)
+{
+       struct file_id key;
+
+       /* the ZERO_STRUCT ensures padding doesn't break using the key as a
+        * blob */
+       ZERO_STRUCT(key);
+
+       key.devid = dev;
+       key.inode = inode;
+
+       return key;
+}
+
 /****************************************************************************
  * Deal with the IRIX kernel <--> smbd
  * oplock break protocol.
 ****************************************************************************/
 
-static files_struct *irix_oplock_receive_message(fd_set *fds)
+static files_struct *irix_oplock_receive_message(struct kernel_oplocks *_ctx)
 {
+       struct irix_oplocks_context *ctx = talloc_get_type(_ctx->private_data,
+                                          struct irix_oplocks_context);
        oplock_stat_t os;
        char dummy;
        struct file_id fileid;
        files_struct *fsp;
 
-       /* Ensure we only get one call per select fd set. */
-       FD_CLR(oplock_pipe_read, fds);
+       /*
+        * TODO: is it correct to assume we only get one
+        * oplock break, for each byte we read from the pipe?
+        */
+       ctx->pending = false;
 
        /*
         * Read one byte of zero to clear the
         * kernel break notify message.
         */
 
-       if(read(oplock_pipe_read, &dummy, 1) != 1) {
+       if(read(ctx->read_fd, &dummy, 1) != 1) {
                DEBUG(0,("irix_oplock_receive_message: read of kernel "
                         "notification failed. Error was %s.\n",
                         strerror(errno) ));
@@ -130,7 +159,7 @@ static files_struct *irix_oplock_receive_message(fd_set *fds)
         * request outstanding.
         */
 
-       if(sys_fcntl_ptr(oplock_pipe_read, F_OPLKSTAT, &os) < 0) {
+       if(sys_fcntl_ptr(ctx->read_fd, F_OPLKSTAT, &os) < 0) {
                DEBUG(0,("irix_oplock_receive_message: fcntl of kernel "
                         "notification failed. Error was %s.\n",
                         strerror(errno) ));
@@ -172,9 +201,13 @@ static files_struct *irix_oplock_receive_message(fd_set *fds)
  Attempt to set an kernel oplock on a file.
 ****************************************************************************/
 
-static bool irix_set_kernel_oplock(files_struct *fsp, int oplock_type)
+static bool irix_set_kernel_oplock(struct kernel_oplocks *_ctx,
+                                  files_struct *fsp, int oplock_type)
 {
-       if (sys_fcntl_long(fsp->fh->fd, F_OPLKREG, oplock_pipe_write) == -1) {
+       struct irix_oplocks_context *ctx = talloc_get_type(_ctx->private_data,
+                                          struct irix_oplocks_context);
+
+       if (sys_fcntl_long(fsp->fh->fd, F_OPLKREG, ctx->write_fd) == -1) {
                if(errno != EAGAIN) {
                        DEBUG(0,("irix_set_kernel_oplock: Unable to get "
                                 "kernel oplock on file %s, file_id %s "
@@ -184,7 +217,7 @@ static bool irix_set_kernel_oplock(files_struct *fsp, int oplock_type)
                                 strerror(errno) ));
                } else {
                        DEBUG(5,("irix_set_kernel_oplock: Refused oplock on "
-                                "file %s, fd = %d, file_id = 5s, "
+                                "file %s, fd = %d, file_id = %s, "
                                 "gen_id = %ul. Another process had the file "
                                 "open.\n",
                                 fsp->fsp_name, fsp->fh->fd,
@@ -206,7 +239,8 @@ static bool irix_set_kernel_oplock(files_struct *fsp, int oplock_type)
  Release a kernel oplock on a file.
 ****************************************************************************/
 
-static void irix_release_kernel_oplock(files_struct *fsp)
+static void irix_release_kernel_oplock(struct kernel_oplocks *_ctx,
+                                      files_struct *fsp, int oplock_type)
 {
        if (DEBUGLVL(10)) {
                /*
@@ -236,64 +270,70 @@ static void irix_release_kernel_oplock(files_struct *fsp)
        }
 }
 
-/****************************************************************************
- See if there is a message waiting in this fd set.
- Note that fds MAY BE NULL ! If so we must do our own select.
-****************************************************************************/
-
-static bool irix_oplock_msg_waiting(fd_set *fds)
+static void irix_oplocks_read_fde_handler(struct event_context *ev,
+                                         struct fd_event *fde,
+                                         uint16_t flags,
+                                         void *private_data)
 {
-       int selrtn;
-       fd_set myfds;
-       struct timeval to;
-
-       if (oplock_pipe_read == -1)
-               return False;
-
-       if (fds) {
-               return FD_ISSET(oplock_pipe_read, fds);
-       }
-
-       /* Do a zero-time select. We just need to find out if there
-        * are any outstanding messages. We use sys_select_intr as
-        * we need to ignore any signals. */
-
-       FD_ZERO(&myfds);
-       FD_SET(oplock_pipe_read, &myfds);
+       struct irix_oplocks_context *ctx = talloc_get_type(private_data,
+                                          struct irix_oplocks_context);
+       files_struct *fsp;
 
-       to = timeval_set(0, 0);
-       selrtn = sys_select_intr(oplock_pipe_read+1,&myfds,NULL,NULL,&to);
-       return (selrtn == 1) ? True : False;
+       fsp = irix_oplock_receive_message(ctx->ctx);
+       break_kernel_oplock(smbd_messaging_context(), fsp);
 }
 
 /****************************************************************************
  Setup kernel oplocks.
 ****************************************************************************/
 
-struct kernel_oplocks *irix_init_kernel_oplocks(void) 
+static const struct kernel_oplocks_ops irix_koplocks = {
+       .set_oplock                     = irix_set_kernel_oplock,
+       .release_oplock                 = irix_release_kernel_oplock,
+       .contend_level2_oplocks_begin   = NULL,
+       .contend_level2_oplocks_end     = NULL,
+};
+
+struct kernel_oplocks *irix_init_kernel_oplocks(TALLOC_CTX *mem_ctx)
 {
+       struct kernel_oplocks *_ctx;
+       struct irix_oplocks_context *ctx;
        int pfd[2];
-       static struct kernel_oplocks koplocks;
 
        if (!irix_oplocks_available())
                return NULL;
 
+       _ctx = talloc_zero(mem_ctx, struct kernel_oplocks);
+       if (!_ctx) {
+               return NULL;
+       }
+
+       ctx = talloc_zero(_ctx, struct irix_oplocks_context);
+       if (!ctx) {
+               talloc_free(_ctx);
+               return NULL;
+       }
+       _ctx->ops = &irix_koplocks;
+       _ctx->private_data = ctx;
+       ctx->ctx = _ctx;
+
        if(pipe(pfd) != 0) {
+               talloc_free(_ctx);
                DEBUG(0,("setup_kernel_oplock_pipe: Unable to create pipe. "
                         "Error was %s\n", strerror(errno) ));
                return False;
        }
 
-       oplock_pipe_read = pfd[0];
-       oplock_pipe_write = pfd[1];
-
-       koplocks.receive_message = irix_oplock_receive_message;
-       koplocks.set_oplock = irix_set_kernel_oplock;
-       koplocks.release_oplock = irix_release_kernel_oplock;
-       koplocks.msg_waiting = irix_oplock_msg_waiting;
-       koplocks.notification_fd = oplock_pipe_read;
+       ctx->read_fd = pfd[0];
+       ctx->write_fd = pfd[1];
 
-       return &koplocks;
+       ctx->read_fde = event_add_fd(smbd_event_context(),
+                                    ctx,
+                                    ctx->read_fd,
+                                    EVENT_FD_READ,
+                                    irix_oplocks_read_fde_handler,
+                                    ctx);
+       return _ctx;
 }
 #else
  void oplock_irix_dummy(void);