4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
6 * Author: Arnd Bergmann <arndb@de.ibm.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/file.h>
25 #include <linux/backing-dev.h>
26 #include <linux/init.h>
27 #include <linux/ioctl.h>
28 #include <linux/module.h>
29 #include <linux/mount.h>
30 #include <linux/namei.h>
31 #include <linux/pagemap.h>
32 #include <linux/poll.h>
33 #include <linux/slab.h>
34 #include <linux/parser.h>
37 #include <asm/semaphore.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);
175 return simple_rmdir(parent, dir);
178 static int spufs_fill_dir(struct dentry *dir, struct tree_descr *files,
179 int mode, struct spu_context *ctx)
181 struct dentry *dentry, *tmp;
184 while (files->name && files->name[0]) {
186 dentry = d_alloc_name(dir, files->name);
189 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
190 files->mode & mode, ctx);
198 * remove all children from dir. dir->inode is not set so don't
199 * just simply use spufs_prune_dir() and panic afterwards :)
200 * dput() looks like it will do the right thing:
201 * - dec parent's ref counter
202 * - remove child from parent's child list
203 * - free child's inode if possible
206 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
210 shrink_dcache_parent(dir);
214 static int spufs_dir_close(struct inode *inode, struct file *file)
216 struct spu_context *ctx;
217 struct inode *parent;
221 dir = file->f_path.dentry;
222 parent = dir->d_parent->d_inode;
223 ctx = SPUFS_I(dir->d_inode)->i_ctx;
225 mutex_lock(&parent->i_mutex);
226 ret = spufs_rmdir(parent, dir);
227 mutex_unlock(&parent->i_mutex);
230 /* We have to give up the mm_struct */
233 return dcache_dir_close(inode, file);
236 const struct file_operations spufs_context_fops = {
237 .open = dcache_dir_open,
238 .release = spufs_dir_close,
239 .llseek = dcache_dir_lseek,
240 .read = generic_read_dir,
241 .readdir = dcache_readdir,
242 .fsync = simple_sync_file,
244 EXPORT_SYMBOL_GPL(spufs_context_fops);
247 spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
252 struct spu_context *ctx;
255 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
259 if (dir->i_mode & S_ISGID) {
260 inode->i_gid = dir->i_gid;
261 inode->i_mode &= S_ISGID;
263 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
264 SPUFS_I(inode)->i_ctx = ctx;
269 inode->i_op = &simple_dir_inode_operations;
270 inode->i_fop = &simple_dir_operations;
271 if (flags & SPU_CREATE_NOSCHED)
272 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
275 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
280 d_instantiate(dentry, inode);
283 dentry->d_inode->i_nlink++;
288 put_spu_context(ctx);
295 static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
300 ret = get_unused_fd();
307 filp = dentry_open(dentry, mnt, O_RDONLY);
314 filp->f_op = &spufs_context_fops;
315 fd_install(ret, filp);
320 static struct spu_context *
321 spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
324 struct spu_context *tmp, *neighbor;
328 aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
329 struct spu, cbe_list))->aff_list);
332 return ERR_PTR(-EINVAL);
334 if (flags & SPU_CREATE_GANG)
335 return ERR_PTR(-EINVAL);
337 if (flags & SPU_CREATE_AFFINITY_MEM &&
339 gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
340 return ERR_PTR(-EEXIST);
342 if (gang->aff_flags & AFF_MERGED)
343 return ERR_PTR(-EBUSY);
346 if (flags & SPU_CREATE_AFFINITY_SPU) {
347 if (!filp || filp->f_op != &spufs_context_fops)
348 return ERR_PTR(-EINVAL);
350 neighbor = get_spu_context(
351 SPUFS_I(filp->f_dentry->d_inode)->i_ctx);
353 if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
354 !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
355 !list_entry(neighbor->aff_list.next, struct spu_context,
357 return ERR_PTR(-EEXIST);
359 if (gang != neighbor->gang)
360 return ERR_PTR(-EINVAL);
363 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
365 if (list_empty(&neighbor->aff_list))
368 for (node = 0; node < MAX_NUMNODES; node++) {
369 if ((cbe_spu_info[node].n_spus - atomic_read(
370 &cbe_spu_info[node].reserved_spus)) >= count)
374 if (node == MAX_NUMNODES)
375 return ERR_PTR(-EEXIST);
382 spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
383 struct spu_context *neighbor)
385 if (flags & SPU_CREATE_AFFINITY_MEM)
386 ctx->gang->aff_ref_ctx = ctx;
388 if (flags & SPU_CREATE_AFFINITY_SPU) {
389 if (list_empty(&neighbor->aff_list)) {
390 list_add_tail(&neighbor->aff_list,
391 &ctx->gang->aff_list_head);
392 neighbor->aff_head = 1;
395 if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
396 || list_entry(neighbor->aff_list.next, struct spu_context,
397 aff_list)->aff_head) {
398 list_add(&ctx->aff_list, &neighbor->aff_list);
400 list_add_tail(&ctx->aff_list, &neighbor->aff_list);
401 if (neighbor->aff_head) {
402 neighbor->aff_head = 0;
407 if (!ctx->gang->aff_ref_ctx)
408 ctx->gang->aff_ref_ctx = ctx;
413 spufs_create_context(struct inode *inode, struct dentry *dentry,
414 struct vfsmount *mnt, int flags, int mode,
415 struct file *aff_filp)
419 struct spu_gang *gang;
420 struct spu_context *neighbor;
423 if ((flags & SPU_CREATE_NOSCHED) &&
424 !capable(CAP_SYS_NICE))
428 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
429 == SPU_CREATE_ISOLATE)
433 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
438 affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
440 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 & S_IRWXUGO);
457 spufs_set_affinity(flags, SPUFS_I(dentry->d_inode)->i_ctx,
461 * get references for dget and mntget, will be released
462 * in error path of *_open().
464 ret = spufs_context_open(dget(dentry), mntget(mnt));
466 WARN_ON(spufs_rmdir(inode, dentry));
467 mutex_unlock(&inode->i_mutex);
468 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
474 mutex_unlock(&gang->aff_mutex);
476 mutex_unlock(&inode->i_mutex);
483 spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
487 struct spu_gang *gang;
490 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
495 if (dir->i_mode & S_ISGID) {
496 inode->i_gid = dir->i_gid;
497 inode->i_mode &= S_ISGID;
499 gang = alloc_spu_gang();
500 SPUFS_I(inode)->i_ctx = NULL;
501 SPUFS_I(inode)->i_gang = gang;
505 inode->i_op = &simple_dir_inode_operations;
506 inode->i_fop = &simple_dir_operations;
508 d_instantiate(dentry, inode);
510 dentry->d_inode->i_nlink++;
519 static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
524 ret = get_unused_fd();
531 filp = dentry_open(dentry, mnt, O_RDONLY);
538 filp->f_op = &simple_dir_operations;
539 fd_install(ret, filp);
544 static int spufs_create_gang(struct inode *inode,
545 struct dentry *dentry,
546 struct vfsmount *mnt, int mode)
550 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
555 * get references for dget and mntget, will be released
556 * in error path of *_open().
558 ret = spufs_gang_open(dget(dentry), mntget(mnt));
560 int err = simple_rmdir(inode, dentry);
565 mutex_unlock(&inode->i_mutex);
571 static struct file_system_type spufs_type;
573 long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode,
576 struct dentry *dentry;
580 /* check if we are on spufs */
581 if (nd->dentry->d_sb->s_type != &spufs_type)
584 /* don't accept undefined flags */
585 if (flags & (~SPU_CREATE_FLAG_ALL))
588 /* only threads can be underneath a gang */
589 if (nd->dentry != nd->dentry->d_sb->s_root) {
590 if ((flags & SPU_CREATE_GANG) ||
591 !SPUFS_I(nd->dentry->d_inode)->i_gang)
595 dentry = lookup_create(nd, 1);
596 ret = PTR_ERR(dentry);
604 mode &= ~current->fs->umask;
606 if (flags & SPU_CREATE_GANG)
607 return spufs_create_gang(nd->dentry->d_inode,
608 dentry, nd->mnt, mode);
610 return spufs_create_context(nd->dentry->d_inode,
611 dentry, nd->mnt, flags, mode, filp);
616 mutex_unlock(&nd->dentry->d_inode->i_mutex);
621 /* File system initialization */
623 Opt_uid, Opt_gid, Opt_mode, Opt_err,
626 static match_table_t spufs_tokens = {
627 { Opt_uid, "uid=%d" },
628 { Opt_gid, "gid=%d" },
629 { Opt_mode, "mode=%o" },
634 spufs_parse_options(char *options, struct inode *root)
637 substring_t args[MAX_OPT_ARGS];
639 while ((p = strsep(&options, ",")) != NULL) {
645 token = match_token(p, spufs_tokens, args);
648 if (match_int(&args[0], &option))
650 root->i_uid = option;
653 if (match_int(&args[0], &option))
655 root->i_gid = option;
658 if (match_octal(&args[0], &option))
660 root->i_mode = option | S_IFDIR;
669 static void spufs_exit_isolated_loader(void)
671 free_pages((unsigned long) isolated_loader,
672 get_order(isolated_loader_size));
676 spufs_init_isolated_loader(void)
678 struct device_node *dn;
682 dn = of_find_node_by_path("/spu-isolation");
686 loader = of_get_property(dn, "loader", &size);
690 /* the loader must be align on a 16 byte boundary */
691 isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
692 if (!isolated_loader)
695 isolated_loader_size = size;
696 memcpy(isolated_loader, loader, size);
697 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
701 spufs_create_root(struct super_block *sb, void *data)
707 if (!spu_management_ops)
711 inode = spufs_new_inode(sb, S_IFDIR | 0775);
715 inode->i_op = &simple_dir_inode_operations;
716 inode->i_fop = &simple_dir_operations;
717 SPUFS_I(inode)->i_ctx = NULL;
720 if (!spufs_parse_options(data, inode))
724 sb->s_root = d_alloc_root(inode);
736 spufs_fill_super(struct super_block *sb, void *data, int silent)
738 static struct super_operations s_ops = {
739 .alloc_inode = spufs_alloc_inode,
740 .destroy_inode = spufs_destroy_inode,
741 .statfs = simple_statfs,
742 .delete_inode = spufs_delete_inode,
743 .drop_inode = generic_delete_inode,
746 sb->s_maxbytes = MAX_LFS_FILESIZE;
747 sb->s_blocksize = PAGE_CACHE_SIZE;
748 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
749 sb->s_magic = SPUFS_MAGIC;
752 return spufs_create_root(sb, data);
756 spufs_get_sb(struct file_system_type *fstype, int flags,
757 const char *name, void *data, struct vfsmount *mnt)
759 return get_sb_single(fstype, flags, data, spufs_fill_super, mnt);
762 static struct file_system_type spufs_type = {
763 .owner = THIS_MODULE,
765 .get_sb = spufs_get_sb,
766 .kill_sb = kill_litter_super,
769 static int __init spufs_init(void)
774 if (!spu_management_ops)
778 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
779 sizeof(struct spufs_inode_info), 0,
780 SLAB_HWCACHE_ALIGN, spufs_init_once);
782 if (!spufs_inode_cache)
784 ret = spu_sched_init();
787 ret = register_filesystem(&spufs_type);
790 ret = register_spu_syscalls(&spufs_calls);
794 spufs_init_isolated_loader();
799 unregister_filesystem(&spufs_type);
803 kmem_cache_destroy(spufs_inode_cache);
807 module_init(spufs_init);
809 static void __exit spufs_exit(void)
812 spufs_exit_isolated_loader();
813 unregister_spu_syscalls(&spufs_calls);
814 unregister_filesystem(&spufs_type);
815 kmem_cache_destroy(spufs_inode_cache);
817 module_exit(spufs_exit);
819 MODULE_LICENSE("GPL");
820 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");