s3:smbd/oplock: pass smbd_server_connection to irix_init_kernel_oplocks()
[kai/samba.git] / source3 / smbd / oplock_irix.c
index 496b0b72ea845d976762dd94dee1e27ebdc53b36..0bc964ffff682a11baf9e95a178e33ed5cd31f1e 100644 (file)
 
 #define DBGC_CLASS DBGC_LOCKING
 #include "includes.h"
+#include "system/filesys.h"
+#include "smbd/smbd.h"
 #include "smbd/globals.h"
 
 #if HAVE_KERNEL_OPLOCKS_IRIX
 
+struct irix_oplocks_context {
+       struct kernel_oplocks *ctx;
+       struct smbd_server_connection *sconn;
+       int write_fd;
+       int read_fd;
+       struct fd_event *read_fde;
+       bool pending;
+};
+
 /****************************************************************************
  Test to see if IRIX kernel oplocks work.
 ****************************************************************************/
@@ -95,27 +106,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) ));
@@ -128,7 +162,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) ));
@@ -151,7 +185,7 @@ static files_struct *irix_oplock_receive_message(fd_set *fds)
 
        fileid = file_id_create_dev((SMB_DEV_T)os.os_dev,
                                    (SMB_INO_T)os.os_ino);
-       if ((fsp = file_find_di_first(fileid)) == NULL) {
+       if ((fsp = file_find_di_first(ctx->sconn, fileid)) == NULL) {
                DEBUG(0,("irix_oplock_receive_message: unable to find open "
                         "file with dev = %x, inode = %.0f\n",
                         (unsigned int)os.os_dev, (double)os.os_ino ));
@@ -170,14 +204,19 @@ 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 "
                                 "gen_id = %ul. Error was %s\n", 
-                                fsp->fsp_name, file_id_string_tos(&fsp->file_id), 
+                                fsp_str_dbg(fsp),
+                                file_id_string_tos(&fsp->file_id),
                                 fsp->fh->gen_id,
                                 strerror(errno) ));
                } else {
@@ -185,7 +224,7 @@ static bool irix_set_kernel_oplock(files_struct *fsp, int oplock_type)
                                 "file %s, fd = %d, file_id = %s, "
                                 "gen_id = %ul. Another process had the file "
                                 "open.\n",
-                                fsp->fsp_name, fsp->fh->fd,
+                                fsp_str_dbg(fsp), fsp->fh->fd,
                                 file_id_string_tos(&fsp->file_id),
                                 fsp->fh->gen_id ));
                }
@@ -194,7 +233,7 @@ static bool irix_set_kernel_oplock(files_struct *fsp, int oplock_type)
        
        DEBUG(10,("irix_set_kernel_oplock: got kernel oplock on file %s, file_id = %s "
                  "gen_id = %ul\n",
-                 fsp->fsp_name, file_id_string_tos(&fsp->file_id),
+                 fsp_str_dbg(fsp), file_id_string_tos(&fsp->file_id),
                  fsp->fh->gen_id));
 
        return True;
@@ -204,7 +243,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)) {
                /*
@@ -214,7 +254,8 @@ static void irix_release_kernel_oplock(files_struct *fsp)
                int state = sys_fcntl_long(fsp->fh->fd, F_OPLKACK, -1);
                dbgtext("irix_release_kernel_oplock: file %s, file_id = %s"
                        "gen_id = %ul, has kernel oplock state "
-                       "of %x.\n", fsp->fsp_name, file_id_string_tos(&fsp->file_id),
+                       "of %x.\n", fsp_str_dbg(fsp),
+                       file_id_string_tos(&fsp->file_id),
                         fsp->fh->gen_id, state );
        }
 
@@ -227,70 +268,79 @@ static void irix_release_kernel_oplock(files_struct *fsp)
                                "removing kernel oplock on file " );
                        dbgtext("%s, file_id = %s gen_id = %ul. "
                                "Error was %s\n",
-                               fsp->fsp_name, file_id_string_tos(&fsp->file_id),
+                               fsp_str_dbg(fsp),
+                               file_id_string_tos(&fsp->file_id),
                                fsp->fh->gen_id,
                                strerror(errno) );
                }
        }
 }
 
-/****************************************************************************
- 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(ctx->sconn->msg_ctx, 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(struct smbd_server_connection *sconn)
 {
+       struct kernel_oplocks *_ctx;
+       struct irix_oplocks_context *ctx;
        int pfd[2];
 
        if (!irix_oplocks_available())
                return NULL;
 
+       _ctx = talloc_zero(sconn, 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;
+       ctx->sconn = sconn;
+
        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];
-
-       irix_koplocks.receive_message = irix_oplock_receive_message;
-       irix_koplocks.set_oplock = irix_set_kernel_oplock;
-       irix_koplocks.release_oplock = irix_release_kernel_oplock;
-       irix_koplocks.msg_waiting = irix_oplock_msg_waiting;
-       irix_koplocks.notification_fd = oplock_pipe_read;
+       ctx->read_fd = pfd[0];
+       ctx->write_fd = pfd[1];
 
-       return &irix_koplocks;
+       ctx->read_fde = event_add_fd(sconn->ev_ctx,
+                                    ctx,
+                                    ctx->read_fd,
+                                    EVENT_FD_READ,
+                                    irix_oplocks_read_fde_handler,
+                                    ctx);
+       return _ctx;
 }
 #else
  void oplock_irix_dummy(void);