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