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