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 kmem_cache_t *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, SLAB_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, kmem_cache_t * 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_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)
326 ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
331 * get references for dget and mntget, will be released
332 * in error path of *_open().
334 ret = spufs_context_open(dget(dentry), mntget(mnt));
336 WARN_ON(spufs_rmdir(inode, dentry));
337 mutex_unlock(&inode->i_mutex);
338 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
343 mutex_unlock(&inode->i_mutex);
349 static int spufs_rmgang(struct inode *root, struct dentry *dir)
351 /* FIXME: this fails if the dir is not empty,
352 which causes a leak of gangs. */
353 return simple_rmdir(root, dir);
356 static int spufs_gang_close(struct inode *inode, struct file *file)
358 struct inode *parent;
362 dir = file->f_dentry;
363 parent = dir->d_parent->d_inode;
365 ret = spufs_rmgang(parent, dir);
368 return dcache_dir_close(inode, file);
371 struct file_operations spufs_gang_fops = {
372 .open = dcache_dir_open,
373 .release = spufs_gang_close,
374 .llseek = dcache_dir_lseek,
375 .read = generic_read_dir,
376 .readdir = dcache_readdir,
377 .fsync = simple_sync_file,
381 spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
385 struct spu_gang *gang;
388 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
393 if (dir->i_mode & S_ISGID) {
394 inode->i_gid = dir->i_gid;
395 inode->i_mode &= S_ISGID;
397 gang = alloc_spu_gang();
398 SPUFS_I(inode)->i_ctx = NULL;
399 SPUFS_I(inode)->i_gang = gang;
403 inode->i_op = &spufs_dir_inode_operations;
404 inode->i_fop = &simple_dir_operations;
406 d_instantiate(dentry, inode);
409 dentry->d_inode->i_nlink++;
418 static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
423 ret = get_unused_fd();
430 filp = dentry_open(dentry, mnt, O_RDONLY);
437 filp->f_op = &spufs_gang_fops;
438 fd_install(ret, filp);
443 static int spufs_create_gang(struct inode *inode,
444 struct dentry *dentry,
445 struct vfsmount *mnt, int mode)
449 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
454 * get references for dget and mntget, will be released
455 * in error path of *_open().
457 ret = spufs_gang_open(dget(dentry), mntget(mnt));
459 WARN_ON(spufs_rmgang(inode, dentry));
462 mutex_unlock(&inode->i_mutex);
468 static struct file_system_type spufs_type;
470 long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode)
472 struct dentry *dentry;
476 /* check if we are on spufs */
477 if (nd->dentry->d_sb->s_type != &spufs_type)
480 /* don't accept undefined flags */
481 if (flags & (~SPU_CREATE_FLAG_ALL))
484 /* only threads can be underneath a gang */
485 if (nd->dentry != nd->dentry->d_sb->s_root) {
486 if ((flags & SPU_CREATE_GANG) ||
487 !SPUFS_I(nd->dentry->d_inode)->i_gang)
491 dentry = lookup_create(nd, 1);
492 ret = PTR_ERR(dentry);
500 mode &= ~current->fs->umask;
502 if (flags & SPU_CREATE_GANG)
503 return spufs_create_gang(nd->dentry->d_inode,
504 dentry, nd->mnt, mode);
506 return spufs_create_context(nd->dentry->d_inode,
507 dentry, nd->mnt, flags, mode);
512 mutex_unlock(&nd->dentry->d_inode->i_mutex);
517 /* File system initialization */
519 Opt_uid, Opt_gid, Opt_err,
522 static match_table_t spufs_tokens = {
523 { Opt_uid, "uid=%d" },
524 { Opt_gid, "gid=%d" },
529 spufs_parse_options(char *options, struct inode *root)
532 substring_t args[MAX_OPT_ARGS];
534 while ((p = strsep(&options, ",")) != NULL) {
540 token = match_token(p, spufs_tokens, args);
543 if (match_int(&args[0], &option))
545 root->i_uid = option;
548 if (match_int(&args[0], &option))
550 root->i_gid = option;
560 spufs_init_isolated_loader(void)
562 struct device_node *dn;
566 dn = of_find_node_by_path("/spu-isolation");
570 loader = get_property(dn, "loader", &size);
574 /* kmalloc should align on a 16 byte boundary..* */
575 isolated_loader = kmalloc(size, GFP_KERNEL);
576 if (!isolated_loader)
579 memcpy(isolated_loader, loader, size);
580 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
584 spufs_create_root(struct super_block *sb, void *data)
590 inode = spufs_new_inode(sb, S_IFDIR | 0775);
594 inode->i_op = &spufs_dir_inode_operations;
595 inode->i_fop = &simple_dir_operations;
596 SPUFS_I(inode)->i_ctx = NULL;
599 if (!spufs_parse_options(data, inode))
603 sb->s_root = d_alloc_root(inode);
615 spufs_fill_super(struct super_block *sb, void *data, int silent)
617 static struct super_operations s_ops = {
618 .alloc_inode = spufs_alloc_inode,
619 .destroy_inode = spufs_destroy_inode,
620 .statfs = simple_statfs,
621 .delete_inode = spufs_delete_inode,
622 .drop_inode = generic_delete_inode,
625 sb->s_maxbytes = MAX_LFS_FILESIZE;
626 sb->s_blocksize = PAGE_CACHE_SIZE;
627 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
628 sb->s_magic = SPUFS_MAGIC;
631 return spufs_create_root(sb, data);
635 spufs_get_sb(struct file_system_type *fstype, int flags,
636 const char *name, void *data, struct vfsmount *mnt)
638 return get_sb_single(fstype, flags, data, spufs_fill_super, mnt);
641 static struct file_system_type spufs_type = {
642 .owner = THIS_MODULE,
644 .get_sb = spufs_get_sb,
645 .kill_sb = kill_litter_super,
648 static int __init spufs_init(void)
653 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
654 sizeof(struct spufs_inode_info), 0,
655 SLAB_HWCACHE_ALIGN, spufs_init_once, NULL);
657 if (!spufs_inode_cache)
659 if (spu_sched_init() != 0) {
660 kmem_cache_destroy(spufs_inode_cache);
663 ret = register_filesystem(&spufs_type);
666 ret = register_spu_syscalls(&spufs_calls);
669 ret = register_arch_coredump_calls(&spufs_coredump_calls);
673 spufs_init_isolated_loader();
677 unregister_filesystem(&spufs_type);
679 kmem_cache_destroy(spufs_inode_cache);
683 module_init(spufs_init);
685 static void __exit spufs_exit(void)
688 unregister_arch_coredump_calls(&spufs_coredump_calls);
689 unregister_spu_syscalls(&spufs_calls);
690 unregister_filesystem(&spufs_type);
691 kmem_cache_destroy(spufs_inode_cache);
693 module_exit(spufs_exit);
695 MODULE_LICENSE("GPL");
696 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");