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