1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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>
35
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>
41
42#include "spufs.h"
43
44static struct kmem_cache *spufs_inode_cache;
45char *isolated_loader;
46static int isolated_loader_size;
47
48static struct inode *
49spufs_alloc_inode(struct super_block *sb)
50{
51 struct spufs_inode_info *ei;
52
53 ei = kmem_cache_alloc(spufs_inode_cache, GFP_KERNEL);
54 if (!ei)
55 return NULL;
56
57 ei->i_gang = NULL;
58 ei->i_ctx = NULL;
59 ei->i_openers = 0;
60
61 return &ei->vfs_inode;
62}
63
64static void
65spufs_destroy_inode(struct inode *inode)
66{
67 kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
68}
69
70static void
71spufs_init_once(struct kmem_cache *cachep, void *p)
72{
73 struct spufs_inode_info *ei = p;
74
75 inode_init_once(&ei->vfs_inode);
76}
77
78static struct inode *
79spufs_new_inode(struct super_block *sb, int mode)
80{
81 struct inode *inode;
82
83 inode = new_inode(sb);
84 if (!inode)
85 goto out;
86
87 inode->i_mode = mode;
88 inode->i_uid = current->fsuid;
89 inode->i_gid = current->fsgid;
90 inode->i_blocks = 0;
91 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
92out:
93 return inode;
94}
95
96static int
97spufs_setattr(struct dentry *dentry, struct iattr *attr)
98{
99 struct inode *inode = dentry->d_inode;
100
101 if ((attr->ia_valid & ATTR_SIZE) &&
102 (attr->ia_size != inode->i_size))
103 return -EINVAL;
104 return inode_setattr(inode, attr);
105}
106
107
108static int
109spufs_new_file(struct super_block *sb, struct dentry *dentry,
110 const struct file_operations *fops, int mode,
111 struct spu_context *ctx)
112{
113 static struct inode_operations spufs_file_iops = {
114 .setattr = spufs_setattr,
115 };
116 struct inode *inode;
117 int ret;
118
119 ret = -ENOSPC;
120 inode = spufs_new_inode(sb, S_IFREG | mode);
121 if (!inode)
122 goto out;
123
124 ret = 0;
125 inode->i_op = &spufs_file_iops;
126 inode->i_fop = fops;
127 inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
128 d_add(dentry, inode);
129out:
130 return ret;
131}
132
133static void
134spufs_delete_inode(struct inode *inode)
135{
136 struct spufs_inode_info *ei = SPUFS_I(inode);
137
138 if (ei->i_ctx)
139 put_spu_context(ei->i_ctx);
140 if (ei->i_gang)
141 put_spu_gang(ei->i_gang);
142 clear_inode(inode);
143}
144
145static void spufs_prune_dir(struct dentry *dir)
146{
147 struct dentry *dentry, *tmp;
148
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) {
154 dget_locked(dentry);
155 __d_drop(dentry);
156 spin_unlock(&dentry->d_lock);
157 simple_unlink(dir->d_inode, dentry);
158 spin_unlock(&dcache_lock);
159 dput(dentry);
160 } else {
161 spin_unlock(&dentry->d_lock);
162 spin_unlock(&dcache_lock);
163 }
164 }
165 shrink_dcache_parent(dir);
166 mutex_unlock(&dir->d_inode->i_mutex);
167}
168
169
170static int spufs_rmdir(struct inode *parent, struct dentry *dir)
171{
172
173 spufs_prune_dir(dir);
174 d_drop(dir);
175
176 return simple_rmdir(parent, dir);
177}
178
179static int spufs_fill_dir(struct dentry *dir, struct tree_descr *files,
180 int mode, struct spu_context *ctx)
181{
182 struct dentry *dentry, *tmp;
183 int ret;
184
185 while (files->name && files->name[0]) {
186 ret = -ENOMEM;
187 dentry = d_alloc_name(dir, files->name);
188 if (!dentry)
189 goto out;
190 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
191 files->mode & mode, ctx);
192 if (ret)
193 goto out;
194 files++;
195 }
196 return 0;
197out:
198
199
200
201
202
203
204
205
206
207 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
208 dput(dentry);
209 }
210
211 shrink_dcache_parent(dir);
212 return ret;
213}
214
215static int spufs_dir_close(struct inode *inode, struct file *file)
216{
217 struct spu_context *ctx;
218 struct inode *parent;
219 struct dentry *dir;
220 int ret;
221
222 dir = file->f_path.dentry;
223 parent = dir->d_parent->d_inode;
224 ctx = SPUFS_I(dir->d_inode)->i_ctx;
225
226 mutex_lock(&parent->i_mutex);
227 ret = spufs_rmdir(parent, dir);
228 mutex_unlock(&parent->i_mutex);
229 WARN_ON(ret);
230
231
232 spu_forget(ctx);
233
234 return dcache_dir_close(inode, file);
235}
236
237const struct file_operations spufs_context_fops = {
238 .open = dcache_dir_open,
239 .release = spufs_dir_close,
240 .llseek = dcache_dir_lseek,
241 .read = generic_read_dir,
242 .readdir = dcache_readdir,
243 .fsync = simple_sync_file,
244};
245EXPORT_SYMBOL_GPL(spufs_context_fops);
246
247static int
248spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
249 int mode)
250{
251 int ret;
252 struct inode *inode;
253 struct spu_context *ctx;
254
255 ret = -ENOSPC;
256 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
257 if (!inode)
258 goto out;
259
260 if (dir->i_mode & S_ISGID) {
261 inode->i_gid = dir->i_gid;
262 inode->i_mode &= S_ISGID;
263 }
264 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang);
265 SPUFS_I(inode)->i_ctx = ctx;
266 if (!ctx)
267 goto out_iput;
268
269 ctx->flags = flags;
270 inode->i_op = &simple_dir_inode_operations;
271 inode->i_fop = &simple_dir_operations;
272 if (flags & SPU_CREATE_NOSCHED)
273 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
274 mode, ctx);
275 else
276 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
277
278 if (ret)
279 goto out_free_ctx;
280
281 d_instantiate(dentry, inode);
282 dget(dentry);
283 dir->i_nlink++;
284 dentry->d_inode->i_nlink++;
285 goto out;
286
287out_free_ctx:
288 spu_forget(ctx);
289 put_spu_context(ctx);
290out_iput:
291 iput(inode);
292out:
293 return ret;
294}
295
296static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
297{
298 int ret;
299 struct file *filp;
300
301 ret = get_unused_fd();
302 if (ret < 0) {
303 dput(dentry);
304 mntput(mnt);
305 goto out;
306 }
307
308 filp = dentry_open(dentry, mnt, O_RDONLY);
309 if (IS_ERR(filp)) {
310 put_unused_fd(ret);
311 ret = PTR_ERR(filp);
312 goto out;
313 }
314
315 filp->f_op = &spufs_context_fops;
316 fd_install(ret, filp);
317out:
318 return ret;
319}
320
321static struct spu_context *
322spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
323 struct file *filp)
324{
325 struct spu_context *tmp, *neighbor;
326 int count, node;
327 int aff_supp;
328
329 aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
330 struct spu, cbe_list))->aff_list);
331
332 if (!aff_supp)
333 return ERR_PTR(-EINVAL);
334
335 if (flags & SPU_CREATE_GANG)
336 return ERR_PTR(-EINVAL);
337
338 if (flags & SPU_CREATE_AFFINITY_MEM &&
339 gang->aff_ref_ctx &&
340 gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
341 return ERR_PTR(-EEXIST);
342
343 if (gang->aff_flags & AFF_MERGED)
344 return ERR_PTR(-EBUSY);
345
346 neighbor = NULL;
347 if (flags & SPU_CREATE_AFFINITY_SPU) {
348 if (!filp || filp->f_op != &spufs_context_fops)
349 return ERR_PTR(-EINVAL);
350
351 neighbor = get_spu_context(
352 SPUFS_I(filp->f_dentry->d_inode)->i_ctx);
353
354 if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
355 !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
356 !list_entry(neighbor->aff_list.next, struct spu_context,
357 aff_list)->aff_head)
358 return ERR_PTR(-EEXIST);
359
360 if (gang != neighbor->gang)
361 return ERR_PTR(-EINVAL);
362
363 count = 1;
364 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
365 count++;
366 if (list_empty(&neighbor->aff_list))
367 count++;
368
369 for (node = 0; node < MAX_NUMNODES; node++) {
370 if ((cbe_spu_info[node].n_spus - atomic_read(
371 &cbe_spu_info[node].reserved_spus)) >= count)
372 break;
373 }
374
375 if (node == MAX_NUMNODES)
376 return ERR_PTR(-EEXIST);
377 }
378
379 return neighbor;
380}
381
382static void
383spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
384 struct spu_context *neighbor)
385{
386 if (flags & SPU_CREATE_AFFINITY_MEM)
387 ctx->gang->aff_ref_ctx = ctx;
388
389 if (flags & SPU_CREATE_AFFINITY_SPU) {
390 if (list_empty(&neighbor->aff_list)) {
391 list_add_tail(&neighbor->aff_list,
392 &ctx->gang->aff_list_head);
393 neighbor->aff_head = 1;
394 }
395
396 if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
397 || list_entry(neighbor->aff_list.next, struct spu_context,
398 aff_list)->aff_head) {
399 list_add(&ctx->aff_list, &neighbor->aff_list);
400 } else {
401 list_add_tail(&ctx->aff_list, &neighbor->aff_list);
402 if (neighbor->aff_head) {
403 neighbor->aff_head = 0;
404 ctx->aff_head = 1;
405 }
406 }
407
408 if (!ctx->gang->aff_ref_ctx)
409 ctx->gang->aff_ref_ctx = ctx;
410 }
411}
412
413static int
414spufs_create_context(struct inode *inode, struct dentry *dentry,
415 struct vfsmount *mnt, int flags, int mode,
416 struct file *aff_filp)
417{
418 int ret;
419 int affinity;
420 struct spu_gang *gang;
421 struct spu_context *neighbor;
422
423 ret = -EPERM;
424 if ((flags & SPU_CREATE_NOSCHED) &&
425 !capable(CAP_SYS_NICE))
426 goto out_unlock;
427
428 ret = -EINVAL;
429 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
430 == SPU_CREATE_ISOLATE)
431 goto out_unlock;
432
433 ret = -ENODEV;
434 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
435 goto out_unlock;
436
437 gang = NULL;
438 neighbor = NULL;
439 affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
440 if (affinity) {
441 gang = SPUFS_I(inode)->i_gang;
442 ret = -EINVAL;
443 if (!gang)
444 goto out_unlock;
445 mutex_lock(&gang->aff_mutex);
446 neighbor = spufs_assert_affinity(flags, gang, aff_filp);
447 if (IS_ERR(neighbor)) {
448 ret = PTR_ERR(neighbor);
449 goto out_aff_unlock;
450 }
451 }
452
453 ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
454 if (ret)
455 goto out_aff_unlock;
456
457 if (affinity)
458 spufs_set_affinity(flags, SPUFS_I(dentry->d_inode)->i_ctx,
459 neighbor);
460
461
462
463
464
465 ret = spufs_context_open(dget(dentry), mntget(mnt));
466 if (ret < 0) {
467 WARN_ON(spufs_rmdir(inode, dentry));
468 mutex_unlock(&inode->i_mutex);
469 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
470 goto out;
471 }
472
473out_aff_unlock:
474 if (affinity)
475 mutex_unlock(&gang->aff_mutex);
476out_unlock:
477 mutex_unlock(&inode->i_mutex);
478out:
479 dput(dentry);
480 return ret;
481}
482
483static int
484spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
485{
486 int ret;
487 struct inode *inode;
488 struct spu_gang *gang;
489
490 ret = -ENOSPC;
491 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
492 if (!inode)
493 goto out;
494
495 ret = 0;
496 if (dir->i_mode & S_ISGID) {
497 inode->i_gid = dir->i_gid;
498 inode->i_mode &= S_ISGID;
499 }
500 gang = alloc_spu_gang();
501 SPUFS_I(inode)->i_ctx = NULL;
502 SPUFS_I(inode)->i_gang = gang;
503 if (!gang)
504 goto out_iput;
505
506 inode->i_op = &simple_dir_inode_operations;
507 inode->i_fop = &simple_dir_operations;
508
509 d_instantiate(dentry, inode);
510 dir->i_nlink++;
511 dentry->d_inode->i_nlink++;
512 return ret;
513
514out_iput:
515 iput(inode);
516out:
517 return ret;
518}
519
520static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
521{
522 int ret;
523 struct file *filp;
524
525 ret = get_unused_fd();
526 if (ret < 0) {
527 dput(dentry);
528 mntput(mnt);
529 goto out;
530 }
531
532 filp = dentry_open(dentry, mnt, O_RDONLY);
533 if (IS_ERR(filp)) {
534 put_unused_fd(ret);
535 ret = PTR_ERR(filp);
536 goto out;
537 }
538
539 filp->f_op = &simple_dir_operations;
540 fd_install(ret, filp);
541out:
542 return ret;
543}
544
545static int spufs_create_gang(struct inode *inode,
546 struct dentry *dentry,
547 struct vfsmount *mnt, int mode)
548{
549 int ret;
550
551 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
552 if (ret)
553 goto out;
554
555
556
557
558
559 ret = spufs_gang_open(dget(dentry), mntget(mnt));
560 if (ret < 0) {
561 int err = simple_rmdir(inode, dentry);
562 WARN_ON(err);
563 }
564
565out:
566 mutex_unlock(&inode->i_mutex);
567 dput(dentry);
568 return ret;
569}
570
571
572static struct file_system_type spufs_type;
573
574long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode,
575 struct file *filp)
576{
577 struct dentry *dentry;
578 int ret;
579
580 ret = -EINVAL;
581
582 if (nd->dentry->d_sb->s_type != &spufs_type)
583 goto out;
584
585
586 if (flags & (~SPU_CREATE_FLAG_ALL))
587 goto out;
588
589
590 if (nd->dentry != nd->dentry->d_sb->s_root) {
591 if ((flags & SPU_CREATE_GANG) ||
592 !SPUFS_I(nd->dentry->d_inode)->i_gang)
593 goto out;
594 }
595
596 dentry = lookup_create(nd, 1);
597 ret = PTR_ERR(dentry);
598 if (IS_ERR(dentry))
599 goto out_dir;
600
601 ret = -EEXIST;
602 if (dentry->d_inode)
603 goto out_dput;
604
605 mode &= ~current->fs->umask;
606
607 if (flags & SPU_CREATE_GANG)
608 return spufs_create_gang(nd->dentry->d_inode,
609 dentry, nd->mnt, mode);
610 else
611 return spufs_create_context(nd->dentry->d_inode,
612 dentry, nd->mnt, flags, mode, filp);
613
614out_dput:
615 dput(dentry);
616out_dir:
617 mutex_unlock(&nd->dentry->d_inode->i_mutex);
618out:
619 return ret;
620}
621
622
623enum {
624 Opt_uid, Opt_gid, Opt_mode, Opt_err,
625};
626
627static match_table_t spufs_tokens = {
628 { Opt_uid, "uid=%d" },
629 { Opt_gid, "gid=%d" },
630 { Opt_mode, "mode=%o" },
631 { Opt_err, NULL },
632};
633
634static int
635spufs_parse_options(char *options, struct inode *root)
636{
637 char *p;
638 substring_t args[MAX_OPT_ARGS];
639
640 while ((p = strsep(&options, ",")) != NULL) {
641 int token, option;
642
643 if (!*p)
644 continue;
645
646 token = match_token(p, spufs_tokens, args);
647 switch (token) {
648 case Opt_uid:
649 if (match_int(&args[0], &option))
650 return 0;
651 root->i_uid = option;
652 break;
653 case Opt_gid:
654 if (match_int(&args[0], &option))
655 return 0;
656 root->i_gid = option;
657 break;
658 case Opt_mode:
659 if (match_octal(&args[0], &option))
660 return 0;
661 root->i_mode = option | S_IFDIR;
662 break;
663 default:
664 return 0;
665 }
666 }
667 return 1;
668}
669
670static void spufs_exit_isolated_loader(void)
671{
672 free_pages((unsigned long) isolated_loader,
673 get_order(isolated_loader_size));
674}
675
676static void
677spufs_init_isolated_loader(void)
678{
679 struct device_node *dn;
680 const char *loader;
681 int size;
682
683 dn = of_find_node_by_path("/spu-isolation");
684 if (!dn)
685 return;
686
687 loader = of_get_property(dn, "loader", &size);
688 if (!loader)
689 return;
690
691
692 isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
693 if (!isolated_loader)
694 return;
695
696 isolated_loader_size = size;
697 memcpy(isolated_loader, loader, size);
698 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
699}
700
701static int
702spufs_create_root(struct super_block *sb, void *data)
703{
704 struct inode *inode;
705 int ret;
706
707 ret = -ENODEV;
708 if (!spu_management_ops)
709 goto out;
710
711 ret = -ENOMEM;
712 inode = spufs_new_inode(sb, S_IFDIR | 0775);
713 if (!inode)
714 goto out;
715
716 inode->i_op = &simple_dir_inode_operations;
717 inode->i_fop = &simple_dir_operations;
718 SPUFS_I(inode)->i_ctx = NULL;
719
720 ret = -EINVAL;
721 if (!spufs_parse_options(data, inode))
722 goto out_iput;
723
724 ret = -ENOMEM;
725 sb->s_root = d_alloc_root(inode);
726 if (!sb->s_root)
727 goto out_iput;
728
729 return 0;
730out_iput:
731 iput(inode);
732out:
733 return ret;
734}
735
736static int
737spufs_fill_super(struct super_block *sb, void *data, int silent)
738{
739 static struct super_operations s_ops = {
740 .alloc_inode = spufs_alloc_inode,
741 .destroy_inode = spufs_destroy_inode,
742 .statfs = simple_statfs,
743 .delete_inode = spufs_delete_inode,
744 .drop_inode = generic_delete_inode,
745 };
746
747 sb->s_maxbytes = MAX_LFS_FILESIZE;
748 sb->s_blocksize = PAGE_CACHE_SIZE;
749 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
750 sb->s_magic = SPUFS_MAGIC;
751 sb->s_op = &s_ops;
752
753 return spufs_create_root(sb, data);
754}
755
756static int
757spufs_get_sb(struct file_system_type *fstype, int flags,
758 const char *name, void *data, struct vfsmount *mnt)
759{
760 return get_sb_single(fstype, flags, data, spufs_fill_super, mnt);
761}
762
763static struct file_system_type spufs_type = {
764 .owner = THIS_MODULE,
765 .name = "spufs",
766 .get_sb = spufs_get_sb,
767 .kill_sb = kill_litter_super,
768};
769
770static int __init spufs_init(void)
771{
772 int ret;
773
774 ret = -ENODEV;
775 if (!spu_management_ops)
776 goto out;
777
778 ret = -ENOMEM;
779 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
780 sizeof(struct spufs_inode_info), 0,
781 SLAB_HWCACHE_ALIGN, spufs_init_once);
782
783 if (!spufs_inode_cache)
784 goto out;
785 ret = spu_sched_init();
786 if (ret)
787 goto out_cache;
788 ret = register_filesystem(&spufs_type);
789 if (ret)
790 goto out_sched;
791 ret = register_spu_syscalls(&spufs_calls);
792 if (ret)
793 goto out_fs;
794
795 spufs_init_isolated_loader();
796
797 return 0;
798
799out_fs:
800 unregister_filesystem(&spufs_type);
801out_sched:
802 spu_sched_exit();
803out_cache:
804 kmem_cache_destroy(spufs_inode_cache);
805out:
806 return ret;
807}
808module_init(spufs_init);
809
810static void __exit spufs_exit(void)
811{
812 spu_sched_exit();
813 spufs_exit_isolated_loader();
814 unregister_spu_syscalls(&spufs_calls);
815 unregister_filesystem(&spufs_type);
816 kmem_cache_destroy(spufs_inode_cache);
817}
818module_exit(spufs_exit);
819
820MODULE_LICENSE("GPL");
821MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
822
823