1 |
kumaneko |
111 |
/* |
2 |
|
|
* fs/syaoran_2.6.c |
3 |
|
|
* |
4 |
|
|
* Implementation of the Tamper-Proof Device Filesystem. |
5 |
|
|
* |
6 |
|
|
* Portions Copyright (C) 2005-2007 NTT DATA CORPORATION |
7 |
|
|
* |
8 |
kumaneko |
224 |
* Version: 1.4.1-rc1 2007/05/20 |
9 |
kumaneko |
111 |
* |
10 |
|
|
* This file is applicable to 2.6.11 and later. |
11 |
|
|
* See README.ccs for ChangeLog. |
12 |
|
|
* This filesystem is developed using the ramfs implementation. |
13 |
|
|
* |
14 |
|
|
*/ |
15 |
|
|
/* |
16 |
|
|
* Resizable simple ram filesystem for Linux. |
17 |
|
|
* |
18 |
|
|
* Copyright (C) 2000 Linus Torvalds. |
19 |
|
|
* 2000 Transmeta Corp. |
20 |
|
|
* |
21 |
|
|
* Usage limits added by David Gibson, Linuxcare Australia. |
22 |
|
|
* This file is released under the GPL. |
23 |
|
|
*/ |
24 |
|
|
|
25 |
|
|
/* |
26 |
|
|
* NOTE! This filesystem is probably most useful |
27 |
|
|
* not as a real filesystem, but as an example of |
28 |
|
|
* how virtual filesystems can be written. |
29 |
|
|
* |
30 |
|
|
* It doesn't get much simpler than this. Consider |
31 |
|
|
* that this file implements the full semantics of |
32 |
|
|
* a POSIX-compliant read-write filesystem. |
33 |
|
|
* |
34 |
|
|
* Note in particular how the filesystem does not |
35 |
|
|
* need to implement any data structures of its own |
36 |
|
|
* to keep track of the virtual data: using the VFS |
37 |
|
|
* caches is sufficient. |
38 |
|
|
*/ |
39 |
|
|
|
40 |
|
|
#include <linux/module.h> |
41 |
|
|
#include <linux/fs.h> |
42 |
|
|
#include <linux/pagemap.h> |
43 |
|
|
#include <linux/highmem.h> |
44 |
|
|
#include <linux/init.h> |
45 |
|
|
#include <linux/string.h> |
46 |
|
|
#include <linux/smp_lock.h> |
47 |
|
|
#include <linux/backing-dev.h> |
48 |
|
|
|
49 |
|
|
#include <asm/uaccess.h> |
50 |
|
|
|
51 |
|
|
#include <linux/namei.h> |
52 |
|
|
#include <linux/version.h> |
53 |
|
|
|
54 |
|
|
static struct super_operations syaoran_ops; |
55 |
|
|
static struct address_space_operations syaoran_aops; |
56 |
|
|
static struct inode_operations syaoran_file_inode_operations; |
57 |
|
|
static struct inode_operations syaoran_dir_inode_operations; |
58 |
|
|
static struct inode_operations syaoran_symlink_inode_operations; |
59 |
|
|
static struct file_operations syaoran_file_operations; |
60 |
|
|
|
61 |
|
|
static struct backing_dev_info syaoran_backing_dev_info = { |
62 |
|
|
.ra_pages = 0, /* No readahead */ |
63 |
|
|
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12) |
64 |
|
|
.memory_backed = 1, /* Does not contribute to dirty memory */ |
65 |
|
|
#else |
66 |
|
|
.capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK | |
67 |
|
|
BDI_CAP_MAP_DIRECT | BDI_CAP_MAP_COPY | |
68 |
|
|
BDI_CAP_READ_MAP | BDI_CAP_WRITE_MAP | BDI_CAP_EXEC_MAP, |
69 |
|
|
#endif |
70 |
|
|
}; |
71 |
|
|
|
72 |
|
|
#include <linux/syaoran.h> |
73 |
|
|
|
74 |
|
|
struct inode *syaoran_get_inode(struct super_block *sb, int mode, dev_t dev) |
75 |
|
|
{ |
76 |
|
|
struct inode * inode = new_inode(sb); |
77 |
|
|
|
78 |
|
|
if (inode) { |
79 |
|
|
inode->i_mode = mode; |
80 |
|
|
inode->i_uid = current->fsuid; |
81 |
|
|
inode->i_gid = current->fsgid; |
82 |
|
|
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19) |
83 |
|
|
inode->i_blksize = PAGE_CACHE_SIZE; |
84 |
|
|
#endif |
85 |
|
|
inode->i_blocks = 0; |
86 |
|
|
inode->i_mapping->a_ops = &syaoran_aops; |
87 |
|
|
inode->i_mapping->backing_dev_info = &syaoran_backing_dev_info; |
88 |
|
|
inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
89 |
|
|
switch (mode & S_IFMT) { |
90 |
|
|
default: |
91 |
|
|
init_special_inode(inode, mode, dev); |
92 |
|
|
if (S_ISBLK(mode)) inode->i_fop = &wrapped_def_blk_fops; |
93 |
|
|
else if (S_ISCHR(mode)) inode->i_fop = &wrapped_def_chr_fops; |
94 |
|
|
inode->i_op = &syaoran_file_inode_operations; |
95 |
|
|
break; |
96 |
|
|
case S_IFREG: |
97 |
|
|
inode->i_op = &syaoran_file_inode_operations; |
98 |
|
|
inode->i_fop = &syaoran_file_operations; |
99 |
|
|
break; |
100 |
|
|
case S_IFDIR: |
101 |
|
|
inode->i_op = &syaoran_dir_inode_operations; |
102 |
|
|
inode->i_fop = &simple_dir_operations; |
103 |
|
|
|
104 |
|
|
/* directory inodes start off with i_nlink == 2 (for "." entry) */ |
105 |
|
|
inode->i_nlink++; |
106 |
|
|
break; |
107 |
|
|
case S_IFLNK: |
108 |
|
|
inode->i_op = &syaoran_symlink_inode_operations; |
109 |
|
|
break; |
110 |
|
|
} |
111 |
|
|
} |
112 |
|
|
return inode; |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
/* |
116 |
|
|
* File creation. Allocate an inode, and we're done.. |
117 |
|
|
*/ |
118 |
|
|
/* SMP-safe */ |
119 |
|
|
static int |
120 |
|
|
syaoran_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) |
121 |
|
|
{ |
122 |
|
|
struct inode * inode; |
123 |
|
|
int error = -ENOSPC; |
124 |
|
|
if (MayCreateNode(dentry, mode, dev) < 0) return -EPERM; |
125 |
|
|
inode = syaoran_get_inode(dir->i_sb, mode, dev); |
126 |
|
|
if (inode) { |
127 |
|
|
if (dir->i_mode & S_ISGID) { |
128 |
|
|
inode->i_gid = dir->i_gid; |
129 |
|
|
if (S_ISDIR(mode)) |
130 |
|
|
inode->i_mode |= S_ISGID; |
131 |
|
|
} |
132 |
|
|
d_instantiate(dentry, inode); |
133 |
|
|
dget(dentry); /* Extra count - pin the dentry in core */ |
134 |
|
|
error = 0; |
135 |
|
|
} |
136 |
|
|
return error; |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
static int syaoran_mkdir(struct inode * dir, struct dentry * dentry, int mode) |
140 |
|
|
{ |
141 |
|
|
int retval = syaoran_mknod(dir, dentry, mode | S_IFDIR, 0); |
142 |
|
|
if (!retval) |
143 |
|
|
dir->i_nlink++; |
144 |
|
|
return retval; |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
static int syaoran_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd) |
148 |
|
|
{ |
149 |
|
|
return syaoran_mknod(dir, dentry, mode | S_IFREG, 0); |
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
static int syaoran_symlink(struct inode * dir, struct dentry *dentry, const char * symname) |
153 |
|
|
{ |
154 |
|
|
struct inode *inode; |
155 |
|
|
int error = -ENOSPC; |
156 |
|
|
if (MayCreateNode(dentry, S_IFLNK, 0) < 0) return -EPERM; |
157 |
|
|
inode = syaoran_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0); |
158 |
|
|
if (inode) { |
159 |
|
|
int l = strlen(symname)+1; |
160 |
|
|
error = page_symlink(inode, symname, l); |
161 |
|
|
if (!error) { |
162 |
|
|
if (dir->i_mode & S_ISGID) |
163 |
|
|
inode->i_gid = dir->i_gid; |
164 |
|
|
d_instantiate(dentry, inode); |
165 |
|
|
dget(dentry); |
166 |
|
|
} else |
167 |
|
|
iput(inode); |
168 |
|
|
} |
169 |
|
|
return error; |
170 |
|
|
} |
171 |
|
|
|
172 |
|
|
static int syaoran_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) |
173 |
|
|
{ |
174 |
|
|
struct inode *inode = old_dentry->d_inode; |
175 |
|
|
if (!inode || MayCreateNode(dentry, inode->i_mode, inode->i_rdev) < 0) return -EPERM; |
176 |
|
|
return simple_link(old_dentry, dir, dentry); |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
static int syaoran_unlink(struct inode * dir, struct dentry *dentry) |
180 |
|
|
{ |
181 |
|
|
if (MayModifyNode(dentry, MAY_DELETE) < 0) return -EPERM; |
182 |
|
|
return simple_unlink(dir, dentry); |
183 |
|
|
} |
184 |
|
|
|
185 |
|
|
static int syaoran_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry) |
186 |
|
|
{ |
187 |
|
|
struct inode *inode = old_dentry->d_inode; |
188 |
|
|
if (!inode || MayModifyNode(old_dentry, MAY_DELETE) < 0 || MayCreateNode(new_dentry, inode->i_mode, inode->i_rdev) < 0) return -EPERM; |
189 |
|
|
return simple_rename(old_dir, old_dentry, new_dir, new_dentry); |
190 |
|
|
} |
191 |
|
|
|
192 |
|
|
static int syaoran_rmdir(struct inode *dir, struct dentry *dentry) |
193 |
|
|
{ |
194 |
|
|
if (MayModifyNode(dentry, MAY_DELETE) < 0) return -EPERM; |
195 |
|
|
return simple_rmdir(dir, dentry); |
196 |
|
|
} |
197 |
|
|
|
198 |
|
|
static int syaoran_setattr(struct dentry * dentry, struct iattr * attr) |
199 |
|
|
{ |
200 |
|
|
struct inode *inode = dentry->d_inode; |
201 |
|
|
int error = inode_change_ok(inode, attr); |
202 |
|
|
if (!error) { |
203 |
|
|
unsigned int ia_valid = attr->ia_valid; |
204 |
|
|
unsigned int flags = 0; |
205 |
|
|
if (ia_valid & (ATTR_UID | ATTR_GID)) flags |= MAY_CHOWN; |
206 |
|
|
if (ia_valid & ATTR_MODE) flags |= MAY_CHMOD; |
207 |
|
|
if (MayModifyNode(dentry, flags) < 0) return -EPERM; |
208 |
|
|
if (!error) error = inode_setattr(inode, attr); |
209 |
|
|
} |
210 |
|
|
return error; |
211 |
|
|
} |
212 |
|
|
|
213 |
|
|
static struct address_space_operations syaoran_aops = { |
214 |
|
|
.readpage = simple_readpage, |
215 |
|
|
.prepare_write = simple_prepare_write, |
216 |
|
|
.commit_write = simple_commit_write |
217 |
|
|
}; |
218 |
|
|
|
219 |
|
|
static struct file_operations syaoran_file_operations = { |
220 |
|
|
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19) |
221 |
|
|
.read = generic_file_read, |
222 |
|
|
.write = generic_file_write, |
223 |
|
|
#else |
224 |
|
|
.aio_read = generic_file_aio_read, |
225 |
|
|
.read = do_sync_read, |
226 |
|
|
.aio_write = generic_file_aio_write, |
227 |
|
|
.write = do_sync_write, |
228 |
|
|
#endif |
229 |
|
|
.mmap = generic_file_mmap, |
230 |
|
|
.fsync = simple_sync_file, |
231 |
|
|
.sendfile = generic_file_sendfile, |
232 |
|
|
.llseek = generic_file_llseek, |
233 |
|
|
}; |
234 |
|
|
|
235 |
|
|
static struct inode_operations syaoran_file_inode_operations = { |
236 |
|
|
.getattr = simple_getattr, |
237 |
|
|
.setattr = syaoran_setattr, |
238 |
|
|
}; |
239 |
|
|
|
240 |
|
|
static struct inode_operations syaoran_dir_inode_operations = { |
241 |
|
|
.create = syaoran_create, |
242 |
|
|
.lookup = simple_lookup, |
243 |
|
|
.link = syaoran_link, |
244 |
|
|
.unlink = syaoran_unlink, |
245 |
|
|
.symlink = syaoran_symlink, |
246 |
|
|
.mkdir = syaoran_mkdir, |
247 |
|
|
.rmdir = syaoran_rmdir, |
248 |
|
|
.mknod = syaoran_mknod, |
249 |
|
|
.rename = syaoran_rename, |
250 |
|
|
.setattr = syaoran_setattr, |
251 |
|
|
}; |
252 |
|
|
|
253 |
|
|
static struct inode_operations syaoran_symlink_inode_operations = { |
254 |
|
|
.readlink = generic_readlink, |
255 |
|
|
.follow_link = page_follow_link_light, |
256 |
|
|
.put_link = page_put_link, |
257 |
|
|
.setattr = syaoran_setattr, |
258 |
|
|
}; |
259 |
|
|
|
260 |
|
|
static struct super_operations syaoran_ops = { |
261 |
|
|
.statfs = simple_statfs, |
262 |
|
|
.drop_inode = generic_delete_inode, |
263 |
|
|
.put_super = syaoran_put_super, |
264 |
|
|
}; |
265 |
|
|
|
266 |
|
|
static int syaoran_fill_super(struct super_block * sb, void * data, int silent) |
267 |
|
|
{ |
268 |
|
|
struct inode * inode; |
269 |
|
|
struct dentry * root; |
270 |
|
|
|
271 |
|
|
sb->s_maxbytes = MAX_LFS_FILESIZE; |
272 |
|
|
sb->s_blocksize = PAGE_CACHE_SIZE; |
273 |
|
|
sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
274 |
|
|
sb->s_magic = SYAORAN_MAGIC; |
275 |
|
|
sb->s_op = &syaoran_ops; |
276 |
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,9) |
277 |
|
|
sb->s_time_gran = 1; |
278 |
|
|
#endif |
279 |
|
|
{ |
280 |
|
|
int error; |
281 |
|
|
if ((error = Syaoran_Initialize(sb, data)) < 0) return error; |
282 |
|
|
} |
283 |
|
|
inode = syaoran_get_inode(sb, S_IFDIR | 0755, 0); |
284 |
|
|
if (!inode) |
285 |
|
|
return -ENOMEM; |
286 |
|
|
|
287 |
|
|
root = d_alloc_root(inode); |
288 |
|
|
if (!root) { |
289 |
|
|
iput(inode); |
290 |
|
|
return -ENOMEM; |
291 |
|
|
} |
292 |
|
|
sb->s_root = root; |
293 |
|
|
MakeInitialNodes(sb); |
294 |
|
|
return 0; |
295 |
|
|
} |
296 |
|
|
|
297 |
|
|
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) |
298 |
|
|
struct super_block *syaoran_get_sb(struct file_system_type *fs_type, |
299 |
|
|
int flags, const char *dev_name, void *data) |
300 |
|
|
{ |
301 |
|
|
return get_sb_nodev(fs_type, flags, data, syaoran_fill_super); |
302 |
|
|
} |
303 |
|
|
#else |
304 |
|
|
int syaoran_get_sb(struct file_system_type *fs_type, |
305 |
|
|
int flags, const char *dev_name, void *data, struct vfsmount *mnt) |
306 |
|
|
{ |
307 |
|
|
return get_sb_nodev(fs_type, flags, data, syaoran_fill_super, mnt); |
308 |
|
|
} |
309 |
|
|
#endif |
310 |
kumaneko |
223 |
EXPORT_SYMBOL(syaoran_get_sb); |
311 |
kumaneko |
111 |
|
312 |
|
|
static struct file_system_type syaoran_fs_type = { |
313 |
|
|
.owner = THIS_MODULE, |
314 |
|
|
.name = "syaoran", |
315 |
|
|
.get_sb = syaoran_get_sb, |
316 |
|
|
.kill_sb = kill_litter_super, |
317 |
|
|
}; |
318 |
|
|
|
319 |
|
|
static int __init init_syaoran_fs(void) |
320 |
|
|
{ |
321 |
|
|
return register_filesystem(&syaoran_fs_type); |
322 |
|
|
} |
323 |
|
|
|
324 |
|
|
static void __exit exit_syaoran_fs(void) |
325 |
|
|
{ |
326 |
|
|
unregister_filesystem(&syaoran_fs_type); |
327 |
|
|
} |
328 |
|
|
|
329 |
|
|
module_init(init_syaoran_fs) |
330 |
|
|
module_exit(exit_syaoran_fs) |
331 |
|
|
|
332 |
|
|
MODULE_LICENSE("GPL"); |