+static ssize_t helloc_write(struct file *filp, const char __user *buf,
+ size_t len, loff_t *pos)
+{
+ int r;
+ struct message *msg = filp->private_data;
+ /* do not read pass through the size of the message */
+ if (*pos >= BUFFER_SIZE)
+ /* return end of file */
+ return 0;
+ /* if len is bigger than the rest of the message, clamp it */
+ if (len > BUFFER_SIZE - *pos)
+ len = BUFFER_SIZE - *pos;
+
+ /* copy message to user space and return error if it fails */
+ r = copy_from_user(msg->text + *pos, buf, len);
+ if (r) {
+ if (*pos + len > msg->len)
+ memset(msg->text + msg->len, 0, *pos + len - msg->len);
+ return -EFAULT;
+ }
+ /* update the file position */
+ *pos += len;
+ if (msg->len < *pos) {
+ printk(KERN_DEBUG "Updating size from %d to %lld\n", msg->len, *pos);
+ msg->len = *pos;
+ }
+
+ return len;
+}
+