Path: blob/master/arch/powerpc/platforms/cell/spufs/inode.c
51823 views
// SPDX-License-Identifier: GPL-2.0-or-later12/*3* SPU file system4*5* (C) Copyright IBM Deutschland Entwicklung GmbH 20056*7* Author: Arnd Bergmann <[email protected]>8*/910#include <linux/file.h>11#include <linux/fs.h>12#include <linux/fs_context.h>13#include <linux/fs_parser.h>14#include <linux/fsnotify.h>15#include <linux/backing-dev.h>16#include <linux/init.h>17#include <linux/ioctl.h>18#include <linux/module.h>19#include <linux/mount.h>20#include <linux/namei.h>21#include <linux/pagemap.h>22#include <linux/poll.h>23#include <linux/of.h>24#include <linux/seq_file.h>25#include <linux/slab.h>2627#include <asm/spu.h>28#include <asm/spu_priv1.h>29#include <linux/uaccess.h>3031#include "spufs.h"3233struct spufs_sb_info {34bool debug;35};3637static struct kmem_cache *spufs_inode_cache;38char *isolated_loader;39static int isolated_loader_size;4041static struct spufs_sb_info *spufs_get_sb_info(struct super_block *sb)42{43return sb->s_fs_info;44}4546static struct inode *47spufs_alloc_inode(struct super_block *sb)48{49struct spufs_inode_info *ei;5051ei = kmem_cache_alloc(spufs_inode_cache, GFP_KERNEL);52if (!ei)53return NULL;5455ei->i_gang = NULL;56ei->i_ctx = NULL;57ei->i_openers = 0;5859return &ei->vfs_inode;60}6162static void spufs_free_inode(struct inode *inode)63{64kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));65}6667static void68spufs_init_once(void *p)69{70struct spufs_inode_info *ei = p;7172inode_init_once(&ei->vfs_inode);73}7475static struct inode *76spufs_new_inode(struct super_block *sb, umode_t mode)77{78struct inode *inode;7980inode = new_inode(sb);81if (!inode)82goto out;8384inode->i_ino = get_next_ino();85inode->i_mode = mode;86inode->i_uid = current_fsuid();87inode->i_gid = current_fsgid();88simple_inode_init_ts(inode);89out:90return inode;91}9293static int94spufs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,95struct iattr *attr)96{97struct inode *inode = d_inode(dentry);9899if ((attr->ia_valid & ATTR_SIZE) &&100(attr->ia_size != inode->i_size))101return -EINVAL;102setattr_copy(&nop_mnt_idmap, inode, attr);103mark_inode_dirty(inode);104return 0;105}106107108static int109spufs_new_file(struct super_block *sb, struct dentry *dentry,110const struct file_operations *fops, umode_t mode,111size_t size, struct spu_context *ctx)112{113static const struct inode_operations spufs_file_iops = {114.setattr = spufs_setattr,115};116struct inode *inode;117int ret;118119ret = -ENOSPC;120inode = spufs_new_inode(sb, S_IFREG | mode);121if (!inode)122goto out;123124ret = 0;125inode->i_op = &spufs_file_iops;126inode->i_fop = fops;127inode->i_size = size;128inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);129d_make_persistent(dentry, inode);130out:131return ret;132}133134static void135spufs_evict_inode(struct inode *inode)136{137struct spufs_inode_info *ei = SPUFS_I(inode);138clear_inode(inode);139if (ei->i_ctx)140put_spu_context(ei->i_ctx);141if (ei->i_gang)142put_spu_gang(ei->i_gang);143}144145/* Caller must hold parent->i_mutex */146static void spufs_rmdir(struct inode *parent, struct dentry *dir)147{148struct spu_context *ctx = SPUFS_I(d_inode(dir))->i_ctx;149150locked_recursive_removal(dir, NULL);151spu_forget(ctx);152}153154static int spufs_fill_dir(struct dentry *dir,155const struct spufs_tree_descr *files, umode_t mode,156struct spu_context *ctx)157{158while (files->name && files->name[0]) {159int ret;160struct dentry *dentry = d_alloc_name(dir, files->name);161if (!dentry)162return -ENOMEM;163ret = spufs_new_file(dir->d_sb, dentry, files->ops,164files->mode & mode, files->size, ctx);165dput(dentry);166if (ret)167return ret;168files++;169}170return 0;171}172173static void unuse_gang(struct dentry *dir)174{175struct inode *inode = dir->d_inode;176struct spu_gang *gang = SPUFS_I(inode)->i_gang;177178if (gang) {179bool dead;180181inode_lock(inode); // exclusion with spufs_create_context()182dead = !--gang->alive;183inode_unlock(inode);184185if (dead)186simple_recursive_removal(dir, NULL);187}188}189190static int spufs_dir_close(struct inode *inode, struct file *file)191{192struct inode *parent;193struct dentry *dir;194195dir = file->f_path.dentry;196parent = d_inode(dir->d_parent);197198inode_lock_nested(parent, I_MUTEX_PARENT);199spufs_rmdir(parent, dir);200inode_unlock(parent);201202unuse_gang(dir->d_parent);203return dcache_dir_close(inode, file);204}205206const struct file_operations spufs_context_fops = {207.open = dcache_dir_open,208.release = spufs_dir_close,209.llseek = dcache_dir_lseek,210.read = generic_read_dir,211.iterate_shared = dcache_readdir,212.fsync = noop_fsync,213};214EXPORT_SYMBOL_GPL(spufs_context_fops);215216static int217spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,218umode_t mode)219{220int ret;221struct inode *inode;222struct spu_context *ctx;223224inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);225if (!inode)226return -ENOSPC;227228inode_init_owner(&nop_mnt_idmap, inode, dir, mode | S_IFDIR);229ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */230SPUFS_I(inode)->i_ctx = ctx;231if (!ctx) {232iput(inode);233return -ENOSPC;234}235236ctx->flags = flags;237inode->i_op = &simple_dir_inode_operations;238inode->i_fop = &simple_dir_operations;239240inode_lock(inode);241242inc_nlink(dir);243inc_nlink(inode);244245d_make_persistent(dentry, inode);246247if (flags & SPU_CREATE_NOSCHED)248ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,249mode, ctx);250else251ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);252253if (!ret && spufs_get_sb_info(dir->i_sb)->debug)254ret = spufs_fill_dir(dentry, spufs_dir_debug_contents,255mode, ctx);256257inode_unlock(inode);258259if (ret)260spufs_rmdir(dir, dentry);261262return ret;263}264265static int spufs_context_open(const struct path *path)266{267FD_PREPARE(fdf, 0, dentry_open(path, O_RDONLY, current_cred()));268if (fdf.err)269return fdf.err;270fd_prepare_file(fdf)->f_op = &spufs_context_fops;271return fd_publish(fdf);272}273274static struct spu_context *275spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,276struct file *filp)277{278struct spu_context *tmp, *neighbor, *err;279int count, node;280int aff_supp;281282aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,283struct spu, cbe_list))->aff_list);284285if (!aff_supp)286return ERR_PTR(-EINVAL);287288if (flags & SPU_CREATE_GANG)289return ERR_PTR(-EINVAL);290291if (flags & SPU_CREATE_AFFINITY_MEM &&292gang->aff_ref_ctx &&293gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)294return ERR_PTR(-EEXIST);295296if (gang->aff_flags & AFF_MERGED)297return ERR_PTR(-EBUSY);298299neighbor = NULL;300if (flags & SPU_CREATE_AFFINITY_SPU) {301if (!filp || filp->f_op != &spufs_context_fops)302return ERR_PTR(-EINVAL);303304neighbor = get_spu_context(305SPUFS_I(file_inode(filp))->i_ctx);306307if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&308!list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&309!list_entry(neighbor->aff_list.next, struct spu_context,310aff_list)->aff_head) {311err = ERR_PTR(-EEXIST);312goto out_put_neighbor;313}314315if (gang != neighbor->gang) {316err = ERR_PTR(-EINVAL);317goto out_put_neighbor;318}319320count = 1;321list_for_each_entry(tmp, &gang->aff_list_head, aff_list)322count++;323if (list_empty(&neighbor->aff_list))324count++;325326for (node = 0; node < MAX_NUMNODES; node++) {327if ((cbe_spu_info[node].n_spus - atomic_read(328&cbe_spu_info[node].reserved_spus)) >= count)329break;330}331332if (node == MAX_NUMNODES) {333err = ERR_PTR(-EEXIST);334goto out_put_neighbor;335}336}337338return neighbor;339340out_put_neighbor:341put_spu_context(neighbor);342return err;343}344345static void346spufs_set_affinity(unsigned int flags, struct spu_context *ctx,347struct spu_context *neighbor)348{349if (flags & SPU_CREATE_AFFINITY_MEM)350ctx->gang->aff_ref_ctx = ctx;351352if (flags & SPU_CREATE_AFFINITY_SPU) {353if (list_empty(&neighbor->aff_list)) {354list_add_tail(&neighbor->aff_list,355&ctx->gang->aff_list_head);356neighbor->aff_head = 1;357}358359if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)360|| list_entry(neighbor->aff_list.next, struct spu_context,361aff_list)->aff_head) {362list_add(&ctx->aff_list, &neighbor->aff_list);363} else {364list_add_tail(&ctx->aff_list, &neighbor->aff_list);365if (neighbor->aff_head) {366neighbor->aff_head = 0;367ctx->aff_head = 1;368}369}370371if (!ctx->gang->aff_ref_ctx)372ctx->gang->aff_ref_ctx = ctx;373}374}375376static int377spufs_create_context(struct inode *inode, struct dentry *dentry,378struct vfsmount *mnt, int flags, umode_t mode,379struct file *aff_filp)380{381int ret;382int affinity;383struct spu_gang *gang = SPUFS_I(inode)->i_gang;384struct spu_context *neighbor;385struct path path = {.mnt = mnt, .dentry = dentry};386387if ((flags & SPU_CREATE_NOSCHED) &&388!capable(CAP_SYS_NICE))389return -EPERM;390391if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))392== SPU_CREATE_ISOLATE)393return -EINVAL;394395if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)396return -ENODEV;397398if (gang) {399if (!gang->alive)400return -ENOENT;401gang->alive++;402}403404neighbor = NULL;405affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);406if (affinity) {407if (!gang)408return -EINVAL;409mutex_lock(&gang->aff_mutex);410neighbor = spufs_assert_affinity(flags, gang, aff_filp);411if (IS_ERR(neighbor)) {412ret = PTR_ERR(neighbor);413goto out_aff_unlock;414}415}416417ret = spufs_mkdir(inode, dentry, flags, mode & 0777);418if (ret) {419if (neighbor)420put_spu_context(neighbor);421goto out_aff_unlock;422}423424if (affinity) {425spufs_set_affinity(flags, SPUFS_I(d_inode(dentry))->i_ctx,426neighbor);427if (neighbor)428put_spu_context(neighbor);429}430431ret = spufs_context_open(&path);432if (ret < 0)433spufs_rmdir(inode, dentry);434435out_aff_unlock:436if (affinity)437mutex_unlock(&gang->aff_mutex);438if (ret && gang)439gang->alive--; // can't reach 0440return ret;441}442443static int444spufs_mkgang(struct inode *dir, struct dentry *dentry, umode_t mode)445{446int ret;447struct inode *inode;448struct spu_gang *gang;449450ret = -ENOSPC;451inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);452if (!inode)453goto out;454455ret = 0;456inode_init_owner(&nop_mnt_idmap, inode, dir, mode | S_IFDIR);457gang = alloc_spu_gang();458SPUFS_I(inode)->i_ctx = NULL;459SPUFS_I(inode)->i_gang = gang;460if (!gang) {461ret = -ENOMEM;462goto out_iput;463}464465inode->i_op = &simple_dir_inode_operations;466inode->i_fop = &simple_dir_operations;467468inc_nlink(dir);469inc_nlink(inode);470d_make_persistent(dentry, inode);471return ret;472473out_iput:474iput(inode);475out:476return ret;477}478479static int spufs_gang_close(struct inode *inode, struct file *file)480{481unuse_gang(file->f_path.dentry);482return dcache_dir_close(inode, file);483}484485static const struct file_operations spufs_gang_fops = {486.open = dcache_dir_open,487.release = spufs_gang_close,488.llseek = dcache_dir_lseek,489.read = generic_read_dir,490.iterate_shared = dcache_readdir,491.fsync = noop_fsync,492};493494static int spufs_gang_open(const struct path *path)495{496/*497* get references for dget and mntget, will be released498* in error path of *_open().499*/500FD_PREPARE(fdf, 0, dentry_open(path, O_RDONLY, current_cred()));501if (fdf.err)502return fdf.err;503fd_prepare_file(fdf)->f_op = &spufs_gang_fops;504return fd_publish(fdf);505}506507static int spufs_create_gang(struct inode *inode,508struct dentry *dentry,509struct vfsmount *mnt, umode_t mode)510{511struct path path = {.mnt = mnt, .dentry = dentry};512int ret;513514ret = spufs_mkgang(inode, dentry, mode & 0777);515if (!ret) {516ret = spufs_gang_open(&path);517if (ret < 0)518unuse_gang(dentry);519}520return ret;521}522523524static struct file_system_type spufs_type;525526long spufs_create(const struct path *path, struct dentry *dentry,527unsigned int flags, umode_t mode, struct file *filp)528{529struct inode *dir = d_inode(path->dentry);530int ret;531532/* check if we are on spufs */533if (path->dentry->d_sb->s_type != &spufs_type)534return -EINVAL;535536/* don't accept undefined flags */537if (flags & (~SPU_CREATE_FLAG_ALL))538return -EINVAL;539540/* only threads can be underneath a gang */541if (path->dentry != path->dentry->d_sb->s_root)542if ((flags & SPU_CREATE_GANG) || !SPUFS_I(dir)->i_gang)543return -EINVAL;544545mode &= ~current_umask();546547if (flags & SPU_CREATE_GANG)548ret = spufs_create_gang(dir, dentry, path->mnt, mode);549else550ret = spufs_create_context(dir, dentry, path->mnt, flags, mode,551filp);552if (ret >= 0)553fsnotify_mkdir(dir, dentry);554555return ret;556}557558/* File system initialization */559struct spufs_fs_context {560kuid_t uid;561kgid_t gid;562umode_t mode;563};564565enum {566Opt_uid, Opt_gid, Opt_mode, Opt_debug,567};568569static const struct fs_parameter_spec spufs_fs_parameters[] = {570fsparam_u32 ("gid", Opt_gid),571fsparam_u32oct ("mode", Opt_mode),572fsparam_u32 ("uid", Opt_uid),573fsparam_flag ("debug", Opt_debug),574{}575};576577static int spufs_show_options(struct seq_file *m, struct dentry *root)578{579struct spufs_sb_info *sbi = spufs_get_sb_info(root->d_sb);580struct inode *inode = root->d_inode;581582if (!uid_eq(inode->i_uid, GLOBAL_ROOT_UID))583seq_printf(m, ",uid=%u",584from_kuid_munged(&init_user_ns, inode->i_uid));585if (!gid_eq(inode->i_gid, GLOBAL_ROOT_GID))586seq_printf(m, ",gid=%u",587from_kgid_munged(&init_user_ns, inode->i_gid));588if ((inode->i_mode & S_IALLUGO) != 0775)589seq_printf(m, ",mode=%o", inode->i_mode);590if (sbi->debug)591seq_puts(m, ",debug");592return 0;593}594595static int spufs_parse_param(struct fs_context *fc, struct fs_parameter *param)596{597struct spufs_fs_context *ctx = fc->fs_private;598struct spufs_sb_info *sbi = fc->s_fs_info;599struct fs_parse_result result;600kuid_t uid;601kgid_t gid;602int opt;603604opt = fs_parse(fc, spufs_fs_parameters, param, &result);605if (opt < 0)606return opt;607608switch (opt) {609case Opt_uid:610uid = make_kuid(current_user_ns(), result.uint_32);611if (!uid_valid(uid))612return invalf(fc, "Unknown uid");613ctx->uid = uid;614break;615case Opt_gid:616gid = make_kgid(current_user_ns(), result.uint_32);617if (!gid_valid(gid))618return invalf(fc, "Unknown gid");619ctx->gid = gid;620break;621case Opt_mode:622ctx->mode = result.uint_32 & S_IALLUGO;623break;624case Opt_debug:625sbi->debug = true;626break;627}628629return 0;630}631632static void spufs_exit_isolated_loader(void)633{634free_pages((unsigned long) isolated_loader,635get_order(isolated_loader_size));636}637638static void __init639spufs_init_isolated_loader(void)640{641struct device_node *dn;642const char *loader;643int size;644645dn = of_find_node_by_path("/spu-isolation");646if (!dn)647return;648649loader = of_get_property(dn, "loader", &size);650of_node_put(dn);651if (!loader)652return;653654/* the loader must be align on a 16 byte boundary */655isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));656if (!isolated_loader)657return;658659isolated_loader_size = size;660memcpy(isolated_loader, loader, size);661printk(KERN_INFO "spufs: SPU isolation mode enabled\n");662}663664static int spufs_create_root(struct super_block *sb, struct fs_context *fc)665{666struct spufs_fs_context *ctx = fc->fs_private;667struct inode *inode;668669if (!spu_management_ops)670return -ENODEV;671672inode = spufs_new_inode(sb, S_IFDIR | ctx->mode);673if (!inode)674return -ENOMEM;675676inode->i_uid = ctx->uid;677inode->i_gid = ctx->gid;678inode->i_op = &simple_dir_inode_operations;679inode->i_fop = &simple_dir_operations;680SPUFS_I(inode)->i_ctx = NULL;681inc_nlink(inode);682683sb->s_root = d_make_root(inode);684if (!sb->s_root)685return -ENOMEM;686return 0;687}688689static const struct super_operations spufs_ops = {690.alloc_inode = spufs_alloc_inode,691.free_inode = spufs_free_inode,692.statfs = simple_statfs,693.evict_inode = spufs_evict_inode,694.show_options = spufs_show_options,695};696697static int spufs_fill_super(struct super_block *sb, struct fs_context *fc)698{699sb->s_maxbytes = MAX_LFS_FILESIZE;700sb->s_blocksize = PAGE_SIZE;701sb->s_blocksize_bits = PAGE_SHIFT;702sb->s_magic = SPUFS_MAGIC;703sb->s_op = &spufs_ops;704705return spufs_create_root(sb, fc);706}707708static int spufs_get_tree(struct fs_context *fc)709{710return get_tree_single(fc, spufs_fill_super);711}712713static void spufs_free_fc(struct fs_context *fc)714{715kfree(fc->s_fs_info);716}717718static const struct fs_context_operations spufs_context_ops = {719.free = spufs_free_fc,720.parse_param = spufs_parse_param,721.get_tree = spufs_get_tree,722};723724static int spufs_init_fs_context(struct fs_context *fc)725{726struct spufs_fs_context *ctx;727struct spufs_sb_info *sbi;728729ctx = kzalloc(sizeof(struct spufs_fs_context), GFP_KERNEL);730if (!ctx)731goto nomem;732733sbi = kzalloc(sizeof(struct spufs_sb_info), GFP_KERNEL);734if (!sbi)735goto nomem_ctx;736737ctx->uid = current_uid();738ctx->gid = current_gid();739ctx->mode = 0755;740741fc->fs_private = ctx;742fc->s_fs_info = sbi;743fc->ops = &spufs_context_ops;744return 0;745746nomem_ctx:747kfree(ctx);748nomem:749return -ENOMEM;750}751752static struct file_system_type spufs_type = {753.owner = THIS_MODULE,754.name = "spufs",755.init_fs_context = spufs_init_fs_context,756.parameters = spufs_fs_parameters,757.kill_sb = kill_anon_super,758};759MODULE_ALIAS_FS("spufs");760761static int __init spufs_init(void)762{763int ret;764765ret = -ENODEV;766if (!spu_management_ops)767goto out;768769ret = -ENOMEM;770spufs_inode_cache = kmem_cache_create("spufs_inode_cache",771sizeof(struct spufs_inode_info), 0,772SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT, spufs_init_once);773774if (!spufs_inode_cache)775goto out;776ret = spu_sched_init();777if (ret)778goto out_cache;779ret = register_spu_syscalls(&spufs_calls);780if (ret)781goto out_sched;782ret = register_filesystem(&spufs_type);783if (ret)784goto out_syscalls;785786spufs_init_isolated_loader();787788return 0;789790out_syscalls:791unregister_spu_syscalls(&spufs_calls);792out_sched:793spu_sched_exit();794out_cache:795kmem_cache_destroy(spufs_inode_cache);796out:797return ret;798}799module_init(spufs_init);800801static void __exit spufs_exit(void)802{803spu_sched_exit();804spufs_exit_isolated_loader();805unregister_spu_syscalls(&spufs_calls);806unregister_filesystem(&spufs_type);807kmem_cache_destroy(spufs_inode_cache);808}809module_exit(spufs_exit);810811MODULE_DESCRIPTION("SPU file system");812MODULE_LICENSE("GPL");813MODULE_AUTHOR("Arnd Bergmann <[email protected]>");814815816817