Update ia64 patch to 2.5.72-030619
[linux-flexiantxendom0-3.2.10.git] / arch / ia64 / sn / io / hwgfs / ramfs.c
1 /*
2  * Copyright (c) 2003 Silicon Graphics, Inc.  All Rights Reserved.
3  *
4  *  Mostly shameless copied from Linus Torvalds' ramfs and thus
5  *  Copyright (C) 2000 Linus Torvalds.
6  *                2000 Transmeta Corp.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of version 2 of the GNU General Public License as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it would be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15  *
16  * Further, this software is distributed without any warranty that it is
17  * free of the rightful claim of any third person regarding infringement
18  * or the like.  Any license provided herein, whether implied or
19  * otherwise, applies only to this software file.  Patent licenses, if
20  * any, provided herein do not apply to combinations of this program with
21  * other software, or any other product whatsoever.
22  *
23  * You should have received a copy of the GNU General Public License along
24  * with this program; if not, write the Free Software Foundation, Inc., 59
25  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
26  *
27  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
28  * Mountain View, CA  94043, or:
29  *
30  * http://www.sgi.com
31  *
32  * For further information regarding this notice, see:
33  *
34  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
35  */
36
37 #include <linux/module.h>
38 #include <linux/backing-dev.h>
39 #include <linux/fs.h>
40 #include <linux/pagemap.h>
41 #include <linux/init.h>
42 #include <linux/string.h>
43 #include <asm/uaccess.h>
44
45 /* some random number */
46 #define HWGFS_MAGIC     0x12061983
47
48 static struct super_operations hwgfs_ops;
49 static struct address_space_operations hwgfs_aops;
50 static struct file_operations hwgfs_file_operations;
51 static struct inode_operations hwgfs_file_inode_operations;
52 static struct inode_operations hwgfs_dir_inode_operations;
53
54 static struct backing_dev_info hwgfs_backing_dev_info = {
55         .ra_pages       = 0,    /* No readahead */
56         .memory_backed  = 1,    /* Does not contribute to dirty memory */
57 };
58
59 struct inode *hwgfs_get_inode(struct super_block *sb, int mode, dev_t dev)
60 {
61         struct inode * inode = new_inode(sb);
62
63         if (inode) {
64                 inode->i_mode = mode;
65                 inode->i_uid = current->fsuid;
66                 inode->i_gid = current->fsgid;
67                 inode->i_blksize = PAGE_CACHE_SIZE;
68                 inode->i_blocks = 0;
69                 inode->i_rdev = NODEV;
70                 inode->i_mapping->a_ops = &hwgfs_aops;
71                 inode->i_mapping->backing_dev_info = &hwgfs_backing_dev_info;
72                 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
73                 switch (mode & S_IFMT) {
74                 default:
75                         init_special_inode(inode, mode, dev);
76                         break;
77                 case S_IFREG:
78                         inode->i_op = &hwgfs_file_inode_operations;
79                         inode->i_fop = &hwgfs_file_operations;
80                         break;
81                 case S_IFDIR:
82                         inode->i_op = &hwgfs_dir_inode_operations;
83                         inode->i_fop = &simple_dir_operations;
84                         inode->i_nlink++;
85                         break;
86                 case S_IFLNK:
87                         inode->i_op = &page_symlink_inode_operations;
88                         break;
89                 }
90         }
91         return inode;
92 }
93
94 static int hwgfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
95 {
96         struct inode * inode = hwgfs_get_inode(dir->i_sb, mode, dev);
97         int error = -ENOSPC;
98
99         if (inode) {
100                 d_instantiate(dentry, inode);
101                 dget(dentry);           /* Extra count - pin the dentry in core */
102                 error = 0;
103         }
104         return error;
105 }
106
107 static int hwgfs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
108 {
109         return hwgfs_mknod(dir, dentry, mode | S_IFDIR, 0);
110 }
111
112 static int hwgfs_create(struct inode *dir, struct dentry *dentry, int mode)
113 {
114         return hwgfs_mknod(dir, dentry, mode | S_IFREG, 0);
115 }
116
117 static int hwgfs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
118 {
119         struct inode *inode;
120         int error = -ENOSPC;
121
122         inode = hwgfs_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0);
123         if (inode) {
124                 int l = strlen(symname)+1;
125                 error = page_symlink(inode, symname, l);
126                 if (!error) {
127                         d_instantiate(dentry, inode);
128                         dget(dentry);
129                 } else
130                         iput(inode);
131         }
132         return error;
133 }
134
135 static struct address_space_operations hwgfs_aops = {
136         .readpage       = simple_readpage,
137         .prepare_write  = simple_prepare_write,
138         .commit_write   = simple_commit_write
139 };
140
141 static struct file_operations hwgfs_file_operations = {
142         .read           = generic_file_read,
143         .write          = generic_file_write,
144         .mmap           = generic_file_mmap,
145         .fsync          = simple_sync_file,
146         .sendfile       = generic_file_sendfile,
147 };
148
149 static struct inode_operations hwgfs_file_inode_operations = {
150         .getattr        = simple_getattr,
151 };
152
153 static struct inode_operations hwgfs_dir_inode_operations = {
154         .create         = hwgfs_create,
155         .lookup         = simple_lookup,
156         .link           = simple_link,
157         .unlink         = simple_unlink,
158         .symlink        = hwgfs_symlink,
159         .mkdir          = hwgfs_mkdir,
160         .rmdir          = simple_rmdir,
161         .mknod          = hwgfs_mknod,
162         .rename         = simple_rename,
163 };
164
165 static struct super_operations hwgfs_ops = {
166         .statfs         = simple_statfs,
167         .drop_inode     = generic_delete_inode,
168 };
169
170 static int hwgfs_fill_super(struct super_block * sb, void * data, int silent)
171 {
172         struct inode * inode;
173         struct dentry * root;
174
175         sb->s_blocksize = PAGE_CACHE_SIZE;
176         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
177         sb->s_magic = HWGFS_MAGIC;
178         sb->s_op = &hwgfs_ops;
179         inode = hwgfs_get_inode(sb, S_IFDIR | 0755, 0);
180         if (!inode)
181                 return -ENOMEM;
182
183         root = d_alloc_root(inode);
184         if (!root) {
185                 iput(inode);
186                 return -ENOMEM;
187         }
188         sb->s_root = root;
189         return 0;
190 }
191
192 static struct super_block *hwgfs_get_sb(struct file_system_type *fs_type,
193         int flags, char *dev_name, void *data)
194 {
195         return get_sb_single(fs_type, flags, data, hwgfs_fill_super);
196 }
197
198 static struct file_system_type hwgfs_fs_type = {
199         .owner          = THIS_MODULE,
200         .name           = "hwgfs",
201         .get_sb         = hwgfs_get_sb,
202         .kill_sb        = kill_litter_super,
203 };
204
205 struct vfsmount *hwgfs_vfsmount;
206
207 int __init init_hwgfs_fs(void)
208 {
209         int error;
210
211         error = register_filesystem(&hwgfs_fs_type);
212         if (error)
213                 return error;
214
215         hwgfs_vfsmount = kern_mount(&hwgfs_fs_type);
216         if (IS_ERR(hwgfs_vfsmount))
217                 goto fail;
218         return 0;
219
220 fail:
221         unregister_filesystem(&hwgfs_fs_type);
222         return PTR_ERR(hwgfs_vfsmount);
223 }
224
225 static void __exit exit_hwgfs_fs(void)
226 {
227         unregister_filesystem(&hwgfs_fs_type);
228 }
229
230 MODULE_LICENSE("GPL");
231
232 module_init(init_hwgfs_fs)
233 module_exit(exit_hwgfs_fs)