Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / arch / powerpc / platforms / cell / spufs / inode.c
blob6d1228c66c5e59bbff35e1abcc75b94f23e0740d
2 /*
3 * SPU file system
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)
12 * any later version.
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>
25 #include <linux/fs.h>
26 #include <linux/backing-dev.h>
27 #include <linux/init.h>
28 #include <linux/ioctl.h>
29 #include <linux/module.h>
30 #include <linux/mount.h>
31 #include <linux/namei.h>
32 #include <linux/pagemap.h>
33 #include <linux/poll.h>
34 #include <linux/slab.h>
35 #include <linux/parser.h>
37 #include <asm/prom.h>
38 #include <asm/semaphore.h>
39 #include <asm/spu.h>
40 #include <asm/spu_priv1.h>
41 #include <asm/uaccess.h>
43 #include "spufs.h"
45 static struct kmem_cache *spufs_inode_cache;
46 char *isolated_loader;
47 static int isolated_loader_size;
49 static struct inode *
50 spufs_alloc_inode(struct super_block *sb)
52 struct spufs_inode_info *ei;
54 ei = kmem_cache_alloc(spufs_inode_cache, GFP_KERNEL);
55 if (!ei)
56 return NULL;
58 ei->i_gang = NULL;
59 ei->i_ctx = NULL;
60 ei->i_openers = 0;
62 return &ei->vfs_inode;
65 static void
66 spufs_destroy_inode(struct inode *inode)
68 kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
71 static void
72 spufs_init_once(struct kmem_cache *cachep, void *p)
74 struct spufs_inode_info *ei = p;
76 inode_init_once(&ei->vfs_inode);
79 static struct inode *
80 spufs_new_inode(struct super_block *sb, int mode)
82 struct inode *inode;
84 inode = new_inode(sb);
85 if (!inode)
86 goto out;
88 inode->i_mode = mode;
89 inode->i_uid = current->fsuid;
90 inode->i_gid = current->fsgid;
91 inode->i_blocks = 0;
92 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
93 out:
94 return inode;
97 static int
98 spufs_setattr(struct dentry *dentry, struct iattr *attr)
100 struct inode *inode = dentry->d_inode;
102 if ((attr->ia_valid & ATTR_SIZE) &&
103 (attr->ia_size != inode->i_size))
104 return -EINVAL;
105 return inode_setattr(inode, attr);
109 static int
110 spufs_new_file(struct super_block *sb, struct dentry *dentry,
111 const struct file_operations *fops, int mode,
112 struct spu_context *ctx)
114 static struct inode_operations spufs_file_iops = {
115 .setattr = spufs_setattr,
117 struct inode *inode;
118 int ret;
120 ret = -ENOSPC;
121 inode = spufs_new_inode(sb, S_IFREG | mode);
122 if (!inode)
123 goto out;
125 ret = 0;
126 inode->i_op = &spufs_file_iops;
127 inode->i_fop = fops;
128 inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
129 d_add(dentry, inode);
130 out:
131 return ret;
134 static void
135 spufs_delete_inode(struct inode *inode)
137 struct spufs_inode_info *ei = SPUFS_I(inode);
139 if (ei->i_ctx)
140 put_spu_context(ei->i_ctx);
141 if (ei->i_gang)
142 put_spu_gang(ei->i_gang);
143 clear_inode(inode);
146 static void spufs_prune_dir(struct dentry *dir)
148 struct dentry *dentry, *tmp;
150 mutex_lock(&dir->d_inode->i_mutex);
151 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
152 spin_lock(&dcache_lock);
153 spin_lock(&dentry->d_lock);
154 if (!(d_unhashed(dentry)) && dentry->d_inode) {
155 dget_locked(dentry);
156 __d_drop(dentry);
157 spin_unlock(&dentry->d_lock);
158 simple_unlink(dir->d_inode, dentry);
159 spin_unlock(&dcache_lock);
160 dput(dentry);
161 } else {
162 spin_unlock(&dentry->d_lock);
163 spin_unlock(&dcache_lock);
166 shrink_dcache_parent(dir);
167 mutex_unlock(&dir->d_inode->i_mutex);
170 /* Caller must hold parent->i_mutex */
171 static int spufs_rmdir(struct inode *parent, struct dentry *dir)
173 /* remove all entries */
174 spufs_prune_dir(dir);
175 d_drop(dir);
177 return simple_rmdir(parent, dir);
180 static int spufs_fill_dir(struct dentry *dir, struct tree_descr *files,
181 int mode, struct spu_context *ctx)
183 struct dentry *dentry, *tmp;
184 int ret;
186 while (files->name && files->name[0]) {
187 ret = -ENOMEM;
188 dentry = d_alloc_name(dir, files->name);
189 if (!dentry)
190 goto out;
191 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
192 files->mode & mode, ctx);
193 if (ret)
194 goto out;
195 files++;
197 return 0;
198 out:
200 * remove all children from dir. dir->inode is not set so don't
201 * just simply use spufs_prune_dir() and panic afterwards :)
202 * dput() looks like it will do the right thing:
203 * - dec parent's ref counter
204 * - remove child from parent's child list
205 * - free child's inode if possible
206 * - free child
208 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
209 dput(dentry);
212 shrink_dcache_parent(dir);
213 return ret;
216 static int spufs_dir_close(struct inode *inode, struct file *file)
218 struct spu_context *ctx;
219 struct inode *parent;
220 struct dentry *dir;
221 int ret;
223 dir = file->f_path.dentry;
224 parent = dir->d_parent->d_inode;
225 ctx = SPUFS_I(dir->d_inode)->i_ctx;
227 mutex_lock(&parent->i_mutex);
228 ret = spufs_rmdir(parent, dir);
229 mutex_unlock(&parent->i_mutex);
230 WARN_ON(ret);
232 /* We have to give up the mm_struct */
233 spu_forget(ctx);
235 return dcache_dir_close(inode, file);
238 const struct file_operations spufs_context_fops = {
239 .open = dcache_dir_open,
240 .release = spufs_dir_close,
241 .llseek = dcache_dir_lseek,
242 .read = generic_read_dir,
243 .readdir = dcache_readdir,
244 .fsync = simple_sync_file,
246 EXPORT_SYMBOL_GPL(spufs_context_fops);
248 static int
249 spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
250 int mode)
252 int ret;
253 struct inode *inode;
254 struct spu_context *ctx;
256 ret = -ENOSPC;
257 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
258 if (!inode)
259 goto out;
261 if (dir->i_mode & S_ISGID) {
262 inode->i_gid = dir->i_gid;
263 inode->i_mode &= S_ISGID;
265 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
266 SPUFS_I(inode)->i_ctx = ctx;
267 if (!ctx)
268 goto out_iput;
270 ctx->flags = flags;
271 inode->i_op = &simple_dir_inode_operations;
272 inode->i_fop = &simple_dir_operations;
273 if (flags & SPU_CREATE_NOSCHED)
274 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
275 mode, ctx);
276 else
277 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
279 if (ret)
280 goto out_free_ctx;
282 d_instantiate(dentry, inode);
283 dget(dentry);
284 dir->i_nlink++;
285 dentry->d_inode->i_nlink++;
286 goto out;
288 out_free_ctx:
289 spu_forget(ctx);
290 put_spu_context(ctx);
291 out_iput:
292 iput(inode);
293 out:
294 return ret;
297 static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
299 int ret;
300 struct file *filp;
302 ret = get_unused_fd();
303 if (ret < 0) {
304 dput(dentry);
305 mntput(mnt);
306 goto out;
309 filp = dentry_open(dentry, mnt, O_RDONLY);
310 if (IS_ERR(filp)) {
311 put_unused_fd(ret);
312 ret = PTR_ERR(filp);
313 goto out;
316 filp->f_op = &spufs_context_fops;
317 fd_install(ret, filp);
318 out:
319 return ret;
322 static struct spu_context *
323 spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
324 struct file *filp)
326 struct spu_context *tmp, *neighbor, *err;
327 int count, node;
328 int aff_supp;
330 aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
331 struct spu, cbe_list))->aff_list);
333 if (!aff_supp)
334 return ERR_PTR(-EINVAL);
336 if (flags & SPU_CREATE_GANG)
337 return ERR_PTR(-EINVAL);
339 if (flags & SPU_CREATE_AFFINITY_MEM &&
340 gang->aff_ref_ctx &&
341 gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
342 return ERR_PTR(-EEXIST);
344 if (gang->aff_flags & AFF_MERGED)
345 return ERR_PTR(-EBUSY);
347 neighbor = NULL;
348 if (flags & SPU_CREATE_AFFINITY_SPU) {
349 if (!filp || filp->f_op != &spufs_context_fops)
350 return ERR_PTR(-EINVAL);
352 neighbor = get_spu_context(
353 SPUFS_I(filp->f_dentry->d_inode)->i_ctx);
355 if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
356 !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
357 !list_entry(neighbor->aff_list.next, struct spu_context,
358 aff_list)->aff_head) {
359 err = ERR_PTR(-EEXIST);
360 goto out_put_neighbor;
363 if (gang != neighbor->gang) {
364 err = ERR_PTR(-EINVAL);
365 goto out_put_neighbor;
368 count = 1;
369 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
370 count++;
371 if (list_empty(&neighbor->aff_list))
372 count++;
374 for (node = 0; node < MAX_NUMNODES; node++) {
375 if ((cbe_spu_info[node].n_spus - atomic_read(
376 &cbe_spu_info[node].reserved_spus)) >= count)
377 break;
380 if (node == MAX_NUMNODES) {
381 err = ERR_PTR(-EEXIST);
382 goto out_put_neighbor;
386 return neighbor;
388 out_put_neighbor:
389 put_spu_context(neighbor);
390 return err;
393 static void
394 spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
395 struct spu_context *neighbor)
397 if (flags & SPU_CREATE_AFFINITY_MEM)
398 ctx->gang->aff_ref_ctx = ctx;
400 if (flags & SPU_CREATE_AFFINITY_SPU) {
401 if (list_empty(&neighbor->aff_list)) {
402 list_add_tail(&neighbor->aff_list,
403 &ctx->gang->aff_list_head);
404 neighbor->aff_head = 1;
407 if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
408 || list_entry(neighbor->aff_list.next, struct spu_context,
409 aff_list)->aff_head) {
410 list_add(&ctx->aff_list, &neighbor->aff_list);
411 } else {
412 list_add_tail(&ctx->aff_list, &neighbor->aff_list);
413 if (neighbor->aff_head) {
414 neighbor->aff_head = 0;
415 ctx->aff_head = 1;
419 if (!ctx->gang->aff_ref_ctx)
420 ctx->gang->aff_ref_ctx = ctx;
424 static int
425 spufs_create_context(struct inode *inode, struct dentry *dentry,
426 struct vfsmount *mnt, int flags, int mode,
427 struct file *aff_filp)
429 int ret;
430 int affinity;
431 struct spu_gang *gang;
432 struct spu_context *neighbor;
434 ret = -EPERM;
435 if ((flags & SPU_CREATE_NOSCHED) &&
436 !capable(CAP_SYS_NICE))
437 goto out_unlock;
439 ret = -EINVAL;
440 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
441 == SPU_CREATE_ISOLATE)
442 goto out_unlock;
444 ret = -ENODEV;
445 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
446 goto out_unlock;
448 gang = NULL;
449 neighbor = NULL;
450 affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
451 if (affinity) {
452 gang = SPUFS_I(inode)->i_gang;
453 ret = -EINVAL;
454 if (!gang)
455 goto out_unlock;
456 mutex_lock(&gang->aff_mutex);
457 neighbor = spufs_assert_affinity(flags, gang, aff_filp);
458 if (IS_ERR(neighbor)) {
459 ret = PTR_ERR(neighbor);
460 goto out_aff_unlock;
464 ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
465 if (ret)
466 goto out_aff_unlock;
468 if (affinity) {
469 spufs_set_affinity(flags, SPUFS_I(dentry->d_inode)->i_ctx,
470 neighbor);
471 if (neighbor)
472 put_spu_context(neighbor);
476 * get references for dget and mntget, will be released
477 * in error path of *_open().
479 ret = spufs_context_open(dget(dentry), mntget(mnt));
480 if (ret < 0) {
481 WARN_ON(spufs_rmdir(inode, dentry));
482 mutex_unlock(&inode->i_mutex);
483 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
484 goto out;
487 out_aff_unlock:
488 if (affinity)
489 mutex_unlock(&gang->aff_mutex);
490 out_unlock:
491 mutex_unlock(&inode->i_mutex);
492 out:
493 dput(dentry);
494 return ret;
497 static int
498 spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
500 int ret;
501 struct inode *inode;
502 struct spu_gang *gang;
504 ret = -ENOSPC;
505 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
506 if (!inode)
507 goto out;
509 ret = 0;
510 if (dir->i_mode & S_ISGID) {
511 inode->i_gid = dir->i_gid;
512 inode->i_mode &= S_ISGID;
514 gang = alloc_spu_gang();
515 SPUFS_I(inode)->i_ctx = NULL;
516 SPUFS_I(inode)->i_gang = gang;
517 if (!gang)
518 goto out_iput;
520 inode->i_op = &simple_dir_inode_operations;
521 inode->i_fop = &simple_dir_operations;
523 d_instantiate(dentry, inode);
524 dir->i_nlink++;
525 dentry->d_inode->i_nlink++;
526 return ret;
528 out_iput:
529 iput(inode);
530 out:
531 return ret;
534 static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
536 int ret;
537 struct file *filp;
539 ret = get_unused_fd();
540 if (ret < 0) {
541 dput(dentry);
542 mntput(mnt);
543 goto out;
546 filp = dentry_open(dentry, mnt, O_RDONLY);
547 if (IS_ERR(filp)) {
548 put_unused_fd(ret);
549 ret = PTR_ERR(filp);
550 goto out;
553 filp->f_op = &simple_dir_operations;
554 fd_install(ret, filp);
555 out:
556 return ret;
559 static int spufs_create_gang(struct inode *inode,
560 struct dentry *dentry,
561 struct vfsmount *mnt, int mode)
563 int ret;
565 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
566 if (ret)
567 goto out;
570 * get references for dget and mntget, will be released
571 * in error path of *_open().
573 ret = spufs_gang_open(dget(dentry), mntget(mnt));
574 if (ret < 0) {
575 int err = simple_rmdir(inode, dentry);
576 WARN_ON(err);
579 out:
580 mutex_unlock(&inode->i_mutex);
581 dput(dentry);
582 return ret;
586 static struct file_system_type spufs_type;
588 long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode,
589 struct file *filp)
591 struct dentry *dentry;
592 int ret;
594 ret = -EINVAL;
595 /* check if we are on spufs */
596 if (nd->path.dentry->d_sb->s_type != &spufs_type)
597 goto out;
599 /* don't accept undefined flags */
600 if (flags & (~SPU_CREATE_FLAG_ALL))
601 goto out;
603 /* only threads can be underneath a gang */
604 if (nd->path.dentry != nd->path.dentry->d_sb->s_root) {
605 if ((flags & SPU_CREATE_GANG) ||
606 !SPUFS_I(nd->path.dentry->d_inode)->i_gang)
607 goto out;
610 dentry = lookup_create(nd, 1);
611 ret = PTR_ERR(dentry);
612 if (IS_ERR(dentry))
613 goto out_dir;
615 ret = -EEXIST;
616 if (dentry->d_inode)
617 goto out_dput;
619 mode &= ~current->fs->umask;
621 if (flags & SPU_CREATE_GANG)
622 return spufs_create_gang(nd->path.dentry->d_inode,
623 dentry, nd->path.mnt, mode);
624 else
625 return spufs_create_context(nd->path.dentry->d_inode,
626 dentry, nd->path.mnt, flags, mode,
627 filp);
629 out_dput:
630 dput(dentry);
631 out_dir:
632 mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
633 out:
634 return ret;
637 /* File system initialization */
638 enum {
639 Opt_uid, Opt_gid, Opt_mode, Opt_err,
642 static match_table_t spufs_tokens = {
643 { Opt_uid, "uid=%d" },
644 { Opt_gid, "gid=%d" },
645 { Opt_mode, "mode=%o" },
646 { Opt_err, NULL },
649 static int
650 spufs_parse_options(char *options, struct inode *root)
652 char *p;
653 substring_t args[MAX_OPT_ARGS];
655 while ((p = strsep(&options, ",")) != NULL) {
656 int token, option;
658 if (!*p)
659 continue;
661 token = match_token(p, spufs_tokens, args);
662 switch (token) {
663 case Opt_uid:
664 if (match_int(&args[0], &option))
665 return 0;
666 root->i_uid = option;
667 break;
668 case Opt_gid:
669 if (match_int(&args[0], &option))
670 return 0;
671 root->i_gid = option;
672 break;
673 case Opt_mode:
674 if (match_octal(&args[0], &option))
675 return 0;
676 root->i_mode = option | S_IFDIR;
677 break;
678 default:
679 return 0;
682 return 1;
685 static void spufs_exit_isolated_loader(void)
687 free_pages((unsigned long) isolated_loader,
688 get_order(isolated_loader_size));
691 static void
692 spufs_init_isolated_loader(void)
694 struct device_node *dn;
695 const char *loader;
696 int size;
698 dn = of_find_node_by_path("/spu-isolation");
699 if (!dn)
700 return;
702 loader = of_get_property(dn, "loader", &size);
703 if (!loader)
704 return;
706 /* the loader must be align on a 16 byte boundary */
707 isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
708 if (!isolated_loader)
709 return;
711 isolated_loader_size = size;
712 memcpy(isolated_loader, loader, size);
713 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
716 static int
717 spufs_create_root(struct super_block *sb, void *data)
719 struct inode *inode;
720 int ret;
722 ret = -ENODEV;
723 if (!spu_management_ops)
724 goto out;
726 ret = -ENOMEM;
727 inode = spufs_new_inode(sb, S_IFDIR | 0775);
728 if (!inode)
729 goto out;
731 inode->i_op = &simple_dir_inode_operations;
732 inode->i_fop = &simple_dir_operations;
733 SPUFS_I(inode)->i_ctx = NULL;
735 ret = -EINVAL;
736 if (!spufs_parse_options(data, inode))
737 goto out_iput;
739 ret = -ENOMEM;
740 sb->s_root = d_alloc_root(inode);
741 if (!sb->s_root)
742 goto out_iput;
744 return 0;
745 out_iput:
746 iput(inode);
747 out:
748 return ret;
751 static int
752 spufs_fill_super(struct super_block *sb, void *data, int silent)
754 static struct super_operations s_ops = {
755 .alloc_inode = spufs_alloc_inode,
756 .destroy_inode = spufs_destroy_inode,
757 .statfs = simple_statfs,
758 .delete_inode = spufs_delete_inode,
759 .drop_inode = generic_delete_inode,
760 .show_options = generic_show_options,
763 save_mount_options(sb, data);
765 sb->s_maxbytes = MAX_LFS_FILESIZE;
766 sb->s_blocksize = PAGE_CACHE_SIZE;
767 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
768 sb->s_magic = SPUFS_MAGIC;
769 sb->s_op = &s_ops;
771 return spufs_create_root(sb, data);
774 static int
775 spufs_get_sb(struct file_system_type *fstype, int flags,
776 const char *name, void *data, struct vfsmount *mnt)
778 return get_sb_single(fstype, flags, data, spufs_fill_super, mnt);
781 static struct file_system_type spufs_type = {
782 .owner = THIS_MODULE,
783 .name = "spufs",
784 .get_sb = spufs_get_sb,
785 .kill_sb = kill_litter_super,
788 static int __init spufs_init(void)
790 int ret;
792 ret = -ENODEV;
793 if (!spu_management_ops)
794 goto out;
796 ret = -ENOMEM;
797 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
798 sizeof(struct spufs_inode_info), 0,
799 SLAB_HWCACHE_ALIGN, spufs_init_once);
801 if (!spufs_inode_cache)
802 goto out;
803 ret = spu_sched_init();
804 if (ret)
805 goto out_cache;
806 ret = register_filesystem(&spufs_type);
807 if (ret)
808 goto out_sched;
809 ret = register_spu_syscalls(&spufs_calls);
810 if (ret)
811 goto out_fs;
813 spufs_init_isolated_loader();
815 return 0;
817 out_fs:
818 unregister_filesystem(&spufs_type);
819 out_sched:
820 spu_sched_exit();
821 out_cache:
822 kmem_cache_destroy(spufs_inode_cache);
823 out:
824 return ret;
826 module_init(spufs_init);
828 static void __exit spufs_exit(void)
830 spu_sched_exit();
831 spufs_exit_isolated_loader();
832 unregister_spu_syscalls(&spufs_calls);
833 unregister_filesystem(&spufs_type);
834 kmem_cache_destroy(spufs_inode_cache);
836 module_exit(spufs_exit);
838 MODULE_LICENSE("GPL");
839 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");