Whamcloud - gitweb
LU-6158 mdt: always shrink_capsule in getxattr_all
[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) 2012, 2013, 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 #define D_KUC D_OTHER
39
40 #include <obd_support.h>
41 #include <lustre_kernelcomm.h>
42
43 /* write a userspace buffer to disk.
44  * NOTE: this returns 0 on success, not the number of bytes written. */
45 static ssize_t
46 filp_user_write(struct file *filp, const void *buf, size_t count,
47                 loff_t *offset)
48 {
49         mm_segment_t fs;
50         ssize_t size = 0;
51
52         fs = get_fs();
53         set_fs(KERNEL_DS);
54         while ((ssize_t)count > 0) {
55                 size = vfs_write(filp, (const void __user *)buf, count, offset);
56                 if (size < 0)
57                         break;
58                 count -= size;
59                 buf += size;
60                 size = 0;
61         }
62         set_fs(fs);
63
64         return size;
65 }
66
67 /**
68  * libcfs_kkuc_msg_put - send an message from kernel to userspace
69  * @param fp to send the message to
70  * @param payload Payload data.  First field of payload is always
71  *   struct kuc_hdr
72  */
73 int libcfs_kkuc_msg_put(struct file *filp, void *payload)
74 {
75         struct kuc_hdr *kuch = (struct kuc_hdr *)payload;
76         int rc = -ENOSYS;
77         loff_t offset = 0;
78
79         if (filp == NULL || IS_ERR(filp))
80                 return -EBADF;
81
82         if (kuch->kuc_magic != KUC_MAGIC) {
83                 CERROR("KernelComm: bad magic %x\n", kuch->kuc_magic);
84                 return -ENOSYS;
85         }
86
87         rc = filp_user_write(filp, payload, kuch->kuc_msglen, &offset);
88         if (rc < 0)
89                 CWARN("message send failed (%d)\n", rc);
90         else
91                 CDEBUG(D_KUC, "Sent message rc=%d, fp=%p\n", rc, filp);
92
93         return rc;
94 }
95 EXPORT_SYMBOL(libcfs_kkuc_msg_put);
96
97 /* Broadcast groups are global across all mounted filesystems;
98  * i.e. registering for a group on 1 fs will get messages for that
99  * group from any fs */
100 /** A single group registration has a uid and a file pointer */
101 struct kkuc_reg {
102         struct list_head kr_chain;
103         int              kr_uid;
104         struct file     *kr_fp;
105         void            *kr_data;
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 /** 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, int uid, int group, void *data)
119 {
120         struct kkuc_reg *reg;
121
122         if (group > KUC_GRP_MAX) {
123                 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
124                 return -EINVAL;
125         }
126
127         /* fput in group_rem */
128         if (filp == NULL)
129                 return -EBADF;
130
131         /* freed in group_rem */
132         reg = kmalloc(sizeof(*reg), 0);
133         if (reg == NULL)
134                 return -ENOMEM;
135
136         reg->kr_fp = filp;
137         reg->kr_uid = uid;
138         reg->kr_data = data;
139
140         down_write(&kg_sem);
141         if (kkuc_groups[group].next == NULL)
142                 INIT_LIST_HEAD(&kkuc_groups[group]);
143         list_add(&reg->kr_chain, &kkuc_groups[group]);
144         up_write(&kg_sem);
145
146         CDEBUG(D_KUC, "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(int uid, int group, void **pdata)
153 {
154         struct kkuc_reg *reg, *next;
155         ENTRY;
156
157         if (kkuc_groups[group].next == NULL)
158                 RETURN(0);
159
160         if (uid == 0) {
161                 /* Broadcast a shutdown message */
162                 struct kuc_hdr lh;
163
164                 lh.kuc_magic = KUC_MAGIC;
165                 lh.kuc_transport = KUC_TRANSPORT_GENERIC;
166                 lh.kuc_msgtype = KUC_MSG_SHUTDOWN;
167                 lh.kuc_msglen = sizeof(lh);
168                 libcfs_kkuc_group_put(group, &lh);
169         }
170
171         down_write(&kg_sem);
172         list_for_each_entry_safe(reg, next, &kkuc_groups[group], kr_chain) {
173                 if ((uid == 0) || (uid == reg->kr_uid)) {
174                         list_del(&reg->kr_chain);
175                         CDEBUG(D_KUC, "Removed uid=%d fp=%p from group %d\n",
176                                 reg->kr_uid, reg->kr_fp, group);
177                         if (reg->kr_fp != NULL)
178                                 fput(reg->kr_fp);
179                         if (pdata != NULL)
180                                 *pdata = reg->kr_data;
181                         kfree(reg);
182                 }
183         }
184         up_write(&kg_sem);
185
186         RETURN(0);
187 }
188 EXPORT_SYMBOL(libcfs_kkuc_group_rem);
189
190 int libcfs_kkuc_group_put(int group, void *payload)
191 {
192         struct kkuc_reg *reg;
193         int              rc = 0;
194         int one_success = 0;
195         ENTRY;
196
197         down_write(&kg_sem);
198         list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
199                 if (reg->kr_fp != NULL) {
200                         rc = libcfs_kkuc_msg_put(reg->kr_fp, payload);
201                         if (rc == 0)
202                                 one_success = 1;
203                         else if (rc == -EPIPE) {
204                                 fput(reg->kr_fp);
205                                 reg->kr_fp = NULL;
206                         }
207                 }
208         }
209         up_write(&kg_sem);
210
211         /* don't return an error if the message has been delivered
212          * at least to one agent */
213         if (one_success)
214                 rc = 0;
215
216         RETURN(rc);
217 }
218 EXPORT_SYMBOL(libcfs_kkuc_group_put);
219
220 /**
221  * Calls a callback function for each link of the given kuc group.
222  * @param group the group to call the function on.
223  * @param cb_func the function to be called.
224  * @param cb_arg extra argument to be passed to the callback function.
225  */
226 int libcfs_kkuc_group_foreach(int group, libcfs_kkuc_cb_t cb_func,
227                               void *cb_arg)
228 {
229         struct kkuc_reg *reg;
230         int              rc = 0;
231         ENTRY;
232
233         if (group > KUC_GRP_MAX) {
234                 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
235                 RETURN(-EINVAL);
236         }
237
238         /* no link for this group */
239         if (kkuc_groups[group].next == NULL)
240                 RETURN(0);
241
242         down_read(&kg_sem);
243         list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
244                 if (reg->kr_fp != NULL)
245                         rc = cb_func(reg->kr_data, cb_arg);
246         }
247         up_read(&kg_sem);
248
249         RETURN(rc);
250 }
251 EXPORT_SYMBOL(libcfs_kkuc_group_foreach);