c6038bd60897334158441586261758c5f5e195e8
[safe/jmp/linux-2.6] / 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 "ecryptfs_kernel.h"
24
25 static LIST_HEAD(ecryptfs_msg_ctx_free_list);
26 static LIST_HEAD(ecryptfs_msg_ctx_alloc_list);
27 static struct mutex ecryptfs_msg_ctx_lists_mux;
28
29 static struct hlist_head *ecryptfs_daemon_hash;
30 struct mutex ecryptfs_daemon_hash_mux;
31 static int ecryptfs_hash_buckets;
32 #define ecryptfs_uid_hash(uid) \
33         hash_long((unsigned long)uid, ecryptfs_hash_buckets)
34
35 static u32 ecryptfs_msg_counter;
36 static struct ecryptfs_msg_ctx *ecryptfs_msg_ctx_arr;
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
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.
47  */
48 static 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)) {
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);
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;
71 out:
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  *
79  * Must be called with ecryptfs_msg_ctx_lists_mux held.
80  */
81 static 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  *
92  * Must be called with ecryptfs_msg_ctx_lists_mux held.
93  */
94 void ecryptfs_msg_ctx_alloc_to_free(struct ecryptfs_msg_ctx *msg_ctx)
95 {
96         list_move(&(msg_ctx->node), &ecryptfs_msg_ctx_free_list);
97         if (msg_ctx->msg)
98                 kfree(msg_ctx->msg);
99         msg_ctx->msg = NULL;
100         msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_FREE;
101 }
102
103 /**
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
107  *
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.
113  */
114 int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon, uid_t euid)
115 {
116         struct hlist_node *elem;
117         int rc;
118
119         hlist_for_each_entry(*daemon, elem,
120                              &ecryptfs_daemon_hash[ecryptfs_uid_hash(euid)],
121                              euid_chain) {
122                 if ((*daemon)->euid == euid) {
123                         rc = 0;
124                         goto out;
125                 }
126         }
127         rc = -EINVAL;
128 out:
129         return rc;
130 }
131
132 static int
133 ecryptfs_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  */
149 static int ecryptfs_send_raw_message(unsigned int transport, u8 msg_type,
150                                      struct ecryptfs_daemon *daemon)
151 {
152         struct ecryptfs_msg_ctx *msg_ctx;
153         int rc;
154
155         switch(transport) {
156         case ECRYPTFS_TRANSPORT_NETLINK:
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);
175                 break;
176         case ECRYPTFS_TRANSPORT_CONNECTOR:
177         case ECRYPTFS_TRANSPORT_RELAYFS:
178         default:
179                 rc = -ENOSYS;
180         }
181 out:
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  */
196 int
197 ecryptfs_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)]);
217 out:
218         return rc;
219 }
220
221 /**
222  * ecryptfs_process_helo
223  * @transport: The underlying transport (netlink, etc.)
224  * @euid: The user ID owner of the message
225  * @pid: The process ID for the userspace program that sent the
226  *       message
227  *
228  * Adds the euid and pid values to the daemon euid hash.  If an euid
229  * already has a daemon pid registered, the daemon will be
230  * unregistered before the new daemon is put into the hash list.
231  * Returns zero after adding a new daemon to the hash list;
232  * non-zero otherwise.
233  */
234 int ecryptfs_process_helo(unsigned int transport, uid_t euid, pid_t pid)
235 {
236         struct ecryptfs_daemon *new_daemon;
237         struct ecryptfs_daemon *old_daemon;
238         int rc;
239
240         mutex_lock(&ecryptfs_daemon_hash_mux);
241         rc = ecryptfs_find_daemon_by_euid(&old_daemon, euid);
242         if (rc != 0) {
243                 printk(KERN_WARNING "Received request from user [%d] "
244                        "to register daemon [%d]; unregistering daemon "
245                        "[%d]\n", euid, pid, old_daemon->pid);
246                 rc = ecryptfs_send_raw_message(transport, ECRYPTFS_MSG_QUIT,
247                                                old_daemon);
248                 if (rc)
249                         printk(KERN_WARNING "Failed to send QUIT "
250                                "message to daemon [%d]; rc = [%d]\n",
251                                old_daemon->pid, rc);
252                 hlist_del(&old_daemon->euid_chain);
253                 kfree(old_daemon);
254         }
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  */
270 int 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);
299 out:
300         return rc;
301 }
302
303 /**
304  * ecryptfs_process_quit
305  * @euid: The user ID owner of the message
306  * @pid: The process ID for the userspace program that sent the
307  *       message
308  *
309  * Deletes the corresponding daemon for the given euid and pid, if
310  * it is the registered that is requesting the deletion. Returns zero
311  * after deleting the desired daemon; non-zero otherwise.
312  */
313 int ecryptfs_process_quit(uid_t euid, pid_t pid)
314 {
315         struct ecryptfs_daemon *daemon;
316         int rc;
317
318         mutex_lock(&ecryptfs_daemon_hash_mux);
319         rc = ecryptfs_find_daemon_by_euid(&daemon, euid);
320         if (rc || !daemon) {
321                 rc = -EINVAL;
322                 printk(KERN_ERR "Received request from user [%d] to "
323                        "unregister unrecognized daemon [%d]\n", euid, pid);
324                 goto out_unlock;
325         }
326         rc = ecryptfs_exorcise_daemon(daemon);
327 out_unlock:
328         mutex_unlock(&ecryptfs_daemon_hash_mux);
329         return rc;
330 }
331
332 /**
333  * ecryptfs_process_reponse
334  * @msg: The ecryptfs message received; the caller should sanity check
335  *       msg->data_len and free the memory
336  * @pid: The process ID of the userspace application that sent the
337  *       message
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.
353  *
354  * Returns zero on success; non-zero otherwise
355  */
356 int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t euid,
357                               pid_t pid, u32 seq)
358 {
359         struct ecryptfs_daemon *daemon;
360         struct ecryptfs_msg_ctx *msg_ctx;
361         size_t msg_size;
362         int rc;
363
364         if (msg->index >= ecryptfs_message_buf_len) {
365                 rc = -EINVAL;
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));
370                 goto out;
371         }
372         msg_ctx = &ecryptfs_msg_ctx_arr[msg->index];
373         mutex_lock(&msg_ctx->mux);
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) {
378                 rc = -EBADMSG;
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);
383                 goto wake_up;
384         }
385         if (msg_ctx->task->euid != euid) {
386                 rc = -EBADMSG;
387                 printk(KERN_WARNING "%s: Received message from user "
388                        "[%d]; expected message from user [%d]\n", __func__,
389                        euid, msg_ctx->task->euid);
390                 goto unlock;
391         }
392         if (daemon->pid != pid) {
393                 rc = -EBADMSG;
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);
397                 goto unlock;
398         }
399         if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_PENDING) {
400                 rc = -EINVAL;
401                 printk(KERN_WARNING "%s: Desired context element is not "
402                        "pending a response\n", __func__);
403                 goto unlock;
404         } else if (msg_ctx->counter != seq) {
405                 rc = -EINVAL;
406                 printk(KERN_WARNING "%s: Invalid message sequence; "
407                        "expected [%d]; received [%d]\n", __func__,
408                        msg_ctx->counter, seq);
409                 goto unlock;
410         }
411         msg_size = (sizeof(*msg) + msg->data_len);
412         msg_ctx->msg = kmalloc(msg_size, GFP_KERNEL);
413         if (!msg_ctx->msg) {
414                 rc = -ENOMEM;
415                 printk(KERN_ERR "%s: Failed to allocate [%Zd] bytes of "
416                        "GFP_KERNEL memory\n", __func__, msg_size);
417                 goto unlock;
418         }
419         memcpy(msg_ctx->msg, msg, msg_size);
420         msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_DONE;
421         rc = 0;
422 wake_up:
423         wake_up_process(msg_ctx->task);
424 unlock:
425         mutex_unlock(&msg_ctx->mux);
426 out:
427         return rc;
428 }
429
430 /**
431  * ecryptfs_send_message_locked
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
437  *
438  * Must be called with ecryptfs_daemon_hash_mux held.
439  *
440  * Returns zero on success; non-zero otherwise
441  */
442 static int
443 ecryptfs_send_message_locked(unsigned int transport, char *data, int data_len,
444                              u8 msg_type, struct ecryptfs_msg_ctx **msg_ctx)
445 {
446         struct ecryptfs_daemon *daemon;
447         int rc;
448
449         rc = ecryptfs_find_daemon_by_euid(&daemon, current->euid);
450         if (rc || !daemon) {
451                 rc = -ENOTCONN;
452                 printk(KERN_ERR "%s: User [%d] does not have a daemon "
453                        "registered\n", __func__, current->euid);
454                 goto out;
455         }
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);
460                 printk(KERN_WARNING "%s: Could not claim a free "
461                        "context element\n", __func__);
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:
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);
475                 break;
476         case ECRYPTFS_TRANSPORT_CONNECTOR:
477         case ECRYPTFS_TRANSPORT_RELAYFS:
478         default:
479                 rc = -ENOSYS;
480         }
481         if (rc)
482                 printk(KERN_ERR "%s: Error attempting to send message to "
483                        "userspace daemon; rc = [%d]\n", __func__, rc);
484 out:
485         return rc;
486 }
487
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  */
500 int 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
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
521  * error occurs. Callee must free @msg on success.
522  */
523 int 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
529 sleep:
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
550 int 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;
557                 printk(KERN_WARNING "%s: Specified number of users is "
558                        "too large, defaulting to [%d] users\n", __func__,
559                        ecryptfs_number_of_users);
560         }
561         mutex_init(&ecryptfs_daemon_hash_mux);
562         mutex_lock(&ecryptfs_daemon_hash_mux);
563         ecryptfs_hash_buckets = 1;
564         while (ecryptfs_number_of_users >> ecryptfs_hash_buckets)
565                 ecryptfs_hash_buckets++;
566         ecryptfs_daemon_hash = kmalloc((sizeof(struct hlist_head)
567                                         * ecryptfs_hash_buckets), GFP_KERNEL);
568         if (!ecryptfs_daemon_hash) {
569                 rc = -ENOMEM;
570                 printk(KERN_ERR "%s: Failed to allocate memory\n", __func__);
571                 mutex_unlock(&ecryptfs_daemon_hash_mux);
572                 goto out;
573         }
574         for (i = 0; i < ecryptfs_hash_buckets; i++)
575                 INIT_HLIST_HEAD(&ecryptfs_daemon_hash[i]);
576         mutex_unlock(&ecryptfs_daemon_hash_mux);
577         ecryptfs_msg_ctx_arr = kmalloc((sizeof(struct ecryptfs_msg_ctx)
578                                         * ecryptfs_message_buf_len),
579                                        GFP_KERNEL);
580         if (!ecryptfs_msg_ctx_arr) {
581                 rc = -ENOMEM;
582                 printk(KERN_ERR "%s: Failed to allocate memory\n", __func__);
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);
590                 INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].daemon_out_list);
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;
609         case ECRYPTFS_TRANSPORT_MISCDEV:
610                 rc = ecryptfs_init_ecryptfs_miscdev();
611                 if (rc)
612                         ecryptfs_release_messaging(transport);
613                 break;
614         case ECRYPTFS_TRANSPORT_CONNECTOR:
615         case ECRYPTFS_TRANSPORT_RELAYFS:
616         default:
617                 rc = -ENOSYS;
618         }
619 out:
620         return rc;
621 }
622
623 void 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         }
638         if (ecryptfs_daemon_hash) {
639                 struct hlist_node *elem;
640                 struct ecryptfs_daemon *daemon;
641                 int i;
642
643                 mutex_lock(&ecryptfs_daemon_hash_mux);
644                 for (i = 0; i < ecryptfs_hash_buckets; i++) {
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);
657                         }
658                 }
659                 kfree(ecryptfs_daemon_hash);
660                 mutex_unlock(&ecryptfs_daemon_hash_mux);
661         }
662         switch(transport) {
663         case ECRYPTFS_TRANSPORT_NETLINK:
664                 ecryptfs_release_netlink();
665                 break;
666         case ECRYPTFS_TRANSPORT_MISCDEV:
667                 ecryptfs_destroy_ecryptfs_miscdev();
668                 break;
669         case ECRYPTFS_TRANSPORT_CONNECTOR:
670         case ECRYPTFS_TRANSPORT_RELAYFS:
671         default:
672                 break;
673         }
674         return;
675 }