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