eCryptfs: integrate eCryptfs device handle into the module.
[deliverable/linux.git] / fs / ecryptfs / messaging.c
CommitLineData
88b4a07e
MH
1/**
2 * eCryptfs: Linux filesystem encryption layer
3 *
f66e883e 4 * Copyright (C) 2004-2008 International Business Machines Corp.
88b4a07e
MH
5 * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
6 * Tyler Hicks <tyhicks@ou.edu>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
e8edc6e0 22#include <linux/sched.h>
88b4a07e
MH
23#include "ecryptfs_kernel.h"
24
dd2a3b7a
MH
25static LIST_HEAD(ecryptfs_msg_ctx_free_list);
26static LIST_HEAD(ecryptfs_msg_ctx_alloc_list);
27static struct mutex ecryptfs_msg_ctx_lists_mux;
88b4a07e 28
f66e883e
MH
29static struct hlist_head *ecryptfs_daemon_hash;
30struct mutex ecryptfs_daemon_hash_mux;
dd2a3b7a
MH
31static int ecryptfs_hash_buckets;
32#define ecryptfs_uid_hash(uid) \
33 hash_long((unsigned long)uid, ecryptfs_hash_buckets)
88b4a07e 34
f66e883e 35static u32 ecryptfs_msg_counter;
dd2a3b7a 36static struct ecryptfs_msg_ctx *ecryptfs_msg_ctx_arr;
88b4a07e
MH
37
38/**
39 * ecryptfs_acquire_free_msg_ctx
40 * @msg_ctx: The context that was acquired from the free list
41 *
42 * Acquires a context element from the free list and locks the mutex
f66e883e
MH
43 * on the context. Sets the msg_ctx task to current. Returns zero on
44 * success; non-zero on error or upon failure to acquire a free
45 * context element. Must be called with ecryptfs_msg_ctx_lists_mux
46 * held.
88b4a07e
MH
47 */
48static int ecryptfs_acquire_free_msg_ctx(struct ecryptfs_msg_ctx **msg_ctx)
49{
50 struct list_head *p;
51 int rc;
52
53 if (list_empty(&ecryptfs_msg_ctx_free_list)) {
f66e883e
MH
54 printk(KERN_WARNING "%s: The eCryptfs free "
55 "context list is empty. It may be helpful to "
56 "specify the ecryptfs_message_buf_len "
57 "parameter to be greater than the current "
58 "value of [%d]\n", __func__, ecryptfs_message_buf_len);
88b4a07e
MH
59 rc = -ENOMEM;
60 goto out;
61 }
62 list_for_each(p, &ecryptfs_msg_ctx_free_list) {
63 *msg_ctx = list_entry(p, struct ecryptfs_msg_ctx, node);
64 if (mutex_trylock(&(*msg_ctx)->mux)) {
65 (*msg_ctx)->task = current;
66 rc = 0;
67 goto out;
68 }
69 }
70 rc = -ENOMEM;
71out:
72 return rc;
73}
74
75/**
76 * ecryptfs_msg_ctx_free_to_alloc
77 * @msg_ctx: The context to move from the free list to the alloc list
78 *
f66e883e 79 * Must be called with ecryptfs_msg_ctx_lists_mux held.
88b4a07e
MH
80 */
81static void ecryptfs_msg_ctx_free_to_alloc(struct ecryptfs_msg_ctx *msg_ctx)
82{
83 list_move(&msg_ctx->node, &ecryptfs_msg_ctx_alloc_list);
84 msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_PENDING;
85 msg_ctx->counter = ++ecryptfs_msg_counter;
86}
87
88/**
89 * ecryptfs_msg_ctx_alloc_to_free
90 * @msg_ctx: The context to move from the alloc list to the free list
91 *
f66e883e 92 * Must be called with ecryptfs_msg_ctx_lists_mux held.
88b4a07e 93 */
f66e883e 94void ecryptfs_msg_ctx_alloc_to_free(struct ecryptfs_msg_ctx *msg_ctx)
88b4a07e
MH
95{
96 list_move(&(msg_ctx->node), &ecryptfs_msg_ctx_free_list);
97 if (msg_ctx->msg)
98 kfree(msg_ctx->msg);
f66e883e 99 msg_ctx->msg = NULL;
88b4a07e
MH
100 msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_FREE;
101}
102
103/**
f66e883e
MH
104 * ecryptfs_find_daemon_by_euid
105 * @euid: The effective user id which maps to the desired daemon id
106 * @daemon: If return value is zero, points to the desired daemon pointer
88b4a07e 107 *
f66e883e
MH
108 * Must be called with ecryptfs_daemon_hash_mux held.
109 *
110 * Search the hash list for the given user id.
111 *
112 * Returns zero if the user id exists in the list; non-zero otherwise.
88b4a07e 113 */
f66e883e 114int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon, uid_t euid)
88b4a07e
MH
115{
116 struct hlist_node *elem;
117 int rc;
118
f66e883e
MH
119 hlist_for_each_entry(*daemon, elem,
120 &ecryptfs_daemon_hash[ecryptfs_uid_hash(euid)],
121 euid_chain) {
122 if ((*daemon)->euid == euid) {
88b4a07e
MH
123 rc = 0;
124 goto out;
125 }
126 }
127 rc = -EINVAL;
128out:
129 return rc;
130}
131
f66e883e
MH
132static int
133ecryptfs_send_message_locked(unsigned int transport, char *data, int data_len,
134 u8 msg_type, struct ecryptfs_msg_ctx **msg_ctx);
135
136/**
137 * ecryptfs_send_raw_message
138 * @transport: Transport type
139 * @msg_type: Message type
140 * @daemon: Daemon struct for recipient of message
141 *
142 * A raw message is one that does not include an ecryptfs_message
143 * struct. It simply has a type.
144 *
145 * Must be called with ecryptfs_daemon_hash_mux held.
146 *
147 * Returns zero on success; non-zero otherwise
148 */
149static int ecryptfs_send_raw_message(unsigned int transport, u8 msg_type,
150 struct ecryptfs_daemon *daemon)
88b4a07e 151{
f66e883e 152 struct ecryptfs_msg_ctx *msg_ctx;
88b4a07e
MH
153 int rc;
154
155 switch(transport) {
156 case ECRYPTFS_TRANSPORT_NETLINK:
f66e883e
MH
157 rc = ecryptfs_send_netlink(NULL, 0, NULL, msg_type, 0,
158 daemon->pid);
159 break;
160 case ECRYPTFS_TRANSPORT_MISCDEV:
161 rc = ecryptfs_send_message_locked(transport, NULL, 0, msg_type,
162 &msg_ctx);
163 if (rc) {
164 printk(KERN_ERR "%s: Error whilst attempting to send "
165 "message via procfs; rc = [%d]\n", __func__, rc);
166 goto out;
167 }
168 /* Raw messages are logically context-free (e.g., no
169 * reply is expected), so we set the state of the
170 * ecryptfs_msg_ctx object to indicate that it should
171 * be freed as soon as the transport sends out the message. */
172 mutex_lock(&msg_ctx->mux);
173 msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_NO_REPLY;
174 mutex_unlock(&msg_ctx->mux);
88b4a07e
MH
175 break;
176 case ECRYPTFS_TRANSPORT_CONNECTOR:
177 case ECRYPTFS_TRANSPORT_RELAYFS:
178 default:
179 rc = -ENOSYS;
180 }
f66e883e
MH
181out:
182 return rc;
183}
184
185/**
186 * ecryptfs_spawn_daemon - Create and initialize a new daemon struct
187 * @daemon: Pointer to set to newly allocated daemon struct
188 * @euid: Effective user id for the daemon
189 * @pid: Process id for the daemon
190 *
191 * Must be called ceremoniously while in possession of
192 * ecryptfs_sacred_daemon_hash_mux
193 *
194 * Returns zero on success; non-zero otherwise
195 */
196int
197ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, uid_t euid, pid_t pid)
198{
199 int rc = 0;
200
201 (*daemon) = kzalloc(sizeof(**daemon), GFP_KERNEL);
202 if (!(*daemon)) {
203 rc = -ENOMEM;
204 printk(KERN_ERR "%s: Failed to allocate [%Zd] bytes of "
205 "GFP_KERNEL memory\n", __func__, sizeof(**daemon));
206 goto out;
207 }
208 (*daemon)->euid = euid;
209 (*daemon)->pid = pid;
210 (*daemon)->task = current;
211 mutex_init(&(*daemon)->mux);
212 INIT_LIST_HEAD(&(*daemon)->msg_ctx_out_queue);
213 init_waitqueue_head(&(*daemon)->wait);
214 (*daemon)->num_queued_msg_ctx = 0;
215 hlist_add_head(&(*daemon)->euid_chain,
216 &ecryptfs_daemon_hash[ecryptfs_uid_hash(euid)]);
217out:
88b4a07e
MH
218 return rc;
219}
220
221/**
222 * ecryptfs_process_helo
223 * @transport: The underlying transport (netlink, etc.)
f66e883e 224 * @euid: The user ID owner of the message
88b4a07e
MH
225 * @pid: The process ID for the userspace program that sent the
226 * message
227 *
f66e883e 228 * Adds the euid and pid values to the daemon euid hash. If an euid
88b4a07e 229 * already has a daemon pid registered, the daemon will be
f66e883e
MH
230 * unregistered before the new daemon is put into the hash list.
231 * Returns zero after adding a new daemon to the hash list;
88b4a07e
MH
232 * non-zero otherwise.
233 */
f66e883e 234int ecryptfs_process_helo(unsigned int transport, uid_t euid, pid_t pid)
88b4a07e 235{
f66e883e
MH
236 struct ecryptfs_daemon *new_daemon;
237 struct ecryptfs_daemon *old_daemon;
88b4a07e
MH
238 int rc;
239
f66e883e
MH
240 mutex_lock(&ecryptfs_daemon_hash_mux);
241 rc = ecryptfs_find_daemon_by_euid(&old_daemon, euid);
242 if (rc != 0) {
88b4a07e
MH
243 printk(KERN_WARNING "Received request from user [%d] "
244 "to register daemon [%d]; unregistering daemon "
f66e883e
MH
245 "[%d]\n", euid, pid, old_daemon->pid);
246 rc = ecryptfs_send_raw_message(transport, ECRYPTFS_MSG_QUIT,
247 old_daemon);
88b4a07e
MH
248 if (rc)
249 printk(KERN_WARNING "Failed to send QUIT "
250 "message to daemon [%d]; rc = [%d]\n",
f66e883e
MH
251 old_daemon->pid, rc);
252 hlist_del(&old_daemon->euid_chain);
253 kfree(old_daemon);
88b4a07e 254 }
f66e883e
MH
255 rc = ecryptfs_spawn_daemon(&new_daemon, euid, pid);
256 if (rc)
257 printk(KERN_ERR "%s: The gods are displeased with this attempt "
258 "to create a new daemon object for euid [%d]; pid [%d]; "
259 "rc = [%d]\n", __func__, euid, pid, rc);
260 mutex_unlock(&ecryptfs_daemon_hash_mux);
261 return rc;
262}
263
264/**
265 * ecryptfs_exorcise_daemon - Destroy the daemon struct
266 *
267 * Must be called ceremoniously while in possession of
268 * ecryptfs_daemon_hash_mux and the daemon's own mux.
269 */
270int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon)
271{
272 struct ecryptfs_msg_ctx *msg_ctx, *msg_ctx_tmp;
273 int rc = 0;
274
275 mutex_lock(&daemon->mux);
276 if ((daemon->flags & ECRYPTFS_DAEMON_IN_READ)
277 || (daemon->flags & ECRYPTFS_DAEMON_IN_POLL)) {
278 rc = -EBUSY;
279 printk(KERN_WARNING "%s: Attempt to destroy daemon with pid "
280 "[%d], but it is in the midst of a read or a poll\n",
281 __func__, daemon->pid);
282 mutex_unlock(&daemon->mux);
283 goto out;
284 }
285 list_for_each_entry_safe(msg_ctx, msg_ctx_tmp,
286 &daemon->msg_ctx_out_queue, daemon_out_list) {
287 list_del(&msg_ctx->daemon_out_list);
288 daemon->num_queued_msg_ctx--;
289 printk(KERN_WARNING "%s: Warning: dropping message that is in "
290 "the out queue of a dying daemon\n", __func__);
291 ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
292 }
293 hlist_del(&daemon->euid_chain);
294 if (daemon->task)
295 wake_up_process(daemon->task);
296 mutex_unlock(&daemon->mux);
297 memset(daemon, 0, sizeof(*daemon));
298 kfree(daemon);
299out:
88b4a07e
MH
300 return rc;
301}
302
303/**
304 * ecryptfs_process_quit
f66e883e 305 * @euid: The user ID owner of the message
88b4a07e
MH
306 * @pid: The process ID for the userspace program that sent the
307 * message
308 *
f66e883e 309 * Deletes the corresponding daemon for the given euid and pid, if
88b4a07e 310 * it is the registered that is requesting the deletion. Returns zero
f66e883e 311 * after deleting the desired daemon; non-zero otherwise.
88b4a07e 312 */
f66e883e 313int ecryptfs_process_quit(uid_t euid, pid_t pid)
88b4a07e 314{
f66e883e 315 struct ecryptfs_daemon *daemon;
88b4a07e
MH
316 int rc;
317
f66e883e
MH
318 mutex_lock(&ecryptfs_daemon_hash_mux);
319 rc = ecryptfs_find_daemon_by_euid(&daemon, euid);
320 if (rc || !daemon) {
88b4a07e 321 rc = -EINVAL;
f66e883e
MH
322 printk(KERN_ERR "Received request from user [%d] to "
323 "unregister unrecognized daemon [%d]\n", euid, pid);
324 goto out_unlock;
88b4a07e 325 }
f66e883e
MH
326 rc = ecryptfs_exorcise_daemon(daemon);
327out_unlock:
328 mutex_unlock(&ecryptfs_daemon_hash_mux);
88b4a07e
MH
329 return rc;
330}
331
332/**
333 * ecryptfs_process_reponse
334 * @msg: The ecryptfs message received; the caller should sanity check
f66e883e 335 * msg->data_len and free the memory
88b4a07e
MH
336 * @pid: The process ID of the userspace application that sent the
337 * message
f66e883e
MH
338 * @seq: The sequence number of the message; must match the sequence
339 * number for the existing message context waiting for this
340 * response
341 *
342 * Processes a response message after sending an operation request to
343 * userspace. Some other process is awaiting this response. Before
344 * sending out its first communications, the other process allocated a
345 * msg_ctx from the ecryptfs_msg_ctx_arr at a particular index. The
346 * response message contains this index so that we can copy over the
347 * response message into the msg_ctx that the process holds a
348 * reference to. The other process is going to wake up, check to see
349 * that msg_ctx->state == ECRYPTFS_MSG_CTX_STATE_DONE, and then
350 * proceed to read off and process the response message. Returns zero
351 * upon delivery to desired context element; non-zero upon delivery
352 * failure or error.
88b4a07e 353 *
f66e883e 354 * Returns zero on success; non-zero otherwise
88b4a07e 355 */
f66e883e 356int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t euid,
dddfa461 357 pid_t pid, u32 seq)
88b4a07e 358{
f66e883e 359 struct ecryptfs_daemon *daemon;
88b4a07e 360 struct ecryptfs_msg_ctx *msg_ctx;
f66e883e 361 size_t msg_size;
88b4a07e
MH
362 int rc;
363
364 if (msg->index >= ecryptfs_message_buf_len) {
365 rc = -EINVAL;
f66e883e
MH
366 printk(KERN_ERR "%s: Attempt to reference "
367 "context buffer at index [%d]; maximum "
368 "allowable is [%d]\n", __func__, msg->index,
369 (ecryptfs_message_buf_len - 1));
88b4a07e
MH
370 goto out;
371 }
372 msg_ctx = &ecryptfs_msg_ctx_arr[msg->index];
373 mutex_lock(&msg_ctx->mux);
f66e883e
MH
374 mutex_lock(&ecryptfs_daemon_hash_mux);
375 rc = ecryptfs_find_daemon_by_euid(&daemon, msg_ctx->task->euid);
376 mutex_unlock(&ecryptfs_daemon_hash_mux);
377 if (rc) {
88b4a07e 378 rc = -EBADMSG;
f66e883e
MH
379 printk(KERN_WARNING "%s: User [%d] received a "
380 "message response from process [%d] but does "
381 "not have a registered daemon\n", __func__,
382 msg_ctx->task->euid, pid);
88b4a07e
MH
383 goto wake_up;
384 }
f66e883e 385 if (msg_ctx->task->euid != euid) {
dddfa461 386 rc = -EBADMSG;
f66e883e
MH
387 printk(KERN_WARNING "%s: Received message from user "
388 "[%d]; expected message from user [%d]\n", __func__,
389 euid, msg_ctx->task->euid);
dddfa461
MH
390 goto unlock;
391 }
f66e883e 392 if (daemon->pid != pid) {
88b4a07e 393 rc = -EBADMSG;
f66e883e
MH
394 printk(KERN_ERR "%s: User [%d] sent a message response "
395 "from an unrecognized process [%d]\n",
396 __func__, msg_ctx->task->euid, pid);
88b4a07e
MH
397 goto unlock;
398 }
399 if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_PENDING) {
400 rc = -EINVAL;
f66e883e
MH
401 printk(KERN_WARNING "%s: Desired context element is not "
402 "pending a response\n", __func__);
88b4a07e
MH
403 goto unlock;
404 } else if (msg_ctx->counter != seq) {
405 rc = -EINVAL;
f66e883e
MH
406 printk(KERN_WARNING "%s: Invalid message sequence; "
407 "expected [%d]; received [%d]\n", __func__,
408 msg_ctx->counter, seq);
88b4a07e
MH
409 goto unlock;
410 }
f66e883e 411 msg_size = (sizeof(*msg) + msg->data_len);
88b4a07e
MH
412 msg_ctx->msg = kmalloc(msg_size, GFP_KERNEL);
413 if (!msg_ctx->msg) {
414 rc = -ENOMEM;
f66e883e
MH
415 printk(KERN_ERR "%s: Failed to allocate [%Zd] bytes of "
416 "GFP_KERNEL memory\n", __func__, msg_size);
88b4a07e
MH
417 goto unlock;
418 }
419 memcpy(msg_ctx->msg, msg, msg_size);
420 msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_DONE;
421 rc = 0;
422wake_up:
423 wake_up_process(msg_ctx->task);
424unlock:
425 mutex_unlock(&msg_ctx->mux);
426out:
427 return rc;
428}
429
430/**
f66e883e 431 * ecryptfs_send_message_locked
88b4a07e
MH
432 * @transport: The transport over which to send the message (i.e.,
433 * netlink)
434 * @data: The data to send
435 * @data_len: The length of data
436 * @msg_ctx: The message context allocated for the send
f66e883e
MH
437 *
438 * Must be called with ecryptfs_daemon_hash_mux held.
439 *
440 * Returns zero on success; non-zero otherwise
88b4a07e 441 */
f66e883e
MH
442static int
443ecryptfs_send_message_locked(unsigned int transport, char *data, int data_len,
444 u8 msg_type, struct ecryptfs_msg_ctx **msg_ctx)
88b4a07e 445{
f66e883e 446 struct ecryptfs_daemon *daemon;
88b4a07e
MH
447 int rc;
448
f66e883e
MH
449 rc = ecryptfs_find_daemon_by_euid(&daemon, current->euid);
450 if (rc || !daemon) {
88b4a07e 451 rc = -ENOTCONN;
f66e883e
MH
452 printk(KERN_ERR "%s: User [%d] does not have a daemon "
453 "registered\n", __func__, current->euid);
88b4a07e
MH
454 goto out;
455 }
88b4a07e
MH
456 mutex_lock(&ecryptfs_msg_ctx_lists_mux);
457 rc = ecryptfs_acquire_free_msg_ctx(msg_ctx);
458 if (rc) {
459 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
f66e883e
MH
460 printk(KERN_WARNING "%s: Could not claim a free "
461 "context element\n", __func__);
88b4a07e
MH
462 goto out;
463 }
464 ecryptfs_msg_ctx_free_to_alloc(*msg_ctx);
465 mutex_unlock(&(*msg_ctx)->mux);
466 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
467 switch (transport) {
468 case ECRYPTFS_TRANSPORT_NETLINK:
f66e883e
MH
469 rc = ecryptfs_send_netlink(data, data_len, *msg_ctx, msg_type,
470 0, daemon->pid);
471 break;
472 case ECRYPTFS_TRANSPORT_MISCDEV:
473 rc = ecryptfs_send_miscdev(data, data_len, *msg_ctx, msg_type,
474 0, daemon);
88b4a07e
MH
475 break;
476 case ECRYPTFS_TRANSPORT_CONNECTOR:
477 case ECRYPTFS_TRANSPORT_RELAYFS:
478 default:
479 rc = -ENOSYS;
480 }
f66e883e
MH
481 if (rc)
482 printk(KERN_ERR "%s: Error attempting to send message to "
483 "userspace daemon; rc = [%d]\n", __func__, rc);
88b4a07e
MH
484out:
485 return rc;
486}
487
f66e883e
MH
488/**
489 * ecryptfs_send_message
490 * @transport: The transport over which to send the message (i.e.,
491 * netlink)
492 * @data: The data to send
493 * @data_len: The length of data
494 * @msg_ctx: The message context allocated for the send
495 *
496 * Grabs ecryptfs_daemon_hash_mux.
497 *
498 * Returns zero on success; non-zero otherwise
499 */
500int ecryptfs_send_message(unsigned int transport, char *data, int data_len,
501 struct ecryptfs_msg_ctx **msg_ctx)
502{
503 int rc;
504
505 mutex_lock(&ecryptfs_daemon_hash_mux);
506 rc = ecryptfs_send_message_locked(transport, data, data_len,
507 ECRYPTFS_MSG_REQUEST, msg_ctx);
508 mutex_unlock(&ecryptfs_daemon_hash_mux);
509 return rc;
510}
511
88b4a07e
MH
512/**
513 * ecryptfs_wait_for_response
514 * @msg_ctx: The context that was assigned when sending a message
515 * @msg: The incoming message from userspace; not set if rc != 0
516 *
517 * Sleeps until awaken by ecryptfs_receive_message or until the amount
518 * of time exceeds ecryptfs_message_wait_timeout. If zero is
519 * returned, msg will point to a valid message from userspace; a
520 * non-zero value is returned upon failure to receive a message or an
f66e883e 521 * error occurs. Callee must free @msg on success.
88b4a07e
MH
522 */
523int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx,
524 struct ecryptfs_message **msg)
525{
526 signed long timeout = ecryptfs_message_wait_timeout * HZ;
527 int rc = 0;
528
529sleep:
530 timeout = schedule_timeout_interruptible(timeout);
531 mutex_lock(&ecryptfs_msg_ctx_lists_mux);
532 mutex_lock(&msg_ctx->mux);
533 if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_DONE) {
534 if (timeout) {
535 mutex_unlock(&msg_ctx->mux);
536 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
537 goto sleep;
538 }
539 rc = -ENOMSG;
540 } else {
541 *msg = msg_ctx->msg;
542 msg_ctx->msg = NULL;
543 }
544 ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
545 mutex_unlock(&msg_ctx->mux);
546 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
547 return rc;
548}
549
550int ecryptfs_init_messaging(unsigned int transport)
551{
552 int i;
553 int rc = 0;
554
555 if (ecryptfs_number_of_users > ECRYPTFS_MAX_NUM_USERS) {
556 ecryptfs_number_of_users = ECRYPTFS_MAX_NUM_USERS;
f66e883e
MH
557 printk(KERN_WARNING "%s: Specified number of users is "
558 "too large, defaulting to [%d] users\n", __func__,
559 ecryptfs_number_of_users);
88b4a07e 560 }
f66e883e
MH
561 mutex_init(&ecryptfs_daemon_hash_mux);
562 mutex_lock(&ecryptfs_daemon_hash_mux);
5dda6992
MH
563 ecryptfs_hash_buckets = 1;
564 while (ecryptfs_number_of_users >> ecryptfs_hash_buckets)
565 ecryptfs_hash_buckets++;
f66e883e
MH
566 ecryptfs_daemon_hash = kmalloc((sizeof(struct hlist_head)
567 * ecryptfs_hash_buckets), GFP_KERNEL);
568 if (!ecryptfs_daemon_hash) {
88b4a07e 569 rc = -ENOMEM;
f66e883e
MH
570 printk(KERN_ERR "%s: Failed to allocate memory\n", __func__);
571 mutex_unlock(&ecryptfs_daemon_hash_mux);
88b4a07e
MH
572 goto out;
573 }
574 for (i = 0; i < ecryptfs_hash_buckets; i++)
f66e883e
MH
575 INIT_HLIST_HEAD(&ecryptfs_daemon_hash[i]);
576 mutex_unlock(&ecryptfs_daemon_hash_mux);
88b4a07e 577 ecryptfs_msg_ctx_arr = kmalloc((sizeof(struct ecryptfs_msg_ctx)
f66e883e
MH
578 * ecryptfs_message_buf_len),
579 GFP_KERNEL);
88b4a07e
MH
580 if (!ecryptfs_msg_ctx_arr) {
581 rc = -ENOMEM;
f66e883e 582 printk(KERN_ERR "%s: Failed to allocate memory\n", __func__);
88b4a07e
MH
583 goto out;
584 }
585 mutex_init(&ecryptfs_msg_ctx_lists_mux);
586 mutex_lock(&ecryptfs_msg_ctx_lists_mux);
587 ecryptfs_msg_counter = 0;
588 for (i = 0; i < ecryptfs_message_buf_len; i++) {
589 INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].node);
f66e883e 590 INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].daemon_out_list);
88b4a07e
MH
591 mutex_init(&ecryptfs_msg_ctx_arr[i].mux);
592 mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
593 ecryptfs_msg_ctx_arr[i].index = i;
594 ecryptfs_msg_ctx_arr[i].state = ECRYPTFS_MSG_CTX_STATE_FREE;
595 ecryptfs_msg_ctx_arr[i].counter = 0;
596 ecryptfs_msg_ctx_arr[i].task = NULL;
597 ecryptfs_msg_ctx_arr[i].msg = NULL;
598 list_add_tail(&ecryptfs_msg_ctx_arr[i].node,
599 &ecryptfs_msg_ctx_free_list);
600 mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
601 }
602 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
603 switch(transport) {
604 case ECRYPTFS_TRANSPORT_NETLINK:
605 rc = ecryptfs_init_netlink();
606 if (rc)
607 ecryptfs_release_messaging(transport);
608 break;
f66e883e
MH
609 case ECRYPTFS_TRANSPORT_MISCDEV:
610 rc = ecryptfs_init_ecryptfs_miscdev();
611 if (rc)
612 ecryptfs_release_messaging(transport);
613 break;
88b4a07e
MH
614 case ECRYPTFS_TRANSPORT_CONNECTOR:
615 case ECRYPTFS_TRANSPORT_RELAYFS:
616 default:
617 rc = -ENOSYS;
618 }
619out:
620 return rc;
621}
622
623void ecryptfs_release_messaging(unsigned int transport)
624{
625 if (ecryptfs_msg_ctx_arr) {
626 int i;
627
628 mutex_lock(&ecryptfs_msg_ctx_lists_mux);
629 for (i = 0; i < ecryptfs_message_buf_len; i++) {
630 mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
631 if (ecryptfs_msg_ctx_arr[i].msg)
632 kfree(ecryptfs_msg_ctx_arr[i].msg);
633 mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
634 }
635 kfree(ecryptfs_msg_ctx_arr);
636 mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
637 }
f66e883e 638 if (ecryptfs_daemon_hash) {
88b4a07e 639 struct hlist_node *elem;
f66e883e 640 struct ecryptfs_daemon *daemon;
88b4a07e
MH
641 int i;
642
f66e883e 643 mutex_lock(&ecryptfs_daemon_hash_mux);
88b4a07e 644 for (i = 0; i < ecryptfs_hash_buckets; i++) {
f66e883e
MH
645 int rc;
646
647 hlist_for_each_entry(daemon, elem,
648 &ecryptfs_daemon_hash[i],
649 euid_chain) {
650 rc = ecryptfs_exorcise_daemon(daemon);
651 if (rc)
652 printk(KERN_ERR "%s: Error whilst "
653 "attempting to destroy daemon; "
654 "rc = [%d]. Dazed and confused, "
655 "but trying to continue.\n",
656 __func__, rc);
88b4a07e
MH
657 }
658 }
f66e883e
MH
659 kfree(ecryptfs_daemon_hash);
660 mutex_unlock(&ecryptfs_daemon_hash_mux);
88b4a07e
MH
661 }
662 switch(transport) {
663 case ECRYPTFS_TRANSPORT_NETLINK:
664 ecryptfs_release_netlink();
665 break;
f66e883e
MH
666 case ECRYPTFS_TRANSPORT_MISCDEV:
667 ecryptfs_destroy_ecryptfs_miscdev();
668 break;
88b4a07e
MH
669 case ECRYPTFS_TRANSPORT_CONNECTOR:
670 case ECRYPTFS_TRANSPORT_RELAYFS:
671 default:
672 break;
673 }
674 return;
675}
This page took 0.241811 seconds and 5 git commands to generate.