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;
75 spufs_destroy_inode(struct inode *inode)
77 kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
81 spufs_init_once(void *p)
83 struct spufs_inode_info *ei = p;
85 inode_init_once(&ei->vfs_inode);
89 spufs_new_inode(struct super_block *sb, int mode)
93 inode = new_inode(sb);
98 inode->i_uid = current_fsuid();
99 inode->i_gid = current_fsgid();
100 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
106 spufs_setattr(struct dentry *dentry, struct iattr *attr)
108 struct inode *inode = dentry->d_inode;
110 if ((attr->ia_valid & ATTR_SIZE) &&
111 (attr->ia_size != inode->i_size))
113 return inode_setattr(inode, attr);
118 spufs_new_file(struct super_block *sb, struct dentry *dentry,
119 const struct file_operations *fops, int mode,
120 size_t size, struct spu_context *ctx)
122 static struct inode_operations spufs_file_iops = {
123 .setattr = spufs_setattr,
129 inode = spufs_new_inode(sb, S_IFREG | mode);
134 inode->i_op = &spufs_file_iops;
136 inode->i_size = size;
137 inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
138 d_add(dentry, inode);
144 spufs_delete_inode(struct inode *inode)
146 struct spufs_inode_info *ei = SPUFS_I(inode);
149 put_spu_context(ei->i_ctx);
151 put_spu_gang(ei->i_gang);
155 static void spufs_prune_dir(struct dentry *dir)
157 struct dentry *dentry, *tmp;
159 mutex_lock(&dir->d_inode->i_mutex);
160 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
161 spin_lock(&dcache_lock);
162 spin_lock(&dentry->d_lock);
163 if (!(d_unhashed(dentry)) && dentry->d_inode) {
166 spin_unlock(&dentry->d_lock);
167 simple_unlink(dir->d_inode, dentry);
168 spin_unlock(&dcache_lock);
171 spin_unlock(&dentry->d_lock);
172 spin_unlock(&dcache_lock);
175 shrink_dcache_parent(dir);
176 mutex_unlock(&dir->d_inode->i_mutex);
179 /* Caller must hold parent->i_mutex */
180 static int spufs_rmdir(struct inode *parent, struct dentry *dir)
182 /* remove all entries */
183 spufs_prune_dir(dir);
186 return simple_rmdir(parent, dir);
189 static int spufs_fill_dir(struct dentry *dir, struct spufs_tree_descr *files,
190 int mode, struct spu_context *ctx)
192 struct dentry *dentry, *tmp;
195 while (files->name && files->name[0]) {
197 dentry = d_alloc_name(dir, files->name);
200 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
201 files->mode & mode, files->size, ctx);
209 * remove all children from dir. dir->inode is not set so don't
210 * just simply use spufs_prune_dir() and panic afterwards :)
211 * dput() looks like it will do the right thing:
212 * - dec parent's ref counter
213 * - remove child from parent's child list
214 * - free child's inode if possible
217 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
221 shrink_dcache_parent(dir);
225 static int spufs_dir_close(struct inode *inode, struct file *file)
227 struct spu_context *ctx;
228 struct inode *parent;
232 dir = file->f_path.dentry;
233 parent = dir->d_parent->d_inode;
234 ctx = SPUFS_I(dir->d_inode)->i_ctx;
236 mutex_lock_nested(&parent->i_mutex, I_MUTEX_PARENT);
237 ret = spufs_rmdir(parent, dir);
238 mutex_unlock(&parent->i_mutex);
241 /* We have to give up the mm_struct */
244 return dcache_dir_close(inode, file);
247 const struct file_operations spufs_context_fops = {
248 .open = dcache_dir_open,
249 .release = spufs_dir_close,
250 .llseek = dcache_dir_lseek,
251 .read = generic_read_dir,
252 .readdir = dcache_readdir,
253 .fsync = simple_sync_file,
255 EXPORT_SYMBOL_GPL(spufs_context_fops);
258 spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
263 struct spu_context *ctx;
266 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
270 if (dir->i_mode & S_ISGID) {
271 inode->i_gid = dir->i_gid;
272 inode->i_mode &= S_ISGID;
274 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
275 SPUFS_I(inode)->i_ctx = ctx;
280 inode->i_op = &simple_dir_inode_operations;
281 inode->i_fop = &simple_dir_operations;
282 if (flags & SPU_CREATE_NOSCHED)
283 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
286 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
291 if (spufs_get_sb_info(dir->i_sb)->debug)
292 ret = spufs_fill_dir(dentry, spufs_dir_debug_contents,
298 d_instantiate(dentry, inode);
301 inc_nlink(dentry->d_inode);
306 put_spu_context(ctx);
313 static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
318 ret = get_unused_fd();
325 filp = dentry_open(dentry, mnt, O_RDONLY, current_cred());
332 filp->f_op = &spufs_context_fops;
333 fd_install(ret, filp);
338 static struct spu_context *
339 spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
342 struct spu_context *tmp, *neighbor, *err;
346 aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
347 struct spu, cbe_list))->aff_list);
350 return ERR_PTR(-EINVAL);
352 if (flags & SPU_CREATE_GANG)
353 return ERR_PTR(-EINVAL);
355 if (flags & SPU_CREATE_AFFINITY_MEM &&
357 gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
358 return ERR_PTR(-EEXIST);
360 if (gang->aff_flags & AFF_MERGED)
361 return ERR_PTR(-EBUSY);
364 if (flags & SPU_CREATE_AFFINITY_SPU) {
365 if (!filp || filp->f_op != &spufs_context_fops)
366 return ERR_PTR(-EINVAL);
368 neighbor = get_spu_context(
369 SPUFS_I(filp->f_dentry->d_inode)->i_ctx);
371 if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
372 !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
373 !list_entry(neighbor->aff_list.next, struct spu_context,
374 aff_list)->aff_head) {
375 err = ERR_PTR(-EEXIST);
376 goto out_put_neighbor;
379 if (gang != neighbor->gang) {
380 err = ERR_PTR(-EINVAL);
381 goto out_put_neighbor;
385 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
387 if (list_empty(&neighbor->aff_list))
390 for (node = 0; node < MAX_NUMNODES; node++) {
391 if ((cbe_spu_info[node].n_spus - atomic_read(
392 &cbe_spu_info[node].reserved_spus)) >= count)
396 if (node == MAX_NUMNODES) {
397 err = ERR_PTR(-EEXIST);
398 goto out_put_neighbor;
405 put_spu_context(neighbor);
410 spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
411 struct spu_context *neighbor)
413 if (flags & SPU_CREATE_AFFINITY_MEM)
414 ctx->gang->aff_ref_ctx = ctx;
416 if (flags & SPU_CREATE_AFFINITY_SPU) {
417 if (list_empty(&neighbor->aff_list)) {
418 list_add_tail(&neighbor->aff_list,
419 &ctx->gang->aff_list_head);
420 neighbor->aff_head = 1;
423 if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
424 || list_entry(neighbor->aff_list.next, struct spu_context,
425 aff_list)->aff_head) {
426 list_add(&ctx->aff_list, &neighbor->aff_list);
428 list_add_tail(&ctx->aff_list, &neighbor->aff_list);
429 if (neighbor->aff_head) {
430 neighbor->aff_head = 0;
435 if (!ctx->gang->aff_ref_ctx)
436 ctx->gang->aff_ref_ctx = ctx;
441 spufs_create_context(struct inode *inode, struct dentry *dentry,
442 struct vfsmount *mnt, int flags, int mode,
443 struct file *aff_filp)
447 struct spu_gang *gang;
448 struct spu_context *neighbor;
451 if ((flags & SPU_CREATE_NOSCHED) &&
452 !capable(CAP_SYS_NICE))
456 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
457 == SPU_CREATE_ISOLATE)
461 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
466 affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
468 gang = SPUFS_I(inode)->i_gang;
472 mutex_lock(&gang->aff_mutex);
473 neighbor = spufs_assert_affinity(flags, gang, aff_filp);
474 if (IS_ERR(neighbor)) {
475 ret = PTR_ERR(neighbor);
480 ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
485 spufs_set_affinity(flags, SPUFS_I(dentry->d_inode)->i_ctx,
488 put_spu_context(neighbor);
492 * get references for dget and mntget, will be released
493 * in error path of *_open().
495 ret = spufs_context_open(dget(dentry), mntget(mnt));
497 WARN_ON(spufs_rmdir(inode, dentry));
499 mutex_unlock(&gang->aff_mutex);
500 mutex_unlock(&inode->i_mutex);
501 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
507 mutex_unlock(&gang->aff_mutex);
509 mutex_unlock(&inode->i_mutex);
516 spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
520 struct spu_gang *gang;
523 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
528 if (dir->i_mode & S_ISGID) {
529 inode->i_gid = dir->i_gid;
530 inode->i_mode &= S_ISGID;
532 gang = alloc_spu_gang();
533 SPUFS_I(inode)->i_ctx = NULL;
534 SPUFS_I(inode)->i_gang = gang;
538 inode->i_op = &simple_dir_inode_operations;
539 inode->i_fop = &simple_dir_operations;
541 d_instantiate(dentry, inode);
543 inc_nlink(dentry->d_inode);
552 static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
557 ret = get_unused_fd();
564 filp = dentry_open(dentry, mnt, O_RDONLY, current_cred());
571 filp->f_op = &simple_dir_operations;
572 fd_install(ret, filp);
577 static int spufs_create_gang(struct inode *inode,
578 struct dentry *dentry,
579 struct vfsmount *mnt, int mode)
583 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
588 * get references for dget and mntget, will be released
589 * in error path of *_open().
591 ret = spufs_gang_open(dget(dentry), mntget(mnt));
593 int err = simple_rmdir(inode, dentry);
598 mutex_unlock(&inode->i_mutex);
604 static struct file_system_type spufs_type;
606 long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode,
609 struct dentry *dentry;
613 /* check if we are on spufs */
614 if (nd->path.dentry->d_sb->s_type != &spufs_type)
617 /* don't accept undefined flags */
618 if (flags & (~SPU_CREATE_FLAG_ALL))
621 /* only threads can be underneath a gang */
622 if (nd->path.dentry != nd->path.dentry->d_sb->s_root) {
623 if ((flags & SPU_CREATE_GANG) ||
624 !SPUFS_I(nd->path.dentry->d_inode)->i_gang)
628 dentry = lookup_create(nd, 1);
629 ret = PTR_ERR(dentry);
637 mode &= ~current->fs->umask;
639 if (flags & SPU_CREATE_GANG)
640 ret = spufs_create_gang(nd->path.dentry->d_inode,
641 dentry, nd->path.mnt, mode);
643 ret = spufs_create_context(nd->path.dentry->d_inode,
644 dentry, nd->path.mnt, flags, mode,
647 fsnotify_mkdir(nd->path.dentry->d_inode, dentry);
653 mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
658 /* File system initialization */
660 Opt_uid, Opt_gid, Opt_mode, Opt_debug, Opt_err,
663 static const match_table_t spufs_tokens = {
664 { Opt_uid, "uid=%d" },
665 { Opt_gid, "gid=%d" },
666 { Opt_mode, "mode=%o" },
667 { Opt_debug, "debug" },
672 spufs_parse_options(struct super_block *sb, char *options, struct inode *root)
675 substring_t args[MAX_OPT_ARGS];
677 while ((p = strsep(&options, ",")) != NULL) {
683 token = match_token(p, spufs_tokens, args);
686 if (match_int(&args[0], &option))
688 root->i_uid = option;
691 if (match_int(&args[0], &option))
693 root->i_gid = option;
696 if (match_octal(&args[0], &option))
698 root->i_mode = option | S_IFDIR;
701 spufs_get_sb_info(sb)->debug = 1;
710 static void spufs_exit_isolated_loader(void)
712 free_pages((unsigned long) isolated_loader,
713 get_order(isolated_loader_size));
717 spufs_init_isolated_loader(void)
719 struct device_node *dn;
723 dn = of_find_node_by_path("/spu-isolation");
727 loader = of_get_property(dn, "loader", &size);
731 /* the loader must be align on a 16 byte boundary */
732 isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
733 if (!isolated_loader)
736 isolated_loader_size = size;
737 memcpy(isolated_loader, loader, size);
738 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
742 spufs_create_root(struct super_block *sb, void *data)
748 if (!spu_management_ops)
752 inode = spufs_new_inode(sb, S_IFDIR | 0775);
756 inode->i_op = &simple_dir_inode_operations;
757 inode->i_fop = &simple_dir_operations;
758 SPUFS_I(inode)->i_ctx = NULL;
762 if (!spufs_parse_options(sb, data, inode))
766 sb->s_root = d_alloc_root(inode);
778 spufs_fill_super(struct super_block *sb, void *data, int silent)
780 struct spufs_sb_info *info;
781 static struct super_operations s_ops = {
782 .alloc_inode = spufs_alloc_inode,
783 .destroy_inode = spufs_destroy_inode,
784 .statfs = simple_statfs,
785 .delete_inode = spufs_delete_inode,
786 .drop_inode = generic_delete_inode,
787 .show_options = generic_show_options,
790 save_mount_options(sb, data);
792 info = kzalloc(sizeof(*info), GFP_KERNEL);
796 sb->s_maxbytes = MAX_LFS_FILESIZE;
797 sb->s_blocksize = PAGE_CACHE_SIZE;
798 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
799 sb->s_magic = SPUFS_MAGIC;
801 sb->s_fs_info = info;
803 return spufs_create_root(sb, data);
807 spufs_get_sb(struct file_system_type *fstype, int flags,
808 const char *name, void *data, struct vfsmount *mnt)
810 return get_sb_single(fstype, flags, data, spufs_fill_super, mnt);
813 static struct file_system_type spufs_type = {
814 .owner = THIS_MODULE,
816 .get_sb = spufs_get_sb,
817 .kill_sb = kill_litter_super,
820 static int __init spufs_init(void)
825 if (!spu_management_ops)
829 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
830 sizeof(struct spufs_inode_info), 0,
831 SLAB_HWCACHE_ALIGN, spufs_init_once);
833 if (!spufs_inode_cache)
835 ret = spu_sched_init();
838 ret = register_filesystem(&spufs_type);
841 ret = register_spu_syscalls(&spufs_calls);
845 spufs_init_isolated_loader();
850 unregister_filesystem(&spufs_type);
854 kmem_cache_destroy(spufs_inode_cache);
858 module_init(spufs_init);
860 static void __exit spufs_exit(void)
863 spufs_exit_isolated_loader();
864 unregister_spu_syscalls(&spufs_calls);
865 unregister_filesystem(&spufs_type);
866 kmem_cache_destroy(spufs_inode_cache);
868 module_exit(spufs_exit);
870 MODULE_LICENSE("GPL");
871 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");