1 /* $Id$
2  *
3  * This file is subject to the terms and conditions of the GNU General Public
4  * License.  See the file "COPYING" in the main directory of this archive
5  * for more details.
6  *
7  * Copyright (C) 1992 - 1997, 2000-2003 Silicon Graphics, Inc. All rights reserved.
8  */
9 #include <linux/module.h>
10 #include <linux/fs.h>
11 #include <linux/pagemap.h>
12 #include <linux/init.h>
13 #include <linux/string.h>
14 #include <asm/uaccess.h>
15 #include <asm/system.h>
16 #include <asm/machvec.h>
17 
18 /* some random number */
19 #define HWGFS_MAGIC	0x12061983
20 
21 static struct super_operations hwgfs_ops;
22 static struct address_space_operations hwgfs_aops;
23 static struct file_operations hwgfs_file_operations;
24 static struct inode_operations hwgfs_dir_inode_operations;
25 
hwgfs_statfs(struct super_block * sb,struct statfs * buf)26 static int hwgfs_statfs(struct super_block *sb, struct statfs *buf)
27 {
28 	buf->f_type = HWGFS_MAGIC;
29 	buf->f_bsize = PAGE_CACHE_SIZE;
30 	buf->f_namelen = 255;
31 	return 0;
32 }
33 
34 /*
35  * Lookup the data. This is trivial - if the dentry didn't already
36  * exist, we know it is negative.
37  */
hwgfs_lookup(struct inode * dir,struct dentry * dentry)38 static struct dentry * hwgfs_lookup(struct inode *dir, struct dentry *dentry)
39 {
40 	d_add(dentry, NULL);
41 	return NULL;
42 }
43 
44 /*
45  * Read a page. Again trivial. If it didn't already exist
46  * in the page cache, it is zero-filled.
47  */
hwgfs_readpage(struct file * file,struct page * page)48 static int hwgfs_readpage(struct file *file, struct page * page)
49 {
50 	if (!Page_Uptodate(page)) {
51 		memset(kmap(page), 0, PAGE_CACHE_SIZE);
52 		kunmap(page);
53 		flush_dcache_page(page);
54 		SetPageUptodate(page);
55 	}
56 	UnlockPage(page);
57 	return 0;
58 }
59 
hwgfs_prepare_write(struct file * file,struct page * page,unsigned offset,unsigned to)60 static int hwgfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
61 {
62 	void *addr = kmap(page);
63 	if (!Page_Uptodate(page)) {
64 		memset(addr, 0, PAGE_CACHE_SIZE);
65 		flush_dcache_page(page);
66 		SetPageUptodate(page);
67 	}
68 	SetPageDirty(page);
69 	return 0;
70 }
71 
hwgfs_commit_write(struct file * file,struct page * page,unsigned offset,unsigned to)72 static int hwgfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
73 {
74 	struct inode *inode = page->mapping->host;
75 	loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
76 
77 	kunmap(page);
78 	if (pos > inode->i_size)
79 		inode->i_size = pos;
80 	return 0;
81 }
82 
hwgfs_get_inode(struct super_block * sb,int mode,int dev)83 struct inode *hwgfs_get_inode(struct super_block *sb, int mode, int dev)
84 {
85 	struct inode * inode = new_inode(sb);
86 
87 	if (inode) {
88 		inode->i_mode = mode;
89 		inode->i_uid = current->fsuid;
90 		inode->i_gid = current->fsgid;
91 		inode->i_blksize = PAGE_CACHE_SIZE;
92 		inode->i_blocks = 0;
93 		inode->i_rdev = NODEV;
94 		inode->i_mapping->a_ops = &hwgfs_aops;
95 		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
96 		switch (mode & S_IFMT) {
97 		default:
98 			init_special_inode(inode, mode, dev);
99 			break;
100 		case S_IFREG:
101 			inode->i_fop = &hwgfs_file_operations;
102 			break;
103 		case S_IFDIR:
104 			inode->i_op = &hwgfs_dir_inode_operations;
105 			inode->i_fop = &dcache_dir_ops;
106 			break;
107 		case S_IFLNK:
108 			inode->i_op = &page_symlink_inode_operations;
109 			break;
110 		}
111 	}
112 	return inode;
113 }
114 
115 /*
116  * File creation. Allocate an inode, and we're done..
117  */
hwgfs_mknod(struct inode * dir,struct dentry * dentry,int mode,int dev)118 static int hwgfs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev)
119 {
120 	struct inode * inode = hwgfs_get_inode(dir->i_sb, mode, dev);
121 	int error = -ENOSPC;
122 
123 	if (inode) {
124 		d_instantiate(dentry, inode);
125 		dget(dentry);		/* Extra count - pin the dentry in core */
126 		error = 0;
127 	}
128 	return error;
129 }
130 
hwgfs_mkdir(struct inode * dir,struct dentry * dentry,int mode)131 static int hwgfs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
132 {
133 	return hwgfs_mknod(dir, dentry, mode | S_IFDIR, 0);
134 }
135 
hwgfs_create(struct inode * dir,struct dentry * dentry,int mode)136 static int hwgfs_create(struct inode *dir, struct dentry *dentry, int mode)
137 {
138 	return hwgfs_mknod(dir, dentry, mode | S_IFREG, 0);
139 }
140 
141 /*
142  * Link a file..
143  */
hwgfs_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)144 static int hwgfs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry)
145 {
146 	struct inode *inode = old_dentry->d_inode;
147 
148 	if (S_ISDIR(inode->i_mode))
149 		return -EPERM;
150 
151 	inode->i_nlink++;
152 	atomic_inc(&inode->i_count);	/* New dentry reference */
153 	dget(dentry);		/* Extra pinning count for the created dentry */
154 	d_instantiate(dentry, inode);
155 	return 0;
156 }
157 
hwgfs_positive(struct dentry * dentry)158 static inline int hwgfs_positive(struct dentry *dentry)
159 {
160 	return dentry->d_inode && !d_unhashed(dentry);
161 }
162 
163 /*
164  * Check that a directory is empty (this works
165  * for regular files too, they'll just always be
166  * considered empty..).
167  *
168  * Note that an empty directory can still have
169  * children, they just all have to be negative..
170  */
hwgfs_empty(struct dentry * dentry)171 static int hwgfs_empty(struct dentry *dentry)
172 {
173 	struct list_head *list;
174 
175 	spin_lock(&dcache_lock);
176 	list = dentry->d_subdirs.next;
177 
178 	while (list != &dentry->d_subdirs) {
179 		struct dentry *de = list_entry(list, struct dentry, d_child);
180 
181 		if (hwgfs_positive(de)) {
182 			spin_unlock(&dcache_lock);
183 			return 0;
184 		}
185 		list = list->next;
186 	}
187 	spin_unlock(&dcache_lock);
188 	return 1;
189 }
190 
191 /*
192  * This works for both directories and regular files.
193  * (non-directories will always have empty subdirs)
194  */
hwgfs_unlink(struct inode * dir,struct dentry * dentry)195 static int hwgfs_unlink(struct inode * dir, struct dentry *dentry)
196 {
197 	int retval = -ENOTEMPTY;
198 
199 	if (hwgfs_empty(dentry)) {
200 		struct inode *inode = dentry->d_inode;
201 
202 		inode->i_nlink--;
203 		dput(dentry);			/* Undo the count from "create" - this does all the work */
204 		retval = 0;
205 	}
206 	return retval;
207 }
208 
209 #define hwgfs_rmdir hwgfs_unlink
210 
211 /*
212  * The VFS layer already does all the dentry stuff for rename,
213  * we just have to decrement the usage count for the target if
214  * it exists so that the VFS layer correctly free's it when it
215  * gets overwritten.
216  */
hwgfs_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry)217 static int hwgfs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry)
218 {
219 	int error = -ENOTEMPTY;
220 
221 	if (hwgfs_empty(new_dentry)) {
222 		struct inode *inode = new_dentry->d_inode;
223 		if (inode) {
224 			inode->i_nlink--;
225 			dput(new_dentry);
226 		}
227 		error = 0;
228 	}
229 	return error;
230 }
231 
hwgfs_symlink(struct inode * dir,struct dentry * dentry,const char * symname)232 static int hwgfs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
233 {
234 	int error;
235 
236 	error = hwgfs_mknod(dir, dentry, S_IFLNK | S_IRWXUGO, 0);
237 	if (!error) {
238 		int l = strlen(symname)+1;
239 		struct inode *inode = dentry->d_inode;
240 		error = block_symlink(inode, symname, l);
241 	}
242 	return error;
243 }
244 
hwgfs_sync_file(struct file * file,struct dentry * dentry,int datasync)245 static int hwgfs_sync_file(struct file * file, struct dentry *dentry, int datasync)
246 {
247 	return 0;
248 }
249 
250 static struct address_space_operations hwgfs_aops = {
251 	.readpage	= hwgfs_readpage,
252 	.writepage	= fail_writepage,
253 	.prepare_write	= hwgfs_prepare_write,
254 	.commit_write	= hwgfs_commit_write
255 };
256 
257 static struct file_operations hwgfs_file_operations = {
258 	.read		= generic_file_read,
259 	.write		= generic_file_write,
260 	.mmap		= generic_file_mmap,
261 	.fsync		= hwgfs_sync_file,
262 };
263 
264 static struct inode_operations hwgfs_dir_inode_operations = {
265 	.create		= hwgfs_create,
266 	.lookup		= hwgfs_lookup,
267 	.link		= hwgfs_link,
268 	.unlink		= hwgfs_unlink,
269 	.symlink	= hwgfs_symlink,
270 	.mkdir		= hwgfs_mkdir,
271 	.rmdir		= hwgfs_rmdir,
272 	.mknod		= hwgfs_mknod,
273 	.rename		= hwgfs_rename,
274 };
275 
276 static struct super_operations hwgfs_ops = {
277 	.statfs		= hwgfs_statfs,
278 	.put_inode	= force_delete,
279 };
280 
hwgfs_read_super(struct super_block * sb,void * data,int silent)281 static struct super_block *hwgfs_read_super(struct super_block * sb, void * data, int silent)
282 {
283 	struct inode * inode;
284 	struct dentry * root;
285 
286 	sb->s_blocksize = PAGE_CACHE_SIZE;
287 	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
288 	sb->s_magic = HWGFS_MAGIC;
289 	sb->s_op = &hwgfs_ops;
290 	inode = hwgfs_get_inode(sb, S_IFDIR | 0755, 0);
291 	if (!inode)
292 		return NULL;
293 
294 	root = d_alloc_root(inode);
295 	if (!root) {
296 		iput(inode);
297 		return NULL;
298 	}
299 	sb->s_root = root;
300 	return sb;
301 }
302 
303 static struct file_system_type hwgfs_fs_type = {
304 	.owner			= THIS_MODULE,
305 	.name			= "hwgfs",
306 	.read_super		= hwgfs_read_super,
307 	.fs_flags		= FS_SINGLE|FS_LITTER,
308 };
309 
310 struct vfsmount *hwgfs_vfsmount;
311 
init_hwgfs_fs(void)312 int __init init_hwgfs_fs(void)
313 {
314 	int error;
315 
316 	if (!ia64_platform_is("sn2"))
317 		return -ENODEV;
318 
319 	error = register_filesystem(&hwgfs_fs_type);
320 	if (error)
321 		return error;
322 
323 	hwgfs_vfsmount = kern_mount(&hwgfs_fs_type);
324 	if (IS_ERR(hwgfs_vfsmount))
325 		goto fail;
326 	return 0;
327 
328 fail:
329 	unregister_filesystem(&hwgfs_fs_type);
330 	return PTR_ERR(hwgfs_vfsmount);
331 }
332 
exit_hwgfs_fs(void)333 static void __exit exit_hwgfs_fs(void)
334 {
335 	unregister_filesystem(&hwgfs_fs_type);
336 }
337 
338 MODULE_LICENSE("GPL");
339 
340 module_init(init_hwgfs_fs)
341 module_exit(exit_hwgfs_fs)
342