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_ino = get_next_ino();
104 inode->i_mode = mode;
105 inode->i_uid = current_fsuid();
106 inode->i_gid = current_fsgid();
107 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
113 spufs_setattr(struct dentry *dentry, struct iattr *attr)
115 struct inode *inode = dentry->d_inode;
117 if ((attr->ia_valid & ATTR_SIZE) &&
118 (attr->ia_size != inode->i_size))
120 setattr_copy(inode, attr);
121 mark_inode_dirty(inode);
127 spufs_new_file(struct super_block *sb, struct dentry *dentry,
128 const struct file_operations *fops, int mode,
129 size_t size, struct spu_context *ctx)
131 static const struct inode_operations spufs_file_iops = {
132 .setattr = spufs_setattr,
138 inode = spufs_new_inode(sb, S_IFREG | mode);
143 inode->i_op = &spufs_file_iops;
145 inode->i_size = size;
146 inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
147 d_add(dentry, inode);
153 spufs_evict_inode(struct inode *inode)
155 struct spufs_inode_info *ei = SPUFS_I(inode);
156 end_writeback(inode);
158 put_spu_context(ei->i_ctx);
160 put_spu_gang(ei->i_gang);
163 static void spufs_prune_dir(struct dentry *dir)
165 struct dentry *dentry, *tmp;
167 mutex_lock(&dir->d_inode->i_mutex);
168 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
169 spin_lock(&dentry->d_lock);
170 if (!(d_unhashed(dentry)) && dentry->d_inode) {
173 spin_unlock(&dentry->d_lock);
174 simple_unlink(dir->d_inode, dentry);
175 /* XXX: what was dcache_lock protecting here? Other
176 * filesystems (IB, configfs) release dcache_lock
180 spin_unlock(&dentry->d_lock);
183 shrink_dcache_parent(dir);
184 mutex_unlock(&dir->d_inode->i_mutex);
187 /* Caller must hold parent->i_mutex */
188 static int spufs_rmdir(struct inode *parent, struct dentry *dir)
190 /* remove all entries */
191 spufs_prune_dir(dir);
194 return simple_rmdir(parent, dir);
197 static int spufs_fill_dir(struct dentry *dir,
198 const struct spufs_tree_descr *files, int mode,
199 struct spu_context *ctx)
201 struct dentry *dentry, *tmp;
204 while (files->name && files->name[0]) {
206 dentry = d_alloc_name(dir, files->name);
209 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
210 files->mode & mode, files->size, ctx);
218 * remove all children from dir. dir->inode is not set so don't
219 * just simply use spufs_prune_dir() and panic afterwards :)
220 * dput() looks like it will do the right thing:
221 * - dec parent's ref counter
222 * - remove child from parent's child list
223 * - free child's inode if possible
226 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
230 shrink_dcache_parent(dir);
234 static int spufs_dir_close(struct inode *inode, struct file *file)
236 struct spu_context *ctx;
237 struct inode *parent;
241 dir = file->f_path.dentry;
242 parent = dir->d_parent->d_inode;
243 ctx = SPUFS_I(dir->d_inode)->i_ctx;
245 mutex_lock_nested(&parent->i_mutex, I_MUTEX_PARENT);
246 ret = spufs_rmdir(parent, dir);
247 mutex_unlock(&parent->i_mutex);
250 /* We have to give up the mm_struct */
253 return dcache_dir_close(inode, file);
256 const struct file_operations spufs_context_fops = {
257 .open = dcache_dir_open,
258 .release = spufs_dir_close,
259 .llseek = dcache_dir_lseek,
260 .read = generic_read_dir,
261 .readdir = dcache_readdir,
264 EXPORT_SYMBOL_GPL(spufs_context_fops);
267 spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
272 struct spu_context *ctx;
275 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
279 if (dir->i_mode & S_ISGID) {
280 inode->i_gid = dir->i_gid;
281 inode->i_mode &= S_ISGID;
283 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
284 SPUFS_I(inode)->i_ctx = ctx;
289 inode->i_op = &simple_dir_inode_operations;
290 inode->i_fop = &simple_dir_operations;
291 if (flags & SPU_CREATE_NOSCHED)
292 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
295 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
300 if (spufs_get_sb_info(dir->i_sb)->debug)
301 ret = spufs_fill_dir(dentry, spufs_dir_debug_contents,
307 d_instantiate(dentry, inode);
310 inc_nlink(dentry->d_inode);
315 put_spu_context(ctx);
322 static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
327 ret = get_unused_fd();
334 filp = dentry_open(dentry, mnt, O_RDONLY, current_cred());
341 filp->f_op = &spufs_context_fops;
342 fd_install(ret, filp);
347 static struct spu_context *
348 spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
351 struct spu_context *tmp, *neighbor, *err;
355 aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
356 struct spu, cbe_list))->aff_list);
359 return ERR_PTR(-EINVAL);
361 if (flags & SPU_CREATE_GANG)
362 return ERR_PTR(-EINVAL);
364 if (flags & SPU_CREATE_AFFINITY_MEM &&
366 gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
367 return ERR_PTR(-EEXIST);
369 if (gang->aff_flags & AFF_MERGED)
370 return ERR_PTR(-EBUSY);
373 if (flags & SPU_CREATE_AFFINITY_SPU) {
374 if (!filp || filp->f_op != &spufs_context_fops)
375 return ERR_PTR(-EINVAL);
377 neighbor = get_spu_context(
378 SPUFS_I(filp->f_dentry->d_inode)->i_ctx);
380 if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
381 !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
382 !list_entry(neighbor->aff_list.next, struct spu_context,
383 aff_list)->aff_head) {
384 err = ERR_PTR(-EEXIST);
385 goto out_put_neighbor;
388 if (gang != neighbor->gang) {
389 err = ERR_PTR(-EINVAL);
390 goto out_put_neighbor;
394 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
396 if (list_empty(&neighbor->aff_list))
399 for (node = 0; node < MAX_NUMNODES; node++) {
400 if ((cbe_spu_info[node].n_spus - atomic_read(
401 &cbe_spu_info[node].reserved_spus)) >= count)
405 if (node == MAX_NUMNODES) {
406 err = ERR_PTR(-EEXIST);
407 goto out_put_neighbor;
414 put_spu_context(neighbor);
419 spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
420 struct spu_context *neighbor)
422 if (flags & SPU_CREATE_AFFINITY_MEM)
423 ctx->gang->aff_ref_ctx = ctx;
425 if (flags & SPU_CREATE_AFFINITY_SPU) {
426 if (list_empty(&neighbor->aff_list)) {
427 list_add_tail(&neighbor->aff_list,
428 &ctx->gang->aff_list_head);
429 neighbor->aff_head = 1;
432 if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
433 || list_entry(neighbor->aff_list.next, struct spu_context,
434 aff_list)->aff_head) {
435 list_add(&ctx->aff_list, &neighbor->aff_list);
437 list_add_tail(&ctx->aff_list, &neighbor->aff_list);
438 if (neighbor->aff_head) {
439 neighbor->aff_head = 0;
444 if (!ctx->gang->aff_ref_ctx)
445 ctx->gang->aff_ref_ctx = ctx;
450 spufs_create_context(struct inode *inode, struct dentry *dentry,
451 struct vfsmount *mnt, int flags, int mode,
452 struct file *aff_filp)
456 struct spu_gang *gang;
457 struct spu_context *neighbor;
460 if ((flags & SPU_CREATE_NOSCHED) &&
461 !capable(CAP_SYS_NICE))
465 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
466 == SPU_CREATE_ISOLATE)
470 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
475 affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
477 gang = SPUFS_I(inode)->i_gang;
481 mutex_lock(&gang->aff_mutex);
482 neighbor = spufs_assert_affinity(flags, gang, aff_filp);
483 if (IS_ERR(neighbor)) {
484 ret = PTR_ERR(neighbor);
489 ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
494 spufs_set_affinity(flags, SPUFS_I(dentry->d_inode)->i_ctx,
497 put_spu_context(neighbor);
501 * get references for dget and mntget, will be released
502 * in error path of *_open().
504 ret = spufs_context_open(dget(dentry), mntget(mnt));
506 WARN_ON(spufs_rmdir(inode, dentry));
508 mutex_unlock(&gang->aff_mutex);
509 mutex_unlock(&inode->i_mutex);
510 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
516 mutex_unlock(&gang->aff_mutex);
518 mutex_unlock(&inode->i_mutex);
525 spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
529 struct spu_gang *gang;
532 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
537 if (dir->i_mode & S_ISGID) {
538 inode->i_gid = dir->i_gid;
539 inode->i_mode &= S_ISGID;
541 gang = alloc_spu_gang();
542 SPUFS_I(inode)->i_ctx = NULL;
543 SPUFS_I(inode)->i_gang = gang;
547 inode->i_op = &simple_dir_inode_operations;
548 inode->i_fop = &simple_dir_operations;
550 d_instantiate(dentry, inode);
552 inc_nlink(dentry->d_inode);
561 static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
566 ret = get_unused_fd();
573 filp = dentry_open(dentry, mnt, O_RDONLY, current_cred());
580 filp->f_op = &simple_dir_operations;
581 fd_install(ret, filp);
586 static int spufs_create_gang(struct inode *inode,
587 struct dentry *dentry,
588 struct vfsmount *mnt, int mode)
592 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
597 * get references for dget and mntget, will be released
598 * in error path of *_open().
600 ret = spufs_gang_open(dget(dentry), mntget(mnt));
602 int err = simple_rmdir(inode, dentry);
607 mutex_unlock(&inode->i_mutex);
613 static struct file_system_type spufs_type;
615 long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode,
618 struct dentry *dentry;
622 /* check if we are on spufs */
623 if (nd->path.dentry->d_sb->s_type != &spufs_type)
626 /* don't accept undefined flags */
627 if (flags & (~SPU_CREATE_FLAG_ALL))
630 /* only threads can be underneath a gang */
631 if (nd->path.dentry != nd->path.dentry->d_sb->s_root) {
632 if ((flags & SPU_CREATE_GANG) ||
633 !SPUFS_I(nd->path.dentry->d_inode)->i_gang)
637 dentry = lookup_create(nd, 1);
638 ret = PTR_ERR(dentry);
642 mode &= ~current_umask();
644 if (flags & SPU_CREATE_GANG)
645 ret = spufs_create_gang(nd->path.dentry->d_inode,
646 dentry, nd->path.mnt, mode);
648 ret = spufs_create_context(nd->path.dentry->d_inode,
649 dentry, nd->path.mnt, flags, mode,
652 fsnotify_mkdir(nd->path.dentry->d_inode, dentry);
656 mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
661 /* File system initialization */
663 Opt_uid, Opt_gid, Opt_mode, Opt_debug, Opt_err,
666 static const match_table_t spufs_tokens = {
667 { Opt_uid, "uid=%d" },
668 { Opt_gid, "gid=%d" },
669 { Opt_mode, "mode=%o" },
670 { Opt_debug, "debug" },
675 spufs_parse_options(struct super_block *sb, char *options, struct inode *root)
678 substring_t args[MAX_OPT_ARGS];
680 while ((p = strsep(&options, ",")) != NULL) {
686 token = match_token(p, spufs_tokens, args);
689 if (match_int(&args[0], &option))
691 root->i_uid = option;
694 if (match_int(&args[0], &option))
696 root->i_gid = option;
699 if (match_octal(&args[0], &option))
701 root->i_mode = option | S_IFDIR;
704 spufs_get_sb_info(sb)->debug = 1;
713 static void spufs_exit_isolated_loader(void)
715 free_pages((unsigned long) isolated_loader,
716 get_order(isolated_loader_size));
720 spufs_init_isolated_loader(void)
722 struct device_node *dn;
726 dn = of_find_node_by_path("/spu-isolation");
730 loader = of_get_property(dn, "loader", &size);
734 /* the loader must be align on a 16 byte boundary */
735 isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
736 if (!isolated_loader)
739 isolated_loader_size = size;
740 memcpy(isolated_loader, loader, size);
741 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
745 spufs_create_root(struct super_block *sb, void *data)
751 if (!spu_management_ops)
755 inode = spufs_new_inode(sb, S_IFDIR | 0775);
759 inode->i_op = &simple_dir_inode_operations;
760 inode->i_fop = &simple_dir_operations;
761 SPUFS_I(inode)->i_ctx = NULL;
765 if (!spufs_parse_options(sb, data, inode))
769 sb->s_root = d_alloc_root(inode);
781 spufs_fill_super(struct super_block *sb, void *data, int silent)
783 struct spufs_sb_info *info;
784 static const struct super_operations s_ops = {
785 .alloc_inode = spufs_alloc_inode,
786 .destroy_inode = spufs_destroy_inode,
787 .statfs = simple_statfs,
788 .evict_inode = spufs_evict_inode,
789 .show_options = generic_show_options,
792 save_mount_options(sb, data);
794 info = kzalloc(sizeof(*info), GFP_KERNEL);
798 sb->s_maxbytes = MAX_LFS_FILESIZE;
799 sb->s_blocksize = PAGE_CACHE_SIZE;
800 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
801 sb->s_magic = SPUFS_MAGIC;
803 sb->s_fs_info = info;
805 return spufs_create_root(sb, data);
808 static struct dentry *
809 spufs_mount(struct file_system_type *fstype, int flags,
810 const char *name, void *data)
812 return mount_single(fstype, flags, data, spufs_fill_super);
815 static struct file_system_type spufs_type = {
816 .owner = THIS_MODULE,
818 .mount = spufs_mount,
819 .kill_sb = kill_litter_super,
822 static int __init spufs_init(void)
827 if (!spu_management_ops)
831 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
832 sizeof(struct spufs_inode_info), 0,
833 SLAB_HWCACHE_ALIGN, spufs_init_once);
835 if (!spufs_inode_cache)
837 ret = spu_sched_init();
840 ret = register_filesystem(&spufs_type);
843 ret = register_spu_syscalls(&spufs_calls);
847 spufs_init_isolated_loader();
852 unregister_filesystem(&spufs_type);
856 kmem_cache_destroy(spufs_inode_cache);
860 module_init(spufs_init);
862 static void __exit spufs_exit(void)
865 spufs_exit_isolated_loader();
866 unregister_spu_syscalls(&spufs_calls);
867 unregister_filesystem(&spufs_type);
868 kmem_cache_destroy(spufs_inode_cache);
870 module_exit(spufs_exit);
872 MODULE_LICENSE("GPL");
873 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");