22 #include <linux/sched.h>
23 #include <linux/slab.h>
28 static LIST_HEAD(ecryptfs_msg_ctx_free_list);
29 static LIST_HEAD(ecryptfs_msg_ctx_alloc_list);
30 static struct mutex ecryptfs_msg_ctx_lists_mux;
32 static struct hlist_head *ecryptfs_daemon_hash;
34 static int ecryptfs_hash_bits;
35 #define ecryptfs_current_euid_hash(uid) \
36 hash_long((unsigned long)from_kuid(&init_user_ns, current_euid()), ecryptfs_hash_bits)
38 static u32 ecryptfs_msg_counter;
56 if (list_empty(&ecryptfs_msg_ctx_free_list)) {
58 "context list is empty. It may be helpful to "
59 "specify the ecryptfs_message_buf_len "
60 "parameter to be greater than the current "
86 list_move(&msg_ctx->
node, &ecryptfs_msg_ctx_alloc_list);
88 msg_ctx->
counter = ++ecryptfs_msg_counter;
99 list_move(&(msg_ctx->
node), &ecryptfs_msg_ctx_free_list);
124 if (uid_eq((*daemon)->file->f_cred->euid,
current_euid())) {
149 (*daemon) = kzalloc(
sizeof(**daemon),
GFP_KERNEL);
153 "GFP_KERNEL memory\n", __func__,
sizeof(**daemon));
156 (*daemon)->file =
file;
158 INIT_LIST_HEAD(&(*daemon)->msg_ctx_out_queue);
160 (*daemon)->num_queued_msg_ctx = 0;
161 hlist_add_head(&(*daemon)->euid_chain,
190 "the out queue of a dying daemon\n", __func__);
232 "context buffer at index [%d]; maximum "
233 "allowable is [%d]\n", __func__, msg->
index,
237 msg_ctx = &ecryptfs_msg_ctx_arr[msg->
index];
242 "pending a response\n", __func__);
244 }
else if (msg_ctx->
counter != seq) {
247 "expected [%d]; received [%d]\n", __func__,
251 msg_size = (
sizeof(*msg) + msg->
data_len);
256 "GFP_KERNEL memory\n", __func__, msg_size);
292 rc = ecryptfs_acquire_free_msg_ctx(msg_ctx);
296 "context element\n", __func__);
299 ecryptfs_msg_ctx_free_to_alloc(*msg_ctx);
306 "userspace daemon; rc = [%d]\n", __func__, rc);
379 "too large, defaulting to [%d] users\n", __func__,
384 ecryptfs_hash_bits = 1;
386 ecryptfs_hash_bits++;
388 * (1 << ecryptfs_hash_bits)),
390 if (!ecryptfs_daemon_hash) {
396 for (i = 0; i < (1 << ecryptfs_hash_bits); i++)
402 if (!ecryptfs_msg_ctx_arr) {
409 ecryptfs_msg_counter = 0;
411 INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].
node);
412 INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].daemon_out_list);
415 ecryptfs_msg_ctx_arr[
i].
index =
i;
417 ecryptfs_msg_ctx_arr[
i].
counter = 0;
421 &ecryptfs_msg_ctx_free_list);
434 if (ecryptfs_msg_ctx_arr) {
440 if (ecryptfs_msg_ctx_arr[i].
msg)
441 kfree(ecryptfs_msg_ctx_arr[i].msg);
444 kfree(ecryptfs_msg_ctx_arr);
447 if (ecryptfs_daemon_hash) {
453 for (i = 0; i < (1 << ecryptfs_hash_bits); i++) {
457 &ecryptfs_daemon_hash[i],
462 "attempting to destroy daemon; "
463 "rc = [%d]. Dazed and confused, "
464 "but trying to continue.\n",
468 kfree(ecryptfs_daemon_hash);