Whamcloud - gitweb
LU-13004 ptlrpc: Allow BULK_BUF_KIOV to accept a kvec
[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 <linux/file.h>
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 = 0;
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 -ENOSYS;
63         }
64
65         while (count > 0) {
66                 rc = cfs_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_HSM, "Sent message rc=%d, fp=%p\n", rc, filp);
78
79         return rc;
80 }
81 EXPORT_SYMBOL(libcfs_kkuc_msg_put);
82
83 /* Broadcast groups are global across all mounted filesystems;
84  * i.e. registering for a group on 1 fs will get messages for that
85  * group from any fs */
86 /** A single group registration has a uid and a file pointer */
87 struct kkuc_reg {
88         struct list_head kr_chain;
89         struct obd_uuid  kr_uuid;
90         int              kr_uid;
91         struct file     *kr_fp;
92         char             kr_data[0];
93 };
94
95 static struct list_head kkuc_groups[KUC_GRP_MAX + 1];
96 /* Protect message sending against remove and adds */
97 static DECLARE_RWSEM(kg_sem);
98
99 static inline bool libcfs_kkuc_group_is_valid(int group)
100 {
101         return 0 <= group && group < ARRAY_SIZE(kkuc_groups);
102 }
103
104 void libcfs_kkuc_init(void)
105 {
106         int group;
107
108         for (group = 0; group < ARRAY_SIZE(kkuc_groups); group++)
109                 INIT_LIST_HEAD(&kkuc_groups[group]);
110 }
111
112 /** Add a receiver to a broadcast group
113  * @param filp pipe to write into
114  * @param uid identifier for this receiver
115  * @param group group number
116  * @param data user data
117  */
118 int libcfs_kkuc_group_add(struct file *filp, const struct obd_uuid *uuid,
119                           int uid, int group, void *data, size_t data_len)
120 {
121         struct kkuc_reg *reg;
122
123         if (!libcfs_kkuc_group_is_valid(group)) {
124                 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
125                 return -EINVAL;
126         }
127
128         /* fput in group_rem */
129         if (filp == NULL)
130                 return -EBADF;
131
132         /* freed in group_rem */
133         reg = kzalloc(sizeof(*reg) + data_len, 0);
134         if (reg == NULL)
135                 return -ENOMEM;
136
137         reg->kr_uuid = *uuid;
138         reg->kr_fp = filp;
139         reg->kr_uid = uid;
140         memcpy(reg->kr_data, data, data_len);
141
142         down_write(&kg_sem);
143         list_add(&reg->kr_chain, &kkuc_groups[group]);
144         up_write(&kg_sem);
145
146         CDEBUG(D_HSM, "Added uid=%d fp=%p to group %d\n", uid, filp, group);
147
148         return 0;
149 }
150 EXPORT_SYMBOL(libcfs_kkuc_group_add);
151
152 int libcfs_kkuc_group_rem(const struct obd_uuid *uuid, int uid, int group)
153 {
154         struct kkuc_reg *reg, *next;
155         ENTRY;
156
157         if (!libcfs_kkuc_group_is_valid(group)) {
158                 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
159                 return -EINVAL;
160         }
161
162         if (uid == 0) {
163                 /* Broadcast a shutdown message */
164                 struct kuc_hdr lh;
165
166                 lh.kuc_magic = KUC_MAGIC;
167                 lh.kuc_transport = KUC_TRANSPORT_GENERIC;
168                 lh.kuc_msgtype = KUC_MSG_SHUTDOWN;
169                 lh.kuc_msglen = sizeof(lh);
170                 libcfs_kkuc_group_put(uuid, group, &lh);
171         }
172
173         down_write(&kg_sem);
174         list_for_each_entry_safe(reg, next, &kkuc_groups[group], kr_chain) {
175                 if (obd_uuid_equals(uuid, &reg->kr_uuid) &&
176                     (uid == 0 || uid == reg->kr_uid)) {
177                         list_del(&reg->kr_chain);
178                         CDEBUG(D_HSM, "Removed uid=%d fp=%p from group %d\n",
179                                 reg->kr_uid, reg->kr_fp, group);
180                         if (reg->kr_fp != NULL)
181                                 fput(reg->kr_fp);
182                         kfree(reg);
183                 }
184         }
185         up_write(&kg_sem);
186
187         RETURN(0);
188 }
189 EXPORT_SYMBOL(libcfs_kkuc_group_rem);
190
191 int libcfs_kkuc_group_put(const struct obd_uuid *uuid, int group, void *payload)
192 {
193         struct kkuc_reg *reg;
194         int              rc = 0;
195         int one_success = 0;
196         ENTRY;
197
198         if (!libcfs_kkuc_group_is_valid(group)) {
199                 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
200                 return -EINVAL;
201         }
202
203         down_write(&kg_sem);
204
205         if (unlikely(list_empty(&kkuc_groups[group])) ||
206             unlikely(OBD_FAIL_CHECK(OBD_FAIL_MDS_HSM_CT_REGISTER_NET))) {
207                 /* no agent have fully registered, CDT will retry */
208                 up_write(&kg_sem);
209                 RETURN(-EAGAIN);
210         }
211
212         list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
213                 if (obd_uuid_equals(uuid, &reg->kr_uuid) &&
214                     reg->kr_fp != NULL) {
215                         rc = libcfs_kkuc_msg_put(reg->kr_fp, payload);
216                         if (rc == 0)
217                                 one_success = 1;
218                         else if (rc == -EPIPE) {
219                                 fput(reg->kr_fp);
220                                 reg->kr_fp = NULL;
221                         }
222                 }
223         }
224         up_write(&kg_sem);
225
226         /* don't return an error if the message has been delivered
227          * at least to one agent */
228         if (one_success)
229                 rc = 0;
230
231         RETURN(rc);
232 }
233 EXPORT_SYMBOL(libcfs_kkuc_group_put);
234
235 /**
236  * Calls a callback function for each link of the given kuc group.
237  * @param group the group to call the function on.
238  * @param cb_func the function to be called.
239  * @param cb_arg extra argument to be passed to the callback function.
240  */
241 int libcfs_kkuc_group_foreach(const struct obd_uuid *uuid, int group,
242                               libcfs_kkuc_cb_t cb_func, void *cb_arg)
243 {
244         struct kkuc_reg *reg;
245         int              rc = 0;
246         ENTRY;
247
248         if (!libcfs_kkuc_group_is_valid(group)) {
249                 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
250                 RETURN(-EINVAL);
251         }
252
253         down_read(&kg_sem);
254         list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
255                 if (obd_uuid_equals(uuid, &reg->kr_uuid) && reg->kr_fp != NULL)
256                         rc = cb_func(reg->kr_data, cb_arg);
257         }
258         up_read(&kg_sem);
259
260         RETURN(rc);
261 }
262 EXPORT_SYMBOL(libcfs_kkuc_group_foreach);