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 <asm/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_i_callback(struct rcu_head *head)
76 struct inode *inode = container_of(head, struct inode, i_rcu);
77 kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
80 static void spufs_destroy_inode(struct inode *inode)
82 call_rcu(&inode->i_rcu, spufs_i_callback);
86 spufs_init_once(void *p)
88 struct spufs_inode_info *ei = p;
90 inode_init_once(&ei->vfs_inode);
94 spufs_new_inode(struct super_block *sb, umode_t mode)
98 inode = new_inode(sb);
102 inode->i_mode = mode;
103 inode->i_uid = current_fsuid();
104 inode->i_gid = current_fsgid();
105 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
111 spufs_setattr(struct dentry *dentry, struct iattr *attr)
113 struct inode *inode = dentry->d_inode;
115 if ((attr->ia_valid & ATTR_SIZE) &&
116 (attr->ia_size != inode->i_size))
118 setattr_copy(inode, attr);
119 mark_inode_dirty(inode);
125 spufs_new_file(struct super_block *sb, struct dentry *dentry,
126 const struct file_operations *fops, umode_t mode,
127 size_t size, struct spu_context *ctx)
129 static const struct inode_operations spufs_file_iops = {
130 .setattr = spufs_setattr,
136 inode = spufs_new_inode(sb, S_IFREG | mode);
141 inode->i_op = &spufs_file_iops;
143 inode->i_size = size;
144 inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
145 d_add(dentry, inode);
151 spufs_evict_inode(struct inode *inode)
153 struct spufs_inode_info *ei = SPUFS_I(inode);
156 put_spu_context(ei->i_ctx);
158 put_spu_gang(ei->i_gang);
161 static void spufs_prune_dir(struct dentry *dir)
163 struct dentry *dentry, *tmp;
165 mutex_lock(&dir->d_inode->i_mutex);
166 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
167 spin_lock(&dentry->d_lock);
168 if (!(d_unhashed(dentry)) && dentry->d_inode) {
171 spin_unlock(&dentry->d_lock);
172 simple_unlink(dir->d_inode, dentry);
173 /* XXX: what was dcache_lock protecting here? Other
174 * filesystems (IB, configfs) release dcache_lock
178 spin_unlock(&dentry->d_lock);
181 shrink_dcache_parent(dir);
182 mutex_unlock(&dir->d_inode->i_mutex);
185 /* Caller must hold parent->i_mutex */
186 static int spufs_rmdir(struct inode *parent, struct dentry *dir)
188 /* remove all entries */
190 spufs_prune_dir(dir);
192 res = simple_rmdir(parent, dir);
193 /* We have to give up the mm_struct */
194 spu_forget(SPUFS_I(dir->d_inode)->i_ctx);
198 static int spufs_fill_dir(struct dentry *dir,
199 const struct spufs_tree_descr *files, umode_t mode,
200 struct spu_context *ctx)
202 while (files->name && files->name[0]) {
204 struct dentry *dentry = d_alloc_name(dir, files->name);
207 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
208 files->mode & mode, files->size, ctx);
216 static int spufs_dir_close(struct inode *inode, struct file *file)
218 struct spu_context *ctx;
219 struct inode *parent;
223 dir = file->f_path.dentry;
224 parent = dir->d_parent->d_inode;
225 ctx = SPUFS_I(dir->d_inode)->i_ctx;
227 mutex_lock_nested(&parent->i_mutex, I_MUTEX_PARENT);
228 ret = spufs_rmdir(parent, dir);
229 mutex_unlock(&parent->i_mutex);
232 return dcache_dir_close(inode, file);
235 const struct file_operations spufs_context_fops = {
236 .open = dcache_dir_open,
237 .release = spufs_dir_close,
238 .llseek = dcache_dir_lseek,
239 .read = generic_read_dir,
240 .readdir = dcache_readdir,
243 EXPORT_SYMBOL_GPL(spufs_context_fops);
246 spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
251 struct spu_context *ctx;
253 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
257 if (dir->i_mode & S_ISGID) {
258 inode->i_gid = dir->i_gid;
259 inode->i_mode &= S_ISGID;
261 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
262 SPUFS_I(inode)->i_ctx = ctx;
269 inode->i_op = &simple_dir_inode_operations;
270 inode->i_fop = &simple_dir_operations;
272 mutex_lock(&inode->i_mutex);
278 d_instantiate(dentry, inode);
280 if (flags & SPU_CREATE_NOSCHED)
281 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
284 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
286 if (!ret && spufs_get_sb_info(dir->i_sb)->debug)
287 ret = spufs_fill_dir(dentry, spufs_dir_debug_contents,
291 spufs_rmdir(dir, dentry);
293 mutex_unlock(&inode->i_mutex);
298 static int spufs_context_open(struct path *path)
303 ret = get_unused_fd();
307 filp = dentry_open(path, O_RDONLY, current_cred());
310 return PTR_ERR(filp);
313 filp->f_op = &spufs_context_fops;
314 fd_install(ret, filp);
318 static struct spu_context *
319 spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
322 struct spu_context *tmp, *neighbor, *err;
326 aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
327 struct spu, cbe_list))->aff_list);
330 return ERR_PTR(-EINVAL);
332 if (flags & SPU_CREATE_GANG)
333 return ERR_PTR(-EINVAL);
335 if (flags & SPU_CREATE_AFFINITY_MEM &&
337 gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
338 return ERR_PTR(-EEXIST);
340 if (gang->aff_flags & AFF_MERGED)
341 return ERR_PTR(-EBUSY);
344 if (flags & SPU_CREATE_AFFINITY_SPU) {
345 if (!filp || filp->f_op != &spufs_context_fops)
346 return ERR_PTR(-EINVAL);
348 neighbor = get_spu_context(
349 SPUFS_I(file_inode(filp))->i_ctx);
351 if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
352 !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
353 !list_entry(neighbor->aff_list.next, struct spu_context,
354 aff_list)->aff_head) {
355 err = ERR_PTR(-EEXIST);
356 goto out_put_neighbor;
359 if (gang != neighbor->gang) {
360 err = ERR_PTR(-EINVAL);
361 goto out_put_neighbor;
365 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
367 if (list_empty(&neighbor->aff_list))
370 for (node = 0; node < MAX_NUMNODES; node++) {
371 if ((cbe_spu_info[node].n_spus - atomic_read(
372 &cbe_spu_info[node].reserved_spus)) >= count)
376 if (node == MAX_NUMNODES) {
377 err = ERR_PTR(-EEXIST);
378 goto out_put_neighbor;
385 put_spu_context(neighbor);
390 spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
391 struct spu_context *neighbor)
393 if (flags & SPU_CREATE_AFFINITY_MEM)
394 ctx->gang->aff_ref_ctx = ctx;
396 if (flags & SPU_CREATE_AFFINITY_SPU) {
397 if (list_empty(&neighbor->aff_list)) {
398 list_add_tail(&neighbor->aff_list,
399 &ctx->gang->aff_list_head);
400 neighbor->aff_head = 1;
403 if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
404 || list_entry(neighbor->aff_list.next, struct spu_context,
405 aff_list)->aff_head) {
406 list_add(&ctx->aff_list, &neighbor->aff_list);
408 list_add_tail(&ctx->aff_list, &neighbor->aff_list);
409 if (neighbor->aff_head) {
410 neighbor->aff_head = 0;
415 if (!ctx->gang->aff_ref_ctx)
416 ctx->gang->aff_ref_ctx = ctx;
421 spufs_create_context(struct inode *inode, struct dentry *dentry,
422 struct vfsmount *mnt, int flags, umode_t mode,
423 struct file *aff_filp)
427 struct spu_gang *gang;
428 struct spu_context *neighbor;
429 struct path path = {.mnt = mnt, .dentry = dentry};
431 if ((flags & SPU_CREATE_NOSCHED) &&
432 !capable(CAP_SYS_NICE))
435 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
436 == SPU_CREATE_ISOLATE)
439 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
444 affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
446 gang = SPUFS_I(inode)->i_gang;
449 mutex_lock(&gang->aff_mutex);
450 neighbor = spufs_assert_affinity(flags, gang, aff_filp);
451 if (IS_ERR(neighbor)) {
452 ret = PTR_ERR(neighbor);
457 ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
462 spufs_set_affinity(flags, SPUFS_I(dentry->d_inode)->i_ctx,
465 put_spu_context(neighbor);
468 ret = spufs_context_open(&path);
470 WARN_ON(spufs_rmdir(inode, dentry));
474 mutex_unlock(&gang->aff_mutex);
479 spufs_mkgang(struct inode *dir, struct dentry *dentry, umode_t mode)
483 struct spu_gang *gang;
486 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
491 if (dir->i_mode & S_ISGID) {
492 inode->i_gid = dir->i_gid;
493 inode->i_mode &= S_ISGID;
495 gang = alloc_spu_gang();
496 SPUFS_I(inode)->i_ctx = NULL;
497 SPUFS_I(inode)->i_gang = gang;
501 inode->i_op = &simple_dir_inode_operations;
502 inode->i_fop = &simple_dir_operations;
504 d_instantiate(dentry, inode);
506 inc_nlink(dentry->d_inode);
515 static int spufs_gang_open(struct path *path)
520 ret = get_unused_fd();
525 * get references for dget and mntget, will be released
526 * in error path of *_open().
528 filp = dentry_open(path, O_RDONLY, current_cred());
531 return PTR_ERR(filp);
534 filp->f_op = &simple_dir_operations;
535 fd_install(ret, filp);
539 static int spufs_create_gang(struct inode *inode,
540 struct dentry *dentry,
541 struct vfsmount *mnt, umode_t mode)
543 struct path path = {.mnt = mnt, .dentry = dentry};
546 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
548 ret = spufs_gang_open(&path);
550 int err = simple_rmdir(inode, dentry);
558 static struct file_system_type spufs_type;
560 long spufs_create(struct path *path, struct dentry *dentry,
561 unsigned int flags, umode_t mode, struct file *filp)
563 struct inode *dir = path->dentry->d_inode;
566 /* check if we are on spufs */
567 if (path->dentry->d_sb->s_type != &spufs_type)
570 /* don't accept undefined flags */
571 if (flags & (~SPU_CREATE_FLAG_ALL))
574 /* only threads can be underneath a gang */
575 if (path->dentry != path->dentry->d_sb->s_root)
576 if ((flags & SPU_CREATE_GANG) || !SPUFS_I(dir)->i_gang)
579 mode &= ~current_umask();
581 if (flags & SPU_CREATE_GANG)
582 ret = spufs_create_gang(dir, dentry, path->mnt, mode);
584 ret = spufs_create_context(dir, dentry, path->mnt, flags, mode,
587 fsnotify_mkdir(dir, dentry);
592 /* File system initialization */
594 Opt_uid, Opt_gid, Opt_mode, Opt_debug, Opt_err,
597 static const match_table_t spufs_tokens = {
598 { Opt_uid, "uid=%d" },
599 { Opt_gid, "gid=%d" },
600 { Opt_mode, "mode=%o" },
601 { Opt_debug, "debug" },
606 spufs_parse_options(struct super_block *sb, char *options, struct inode *root)
609 substring_t args[MAX_OPT_ARGS];
611 while ((p = strsep(&options, ",")) != NULL) {
617 token = match_token(p, spufs_tokens, args);
620 if (match_int(&args[0], &option))
622 root->i_uid = option;
625 if (match_int(&args[0], &option))
627 root->i_gid = option;
630 if (match_octal(&args[0], &option))
632 root->i_mode = option | S_IFDIR;
635 spufs_get_sb_info(sb)->debug = 1;
644 static void spufs_exit_isolated_loader(void)
646 free_pages((unsigned long) isolated_loader,
647 get_order(isolated_loader_size));
651 spufs_init_isolated_loader(void)
653 struct device_node *dn;
657 dn = of_find_node_by_path("/spu-isolation");
661 loader = of_get_property(dn, "loader", &size);
665 /* the loader must be align on a 16 byte boundary */
666 isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
667 if (!isolated_loader)
670 isolated_loader_size = size;
671 memcpy(isolated_loader, loader, size);
672 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
676 spufs_create_root(struct super_block *sb, void *data)
682 if (!spu_management_ops)
686 inode = spufs_new_inode(sb, S_IFDIR | 0775);
690 inode->i_op = &simple_dir_inode_operations;
691 inode->i_fop = &simple_dir_operations;
692 SPUFS_I(inode)->i_ctx = NULL;
696 if (!spufs_parse_options(sb, data, inode))
700 sb->s_root = d_make_root(inode);
712 spufs_fill_super(struct super_block *sb, void *data, int silent)
714 struct spufs_sb_info *info;
715 static const struct super_operations s_ops = {
716 .alloc_inode = spufs_alloc_inode,
717 .destroy_inode = spufs_destroy_inode,
718 .statfs = simple_statfs,
719 .evict_inode = spufs_evict_inode,
720 .show_options = generic_show_options,
723 save_mount_options(sb, data);
725 info = kzalloc(sizeof(*info), GFP_KERNEL);
729 sb->s_maxbytes = MAX_LFS_FILESIZE;
730 sb->s_blocksize = PAGE_CACHE_SIZE;
731 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
732 sb->s_magic = SPUFS_MAGIC;
734 sb->s_fs_info = info;
736 return spufs_create_root(sb, data);
739 static struct dentry *
740 spufs_mount(struct file_system_type *fstype, int flags,
741 const char *name, void *data)
743 return mount_single(fstype, flags, data, spufs_fill_super);
746 static struct file_system_type spufs_type = {
747 .owner = THIS_MODULE,
749 .mount = spufs_mount,
750 .kill_sb = kill_litter_super,
752 MODULE_ALIAS_FS("spufs");
754 static int __init spufs_init(void)
759 if (!spu_management_ops)
763 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
764 sizeof(struct spufs_inode_info), 0,
765 SLAB_HWCACHE_ALIGN, spufs_init_once);
767 if (!spufs_inode_cache)
769 ret = spu_sched_init();
772 ret = register_spu_syscalls(&spufs_calls);
775 ret = register_filesystem(&spufs_type);
779 spufs_init_isolated_loader();
784 unregister_spu_syscalls(&spufs_calls);
788 kmem_cache_destroy(spufs_inode_cache);
792 module_init(spufs_init);
794 static void __exit spufs_exit(void)
797 spufs_exit_isolated_loader();
798 unregister_spu_syscalls(&spufs_calls);
799 unregister_filesystem(&spufs_type);
800 kmem_cache_destroy(spufs_inode_cache);
802 module_exit(spufs_exit);
804 MODULE_LICENSE("GPL");
805 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");