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 INIT_LIST_HEAD(&inode->i_dentry);
78 kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
81 static void spufs_destroy_inode(struct inode *inode)
83 call_rcu(&inode->i_rcu, spufs_i_callback);
87 spufs_init_once(void *p)
89 struct spufs_inode_info *ei = p;
91 inode_init_once(&ei->vfs_inode);
95 spufs_new_inode(struct super_block *sb, int mode)
99 inode = new_inode(sb);
103 inode->i_mode = mode;
104 inode->i_uid = current_fsuid();
105 inode->i_gid = current_fsgid();
106 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
112 spufs_setattr(struct dentry *dentry, struct iattr *attr)
114 struct inode *inode = dentry->d_inode;
116 if ((attr->ia_valid & ATTR_SIZE) &&
117 (attr->ia_size != inode->i_size))
119 setattr_copy(inode, attr);
120 mark_inode_dirty(inode);
126 spufs_new_file(struct super_block *sb, struct dentry *dentry,
127 const struct file_operations *fops, int mode,
128 size_t size, struct spu_context *ctx)
130 static const struct inode_operations spufs_file_iops = {
131 .setattr = spufs_setattr,
137 inode = spufs_new_inode(sb, S_IFREG | mode);
142 inode->i_op = &spufs_file_iops;
144 inode->i_size = size;
145 inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
146 d_add(dentry, inode);
152 spufs_evict_inode(struct inode *inode)
154 struct spufs_inode_info *ei = SPUFS_I(inode);
155 end_writeback(inode);
157 put_spu_context(ei->i_ctx);
159 put_spu_gang(ei->i_gang);
162 static void spufs_prune_dir(struct dentry *dir)
164 struct dentry *dentry, *tmp;
166 mutex_lock(&dir->d_inode->i_mutex);
167 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
168 spin_lock(&dentry->d_lock);
169 if (!(d_unhashed(dentry)) && dentry->d_inode) {
172 spin_unlock(&dentry->d_lock);
173 simple_unlink(dir->d_inode, dentry);
174 /* XXX: what was dcache_lock protecting here? Other
175 * filesystems (IB, configfs) release dcache_lock
179 spin_unlock(&dentry->d_lock);
182 shrink_dcache_parent(dir);
183 mutex_unlock(&dir->d_inode->i_mutex);
186 /* Caller must hold parent->i_mutex */
187 static int spufs_rmdir(struct inode *parent, struct dentry *dir)
189 /* remove all entries */
190 spufs_prune_dir(dir);
193 return simple_rmdir(parent, dir);
196 static int spufs_fill_dir(struct dentry *dir,
197 const struct spufs_tree_descr *files, int mode,
198 struct spu_context *ctx)
200 struct dentry *dentry, *tmp;
203 while (files->name && files->name[0]) {
205 dentry = d_alloc_name(dir, files->name);
208 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
209 files->mode & mode, files->size, ctx);
217 * remove all children from dir. dir->inode is not set so don't
218 * just simply use spufs_prune_dir() and panic afterwards :)
219 * dput() looks like it will do the right thing:
220 * - dec parent's ref counter
221 * - remove child from parent's child list
222 * - free child's inode if possible
225 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
229 shrink_dcache_parent(dir);
233 static int spufs_dir_close(struct inode *inode, struct file *file)
235 struct spu_context *ctx;
236 struct inode *parent;
240 dir = file->f_path.dentry;
241 parent = dir->d_parent->d_inode;
242 ctx = SPUFS_I(dir->d_inode)->i_ctx;
244 mutex_lock_nested(&parent->i_mutex, I_MUTEX_PARENT);
245 ret = spufs_rmdir(parent, dir);
246 mutex_unlock(&parent->i_mutex);
249 /* We have to give up the mm_struct */
252 return dcache_dir_close(inode, file);
255 const struct file_operations spufs_context_fops = {
256 .open = dcache_dir_open,
257 .release = spufs_dir_close,
258 .llseek = dcache_dir_lseek,
259 .read = generic_read_dir,
260 .readdir = dcache_readdir,
263 EXPORT_SYMBOL_GPL(spufs_context_fops);
266 spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
271 struct spu_context *ctx;
274 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
278 if (dir->i_mode & S_ISGID) {
279 inode->i_gid = dir->i_gid;
280 inode->i_mode &= S_ISGID;
282 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
283 SPUFS_I(inode)->i_ctx = ctx;
288 inode->i_op = &simple_dir_inode_operations;
289 inode->i_fop = &simple_dir_operations;
290 if (flags & SPU_CREATE_NOSCHED)
291 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
294 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
299 if (spufs_get_sb_info(dir->i_sb)->debug)
300 ret = spufs_fill_dir(dentry, spufs_dir_debug_contents,
306 d_instantiate(dentry, inode);
309 inc_nlink(dentry->d_inode);
314 put_spu_context(ctx);
321 static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
326 ret = get_unused_fd();
333 filp = dentry_open(dentry, mnt, O_RDONLY, current_cred());
340 filp->f_op = &spufs_context_fops;
341 fd_install(ret, filp);
346 static struct spu_context *
347 spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
350 struct spu_context *tmp, *neighbor, *err;
354 aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
355 struct spu, cbe_list))->aff_list);
358 return ERR_PTR(-EINVAL);
360 if (flags & SPU_CREATE_GANG)
361 return ERR_PTR(-EINVAL);
363 if (flags & SPU_CREATE_AFFINITY_MEM &&
365 gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
366 return ERR_PTR(-EEXIST);
368 if (gang->aff_flags & AFF_MERGED)
369 return ERR_PTR(-EBUSY);
372 if (flags & SPU_CREATE_AFFINITY_SPU) {
373 if (!filp || filp->f_op != &spufs_context_fops)
374 return ERR_PTR(-EINVAL);
376 neighbor = get_spu_context(
377 SPUFS_I(filp->f_dentry->d_inode)->i_ctx);
379 if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
380 !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
381 !list_entry(neighbor->aff_list.next, struct spu_context,
382 aff_list)->aff_head) {
383 err = ERR_PTR(-EEXIST);
384 goto out_put_neighbor;
387 if (gang != neighbor->gang) {
388 err = ERR_PTR(-EINVAL);
389 goto out_put_neighbor;
393 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
395 if (list_empty(&neighbor->aff_list))
398 for (node = 0; node < MAX_NUMNODES; node++) {
399 if ((cbe_spu_info[node].n_spus - atomic_read(
400 &cbe_spu_info[node].reserved_spus)) >= count)
404 if (node == MAX_NUMNODES) {
405 err = ERR_PTR(-EEXIST);
406 goto out_put_neighbor;
413 put_spu_context(neighbor);
418 spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
419 struct spu_context *neighbor)
421 if (flags & SPU_CREATE_AFFINITY_MEM)
422 ctx->gang->aff_ref_ctx = ctx;
424 if (flags & SPU_CREATE_AFFINITY_SPU) {
425 if (list_empty(&neighbor->aff_list)) {
426 list_add_tail(&neighbor->aff_list,
427 &ctx->gang->aff_list_head);
428 neighbor->aff_head = 1;
431 if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
432 || list_entry(neighbor->aff_list.next, struct spu_context,
433 aff_list)->aff_head) {
434 list_add(&ctx->aff_list, &neighbor->aff_list);
436 list_add_tail(&ctx->aff_list, &neighbor->aff_list);
437 if (neighbor->aff_head) {
438 neighbor->aff_head = 0;
443 if (!ctx->gang->aff_ref_ctx)
444 ctx->gang->aff_ref_ctx = ctx;
449 spufs_create_context(struct inode *inode, struct dentry *dentry,
450 struct vfsmount *mnt, int flags, int mode,
451 struct file *aff_filp)
455 struct spu_gang *gang;
456 struct spu_context *neighbor;
459 if ((flags & SPU_CREATE_NOSCHED) &&
460 !capable(CAP_SYS_NICE))
464 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
465 == SPU_CREATE_ISOLATE)
469 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
474 affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
476 gang = SPUFS_I(inode)->i_gang;
480 mutex_lock(&gang->aff_mutex);
481 neighbor = spufs_assert_affinity(flags, gang, aff_filp);
482 if (IS_ERR(neighbor)) {
483 ret = PTR_ERR(neighbor);
488 ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
493 spufs_set_affinity(flags, SPUFS_I(dentry->d_inode)->i_ctx,
496 put_spu_context(neighbor);
500 * get references for dget and mntget, will be released
501 * in error path of *_open().
503 ret = spufs_context_open(dget(dentry), mntget(mnt));
505 WARN_ON(spufs_rmdir(inode, dentry));
507 mutex_unlock(&gang->aff_mutex);
508 mutex_unlock(&inode->i_mutex);
509 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
515 mutex_unlock(&gang->aff_mutex);
517 mutex_unlock(&inode->i_mutex);
524 spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
528 struct spu_gang *gang;
531 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
536 if (dir->i_mode & S_ISGID) {
537 inode->i_gid = dir->i_gid;
538 inode->i_mode &= S_ISGID;
540 gang = alloc_spu_gang();
541 SPUFS_I(inode)->i_ctx = NULL;
542 SPUFS_I(inode)->i_gang = gang;
546 inode->i_op = &simple_dir_inode_operations;
547 inode->i_fop = &simple_dir_operations;
549 d_instantiate(dentry, inode);
551 inc_nlink(dentry->d_inode);
560 static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
565 ret = get_unused_fd();
572 filp = dentry_open(dentry, mnt, O_RDONLY, current_cred());
579 filp->f_op = &simple_dir_operations;
580 fd_install(ret, filp);
585 static int spufs_create_gang(struct inode *inode,
586 struct dentry *dentry,
587 struct vfsmount *mnt, int mode)
591 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
596 * get references for dget and mntget, will be released
597 * in error path of *_open().
599 ret = spufs_gang_open(dget(dentry), mntget(mnt));
601 int err = simple_rmdir(inode, dentry);
606 mutex_unlock(&inode->i_mutex);
612 static struct file_system_type spufs_type;
614 long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode,
617 struct dentry *dentry;
621 /* check if we are on spufs */
622 if (nd->path.dentry->d_sb->s_type != &spufs_type)
625 /* don't accept undefined flags */
626 if (flags & (~SPU_CREATE_FLAG_ALL))
629 /* only threads can be underneath a gang */
630 if (nd->path.dentry != nd->path.dentry->d_sb->s_root) {
631 if ((flags & SPU_CREATE_GANG) ||
632 !SPUFS_I(nd->path.dentry->d_inode)->i_gang)
636 dentry = lookup_create(nd, 1);
637 ret = PTR_ERR(dentry);
641 mode &= ~current_umask();
643 if (flags & SPU_CREATE_GANG)
644 ret = spufs_create_gang(nd->path.dentry->d_inode,
645 dentry, nd->path.mnt, mode);
647 ret = spufs_create_context(nd->path.dentry->d_inode,
648 dentry, nd->path.mnt, flags, mode,
651 fsnotify_mkdir(nd->path.dentry->d_inode, dentry);
655 mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
660 /* File system initialization */
662 Opt_uid, Opt_gid, Opt_mode, Opt_debug, Opt_err,
665 static const match_table_t spufs_tokens = {
666 { Opt_uid, "uid=%d" },
667 { Opt_gid, "gid=%d" },
668 { Opt_mode, "mode=%o" },
669 { Opt_debug, "debug" },
674 spufs_parse_options(struct super_block *sb, char *options, struct inode *root)
677 substring_t args[MAX_OPT_ARGS];
679 while ((p = strsep(&options, ",")) != NULL) {
685 token = match_token(p, spufs_tokens, args);
688 if (match_int(&args[0], &option))
690 root->i_uid = option;
693 if (match_int(&args[0], &option))
695 root->i_gid = option;
698 if (match_octal(&args[0], &option))
700 root->i_mode = option | S_IFDIR;
703 spufs_get_sb_info(sb)->debug = 1;
712 static void spufs_exit_isolated_loader(void)
714 free_pages((unsigned long) isolated_loader,
715 get_order(isolated_loader_size));
719 spufs_init_isolated_loader(void)
721 struct device_node *dn;
725 dn = of_find_node_by_path("/spu-isolation");
729 loader = of_get_property(dn, "loader", &size);
733 /* the loader must be align on a 16 byte boundary */
734 isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
735 if (!isolated_loader)
738 isolated_loader_size = size;
739 memcpy(isolated_loader, loader, size);
740 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
744 spufs_create_root(struct super_block *sb, void *data)
750 if (!spu_management_ops)
754 inode = spufs_new_inode(sb, S_IFDIR | 0775);
758 inode->i_op = &simple_dir_inode_operations;
759 inode->i_fop = &simple_dir_operations;
760 SPUFS_I(inode)->i_ctx = NULL;
764 if (!spufs_parse_options(sb, data, inode))
768 sb->s_root = d_alloc_root(inode);
780 spufs_fill_super(struct super_block *sb, void *data, int silent)
782 struct spufs_sb_info *info;
783 static const struct super_operations s_ops = {
784 .alloc_inode = spufs_alloc_inode,
785 .destroy_inode = spufs_destroy_inode,
786 .statfs = simple_statfs,
787 .evict_inode = spufs_evict_inode,
788 .show_options = generic_show_options,
791 save_mount_options(sb, data);
793 info = kzalloc(sizeof(*info), GFP_KERNEL);
797 sb->s_maxbytes = MAX_LFS_FILESIZE;
798 sb->s_blocksize = PAGE_CACHE_SIZE;
799 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
800 sb->s_magic = SPUFS_MAGIC;
802 sb->s_fs_info = info;
804 return spufs_create_root(sb, data);
807 static struct dentry *
808 spufs_mount(struct file_system_type *fstype, int flags,
809 const char *name, void *data)
811 return mount_single(fstype, flags, data, spufs_fill_super);
814 static struct file_system_type spufs_type = {
815 .owner = THIS_MODULE,
817 .mount = spufs_mount,
818 .kill_sb = kill_litter_super,
821 static int __init spufs_init(void)
826 if (!spu_management_ops)
830 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
831 sizeof(struct spufs_inode_info), 0,
832 SLAB_HWCACHE_ALIGN, spufs_init_once);
834 if (!spufs_inode_cache)
836 ret = spu_sched_init();
839 ret = register_filesystem(&spufs_type);
842 ret = register_spu_syscalls(&spufs_calls);
846 spufs_init_isolated_loader();
851 unregister_filesystem(&spufs_type);
855 kmem_cache_destroy(spufs_inode_cache);
859 module_init(spufs_init);
861 static void __exit spufs_exit(void)
864 spufs_exit_isolated_loader();
865 unregister_spu_syscalls(&spufs_calls);
866 unregister_filesystem(&spufs_type);
867 kmem_cache_destroy(spufs_inode_cache);
869 module_exit(spufs_exit);
871 MODULE_LICENSE("GPL");
872 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");