Linux-libre 4.14.132-gnu
[librecmc/linux-libre.git] / drivers / staging / lustre / lustre / obdclass / kernelcomm.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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 version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * Author: Nathan Rutman <nathan.rutman@sun.com>
33  *
34  * Kernel <-> userspace communication routines.
35  * Using pipes for all arches.
36  */
37
38 #define DEBUG_SUBSYSTEM S_CLASS
39 #define D_KUC D_OTHER
40
41 #include <obd_support.h>
42 #include <lustre_kernelcomm.h>
43
44 /**
45  * libcfs_kkuc_msg_put - send an message from kernel to userspace
46  * @param fp to send the message to
47  * @param payload Payload data.  First field of payload is always
48  *   struct kuc_hdr
49  */
50 int libcfs_kkuc_msg_put(struct file *filp, void *payload)
51 {
52         struct kuc_hdr *kuch = (struct kuc_hdr *)payload;
53         ssize_t count = kuch->kuc_msglen;
54         loff_t offset = 0;
55         int rc = -ENXIO;
56
57         if (IS_ERR_OR_NULL(filp))
58                 return -EBADF;
59
60         if (kuch->kuc_magic != KUC_MAGIC) {
61                 CERROR("KernelComm: bad magic %x\n", kuch->kuc_magic);
62                 return rc;
63         }
64
65         while (count > 0) {
66                 rc = kernel_write(filp, payload, count, &offset);
67                 if (rc < 0)
68                         break;
69                 count -= rc;
70                 payload += rc;
71                 rc = 0;
72         }
73
74         if (rc < 0)
75                 CWARN("message send failed (%d)\n", rc);
76         else
77                 CDEBUG(D_KUC, "Sent message rc=%d, fp=%p\n", rc, filp);
78
79         return rc;
80 }
81 EXPORT_SYMBOL(libcfs_kkuc_msg_put);
82
83 /*
84  * Broadcast groups are global across all mounted filesystems;
85  * i.e. registering for a group on 1 fs will get messages for that
86  * group from any fs
87  */
88 /** A single group registration has a uid and a file pointer */
89 struct kkuc_reg {
90         struct list_head kr_chain;
91         int              kr_uid;
92         struct file     *kr_fp;
93         char             kr_data[0];
94 };
95
96 static struct list_head kkuc_groups[KUC_GRP_MAX + 1] = {};
97 /* Protect message sending against remove and adds */
98 static DECLARE_RWSEM(kg_sem);
99
100 /** Add a receiver to a broadcast group
101  * @param filp pipe to write into
102  * @param uid identifier for this receiver
103  * @param group group number
104  * @param data user data
105  */
106 int libcfs_kkuc_group_add(struct file *filp, int uid, unsigned int group,
107                           void *data, size_t data_len)
108 {
109         struct kkuc_reg *reg;
110
111         if (group > KUC_GRP_MAX) {
112                 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
113                 return -EINVAL;
114         }
115
116         /* fput in group_rem */
117         if (!filp)
118                 return -EBADF;
119
120         /* freed in group_rem */
121         reg = kmalloc(sizeof(*reg) + data_len, 0);
122         if (!reg)
123                 return -ENOMEM;
124
125         reg->kr_fp = filp;
126         reg->kr_uid = uid;
127         memcpy(reg->kr_data, data, data_len);
128
129         down_write(&kg_sem);
130         if (!kkuc_groups[group].next)
131                 INIT_LIST_HEAD(&kkuc_groups[group]);
132         list_add(&reg->kr_chain, &kkuc_groups[group]);
133         up_write(&kg_sem);
134
135         CDEBUG(D_KUC, "Added uid=%d fp=%p to group %d\n", uid, filp, group);
136
137         return 0;
138 }
139 EXPORT_SYMBOL(libcfs_kkuc_group_add);
140
141 int libcfs_kkuc_group_rem(int uid, unsigned int group)
142 {
143         struct kkuc_reg *reg, *next;
144
145         if (!kkuc_groups[group].next)
146                 return 0;
147
148         if (!uid) {
149                 /* Broadcast a shutdown message */
150                 struct kuc_hdr lh;
151
152                 lh.kuc_magic = KUC_MAGIC;
153                 lh.kuc_transport = KUC_TRANSPORT_GENERIC;
154                 lh.kuc_msgtype = KUC_MSG_SHUTDOWN;
155                 lh.kuc_msglen = sizeof(lh);
156                 libcfs_kkuc_group_put(group, &lh);
157         }
158
159         down_write(&kg_sem);
160         list_for_each_entry_safe(reg, next, &kkuc_groups[group], kr_chain) {
161                 if (!uid || (uid == reg->kr_uid)) {
162                         list_del(&reg->kr_chain);
163                         CDEBUG(D_KUC, "Removed uid=%d fp=%p from group %d\n",
164                                reg->kr_uid, reg->kr_fp, group);
165                         if (reg->kr_fp)
166                                 fput(reg->kr_fp);
167                         kfree(reg);
168                 }
169         }
170         up_write(&kg_sem);
171
172         return 0;
173 }
174 EXPORT_SYMBOL(libcfs_kkuc_group_rem);
175
176 int libcfs_kkuc_group_put(unsigned int group, void *payload)
177 {
178         struct kkuc_reg *reg;
179         int rc = 0;
180         int one_success = 0;
181
182         down_write(&kg_sem);
183         list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
184                 if (reg->kr_fp) {
185                         rc = libcfs_kkuc_msg_put(reg->kr_fp, payload);
186                         if (!rc) {
187                                 one_success = 1;
188                         } else if (rc == -EPIPE) {
189                                 fput(reg->kr_fp);
190                                 reg->kr_fp = NULL;
191                         }
192                 }
193         }
194         up_write(&kg_sem);
195
196         /*
197          * don't return an error if the message has been delivered
198          * at least to one agent
199          */
200         if (one_success)
201                 rc = 0;
202
203         return rc;
204 }
205 EXPORT_SYMBOL(libcfs_kkuc_group_put);
206
207 /**
208  * Calls a callback function for each link of the given kuc group.
209  * @param group the group to call the function on.
210  * @param cb_func the function to be called.
211  * @param cb_arg extra argument to be passed to the callback function.
212  */
213 int libcfs_kkuc_group_foreach(unsigned int group, libcfs_kkuc_cb_t cb_func,
214                               void *cb_arg)
215 {
216         struct kkuc_reg *reg;
217         int rc = 0;
218
219         if (group > KUC_GRP_MAX) {
220                 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
221                 return -EINVAL;
222         }
223
224         /* no link for this group */
225         if (!kkuc_groups[group].next)
226                 return 0;
227
228         down_read(&kg_sem);
229         list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
230                 if (reg->kr_fp)
231                         rc = cb_func(reg->kr_data, cb_arg);
232         }
233         up_read(&kg_sem);
234
235         return rc;
236 }
237 EXPORT_SYMBOL(libcfs_kkuc_group_foreach);