5 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
7 * Author: Arnd Bergmann <arndb@de.ibm.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/file.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>
40 #include <asm/spu_priv1.h>
41 #include <linux/uaccess.h>
45 struct spufs_sb_info {
49 static struct kmem_cache *spufs_inode_cache;
50 char *isolated_loader;
51 static int isolated_loader_size;
53 static struct spufs_sb_info *spufs_get_sb_info(struct super_block *sb)
59 spufs_alloc_inode(struct super_block *sb)
61 struct spufs_inode_info *ei;
63 ei = kmem_cache_alloc(spufs_inode_cache, GFP_KERNEL);
71 return &ei->vfs_inode;
74 static void spufs_free_inode(struct inode *inode)
76 kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
80 spufs_init_once(void *p)
82 struct spufs_inode_info *ei = p;
84 inode_init_once(&ei->vfs_inode);
88 spufs_new_inode(struct super_block *sb, umode_t mode)
92 inode = new_inode(sb);
96 inode->i_ino = get_next_ino();
98 inode->i_uid = current_fsuid();
99 inode->i_gid = current_fsgid();
100 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
106 spufs_setattr(struct dentry *dentry, struct iattr *attr)
108 struct inode *inode = d_inode(dentry);
110 if ((attr->ia_valid & ATTR_SIZE) &&
111 (attr->ia_size != inode->i_size))
113 setattr_copy(inode, attr);
114 mark_inode_dirty(inode);
120 spufs_new_file(struct super_block *sb, struct dentry *dentry,
121 const struct file_operations *fops, umode_t mode,
122 size_t size, struct spu_context *ctx)
124 static const struct inode_operations spufs_file_iops = {
125 .setattr = spufs_setattr,
131 inode = spufs_new_inode(sb, S_IFREG | mode);
136 inode->i_op = &spufs_file_iops;
138 inode->i_size = size;
139 inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
140 d_add(dentry, inode);
146 spufs_evict_inode(struct inode *inode)
148 struct spufs_inode_info *ei = SPUFS_I(inode);
151 put_spu_context(ei->i_ctx);
153 put_spu_gang(ei->i_gang);
156 static void spufs_prune_dir(struct dentry *dir)
158 struct dentry *dentry, *tmp;
160 inode_lock(d_inode(dir));
161 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_child) {
162 spin_lock(&dentry->d_lock);
163 if (simple_positive(dentry)) {
166 spin_unlock(&dentry->d_lock);
167 simple_unlink(d_inode(dir), dentry);
168 /* XXX: what was dcache_lock protecting here? Other
169 * filesystems (IB, configfs) release dcache_lock
173 spin_unlock(&dentry->d_lock);
176 shrink_dcache_parent(dir);
177 inode_unlock(d_inode(dir));
180 /* Caller must hold parent->i_mutex */
181 static int spufs_rmdir(struct inode *parent, struct dentry *dir)
183 /* remove all entries */
185 spufs_prune_dir(dir);
187 res = simple_rmdir(parent, dir);
188 /* We have to give up the mm_struct */
189 spu_forget(SPUFS_I(d_inode(dir))->i_ctx);
193 static int spufs_fill_dir(struct dentry *dir,
194 const struct spufs_tree_descr *files, umode_t mode,
195 struct spu_context *ctx)
197 while (files->name && files->name[0]) {
199 struct dentry *dentry = d_alloc_name(dir, files->name);
202 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
203 files->mode & mode, files->size, ctx);
211 static int spufs_dir_close(struct inode *inode, struct file *file)
213 struct spu_context *ctx;
214 struct inode *parent;
218 dir = file->f_path.dentry;
219 parent = d_inode(dir->d_parent);
220 ctx = SPUFS_I(d_inode(dir))->i_ctx;
222 inode_lock_nested(parent, I_MUTEX_PARENT);
223 ret = spufs_rmdir(parent, dir);
224 inode_unlock(parent);
227 return dcache_dir_close(inode, file);
230 const struct file_operations spufs_context_fops = {
231 .open = dcache_dir_open,
232 .release = spufs_dir_close,
233 .llseek = dcache_dir_lseek,
234 .read = generic_read_dir,
235 .iterate_shared = dcache_readdir,
238 EXPORT_SYMBOL_GPL(spufs_context_fops);
241 spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
246 struct spu_context *ctx;
248 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
252 if (dir->i_mode & S_ISGID) {
253 inode->i_gid = dir->i_gid;
254 inode->i_mode &= S_ISGID;
256 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
257 SPUFS_I(inode)->i_ctx = ctx;
264 inode->i_op = &simple_dir_inode_operations;
265 inode->i_fop = &simple_dir_operations;
273 d_instantiate(dentry, inode);
275 if (flags & SPU_CREATE_NOSCHED)
276 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
279 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
281 if (!ret && spufs_get_sb_info(dir->i_sb)->debug)
282 ret = spufs_fill_dir(dentry, spufs_dir_debug_contents,
286 spufs_rmdir(dir, dentry);
293 static int spufs_context_open(struct path *path)
298 ret = get_unused_fd_flags(0);
302 filp = dentry_open(path, O_RDONLY, current_cred());
305 return PTR_ERR(filp);
308 filp->f_op = &spufs_context_fops;
309 fd_install(ret, filp);
313 static struct spu_context *
314 spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
317 struct spu_context *tmp, *neighbor, *err;
321 aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
322 struct spu, cbe_list))->aff_list);
325 return ERR_PTR(-EINVAL);
327 if (flags & SPU_CREATE_GANG)
328 return ERR_PTR(-EINVAL);
330 if (flags & SPU_CREATE_AFFINITY_MEM &&
332 gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
333 return ERR_PTR(-EEXIST);
335 if (gang->aff_flags & AFF_MERGED)
336 return ERR_PTR(-EBUSY);
339 if (flags & SPU_CREATE_AFFINITY_SPU) {
340 if (!filp || filp->f_op != &spufs_context_fops)
341 return ERR_PTR(-EINVAL);
343 neighbor = get_spu_context(
344 SPUFS_I(file_inode(filp))->i_ctx);
346 if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
347 !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
348 !list_entry(neighbor->aff_list.next, struct spu_context,
349 aff_list)->aff_head) {
350 err = ERR_PTR(-EEXIST);
351 goto out_put_neighbor;
354 if (gang != neighbor->gang) {
355 err = ERR_PTR(-EINVAL);
356 goto out_put_neighbor;
360 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
362 if (list_empty(&neighbor->aff_list))
365 for (node = 0; node < MAX_NUMNODES; node++) {
366 if ((cbe_spu_info[node].n_spus - atomic_read(
367 &cbe_spu_info[node].reserved_spus)) >= count)
371 if (node == MAX_NUMNODES) {
372 err = ERR_PTR(-EEXIST);
373 goto out_put_neighbor;
380 put_spu_context(neighbor);
385 spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
386 struct spu_context *neighbor)
388 if (flags & SPU_CREATE_AFFINITY_MEM)
389 ctx->gang->aff_ref_ctx = ctx;
391 if (flags & SPU_CREATE_AFFINITY_SPU) {
392 if (list_empty(&neighbor->aff_list)) {
393 list_add_tail(&neighbor->aff_list,
394 &ctx->gang->aff_list_head);
395 neighbor->aff_head = 1;
398 if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
399 || list_entry(neighbor->aff_list.next, struct spu_context,
400 aff_list)->aff_head) {
401 list_add(&ctx->aff_list, &neighbor->aff_list);
403 list_add_tail(&ctx->aff_list, &neighbor->aff_list);
404 if (neighbor->aff_head) {
405 neighbor->aff_head = 0;
410 if (!ctx->gang->aff_ref_ctx)
411 ctx->gang->aff_ref_ctx = ctx;
416 spufs_create_context(struct inode *inode, struct dentry *dentry,
417 struct vfsmount *mnt, int flags, umode_t mode,
418 struct file *aff_filp)
422 struct spu_gang *gang;
423 struct spu_context *neighbor;
424 struct path path = {.mnt = mnt, .dentry = dentry};
426 if ((flags & SPU_CREATE_NOSCHED) &&
427 !capable(CAP_SYS_NICE))
430 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
431 == SPU_CREATE_ISOLATE)
434 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
439 affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
441 gang = SPUFS_I(inode)->i_gang;
444 mutex_lock(&gang->aff_mutex);
445 neighbor = spufs_assert_affinity(flags, gang, aff_filp);
446 if (IS_ERR(neighbor)) {
447 ret = PTR_ERR(neighbor);
452 ret = spufs_mkdir(inode, dentry, flags, mode & 0777);
457 spufs_set_affinity(flags, SPUFS_I(d_inode(dentry))->i_ctx,
460 put_spu_context(neighbor);
463 ret = spufs_context_open(&path);
465 WARN_ON(spufs_rmdir(inode, dentry));
469 mutex_unlock(&gang->aff_mutex);
474 spufs_mkgang(struct inode *dir, struct dentry *dentry, umode_t mode)
478 struct spu_gang *gang;
481 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
486 if (dir->i_mode & S_ISGID) {
487 inode->i_gid = dir->i_gid;
488 inode->i_mode &= S_ISGID;
490 gang = alloc_spu_gang();
491 SPUFS_I(inode)->i_ctx = NULL;
492 SPUFS_I(inode)->i_gang = gang;
498 inode->i_op = &simple_dir_inode_operations;
499 inode->i_fop = &simple_dir_operations;
501 d_instantiate(dentry, inode);
503 inc_nlink(d_inode(dentry));
512 static int spufs_gang_open(struct path *path)
517 ret = get_unused_fd_flags(0);
522 * get references for dget and mntget, will be released
523 * in error path of *_open().
525 filp = dentry_open(path, O_RDONLY, current_cred());
528 return PTR_ERR(filp);
531 filp->f_op = &simple_dir_operations;
532 fd_install(ret, filp);
536 static int spufs_create_gang(struct inode *inode,
537 struct dentry *dentry,
538 struct vfsmount *mnt, umode_t mode)
540 struct path path = {.mnt = mnt, .dentry = dentry};
543 ret = spufs_mkgang(inode, dentry, mode & 0777);
545 ret = spufs_gang_open(&path);
547 int err = simple_rmdir(inode, dentry);
555 static struct file_system_type spufs_type;
557 long spufs_create(struct path *path, struct dentry *dentry,
558 unsigned int flags, umode_t mode, struct file *filp)
560 struct inode *dir = d_inode(path->dentry);
563 /* check if we are on spufs */
564 if (path->dentry->d_sb->s_type != &spufs_type)
567 /* don't accept undefined flags */
568 if (flags & (~SPU_CREATE_FLAG_ALL))
571 /* only threads can be underneath a gang */
572 if (path->dentry != path->dentry->d_sb->s_root)
573 if ((flags & SPU_CREATE_GANG) || !SPUFS_I(dir)->i_gang)
576 mode &= ~current_umask();
578 if (flags & SPU_CREATE_GANG)
579 ret = spufs_create_gang(dir, dentry, path->mnt, mode);
581 ret = spufs_create_context(dir, dentry, path->mnt, flags, mode,
584 fsnotify_mkdir(dir, dentry);
589 /* File system initialization */
591 Opt_uid, Opt_gid, Opt_mode, Opt_debug, Opt_err,
594 static const match_table_t spufs_tokens = {
595 { Opt_uid, "uid=%d" },
596 { Opt_gid, "gid=%d" },
597 { Opt_mode, "mode=%o" },
598 { Opt_debug, "debug" },
602 static int spufs_show_options(struct seq_file *m, struct dentry *root)
604 struct spufs_sb_info *sbi = spufs_get_sb_info(root->d_sb);
605 struct inode *inode = root->d_inode;
607 if (!uid_eq(inode->i_uid, GLOBAL_ROOT_UID))
608 seq_printf(m, ",uid=%u",
609 from_kuid_munged(&init_user_ns, inode->i_uid));
610 if (!gid_eq(inode->i_gid, GLOBAL_ROOT_GID))
611 seq_printf(m, ",gid=%u",
612 from_kgid_munged(&init_user_ns, inode->i_gid));
613 if ((inode->i_mode & S_IALLUGO) != 0775)
614 seq_printf(m, ",mode=%o", inode->i_mode);
616 seq_puts(m, ",debug");
621 spufs_parse_options(struct super_block *sb, char *options, struct inode *root)
624 substring_t args[MAX_OPT_ARGS];
626 while ((p = strsep(&options, ",")) != NULL) {
632 token = match_token(p, spufs_tokens, args);
635 if (match_int(&args[0], &option))
637 root->i_uid = make_kuid(current_user_ns(), option);
638 if (!uid_valid(root->i_uid))
642 if (match_int(&args[0], &option))
644 root->i_gid = make_kgid(current_user_ns(), option);
645 if (!gid_valid(root->i_gid))
649 if (match_octal(&args[0], &option))
651 root->i_mode = option | S_IFDIR;
654 spufs_get_sb_info(sb)->debug = 1;
663 static void spufs_exit_isolated_loader(void)
665 free_pages((unsigned long) isolated_loader,
666 get_order(isolated_loader_size));
670 spufs_init_isolated_loader(void)
672 struct device_node *dn;
676 dn = of_find_node_by_path("/spu-isolation");
680 loader = of_get_property(dn, "loader", &size);
684 /* the loader must be align on a 16 byte boundary */
685 isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
686 if (!isolated_loader)
689 isolated_loader_size = size;
690 memcpy(isolated_loader, loader, size);
691 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
695 spufs_create_root(struct super_block *sb, void *data)
701 if (!spu_management_ops)
705 inode = spufs_new_inode(sb, S_IFDIR | 0775);
709 inode->i_op = &simple_dir_inode_operations;
710 inode->i_fop = &simple_dir_operations;
711 SPUFS_I(inode)->i_ctx = NULL;
715 if (!spufs_parse_options(sb, data, inode))
719 sb->s_root = d_make_root(inode);
731 spufs_fill_super(struct super_block *sb, void *data, int silent)
733 struct spufs_sb_info *info;
734 static const struct super_operations s_ops = {
735 .alloc_inode = spufs_alloc_inode,
736 .free_inode = spufs_free_inode,
737 .statfs = simple_statfs,
738 .evict_inode = spufs_evict_inode,
739 .show_options = spufs_show_options,
742 info = kzalloc(sizeof(*info), GFP_KERNEL);
746 sb->s_maxbytes = MAX_LFS_FILESIZE;
747 sb->s_blocksize = PAGE_SIZE;
748 sb->s_blocksize_bits = PAGE_SHIFT;
749 sb->s_magic = SPUFS_MAGIC;
751 sb->s_fs_info = info;
753 return spufs_create_root(sb, data);
756 static struct dentry *
757 spufs_mount(struct file_system_type *fstype, int flags,
758 const char *name, void *data)
760 return mount_single(fstype, flags, data, spufs_fill_super);
763 static struct file_system_type spufs_type = {
764 .owner = THIS_MODULE,
766 .mount = spufs_mount,
767 .kill_sb = kill_litter_super,
769 MODULE_ALIAS_FS("spufs");
771 static int __init spufs_init(void)
776 if (!spu_management_ops)
780 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
781 sizeof(struct spufs_inode_info), 0,
782 SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT, spufs_init_once);
784 if (!spufs_inode_cache)
786 ret = spu_sched_init();
789 ret = register_spu_syscalls(&spufs_calls);
792 ret = register_filesystem(&spufs_type);
796 spufs_init_isolated_loader();
801 unregister_spu_syscalls(&spufs_calls);
805 kmem_cache_destroy(spufs_inode_cache);
809 module_init(spufs_init);
811 static void __exit spufs_exit(void)
814 spufs_exit_isolated_loader();
815 unregister_spu_syscalls(&spufs_calls);
816 unregister_filesystem(&spufs_type);
817 kmem_cache_destroy(spufs_inode_cache);
819 module_exit(spufs_exit);
821 MODULE_LICENSE("GPL");
822 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");