-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmap.c
176 lines (141 loc) · 3.87 KB
/
mmap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "xcfs.h"
#include <linux/pagemap.h>
#include <linux/writeback.h>
#include <linux/page-flags.h>
#include <linux/mount.h>
#include <linux/file.h>
#include <linux/slab.h>
#include <asm/unaligned.h>
//Reading and Decryption
void xcfs_decrypt(char* buf, size_t count)
{
int i = 0;
printk("xcfs_decrypt\n");
for(i = 0; i < count; ++i) {
buf[i]--;
}
}
int xcfs_decrypt_page(struct file *file, struct page *page)
{
char* virt = kmap(page);
printk("xcfs_decrypt_page\n");
xcfs_decrypt(virt, PAGE_SIZE);
//some cleanup
kunmap(page);
return 0;
}
//returns number of bytes read (positive) or an error (negative)
static int read_lower(struct file* file, char *data, loff_t offset, size_t size)
{
struct file *lower_file = NULL;
printk("read_lower\n");
lower_file = xcfs_lower_file(file);
if(!lower_file)
return -EIO;
return kernel_read(lower_file, offset, data, size);
}
//returns 0 on success, nonzero on failure
static int read_lower_page_segment( struct file *file,
struct page *page, pgoff_t page_index,
size_t offset_in_page, size_t size)
{
char *virt = NULL;
loff_t offset = 0;
int rc = 0;
printk("read_lower_page_segment\n");
//calculate file offset from page offset and page index
offset = ((((loff_t)page_index) << PAGE_SHIFT) + offset_in_page);
virt = kmap(page);
//hand off actual reading
rc = read_lower(file, virt, offset, size);
if(rc > 0) //positive values mean we read correctly,
rc = 0; // so this function should return 0
//some cleanup
kunmap(page);
flush_dcache_page(page);
return rc;
}
//returns 0 on success, nonzero on failure
static int xcfs_readpage(struct file *file, struct page *page)
{
int rc = 0;
printk("xcfs_readpage\n");
rc = read_lower_page_segment(file, page, page->index, 0,
PAGE_SIZE);
//do decryption
xcfs_decrypt_page(file, page);
if(rc)
ClearPageUptodate(page);
else
SetPageUptodate(page);
unlock_page(page);
printk("xcfs_readpage returns %d\n", rc);
return rc;
}
//Writing and Encryption
void xcfs_encrypt(char* buf, size_t count)
{
int i = 0;
printk("xcfs_encrypt\n");
for(i = 0; i < count; ++i) {
buf[i]++;
}
}
int xcfs_encrypt_page(struct page *page, struct page *crypt_page)
{
char *old_page_virt = kmap(page);
char *crypt_page_virt = kmap(crypt_page);
printk("xcfs_encrypt_page\n");
//copies old page to temp page
memcpy(crypt_page_virt, old_page_virt, PAGE_SIZE);
xcfs_encrypt(crypt_page_virt, PAGE_SIZE);
return 0;
}
static int xcfs_writepage(struct page *page, struct writeback_control *wbc)
{
struct page *crypt_page = NULL;
struct inode *lower_inode = xcfs_lower_inode(page->mapping->host);
int retval = 0;
printk("xcfs_writepage\n");
//allocate temporary page
crypt_page = alloc_page(GFP_USER);
if(crypt_page == NULL)
{
retval = -ENOMEM;
printk("Error allocation memory for temp encrypted page\n");
goto xcfs_writepage_out;
}
//encrypt the page given to us and store it in a temporary page
xcfs_encrypt_page(page, crypt_page);
//passes the writepage call down to the lower filesystem,
// using the encrypted page
//we can't pass it down to kernel_write like we do in readpage,
// because there's no way to get the "struct file" data structure
// for the page we're given without making some modifications
// to the module's data structures (see the ecrypfs_inode_info
// struct)
//(we could probably implement readpage in the same way, but it works
// as-is, and I don't want to mess with it)
retval = lower_inode->i_mapping->a_ops->writepage(crypt_page, wbc);
xcfs_writepage_out:
//frees temporary page, if necessary
if(crypt_page)
{
__free_page(crypt_page);
}
if(retval)
{
printk("xcfs_encrypt_page: error encrypting page\n");
ClearPageUptodate(page);
}
else
{
SetPageUptodate(page);
}
unlock_page(page);
return retval;
}
const struct address_space_operations xcfs_addr_ops = {
.readpage = xcfs_readpage,
.writepage = xcfs_writepage,
};