4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
6 * Author: Arnd Bergmann <arndb@de.ibm.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/file.h>
25 #include <linux/backing-dev.h>
26 #include <linux/init.h>
27 #include <linux/ioctl.h>
28 #include <linux/module.h>
29 #include <linux/namei.h>
30 #include <linux/pagemap.h>
31 #include <linux/poll.h>
32 #include <linux/slab.h>
33 #include <linux/parser.h>
36 #include <asm/semaphore.h>
38 #include <asm/uaccess.h>
42 static kmem_cache_t *spufs_inode_cache;
45 spufs_alloc_inode(struct super_block *sb)
47 struct spufs_inode_info *ei;
49 ei = kmem_cache_alloc(spufs_inode_cache, SLAB_KERNEL);
52 return &ei->vfs_inode;
56 spufs_destroy_inode(struct inode *inode)
58 kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
62 spufs_init_once(void *p, kmem_cache_t * cachep, unsigned long flags)
64 struct spufs_inode_info *ei = p;
66 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
67 SLAB_CTOR_CONSTRUCTOR) {
68 inode_init_once(&ei->vfs_inode);
73 spufs_new_inode(struct super_block *sb, int mode)
77 inode = new_inode(sb);
82 inode->i_uid = current->fsuid;
83 inode->i_gid = current->fsgid;
84 inode->i_blksize = PAGE_CACHE_SIZE;
86 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
92 spufs_setattr(struct dentry *dentry, struct iattr *attr)
94 struct inode *inode = dentry->d_inode;
96 if ((attr->ia_valid & ATTR_SIZE) &&
97 (attr->ia_size != inode->i_size))
99 return inode_setattr(inode, attr);
104 spufs_new_file(struct super_block *sb, struct dentry *dentry,
105 struct file_operations *fops, int mode,
106 struct spu_context *ctx)
108 static struct inode_operations spufs_file_iops = {
109 .setattr = spufs_setattr,
115 inode = spufs_new_inode(sb, S_IFREG | mode);
120 inode->i_op = &spufs_file_iops;
122 inode->u.generic_ip = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
123 d_add(dentry, inode);
129 spufs_delete_inode(struct inode *inode)
131 if (SPUFS_I(inode)->i_ctx)
132 put_spu_context(SPUFS_I(inode)->i_ctx);
137 spufs_fill_dir(struct dentry *dir, struct tree_descr *files,
138 int mode, struct spu_context *ctx)
140 struct dentry *dentry;
143 while (files->name && files->name[0]) {
145 dentry = d_alloc_name(dir, files->name);
148 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
149 files->mode & mode, ctx);
156 // FIXME: remove all files that are left
161 static int spufs_rmdir(struct inode *root, struct dentry *dir_dentry)
163 struct dentry *dentry, *tmp;
164 struct spu_context *ctx;
167 /* remove all entries */
169 list_for_each_entry_safe(dentry, tmp, &dir_dentry->d_subdirs, d_child) {
170 spin_lock(&dcache_lock);
171 spin_lock(&dentry->d_lock);
172 if (!(d_unhashed(dentry)) && dentry->d_inode) {
175 spin_unlock(&dentry->d_lock);
176 simple_unlink(dir_dentry->d_inode, dentry);
177 spin_unlock(&dcache_lock);
180 spin_unlock(&dentry->d_lock);
181 spin_unlock(&dcache_lock);
185 /* We have to give up the mm_struct */
186 ctx = SPUFS_I(dir_dentry->d_inode)->i_ctx;
190 shrink_dcache_parent(dir_dentry);
191 err = simple_rmdir(root, dir_dentry);
196 static int spufs_dir_close(struct inode *inode, struct file *file)
199 struct dentry *dentry;
202 dentry = file->f_dentry;
203 dir = dentry->d_parent->d_inode;
205 ret = spufs_rmdir(dir, file->f_dentry);
208 return dcache_dir_close(inode, file);
211 struct inode_operations spufs_dir_inode_operations = {
212 .lookup = simple_lookup,
215 struct file_operations spufs_autodelete_dir_operations = {
216 .open = dcache_dir_open,
217 .release = spufs_dir_close,
218 .llseek = dcache_dir_lseek,
219 .read = generic_read_dir,
220 .readdir = dcache_readdir,
221 .fsync = simple_sync_file,
225 spufs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
229 struct spu_context *ctx;
232 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
236 if (dir->i_mode & S_ISGID) {
237 inode->i_gid = dir->i_gid;
238 inode->i_mode &= S_ISGID;
240 ctx = alloc_spu_context(inode->i_mapping);
241 SPUFS_I(inode)->i_ctx = ctx;
245 inode->i_op = &spufs_dir_inode_operations;
246 inode->i_fop = &simple_dir_operations;
247 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
251 d_instantiate(dentry, inode);
257 put_spu_context(ctx);
265 spufs_create_thread(struct nameidata *nd, const char *name,
266 unsigned int flags, mode_t mode)
268 struct dentry *dentry;
272 /* need to be at the root of spufs */
274 if (nd->dentry->d_sb->s_magic != SPUFS_MAGIC ||
275 nd->dentry != nd->dentry->d_sb->s_root)
278 dentry = lookup_create(nd, 1);
279 ret = PTR_ERR(dentry);
287 mode &= ~current->fs->umask;
288 ret = spufs_mkdir(nd->dentry->d_inode, dentry, mode & S_IRWXUGO);
292 ret = get_unused_fd();
296 dentry->d_inode->i_nlink++;
298 filp = filp_open(name, O_RDONLY, mode);
300 // FIXME: remove directory again
304 filp->f_op = &spufs_autodelete_dir_operations;
305 fd_install(ret, filp);
311 up(&nd->dentry->d_inode->i_sem);
316 /* File system initialization */
318 Opt_uid, Opt_gid, Opt_err,
321 static match_table_t spufs_tokens = {
322 { Opt_uid, "uid=%d" },
323 { Opt_gid, "gid=%d" },
328 spufs_parse_options(char *options, struct inode *root)
331 substring_t args[MAX_OPT_ARGS];
333 while ((p = strsep(&options, ",")) != NULL) {
339 token = match_token(p, spufs_tokens, args);
342 if (match_int(&args[0], &option))
344 root->i_uid = option;
347 if (match_int(&args[0], &option))
349 root->i_gid = option;
359 spufs_create_root(struct super_block *sb, void *data)
365 inode = spufs_new_inode(sb, S_IFDIR | 0775);
369 inode->i_op = &spufs_dir_inode_operations;
370 inode->i_fop = &simple_dir_operations;
371 SPUFS_I(inode)->i_ctx = NULL;
374 if (!spufs_parse_options(data, inode))
378 sb->s_root = d_alloc_root(inode);
390 spufs_fill_super(struct super_block *sb, void *data, int silent)
392 static struct super_operations s_ops = {
393 .alloc_inode = spufs_alloc_inode,
394 .destroy_inode = spufs_destroy_inode,
395 .statfs = simple_statfs,
396 .delete_inode = spufs_delete_inode,
397 .drop_inode = generic_delete_inode,
400 sb->s_maxbytes = MAX_LFS_FILESIZE;
401 sb->s_blocksize = PAGE_CACHE_SIZE;
402 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
403 sb->s_magic = SPUFS_MAGIC;
406 return spufs_create_root(sb, data);
409 static struct super_block *
410 spufs_get_sb(struct file_system_type *fstype, int flags,
411 const char *name, void *data)
413 return get_sb_single(fstype, flags, data, spufs_fill_super);
416 static struct file_system_type spufs_type = {
417 .owner = THIS_MODULE,
419 .get_sb = spufs_get_sb,
420 .kill_sb = kill_litter_super,
423 static int spufs_init(void)
427 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
428 sizeof(struct spufs_inode_info), 0,
429 SLAB_HWCACHE_ALIGN, spufs_init_once, NULL);
431 if (!spufs_inode_cache)
433 if (spu_sched_init() != 0) {
434 kmem_cache_destroy(spufs_inode_cache);
437 ret = register_filesystem(&spufs_type);
440 ret = register_spu_syscalls(&spufs_calls);
445 unregister_filesystem(&spufs_type);
447 kmem_cache_destroy(spufs_inode_cache);
451 module_init(spufs_init);
453 static void spufs_exit(void)
456 unregister_spu_syscalls(&spufs_calls);
457 unregister_filesystem(&spufs_type);
458 kmem_cache_destroy(spufs_inode_cache);
460 module_exit(spufs_exit);
462 MODULE_LICENSE("GPL");
463 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");