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/mount.h>
30 #include <linux/namei.h>
31 #include <linux/pagemap.h>
32 #include <linux/poll.h>
33 #include <linux/slab.h>
34 #include <linux/parser.h>
37 #include <asm/semaphore.h>
39 #include <asm/uaccess.h>
43 static struct kmem_cache *spufs_inode_cache;
44 char *isolated_loader;
47 spufs_alloc_inode(struct super_block *sb)
49 struct spufs_inode_info *ei;
51 ei = kmem_cache_alloc(spufs_inode_cache, GFP_KERNEL);
58 return &ei->vfs_inode;
62 spufs_destroy_inode(struct inode *inode)
64 kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
68 spufs_init_once(void *p, struct kmem_cache * cachep, unsigned long flags)
70 struct spufs_inode_info *ei = p;
72 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
73 SLAB_CTOR_CONSTRUCTOR) {
74 inode_init_once(&ei->vfs_inode);
79 spufs_new_inode(struct super_block *sb, int mode)
83 inode = new_inode(sb);
88 inode->i_uid = current->fsuid;
89 inode->i_gid = current->fsgid;
91 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
97 spufs_setattr(struct dentry *dentry, struct iattr *attr)
99 struct inode *inode = dentry->d_inode;
101 if ((attr->ia_valid & ATTR_SIZE) &&
102 (attr->ia_size != inode->i_size))
104 return inode_setattr(inode, attr);
109 spufs_new_file(struct super_block *sb, struct dentry *dentry,
110 const struct file_operations *fops, int mode,
111 struct spu_context *ctx)
113 static struct inode_operations spufs_file_iops = {
114 .setattr = spufs_setattr,
120 inode = spufs_new_inode(sb, S_IFREG | mode);
125 inode->i_op = &spufs_file_iops;
127 inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
128 d_add(dentry, inode);
134 spufs_delete_inode(struct inode *inode)
136 struct spufs_inode_info *ei = SPUFS_I(inode);
139 put_spu_context(ei->i_ctx);
141 put_spu_gang(ei->i_gang);
145 static void spufs_prune_dir(struct dentry *dir)
147 struct dentry *dentry, *tmp;
149 mutex_lock(&dir->d_inode->i_mutex);
150 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
151 spin_lock(&dcache_lock);
152 spin_lock(&dentry->d_lock);
153 if (!(d_unhashed(dentry)) && dentry->d_inode) {
156 spin_unlock(&dentry->d_lock);
157 simple_unlink(dir->d_inode, dentry);
158 spin_unlock(&dcache_lock);
161 spin_unlock(&dentry->d_lock);
162 spin_unlock(&dcache_lock);
165 shrink_dcache_parent(dir);
166 mutex_unlock(&dir->d_inode->i_mutex);
169 /* Caller must hold parent->i_mutex */
170 static int spufs_rmdir(struct inode *parent, struct dentry *dir)
172 /* remove all entries */
173 spufs_prune_dir(dir);
175 return simple_rmdir(parent, dir);
178 static int spufs_fill_dir(struct dentry *dir, struct tree_descr *files,
179 int mode, struct spu_context *ctx)
181 struct dentry *dentry;
184 while (files->name && files->name[0]) {
186 dentry = d_alloc_name(dir, files->name);
189 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
190 files->mode & mode, ctx);
197 spufs_prune_dir(dir);
201 static int spufs_dir_close(struct inode *inode, struct file *file)
203 struct spu_context *ctx;
204 struct inode *parent;
208 dir = file->f_path.dentry;
209 parent = dir->d_parent->d_inode;
210 ctx = SPUFS_I(dir->d_inode)->i_ctx;
212 mutex_lock(&parent->i_mutex);
213 ret = spufs_rmdir(parent, dir);
214 mutex_unlock(&parent->i_mutex);
217 /* We have to give up the mm_struct */
220 return dcache_dir_close(inode, file);
223 struct inode_operations spufs_dir_inode_operations = {
224 .lookup = simple_lookup,
227 struct file_operations spufs_context_fops = {
228 .open = dcache_dir_open,
229 .release = spufs_dir_close,
230 .llseek = dcache_dir_lseek,
231 .read = generic_read_dir,
232 .readdir = dcache_readdir,
233 .fsync = simple_sync_file,
235 EXPORT_SYMBOL_GPL(spufs_context_fops);
238 spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
243 struct spu_context *ctx;
246 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
250 if (dir->i_mode & S_ISGID) {
251 inode->i_gid = dir->i_gid;
252 inode->i_mode &= S_ISGID;
254 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
255 SPUFS_I(inode)->i_ctx = ctx;
260 inode->i_op = &spufs_dir_inode_operations;
261 inode->i_fop = &simple_dir_operations;
262 if (flags & SPU_CREATE_NOSCHED)
263 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
266 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
271 d_instantiate(dentry, inode);
274 dentry->d_inode->i_nlink++;
278 put_spu_context(ctx);
285 static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
290 ret = get_unused_fd();
297 filp = dentry_open(dentry, mnt, O_RDONLY);
304 filp->f_op = &spufs_context_fops;
305 fd_install(ret, filp);
310 static int spufs_create_context(struct inode *inode,
311 struct dentry *dentry,
312 struct vfsmount *mnt, int flags, int mode)
317 if ((flags & SPU_CREATE_NOSCHED) &&
318 !capable(CAP_SYS_NICE))
322 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
323 == SPU_CREATE_ISOLATE)
327 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
330 ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
335 * get references for dget and mntget, will be released
336 * in error path of *_open().
338 ret = spufs_context_open(dget(dentry), mntget(mnt));
340 WARN_ON(spufs_rmdir(inode, dentry));
341 mutex_unlock(&inode->i_mutex);
342 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
347 mutex_unlock(&inode->i_mutex);
353 static int spufs_rmgang(struct inode *root, struct dentry *dir)
355 /* FIXME: this fails if the dir is not empty,
356 which causes a leak of gangs. */
357 return simple_rmdir(root, dir);
360 static int spufs_gang_close(struct inode *inode, struct file *file)
362 struct inode *parent;
366 dir = file->f_path.dentry;
367 parent = dir->d_parent->d_inode;
369 ret = spufs_rmgang(parent, dir);
372 return dcache_dir_close(inode, file);
375 struct file_operations spufs_gang_fops = {
376 .open = dcache_dir_open,
377 .release = spufs_gang_close,
378 .llseek = dcache_dir_lseek,
379 .read = generic_read_dir,
380 .readdir = dcache_readdir,
381 .fsync = simple_sync_file,
385 spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
389 struct spu_gang *gang;
392 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
397 if (dir->i_mode & S_ISGID) {
398 inode->i_gid = dir->i_gid;
399 inode->i_mode &= S_ISGID;
401 gang = alloc_spu_gang();
402 SPUFS_I(inode)->i_ctx = NULL;
403 SPUFS_I(inode)->i_gang = gang;
407 inode->i_op = &spufs_dir_inode_operations;
408 inode->i_fop = &simple_dir_operations;
410 d_instantiate(dentry, inode);
413 dentry->d_inode->i_nlink++;
422 static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
427 ret = get_unused_fd();
434 filp = dentry_open(dentry, mnt, O_RDONLY);
441 filp->f_op = &spufs_gang_fops;
442 fd_install(ret, filp);
447 static int spufs_create_gang(struct inode *inode,
448 struct dentry *dentry,
449 struct vfsmount *mnt, int mode)
453 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
458 * get references for dget and mntget, will be released
459 * in error path of *_open().
461 ret = spufs_gang_open(dget(dentry), mntget(mnt));
463 WARN_ON(spufs_rmgang(inode, dentry));
466 mutex_unlock(&inode->i_mutex);
472 static struct file_system_type spufs_type;
474 long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode)
476 struct dentry *dentry;
480 /* check if we are on spufs */
481 if (nd->dentry->d_sb->s_type != &spufs_type)
484 /* don't accept undefined flags */
485 if (flags & (~SPU_CREATE_FLAG_ALL))
488 /* only threads can be underneath a gang */
489 if (nd->dentry != nd->dentry->d_sb->s_root) {
490 if ((flags & SPU_CREATE_GANG) ||
491 !SPUFS_I(nd->dentry->d_inode)->i_gang)
495 dentry = lookup_create(nd, 1);
496 ret = PTR_ERR(dentry);
504 mode &= ~current->fs->umask;
506 if (flags & SPU_CREATE_GANG)
507 return spufs_create_gang(nd->dentry->d_inode,
508 dentry, nd->mnt, mode);
510 return spufs_create_context(nd->dentry->d_inode,
511 dentry, nd->mnt, flags, mode);
516 mutex_unlock(&nd->dentry->d_inode->i_mutex);
521 /* File system initialization */
523 Opt_uid, Opt_gid, Opt_err,
526 static match_table_t spufs_tokens = {
527 { Opt_uid, "uid=%d" },
528 { Opt_gid, "gid=%d" },
533 spufs_parse_options(char *options, struct inode *root)
536 substring_t args[MAX_OPT_ARGS];
538 while ((p = strsep(&options, ",")) != NULL) {
544 token = match_token(p, spufs_tokens, args);
547 if (match_int(&args[0], &option))
549 root->i_uid = option;
552 if (match_int(&args[0], &option))
554 root->i_gid = option;
564 spufs_init_isolated_loader(void)
566 struct device_node *dn;
570 dn = of_find_node_by_path("/spu-isolation");
574 loader = get_property(dn, "loader", &size);
578 /* kmalloc should align on a 16 byte boundary..* */
579 isolated_loader = kmalloc(size, GFP_KERNEL);
580 if (!isolated_loader)
583 memcpy(isolated_loader, loader, size);
584 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
588 spufs_create_root(struct super_block *sb, void *data)
594 inode = spufs_new_inode(sb, S_IFDIR | 0775);
598 inode->i_op = &spufs_dir_inode_operations;
599 inode->i_fop = &simple_dir_operations;
600 SPUFS_I(inode)->i_ctx = NULL;
603 if (!spufs_parse_options(data, inode))
607 sb->s_root = d_alloc_root(inode);
619 spufs_fill_super(struct super_block *sb, void *data, int silent)
621 static struct super_operations s_ops = {
622 .alloc_inode = spufs_alloc_inode,
623 .destroy_inode = spufs_destroy_inode,
624 .statfs = simple_statfs,
625 .delete_inode = spufs_delete_inode,
626 .drop_inode = generic_delete_inode,
629 sb->s_maxbytes = MAX_LFS_FILESIZE;
630 sb->s_blocksize = PAGE_CACHE_SIZE;
631 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
632 sb->s_magic = SPUFS_MAGIC;
635 return spufs_create_root(sb, data);
639 spufs_get_sb(struct file_system_type *fstype, int flags,
640 const char *name, void *data, struct vfsmount *mnt)
642 return get_sb_single(fstype, flags, data, spufs_fill_super, mnt);
645 static struct file_system_type spufs_type = {
646 .owner = THIS_MODULE,
648 .get_sb = spufs_get_sb,
649 .kill_sb = kill_litter_super,
652 static int __init spufs_init(void)
657 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
658 sizeof(struct spufs_inode_info), 0,
659 SLAB_HWCACHE_ALIGN, spufs_init_once, NULL);
661 if (!spufs_inode_cache)
663 if (spu_sched_init() != 0) {
664 kmem_cache_destroy(spufs_inode_cache);
667 ret = register_filesystem(&spufs_type);
670 ret = register_spu_syscalls(&spufs_calls);
673 ret = register_arch_coredump_calls(&spufs_coredump_calls);
677 spufs_init_isolated_loader();
681 unregister_filesystem(&spufs_type);
683 kmem_cache_destroy(spufs_inode_cache);
687 module_init(spufs_init);
689 static void __exit spufs_exit(void)
692 unregister_arch_coredump_calls(&spufs_coredump_calls);
693 unregister_spu_syscalls(&spufs_calls);
694 unregister_filesystem(&spufs_type);
695 kmem_cache_destroy(spufs_inode_cache);
697 module_exit(spufs_exit);
699 MODULE_LICENSE("GPL");
700 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");