Whamcloud - gitweb
LU-5095 hsm: Allow receiving messages to be non-blocking
[fs/lustre-release.git] / libcfs / libcfs / kernel_user_comm.c
index c322c85..1c5e0ee 100644 (file)
  * @param link Private descriptor for pipe/socket.
  * @param groups KUC broadcast group to listen to
  *          (can be null for unicast to this pid)
+ * @param rfd_flags flags for read side of pipe (e.g. O_NONBLOCK)
  */
-int libcfs_ukuc_start(lustre_kernelcomm *link, int group)
+int libcfs_ukuc_start(lustre_kernelcomm *link, int group, int rfd_flags)
 {
        int pfd[2];
+       int rc;
 
        link->lk_rfd = link->lk_wfd = LK_NOFD;
 
        if (pipe(pfd) < 0)
                return -errno;
 
+       if (fcntl(pfd[0], F_SETFL, rfd_flags) < 0) {
+               rc = -errno;
+               close(pfd[0]);
+               close(pfd[1]);
+               return rc;
+       }
+
        memset(link, 0, sizeof(*link));
        link->lk_rfd = pfd[0];
        link->lk_wfd = pfd[1];
@@ -80,6 +89,15 @@ int libcfs_ukuc_stop(lustre_kernelcomm *link)
        return rc;
 }
 
+/** Returns the file descriptor for the read side of the pipe,
+ *  to be used with poll/select.
+ * @param link Private descriptor for pipe/socket.
+ */
+int libcfs_ukuc_get_rfd(lustre_kernelcomm *link)
+{
+       return link->lk_rfd;
+}
+
 #define lhsz sizeof(*kuch)
 
 /** Read a message from the link.
@@ -191,23 +209,25 @@ EXPORT_SYMBOL(libcfs_kkuc_msg_put);
 /* Broadcast groups are global across all mounted filesystems;
  * i.e. registering for a group on 1 fs will get messages for that
  * group from any fs */
-/** A single group reigstration has a uid and a file pointer */
+/** A single group registration has a uid and a file pointer */
 struct kkuc_reg {
-       cfs_list_t      kr_chain;
-       int             kr_uid;
+       struct list_head kr_chain;
+       int              kr_uid;
        struct file     *kr_fp;
-       __u32           kr_data;
+       void            *kr_data;
 };
-static cfs_list_t kkuc_groups[KUC_GRP_MAX+1] = {};
+
+static struct list_head kkuc_groups[KUC_GRP_MAX+1] = {};
 /* Protect message sending against remove and adds */
 static DECLARE_RWSEM(kg_sem);
 
 /** Add a receiver to a broadcast group
  * @param filp pipe to write into
- * @param uid identidier for this receiver
+ * @param uid identifier for this receiver
  * @param group group number
+ * @param data user data
  */
-int libcfs_kkuc_group_add(struct file *filp, int uid, int group, __u32 data)
+int libcfs_kkuc_group_add(struct file *filp, int uid, int group, void *data)
 {
         struct kkuc_reg *reg;
 
@@ -231,8 +251,8 @@ int libcfs_kkuc_group_add(struct file *filp, int uid, int group, __u32 data)
 
        down_write(&kg_sem);
        if (kkuc_groups[group].next == NULL)
-               CFS_INIT_LIST_HEAD(&kkuc_groups[group]);
-       cfs_list_add(&reg->kr_chain, &kkuc_groups[group]);
+               INIT_LIST_HEAD(&kkuc_groups[group]);
+       list_add(&reg->kr_chain, &kkuc_groups[group]);
        up_write(&kg_sem);
 
        CDEBUG(D_KUC, "Added uid=%d fp=%p to group %d\n", uid, filp, group);
@@ -241,7 +261,7 @@ int libcfs_kkuc_group_add(struct file *filp, int uid, int group, __u32 data)
 }
 EXPORT_SYMBOL(libcfs_kkuc_group_add);
 
-int libcfs_kkuc_group_rem(int uid, int group)
+int libcfs_kkuc_group_rem(int uid, int group, void **pdata)
 {
         struct kkuc_reg *reg, *next;
         ENTRY;
@@ -261,19 +281,21 @@ int libcfs_kkuc_group_rem(int uid, int group)
         }
 
        down_write(&kg_sem);
-        cfs_list_for_each_entry_safe(reg, next, &kkuc_groups[group], kr_chain) {
-                if ((uid == 0) || (uid == reg->kr_uid)) {
-                        cfs_list_del(&reg->kr_chain);
-                        CDEBUG(D_KUC, "Removed uid=%d fp=%p from group %d\n",
-                               reg->kr_uid, reg->kr_fp, group);
-                        if (reg->kr_fp != NULL)
+       list_for_each_entry_safe(reg, next, &kkuc_groups[group], kr_chain) {
+               if ((uid == 0) || (uid == reg->kr_uid)) {
+                       list_del(&reg->kr_chain);
+                       CDEBUG(D_KUC, "Removed uid=%d fp=%p from group %d\n",
+                               reg->kr_uid, reg->kr_fp, group);
+                       if (reg->kr_fp != NULL)
                                fput(reg->kr_fp);
+                       if (pdata != NULL)
+                               *pdata = reg->kr_data;
                        kfree(reg);
                }
        }
        up_write(&kg_sem);
 
-        RETURN(0);
+       RETURN(0);
 }
 EXPORT_SYMBOL(libcfs_kkuc_group_rem);
 
@@ -285,7 +307,7 @@ int libcfs_kkuc_group_put(int group, void *payload)
        ENTRY;
 
        down_read(&kg_sem);
-       cfs_list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
+       list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
                if (reg->kr_fp != NULL) {
                        rc = libcfs_kkuc_msg_put(reg->kr_fp, payload);
                        if (rc == 0)
@@ -311,33 +333,33 @@ EXPORT_SYMBOL(libcfs_kkuc_group_put);
  * Calls a callback function for each link of the given kuc group.
  * @param group the group to call the function on.
  * @param cb_func the function to be called.
- * @param cb_arg iextra argument to be passed to the callback function.
+ * @param cb_arg extra argument to be passed to the callback function.
  */
 int libcfs_kkuc_group_foreach(int group, libcfs_kkuc_cb_t cb_func,
                               void *cb_arg)
 {
-        struct kkuc_reg *reg;
-        int rc = 0;
-        ENTRY;
+       struct kkuc_reg *reg;
+       int              rc = 0;
+       ENTRY;
 
-        if (group > KUC_GRP_MAX) {
-                CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
-                RETURN(-EINVAL);
-        }
+       if (group > KUC_GRP_MAX) {
+               CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
+               RETURN(-EINVAL);
+       }
 
-        /* no link for this group */
-        if (kkuc_groups[group].next == NULL)
-                RETURN(0);
+       /* no link for this group */
+       if (kkuc_groups[group].next == NULL)
+               RETURN(0);
 
        down_read(&kg_sem);
-        cfs_list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
-                if (reg->kr_fp != NULL) {
-                        rc = cb_func(reg->kr_data, cb_arg);
-                }
-        }
+       list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
+               if (reg->kr_fp != NULL) {
+                       rc = cb_func(reg->kr_data, cb_arg);
+               }
+       }
        up_read(&kg_sem);
 
-        RETURN(rc);
+       RETURN(rc);
 }
 EXPORT_SYMBOL(libcfs_kkuc_group_foreach);