summaryrefslogtreecommitdiff
path: root/other/ecfs/file.c
blob: 15bc0e2cc2e207362e104d3accf02fc6e2bd30d6 (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 *  linux/fs/ecfs/file.c
 *
 * Copyright (C) 1992, 1993, 1994, 1995
 * Remy Card (card@masi.ibp.fr)
 * Laboratoire MASI - Institut Blaise Pascal
 * Universite Pierre et Marie Curie (Paris VI)
 *
 *  from
 *
 *  linux/fs/minix/file.c
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 *
 *  ecfs fs regular file handling primitives
 *
 *  64-bit file support on 64-bit platforms by Jakub Jelinek
 * 	(jj@sunsite.ms.mff.cuni.cz)
 */

#include <linux/fs_ecfs.h>
#include <linux/ecfs_fs.h>
#include "rc4.h"
#include <linux/sched.h>
#include <linux/mm.h>
#include <asm/uaccess.h>
#include <linux/malloc.h>

static loff_t ecfs_file_lseek(struct file *, loff_t, int);
static int ecfs_open_file (struct inode *, struct file *);

#define ECFS_MAX_SIZE(bits)							\
	(((ECFS_NDIR_BLOCKS + (1LL << (bits - 2)) + 				\
	   (1LL << (bits - 2)) * (1LL << (bits - 2)) + 				\
	   (1LL << (bits - 2)) * (1LL << (bits - 2)) * (1LL << (bits - 2))) * 	\
	  (1LL << bits)) - 1)

static long long ecfs_max_sizes[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
ECFS_MAX_SIZE(10), ECFS_MAX_SIZE(11), ECFS_MAX_SIZE(12), ECFS_MAX_SIZE(13)
};

/*
 * Make sure the offset never goes beyond the 32-bit mark..
 */
static loff_t ecfs_file_lseek(
	struct file *file,
	loff_t offset,
	int origin)
{
	struct inode *inode = file->f_dentry->d_inode;

	switch (origin) {
		case 2:
			offset += inode->i_size;
			break;
		case 1:
			offset += file->f_pos;
	}
	if (offset<0)
		return -EINVAL;
	if (((unsigned long long) offset >> 32) != 0) {
		if (offset > ecfs_max_sizes[ECFS_BLOCK_SIZE_BITS(inode->i_sb)])
			return -EINVAL;
	} 
	if (offset != file->f_pos) {
		file->f_pos = offset;
		file->f_reada = 0;
		file->f_version = ++event;
	}
	return offset;
}

/*
 * Called when an inode is released. Note that this is different
 * from ecfs_file_open: open gets called at every open, but release
 * gets called only when /all/ the files are closed.
 */
static int ecfs_release_file (struct inode * inode, struct file * filp)
{
	if (filp->f_mode & FMODE_WRITE)
		ecfs_discard_prealloc (inode);
	return 0;
}

/*
 * Called when an inode is about to be open.
 * We use this to disallow opening RW large files on 32bit systems if
 * the caller didn't specify O_LARGEFILE.  On 64bit systems we force
 * on this flag in sys_open.
 */
static int ecfs_open_file (struct inode * inode, struct file * filp)
{
	if (!(filp->f_flags & O_LARGEFILE) &&
	    inode->i_size > 0x7FFFFFFFLL)
		return -EFBIG;
	return 0;
}


unsigned char *ecfs_key = "A";

/* page flags 21 .. 29 are unused
#define PAGE_PLAIN 22

struct page * ecfs_filemap_nopage(struct vm_area_struct *area,
        unsigned long address, int no_share)
{
	unsigned int from = area->vm_file->f_pos;
	unsigned char *ptr;
	unsigned int count = area->vm_end - area->vm_start;
	struct page *r;
	rc4_key rc4key;

	if (!orig_ops)
		return NULL;

	printk("<1> foo\n");	
	r = orig_ops->nopage(area, address, no_share);

	printk("<1> bar %p %d %d\n", r->virtual, from, r->count);

	ptr = r->virtual;

	if (!ptr)// || test_bit(PAGE_PLAIN, &r->flags))
		return r;

	prepare_key(ecfs_key, strlen(ecfs_key), &rc4key);
	rc4(ptr, PAGE_SIZE, &rc4key, from);

	SetPageReserved(r);
	return r;
}


static struct vm_operations_struct ecfs_nopage = {
        nopage:         ecfs_filemap_nopage,
};



int ecfs_file_mmap(struct file *file, struct vm_area_struct *vma)
{
	int r = generic_file_mmap(file, vma);
	orig_ops = vma->vm_ops;
	vma->vm_ops = &ecfs_nopage;
	return r;	
}


*/
ssize_t ecfs_file_read(struct file *file, char *buf, size_t count, loff_t *ppos)
{
	rc4_key rc4key;
	unsigned int from = file->f_pos;
	unsigned char *ptr;
	ssize_t r;
	char file_key[1024+100];	/* to avoid keystream-reuse */

	memset(file_key, 0, sizeof(file_key));

	/* Should not overflow as init() checked for > 1024 */
	sprintf(file_key, "%s%lx", ecfs_key, file->f_dentry->d_inode->i_ino);

	r = generic_file_read(file, buf, count, ppos);
	if (r < 0)
		return r;

	if ((ptr = kmalloc(count, GFP_KERNEL)) == NULL)
		return r;

	copy_from_user(ptr, buf, r);
	
	prepare_key(file_key, strlen(file_key), &rc4key);
	rc4(ptr, r, &rc4key, from);

	copy_to_user(buf, ptr, r);
	kfree(ptr);
	return r;
}


ssize_t
ecfs_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
{
	rc4_key rc4key;
	unsigned int from = file->f_pos;
	unsigned char *ptr;
	mm_segment_t orig_fs;
	ssize_t r;
	char file_key[1024+100];	/* to avoid keystream-reuse */

	memset(file_key, 0, sizeof(file_key));

	/* Should not overflow as init() checked for > 1024 */
	sprintf(file_key, "%s%lx", ecfs_key, file->f_dentry->d_inode->i_ino);


	if ((ptr = kmalloc(count, GFP_KERNEL)) == NULL)
		return -1;

	copy_from_user(ptr, buf, count);

	prepare_key(file_key, strlen(file_key), &rc4key);
	rc4(ptr, count, &rc4key, from);

	orig_fs = get_fs();
	set_fs(KERNEL_DS);
	r = generic_file_write(file, ptr, count, ppos);
	set_fs(orig_fs);
	kfree(ptr);
	return r;
}


/*
 * We have mostly NULL's here: the current defaults are ok for
 * the ecfs filesystem.
 */
struct file_operations ecfs_file_operations = {
	llseek:		ecfs_file_lseek,
	read:		ecfs_file_read,
	write:		ecfs_file_write,
	ioctl:		ecfs_ioctl,
	mmap:		generic_file_mmap,
	open:		ecfs_open_file,
	release:	ecfs_release_file,
	fsync:		ecfs_sync_file,
};

struct inode_operations ecfs_file_inode_operations = {
	truncate:	ecfs_truncate,
};