[CELL] spufs: extension of spu_create to support affinity definition
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / powerpc / platforms / cell / spufs / inode.c
blobb3d0dd118dd0ededd97d842e7ab9db1408bda23e
1 /*
2 * SPU file system
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)
11 * any later version.
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>
24 #include <linux/fs.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>
36 #include <asm/prom.h>
37 #include <asm/semaphore.h>
38 #include <asm/spu.h>
39 #include <asm/spu_priv1.h>
40 #include <asm/uaccess.h>
42 #include "spufs.h"
44 static struct kmem_cache *spufs_inode_cache;
45 char *isolated_loader;
47 static struct inode *
48 spufs_alloc_inode(struct super_block *sb)
50 struct spufs_inode_info *ei;
52 ei = kmem_cache_alloc(spufs_inode_cache, GFP_KERNEL);
53 if (!ei)
54 return NULL;
56 ei->i_gang = NULL;
57 ei->i_ctx = NULL;
58 ei->i_openers = 0;
60 return &ei->vfs_inode;
63 static void
64 spufs_destroy_inode(struct inode *inode)
66 kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
69 static void
70 spufs_init_once(void *p, struct kmem_cache * cachep, unsigned long flags)
72 struct spufs_inode_info *ei = p;
74 inode_init_once(&ei->vfs_inode);
77 static struct inode *
78 spufs_new_inode(struct super_block *sb, int mode)
80 struct inode *inode;
82 inode = new_inode(sb);
83 if (!inode)
84 goto out;
86 inode->i_mode = mode;
87 inode->i_uid = current->fsuid;
88 inode->i_gid = current->fsgid;
89 inode->i_blocks = 0;
90 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
91 out:
92 return inode;
95 static int
96 spufs_setattr(struct dentry *dentry, struct iattr *attr)
98 struct inode *inode = dentry->d_inode;
100 if ((attr->ia_valid & ATTR_SIZE) &&
101 (attr->ia_size != inode->i_size))
102 return -EINVAL;
103 return inode_setattr(inode, attr);
107 static int
108 spufs_new_file(struct super_block *sb, struct dentry *dentry,
109 const struct file_operations *fops, int mode,
110 struct spu_context *ctx)
112 static struct inode_operations spufs_file_iops = {
113 .setattr = spufs_setattr,
115 struct inode *inode;
116 int ret;
118 ret = -ENOSPC;
119 inode = spufs_new_inode(sb, S_IFREG | mode);
120 if (!inode)
121 goto out;
123 ret = 0;
124 inode->i_op = &spufs_file_iops;
125 inode->i_fop = fops;
126 inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
127 d_add(dentry, inode);
128 out:
129 return ret;
132 static void
133 spufs_delete_inode(struct inode *inode)
135 struct spufs_inode_info *ei = SPUFS_I(inode);
137 if (ei->i_ctx)
138 put_spu_context(ei->i_ctx);
139 if (ei->i_gang)
140 put_spu_gang(ei->i_gang);
141 clear_inode(inode);
144 static void spufs_prune_dir(struct dentry *dir)
146 struct dentry *dentry, *tmp;
148 mutex_lock(&dir->d_inode->i_mutex);
149 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
150 spin_lock(&dcache_lock);
151 spin_lock(&dentry->d_lock);
152 if (!(d_unhashed(dentry)) && dentry->d_inode) {
153 dget_locked(dentry);
154 __d_drop(dentry);
155 spin_unlock(&dentry->d_lock);
156 simple_unlink(dir->d_inode, dentry);
157 spin_unlock(&dcache_lock);
158 dput(dentry);
159 } else {
160 spin_unlock(&dentry->d_lock);
161 spin_unlock(&dcache_lock);
164 shrink_dcache_parent(dir);
165 mutex_unlock(&dir->d_inode->i_mutex);
168 /* Caller must hold parent->i_mutex */
169 static int spufs_rmdir(struct inode *parent, struct dentry *dir)
171 /* remove all entries */
172 spufs_prune_dir(dir);
174 return simple_rmdir(parent, dir);
177 static int spufs_fill_dir(struct dentry *dir, struct tree_descr *files,
178 int mode, struct spu_context *ctx)
180 struct dentry *dentry, *tmp;
181 int ret;
183 while (files->name && files->name[0]) {
184 ret = -ENOMEM;
185 dentry = d_alloc_name(dir, files->name);
186 if (!dentry)
187 goto out;
188 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
189 files->mode & mode, ctx);
190 if (ret)
191 goto out;
192 files++;
194 return 0;
195 out:
197 * remove all children from dir. dir->inode is not set so don't
198 * just simply use spufs_prune_dir() and panic afterwards :)
199 * dput() looks like it will do the right thing:
200 * - dec parent's ref counter
201 * - remove child from parent's child list
202 * - free child's inode if possible
203 * - free child
205 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
206 dput(dentry);
209 shrink_dcache_parent(dir);
210 return ret;
213 static int spufs_dir_close(struct inode *inode, struct file *file)
215 struct spu_context *ctx;
216 struct inode *parent;
217 struct dentry *dir;
218 int ret;
220 dir = file->f_path.dentry;
221 parent = dir->d_parent->d_inode;
222 ctx = SPUFS_I(dir->d_inode)->i_ctx;
224 mutex_lock(&parent->i_mutex);
225 ret = spufs_rmdir(parent, dir);
226 mutex_unlock(&parent->i_mutex);
227 WARN_ON(ret);
229 /* We have to give up the mm_struct */
230 spu_forget(ctx);
232 return dcache_dir_close(inode, file);
235 const struct file_operations spufs_context_fops = {
236 .open = dcache_dir_open,
237 .release = spufs_dir_close,
238 .llseek = dcache_dir_lseek,
239 .read = generic_read_dir,
240 .readdir = dcache_readdir,
241 .fsync = simple_sync_file,
243 EXPORT_SYMBOL_GPL(spufs_context_fops);
245 static int
246 spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
247 int mode)
249 int ret;
250 struct inode *inode;
251 struct spu_context *ctx;
253 ret = -ENOSPC;
254 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
255 if (!inode)
256 goto out;
258 if (dir->i_mode & S_ISGID) {
259 inode->i_gid = dir->i_gid;
260 inode->i_mode &= S_ISGID;
262 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
263 SPUFS_I(inode)->i_ctx = ctx;
264 if (!ctx)
265 goto out_iput;
267 ctx->flags = flags;
268 inode->i_op = &simple_dir_inode_operations;
269 inode->i_fop = &simple_dir_operations;
270 if (flags & SPU_CREATE_NOSCHED)
271 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
272 mode, ctx);
273 else
274 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
276 if (ret)
277 goto out_free_ctx;
279 d_instantiate(dentry, inode);
280 dget(dentry);
281 dir->i_nlink++;
282 dentry->d_inode->i_nlink++;
283 goto out;
285 out_free_ctx:
286 spu_forget(ctx);
287 put_spu_context(ctx);
288 out_iput:
289 iput(inode);
290 out:
291 return ret;
294 static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
296 int ret;
297 struct file *filp;
299 ret = get_unused_fd();
300 if (ret < 0) {
301 dput(dentry);
302 mntput(mnt);
303 goto out;
306 filp = dentry_open(dentry, mnt, O_RDONLY);
307 if (IS_ERR(filp)) {
308 put_unused_fd(ret);
309 ret = PTR_ERR(filp);
310 goto out;
313 filp->f_op = &spufs_context_fops;
314 fd_install(ret, filp);
315 out:
316 return ret;
319 static struct spu_context *
320 spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
321 struct file *filp)
323 struct spu_context *tmp, *neighbor;
324 int count, node;
325 int aff_supp;
327 aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
328 struct spu, cbe_list))->aff_list);
330 if (!aff_supp)
331 return ERR_PTR(-EINVAL);
333 if (flags & SPU_CREATE_GANG)
334 return ERR_PTR(-EINVAL);
336 if (flags & SPU_CREATE_AFFINITY_MEM &&
337 gang->aff_ref_ctx &&
338 gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
339 return ERR_PTR(-EEXIST);
341 if (gang->aff_flags & AFF_MERGED)
342 return ERR_PTR(-EBUSY);
344 neighbor = NULL;
345 if (flags & SPU_CREATE_AFFINITY_SPU) {
346 if (!filp || filp->f_op != &spufs_context_fops)
347 return ERR_PTR(-EINVAL);
349 neighbor = get_spu_context(
350 SPUFS_I(filp->f_dentry->d_inode)->i_ctx);
352 if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
353 !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
354 !list_entry(neighbor->aff_list.next, struct spu_context,
355 aff_list)->aff_head)
356 return ERR_PTR(-EEXIST);
358 if (gang != neighbor->gang)
359 return ERR_PTR(-EINVAL);
361 count = 1;
362 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
363 count++;
364 if (list_empty(&neighbor->aff_list))
365 count++;
367 for (node = 0; node < MAX_NUMNODES; node++) {
368 if ((cbe_spu_info[node].n_spus - atomic_read(
369 &cbe_spu_info[node].reserved_spus)) >= count)
370 break;
373 if (node == MAX_NUMNODES)
374 return ERR_PTR(-EEXIST);
377 return neighbor;
380 static void
381 spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
382 struct spu_context *neighbor)
384 if (flags & SPU_CREATE_AFFINITY_MEM)
385 ctx->gang->aff_ref_ctx = ctx;
387 if (flags & SPU_CREATE_AFFINITY_SPU) {
388 if (list_empty(&neighbor->aff_list)) {
389 list_add_tail(&neighbor->aff_list,
390 &ctx->gang->aff_list_head);
391 neighbor->aff_head = 1;
394 if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
395 || list_entry(neighbor->aff_list.next, struct spu_context,
396 aff_list)->aff_head) {
397 list_add(&ctx->aff_list, &neighbor->aff_list);
398 } else {
399 list_add_tail(&ctx->aff_list, &neighbor->aff_list);
400 if (neighbor->aff_head) {
401 neighbor->aff_head = 0;
402 ctx->aff_head = 1;
406 if (!ctx->gang->aff_ref_ctx)
407 ctx->gang->aff_ref_ctx = ctx;
411 static int
412 spufs_create_context(struct inode *inode, struct dentry *dentry,
413 struct vfsmount *mnt, int flags, int mode,
414 struct file *aff_filp)
416 int ret;
417 int affinity;
418 struct spu_gang *gang;
419 struct spu_context *neighbor;
421 ret = -EPERM;
422 if ((flags & SPU_CREATE_NOSCHED) &&
423 !capable(CAP_SYS_NICE))
424 goto out_unlock;
426 ret = -EINVAL;
427 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
428 == SPU_CREATE_ISOLATE)
429 goto out_unlock;
431 ret = -ENODEV;
432 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
433 goto out_unlock;
435 gang = NULL;
436 neighbor = NULL;
437 affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
438 if (affinity) {
439 gang = SPUFS_I(inode)->i_gang;
440 ret = -EINVAL;
441 if (!gang)
442 goto out_unlock;
443 mutex_lock(&gang->aff_mutex);
444 neighbor = spufs_assert_affinity(flags, gang, aff_filp);
445 if (IS_ERR(neighbor)) {
446 ret = PTR_ERR(neighbor);
447 goto out_aff_unlock;
451 ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
452 if (ret)
453 goto out_aff_unlock;
455 if (affinity)
456 spufs_set_affinity(flags, SPUFS_I(dentry->d_inode)->i_ctx,
457 neighbor);
460 * get references for dget and mntget, will be released
461 * in error path of *_open().
463 ret = spufs_context_open(dget(dentry), mntget(mnt));
464 if (ret < 0) {
465 WARN_ON(spufs_rmdir(inode, dentry));
466 mutex_unlock(&inode->i_mutex);
467 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
468 goto out;
471 out_aff_unlock:
472 if (affinity)
473 mutex_unlock(&gang->aff_mutex);
474 out_unlock:
475 mutex_unlock(&inode->i_mutex);
476 out:
477 dput(dentry);
478 return ret;
481 static int
482 spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
484 int ret;
485 struct inode *inode;
486 struct spu_gang *gang;
488 ret = -ENOSPC;
489 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
490 if (!inode)
491 goto out;
493 ret = 0;
494 if (dir->i_mode & S_ISGID) {
495 inode->i_gid = dir->i_gid;
496 inode->i_mode &= S_ISGID;
498 gang = alloc_spu_gang();
499 SPUFS_I(inode)->i_ctx = NULL;
500 SPUFS_I(inode)->i_gang = gang;
501 if (!gang)
502 goto out_iput;
504 inode->i_op = &simple_dir_inode_operations;
505 inode->i_fop = &simple_dir_operations;
507 d_instantiate(dentry, inode);
508 dir->i_nlink++;
509 dentry->d_inode->i_nlink++;
510 return ret;
512 out_iput:
513 iput(inode);
514 out:
515 return ret;
518 static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
520 int ret;
521 struct file *filp;
523 ret = get_unused_fd();
524 if (ret < 0) {
525 dput(dentry);
526 mntput(mnt);
527 goto out;
530 filp = dentry_open(dentry, mnt, O_RDONLY);
531 if (IS_ERR(filp)) {
532 put_unused_fd(ret);
533 ret = PTR_ERR(filp);
534 goto out;
537 filp->f_op = &simple_dir_operations;
538 fd_install(ret, filp);
539 out:
540 return ret;
543 static int spufs_create_gang(struct inode *inode,
544 struct dentry *dentry,
545 struct vfsmount *mnt, int mode)
547 int ret;
549 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
550 if (ret)
551 goto out;
554 * get references for dget and mntget, will be released
555 * in error path of *_open().
557 ret = spufs_gang_open(dget(dentry), mntget(mnt));
558 if (ret < 0) {
559 int err = simple_rmdir(inode, dentry);
560 WARN_ON(err);
563 out:
564 mutex_unlock(&inode->i_mutex);
565 dput(dentry);
566 return ret;
570 static struct file_system_type spufs_type;
572 long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode,
573 struct file *filp)
575 struct dentry *dentry;
576 int ret;
578 ret = -EINVAL;
579 /* check if we are on spufs */
580 if (nd->dentry->d_sb->s_type != &spufs_type)
581 goto out;
583 /* don't accept undefined flags */
584 if (flags & (~SPU_CREATE_FLAG_ALL))
585 goto out;
587 /* only threads can be underneath a gang */
588 if (nd->dentry != nd->dentry->d_sb->s_root) {
589 if ((flags & SPU_CREATE_GANG) ||
590 !SPUFS_I(nd->dentry->d_inode)->i_gang)
591 goto out;
594 dentry = lookup_create(nd, 1);
595 ret = PTR_ERR(dentry);
596 if (IS_ERR(dentry))
597 goto out_dir;
599 ret = -EEXIST;
600 if (dentry->d_inode)
601 goto out_dput;
603 mode &= ~current->fs->umask;
605 if (flags & SPU_CREATE_GANG)
606 return spufs_create_gang(nd->dentry->d_inode,
607 dentry, nd->mnt, mode);
608 else
609 return spufs_create_context(nd->dentry->d_inode,
610 dentry, nd->mnt, flags, mode, filp);
612 out_dput:
613 dput(dentry);
614 out_dir:
615 mutex_unlock(&nd->dentry->d_inode->i_mutex);
616 out:
617 return ret;
620 /* File system initialization */
621 enum {
622 Opt_uid, Opt_gid, Opt_mode, Opt_err,
625 static match_table_t spufs_tokens = {
626 { Opt_uid, "uid=%d" },
627 { Opt_gid, "gid=%d" },
628 { Opt_mode, "mode=%o" },
629 { Opt_err, NULL },
632 static int
633 spufs_parse_options(char *options, struct inode *root)
635 char *p;
636 substring_t args[MAX_OPT_ARGS];
638 while ((p = strsep(&options, ",")) != NULL) {
639 int token, option;
641 if (!*p)
642 continue;
644 token = match_token(p, spufs_tokens, args);
645 switch (token) {
646 case Opt_uid:
647 if (match_int(&args[0], &option))
648 return 0;
649 root->i_uid = option;
650 break;
651 case Opt_gid:
652 if (match_int(&args[0], &option))
653 return 0;
654 root->i_gid = option;
655 break;
656 case Opt_mode:
657 if (match_octal(&args[0], &option))
658 return 0;
659 root->i_mode = option | S_IFDIR;
660 break;
661 default:
662 return 0;
665 return 1;
668 static void spufs_exit_isolated_loader(void)
670 kfree(isolated_loader);
673 static void
674 spufs_init_isolated_loader(void)
676 struct device_node *dn;
677 const char *loader;
678 int size;
680 dn = of_find_node_by_path("/spu-isolation");
681 if (!dn)
682 return;
684 loader = of_get_property(dn, "loader", &size);
685 if (!loader)
686 return;
688 /* kmalloc should align on a 16 byte boundary..* */
689 isolated_loader = kmalloc(size, GFP_KERNEL);
690 if (!isolated_loader)
691 return;
693 memcpy(isolated_loader, loader, size);
694 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
697 static int
698 spufs_create_root(struct super_block *sb, void *data)
700 struct inode *inode;
701 int ret;
703 ret = -ENODEV;
704 if (!spu_management_ops)
705 goto out;
707 ret = -ENOMEM;
708 inode = spufs_new_inode(sb, S_IFDIR | 0775);
709 if (!inode)
710 goto out;
712 inode->i_op = &simple_dir_inode_operations;
713 inode->i_fop = &simple_dir_operations;
714 SPUFS_I(inode)->i_ctx = NULL;
716 ret = -EINVAL;
717 if (!spufs_parse_options(data, inode))
718 goto out_iput;
720 ret = -ENOMEM;
721 sb->s_root = d_alloc_root(inode);
722 if (!sb->s_root)
723 goto out_iput;
725 return 0;
726 out_iput:
727 iput(inode);
728 out:
729 return ret;
732 static int
733 spufs_fill_super(struct super_block *sb, void *data, int silent)
735 static struct super_operations s_ops = {
736 .alloc_inode = spufs_alloc_inode,
737 .destroy_inode = spufs_destroy_inode,
738 .statfs = simple_statfs,
739 .delete_inode = spufs_delete_inode,
740 .drop_inode = generic_delete_inode,
743 sb->s_maxbytes = MAX_LFS_FILESIZE;
744 sb->s_blocksize = PAGE_CACHE_SIZE;
745 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
746 sb->s_magic = SPUFS_MAGIC;
747 sb->s_op = &s_ops;
749 return spufs_create_root(sb, data);
752 static int
753 spufs_get_sb(struct file_system_type *fstype, int flags,
754 const char *name, void *data, struct vfsmount *mnt)
756 return get_sb_single(fstype, flags, data, spufs_fill_super, mnt);
759 static struct file_system_type spufs_type = {
760 .owner = THIS_MODULE,
761 .name = "spufs",
762 .get_sb = spufs_get_sb,
763 .kill_sb = kill_litter_super,
766 static int __init spufs_init(void)
768 int ret;
770 ret = -ENODEV;
771 if (!spu_management_ops)
772 goto out;
774 ret = -ENOMEM;
775 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
776 sizeof(struct spufs_inode_info), 0,
777 SLAB_HWCACHE_ALIGN, spufs_init_once);
779 if (!spufs_inode_cache)
780 goto out;
781 ret = spu_sched_init();
782 if (ret)
783 goto out_cache;
784 ret = register_filesystem(&spufs_type);
785 if (ret)
786 goto out_sched;
787 ret = register_spu_syscalls(&spufs_calls);
788 if (ret)
789 goto out_fs;
790 ret = register_arch_coredump_calls(&spufs_coredump_calls);
791 if (ret)
792 goto out_syscalls;
794 spufs_init_isolated_loader();
796 return 0;
798 out_syscalls:
799 unregister_spu_syscalls(&spufs_calls);
800 out_fs:
801 unregister_filesystem(&spufs_type);
802 out_sched:
803 spu_sched_exit();
804 out_cache:
805 kmem_cache_destroy(spufs_inode_cache);
806 out:
807 return ret;
809 module_init(spufs_init);
811 static void __exit spufs_exit(void)
813 spu_sched_exit();
814 spufs_exit_isolated_loader();
815 unregister_arch_coredump_calls(&spufs_coredump_calls);
816 unregister_spu_syscalls(&spufs_calls);
817 unregister_filesystem(&spufs_type);
818 kmem_cache_destroy(spufs_inode_cache);
820 module_exit(spufs_exit);
822 MODULE_LICENSE("GPL");
823 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");