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 setattr_copy(inode, attr);
114 mark_inode_dirty(inode);
120 spufs_new_file(struct super_block *sb, struct dentry *dentry,
121 const struct file_operations *fops, int mode,
122 size_t size, struct spu_context *ctx)
124 static const struct inode_operations spufs_file_iops = {
125 .setattr = spufs_setattr,
131 inode = spufs_new_inode(sb, S_IFREG | mode);
136 inode->i_op = &spufs_file_iops;
138 inode->i_size = size;
139 inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
140 d_add(dentry, inode);
146 spufs_evict_inode(struct inode *inode)
148 struct spufs_inode_info *ei = SPUFS_I(inode);
149 end_writeback(inode);
151 put_spu_context(ei->i_ctx);
153 put_spu_gang(ei->i_gang);
156 static void spufs_prune_dir(struct dentry *dir)
158 struct dentry *dentry, *tmp;
160 mutex_lock(&dir->d_inode->i_mutex);
161 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
162 spin_lock(&dcache_lock);
163 spin_lock(&dentry->d_lock);
164 if (!(d_unhashed(dentry)) && dentry->d_inode) {
167 spin_unlock(&dentry->d_lock);
168 simple_unlink(dir->d_inode, dentry);
169 spin_unlock(&dcache_lock);
172 spin_unlock(&dentry->d_lock);
173 spin_unlock(&dcache_lock);
176 shrink_dcache_parent(dir);
177 mutex_unlock(&dir->d_inode->i_mutex);
180 /* Caller must hold parent->i_mutex */
181 static int spufs_rmdir(struct inode *parent, struct dentry *dir)
183 /* remove all entries */
184 spufs_prune_dir(dir);
187 return simple_rmdir(parent, dir);
190 static int spufs_fill_dir(struct dentry *dir,
191 const struct spufs_tree_descr *files, int mode,
192 struct spu_context *ctx)
194 struct dentry *dentry, *tmp;
197 while (files->name && files->name[0]) {
199 dentry = d_alloc_name(dir, files->name);
202 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
203 files->mode & mode, files->size, ctx);
211 * remove all children from dir. dir->inode is not set so don't
212 * just simply use spufs_prune_dir() and panic afterwards :)
213 * dput() looks like it will do the right thing:
214 * - dec parent's ref counter
215 * - remove child from parent's child list
216 * - free child's inode if possible
219 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
223 shrink_dcache_parent(dir);
227 static int spufs_dir_close(struct inode *inode, struct file *file)
229 struct spu_context *ctx;
230 struct inode *parent;
234 dir = file->f_path.dentry;
235 parent = dir->d_parent->d_inode;
236 ctx = SPUFS_I(dir->d_inode)->i_ctx;
238 mutex_lock_nested(&parent->i_mutex, I_MUTEX_PARENT);
239 ret = spufs_rmdir(parent, dir);
240 mutex_unlock(&parent->i_mutex);
243 /* We have to give up the mm_struct */
246 return dcache_dir_close(inode, file);
249 const struct file_operations spufs_context_fops = {
250 .open = dcache_dir_open,
251 .release = spufs_dir_close,
252 .llseek = dcache_dir_lseek,
253 .read = generic_read_dir,
254 .readdir = dcache_readdir,
257 EXPORT_SYMBOL_GPL(spufs_context_fops);
260 spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
265 struct spu_context *ctx;
268 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
272 if (dir->i_mode & S_ISGID) {
273 inode->i_gid = dir->i_gid;
274 inode->i_mode &= S_ISGID;
276 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
277 SPUFS_I(inode)->i_ctx = ctx;
282 inode->i_op = &simple_dir_inode_operations;
283 inode->i_fop = &simple_dir_operations;
284 if (flags & SPU_CREATE_NOSCHED)
285 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
288 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
293 if (spufs_get_sb_info(dir->i_sb)->debug)
294 ret = spufs_fill_dir(dentry, spufs_dir_debug_contents,
300 d_instantiate(dentry, inode);
303 inc_nlink(dentry->d_inode);
308 put_spu_context(ctx);
315 static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
320 ret = get_unused_fd();
327 filp = dentry_open(dentry, mnt, O_RDONLY, current_cred());
334 filp->f_op = &spufs_context_fops;
335 fd_install(ret, filp);
340 static struct spu_context *
341 spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
344 struct spu_context *tmp, *neighbor, *err;
348 aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
349 struct spu, cbe_list))->aff_list);
352 return ERR_PTR(-EINVAL);
354 if (flags & SPU_CREATE_GANG)
355 return ERR_PTR(-EINVAL);
357 if (flags & SPU_CREATE_AFFINITY_MEM &&
359 gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
360 return ERR_PTR(-EEXIST);
362 if (gang->aff_flags & AFF_MERGED)
363 return ERR_PTR(-EBUSY);
366 if (flags & SPU_CREATE_AFFINITY_SPU) {
367 if (!filp || filp->f_op != &spufs_context_fops)
368 return ERR_PTR(-EINVAL);
370 neighbor = get_spu_context(
371 SPUFS_I(filp->f_dentry->d_inode)->i_ctx);
373 if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
374 !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
375 !list_entry(neighbor->aff_list.next, struct spu_context,
376 aff_list)->aff_head) {
377 err = ERR_PTR(-EEXIST);
378 goto out_put_neighbor;
381 if (gang != neighbor->gang) {
382 err = ERR_PTR(-EINVAL);
383 goto out_put_neighbor;
387 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
389 if (list_empty(&neighbor->aff_list))
392 for (node = 0; node < MAX_NUMNODES; node++) {
393 if ((cbe_spu_info[node].n_spus - atomic_read(
394 &cbe_spu_info[node].reserved_spus)) >= count)
398 if (node == MAX_NUMNODES) {
399 err = ERR_PTR(-EEXIST);
400 goto out_put_neighbor;
407 put_spu_context(neighbor);
412 spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
413 struct spu_context *neighbor)
415 if (flags & SPU_CREATE_AFFINITY_MEM)
416 ctx->gang->aff_ref_ctx = ctx;
418 if (flags & SPU_CREATE_AFFINITY_SPU) {
419 if (list_empty(&neighbor->aff_list)) {
420 list_add_tail(&neighbor->aff_list,
421 &ctx->gang->aff_list_head);
422 neighbor->aff_head = 1;
425 if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
426 || list_entry(neighbor->aff_list.next, struct spu_context,
427 aff_list)->aff_head) {
428 list_add(&ctx->aff_list, &neighbor->aff_list);
430 list_add_tail(&ctx->aff_list, &neighbor->aff_list);
431 if (neighbor->aff_head) {
432 neighbor->aff_head = 0;
437 if (!ctx->gang->aff_ref_ctx)
438 ctx->gang->aff_ref_ctx = ctx;
443 spufs_create_context(struct inode *inode, struct dentry *dentry,
444 struct vfsmount *mnt, int flags, int mode,
445 struct file *aff_filp)
449 struct spu_gang *gang;
450 struct spu_context *neighbor;
453 if ((flags & SPU_CREATE_NOSCHED) &&
454 !capable(CAP_SYS_NICE))
458 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
459 == SPU_CREATE_ISOLATE)
463 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
468 affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
470 gang = SPUFS_I(inode)->i_gang;
474 mutex_lock(&gang->aff_mutex);
475 neighbor = spufs_assert_affinity(flags, gang, aff_filp);
476 if (IS_ERR(neighbor)) {
477 ret = PTR_ERR(neighbor);
482 ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
487 spufs_set_affinity(flags, SPUFS_I(dentry->d_inode)->i_ctx,
490 put_spu_context(neighbor);
494 * get references for dget and mntget, will be released
495 * in error path of *_open().
497 ret = spufs_context_open(dget(dentry), mntget(mnt));
499 WARN_ON(spufs_rmdir(inode, dentry));
501 mutex_unlock(&gang->aff_mutex);
502 mutex_unlock(&inode->i_mutex);
503 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
509 mutex_unlock(&gang->aff_mutex);
511 mutex_unlock(&inode->i_mutex);
518 spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
522 struct spu_gang *gang;
525 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
530 if (dir->i_mode & S_ISGID) {
531 inode->i_gid = dir->i_gid;
532 inode->i_mode &= S_ISGID;
534 gang = alloc_spu_gang();
535 SPUFS_I(inode)->i_ctx = NULL;
536 SPUFS_I(inode)->i_gang = gang;
540 inode->i_op = &simple_dir_inode_operations;
541 inode->i_fop = &simple_dir_operations;
543 d_instantiate(dentry, inode);
545 inc_nlink(dentry->d_inode);
554 static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
559 ret = get_unused_fd();
566 filp = dentry_open(dentry, mnt, O_RDONLY, current_cred());
573 filp->f_op = &simple_dir_operations;
574 fd_install(ret, filp);
579 static int spufs_create_gang(struct inode *inode,
580 struct dentry *dentry,
581 struct vfsmount *mnt, int mode)
585 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
590 * get references for dget and mntget, will be released
591 * in error path of *_open().
593 ret = spufs_gang_open(dget(dentry), mntget(mnt));
595 int err = simple_rmdir(inode, dentry);
600 mutex_unlock(&inode->i_mutex);
606 static struct file_system_type spufs_type;
608 long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode,
611 struct dentry *dentry;
615 /* check if we are on spufs */
616 if (nd->path.dentry->d_sb->s_type != &spufs_type)
619 /* don't accept undefined flags */
620 if (flags & (~SPU_CREATE_FLAG_ALL))
623 /* only threads can be underneath a gang */
624 if (nd->path.dentry != nd->path.dentry->d_sb->s_root) {
625 if ((flags & SPU_CREATE_GANG) ||
626 !SPUFS_I(nd->path.dentry->d_inode)->i_gang)
630 dentry = lookup_create(nd, 1);
631 ret = PTR_ERR(dentry);
635 mode &= ~current_umask();
637 if (flags & SPU_CREATE_GANG)
638 ret = spufs_create_gang(nd->path.dentry->d_inode,
639 dentry, nd->path.mnt, mode);
641 ret = spufs_create_context(nd->path.dentry->d_inode,
642 dentry, nd->path.mnt, flags, mode,
645 fsnotify_mkdir(nd->path.dentry->d_inode, dentry);
649 mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
654 /* File system initialization */
656 Opt_uid, Opt_gid, Opt_mode, Opt_debug, Opt_err,
659 static const match_table_t spufs_tokens = {
660 { Opt_uid, "uid=%d" },
661 { Opt_gid, "gid=%d" },
662 { Opt_mode, "mode=%o" },
663 { Opt_debug, "debug" },
668 spufs_parse_options(struct super_block *sb, char *options, struct inode *root)
671 substring_t args[MAX_OPT_ARGS];
673 while ((p = strsep(&options, ",")) != NULL) {
679 token = match_token(p, spufs_tokens, args);
682 if (match_int(&args[0], &option))
684 root->i_uid = option;
687 if (match_int(&args[0], &option))
689 root->i_gid = option;
692 if (match_octal(&args[0], &option))
694 root->i_mode = option | S_IFDIR;
697 spufs_get_sb_info(sb)->debug = 1;
706 static void spufs_exit_isolated_loader(void)
708 free_pages((unsigned long) isolated_loader,
709 get_order(isolated_loader_size));
713 spufs_init_isolated_loader(void)
715 struct device_node *dn;
719 dn = of_find_node_by_path("/spu-isolation");
723 loader = of_get_property(dn, "loader", &size);
727 /* the loader must be align on a 16 byte boundary */
728 isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
729 if (!isolated_loader)
732 isolated_loader_size = size;
733 memcpy(isolated_loader, loader, size);
734 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
738 spufs_create_root(struct super_block *sb, void *data)
744 if (!spu_management_ops)
748 inode = spufs_new_inode(sb, S_IFDIR | 0775);
752 inode->i_op = &simple_dir_inode_operations;
753 inode->i_fop = &simple_dir_operations;
754 SPUFS_I(inode)->i_ctx = NULL;
758 if (!spufs_parse_options(sb, data, inode))
762 sb->s_root = d_alloc_root(inode);
774 spufs_fill_super(struct super_block *sb, void *data, int silent)
776 struct spufs_sb_info *info;
777 static const struct super_operations s_ops = {
778 .alloc_inode = spufs_alloc_inode,
779 .destroy_inode = spufs_destroy_inode,
780 .statfs = simple_statfs,
781 .evict_inode = spufs_evict_inode,
782 .show_options = generic_show_options,
785 save_mount_options(sb, data);
787 info = kzalloc(sizeof(*info), GFP_KERNEL);
791 sb->s_maxbytes = MAX_LFS_FILESIZE;
792 sb->s_blocksize = PAGE_CACHE_SIZE;
793 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
794 sb->s_magic = SPUFS_MAGIC;
796 sb->s_fs_info = info;
798 return spufs_create_root(sb, data);
801 static struct dentry *
802 spufs_mount(struct file_system_type *fstype, int flags,
803 const char *name, void *data)
805 return mount_single(fstype, flags, data, spufs_fill_super);
808 static struct file_system_type spufs_type = {
809 .owner = THIS_MODULE,
811 .mount = spufs_mount,
812 .kill_sb = kill_litter_super,
815 static int __init spufs_init(void)
820 if (!spu_management_ops)
824 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
825 sizeof(struct spufs_inode_info), 0,
826 SLAB_HWCACHE_ALIGN, spufs_init_once);
828 if (!spufs_inode_cache)
830 ret = spu_sched_init();
833 ret = register_filesystem(&spufs_type);
836 ret = register_spu_syscalls(&spufs_calls);
840 spufs_init_isolated_loader();
845 unregister_filesystem(&spufs_type);
849 kmem_cache_destroy(spufs_inode_cache);
853 module_init(spufs_init);
855 static void __exit spufs_exit(void)
858 spufs_exit_isolated_loader();
859 unregister_spu_syscalls(&spufs_calls);
860 unregister_filesystem(&spufs_type);
861 kmem_cache_destroy(spufs_inode_cache);
863 module_exit(spufs_exit);
865 MODULE_LICENSE("GPL");
866 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");