]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/lustre/lustre/libcfs/kernel_user_comm.c
ipc/msg.c: use freezable blocking call
[karo-tx-linux.git] / drivers / staging / lustre / lustre / libcfs / kernel_user_comm.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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * Author: Nathan Rutman <nathan.rutman@sun.com>
37  *
38  * Kernel <-> userspace communication routines.
39  * Using pipes for all arches.
40  */
41
42 #define DEBUG_SUBSYSTEM S_CLASS
43 #define D_KUC D_OTHER
44
45 #include "../../include/linux/libcfs/libcfs.h"
46
47 /* This is the kernel side (liblustre as well). */
48
49 /**
50  * libcfs_kkuc_msg_put - send an message from kernel to userspace
51  * @param fp to send the message to
52  * @param payload Payload data.  First field of payload is always
53  *   struct kuc_hdr
54  */
55 int libcfs_kkuc_msg_put(struct file *filp, void *payload)
56 {
57         struct kuc_hdr *kuch = (struct kuc_hdr *)payload;
58         ssize_t count = kuch->kuc_msglen;
59         loff_t offset = 0;
60         mm_segment_t fs;
61         int rc = -ENOSYS;
62
63         if (filp == NULL || IS_ERR(filp))
64                 return -EBADF;
65
66         if (kuch->kuc_magic != KUC_MAGIC) {
67                 CERROR("KernelComm: bad magic %x\n", kuch->kuc_magic);
68                 return -ENOSYS;
69         }
70
71         fs = get_fs();
72         set_fs(KERNEL_DS);
73         while (count > 0) {
74                 rc = vfs_write(filp, (void __force __user *)payload,
75                                count, &offset);
76                 if (rc < 0)
77                         break;
78                 count -= rc;
79                 payload += rc;
80                 rc = 0;
81         }
82         set_fs(fs);
83
84         if (rc < 0)
85                 CWARN("message send failed (%d)\n", rc);
86         else
87                 CDEBUG(D_KUC, "Sent message rc=%d, fp=%p\n", rc, filp);
88
89         return rc;
90 }
91 EXPORT_SYMBOL(libcfs_kkuc_msg_put);
92
93 /* Broadcast groups are global across all mounted filesystems;
94  * i.e. registering for a group on 1 fs will get messages for that
95  * group from any fs */
96 /** A single group registration has a uid and a file pointer */
97 struct kkuc_reg {
98         struct list_head        kr_chain;
99         int             kr_uid;
100         struct file     *kr_fp;
101         __u32           kr_data;
102 };
103
104 static struct list_head kkuc_groups[KUC_GRP_MAX+1] = {};
105 /* Protect message sending against remove and adds */
106 static DECLARE_RWSEM(kg_sem);
107
108 /** Add a receiver to a broadcast group
109  * @param filp pipe to write into
110  * @param uid identifier for this receiver
111  * @param group group number
112  */
113 int libcfs_kkuc_group_add(struct file *filp, int uid, unsigned int group,
114                           __u32 data)
115 {
116         struct kkuc_reg *reg;
117
118         if (group > KUC_GRP_MAX) {
119                 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
120                 return -EINVAL;
121         }
122
123         /* fput in group_rem */
124         if (filp == NULL)
125                 return -EBADF;
126
127         /* freed in group_rem */
128         reg = kmalloc(sizeof(*reg), 0);
129         if (reg == NULL)
130                 return -ENOMEM;
131
132         reg->kr_fp = filp;
133         reg->kr_uid = uid;
134         reg->kr_data = data;
135
136         down_write(&kg_sem);
137         if (kkuc_groups[group].next == NULL)
138                 INIT_LIST_HEAD(&kkuc_groups[group]);
139         list_add(&reg->kr_chain, &kkuc_groups[group]);
140         up_write(&kg_sem);
141
142         CDEBUG(D_KUC, "Added uid=%d fp=%p to group %d\n", uid, filp, group);
143
144         return 0;
145 }
146 EXPORT_SYMBOL(libcfs_kkuc_group_add);
147
148 int libcfs_kkuc_group_rem(int uid, int group)
149 {
150         struct kkuc_reg *reg, *next;
151
152         if (kkuc_groups[group].next == NULL)
153                 return 0;
154
155         if (uid == 0) {
156                 /* Broadcast a shutdown message */
157                 struct kuc_hdr lh;
158
159                 lh.kuc_magic = KUC_MAGIC;
160                 lh.kuc_transport = KUC_TRANSPORT_GENERIC;
161                 lh.kuc_msgtype = KUC_MSG_SHUTDOWN;
162                 lh.kuc_msglen = sizeof(lh);
163                 libcfs_kkuc_group_put(group, &lh);
164         }
165
166         down_write(&kg_sem);
167         list_for_each_entry_safe(reg, next, &kkuc_groups[group], kr_chain) {
168                 if ((uid == 0) || (uid == reg->kr_uid)) {
169                         list_del(&reg->kr_chain);
170                         CDEBUG(D_KUC, "Removed uid=%d fp=%p from group %d\n",
171                                reg->kr_uid, reg->kr_fp, group);
172                         if (reg->kr_fp != NULL)
173                                 fput(reg->kr_fp);
174                         kfree(reg);
175                 }
176         }
177         up_write(&kg_sem);
178
179         return 0;
180 }
181 EXPORT_SYMBOL(libcfs_kkuc_group_rem);
182
183 int libcfs_kkuc_group_put(int group, void *payload)
184 {
185         struct kkuc_reg *reg;
186         int              rc = 0;
187         int one_success = 0;
188
189         down_read(&kg_sem);
190         list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
191                 if (reg->kr_fp != NULL) {
192                         rc = libcfs_kkuc_msg_put(reg->kr_fp, payload);
193                         if (rc == 0)
194                                 one_success = 1;
195                         else if (rc == -EPIPE) {
196                                 fput(reg->kr_fp);
197                                 reg->kr_fp = NULL;
198                         }
199                 }
200         }
201         up_read(&kg_sem);
202
203         /* don't return an error if the message has been delivered
204          * at least to one agent */
205         if (one_success)
206                 rc = 0;
207
208         return rc;
209 }
210 EXPORT_SYMBOL(libcfs_kkuc_group_put);
211
212 /**
213  * Calls a callback function for each link of the given kuc group.
214  * @param group the group to call the function on.
215  * @param cb_func the function to be called.
216  * @param cb_arg iextra argument to be passed to the callback function.
217  */
218 int libcfs_kkuc_group_foreach(int group, libcfs_kkuc_cb_t cb_func,
219                               void *cb_arg)
220 {
221         struct kkuc_reg *reg;
222         int rc = 0;
223
224         if (group > KUC_GRP_MAX) {
225                 CDEBUG(D_WARNING, "Kernelcomm: bad group %d\n", group);
226                 return -EINVAL;
227         }
228
229         /* no link for this group */
230         if (kkuc_groups[group].next == NULL)
231                 return 0;
232
233         down_write(&kg_sem);
234         list_for_each_entry(reg, &kkuc_groups[group], kr_chain) {
235                 if (reg->kr_fp != NULL)
236                         rc = cb_func(reg->kr_data, cb_arg);
237         }
238         up_write(&kg_sem);
239
240         return rc;
241 }
242 EXPORT_SYMBOL(libcfs_kkuc_group_foreach);