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 kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
78}
79
80static void spufs_destroy_inode(struct inode *inode)
81{
82 call_rcu(&inode->i_rcu, spufs_i_callback);
83}
84
85static void
86spufs_init_once(void *p)
87{
88 struct spufs_inode_info *ei = p;
89
90 inode_init_once(&ei->vfs_inode);
91}
92
93static struct inode *
94spufs_new_inode(struct super_block *sb, umode_t mode)
95{
96 struct inode *inode;
97
98 inode = new_inode(sb);
99 if (!inode)
100 goto out;
101
102 inode->i_ino = get_next_ino();
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 = d_inode(dentry);
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, umode_t 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 clear_inode(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 inode_lock(d_inode(dir));
167 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_child) {
168 spin_lock(&dentry->d_lock);
169 if (simple_positive(dentry)) {
170 dget_dlock(dentry);
171 __d_drop(dentry);
172 spin_unlock(&dentry->d_lock);
173 simple_unlink(d_inode(dir), dentry);
174
175
176
177 dput(dentry);
178 } else {
179 spin_unlock(&dentry->d_lock);
180 }
181 }
182 shrink_dcache_parent(dir);
183 inode_unlock(d_inode(dir));
184}
185
186
187static int spufs_rmdir(struct inode *parent, struct dentry *dir)
188{
189
190 int res;
191 spufs_prune_dir(dir);
192 d_drop(dir);
193 res = simple_rmdir(parent, dir);
194
195 spu_forget(SPUFS_I(d_inode(dir))->i_ctx);
196 return res;
197}
198
199static int spufs_fill_dir(struct dentry *dir,
200 const struct spufs_tree_descr *files, umode_t mode,
201 struct spu_context *ctx)
202{
203 while (files->name && files->name[0]) {
204 int ret;
205 struct dentry *dentry = d_alloc_name(dir, files->name);
206 if (!dentry)
207 return -ENOMEM;
208 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
209 files->mode & mode, files->size, ctx);
210 if (ret)
211 return ret;
212 files++;
213 }
214 return 0;
215}
216
217static int spufs_dir_close(struct inode *inode, struct file *file)
218{
219 struct spu_context *ctx;
220 struct inode *parent;
221 struct dentry *dir;
222 int ret;
223
224 dir = file->f_path.dentry;
225 parent = d_inode(dir->d_parent);
226 ctx = SPUFS_I(d_inode(dir))->i_ctx;
227
228 inode_lock_nested(parent, I_MUTEX_PARENT);
229 ret = spufs_rmdir(parent, dir);
230 inode_unlock(parent);
231 WARN_ON(ret);
232
233 return dcache_dir_close(inode, file);
234}
235
236const struct file_operations spufs_context_fops = {
237 .open = dcache_dir_open,
238 .release = spufs_dir_close,
239 .llseek = dcache_dir_lseek,
240 .read = generic_read_dir,
241 .iterate = dcache_readdir,
242 .fsync = noop_fsync,
243};
244EXPORT_SYMBOL_GPL(spufs_context_fops);
245
246static int
247spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
248 umode_t mode)
249{
250 int ret;
251 struct inode *inode;
252 struct spu_context *ctx;
253
254 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
255 if (!inode)
256 return -ENOSPC;
257
258 if (dir->i_mode & S_ISGID) {
259 inode->i_gid = dir->i_gid;
260 inode->i_mode &= S_ISGID;
261 }
262 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang);
263 SPUFS_I(inode)->i_ctx = ctx;
264 if (!ctx) {
265 iput(inode);
266 return -ENOSPC;
267 }
268
269 ctx->flags = flags;
270 inode->i_op = &simple_dir_inode_operations;
271 inode->i_fop = &simple_dir_operations;
272
273 inode_lock(inode);
274
275 dget(dentry);
276 inc_nlink(dir);
277 inc_nlink(inode);
278
279 d_instantiate(dentry, inode);
280
281 if (flags & SPU_CREATE_NOSCHED)
282 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
283 mode, ctx);
284 else
285 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
286
287 if (!ret && spufs_get_sb_info(dir->i_sb)->debug)
288 ret = spufs_fill_dir(dentry, spufs_dir_debug_contents,
289 mode, ctx);
290
291 if (ret)
292 spufs_rmdir(dir, dentry);
293
294 inode_unlock(inode);
295
296 return ret;
297}
298
299static int spufs_context_open(struct path *path)
300{
301 int ret;
302 struct file *filp;
303
304 ret = get_unused_fd_flags(0);
305 if (ret < 0)
306 return ret;
307
308 filp = dentry_open(path, O_RDONLY, current_cred());
309 if (IS_ERR(filp)) {
310 put_unused_fd(ret);
311 return PTR_ERR(filp);
312 }
313
314 filp->f_op = &spufs_context_fops;
315 fd_install(ret, filp);
316 return ret;
317}
318
319static struct spu_context *
320spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
321 struct file *filp)
322{
323 struct spu_context *tmp, *neighbor, *err;
324 int count, node;
325 int aff_supp;
326
327 aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
328 struct spu, cbe_list))->aff_list);
329
330 if (!aff_supp)
331 return ERR_PTR(-EINVAL);
332
333 if (flags & SPU_CREATE_GANG)
334 return ERR_PTR(-EINVAL);
335
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);
340
341 if (gang->aff_flags & AFF_MERGED)
342 return ERR_PTR(-EBUSY);
343
344 neighbor = NULL;
345 if (flags & SPU_CREATE_AFFINITY_SPU) {
346 if (!filp || filp->f_op != &spufs_context_fops)
347 return ERR_PTR(-EINVAL);
348
349 neighbor = get_spu_context(
350 SPUFS_I(file_inode(filp))->i_ctx);
351
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 err = ERR_PTR(-EEXIST);
357 goto out_put_neighbor;
358 }
359
360 if (gang != neighbor->gang) {
361 err = ERR_PTR(-EINVAL);
362 goto out_put_neighbor;
363 }
364
365 count = 1;
366 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
367 count++;
368 if (list_empty(&neighbor->aff_list))
369 count++;
370
371 for (node = 0; node < MAX_NUMNODES; node++) {
372 if ((cbe_spu_info[node].n_spus - atomic_read(
373 &cbe_spu_info[node].reserved_spus)) >= count)
374 break;
375 }
376
377 if (node == MAX_NUMNODES) {
378 err = ERR_PTR(-EEXIST);
379 goto out_put_neighbor;
380 }
381 }
382
383 return neighbor;
384
385out_put_neighbor:
386 put_spu_context(neighbor);
387 return err;
388}
389
390static void
391spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
392 struct spu_context *neighbor)
393{
394 if (flags & SPU_CREATE_AFFINITY_MEM)
395 ctx->gang->aff_ref_ctx = ctx;
396
397 if (flags & SPU_CREATE_AFFINITY_SPU) {
398 if (list_empty(&neighbor->aff_list)) {
399 list_add_tail(&neighbor->aff_list,
400 &ctx->gang->aff_list_head);
401 neighbor->aff_head = 1;
402 }
403
404 if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
405 || list_entry(neighbor->aff_list.next, struct spu_context,
406 aff_list)->aff_head) {
407 list_add(&ctx->aff_list, &neighbor->aff_list);
408 } else {
409 list_add_tail(&ctx->aff_list, &neighbor->aff_list);
410 if (neighbor->aff_head) {
411 neighbor->aff_head = 0;
412 ctx->aff_head = 1;
413 }
414 }
415
416 if (!ctx->gang->aff_ref_ctx)
417 ctx->gang->aff_ref_ctx = ctx;
418 }
419}
420
421static int
422spufs_create_context(struct inode *inode, struct dentry *dentry,
423 struct vfsmount *mnt, int flags, umode_t mode,
424 struct file *aff_filp)
425{
426 int ret;
427 int affinity;
428 struct spu_gang *gang;
429 struct spu_context *neighbor;
430 struct path path = {.mnt = mnt, .dentry = dentry};
431
432 if ((flags & SPU_CREATE_NOSCHED) &&
433 !capable(CAP_SYS_NICE))
434 return -EPERM;
435
436 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
437 == SPU_CREATE_ISOLATE)
438 return -EINVAL;
439
440 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
441 return -ENODEV;
442
443 gang = NULL;
444 neighbor = NULL;
445 affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
446 if (affinity) {
447 gang = SPUFS_I(inode)->i_gang;
448 if (!gang)
449 return -EINVAL;
450 mutex_lock(&gang->aff_mutex);
451 neighbor = spufs_assert_affinity(flags, gang, aff_filp);
452 if (IS_ERR(neighbor)) {
453 ret = PTR_ERR(neighbor);
454 goto out_aff_unlock;
455 }
456 }
457
458 ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
459 if (ret)
460 goto out_aff_unlock;
461
462 if (affinity) {
463 spufs_set_affinity(flags, SPUFS_I(d_inode(dentry))->i_ctx,
464 neighbor);
465 if (neighbor)
466 put_spu_context(neighbor);
467 }
468
469 ret = spufs_context_open(&path);
470 if (ret < 0)
471 WARN_ON(spufs_rmdir(inode, dentry));
472
473out_aff_unlock:
474 if (affinity)
475 mutex_unlock(&gang->aff_mutex);
476 return ret;
477}
478
479static int
480spufs_mkgang(struct inode *dir, struct dentry *dentry, umode_t mode)
481{
482 int ret;
483 struct inode *inode;
484 struct spu_gang *gang;
485
486 ret = -ENOSPC;
487 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
488 if (!inode)
489 goto out;
490
491 ret = 0;
492 if (dir->i_mode & S_ISGID) {
493 inode->i_gid = dir->i_gid;
494 inode->i_mode &= S_ISGID;
495 }
496 gang = alloc_spu_gang();
497 SPUFS_I(inode)->i_ctx = NULL;
498 SPUFS_I(inode)->i_gang = gang;
499 if (!gang)
500 goto out_iput;
501
502 inode->i_op = &simple_dir_inode_operations;
503 inode->i_fop = &simple_dir_operations;
504
505 d_instantiate(dentry, inode);
506 inc_nlink(dir);
507 inc_nlink(d_inode(dentry));
508 return ret;
509
510out_iput:
511 iput(inode);
512out:
513 return ret;
514}
515
516static int spufs_gang_open(struct path *path)
517{
518 int ret;
519 struct file *filp;
520
521 ret = get_unused_fd_flags(0);
522 if (ret < 0)
523 return ret;
524
525
526
527
528
529 filp = dentry_open(path, O_RDONLY, current_cred());
530 if (IS_ERR(filp)) {
531 put_unused_fd(ret);
532 return PTR_ERR(filp);
533 }
534
535 filp->f_op = &simple_dir_operations;
536 fd_install(ret, filp);
537 return ret;
538}
539
540static int spufs_create_gang(struct inode *inode,
541 struct dentry *dentry,
542 struct vfsmount *mnt, umode_t mode)
543{
544 struct path path = {.mnt = mnt, .dentry = dentry};
545 int ret;
546
547 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
548 if (!ret) {
549 ret = spufs_gang_open(&path);
550 if (ret < 0) {
551 int err = simple_rmdir(inode, dentry);
552 WARN_ON(err);
553 }
554 }
555 return ret;
556}
557
558
559static struct file_system_type spufs_type;
560
561long spufs_create(struct path *path, struct dentry *dentry,
562 unsigned int flags, umode_t mode, struct file *filp)
563{
564 struct inode *dir = d_inode(path->dentry);
565 int ret;
566
567
568 if (path->dentry->d_sb->s_type != &spufs_type)
569 return -EINVAL;
570
571
572 if (flags & (~SPU_CREATE_FLAG_ALL))
573 return -EINVAL;
574
575
576 if (path->dentry != path->dentry->d_sb->s_root)
577 if ((flags & SPU_CREATE_GANG) || !SPUFS_I(dir)->i_gang)
578 return -EINVAL;
579
580 mode &= ~current_umask();
581
582 if (flags & SPU_CREATE_GANG)
583 ret = spufs_create_gang(dir, dentry, path->mnt, mode);
584 else
585 ret = spufs_create_context(dir, dentry, path->mnt, flags, mode,
586 filp);
587 if (ret >= 0)
588 fsnotify_mkdir(dir, dentry);
589
590 return ret;
591}
592
593
594enum {
595 Opt_uid, Opt_gid, Opt_mode, Opt_debug, Opt_err,
596};
597
598static const match_table_t spufs_tokens = {
599 { Opt_uid, "uid=%d" },
600 { Opt_gid, "gid=%d" },
601 { Opt_mode, "mode=%o" },
602 { Opt_debug, "debug" },
603 { Opt_err, NULL },
604};
605
606static int
607spufs_parse_options(struct super_block *sb, char *options, struct inode *root)
608{
609 char *p;
610 substring_t args[MAX_OPT_ARGS];
611
612 while ((p = strsep(&options, ",")) != NULL) {
613 int token, option;
614
615 if (!*p)
616 continue;
617
618 token = match_token(p, spufs_tokens, args);
619 switch (token) {
620 case Opt_uid:
621 if (match_int(&args[0], &option))
622 return 0;
623 root->i_uid = make_kuid(current_user_ns(), option);
624 if (!uid_valid(root->i_uid))
625 return 0;
626 break;
627 case Opt_gid:
628 if (match_int(&args[0], &option))
629 return 0;
630 root->i_gid = make_kgid(current_user_ns(), option);
631 if (!gid_valid(root->i_gid))
632 return 0;
633 break;
634 case Opt_mode:
635 if (match_octal(&args[0], &option))
636 return 0;
637 root->i_mode = option | S_IFDIR;
638 break;
639 case Opt_debug:
640 spufs_get_sb_info(sb)->debug = 1;
641 break;
642 default:
643 return 0;
644 }
645 }
646 return 1;
647}
648
649static void spufs_exit_isolated_loader(void)
650{
651 free_pages((unsigned long) isolated_loader,
652 get_order(isolated_loader_size));
653}
654
655static void
656spufs_init_isolated_loader(void)
657{
658 struct device_node *dn;
659 const char *loader;
660 int size;
661
662 dn = of_find_node_by_path("/spu-isolation");
663 if (!dn)
664 return;
665
666 loader = of_get_property(dn, "loader", &size);
667 if (!loader)
668 return;
669
670
671 isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
672 if (!isolated_loader)
673 return;
674
675 isolated_loader_size = size;
676 memcpy(isolated_loader, loader, size);
677 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
678}
679
680static int
681spufs_create_root(struct super_block *sb, void *data)
682{
683 struct inode *inode;
684 int ret;
685
686 ret = -ENODEV;
687 if (!spu_management_ops)
688 goto out;
689
690 ret = -ENOMEM;
691 inode = spufs_new_inode(sb, S_IFDIR | 0775);
692 if (!inode)
693 goto out;
694
695 inode->i_op = &simple_dir_inode_operations;
696 inode->i_fop = &simple_dir_operations;
697 SPUFS_I(inode)->i_ctx = NULL;
698 inc_nlink(inode);
699
700 ret = -EINVAL;
701 if (!spufs_parse_options(sb, data, inode))
702 goto out_iput;
703
704 ret = -ENOMEM;
705 sb->s_root = d_make_root(inode);
706 if (!sb->s_root)
707 goto out;
708
709 return 0;
710out_iput:
711 iput(inode);
712out:
713 return ret;
714}
715
716static int
717spufs_fill_super(struct super_block *sb, void *data, int silent)
718{
719 struct spufs_sb_info *info;
720 static const struct super_operations s_ops = {
721 .alloc_inode = spufs_alloc_inode,
722 .destroy_inode = spufs_destroy_inode,
723 .statfs = simple_statfs,
724 .evict_inode = spufs_evict_inode,
725 .show_options = generic_show_options,
726 };
727
728 save_mount_options(sb, data);
729
730 info = kzalloc(sizeof(*info), GFP_KERNEL);
731 if (!info)
732 return -ENOMEM;
733
734 sb->s_maxbytes = MAX_LFS_FILESIZE;
735 sb->s_blocksize = PAGE_SIZE;
736 sb->s_blocksize_bits = PAGE_SHIFT;
737 sb->s_magic = SPUFS_MAGIC;
738 sb->s_op = &s_ops;
739 sb->s_fs_info = info;
740
741 return spufs_create_root(sb, data);
742}
743
744static struct dentry *
745spufs_mount(struct file_system_type *fstype, int flags,
746 const char *name, void *data)
747{
748 return mount_single(fstype, flags, data, spufs_fill_super);
749}
750
751static struct file_system_type spufs_type = {
752 .owner = THIS_MODULE,
753 .name = "spufs",
754 .mount = spufs_mount,
755 .kill_sb = kill_litter_super,
756};
757MODULE_ALIAS_FS("spufs");
758
759static int __init spufs_init(void)
760{
761 int ret;
762
763 ret = -ENODEV;
764 if (!spu_management_ops)
765 goto out;
766
767 ret = -ENOMEM;
768 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
769 sizeof(struct spufs_inode_info), 0,
770 SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT, spufs_init_once);
771
772 if (!spufs_inode_cache)
773 goto out;
774 ret = spu_sched_init();
775 if (ret)
776 goto out_cache;
777 ret = register_spu_syscalls(&spufs_calls);
778 if (ret)
779 goto out_sched;
780 ret = register_filesystem(&spufs_type);
781 if (ret)
782 goto out_syscalls;
783
784 spufs_init_isolated_loader();
785
786 return 0;
787
788out_syscalls:
789 unregister_spu_syscalls(&spufs_calls);
790out_sched:
791 spu_sched_exit();
792out_cache:
793 kmem_cache_destroy(spufs_inode_cache);
794out:
795 return ret;
796}
797module_init(spufs_init);
798
799static void __exit spufs_exit(void)
800{
801 spu_sched_exit();
802 spufs_exit_isolated_loader();
803 unregister_spu_syscalls(&spufs_calls);
804 unregister_filesystem(&spufs_type);
805 kmem_cache_destroy(spufs_inode_cache);
806}
807module_exit(spufs_exit);
808
809MODULE_LICENSE("GPL");
810MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
811
812