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/backing-dev.h>
27 #include <linux/init.h>
28 #include <linux/ioctl.h>
29 #include <linux/module.h>
30 #include <linux/mount.h>
31 #include <linux/namei.h>
32 #include <linux/pagemap.h>
33 #include <linux/poll.h>
34 #include <linux/slab.h>
35 #include <linux/parser.h>
39 #include <asm/spu_priv1.h>
40 #include <asm/uaccess.h>
44 static struct kmem_cache *spufs_inode_cache;
45 char *isolated_loader;
46 static int isolated_loader_size;
49 spufs_alloc_inode(struct super_block *sb)
51 struct spufs_inode_info *ei;
53 ei = kmem_cache_alloc(spufs_inode_cache, GFP_KERNEL);
61 return &ei->vfs_inode;
65 spufs_destroy_inode(struct inode *inode)
67 kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
71 spufs_init_once(struct kmem_cache *cachep, void *p)
73 struct spufs_inode_info *ei = p;
75 inode_init_once(&ei->vfs_inode);
79 spufs_new_inode(struct super_block *sb, int mode)
83 inode = new_inode(sb);
88 inode->i_uid = current->fsuid;
89 inode->i_gid = current->fsgid;
91 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
97 spufs_setattr(struct dentry *dentry, struct iattr *attr)
99 struct inode *inode = dentry->d_inode;
101 if ((attr->ia_valid & ATTR_SIZE) &&
102 (attr->ia_size != inode->i_size))
104 return inode_setattr(inode, attr);
109 spufs_new_file(struct super_block *sb, struct dentry *dentry,
110 const struct file_operations *fops, int mode,
111 struct spu_context *ctx)
113 static struct inode_operations spufs_file_iops = {
114 .setattr = spufs_setattr,
120 inode = spufs_new_inode(sb, S_IFREG | mode);
125 inode->i_op = &spufs_file_iops;
127 inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
128 d_add(dentry, inode);
134 spufs_delete_inode(struct inode *inode)
136 struct spufs_inode_info *ei = SPUFS_I(inode);
139 put_spu_context(ei->i_ctx);
141 put_spu_gang(ei->i_gang);
145 static void spufs_prune_dir(struct dentry *dir)
147 struct dentry *dentry, *tmp;
149 mutex_lock(&dir->d_inode->i_mutex);
150 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
151 spin_lock(&dcache_lock);
152 spin_lock(&dentry->d_lock);
153 if (!(d_unhashed(dentry)) && dentry->d_inode) {
156 spin_unlock(&dentry->d_lock);
157 simple_unlink(dir->d_inode, dentry);
158 spin_unlock(&dcache_lock);
161 spin_unlock(&dentry->d_lock);
162 spin_unlock(&dcache_lock);
165 shrink_dcache_parent(dir);
166 mutex_unlock(&dir->d_inode->i_mutex);
169 /* Caller must hold parent->i_mutex */
170 static int spufs_rmdir(struct inode *parent, struct dentry *dir)
172 /* remove all entries */
173 spufs_prune_dir(dir);
176 return simple_rmdir(parent, dir);
179 static int spufs_fill_dir(struct dentry *dir, struct tree_descr *files,
180 int mode, struct spu_context *ctx)
182 struct dentry *dentry, *tmp;
185 while (files->name && files->name[0]) {
187 dentry = d_alloc_name(dir, files->name);
190 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
191 files->mode & mode, ctx);
199 * remove all children from dir. dir->inode is not set so don't
200 * just simply use spufs_prune_dir() and panic afterwards :)
201 * dput() looks like it will do the right thing:
202 * - dec parent's ref counter
203 * - remove child from parent's child list
204 * - free child's inode if possible
207 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
211 shrink_dcache_parent(dir);
215 static int spufs_dir_close(struct inode *inode, struct file *file)
217 struct spu_context *ctx;
218 struct inode *parent;
222 dir = file->f_path.dentry;
223 parent = dir->d_parent->d_inode;
224 ctx = SPUFS_I(dir->d_inode)->i_ctx;
226 mutex_lock(&parent->i_mutex);
227 ret = spufs_rmdir(parent, dir);
228 mutex_unlock(&parent->i_mutex);
231 /* We have to give up the mm_struct */
234 return dcache_dir_close(inode, file);
237 const struct file_operations spufs_context_fops = {
238 .open = dcache_dir_open,
239 .release = spufs_dir_close,
240 .llseek = dcache_dir_lseek,
241 .read = generic_read_dir,
242 .readdir = dcache_readdir,
243 .fsync = simple_sync_file,
245 EXPORT_SYMBOL_GPL(spufs_context_fops);
248 spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
253 struct spu_context *ctx;
256 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
260 if (dir->i_mode & S_ISGID) {
261 inode->i_gid = dir->i_gid;
262 inode->i_mode &= S_ISGID;
264 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
265 SPUFS_I(inode)->i_ctx = ctx;
270 inode->i_op = &simple_dir_inode_operations;
271 inode->i_fop = &simple_dir_operations;
272 if (flags & SPU_CREATE_NOSCHED)
273 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
276 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
281 d_instantiate(dentry, inode);
284 dentry->d_inode->i_nlink++;
289 put_spu_context(ctx);
296 static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
301 ret = get_unused_fd();
308 filp = dentry_open(dentry, mnt, O_RDONLY);
315 filp->f_op = &spufs_context_fops;
316 fd_install(ret, filp);
321 static struct spu_context *
322 spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
325 struct spu_context *tmp, *neighbor, *err;
329 aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
330 struct spu, cbe_list))->aff_list);
333 return ERR_PTR(-EINVAL);
335 if (flags & SPU_CREATE_GANG)
336 return ERR_PTR(-EINVAL);
338 if (flags & SPU_CREATE_AFFINITY_MEM &&
340 gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
341 return ERR_PTR(-EEXIST);
343 if (gang->aff_flags & AFF_MERGED)
344 return ERR_PTR(-EBUSY);
347 if (flags & SPU_CREATE_AFFINITY_SPU) {
348 if (!filp || filp->f_op != &spufs_context_fops)
349 return ERR_PTR(-EINVAL);
351 neighbor = get_spu_context(
352 SPUFS_I(filp->f_dentry->d_inode)->i_ctx);
354 if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
355 !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
356 !list_entry(neighbor->aff_list.next, struct spu_context,
357 aff_list)->aff_head) {
358 err = ERR_PTR(-EEXIST);
359 goto out_put_neighbor;
362 if (gang != neighbor->gang) {
363 err = ERR_PTR(-EINVAL);
364 goto out_put_neighbor;
368 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
370 if (list_empty(&neighbor->aff_list))
373 for (node = 0; node < MAX_NUMNODES; node++) {
374 if ((cbe_spu_info[node].n_spus - atomic_read(
375 &cbe_spu_info[node].reserved_spus)) >= count)
379 if (node == MAX_NUMNODES) {
380 err = ERR_PTR(-EEXIST);
381 goto out_put_neighbor;
388 put_spu_context(neighbor);
393 spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
394 struct spu_context *neighbor)
396 if (flags & SPU_CREATE_AFFINITY_MEM)
397 ctx->gang->aff_ref_ctx = ctx;
399 if (flags & SPU_CREATE_AFFINITY_SPU) {
400 if (list_empty(&neighbor->aff_list)) {
401 list_add_tail(&neighbor->aff_list,
402 &ctx->gang->aff_list_head);
403 neighbor->aff_head = 1;
406 if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
407 || list_entry(neighbor->aff_list.next, struct spu_context,
408 aff_list)->aff_head) {
409 list_add(&ctx->aff_list, &neighbor->aff_list);
411 list_add_tail(&ctx->aff_list, &neighbor->aff_list);
412 if (neighbor->aff_head) {
413 neighbor->aff_head = 0;
418 if (!ctx->gang->aff_ref_ctx)
419 ctx->gang->aff_ref_ctx = ctx;
424 spufs_create_context(struct inode *inode, struct dentry *dentry,
425 struct vfsmount *mnt, int flags, int mode,
426 struct file *aff_filp)
430 struct spu_gang *gang;
431 struct spu_context *neighbor;
434 if ((flags & SPU_CREATE_NOSCHED) &&
435 !capable(CAP_SYS_NICE))
439 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
440 == SPU_CREATE_ISOLATE)
444 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
449 affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
451 gang = SPUFS_I(inode)->i_gang;
455 mutex_lock(&gang->aff_mutex);
456 neighbor = spufs_assert_affinity(flags, gang, aff_filp);
457 if (IS_ERR(neighbor)) {
458 ret = PTR_ERR(neighbor);
463 ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
468 spufs_set_affinity(flags, SPUFS_I(dentry->d_inode)->i_ctx,
471 put_spu_context(neighbor);
475 * get references for dget and mntget, will be released
476 * in error path of *_open().
478 ret = spufs_context_open(dget(dentry), mntget(mnt));
480 WARN_ON(spufs_rmdir(inode, dentry));
481 mutex_unlock(&inode->i_mutex);
482 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
488 mutex_unlock(&gang->aff_mutex);
490 mutex_unlock(&inode->i_mutex);
497 spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
501 struct spu_gang *gang;
504 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
509 if (dir->i_mode & S_ISGID) {
510 inode->i_gid = dir->i_gid;
511 inode->i_mode &= S_ISGID;
513 gang = alloc_spu_gang();
514 SPUFS_I(inode)->i_ctx = NULL;
515 SPUFS_I(inode)->i_gang = gang;
519 inode->i_op = &simple_dir_inode_operations;
520 inode->i_fop = &simple_dir_operations;
522 d_instantiate(dentry, inode);
524 dentry->d_inode->i_nlink++;
533 static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
538 ret = get_unused_fd();
545 filp = dentry_open(dentry, mnt, O_RDONLY);
552 filp->f_op = &simple_dir_operations;
553 fd_install(ret, filp);
558 static int spufs_create_gang(struct inode *inode,
559 struct dentry *dentry,
560 struct vfsmount *mnt, int mode)
564 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
569 * get references for dget and mntget, will be released
570 * in error path of *_open().
572 ret = spufs_gang_open(dget(dentry), mntget(mnt));
574 int err = simple_rmdir(inode, dentry);
579 mutex_unlock(&inode->i_mutex);
585 static struct file_system_type spufs_type;
587 long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode,
590 struct dentry *dentry;
594 /* check if we are on spufs */
595 if (nd->path.dentry->d_sb->s_type != &spufs_type)
598 /* don't accept undefined flags */
599 if (flags & (~SPU_CREATE_FLAG_ALL))
602 /* only threads can be underneath a gang */
603 if (nd->path.dentry != nd->path.dentry->d_sb->s_root) {
604 if ((flags & SPU_CREATE_GANG) ||
605 !SPUFS_I(nd->path.dentry->d_inode)->i_gang)
609 dentry = lookup_create(nd, 1);
610 ret = PTR_ERR(dentry);
618 mode &= ~current->fs->umask;
620 if (flags & SPU_CREATE_GANG)
621 return spufs_create_gang(nd->path.dentry->d_inode,
622 dentry, nd->path.mnt, mode);
624 return spufs_create_context(nd->path.dentry->d_inode,
625 dentry, nd->path.mnt, flags, mode,
631 mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
636 /* File system initialization */
638 Opt_uid, Opt_gid, Opt_mode, Opt_err,
641 static match_table_t spufs_tokens = {
642 { Opt_uid, "uid=%d" },
643 { Opt_gid, "gid=%d" },
644 { Opt_mode, "mode=%o" },
649 spufs_parse_options(char *options, struct inode *root)
652 substring_t args[MAX_OPT_ARGS];
654 while ((p = strsep(&options, ",")) != NULL) {
660 token = match_token(p, spufs_tokens, args);
663 if (match_int(&args[0], &option))
665 root->i_uid = option;
668 if (match_int(&args[0], &option))
670 root->i_gid = option;
673 if (match_octal(&args[0], &option))
675 root->i_mode = option | S_IFDIR;
684 static void spufs_exit_isolated_loader(void)
686 free_pages((unsigned long) isolated_loader,
687 get_order(isolated_loader_size));
691 spufs_init_isolated_loader(void)
693 struct device_node *dn;
697 dn = of_find_node_by_path("/spu-isolation");
701 loader = of_get_property(dn, "loader", &size);
705 /* the loader must be align on a 16 byte boundary */
706 isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
707 if (!isolated_loader)
710 isolated_loader_size = size;
711 memcpy(isolated_loader, loader, size);
712 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
716 spufs_create_root(struct super_block *sb, void *data)
722 if (!spu_management_ops)
726 inode = spufs_new_inode(sb, S_IFDIR | 0775);
730 inode->i_op = &simple_dir_inode_operations;
731 inode->i_fop = &simple_dir_operations;
732 SPUFS_I(inode)->i_ctx = NULL;
735 if (!spufs_parse_options(data, inode))
739 sb->s_root = d_alloc_root(inode);
751 spufs_fill_super(struct super_block *sb, void *data, int silent)
753 static struct super_operations s_ops = {
754 .alloc_inode = spufs_alloc_inode,
755 .destroy_inode = spufs_destroy_inode,
756 .statfs = simple_statfs,
757 .delete_inode = spufs_delete_inode,
758 .drop_inode = generic_delete_inode,
759 .show_options = generic_show_options,
762 save_mount_options(sb, data);
764 sb->s_maxbytes = MAX_LFS_FILESIZE;
765 sb->s_blocksize = PAGE_CACHE_SIZE;
766 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
767 sb->s_magic = SPUFS_MAGIC;
770 return spufs_create_root(sb, data);
774 spufs_get_sb(struct file_system_type *fstype, int flags,
775 const char *name, void *data, struct vfsmount *mnt)
777 return get_sb_single(fstype, flags, data, spufs_fill_super, mnt);
780 static struct file_system_type spufs_type = {
781 .owner = THIS_MODULE,
783 .get_sb = spufs_get_sb,
784 .kill_sb = kill_litter_super,
787 static int __init spufs_init(void)
792 if (!spu_management_ops)
796 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
797 sizeof(struct spufs_inode_info), 0,
798 SLAB_HWCACHE_ALIGN, spufs_init_once);
800 if (!spufs_inode_cache)
802 ret = spu_sched_init();
805 ret = register_filesystem(&spufs_type);
808 ret = register_spu_syscalls(&spufs_calls);
812 spufs_init_isolated_loader();
817 unregister_filesystem(&spufs_type);
821 kmem_cache_destroy(spufs_inode_cache);
825 module_init(spufs_init);
827 static void __exit spufs_exit(void)
830 spufs_exit_isolated_loader();
831 unregister_spu_syscalls(&spufs_calls);
832 unregister_filesystem(&spufs_type);
833 kmem_cache_destroy(spufs_inode_cache);
835 module_exit(spufs_exit);
837 MODULE_LICENSE("GPL");
838 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");