ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ia64 / sn / io / hwgfs / ramfs.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (c) 2003 Silicon Graphics, Inc.  All Rights Reserved.
7  *
8  *  Mostly shameless copied from Linus Torvalds' ramfs and thus
9  *  Copyright (C) 2000 Linus Torvalds.
10  *                2000 Transmeta Corp.
11  */
12
13 #include <linux/module.h>
14 #include <linux/backing-dev.h>
15 #include <linux/fs.h>
16 #include <linux/pagemap.h>
17 #include <linux/init.h>
18 #include <linux/string.h>
19 #include <asm/uaccess.h>
20
21 /* some random number */
22 #define HWGFS_MAGIC     0x12061983
23
24 static struct super_operations hwgfs_ops;
25 static struct address_space_operations hwgfs_aops;
26 static struct file_operations hwgfs_file_operations;
27 static struct inode_operations hwgfs_file_inode_operations;
28 static struct inode_operations hwgfs_dir_inode_operations;
29
30 static struct backing_dev_info hwgfs_backing_dev_info = {
31         .ra_pages       = 0,    /* No readahead */
32         .memory_backed  = 1,    /* Does not contribute to dirty memory */
33 };
34
35 static struct inode *hwgfs_get_inode(struct super_block *sb, int mode, dev_t dev)
36 {
37         struct inode * inode = new_inode(sb);
38
39         if (inode) {
40                 inode->i_mode = mode;
41                 inode->i_uid = current->fsuid;
42                 inode->i_gid = current->fsgid;
43                 inode->i_blksize = PAGE_CACHE_SIZE;
44                 inode->i_blocks = 0;
45                 inode->i_mapping->a_ops = &hwgfs_aops;
46                 inode->i_mapping->backing_dev_info = &hwgfs_backing_dev_info;
47                 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
48                 switch (mode & S_IFMT) {
49                 default:
50                         init_special_inode(inode, mode, dev);
51                         break;
52                 case S_IFREG:
53                         inode->i_op = &hwgfs_file_inode_operations;
54                         inode->i_fop = &hwgfs_file_operations;
55                         break;
56                 case S_IFDIR:
57                         inode->i_op = &hwgfs_dir_inode_operations;
58                         inode->i_fop = &simple_dir_operations;
59                         inode->i_nlink++;
60                         break;
61                 case S_IFLNK:
62                         inode->i_op = &page_symlink_inode_operations;
63                         break;
64                 }
65         }
66         return inode;
67 }
68
69 static int hwgfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
70 {
71         struct inode * inode = hwgfs_get_inode(dir->i_sb, mode, dev);
72         int error = -ENOSPC;
73
74         if (inode) {
75                 d_instantiate(dentry, inode);
76                 dget(dentry);           /* Extra count - pin the dentry in core */
77                 error = 0;
78         }
79         return error;
80 }
81
82 static int hwgfs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
83 {
84         return hwgfs_mknod(dir, dentry, mode | S_IFDIR, 0);
85 }
86
87 static int hwgfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *unused)
88 {
89         return hwgfs_mknod(dir, dentry, mode | S_IFREG, 0);
90 }
91
92 static int hwgfs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
93 {
94         struct inode *inode;
95         int error = -ENOSPC;
96
97         inode = hwgfs_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0);
98         if (inode) {
99                 int l = strlen(symname)+1;
100                 error = page_symlink(inode, symname, l);
101                 if (!error) {
102                         d_instantiate(dentry, inode);
103                         dget(dentry);
104                 } else
105                         iput(inode);
106         }
107         return error;
108 }
109
110 static struct address_space_operations hwgfs_aops = {
111         .readpage       = simple_readpage,
112         .prepare_write  = simple_prepare_write,
113         .commit_write   = simple_commit_write
114 };
115
116 static struct file_operations hwgfs_file_operations = {
117         .read           = generic_file_read,
118         .write          = generic_file_write,
119         .mmap           = generic_file_mmap,
120         .fsync          = simple_sync_file,
121         .sendfile       = generic_file_sendfile,
122 };
123
124 static struct inode_operations hwgfs_file_inode_operations = {
125         .getattr        = simple_getattr,
126 };
127
128 static struct inode_operations hwgfs_dir_inode_operations = {
129         .create         = hwgfs_create,
130         .lookup         = simple_lookup,
131         .link           = simple_link,
132         .unlink         = simple_unlink,
133         .symlink        = hwgfs_symlink,
134         .mkdir          = hwgfs_mkdir,
135         .rmdir          = simple_rmdir,
136         .mknod          = hwgfs_mknod,
137         .rename         = simple_rename,
138 };
139
140 static struct super_operations hwgfs_ops = {
141         .statfs         = simple_statfs,
142         .drop_inode     = generic_delete_inode,
143 };
144
145 static int hwgfs_fill_super(struct super_block * sb, void * data, int silent)
146 {
147         struct inode * inode;
148         struct dentry * root;
149
150         sb->s_blocksize = PAGE_CACHE_SIZE;
151         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
152         sb->s_magic = HWGFS_MAGIC;
153         sb->s_op = &hwgfs_ops;
154         inode = hwgfs_get_inode(sb, S_IFDIR | 0755, 0);
155         if (!inode)
156                 return -ENOMEM;
157
158         root = d_alloc_root(inode);
159         if (!root) {
160                 iput(inode);
161                 return -ENOMEM;
162         }
163         sb->s_root = root;
164         return 0;
165 }
166
167 static struct super_block *hwgfs_get_sb(struct file_system_type *fs_type,
168         int flags, const char *dev_name, void *data)
169 {
170         return get_sb_single(fs_type, flags, data, hwgfs_fill_super);
171 }
172
173 static struct file_system_type hwgfs_fs_type = {
174         .owner          = THIS_MODULE,
175         .name           = "hwgfs",
176         .get_sb         = hwgfs_get_sb,
177         .kill_sb        = kill_litter_super,
178 };
179
180 struct vfsmount *hwgfs_vfsmount;
181
182 int __init init_hwgfs_fs(void)
183 {
184         int error;
185
186         error = register_filesystem(&hwgfs_fs_type);
187         if (error)
188                 return error;
189
190         hwgfs_vfsmount = kern_mount(&hwgfs_fs_type);
191         if (IS_ERR(hwgfs_vfsmount))
192                 goto fail;
193         return 0;
194
195 fail:
196         unregister_filesystem(&hwgfs_fs_type);
197         return PTR_ERR(hwgfs_vfsmount);
198 }
199
200 static void __exit exit_hwgfs_fs(void)
201 {
202         unregister_filesystem(&hwgfs_fs_type);
203 }
204
205 MODULE_LICENSE("GPL");
206
207 module_init(init_hwgfs_fs)
208 module_exit(exit_hwgfs_fs)