btrfs: Don't clear SGID when inheriting ACLs
[linux/fpc-iii.git] / drivers / char / tpm / tpm-dev-common.c
blob610638a80383d4a104a6fe1ec42479722f222f49
1 /*
2 * Copyright (C) 2004 IBM Corporation
3 * Authors:
4 * Leendert van Doorn <leendert@watson.ibm.com>
5 * Dave Safford <safford@watson.ibm.com>
6 * Reiner Sailer <sailer@watson.ibm.com>
7 * Kylene Hall <kjhall@us.ibm.com>
9 * Copyright (C) 2013 Obsidian Research Corp
10 * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
12 * Device file system interface to the TPM
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation, version 2 of the
17 * License.
20 #include <linux/slab.h>
21 #include <linux/uaccess.h>
22 #include "tpm.h"
23 #include "tpm-dev.h"
25 static void user_reader_timeout(unsigned long ptr)
27 struct file_priv *priv = (struct file_priv *)ptr;
29 pr_warn("TPM user space timeout is deprecated (pid=%d)\n",
30 task_tgid_nr(current));
32 schedule_work(&priv->work);
35 static void timeout_work(struct work_struct *work)
37 struct file_priv *priv = container_of(work, struct file_priv, work);
39 mutex_lock(&priv->buffer_mutex);
40 atomic_set(&priv->data_pending, 0);
41 memset(priv->data_buffer, 0, sizeof(priv->data_buffer));
42 mutex_unlock(&priv->buffer_mutex);
45 void tpm_common_open(struct file *file, struct tpm_chip *chip,
46 struct file_priv *priv)
48 priv->chip = chip;
49 atomic_set(&priv->data_pending, 0);
50 mutex_init(&priv->buffer_mutex);
51 setup_timer(&priv->user_read_timer, user_reader_timeout,
52 (unsigned long)priv);
53 INIT_WORK(&priv->work, timeout_work);
55 file->private_data = priv;
58 ssize_t tpm_common_read(struct file *file, char __user *buf,
59 size_t size, loff_t *off)
61 struct file_priv *priv = file->private_data;
62 ssize_t ret_size;
63 ssize_t orig_ret_size;
64 int rc;
66 del_singleshot_timer_sync(&priv->user_read_timer);
67 flush_work(&priv->work);
68 ret_size = atomic_read(&priv->data_pending);
69 if (ret_size > 0) { /* relay data */
70 orig_ret_size = ret_size;
71 if (size < ret_size)
72 ret_size = size;
74 mutex_lock(&priv->buffer_mutex);
75 rc = copy_to_user(buf, priv->data_buffer, ret_size);
76 memset(priv->data_buffer, 0, orig_ret_size);
77 if (rc)
78 ret_size = -EFAULT;
80 mutex_unlock(&priv->buffer_mutex);
83 atomic_set(&priv->data_pending, 0);
85 return ret_size;
88 ssize_t tpm_common_write(struct file *file, const char __user *buf,
89 size_t size, loff_t *off, struct tpm_space *space)
91 struct file_priv *priv = file->private_data;
92 size_t in_size = size;
93 ssize_t out_size;
95 /* Cannot perform a write until the read has cleared either via
96 * tpm_read or a user_read_timer timeout. This also prevents split
97 * buffered writes from blocking here.
99 if (atomic_read(&priv->data_pending) != 0)
100 return -EBUSY;
102 if (in_size > TPM_BUFSIZE)
103 return -E2BIG;
105 mutex_lock(&priv->buffer_mutex);
107 if (copy_from_user
108 (priv->data_buffer, (void __user *) buf, in_size)) {
109 mutex_unlock(&priv->buffer_mutex);
110 return -EFAULT;
113 /* atomic tpm command send and result receive. We only hold the ops
114 * lock during this period so that the tpm can be unregistered even if
115 * the char dev is held open.
117 if (tpm_try_get_ops(priv->chip)) {
118 mutex_unlock(&priv->buffer_mutex);
119 return -EPIPE;
121 out_size = tpm_transmit(priv->chip, space, priv->data_buffer,
122 sizeof(priv->data_buffer), 0);
124 tpm_put_ops(priv->chip);
125 if (out_size < 0) {
126 mutex_unlock(&priv->buffer_mutex);
127 return out_size;
130 atomic_set(&priv->data_pending, out_size);
131 mutex_unlock(&priv->buffer_mutex);
133 /* Set a timeout by which the reader must come claim the result */
134 mod_timer(&priv->user_read_timer, jiffies + (120 * HZ));
136 return in_size;
140 * Called on file close
142 void tpm_common_release(struct file *file, struct file_priv *priv)
144 del_singleshot_timer_sync(&priv->user_read_timer);
145 flush_work(&priv->work);
146 file->private_data = NULL;
147 atomic_set(&priv->data_pending, 0);