5 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
7 * Author: Arnd Bergmann <arndb@de.ibm.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/file.h>
26 #include <linux/fsnotify.h>
27 #include <linux/backing-dev.h>
28 #include <linux/init.h>
29 #include <linux/ioctl.h>
30 #include <linux/module.h>
31 #include <linux/mount.h>
32 #include <linux/namei.h>
33 #include <linux/pagemap.h>
34 #include <linux/poll.h>
35 #include <linux/slab.h>
36 #include <linux/parser.h>
40 #include <asm/spu_priv1.h>
41 #include <asm/uaccess.h>
45 struct spufs_sb_info {
49 static struct kmem_cache *spufs_inode_cache;
50 char *isolated_loader;
51 static int isolated_loader_size;
53 static struct spufs_sb_info *spufs_get_sb_info(struct super_block *sb)
59 spufs_alloc_inode(struct super_block *sb)
61 struct spufs_inode_info *ei;
63 ei = kmem_cache_alloc(spufs_inode_cache, GFP_KERNEL);
71 return &ei->vfs_inode;
75 spufs_destroy_inode(struct inode *inode)
77 kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
81 spufs_init_once(struct kmem_cache *cachep, void *p)
83 struct spufs_inode_info *ei = p;
85 inode_init_once(&ei->vfs_inode);
89 spufs_new_inode(struct super_block *sb, int mode)
93 inode = new_inode(sb);
98 inode->i_uid = current->fsuid;
99 inode->i_gid = current->fsgid;
101 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
107 spufs_setattr(struct dentry *dentry, struct iattr *attr)
109 struct inode *inode = dentry->d_inode;
111 if ((attr->ia_valid & ATTR_SIZE) &&
112 (attr->ia_size != inode->i_size))
114 return inode_setattr(inode, attr);
119 spufs_new_file(struct super_block *sb, struct dentry *dentry,
120 const struct file_operations *fops, int mode,
121 size_t size, struct spu_context *ctx)
123 static struct inode_operations spufs_file_iops = {
124 .setattr = spufs_setattr,
130 inode = spufs_new_inode(sb, S_IFREG | mode);
135 inode->i_op = &spufs_file_iops;
137 inode->i_size = size;
138 inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
139 d_add(dentry, inode);
145 spufs_delete_inode(struct inode *inode)
147 struct spufs_inode_info *ei = SPUFS_I(inode);
150 put_spu_context(ei->i_ctx);
152 put_spu_gang(ei->i_gang);
156 static void spufs_prune_dir(struct dentry *dir)
158 struct dentry *dentry, *tmp;
160 mutex_lock(&dir->d_inode->i_mutex);
161 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
162 spin_lock(&dcache_lock);
163 spin_lock(&dentry->d_lock);
164 if (!(d_unhashed(dentry)) && dentry->d_inode) {
167 spin_unlock(&dentry->d_lock);
168 simple_unlink(dir->d_inode, dentry);
169 spin_unlock(&dcache_lock);
172 spin_unlock(&dentry->d_lock);
173 spin_unlock(&dcache_lock);
176 shrink_dcache_parent(dir);
177 mutex_unlock(&dir->d_inode->i_mutex);
180 /* Caller must hold parent->i_mutex */
181 static int spufs_rmdir(struct inode *parent, struct dentry *dir)
183 /* remove all entries */
184 spufs_prune_dir(dir);
187 return simple_rmdir(parent, dir);
190 static int spufs_fill_dir(struct dentry *dir, struct spufs_tree_descr *files,
191 int mode, struct spu_context *ctx)
193 struct dentry *dentry, *tmp;
196 while (files->name && files->name[0]) {
198 dentry = d_alloc_name(dir, files->name);
201 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
202 files->mode & mode, files->size, ctx);
210 * remove all children from dir. dir->inode is not set so don't
211 * just simply use spufs_prune_dir() and panic afterwards :)
212 * dput() looks like it will do the right thing:
213 * - dec parent's ref counter
214 * - remove child from parent's child list
215 * - free child's inode if possible
218 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
222 shrink_dcache_parent(dir);
226 static int spufs_dir_close(struct inode *inode, struct file *file)
228 struct spu_context *ctx;
229 struct inode *parent;
233 dir = file->f_path.dentry;
234 parent = dir->d_parent->d_inode;
235 ctx = SPUFS_I(dir->d_inode)->i_ctx;
237 mutex_lock_nested(&parent->i_mutex, I_MUTEX_PARENT);
238 ret = spufs_rmdir(parent, dir);
239 mutex_unlock(&parent->i_mutex);
242 /* We have to give up the mm_struct */
245 return dcache_dir_close(inode, file);
248 const struct file_operations spufs_context_fops = {
249 .open = dcache_dir_open,
250 .release = spufs_dir_close,
251 .llseek = dcache_dir_lseek,
252 .read = generic_read_dir,
253 .readdir = dcache_readdir,
254 .fsync = simple_sync_file,
256 EXPORT_SYMBOL_GPL(spufs_context_fops);
259 spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
264 struct spu_context *ctx;
267 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
271 if (dir->i_mode & S_ISGID) {
272 inode->i_gid = dir->i_gid;
273 inode->i_mode &= S_ISGID;
275 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
276 SPUFS_I(inode)->i_ctx = ctx;
281 inode->i_op = &simple_dir_inode_operations;
282 inode->i_fop = &simple_dir_operations;
283 if (flags & SPU_CREATE_NOSCHED)
284 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
287 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
292 if (spufs_get_sb_info(dir->i_sb)->debug)
293 ret = spufs_fill_dir(dentry, spufs_dir_debug_contents,
299 d_instantiate(dentry, inode);
302 dentry->d_inode->i_nlink++;
307 put_spu_context(ctx);
314 static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
319 ret = get_unused_fd();
326 filp = dentry_open(dentry, mnt, O_RDONLY);
333 filp->f_op = &spufs_context_fops;
334 fd_install(ret, filp);
339 static struct spu_context *
340 spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
343 struct spu_context *tmp, *neighbor, *err;
347 aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
348 struct spu, cbe_list))->aff_list);
351 return ERR_PTR(-EINVAL);
353 if (flags & SPU_CREATE_GANG)
354 return ERR_PTR(-EINVAL);
356 if (flags & SPU_CREATE_AFFINITY_MEM &&
358 gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
359 return ERR_PTR(-EEXIST);
361 if (gang->aff_flags & AFF_MERGED)
362 return ERR_PTR(-EBUSY);
365 if (flags & SPU_CREATE_AFFINITY_SPU) {
366 if (!filp || filp->f_op != &spufs_context_fops)
367 return ERR_PTR(-EINVAL);
369 neighbor = get_spu_context(
370 SPUFS_I(filp->f_dentry->d_inode)->i_ctx);
372 if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
373 !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
374 !list_entry(neighbor->aff_list.next, struct spu_context,
375 aff_list)->aff_head) {
376 err = ERR_PTR(-EEXIST);
377 goto out_put_neighbor;
380 if (gang != neighbor->gang) {
381 err = ERR_PTR(-EINVAL);
382 goto out_put_neighbor;
386 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
388 if (list_empty(&neighbor->aff_list))
391 for (node = 0; node < MAX_NUMNODES; node++) {
392 if ((cbe_spu_info[node].n_spus - atomic_read(
393 &cbe_spu_info[node].reserved_spus)) >= count)
397 if (node == MAX_NUMNODES) {
398 err = ERR_PTR(-EEXIST);
399 goto out_put_neighbor;
406 put_spu_context(neighbor);
411 spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
412 struct spu_context *neighbor)
414 if (flags & SPU_CREATE_AFFINITY_MEM)
415 ctx->gang->aff_ref_ctx = ctx;
417 if (flags & SPU_CREATE_AFFINITY_SPU) {
418 if (list_empty(&neighbor->aff_list)) {
419 list_add_tail(&neighbor->aff_list,
420 &ctx->gang->aff_list_head);
421 neighbor->aff_head = 1;
424 if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
425 || list_entry(neighbor->aff_list.next, struct spu_context,
426 aff_list)->aff_head) {
427 list_add(&ctx->aff_list, &neighbor->aff_list);
429 list_add_tail(&ctx->aff_list, &neighbor->aff_list);
430 if (neighbor->aff_head) {
431 neighbor->aff_head = 0;
436 if (!ctx->gang->aff_ref_ctx)
437 ctx->gang->aff_ref_ctx = ctx;
442 spufs_create_context(struct inode *inode, struct dentry *dentry,
443 struct vfsmount *mnt, int flags, int mode,
444 struct file *aff_filp)
448 struct spu_gang *gang;
449 struct spu_context *neighbor;
452 if ((flags & SPU_CREATE_NOSCHED) &&
453 !capable(CAP_SYS_NICE))
457 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
458 == SPU_CREATE_ISOLATE)
462 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
467 affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
469 gang = SPUFS_I(inode)->i_gang;
473 mutex_lock(&gang->aff_mutex);
474 neighbor = spufs_assert_affinity(flags, gang, aff_filp);
475 if (IS_ERR(neighbor)) {
476 ret = PTR_ERR(neighbor);
481 ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
486 spufs_set_affinity(flags, SPUFS_I(dentry->d_inode)->i_ctx,
489 put_spu_context(neighbor);
493 * get references for dget and mntget, will be released
494 * in error path of *_open().
496 ret = spufs_context_open(dget(dentry), mntget(mnt));
498 WARN_ON(spufs_rmdir(inode, dentry));
499 mutex_unlock(&inode->i_mutex);
500 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
506 mutex_unlock(&gang->aff_mutex);
508 mutex_unlock(&inode->i_mutex);
515 spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
519 struct spu_gang *gang;
522 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
527 if (dir->i_mode & S_ISGID) {
528 inode->i_gid = dir->i_gid;
529 inode->i_mode &= S_ISGID;
531 gang = alloc_spu_gang();
532 SPUFS_I(inode)->i_ctx = NULL;
533 SPUFS_I(inode)->i_gang = gang;
537 inode->i_op = &simple_dir_inode_operations;
538 inode->i_fop = &simple_dir_operations;
540 d_instantiate(dentry, inode);
542 dentry->d_inode->i_nlink++;
551 static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
556 ret = get_unused_fd();
563 filp = dentry_open(dentry, mnt, O_RDONLY);
570 filp->f_op = &simple_dir_operations;
571 fd_install(ret, filp);
576 static int spufs_create_gang(struct inode *inode,
577 struct dentry *dentry,
578 struct vfsmount *mnt, int mode)
582 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
587 * get references for dget and mntget, will be released
588 * in error path of *_open().
590 ret = spufs_gang_open(dget(dentry), mntget(mnt));
592 int err = simple_rmdir(inode, dentry);
597 mutex_unlock(&inode->i_mutex);
603 static struct file_system_type spufs_type;
605 long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode,
608 struct dentry *dentry;
612 /* check if we are on spufs */
613 if (nd->path.dentry->d_sb->s_type != &spufs_type)
616 /* don't accept undefined flags */
617 if (flags & (~SPU_CREATE_FLAG_ALL))
620 /* only threads can be underneath a gang */
621 if (nd->path.dentry != nd->path.dentry->d_sb->s_root) {
622 if ((flags & SPU_CREATE_GANG) ||
623 !SPUFS_I(nd->path.dentry->d_inode)->i_gang)
627 dentry = lookup_create(nd, 1);
628 ret = PTR_ERR(dentry);
636 mode &= ~current->fs->umask;
638 if (flags & SPU_CREATE_GANG)
639 ret = spufs_create_gang(nd->path.dentry->d_inode,
640 dentry, nd->path.mnt, mode);
642 ret = spufs_create_context(nd->path.dentry->d_inode,
643 dentry, nd->path.mnt, flags, mode,
646 fsnotify_mkdir(nd->path.dentry->d_inode, dentry);
652 mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
657 /* File system initialization */
659 Opt_uid, Opt_gid, Opt_mode, Opt_debug, Opt_err,
662 static match_table_t spufs_tokens = {
663 { Opt_uid, "uid=%d" },
664 { Opt_gid, "gid=%d" },
665 { Opt_mode, "mode=%o" },
666 { Opt_debug, "debug" },
671 spufs_parse_options(struct super_block *sb, char *options, struct inode *root)
674 substring_t args[MAX_OPT_ARGS];
676 while ((p = strsep(&options, ",")) != NULL) {
682 token = match_token(p, spufs_tokens, args);
685 if (match_int(&args[0], &option))
687 root->i_uid = option;
690 if (match_int(&args[0], &option))
692 root->i_gid = option;
695 if (match_octal(&args[0], &option))
697 root->i_mode = option | S_IFDIR;
700 spufs_get_sb_info(sb)->debug = 1;
709 static void spufs_exit_isolated_loader(void)
711 free_pages((unsigned long) isolated_loader,
712 get_order(isolated_loader_size));
716 spufs_init_isolated_loader(void)
718 struct device_node *dn;
722 dn = of_find_node_by_path("/spu-isolation");
726 loader = of_get_property(dn, "loader", &size);
730 /* the loader must be align on a 16 byte boundary */
731 isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
732 if (!isolated_loader)
735 isolated_loader_size = size;
736 memcpy(isolated_loader, loader, size);
737 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
741 spufs_create_root(struct super_block *sb, void *data)
747 if (!spu_management_ops)
751 inode = spufs_new_inode(sb, S_IFDIR | 0775);
755 inode->i_op = &simple_dir_inode_operations;
756 inode->i_fop = &simple_dir_operations;
757 SPUFS_I(inode)->i_ctx = NULL;
760 if (!spufs_parse_options(sb, data, inode))
764 sb->s_root = d_alloc_root(inode);
776 spufs_fill_super(struct super_block *sb, void *data, int silent)
778 struct spufs_sb_info *info;
779 static struct super_operations s_ops = {
780 .alloc_inode = spufs_alloc_inode,
781 .destroy_inode = spufs_destroy_inode,
782 .statfs = simple_statfs,
783 .delete_inode = spufs_delete_inode,
784 .drop_inode = generic_delete_inode,
785 .show_options = generic_show_options,
788 save_mount_options(sb, data);
790 info = kzalloc(sizeof(*info), GFP_KERNEL);
794 sb->s_maxbytes = MAX_LFS_FILESIZE;
795 sb->s_blocksize = PAGE_CACHE_SIZE;
796 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
797 sb->s_magic = SPUFS_MAGIC;
799 sb->s_fs_info = info;
801 return spufs_create_root(sb, data);
805 spufs_get_sb(struct file_system_type *fstype, int flags,
806 const char *name, void *data, struct vfsmount *mnt)
808 return get_sb_single(fstype, flags, data, spufs_fill_super, mnt);
811 static struct file_system_type spufs_type = {
812 .owner = THIS_MODULE,
814 .get_sb = spufs_get_sb,
815 .kill_sb = kill_litter_super,
818 static int __init spufs_init(void)
823 if (!spu_management_ops)
827 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
828 sizeof(struct spufs_inode_info), 0,
829 SLAB_HWCACHE_ALIGN, spufs_init_once);
831 if (!spufs_inode_cache)
833 ret = spu_sched_init();
836 ret = register_filesystem(&spufs_type);
839 ret = register_spu_syscalls(&spufs_calls);
843 spufs_init_isolated_loader();
848 unregister_filesystem(&spufs_type);
852 kmem_cache_destroy(spufs_inode_cache);
856 module_init(spufs_init);
858 static void __exit spufs_exit(void)
861 spufs_exit_isolated_loader();
862 unregister_spu_syscalls(&spufs_calls);
863 unregister_filesystem(&spufs_type);
864 kmem_cache_destroy(spufs_inode_cache);
866 module_exit(spufs_exit);
868 MODULE_LICENSE("GPL");
869 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");