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>
38 #include <asm/semaphore.h>
40 #include <asm/spu_priv1.h>
41 #include <asm/uaccess.h>
45 static struct kmem_cache *spufs_inode_cache;
46 char *isolated_loader;
47 static int isolated_loader_size;
50 spufs_alloc_inode(struct super_block *sb)
52 struct spufs_inode_info *ei;
54 ei = kmem_cache_alloc(spufs_inode_cache, GFP_KERNEL);
62 return &ei->vfs_inode;
66 spufs_destroy_inode(struct inode *inode)
68 kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
72 spufs_init_once(struct kmem_cache *cachep, void *p)
74 struct spufs_inode_info *ei = p;
76 inode_init_once(&ei->vfs_inode);
80 spufs_new_inode(struct super_block *sb, int mode)
84 inode = new_inode(sb);
89 inode->i_uid = current->fsuid;
90 inode->i_gid = current->fsgid;
92 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
98 spufs_setattr(struct dentry *dentry, struct iattr *attr)
100 struct inode *inode = dentry->d_inode;
102 if ((attr->ia_valid & ATTR_SIZE) &&
103 (attr->ia_size != inode->i_size))
105 return inode_setattr(inode, attr);
110 spufs_new_file(struct super_block *sb, struct dentry *dentry,
111 const struct file_operations *fops, int mode,
112 struct spu_context *ctx)
114 static struct inode_operations spufs_file_iops = {
115 .setattr = spufs_setattr,
121 inode = spufs_new_inode(sb, S_IFREG | mode);
126 inode->i_op = &spufs_file_iops;
128 inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
129 d_add(dentry, inode);
135 spufs_delete_inode(struct inode *inode)
137 struct spufs_inode_info *ei = SPUFS_I(inode);
140 put_spu_context(ei->i_ctx);
142 put_spu_gang(ei->i_gang);
146 static void spufs_prune_dir(struct dentry *dir)
148 struct dentry *dentry, *tmp;
150 mutex_lock(&dir->d_inode->i_mutex);
151 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
152 spin_lock(&dcache_lock);
153 spin_lock(&dentry->d_lock);
154 if (!(d_unhashed(dentry)) && dentry->d_inode) {
157 spin_unlock(&dentry->d_lock);
158 simple_unlink(dir->d_inode, dentry);
159 spin_unlock(&dcache_lock);
162 spin_unlock(&dentry->d_lock);
163 spin_unlock(&dcache_lock);
166 shrink_dcache_parent(dir);
167 mutex_unlock(&dir->d_inode->i_mutex);
170 /* Caller must hold parent->i_mutex */
171 static int spufs_rmdir(struct inode *parent, struct dentry *dir)
173 /* remove all entries */
174 spufs_prune_dir(dir);
177 return simple_rmdir(parent, dir);
180 static int spufs_fill_dir(struct dentry *dir, struct tree_descr *files,
181 int mode, struct spu_context *ctx)
183 struct dentry *dentry, *tmp;
186 while (files->name && files->name[0]) {
188 dentry = d_alloc_name(dir, files->name);
191 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
192 files->mode & mode, ctx);
200 * remove all children from dir. dir->inode is not set so don't
201 * just simply use spufs_prune_dir() and panic afterwards :)
202 * dput() looks like it will do the right thing:
203 * - dec parent's ref counter
204 * - remove child from parent's child list
205 * - free child's inode if possible
208 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
212 shrink_dcache_parent(dir);
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(&parent->i_mutex);
228 ret = spufs_rmdir(parent, dir);
229 mutex_unlock(&parent->i_mutex);
232 /* We have to give up the mm_struct */
235 return dcache_dir_close(inode, file);
238 const struct file_operations spufs_context_fops = {
239 .open = dcache_dir_open,
240 .release = spufs_dir_close,
241 .llseek = dcache_dir_lseek,
242 .read = generic_read_dir,
243 .readdir = dcache_readdir,
244 .fsync = simple_sync_file,
246 EXPORT_SYMBOL_GPL(spufs_context_fops);
249 spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
254 struct spu_context *ctx;
257 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
261 if (dir->i_mode & S_ISGID) {
262 inode->i_gid = dir->i_gid;
263 inode->i_mode &= S_ISGID;
265 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
266 SPUFS_I(inode)->i_ctx = ctx;
271 inode->i_op = &simple_dir_inode_operations;
272 inode->i_fop = &simple_dir_operations;
273 if (flags & SPU_CREATE_NOSCHED)
274 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
277 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
282 d_instantiate(dentry, inode);
285 dentry->d_inode->i_nlink++;
290 put_spu_context(ctx);
297 static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
302 ret = get_unused_fd();
309 filp = dentry_open(dentry, mnt, O_RDONLY);
316 filp->f_op = &spufs_context_fops;
317 fd_install(ret, filp);
322 static struct spu_context *
323 spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
326 struct spu_context *tmp, *neighbor, *err;
330 aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
331 struct spu, cbe_list))->aff_list);
334 return ERR_PTR(-EINVAL);
336 if (flags & SPU_CREATE_GANG)
337 return ERR_PTR(-EINVAL);
339 if (flags & SPU_CREATE_AFFINITY_MEM &&
341 gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
342 return ERR_PTR(-EEXIST);
344 if (gang->aff_flags & AFF_MERGED)
345 return ERR_PTR(-EBUSY);
348 if (flags & SPU_CREATE_AFFINITY_SPU) {
349 if (!filp || filp->f_op != &spufs_context_fops)
350 return ERR_PTR(-EINVAL);
352 neighbor = get_spu_context(
353 SPUFS_I(filp->f_dentry->d_inode)->i_ctx);
355 if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
356 !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
357 !list_entry(neighbor->aff_list.next, struct spu_context,
358 aff_list)->aff_head) {
359 err = ERR_PTR(-EEXIST);
360 goto out_put_neighbor;
363 if (gang != neighbor->gang) {
364 err = ERR_PTR(-EINVAL);
365 goto out_put_neighbor;
369 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
371 if (list_empty(&neighbor->aff_list))
374 for (node = 0; node < MAX_NUMNODES; node++) {
375 if ((cbe_spu_info[node].n_spus - atomic_read(
376 &cbe_spu_info[node].reserved_spus)) >= count)
380 if (node == MAX_NUMNODES) {
381 err = ERR_PTR(-EEXIST);
382 goto out_put_neighbor;
389 put_spu_context(neighbor);
394 spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
395 struct spu_context *neighbor)
397 if (flags & SPU_CREATE_AFFINITY_MEM)
398 ctx->gang->aff_ref_ctx = ctx;
400 if (flags & SPU_CREATE_AFFINITY_SPU) {
401 if (list_empty(&neighbor->aff_list)) {
402 list_add_tail(&neighbor->aff_list,
403 &ctx->gang->aff_list_head);
404 neighbor->aff_head = 1;
407 if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
408 || list_entry(neighbor->aff_list.next, struct spu_context,
409 aff_list)->aff_head) {
410 list_add(&ctx->aff_list, &neighbor->aff_list);
412 list_add_tail(&ctx->aff_list, &neighbor->aff_list);
413 if (neighbor->aff_head) {
414 neighbor->aff_head = 0;
419 if (!ctx->gang->aff_ref_ctx)
420 ctx->gang->aff_ref_ctx = ctx;
425 spufs_create_context(struct inode *inode, struct dentry *dentry,
426 struct vfsmount *mnt, int flags, int mode,
427 struct file *aff_filp)
431 struct spu_gang *gang;
432 struct spu_context *neighbor;
435 if ((flags & SPU_CREATE_NOSCHED) &&
436 !capable(CAP_SYS_NICE))
440 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
441 == SPU_CREATE_ISOLATE)
445 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
450 affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
452 gang = SPUFS_I(inode)->i_gang;
456 mutex_lock(&gang->aff_mutex);
457 neighbor = spufs_assert_affinity(flags, gang, aff_filp);
458 if (IS_ERR(neighbor)) {
459 ret = PTR_ERR(neighbor);
464 ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
469 spufs_set_affinity(flags, SPUFS_I(dentry->d_inode)->i_ctx,
472 put_spu_context(neighbor);
476 * get references for dget and mntget, will be released
477 * in error path of *_open().
479 ret = spufs_context_open(dget(dentry), mntget(mnt));
481 WARN_ON(spufs_rmdir(inode, dentry));
482 mutex_unlock(&inode->i_mutex);
483 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
489 mutex_unlock(&gang->aff_mutex);
491 mutex_unlock(&inode->i_mutex);
498 spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
502 struct spu_gang *gang;
505 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
510 if (dir->i_mode & S_ISGID) {
511 inode->i_gid = dir->i_gid;
512 inode->i_mode &= S_ISGID;
514 gang = alloc_spu_gang();
515 SPUFS_I(inode)->i_ctx = NULL;
516 SPUFS_I(inode)->i_gang = gang;
520 inode->i_op = &simple_dir_inode_operations;
521 inode->i_fop = &simple_dir_operations;
523 d_instantiate(dentry, inode);
525 dentry->d_inode->i_nlink++;
534 static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
539 ret = get_unused_fd();
546 filp = dentry_open(dentry, mnt, O_RDONLY);
553 filp->f_op = &simple_dir_operations;
554 fd_install(ret, filp);
559 static int spufs_create_gang(struct inode *inode,
560 struct dentry *dentry,
561 struct vfsmount *mnt, int mode)
565 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
570 * get references for dget and mntget, will be released
571 * in error path of *_open().
573 ret = spufs_gang_open(dget(dentry), mntget(mnt));
575 int err = simple_rmdir(inode, dentry);
580 mutex_unlock(&inode->i_mutex);
586 static struct file_system_type spufs_type;
588 long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode,
591 struct dentry *dentry;
595 /* check if we are on spufs */
596 if (nd->path.dentry->d_sb->s_type != &spufs_type)
599 /* don't accept undefined flags */
600 if (flags & (~SPU_CREATE_FLAG_ALL))
603 /* only threads can be underneath a gang */
604 if (nd->path.dentry != nd->path.dentry->d_sb->s_root) {
605 if ((flags & SPU_CREATE_GANG) ||
606 !SPUFS_I(nd->path.dentry->d_inode)->i_gang)
610 dentry = lookup_create(nd, 1);
611 ret = PTR_ERR(dentry);
619 mode &= ~current->fs->umask;
621 if (flags & SPU_CREATE_GANG)
622 return spufs_create_gang(nd->path.dentry->d_inode,
623 dentry, nd->path.mnt, mode);
625 return spufs_create_context(nd->path.dentry->d_inode,
626 dentry, nd->path.mnt, flags, mode,
632 mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
637 /* File system initialization */
639 Opt_uid, Opt_gid, Opt_mode, Opt_err,
642 static match_table_t spufs_tokens = {
643 { Opt_uid, "uid=%d" },
644 { Opt_gid, "gid=%d" },
645 { Opt_mode, "mode=%o" },
650 spufs_parse_options(char *options, struct inode *root)
653 substring_t args[MAX_OPT_ARGS];
655 while ((p = strsep(&options, ",")) != NULL) {
661 token = match_token(p, spufs_tokens, args);
664 if (match_int(&args[0], &option))
666 root->i_uid = option;
669 if (match_int(&args[0], &option))
671 root->i_gid = option;
674 if (match_octal(&args[0], &option))
676 root->i_mode = option | S_IFDIR;
685 static void spufs_exit_isolated_loader(void)
687 free_pages((unsigned long) isolated_loader,
688 get_order(isolated_loader_size));
692 spufs_init_isolated_loader(void)
694 struct device_node *dn;
698 dn = of_find_node_by_path("/spu-isolation");
702 loader = of_get_property(dn, "loader", &size);
706 /* the loader must be align on a 16 byte boundary */
707 isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
708 if (!isolated_loader)
711 isolated_loader_size = size;
712 memcpy(isolated_loader, loader, size);
713 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
717 spufs_create_root(struct super_block *sb, void *data)
723 if (!spu_management_ops)
727 inode = spufs_new_inode(sb, S_IFDIR | 0775);
731 inode->i_op = &simple_dir_inode_operations;
732 inode->i_fop = &simple_dir_operations;
733 SPUFS_I(inode)->i_ctx = NULL;
736 if (!spufs_parse_options(data, inode))
740 sb->s_root = d_alloc_root(inode);
752 spufs_fill_super(struct super_block *sb, void *data, int silent)
754 static struct super_operations s_ops = {
755 .alloc_inode = spufs_alloc_inode,
756 .destroy_inode = spufs_destroy_inode,
757 .statfs = simple_statfs,
758 .delete_inode = spufs_delete_inode,
759 .drop_inode = generic_delete_inode,
760 .show_options = generic_show_options,
763 save_mount_options(sb, data);
765 sb->s_maxbytes = MAX_LFS_FILESIZE;
766 sb->s_blocksize = PAGE_CACHE_SIZE;
767 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
768 sb->s_magic = SPUFS_MAGIC;
771 return spufs_create_root(sb, data);
775 spufs_get_sb(struct file_system_type *fstype, int flags,
776 const char *name, void *data, struct vfsmount *mnt)
778 return get_sb_single(fstype, flags, data, spufs_fill_super, mnt);
781 static struct file_system_type spufs_type = {
782 .owner = THIS_MODULE,
784 .get_sb = spufs_get_sb,
785 .kill_sb = kill_litter_super,
788 static int __init spufs_init(void)
793 if (!spu_management_ops)
797 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
798 sizeof(struct spufs_inode_info), 0,
799 SLAB_HWCACHE_ALIGN, spufs_init_once);
801 if (!spufs_inode_cache)
803 ret = spu_sched_init();
806 ret = register_filesystem(&spufs_type);
809 ret = register_spu_syscalls(&spufs_calls);
813 spufs_init_isolated_loader();
818 unregister_filesystem(&spufs_type);
822 kmem_cache_destroy(spufs_inode_cache);
826 module_init(spufs_init);
828 static void __exit spufs_exit(void)
831 spufs_exit_isolated_loader();
832 unregister_spu_syscalls(&spufs_calls);
833 unregister_filesystem(&spufs_type);
834 kmem_cache_destroy(spufs_inode_cache);
836 module_exit(spufs_exit);
838 MODULE_LICENSE("GPL");
839 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");