Whamcloud - gitweb
LU-10308 misc: update Intel copyright messages for 2017
[fs/lustre-release.git] / 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) 2015, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * Author: Nathan Rutman <nathan.rutman@sun.com>
32  *
33  * Kernel <-> userspace communication routines.
34  * Using pipes for all arches.
35  */
36
37 #define DEBUG_SUBSYSTEM S_CLASS
38
39 #include <obd_support.h>
40 #include <lustre_kernelcomm.h>
41
42 /* write a userspace buffer to disk.
43  * NOTE: this returns 0 on success, not the number of bytes written. */
44 static ssize_t
45 filp_user_write(struct file *filp, const void *buf, size_t count,
46                 loff_t *offset)
47 {
48         mm_segment_t fs;
49         ssize_t size = 0;
50
51         fs = get_fs();
52         set_fs(KERNEL_DS);
53         while ((ssize_t)count > 0) {
54                 size = vfs_write(filp, (const void __user *)buf, count, offset);
55                 if (size < 0)
56                         break;
57                 count -= size;
58                 buf += size;
59                 size = 0;
60         }
61         set_fs(fs);
62
63         return size;
64 }
65
66 /**
67  * libcfs_kkuc_msg_put - send an message from kernel to userspace
68  * @param fp to send the message to
69  * @param payload Payload data.  First field of payload is always
70  *   struct kuc_hdr
71  */
72 int libcfs_kkuc_msg_put(struct file *filp, void *payload)
73 {
74         struct kuc_hdr *kuch = (struct kuc_hdr *)payload;
75         int rc = -ENOSYS;
76         loff_t offset = 0;
77
78         if (filp == NULL || IS_ERR(filp))
79                 return -EBADF;
80
81         if (kuch->kuc_magic != KUC_MAGIC) {
82                 CERROR("KernelComm: bad magic %x\n", kuch->kuc_magic);
83                 return -ENOSYS;
84         }
85
86         rc = filp_user_write(filp, payload, kuch->kuc_msglen, &offset);
87         if (rc < 0)
88                 CWARN("message send failed (%d)\n", rc);
89         else
90                 CDEBUG(D_HSM, "Sent message rc=%d, fp=%p\n", rc, filp);
91
92         return rc;
93 }
94 EXPORT_SYMBOL(libcfs_kkuc_msg_put);
95
96 /* Broadcast groups are global across all mounted filesystems;
97  * i.e. registering for a group on 1 fs will get messages for that
98  * group from any fs */
99 /** A single group registration has a uid and a file pointer */
100 struct kkuc_reg {
101         struct list_head kr_chain;
102         struct obd_uuid  kr_uuid;
103         int              kr_uid;
104         struct file     *kr_fp;
105         char             kr_data[0];
106 };
107
108 static struct list_head kkuc_groups[KUC_GRP_MAX + 1];
109 /* Protect message sending against remove and adds */
110 static DECLARE_RWSEM(kg_sem);
111
112 static inline bool libcfs_kkuc_group_is_valid(int group)
113 {
114         return 0 <= group && group < ARRAY_SIZE(kkuc_groups);
115 }
116
117 void libcfs_kkuc_init(void)
118 {
119         int group;
120
121         for (group = 0; group < ARRAY_SIZE(kkuc_groups); group++)
122                 INIT_LIST_HEAD(&kkuc_groups[group]);
123 }
124
125 /** Add a receiver to a broadcast group
126  * @param filp pipe to write into
127  * @param uid identifier for this receiver
128  * @param group group number
129  * @param data user data
130  */
131 int libcfs_kkuc_group_add(struct file *filp, const struct obd_uuid *uuid,
132                           int uid, int group, void *data, size_t data_len)
133 {
134         struct kkuc_reg *reg;
135
136         if (!libcfs_kkuc_group_is_valid(group)) {
137                 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
138                 return -EINVAL;
139         }
140
141         /* fput in group_rem */
142         if (filp == NULL)
143                 return -EBADF;
144
145         /* freed in group_rem */
146         reg = kzalloc(sizeof(*reg) + data_len, 0);
147         if (reg == NULL)
148                 return -ENOMEM;
149
150         reg->kr_uuid = *uuid;
151         reg->kr_fp = filp;
152         reg->kr_uid = uid;
153         memcpy(reg->kr_data, data, data_len);
154
155         down_write(&kg_sem);
156         list_add(&reg->kr_chain, &kkuc_groups[group]);
157         up_write(&kg_sem);
158
159         CDEBUG(D_HSM, "Added uid=%d fp=%p to group %d\n", uid, filp, group);
160
161         return 0;
162 }
163 EXPORT_SYMBOL(libcfs_kkuc_group_add);
164
165 int libcfs_kkuc_group_rem(const struct obd_uuid *uuid, int uid, int group)
166 {
167         struct kkuc_reg *reg, *next;
168         ENTRY;
169
170         if (!libcfs_kkuc_group_is_valid(group)) {
171                 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
172                 return -EINVAL;
173         }
174
175         if (uid == 0) {
176                 /* Broadcast a shutdown message */
177                 struct kuc_hdr lh;
178
179                 lh.kuc_magic = KUC_MAGIC;
180                 lh.kuc_transport = KUC_TRANSPORT_GENERIC;
181                 lh.kuc_msgtype = KUC_MSG_SHUTDOWN;
182                 lh.kuc_msglen = sizeof(lh);
183                 libcfs_kkuc_group_put(uuid, group, &lh);
184         }
185
186         down_write(&kg_sem);
187         list_for_each_entry_safe(reg, next, &kkuc_groups[group], kr_chain) {
188                 if (obd_uuid_equals(uuid, &reg->kr_uuid) &&
189                     (uid == 0 || uid == reg->kr_uid)) {
190                         list_del(&reg->kr_chain);
191                         CDEBUG(D_HSM, "Removed uid=%d fp=%p from group %d\n",
192                                 reg->kr_uid, reg->kr_fp, group);
193                         if (reg->kr_fp != NULL)
194                                 fput(reg->kr_fp);
195                         kfree(reg);
196                 }
197         }
198         up_write(&kg_sem);
199
200         RETURN(0);
201 }
202 EXPORT_SYMBOL(libcfs_kkuc_group_rem);
203
204 int libcfs_kkuc_group_put(const struct obd_uuid *uuid, int group, void *payload)
205 {
206         struct kkuc_reg *reg;
207         int              rc = 0;
208         int one_success = 0;
209         ENTRY;
210
211         if (!libcfs_kkuc_group_is_valid(group)) {
212                 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
213                 return -EINVAL;
214         }
215
216         down_write(&kg_sem);
217
218         if (unlikely(list_empty(&kkuc_groups[group])) ||
219             unlikely(OBD_FAIL_CHECK(OBD_FAIL_MDS_HSM_CT_REGISTER_NET))) {
220                 /* no agent have fully registered, CDT will retry */
221                 up_write(&kg_sem);
222                 RETURN(-EAGAIN);
223         }
224
225         list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
226                 if (obd_uuid_equals(uuid, &reg->kr_uuid) &&
227                     reg->kr_fp != NULL) {
228                         rc = libcfs_kkuc_msg_put(reg->kr_fp, payload);
229                         if (rc == 0)
230                                 one_success = 1;
231                         else if (rc == -EPIPE) {
232                                 fput(reg->kr_fp);
233                                 reg->kr_fp = NULL;
234                         }
235                 }
236         }
237         up_write(&kg_sem);
238
239         /* don't return an error if the message has been delivered
240          * at least to one agent */
241         if (one_success)
242                 rc = 0;
243
244         RETURN(rc);
245 }
246 EXPORT_SYMBOL(libcfs_kkuc_group_put);
247
248 /**
249  * Calls a callback function for each link of the given kuc group.
250  * @param group the group to call the function on.
251  * @param cb_func the function to be called.
252  * @param cb_arg extra argument to be passed to the callback function.
253  */
254 int libcfs_kkuc_group_foreach(const struct obd_uuid *uuid, int group,
255                               libcfs_kkuc_cb_t cb_func, void *cb_arg)
256 {
257         struct kkuc_reg *reg;
258         int              rc = 0;
259         ENTRY;
260
261         if (!libcfs_kkuc_group_is_valid(group)) {
262                 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
263                 RETURN(-EINVAL);
264         }
265
266         down_read(&kg_sem);
267         list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
268                 if (obd_uuid_equals(uuid, &reg->kr_uuid) && reg->kr_fp != NULL)
269                         rc = cb_func(reg->kr_data, cb_arg);
270         }
271         up_read(&kg_sem);
272
273         RETURN(rc);
274 }
275 EXPORT_SYMBOL(libcfs_kkuc_group_foreach);