1 // SPDX-License-Identifier: GPL-2.0-or-later
6 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
8 * Author: Arnd Bergmann <arndb@de.ibm.com>
11 #include <linux/file.h>
13 #include <linux/fsnotify.h>
14 #include <linux/backing-dev.h>
15 #include <linux/init.h>
16 #include <linux/ioctl.h>
17 #include <linux/module.h>
18 #include <linux/mount.h>
19 #include <linux/namei.h>
20 #include <linux/pagemap.h>
21 #include <linux/poll.h>
22 #include <linux/slab.h>
23 #include <linux/parser.h>
27 #include <asm/spu_priv1.h>
28 #include <linux/uaccess.h>
32 struct spufs_sb_info {
36 static struct kmem_cache *spufs_inode_cache;
37 char *isolated_loader;
38 static int isolated_loader_size;
40 static struct spufs_sb_info *spufs_get_sb_info(struct super_block *sb)
46 spufs_alloc_inode(struct super_block *sb)
48 struct spufs_inode_info *ei;
50 ei = kmem_cache_alloc(spufs_inode_cache, GFP_KERNEL);
58 return &ei->vfs_inode;
61 static void spufs_free_inode(struct inode *inode)
63 kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
67 spufs_init_once(void *p)
69 struct spufs_inode_info *ei = p;
71 inode_init_once(&ei->vfs_inode);
75 spufs_new_inode(struct super_block *sb, umode_t mode)
79 inode = new_inode(sb);
83 inode->i_ino = get_next_ino();
85 inode->i_uid = current_fsuid();
86 inode->i_gid = current_fsgid();
87 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
93 spufs_setattr(struct dentry *dentry, struct iattr *attr)
95 struct inode *inode = d_inode(dentry);
97 if ((attr->ia_valid & ATTR_SIZE) &&
98 (attr->ia_size != inode->i_size))
100 setattr_copy(inode, attr);
101 mark_inode_dirty(inode);
107 spufs_new_file(struct super_block *sb, struct dentry *dentry,
108 const struct file_operations *fops, umode_t mode,
109 size_t size, struct spu_context *ctx)
111 static const struct inode_operations spufs_file_iops = {
112 .setattr = spufs_setattr,
118 inode = spufs_new_inode(sb, S_IFREG | mode);
123 inode->i_op = &spufs_file_iops;
125 inode->i_size = size;
126 inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
127 d_add(dentry, inode);
133 spufs_evict_inode(struct inode *inode)
135 struct spufs_inode_info *ei = SPUFS_I(inode);
138 put_spu_context(ei->i_ctx);
140 put_spu_gang(ei->i_gang);
143 static void spufs_prune_dir(struct dentry *dir)
145 struct dentry *dentry, *tmp;
147 inode_lock(d_inode(dir));
148 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_child) {
149 spin_lock(&dentry->d_lock);
150 if (simple_positive(dentry)) {
153 spin_unlock(&dentry->d_lock);
154 simple_unlink(d_inode(dir), dentry);
155 /* XXX: what was dcache_lock protecting here? Other
156 * filesystems (IB, configfs) release dcache_lock
160 spin_unlock(&dentry->d_lock);
163 shrink_dcache_parent(dir);
164 inode_unlock(d_inode(dir));
167 /* Caller must hold parent->i_mutex */
168 static int spufs_rmdir(struct inode *parent, struct dentry *dir)
170 /* remove all entries */
172 spufs_prune_dir(dir);
174 res = simple_rmdir(parent, dir);
175 /* We have to give up the mm_struct */
176 spu_forget(SPUFS_I(d_inode(dir))->i_ctx);
180 static int spufs_fill_dir(struct dentry *dir,
181 const struct spufs_tree_descr *files, umode_t mode,
182 struct spu_context *ctx)
184 while (files->name && files->name[0]) {
186 struct dentry *dentry = d_alloc_name(dir, files->name);
189 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
190 files->mode & mode, files->size, ctx);
198 static int spufs_dir_close(struct inode *inode, struct file *file)
200 struct spu_context *ctx;
201 struct inode *parent;
205 dir = file->f_path.dentry;
206 parent = d_inode(dir->d_parent);
207 ctx = SPUFS_I(d_inode(dir))->i_ctx;
209 inode_lock_nested(parent, I_MUTEX_PARENT);
210 ret = spufs_rmdir(parent, dir);
211 inode_unlock(parent);
214 return dcache_dir_close(inode, file);
217 const struct file_operations spufs_context_fops = {
218 .open = dcache_dir_open,
219 .release = spufs_dir_close,
220 .llseek = dcache_dir_lseek,
221 .read = generic_read_dir,
222 .iterate_shared = dcache_readdir,
225 EXPORT_SYMBOL_GPL(spufs_context_fops);
228 spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
233 struct spu_context *ctx;
235 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
239 if (dir->i_mode & S_ISGID) {
240 inode->i_gid = dir->i_gid;
241 inode->i_mode &= S_ISGID;
243 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
244 SPUFS_I(inode)->i_ctx = ctx;
251 inode->i_op = &simple_dir_inode_operations;
252 inode->i_fop = &simple_dir_operations;
260 d_instantiate(dentry, inode);
262 if (flags & SPU_CREATE_NOSCHED)
263 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
266 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
268 if (!ret && spufs_get_sb_info(dir->i_sb)->debug)
269 ret = spufs_fill_dir(dentry, spufs_dir_debug_contents,
273 spufs_rmdir(dir, dentry);
280 static int spufs_context_open(struct path *path)
285 ret = get_unused_fd_flags(0);
289 filp = dentry_open(path, O_RDONLY, current_cred());
292 return PTR_ERR(filp);
295 filp->f_op = &spufs_context_fops;
296 fd_install(ret, filp);
300 static struct spu_context *
301 spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
304 struct spu_context *tmp, *neighbor, *err;
308 aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
309 struct spu, cbe_list))->aff_list);
312 return ERR_PTR(-EINVAL);
314 if (flags & SPU_CREATE_GANG)
315 return ERR_PTR(-EINVAL);
317 if (flags & SPU_CREATE_AFFINITY_MEM &&
319 gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
320 return ERR_PTR(-EEXIST);
322 if (gang->aff_flags & AFF_MERGED)
323 return ERR_PTR(-EBUSY);
326 if (flags & SPU_CREATE_AFFINITY_SPU) {
327 if (!filp || filp->f_op != &spufs_context_fops)
328 return ERR_PTR(-EINVAL);
330 neighbor = get_spu_context(
331 SPUFS_I(file_inode(filp))->i_ctx);
333 if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
334 !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
335 !list_entry(neighbor->aff_list.next, struct spu_context,
336 aff_list)->aff_head) {
337 err = ERR_PTR(-EEXIST);
338 goto out_put_neighbor;
341 if (gang != neighbor->gang) {
342 err = ERR_PTR(-EINVAL);
343 goto out_put_neighbor;
347 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
349 if (list_empty(&neighbor->aff_list))
352 for (node = 0; node < MAX_NUMNODES; node++) {
353 if ((cbe_spu_info[node].n_spus - atomic_read(
354 &cbe_spu_info[node].reserved_spus)) >= count)
358 if (node == MAX_NUMNODES) {
359 err = ERR_PTR(-EEXIST);
360 goto out_put_neighbor;
367 put_spu_context(neighbor);
372 spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
373 struct spu_context *neighbor)
375 if (flags & SPU_CREATE_AFFINITY_MEM)
376 ctx->gang->aff_ref_ctx = ctx;
378 if (flags & SPU_CREATE_AFFINITY_SPU) {
379 if (list_empty(&neighbor->aff_list)) {
380 list_add_tail(&neighbor->aff_list,
381 &ctx->gang->aff_list_head);
382 neighbor->aff_head = 1;
385 if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
386 || list_entry(neighbor->aff_list.next, struct spu_context,
387 aff_list)->aff_head) {
388 list_add(&ctx->aff_list, &neighbor->aff_list);
390 list_add_tail(&ctx->aff_list, &neighbor->aff_list);
391 if (neighbor->aff_head) {
392 neighbor->aff_head = 0;
397 if (!ctx->gang->aff_ref_ctx)
398 ctx->gang->aff_ref_ctx = ctx;
403 spufs_create_context(struct inode *inode, struct dentry *dentry,
404 struct vfsmount *mnt, int flags, umode_t mode,
405 struct file *aff_filp)
409 struct spu_gang *gang;
410 struct spu_context *neighbor;
411 struct path path = {.mnt = mnt, .dentry = dentry};
413 if ((flags & SPU_CREATE_NOSCHED) &&
414 !capable(CAP_SYS_NICE))
417 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
418 == SPU_CREATE_ISOLATE)
421 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
426 affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
428 gang = SPUFS_I(inode)->i_gang;
431 mutex_lock(&gang->aff_mutex);
432 neighbor = spufs_assert_affinity(flags, gang, aff_filp);
433 if (IS_ERR(neighbor)) {
434 ret = PTR_ERR(neighbor);
439 ret = spufs_mkdir(inode, dentry, flags, mode & 0777);
444 spufs_set_affinity(flags, SPUFS_I(d_inode(dentry))->i_ctx,
447 put_spu_context(neighbor);
450 ret = spufs_context_open(&path);
452 WARN_ON(spufs_rmdir(inode, dentry));
456 mutex_unlock(&gang->aff_mutex);
461 spufs_mkgang(struct inode *dir, struct dentry *dentry, umode_t mode)
465 struct spu_gang *gang;
468 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
473 if (dir->i_mode & S_ISGID) {
474 inode->i_gid = dir->i_gid;
475 inode->i_mode &= S_ISGID;
477 gang = alloc_spu_gang();
478 SPUFS_I(inode)->i_ctx = NULL;
479 SPUFS_I(inode)->i_gang = gang;
485 inode->i_op = &simple_dir_inode_operations;
486 inode->i_fop = &simple_dir_operations;
488 d_instantiate(dentry, inode);
490 inc_nlink(d_inode(dentry));
499 static int spufs_gang_open(struct path *path)
504 ret = get_unused_fd_flags(0);
509 * get references for dget and mntget, will be released
510 * in error path of *_open().
512 filp = dentry_open(path, O_RDONLY, current_cred());
515 return PTR_ERR(filp);
518 filp->f_op = &simple_dir_operations;
519 fd_install(ret, filp);
523 static int spufs_create_gang(struct inode *inode,
524 struct dentry *dentry,
525 struct vfsmount *mnt, umode_t mode)
527 struct path path = {.mnt = mnt, .dentry = dentry};
530 ret = spufs_mkgang(inode, dentry, mode & 0777);
532 ret = spufs_gang_open(&path);
534 int err = simple_rmdir(inode, dentry);
542 static struct file_system_type spufs_type;
544 long spufs_create(struct path *path, struct dentry *dentry,
545 unsigned int flags, umode_t mode, struct file *filp)
547 struct inode *dir = d_inode(path->dentry);
550 /* check if we are on spufs */
551 if (path->dentry->d_sb->s_type != &spufs_type)
554 /* don't accept undefined flags */
555 if (flags & (~SPU_CREATE_FLAG_ALL))
558 /* only threads can be underneath a gang */
559 if (path->dentry != path->dentry->d_sb->s_root)
560 if ((flags & SPU_CREATE_GANG) || !SPUFS_I(dir)->i_gang)
563 mode &= ~current_umask();
565 if (flags & SPU_CREATE_GANG)
566 ret = spufs_create_gang(dir, dentry, path->mnt, mode);
568 ret = spufs_create_context(dir, dentry, path->mnt, flags, mode,
571 fsnotify_mkdir(dir, dentry);
576 /* File system initialization */
578 Opt_uid, Opt_gid, Opt_mode, Opt_debug, Opt_err,
581 static const match_table_t spufs_tokens = {
582 { Opt_uid, "uid=%d" },
583 { Opt_gid, "gid=%d" },
584 { Opt_mode, "mode=%o" },
585 { Opt_debug, "debug" },
589 static int spufs_show_options(struct seq_file *m, struct dentry *root)
591 struct spufs_sb_info *sbi = spufs_get_sb_info(root->d_sb);
592 struct inode *inode = root->d_inode;
594 if (!uid_eq(inode->i_uid, GLOBAL_ROOT_UID))
595 seq_printf(m, ",uid=%u",
596 from_kuid_munged(&init_user_ns, inode->i_uid));
597 if (!gid_eq(inode->i_gid, GLOBAL_ROOT_GID))
598 seq_printf(m, ",gid=%u",
599 from_kgid_munged(&init_user_ns, inode->i_gid));
600 if ((inode->i_mode & S_IALLUGO) != 0775)
601 seq_printf(m, ",mode=%o", inode->i_mode);
603 seq_puts(m, ",debug");
608 spufs_parse_options(struct super_block *sb, char *options, struct inode *root)
611 substring_t args[MAX_OPT_ARGS];
613 while ((p = strsep(&options, ",")) != NULL) {
619 token = match_token(p, spufs_tokens, args);
622 if (match_int(&args[0], &option))
624 root->i_uid = make_kuid(current_user_ns(), option);
625 if (!uid_valid(root->i_uid))
629 if (match_int(&args[0], &option))
631 root->i_gid = make_kgid(current_user_ns(), option);
632 if (!gid_valid(root->i_gid))
636 if (match_octal(&args[0], &option))
638 root->i_mode = option | S_IFDIR;
641 spufs_get_sb_info(sb)->debug = 1;
650 static void spufs_exit_isolated_loader(void)
652 free_pages((unsigned long) isolated_loader,
653 get_order(isolated_loader_size));
657 spufs_init_isolated_loader(void)
659 struct device_node *dn;
663 dn = of_find_node_by_path("/spu-isolation");
667 loader = of_get_property(dn, "loader", &size);
671 /* the loader must be align on a 16 byte boundary */
672 isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
673 if (!isolated_loader)
676 isolated_loader_size = size;
677 memcpy(isolated_loader, loader, size);
678 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
682 spufs_create_root(struct super_block *sb, void *data)
688 if (!spu_management_ops)
692 inode = spufs_new_inode(sb, S_IFDIR | 0775);
696 inode->i_op = &simple_dir_inode_operations;
697 inode->i_fop = &simple_dir_operations;
698 SPUFS_I(inode)->i_ctx = NULL;
702 if (!spufs_parse_options(sb, data, inode))
706 sb->s_root = d_make_root(inode);
718 spufs_fill_super(struct super_block *sb, void *data, int silent)
720 struct spufs_sb_info *info;
721 static const struct super_operations s_ops = {
722 .alloc_inode = spufs_alloc_inode,
723 .free_inode = spufs_free_inode,
724 .statfs = simple_statfs,
725 .evict_inode = spufs_evict_inode,
726 .show_options = spufs_show_options,
729 info = kzalloc(sizeof(*info), GFP_KERNEL);
733 sb->s_maxbytes = MAX_LFS_FILESIZE;
734 sb->s_blocksize = PAGE_SIZE;
735 sb->s_blocksize_bits = PAGE_SHIFT;
736 sb->s_magic = SPUFS_MAGIC;
738 sb->s_fs_info = info;
740 return spufs_create_root(sb, data);
743 static struct dentry *
744 spufs_mount(struct file_system_type *fstype, int flags,
745 const char *name, void *data)
747 return mount_single(fstype, flags, data, spufs_fill_super);
750 static struct file_system_type spufs_type = {
751 .owner = THIS_MODULE,
753 .mount = spufs_mount,
754 .kill_sb = kill_litter_super,
756 MODULE_ALIAS_FS("spufs");
758 static int __init spufs_init(void)
763 if (!spu_management_ops)
767 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
768 sizeof(struct spufs_inode_info), 0,
769 SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT, spufs_init_once);
771 if (!spufs_inode_cache)
773 ret = spu_sched_init();
776 ret = register_spu_syscalls(&spufs_calls);
779 ret = register_filesystem(&spufs_type);
783 spufs_init_isolated_loader();
788 unregister_spu_syscalls(&spufs_calls);
792 kmem_cache_destroy(spufs_inode_cache);
796 module_init(spufs_init);
798 static void __exit spufs_exit(void)
801 spufs_exit_isolated_loader();
802 unregister_spu_syscalls(&spufs_calls);
803 unregister_filesystem(&spufs_type);
804 kmem_cache_destroy(spufs_inode_cache);
806 module_exit(spufs_exit);
808 MODULE_LICENSE("GPL");
809 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");