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/fs_context.h>
14 #include <linux/fs_parser.h>
15 #include <linux/fsnotify.h>
16 #include <linux/backing-dev.h>
17 #include <linux/init.h>
18 #include <linux/ioctl.h>
19 #include <linux/module.h>
20 #include <linux/mount.h>
21 #include <linux/namei.h>
22 #include <linux/pagemap.h>
23 #include <linux/poll.h>
24 #include <linux/slab.h>
28 #include <asm/spu_priv1.h>
29 #include <linux/uaccess.h>
33 struct spufs_sb_info {
37 static struct kmem_cache *spufs_inode_cache;
38 char *isolated_loader;
39 static int isolated_loader_size;
41 static struct spufs_sb_info *spufs_get_sb_info(struct super_block *sb)
47 spufs_alloc_inode(struct super_block *sb)
49 struct spufs_inode_info *ei;
51 ei = kmem_cache_alloc(spufs_inode_cache, GFP_KERNEL);
59 return &ei->vfs_inode;
62 static void spufs_free_inode(struct inode *inode)
64 kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
68 spufs_init_once(void *p)
70 struct spufs_inode_info *ei = p;
72 inode_init_once(&ei->vfs_inode);
76 spufs_new_inode(struct super_block *sb, umode_t mode)
80 inode = new_inode(sb);
84 inode->i_ino = get_next_ino();
86 inode->i_uid = current_fsuid();
87 inode->i_gid = current_fsgid();
88 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
94 spufs_setattr(struct dentry *dentry, struct iattr *attr)
96 struct inode *inode = d_inode(dentry);
98 if ((attr->ia_valid & ATTR_SIZE) &&
99 (attr->ia_size != inode->i_size))
101 setattr_copy(inode, attr);
102 mark_inode_dirty(inode);
108 spufs_new_file(struct super_block *sb, struct dentry *dentry,
109 const struct file_operations *fops, umode_t mode,
110 size_t size, struct spu_context *ctx)
112 static const struct inode_operations spufs_file_iops = {
113 .setattr = spufs_setattr,
119 inode = spufs_new_inode(sb, S_IFREG | mode);
124 inode->i_op = &spufs_file_iops;
126 inode->i_size = size;
127 inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
128 d_add(dentry, inode);
134 spufs_evict_inode(struct inode *inode)
136 struct spufs_inode_info *ei = SPUFS_I(inode);
139 put_spu_context(ei->i_ctx);
141 put_spu_gang(ei->i_gang);
144 static void spufs_prune_dir(struct dentry *dir)
146 struct dentry *dentry, *tmp;
148 inode_lock(d_inode(dir));
149 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_child) {
150 spin_lock(&dentry->d_lock);
151 if (simple_positive(dentry)) {
154 spin_unlock(&dentry->d_lock);
155 simple_unlink(d_inode(dir), dentry);
156 /* XXX: what was dcache_lock protecting here? Other
157 * filesystems (IB, configfs) release dcache_lock
161 spin_unlock(&dentry->d_lock);
164 shrink_dcache_parent(dir);
165 inode_unlock(d_inode(dir));
168 /* Caller must hold parent->i_mutex */
169 static int spufs_rmdir(struct inode *parent, struct dentry *dir)
171 /* remove all entries */
173 spufs_prune_dir(dir);
175 res = simple_rmdir(parent, dir);
176 /* We have to give up the mm_struct */
177 spu_forget(SPUFS_I(d_inode(dir))->i_ctx);
181 static int spufs_fill_dir(struct dentry *dir,
182 const struct spufs_tree_descr *files, umode_t mode,
183 struct spu_context *ctx)
185 while (files->name && files->name[0]) {
187 struct dentry *dentry = d_alloc_name(dir, files->name);
190 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
191 files->mode & mode, files->size, ctx);
199 static int spufs_dir_close(struct inode *inode, struct file *file)
201 struct spu_context *ctx;
202 struct inode *parent;
206 dir = file->f_path.dentry;
207 parent = d_inode(dir->d_parent);
208 ctx = SPUFS_I(d_inode(dir))->i_ctx;
210 inode_lock_nested(parent, I_MUTEX_PARENT);
211 ret = spufs_rmdir(parent, dir);
212 inode_unlock(parent);
215 return dcache_dir_close(inode, file);
218 const struct file_operations spufs_context_fops = {
219 .open = dcache_dir_open,
220 .release = spufs_dir_close,
221 .llseek = dcache_dir_lseek,
222 .read = generic_read_dir,
223 .iterate_shared = dcache_readdir,
226 EXPORT_SYMBOL_GPL(spufs_context_fops);
229 spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
234 struct spu_context *ctx;
236 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
240 if (dir->i_mode & S_ISGID) {
241 inode->i_gid = dir->i_gid;
242 inode->i_mode &= S_ISGID;
244 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
245 SPUFS_I(inode)->i_ctx = ctx;
252 inode->i_op = &simple_dir_inode_operations;
253 inode->i_fop = &simple_dir_operations;
261 d_instantiate(dentry, inode);
263 if (flags & SPU_CREATE_NOSCHED)
264 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
267 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
269 if (!ret && spufs_get_sb_info(dir->i_sb)->debug)
270 ret = spufs_fill_dir(dentry, spufs_dir_debug_contents,
274 spufs_rmdir(dir, dentry);
281 static int spufs_context_open(struct path *path)
286 ret = get_unused_fd_flags(0);
290 filp = dentry_open(path, O_RDONLY, current_cred());
293 return PTR_ERR(filp);
296 filp->f_op = &spufs_context_fops;
297 fd_install(ret, filp);
301 static struct spu_context *
302 spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
305 struct spu_context *tmp, *neighbor, *err;
309 aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
310 struct spu, cbe_list))->aff_list);
313 return ERR_PTR(-EINVAL);
315 if (flags & SPU_CREATE_GANG)
316 return ERR_PTR(-EINVAL);
318 if (flags & SPU_CREATE_AFFINITY_MEM &&
320 gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
321 return ERR_PTR(-EEXIST);
323 if (gang->aff_flags & AFF_MERGED)
324 return ERR_PTR(-EBUSY);
327 if (flags & SPU_CREATE_AFFINITY_SPU) {
328 if (!filp || filp->f_op != &spufs_context_fops)
329 return ERR_PTR(-EINVAL);
331 neighbor = get_spu_context(
332 SPUFS_I(file_inode(filp))->i_ctx);
334 if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
335 !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
336 !list_entry(neighbor->aff_list.next, struct spu_context,
337 aff_list)->aff_head) {
338 err = ERR_PTR(-EEXIST);
339 goto out_put_neighbor;
342 if (gang != neighbor->gang) {
343 err = ERR_PTR(-EINVAL);
344 goto out_put_neighbor;
348 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
350 if (list_empty(&neighbor->aff_list))
353 for (node = 0; node < MAX_NUMNODES; node++) {
354 if ((cbe_spu_info[node].n_spus - atomic_read(
355 &cbe_spu_info[node].reserved_spus)) >= count)
359 if (node == MAX_NUMNODES) {
360 err = ERR_PTR(-EEXIST);
361 goto out_put_neighbor;
368 put_spu_context(neighbor);
373 spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
374 struct spu_context *neighbor)
376 if (flags & SPU_CREATE_AFFINITY_MEM)
377 ctx->gang->aff_ref_ctx = ctx;
379 if (flags & SPU_CREATE_AFFINITY_SPU) {
380 if (list_empty(&neighbor->aff_list)) {
381 list_add_tail(&neighbor->aff_list,
382 &ctx->gang->aff_list_head);
383 neighbor->aff_head = 1;
386 if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
387 || list_entry(neighbor->aff_list.next, struct spu_context,
388 aff_list)->aff_head) {
389 list_add(&ctx->aff_list, &neighbor->aff_list);
391 list_add_tail(&ctx->aff_list, &neighbor->aff_list);
392 if (neighbor->aff_head) {
393 neighbor->aff_head = 0;
398 if (!ctx->gang->aff_ref_ctx)
399 ctx->gang->aff_ref_ctx = ctx;
404 spufs_create_context(struct inode *inode, struct dentry *dentry,
405 struct vfsmount *mnt, int flags, umode_t mode,
406 struct file *aff_filp)
410 struct spu_gang *gang;
411 struct spu_context *neighbor;
412 struct path path = {.mnt = mnt, .dentry = dentry};
414 if ((flags & SPU_CREATE_NOSCHED) &&
415 !capable(CAP_SYS_NICE))
418 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
419 == SPU_CREATE_ISOLATE)
422 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
427 affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
429 gang = SPUFS_I(inode)->i_gang;
432 mutex_lock(&gang->aff_mutex);
433 neighbor = spufs_assert_affinity(flags, gang, aff_filp);
434 if (IS_ERR(neighbor)) {
435 ret = PTR_ERR(neighbor);
440 ret = spufs_mkdir(inode, dentry, flags, mode & 0777);
445 spufs_set_affinity(flags, SPUFS_I(d_inode(dentry))->i_ctx,
448 put_spu_context(neighbor);
451 ret = spufs_context_open(&path);
453 WARN_ON(spufs_rmdir(inode, dentry));
457 mutex_unlock(&gang->aff_mutex);
462 spufs_mkgang(struct inode *dir, struct dentry *dentry, umode_t mode)
466 struct spu_gang *gang;
469 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
474 if (dir->i_mode & S_ISGID) {
475 inode->i_gid = dir->i_gid;
476 inode->i_mode &= S_ISGID;
478 gang = alloc_spu_gang();
479 SPUFS_I(inode)->i_ctx = NULL;
480 SPUFS_I(inode)->i_gang = gang;
486 inode->i_op = &simple_dir_inode_operations;
487 inode->i_fop = &simple_dir_operations;
489 d_instantiate(dentry, inode);
491 inc_nlink(d_inode(dentry));
500 static int spufs_gang_open(struct path *path)
505 ret = get_unused_fd_flags(0);
510 * get references for dget and mntget, will be released
511 * in error path of *_open().
513 filp = dentry_open(path, O_RDONLY, current_cred());
516 return PTR_ERR(filp);
519 filp->f_op = &simple_dir_operations;
520 fd_install(ret, filp);
524 static int spufs_create_gang(struct inode *inode,
525 struct dentry *dentry,
526 struct vfsmount *mnt, umode_t mode)
528 struct path path = {.mnt = mnt, .dentry = dentry};
531 ret = spufs_mkgang(inode, dentry, mode & 0777);
533 ret = spufs_gang_open(&path);
535 int err = simple_rmdir(inode, dentry);
543 static struct file_system_type spufs_type;
545 long spufs_create(struct path *path, struct dentry *dentry,
546 unsigned int flags, umode_t mode, struct file *filp)
548 struct inode *dir = d_inode(path->dentry);
551 /* check if we are on spufs */
552 if (path->dentry->d_sb->s_type != &spufs_type)
555 /* don't accept undefined flags */
556 if (flags & (~SPU_CREATE_FLAG_ALL))
559 /* only threads can be underneath a gang */
560 if (path->dentry != path->dentry->d_sb->s_root)
561 if ((flags & SPU_CREATE_GANG) || !SPUFS_I(dir)->i_gang)
564 mode &= ~current_umask();
566 if (flags & SPU_CREATE_GANG)
567 ret = spufs_create_gang(dir, dentry, path->mnt, mode);
569 ret = spufs_create_context(dir, dentry, path->mnt, flags, mode,
572 fsnotify_mkdir(dir, dentry);
577 /* File system initialization */
578 struct spufs_fs_context {
585 Opt_uid, Opt_gid, Opt_mode, Opt_debug,
588 static const struct fs_parameter_spec spufs_param_specs[] = {
589 fsparam_u32 ("gid", Opt_gid),
590 fsparam_u32oct ("mode", Opt_mode),
591 fsparam_u32 ("uid", Opt_uid),
592 fsparam_flag ("debug", Opt_debug),
596 static const struct fs_parameter_description spufs_fs_parameters = {
598 .specs = spufs_param_specs,
601 static int spufs_show_options(struct seq_file *m, struct dentry *root)
603 struct spufs_sb_info *sbi = spufs_get_sb_info(root->d_sb);
604 struct inode *inode = root->d_inode;
606 if (!uid_eq(inode->i_uid, GLOBAL_ROOT_UID))
607 seq_printf(m, ",uid=%u",
608 from_kuid_munged(&init_user_ns, inode->i_uid));
609 if (!gid_eq(inode->i_gid, GLOBAL_ROOT_GID))
610 seq_printf(m, ",gid=%u",
611 from_kgid_munged(&init_user_ns, inode->i_gid));
612 if ((inode->i_mode & S_IALLUGO) != 0775)
613 seq_printf(m, ",mode=%o", inode->i_mode);
615 seq_puts(m, ",debug");
619 static int spufs_parse_param(struct fs_context *fc, struct fs_parameter *param)
621 struct spufs_fs_context *ctx = fc->fs_private;
622 struct spufs_sb_info *sbi = fc->s_fs_info;
623 struct fs_parse_result result;
628 opt = fs_parse(fc, &spufs_fs_parameters, param, &result);
634 uid = make_kuid(current_user_ns(), result.uint_32);
636 return invalf(fc, "Unknown uid");
640 gid = make_kgid(current_user_ns(), result.uint_32);
642 return invalf(fc, "Unknown gid");
646 ctx->mode = result.uint_32 & S_IALLUGO;
656 static void spufs_exit_isolated_loader(void)
658 free_pages((unsigned long) isolated_loader,
659 get_order(isolated_loader_size));
663 spufs_init_isolated_loader(void)
665 struct device_node *dn;
669 dn = of_find_node_by_path("/spu-isolation");
673 loader = of_get_property(dn, "loader", &size);
678 /* the loader must be align on a 16 byte boundary */
679 isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
680 if (!isolated_loader)
683 isolated_loader_size = size;
684 memcpy(isolated_loader, loader, size);
685 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
688 static int spufs_create_root(struct super_block *sb, struct fs_context *fc)
690 struct spufs_fs_context *ctx = fc->fs_private;
693 if (!spu_management_ops)
696 inode = spufs_new_inode(sb, S_IFDIR | ctx->mode);
700 inode->i_uid = ctx->uid;
701 inode->i_gid = ctx->gid;
702 inode->i_op = &simple_dir_inode_operations;
703 inode->i_fop = &simple_dir_operations;
704 SPUFS_I(inode)->i_ctx = NULL;
707 sb->s_root = d_make_root(inode);
713 static const struct super_operations spufs_ops = {
714 .alloc_inode = spufs_alloc_inode,
715 .free_inode = spufs_free_inode,
716 .statfs = simple_statfs,
717 .evict_inode = spufs_evict_inode,
718 .show_options = spufs_show_options,
721 static int spufs_fill_super(struct super_block *sb, struct fs_context *fc)
723 sb->s_maxbytes = MAX_LFS_FILESIZE;
724 sb->s_blocksize = PAGE_SIZE;
725 sb->s_blocksize_bits = PAGE_SHIFT;
726 sb->s_magic = SPUFS_MAGIC;
727 sb->s_op = &spufs_ops;
729 return spufs_create_root(sb, fc);
732 static int spufs_get_tree(struct fs_context *fc)
734 return get_tree_single(fc, spufs_fill_super);
737 static void spufs_free_fc(struct fs_context *fc)
739 kfree(fc->s_fs_info);
742 static const struct fs_context_operations spufs_context_ops = {
743 .free = spufs_free_fc,
744 .parse_param = spufs_parse_param,
745 .get_tree = spufs_get_tree,
748 static int spufs_init_fs_context(struct fs_context *fc)
750 struct spufs_fs_context *ctx;
751 struct spufs_sb_info *sbi;
753 ctx = kzalloc(sizeof(struct spufs_fs_context), GFP_KERNEL);
757 sbi = kzalloc(sizeof(struct spufs_sb_info), GFP_KERNEL);
761 ctx->uid = current_uid();
762 ctx->gid = current_gid();
765 fc->fs_private = ctx;
767 fc->ops = &spufs_context_ops;
776 static struct file_system_type spufs_type = {
777 .owner = THIS_MODULE,
779 .init_fs_context = spufs_init_fs_context,
780 .parameters = &spufs_fs_parameters,
781 .kill_sb = kill_litter_super,
783 MODULE_ALIAS_FS("spufs");
785 static int __init spufs_init(void)
790 if (!spu_management_ops)
794 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
795 sizeof(struct spufs_inode_info), 0,
796 SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT, spufs_init_once);
798 if (!spufs_inode_cache)
800 ret = spu_sched_init();
803 ret = register_spu_syscalls(&spufs_calls);
806 ret = register_filesystem(&spufs_type);
810 spufs_init_isolated_loader();
815 unregister_spu_syscalls(&spufs_calls);
819 kmem_cache_destroy(spufs_inode_cache);
823 module_init(spufs_init);
825 static void __exit spufs_exit(void)
828 spufs_exit_isolated_loader();
829 unregister_spu_syscalls(&spufs_calls);
830 unregister_filesystem(&spufs_type);
831 kmem_cache_destroy(spufs_inode_cache);
833 module_exit(spufs_exit);
835 MODULE_LICENSE("GPL");
836 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");