1 |
/* |
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 |
* Version: 1.5.2-pre 2007/10/19 |
9 |
* |
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 |
#include <linux/sched.h> |
54 |
|
55 |
static struct super_operations syaoran_ops; |
56 |
static struct address_space_operations syaoran_aops; |
57 |
static struct inode_operations syaoran_file_inode_operations; |
58 |
static struct inode_operations syaoran_dir_inode_operations; |
59 |
static struct inode_operations syaoran_symlink_inode_operations; |
60 |
static struct file_operations syaoran_file_operations; |
61 |
|
62 |
static struct backing_dev_info syaoran_backing_dev_info = { |
63 |
.ra_pages = 0, /* No readahead */ |
64 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12) |
65 |
.memory_backed = 1, /* Does not contribute to dirty memory */ |
66 |
#else |
67 |
.capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK | |
68 |
BDI_CAP_MAP_DIRECT | BDI_CAP_MAP_COPY | |
69 |
BDI_CAP_READ_MAP | BDI_CAP_WRITE_MAP | BDI_CAP_EXEC_MAP, |
70 |
#endif |
71 |
}; |
72 |
|
73 |
#include <linux/syaoran.h> |
74 |
|
75 |
struct inode *syaoran_get_inode(struct super_block *sb, int mode, dev_t dev) |
76 |
{ |
77 |
struct inode * inode = new_inode(sb); |
78 |
|
79 |
if (inode) { |
80 |
inode->i_mode = mode; |
81 |
inode->i_uid = current->fsuid; |
82 |
inode->i_gid = current->fsgid; |
83 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19) |
84 |
inode->i_blksize = PAGE_CACHE_SIZE; |
85 |
#endif |
86 |
inode->i_blocks = 0; |
87 |
inode->i_mapping->a_ops = &syaoran_aops; |
88 |
inode->i_mapping->backing_dev_info = &syaoran_backing_dev_info; |
89 |
inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
90 |
switch (mode & S_IFMT) { |
91 |
default: |
92 |
init_special_inode(inode, mode, dev); |
93 |
if (S_ISBLK(mode)) inode->i_fop = &wrapped_def_blk_fops; |
94 |
else if (S_ISCHR(mode)) inode->i_fop = &wrapped_def_chr_fops; |
95 |
inode->i_op = &syaoran_file_inode_operations; |
96 |
break; |
97 |
case S_IFREG: |
98 |
inode->i_op = &syaoran_file_inode_operations; |
99 |
inode->i_fop = &syaoran_file_operations; |
100 |
break; |
101 |
case S_IFDIR: |
102 |
inode->i_op = &syaoran_dir_inode_operations; |
103 |
inode->i_fop = &simple_dir_operations; |
104 |
|
105 |
/* directory inodes start off with i_nlink == 2 (for "." entry) */ |
106 |
inode->i_nlink++; |
107 |
break; |
108 |
case S_IFLNK: |
109 |
inode->i_op = &syaoran_symlink_inode_operations; |
110 |
break; |
111 |
} |
112 |
} |
113 |
return inode; |
114 |
} |
115 |
|
116 |
/* |
117 |
* File creation. Allocate an inode, and we're done.. |
118 |
*/ |
119 |
/* SMP-safe */ |
120 |
static int |
121 |
syaoran_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) |
122 |
{ |
123 |
struct inode * inode; |
124 |
int error = -ENOSPC; |
125 |
if (MayCreateNode(dentry, mode, dev) < 0) return -EPERM; |
126 |
inode = syaoran_get_inode(dir->i_sb, mode, dev); |
127 |
if (inode) { |
128 |
if (dir->i_mode & S_ISGID) { |
129 |
inode->i_gid = dir->i_gid; |
130 |
if (S_ISDIR(mode)) |
131 |
inode->i_mode |= S_ISGID; |
132 |
} |
133 |
d_instantiate(dentry, inode); |
134 |
dget(dentry); /* Extra count - pin the dentry in core */ |
135 |
error = 0; |
136 |
} |
137 |
return error; |
138 |
} |
139 |
|
140 |
static int syaoran_mkdir(struct inode * dir, struct dentry * dentry, int mode) |
141 |
{ |
142 |
int retval = syaoran_mknod(dir, dentry, mode | S_IFDIR, 0); |
143 |
if (!retval) |
144 |
dir->i_nlink++; |
145 |
return retval; |
146 |
} |
147 |
|
148 |
static int syaoran_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd) |
149 |
{ |
150 |
return syaoran_mknod(dir, dentry, mode | S_IFREG, 0); |
151 |
} |
152 |
|
153 |
static int syaoran_symlink(struct inode * dir, struct dentry *dentry, const char * symname) |
154 |
{ |
155 |
struct inode *inode; |
156 |
int error = -ENOSPC; |
157 |
if (MayCreateNode(dentry, S_IFLNK, 0) < 0) return -EPERM; |
158 |
inode = syaoran_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0); |
159 |
if (inode) { |
160 |
int l = strlen(symname)+1; |
161 |
error = page_symlink(inode, symname, l); |
162 |
if (!error) { |
163 |
if (dir->i_mode & S_ISGID) |
164 |
inode->i_gid = dir->i_gid; |
165 |
d_instantiate(dentry, inode); |
166 |
dget(dentry); |
167 |
} else |
168 |
iput(inode); |
169 |
} |
170 |
return error; |
171 |
} |
172 |
|
173 |
static int syaoran_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) |
174 |
{ |
175 |
struct inode *inode = old_dentry->d_inode; |
176 |
if (!inode || MayCreateNode(dentry, inode->i_mode, inode->i_rdev) < 0) return -EPERM; |
177 |
return simple_link(old_dentry, dir, dentry); |
178 |
} |
179 |
|
180 |
static int syaoran_unlink(struct inode * dir, struct dentry *dentry) |
181 |
{ |
182 |
if (MayModifyNode(dentry, MAY_DELETE) < 0) return -EPERM; |
183 |
return simple_unlink(dir, dentry); |
184 |
} |
185 |
|
186 |
static int syaoran_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry) |
187 |
{ |
188 |
struct inode *inode = old_dentry->d_inode; |
189 |
if (!inode || MayModifyNode(old_dentry, MAY_DELETE) < 0 || MayCreateNode(new_dentry, inode->i_mode, inode->i_rdev) < 0) return -EPERM; |
190 |
return simple_rename(old_dir, old_dentry, new_dir, new_dentry); |
191 |
} |
192 |
|
193 |
static int syaoran_rmdir(struct inode *dir, struct dentry *dentry) |
194 |
{ |
195 |
if (MayModifyNode(dentry, MAY_DELETE) < 0) return -EPERM; |
196 |
return simple_rmdir(dir, dentry); |
197 |
} |
198 |
|
199 |
static int syaoran_setattr(struct dentry * dentry, struct iattr * attr) |
200 |
{ |
201 |
struct inode *inode = dentry->d_inode; |
202 |
int error = inode_change_ok(inode, attr); |
203 |
if (!error) { |
204 |
unsigned int ia_valid = attr->ia_valid; |
205 |
unsigned int flags = 0; |
206 |
if (ia_valid & (ATTR_UID | ATTR_GID)) flags |= MAY_CHOWN; |
207 |
if (ia_valid & ATTR_MODE) flags |= MAY_CHMOD; |
208 |
if (MayModifyNode(dentry, flags) < 0) return -EPERM; |
209 |
if (!error) error = inode_setattr(inode, attr); |
210 |
} |
211 |
return error; |
212 |
} |
213 |
|
214 |
static struct address_space_operations syaoran_aops = { |
215 |
.readpage = simple_readpage, |
216 |
.prepare_write = simple_prepare_write, |
217 |
.commit_write = simple_commit_write |
218 |
}; |
219 |
|
220 |
static struct file_operations syaoran_file_operations = { |
221 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19) |
222 |
.read = generic_file_read, |
223 |
.write = generic_file_write, |
224 |
#else |
225 |
.aio_read = generic_file_aio_read, |
226 |
.read = do_sync_read, |
227 |
.aio_write = generic_file_aio_write, |
228 |
.write = do_sync_write, |
229 |
#endif |
230 |
.mmap = generic_file_mmap, |
231 |
.fsync = simple_sync_file, |
232 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23) |
233 |
.sendfile = generic_file_sendfile, |
234 |
#else |
235 |
.splice_read = generic_file_splice_read, |
236 |
#endif |
237 |
.llseek = generic_file_llseek, |
238 |
}; |
239 |
|
240 |
static struct inode_operations syaoran_file_inode_operations = { |
241 |
.getattr = simple_getattr, |
242 |
.setattr = syaoran_setattr, |
243 |
}; |
244 |
|
245 |
static struct inode_operations syaoran_dir_inode_operations = { |
246 |
.create = syaoran_create, |
247 |
.lookup = simple_lookup, |
248 |
.link = syaoran_link, |
249 |
.unlink = syaoran_unlink, |
250 |
.symlink = syaoran_symlink, |
251 |
.mkdir = syaoran_mkdir, |
252 |
.rmdir = syaoran_rmdir, |
253 |
.mknod = syaoran_mknod, |
254 |
.rename = syaoran_rename, |
255 |
.setattr = syaoran_setattr, |
256 |
}; |
257 |
|
258 |
static struct inode_operations syaoran_symlink_inode_operations = { |
259 |
.readlink = generic_readlink, |
260 |
.follow_link = page_follow_link_light, |
261 |
.put_link = page_put_link, |
262 |
.setattr = syaoran_setattr, |
263 |
}; |
264 |
|
265 |
static struct super_operations syaoran_ops = { |
266 |
.statfs = simple_statfs, |
267 |
.drop_inode = generic_delete_inode, |
268 |
.put_super = syaoran_put_super, |
269 |
}; |
270 |
|
271 |
static int syaoran_fill_super(struct super_block * sb, void * data, int silent) |
272 |
{ |
273 |
struct inode * inode; |
274 |
struct dentry * root; |
275 |
|
276 |
sb->s_maxbytes = MAX_LFS_FILESIZE; |
277 |
sb->s_blocksize = PAGE_CACHE_SIZE; |
278 |
sb->s_blocksize_bits = PAGE_CACHE_SHIFT; |
279 |
sb->s_magic = SYAORAN_MAGIC; |
280 |
sb->s_op = &syaoran_ops; |
281 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,9) |
282 |
sb->s_time_gran = 1; |
283 |
#endif |
284 |
{ |
285 |
int error; |
286 |
if ((error = Syaoran_Initialize(sb, data)) < 0) return error; |
287 |
} |
288 |
inode = syaoran_get_inode(sb, S_IFDIR | 0755, 0); |
289 |
if (!inode) |
290 |
return -ENOMEM; |
291 |
|
292 |
root = d_alloc_root(inode); |
293 |
if (!root) { |
294 |
iput(inode); |
295 |
return -ENOMEM; |
296 |
} |
297 |
sb->s_root = root; |
298 |
MakeInitialNodes(sb); |
299 |
return 0; |
300 |
} |
301 |
|
302 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) |
303 |
struct super_block *syaoran_get_sb(struct file_system_type *fs_type, |
304 |
int flags, const char *dev_name, void *data) |
305 |
{ |
306 |
return get_sb_nodev(fs_type, flags, data, syaoran_fill_super); |
307 |
} |
308 |
#else |
309 |
int syaoran_get_sb(struct file_system_type *fs_type, |
310 |
int flags, const char *dev_name, void *data, struct vfsmount *mnt) |
311 |
{ |
312 |
return get_sb_nodev(fs_type, flags, data, syaoran_fill_super, mnt); |
313 |
} |
314 |
#endif |
315 |
EXPORT_SYMBOL(syaoran_get_sb); |
316 |
|
317 |
static struct file_system_type syaoran_fs_type = { |
318 |
.owner = THIS_MODULE, |
319 |
.name = "syaoran", |
320 |
.get_sb = syaoran_get_sb, |
321 |
.kill_sb = kill_litter_super, |
322 |
}; |
323 |
|
324 |
static int __init init_syaoran_fs(void) |
325 |
{ |
326 |
return register_filesystem(&syaoran_fs_type); |
327 |
} |
328 |
|
329 |
static void __exit exit_syaoran_fs(void) |
330 |
{ |
331 |
unregister_filesystem(&syaoran_fs_type); |
332 |
} |
333 |
|
334 |
module_init(init_syaoran_fs) |
335 |
module_exit(exit_syaoran_fs) |
336 |
|
337 |
MODULE_LICENSE("GPL"); |