xref: /linux/arch/powerpc/platforms/cell/spufs/inode.c (revision 1ba106818615faddb63ba782f85f3498b9eb61c6)
14ac91378SJan Blunck 
267207b96SArnd Bergmann /*
367207b96SArnd Bergmann  * SPU file system
467207b96SArnd Bergmann  *
567207b96SArnd Bergmann  * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
667207b96SArnd Bergmann  *
767207b96SArnd Bergmann  * Author: Arnd Bergmann <arndb@de.ibm.com>
867207b96SArnd Bergmann  *
967207b96SArnd Bergmann  * This program is free software; you can redistribute it and/or modify
1067207b96SArnd Bergmann  * it under the terms of the GNU General Public License as published by
1167207b96SArnd Bergmann  * the Free Software Foundation; either version 2, or (at your option)
1267207b96SArnd Bergmann  * any later version.
1367207b96SArnd Bergmann  *
1467207b96SArnd Bergmann  * This program is distributed in the hope that it will be useful,
1567207b96SArnd Bergmann  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1667207b96SArnd Bergmann  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1767207b96SArnd Bergmann  * GNU General Public License for more details.
1867207b96SArnd Bergmann  *
1967207b96SArnd Bergmann  * You should have received a copy of the GNU General Public License
2067207b96SArnd Bergmann  * along with this program; if not, write to the Free Software
2167207b96SArnd Bergmann  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
2267207b96SArnd Bergmann  */
2367207b96SArnd Bergmann 
2467207b96SArnd Bergmann #include <linux/file.h>
2567207b96SArnd Bergmann #include <linux/fs.h>
26826be063SChristoph Hellwig #include <linux/fsnotify.h>
2767207b96SArnd Bergmann #include <linux/backing-dev.h>
2867207b96SArnd Bergmann #include <linux/init.h>
2967207b96SArnd Bergmann #include <linux/ioctl.h>
3067207b96SArnd Bergmann #include <linux/module.h>
31346f4d3cSArnd Bergmann #include <linux/mount.h>
3267207b96SArnd Bergmann #include <linux/namei.h>
3367207b96SArnd Bergmann #include <linux/pagemap.h>
3467207b96SArnd Bergmann #include <linux/poll.h>
3567207b96SArnd Bergmann #include <linux/slab.h>
3667207b96SArnd Bergmann #include <linux/parser.h>
3767207b96SArnd Bergmann 
380afacde3Sarnd@arndb.de #include <asm/prom.h>
3967207b96SArnd Bergmann #include <asm/spu.h>
40ccf17e9dSJeremy Kerr #include <asm/spu_priv1.h>
4167207b96SArnd Bergmann #include <asm/uaccess.h>
4267207b96SArnd Bergmann 
4367207b96SArnd Bergmann #include "spufs.h"
4467207b96SArnd Bergmann 
452c3e4787SJeremy Kerr struct spufs_sb_info {
462c3e4787SJeremy Kerr 	int debug;
472c3e4787SJeremy Kerr };
482c3e4787SJeremy Kerr 
49e18b890bSChristoph Lameter static struct kmem_cache *spufs_inode_cache;
50c6730ed4SJeremy Kerr char *isolated_loader;
518b0d3121SSebastian Siewior static int isolated_loader_size;
5267207b96SArnd Bergmann 
532c3e4787SJeremy Kerr static struct spufs_sb_info *spufs_get_sb_info(struct super_block *sb)
542c3e4787SJeremy Kerr {
552c3e4787SJeremy Kerr 	return sb->s_fs_info;
562c3e4787SJeremy Kerr }
572c3e4787SJeremy Kerr 
5867207b96SArnd Bergmann static struct inode *
5967207b96SArnd Bergmann spufs_alloc_inode(struct super_block *sb)
6067207b96SArnd Bergmann {
6167207b96SArnd Bergmann 	struct spufs_inode_info *ei;
6267207b96SArnd Bergmann 
63e94b1766SChristoph Lameter 	ei = kmem_cache_alloc(spufs_inode_cache, GFP_KERNEL);
6467207b96SArnd Bergmann 	if (!ei)
6567207b96SArnd Bergmann 		return NULL;
666263203eSArnd Bergmann 
676263203eSArnd Bergmann 	ei->i_gang = NULL;
686263203eSArnd Bergmann 	ei->i_ctx = NULL;
6943c2bbd9SChristoph Hellwig 	ei->i_openers = 0;
706263203eSArnd Bergmann 
7167207b96SArnd Bergmann 	return &ei->vfs_inode;
7267207b96SArnd Bergmann }
7367207b96SArnd Bergmann 
74fa0d7e3dSNick Piggin static void spufs_i_callback(struct rcu_head *head)
7567207b96SArnd Bergmann {
76fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
77fa0d7e3dSNick Piggin 	INIT_LIST_HEAD(&inode->i_dentry);
7867207b96SArnd Bergmann 	kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
7967207b96SArnd Bergmann }
8067207b96SArnd Bergmann 
81fa0d7e3dSNick Piggin static void spufs_destroy_inode(struct inode *inode)
82fa0d7e3dSNick Piggin {
83fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, spufs_i_callback);
84fa0d7e3dSNick Piggin }
85fa0d7e3dSNick Piggin 
8667207b96SArnd Bergmann static void
8751cc5068SAlexey Dobriyan spufs_init_once(void *p)
8867207b96SArnd Bergmann {
8967207b96SArnd Bergmann 	struct spufs_inode_info *ei = p;
9067207b96SArnd Bergmann 
9167207b96SArnd Bergmann 	inode_init_once(&ei->vfs_inode);
9267207b96SArnd Bergmann }
9367207b96SArnd Bergmann 
9467207b96SArnd Bergmann static struct inode *
9567207b96SArnd Bergmann spufs_new_inode(struct super_block *sb, int mode)
9667207b96SArnd Bergmann {
9767207b96SArnd Bergmann 	struct inode *inode;
9867207b96SArnd Bergmann 
9967207b96SArnd Bergmann 	inode = new_inode(sb);
10067207b96SArnd Bergmann 	if (!inode)
10167207b96SArnd Bergmann 		goto out;
10267207b96SArnd Bergmann 
10367207b96SArnd Bergmann 	inode->i_mode = mode;
1041330deb0SDavid Howells 	inode->i_uid = current_fsuid();
1051330deb0SDavid Howells 	inode->i_gid = current_fsgid();
10667207b96SArnd Bergmann 	inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
10767207b96SArnd Bergmann out:
10867207b96SArnd Bergmann 	return inode;
10967207b96SArnd Bergmann }
11067207b96SArnd Bergmann 
11167207b96SArnd Bergmann static int
11267207b96SArnd Bergmann spufs_setattr(struct dentry *dentry, struct iattr *attr)
11367207b96SArnd Bergmann {
11467207b96SArnd Bergmann 	struct inode *inode = dentry->d_inode;
11567207b96SArnd Bergmann 
11667207b96SArnd Bergmann 	if ((attr->ia_valid & ATTR_SIZE) &&
11767207b96SArnd Bergmann 	    (attr->ia_size != inode->i_size))
11867207b96SArnd Bergmann 		return -EINVAL;
1191025774cSChristoph Hellwig 	setattr_copy(inode, attr);
1201025774cSChristoph Hellwig 	mark_inode_dirty(inode);
1211025774cSChristoph Hellwig 	return 0;
12267207b96SArnd Bergmann }
12367207b96SArnd Bergmann 
12467207b96SArnd Bergmann 
12567207b96SArnd Bergmann static int
12667207b96SArnd Bergmann spufs_new_file(struct super_block *sb, struct dentry *dentry,
12799ac48f5SArjan van de Ven 		const struct file_operations *fops, int mode,
12823d893f5SJeremy Kerr 		size_t size, struct spu_context *ctx)
12967207b96SArnd Bergmann {
1306e1d5dccSAlexey Dobriyan 	static const struct inode_operations spufs_file_iops = {
13167207b96SArnd Bergmann 		.setattr = spufs_setattr,
13267207b96SArnd Bergmann 	};
13367207b96SArnd Bergmann 	struct inode *inode;
13467207b96SArnd Bergmann 	int ret;
13567207b96SArnd Bergmann 
13667207b96SArnd Bergmann 	ret = -ENOSPC;
13767207b96SArnd Bergmann 	inode = spufs_new_inode(sb, S_IFREG | mode);
13867207b96SArnd Bergmann 	if (!inode)
13967207b96SArnd Bergmann 		goto out;
14067207b96SArnd Bergmann 
14167207b96SArnd Bergmann 	ret = 0;
14267207b96SArnd Bergmann 	inode->i_op = &spufs_file_iops;
14367207b96SArnd Bergmann 	inode->i_fop = fops;
14423d893f5SJeremy Kerr 	inode->i_size = size;
1458e18e294STheodore Ts'o 	inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
14667207b96SArnd Bergmann 	d_add(dentry, inode);
14767207b96SArnd Bergmann out:
14867207b96SArnd Bergmann 	return ret;
14967207b96SArnd Bergmann }
15067207b96SArnd Bergmann 
15167207b96SArnd Bergmann static void
1520f3f63a4SAl Viro spufs_evict_inode(struct inode *inode)
15367207b96SArnd Bergmann {
1546263203eSArnd Bergmann 	struct spufs_inode_info *ei = SPUFS_I(inode);
1550f3f63a4SAl Viro 	end_writeback(inode);
1566263203eSArnd Bergmann 	if (ei->i_ctx)
1576263203eSArnd Bergmann 		put_spu_context(ei->i_ctx);
1586263203eSArnd Bergmann 	if (ei->i_gang)
1596263203eSArnd Bergmann 		put_spu_gang(ei->i_gang);
16067207b96SArnd Bergmann }
16167207b96SArnd Bergmann 
1623f51dd91SArnd Bergmann static void spufs_prune_dir(struct dentry *dir)
1633f51dd91SArnd Bergmann {
1643f51dd91SArnd Bergmann 	struct dentry *dentry, *tmp;
1656263203eSArnd Bergmann 
1661b1dcc1bSJes Sorensen 	mutex_lock(&dir->d_inode->i_mutex);
167c3a9aea7SAndrew Morton 	list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
1683f51dd91SArnd Bergmann 		spin_lock(&dentry->d_lock);
1693f51dd91SArnd Bergmann 		if (!(d_unhashed(dentry)) && dentry->d_inode) {
170dc0474beSNick Piggin 			dget_dlock(dentry);
1713f51dd91SArnd Bergmann 			__d_drop(dentry);
1723f51dd91SArnd Bergmann 			spin_unlock(&dentry->d_lock);
1733f51dd91SArnd Bergmann 			simple_unlink(dir->d_inode, dentry);
174b5c84bf6SNick Piggin 			/* XXX: what was dcache_lock protecting here? Other
175da502956SNick Piggin 			 * filesystems (IB, configfs) release dcache_lock
176da502956SNick Piggin 			 * before unlink */
1773f51dd91SArnd Bergmann 			dput(dentry);
1783f51dd91SArnd Bergmann 		} else {
1793f51dd91SArnd Bergmann 			spin_unlock(&dentry->d_lock);
1803f51dd91SArnd Bergmann 		}
1813f51dd91SArnd Bergmann 	}
1823f51dd91SArnd Bergmann 	shrink_dcache_parent(dir);
1831b1dcc1bSJes Sorensen 	mutex_unlock(&dir->d_inode->i_mutex);
1843f51dd91SArnd Bergmann }
1853f51dd91SArnd Bergmann 
1866263203eSArnd Bergmann /* Caller must hold parent->i_mutex */
1876263203eSArnd Bergmann static int spufs_rmdir(struct inode *parent, struct dentry *dir)
1883f51dd91SArnd Bergmann {
1893f51dd91SArnd Bergmann 	/* remove all entries */
1906263203eSArnd Bergmann 	spufs_prune_dir(dir);
191c443acabSJeremy Kerr 	d_drop(dir);
1923f51dd91SArnd Bergmann 
1936263203eSArnd Bergmann 	return simple_rmdir(parent, dir);
1943f51dd91SArnd Bergmann }
1953f51dd91SArnd Bergmann 
19674254647SJeremy Kerr static int spufs_fill_dir(struct dentry *dir,
19774254647SJeremy Kerr 		const struct spufs_tree_descr *files, int mode,
19874254647SJeremy Kerr 		struct spu_context *ctx)
19967207b96SArnd Bergmann {
20087873c86SSebastian Siewior 	struct dentry *dentry, *tmp;
20167207b96SArnd Bergmann 	int ret;
20267207b96SArnd Bergmann 
20367207b96SArnd Bergmann 	while (files->name && files->name[0]) {
20467207b96SArnd Bergmann 		ret = -ENOMEM;
20567207b96SArnd Bergmann 		dentry = d_alloc_name(dir, files->name);
20667207b96SArnd Bergmann 		if (!dentry)
20767207b96SArnd Bergmann 			goto out;
20867207b96SArnd Bergmann 		ret = spufs_new_file(dir->d_sb, dentry, files->ops,
20923d893f5SJeremy Kerr 					files->mode & mode, files->size, ctx);
21067207b96SArnd Bergmann 		if (ret)
21167207b96SArnd Bergmann 			goto out;
21267207b96SArnd Bergmann 		files++;
21367207b96SArnd Bergmann 	}
21467207b96SArnd Bergmann 	return 0;
21567207b96SArnd Bergmann out:
21687873c86SSebastian Siewior 	/*
21787873c86SSebastian Siewior 	 * remove all children from dir. dir->inode is not set so don't
21887873c86SSebastian Siewior 	 * just simply use spufs_prune_dir() and panic afterwards :)
21987873c86SSebastian Siewior 	 * dput() looks like it will do the right thing:
22087873c86SSebastian Siewior 	 * - dec parent's ref counter
22187873c86SSebastian Siewior 	 * - remove child from parent's child list
22287873c86SSebastian Siewior 	 * - free child's inode if possible
22387873c86SSebastian Siewior 	 * - free child
22487873c86SSebastian Siewior 	 */
22587873c86SSebastian Siewior 	list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
22687873c86SSebastian Siewior 		dput(dentry);
22787873c86SSebastian Siewior 	}
22887873c86SSebastian Siewior 
22987873c86SSebastian Siewior 	shrink_dcache_parent(dir);
23067207b96SArnd Bergmann 	return ret;
23167207b96SArnd Bergmann }
23267207b96SArnd Bergmann 
23367207b96SArnd Bergmann static int spufs_dir_close(struct inode *inode, struct file *file)
23467207b96SArnd Bergmann {
2350309f02dSMichael Ellerman 	struct spu_context *ctx;
2366263203eSArnd Bergmann 	struct inode *parent;
2376263203eSArnd Bergmann 	struct dentry *dir;
23867207b96SArnd Bergmann 	int ret;
23967207b96SArnd Bergmann 
240b4d1ab58SJosef Sipek 	dir = file->f_path.dentry;
2416263203eSArnd Bergmann 	parent = dir->d_parent->d_inode;
2426263203eSArnd Bergmann 	ctx = SPUFS_I(dir->d_inode)->i_ctx;
243c8ca0633SArnd Bergmann 
24402539d71SChristoph Hellwig 	mutex_lock_nested(&parent->i_mutex, I_MUTEX_PARENT);
2456263203eSArnd Bergmann 	ret = spufs_rmdir(parent, dir);
2466263203eSArnd Bergmann 	mutex_unlock(&parent->i_mutex);
24767207b96SArnd Bergmann 	WARN_ON(ret);
248c8ca0633SArnd Bergmann 
2490309f02dSMichael Ellerman 	/* We have to give up the mm_struct */
2500309f02dSMichael Ellerman 	spu_forget(ctx);
2510309f02dSMichael Ellerman 
25267207b96SArnd Bergmann 	return dcache_dir_close(inode, file);
25367207b96SArnd Bergmann }
25467207b96SArnd Bergmann 
2555dfe4c96SArjan van de Ven const struct file_operations spufs_context_fops = {
25667207b96SArnd Bergmann 	.open		= dcache_dir_open,
25767207b96SArnd Bergmann 	.release	= spufs_dir_close,
25867207b96SArnd Bergmann 	.llseek		= dcache_dir_lseek,
25967207b96SArnd Bergmann 	.read		= generic_read_dir,
26067207b96SArnd Bergmann 	.readdir	= dcache_readdir,
2611b061d92SChristoph Hellwig 	.fsync		= noop_fsync,
26267207b96SArnd Bergmann };
263bf1ab978SDwayne Grant McConnell EXPORT_SYMBOL_GPL(spufs_context_fops);
26467207b96SArnd Bergmann 
26567207b96SArnd Bergmann static int
2669add11daSArnd Bergmann spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
2679add11daSArnd Bergmann 		int mode)
26867207b96SArnd Bergmann {
26967207b96SArnd Bergmann 	int ret;
27067207b96SArnd Bergmann 	struct inode *inode;
27167207b96SArnd Bergmann 	struct spu_context *ctx;
27267207b96SArnd Bergmann 
27367207b96SArnd Bergmann 	ret = -ENOSPC;
27467207b96SArnd Bergmann 	inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
27567207b96SArnd Bergmann 	if (!inode)
27667207b96SArnd Bergmann 		goto out;
27767207b96SArnd Bergmann 
27867207b96SArnd Bergmann 	if (dir->i_mode & S_ISGID) {
27967207b96SArnd Bergmann 		inode->i_gid = dir->i_gid;
28067207b96SArnd Bergmann 		inode->i_mode &= S_ISGID;
28167207b96SArnd Bergmann 	}
2826263203eSArnd Bergmann 	ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
28367207b96SArnd Bergmann 	SPUFS_I(inode)->i_ctx = ctx;
28467207b96SArnd Bergmann 	if (!ctx)
28567207b96SArnd Bergmann 		goto out_iput;
28667207b96SArnd Bergmann 
2879add11daSArnd Bergmann 	ctx->flags = flags;
288b8c295f9SJeremy Kerr 	inode->i_op = &simple_dir_inode_operations;
28967207b96SArnd Bergmann 	inode->i_fop = &simple_dir_operations;
2905737edd1SMark Nutter 	if (flags & SPU_CREATE_NOSCHED)
2915737edd1SMark Nutter 		ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
2925737edd1SMark Nutter 					 mode, ctx);
2935737edd1SMark Nutter 	else
29467207b96SArnd Bergmann 		ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
2955737edd1SMark Nutter 
29667207b96SArnd Bergmann 	if (ret)
29767207b96SArnd Bergmann 		goto out_free_ctx;
29867207b96SArnd Bergmann 
2992c3e4787SJeremy Kerr 	if (spufs_get_sb_info(dir->i_sb)->debug)
3002c3e4787SJeremy Kerr 		ret = spufs_fill_dir(dentry, spufs_dir_debug_contents,
3012c3e4787SJeremy Kerr 				mode, ctx);
3022c3e4787SJeremy Kerr 
3032c3e4787SJeremy Kerr 	if (ret)
3042c3e4787SJeremy Kerr 		goto out_free_ctx;
3052c3e4787SJeremy Kerr 
30667207b96SArnd Bergmann 	d_instantiate(dentry, inode);
30767207b96SArnd Bergmann 	dget(dentry);
308ba0b996dSJeremy Kerr 	inc_nlink(dir);
309ba0b996dSJeremy Kerr 	inc_nlink(dentry->d_inode);
31067207b96SArnd Bergmann 	goto out;
31167207b96SArnd Bergmann 
31267207b96SArnd Bergmann out_free_ctx:
31389df0085SSebastian Siewior 	spu_forget(ctx);
31467207b96SArnd Bergmann 	put_spu_context(ctx);
31567207b96SArnd Bergmann out_iput:
31667207b96SArnd Bergmann 	iput(inode);
31767207b96SArnd Bergmann out:
31867207b96SArnd Bergmann 	return ret;
31967207b96SArnd Bergmann }
32067207b96SArnd Bergmann 
321346f4d3cSArnd Bergmann static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
322346f4d3cSArnd Bergmann {
323346f4d3cSArnd Bergmann 	int ret;
324346f4d3cSArnd Bergmann 	struct file *filp;
325346f4d3cSArnd Bergmann 
326346f4d3cSArnd Bergmann 	ret = get_unused_fd();
327346f4d3cSArnd Bergmann 	if (ret < 0) {
328346f4d3cSArnd Bergmann 		dput(dentry);
329346f4d3cSArnd Bergmann 		mntput(mnt);
330346f4d3cSArnd Bergmann 		goto out;
331346f4d3cSArnd Bergmann 	}
332346f4d3cSArnd Bergmann 
333745ca247SDavid Howells 	filp = dentry_open(dentry, mnt, O_RDONLY, current_cred());
334346f4d3cSArnd Bergmann 	if (IS_ERR(filp)) {
335346f4d3cSArnd Bergmann 		put_unused_fd(ret);
336346f4d3cSArnd Bergmann 		ret = PTR_ERR(filp);
337346f4d3cSArnd Bergmann 		goto out;
338346f4d3cSArnd Bergmann 	}
339346f4d3cSArnd Bergmann 
340346f4d3cSArnd Bergmann 	filp->f_op = &spufs_context_fops;
341346f4d3cSArnd Bergmann 	fd_install(ret, filp);
342346f4d3cSArnd Bergmann out:
343346f4d3cSArnd Bergmann 	return ret;
344346f4d3cSArnd Bergmann }
345346f4d3cSArnd Bergmann 
3468e68e2f2SArnd Bergmann static struct spu_context *
3478e68e2f2SArnd Bergmann spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
3488e68e2f2SArnd Bergmann 						struct file *filp)
3498e68e2f2SArnd Bergmann {
35058119068SAndre Detsch 	struct spu_context *tmp, *neighbor, *err;
3518e68e2f2SArnd Bergmann 	int count, node;
3528e68e2f2SArnd Bergmann 	int aff_supp;
3538e68e2f2SArnd Bergmann 
3548e68e2f2SArnd Bergmann 	aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
3558e68e2f2SArnd Bergmann 					struct spu, cbe_list))->aff_list);
3568e68e2f2SArnd Bergmann 
3578e68e2f2SArnd Bergmann 	if (!aff_supp)
3588e68e2f2SArnd Bergmann 		return ERR_PTR(-EINVAL);
3598e68e2f2SArnd Bergmann 
3608e68e2f2SArnd Bergmann 	if (flags & SPU_CREATE_GANG)
3618e68e2f2SArnd Bergmann 		return ERR_PTR(-EINVAL);
3628e68e2f2SArnd Bergmann 
3638e68e2f2SArnd Bergmann 	if (flags & SPU_CREATE_AFFINITY_MEM &&
3648e68e2f2SArnd Bergmann 	    gang->aff_ref_ctx &&
3658e68e2f2SArnd Bergmann 	    gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
3668e68e2f2SArnd Bergmann 		return ERR_PTR(-EEXIST);
3678e68e2f2SArnd Bergmann 
3688e68e2f2SArnd Bergmann 	if (gang->aff_flags & AFF_MERGED)
3698e68e2f2SArnd Bergmann 		return ERR_PTR(-EBUSY);
3708e68e2f2SArnd Bergmann 
3718e68e2f2SArnd Bergmann 	neighbor = NULL;
3728e68e2f2SArnd Bergmann 	if (flags & SPU_CREATE_AFFINITY_SPU) {
3738e68e2f2SArnd Bergmann 		if (!filp || filp->f_op != &spufs_context_fops)
3748e68e2f2SArnd Bergmann 			return ERR_PTR(-EINVAL);
3758e68e2f2SArnd Bergmann 
3768e68e2f2SArnd Bergmann 		neighbor = get_spu_context(
3778e68e2f2SArnd Bergmann 				SPUFS_I(filp->f_dentry->d_inode)->i_ctx);
3788e68e2f2SArnd Bergmann 
3798e68e2f2SArnd Bergmann 		if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
3808e68e2f2SArnd Bergmann 		    !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
3818e68e2f2SArnd Bergmann 		    !list_entry(neighbor->aff_list.next, struct spu_context,
38258119068SAndre Detsch 		    aff_list)->aff_head) {
38358119068SAndre Detsch 			err = ERR_PTR(-EEXIST);
38458119068SAndre Detsch 			goto out_put_neighbor;
38558119068SAndre Detsch 		}
3868e68e2f2SArnd Bergmann 
38758119068SAndre Detsch 		if (gang != neighbor->gang) {
38858119068SAndre Detsch 			err = ERR_PTR(-EINVAL);
38958119068SAndre Detsch 			goto out_put_neighbor;
39058119068SAndre Detsch 		}
3918e68e2f2SArnd Bergmann 
3928e68e2f2SArnd Bergmann 		count = 1;
3938e68e2f2SArnd Bergmann 		list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
3948e68e2f2SArnd Bergmann 			count++;
3958e68e2f2SArnd Bergmann 		if (list_empty(&neighbor->aff_list))
3968e68e2f2SArnd Bergmann 			count++;
3978e68e2f2SArnd Bergmann 
3988e68e2f2SArnd Bergmann 		for (node = 0; node < MAX_NUMNODES; node++) {
3998e68e2f2SArnd Bergmann 			if ((cbe_spu_info[node].n_spus - atomic_read(
4008e68e2f2SArnd Bergmann 				&cbe_spu_info[node].reserved_spus)) >= count)
4018e68e2f2SArnd Bergmann 				break;
4028e68e2f2SArnd Bergmann 		}
4038e68e2f2SArnd Bergmann 
40458119068SAndre Detsch 		if (node == MAX_NUMNODES) {
40558119068SAndre Detsch 			err = ERR_PTR(-EEXIST);
40658119068SAndre Detsch 			goto out_put_neighbor;
40758119068SAndre Detsch 		}
4088e68e2f2SArnd Bergmann 	}
4098e68e2f2SArnd Bergmann 
4108e68e2f2SArnd Bergmann 	return neighbor;
41158119068SAndre Detsch 
41258119068SAndre Detsch out_put_neighbor:
41358119068SAndre Detsch 	put_spu_context(neighbor);
41458119068SAndre Detsch 	return err;
4158e68e2f2SArnd Bergmann }
4168e68e2f2SArnd Bergmann 
4178e68e2f2SArnd Bergmann static void
4188e68e2f2SArnd Bergmann spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
4198e68e2f2SArnd Bergmann 					struct spu_context *neighbor)
4208e68e2f2SArnd Bergmann {
4218e68e2f2SArnd Bergmann 	if (flags & SPU_CREATE_AFFINITY_MEM)
4228e68e2f2SArnd Bergmann 		ctx->gang->aff_ref_ctx = ctx;
4238e68e2f2SArnd Bergmann 
4248e68e2f2SArnd Bergmann 	if (flags & SPU_CREATE_AFFINITY_SPU) {
4258e68e2f2SArnd Bergmann 		if (list_empty(&neighbor->aff_list)) {
4268e68e2f2SArnd Bergmann 			list_add_tail(&neighbor->aff_list,
4278e68e2f2SArnd Bergmann 				&ctx->gang->aff_list_head);
4288e68e2f2SArnd Bergmann 			neighbor->aff_head = 1;
4298e68e2f2SArnd Bergmann 		}
4308e68e2f2SArnd Bergmann 
4318e68e2f2SArnd Bergmann 		if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
4328e68e2f2SArnd Bergmann 		    || list_entry(neighbor->aff_list.next, struct spu_context,
4338e68e2f2SArnd Bergmann 							aff_list)->aff_head) {
4348e68e2f2SArnd Bergmann 			list_add(&ctx->aff_list, &neighbor->aff_list);
4358e68e2f2SArnd Bergmann 		} else  {
4368e68e2f2SArnd Bergmann 			list_add_tail(&ctx->aff_list, &neighbor->aff_list);
4378e68e2f2SArnd Bergmann 			if (neighbor->aff_head) {
4388e68e2f2SArnd Bergmann 				neighbor->aff_head = 0;
4398e68e2f2SArnd Bergmann 				ctx->aff_head = 1;
4408e68e2f2SArnd Bergmann 			}
4418e68e2f2SArnd Bergmann 		}
4428e68e2f2SArnd Bergmann 
4438e68e2f2SArnd Bergmann 		if (!ctx->gang->aff_ref_ctx)
4448e68e2f2SArnd Bergmann 			ctx->gang->aff_ref_ctx = ctx;
4458e68e2f2SArnd Bergmann 	}
4468e68e2f2SArnd Bergmann }
4478e68e2f2SArnd Bergmann 
4488e68e2f2SArnd Bergmann static int
4498e68e2f2SArnd Bergmann spufs_create_context(struct inode *inode, struct dentry *dentry,
4508e68e2f2SArnd Bergmann 			struct vfsmount *mnt, int flags, int mode,
4518e68e2f2SArnd Bergmann 			struct file *aff_filp)
4526263203eSArnd Bergmann {
4536263203eSArnd Bergmann 	int ret;
4548e68e2f2SArnd Bergmann 	int affinity;
4558e68e2f2SArnd Bergmann 	struct spu_gang *gang;
4568e68e2f2SArnd Bergmann 	struct spu_context *neighbor;
4576263203eSArnd Bergmann 
4585737edd1SMark Nutter 	ret = -EPERM;
4595737edd1SMark Nutter 	if ((flags & SPU_CREATE_NOSCHED) &&
4605737edd1SMark Nutter 	    !capable(CAP_SYS_NICE))
4615737edd1SMark Nutter 		goto out_unlock;
4625737edd1SMark Nutter 
4635737edd1SMark Nutter 	ret = -EINVAL;
4645737edd1SMark Nutter 	if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
4655737edd1SMark Nutter 	    == SPU_CREATE_ISOLATE)
4665737edd1SMark Nutter 		goto out_unlock;
4675737edd1SMark Nutter 
468bd2e5f82SJeremy Kerr 	ret = -ENODEV;
469bd2e5f82SJeremy Kerr 	if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
470bd2e5f82SJeremy Kerr 		goto out_unlock;
471bd2e5f82SJeremy Kerr 
4728e68e2f2SArnd Bergmann 	gang = NULL;
4738e68e2f2SArnd Bergmann 	neighbor = NULL;
4748e68e2f2SArnd Bergmann 	affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
4758e68e2f2SArnd Bergmann 	if (affinity) {
4768e68e2f2SArnd Bergmann 		gang = SPUFS_I(inode)->i_gang;
4778e68e2f2SArnd Bergmann 		ret = -EINVAL;
4788e68e2f2SArnd Bergmann 		if (!gang)
4798e68e2f2SArnd Bergmann 			goto out_unlock;
4808e68e2f2SArnd Bergmann 		mutex_lock(&gang->aff_mutex);
4818e68e2f2SArnd Bergmann 		neighbor = spufs_assert_affinity(flags, gang, aff_filp);
4828e68e2f2SArnd Bergmann 		if (IS_ERR(neighbor)) {
4838e68e2f2SArnd Bergmann 			ret = PTR_ERR(neighbor);
4848e68e2f2SArnd Bergmann 			goto out_aff_unlock;
4858e68e2f2SArnd Bergmann 		}
4868e68e2f2SArnd Bergmann 	}
4878e68e2f2SArnd Bergmann 
4886263203eSArnd Bergmann 	ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
4896263203eSArnd Bergmann 	if (ret)
4908e68e2f2SArnd Bergmann 		goto out_aff_unlock;
4918e68e2f2SArnd Bergmann 
49258119068SAndre Detsch 	if (affinity) {
4938e68e2f2SArnd Bergmann 		spufs_set_affinity(flags, SPUFS_I(dentry->d_inode)->i_ctx,
4948e68e2f2SArnd Bergmann 								neighbor);
49558119068SAndre Detsch 		if (neighbor)
49658119068SAndre Detsch 			put_spu_context(neighbor);
49758119068SAndre Detsch 	}
4986263203eSArnd Bergmann 
4996263203eSArnd Bergmann 	/*
5006263203eSArnd Bergmann 	 * get references for dget and mntget, will be released
5016263203eSArnd Bergmann 	 * in error path of *_open().
5026263203eSArnd Bergmann 	 */
5036263203eSArnd Bergmann 	ret = spufs_context_open(dget(dentry), mntget(mnt));
5046263203eSArnd Bergmann 	if (ret < 0) {
5056263203eSArnd Bergmann 		WARN_ON(spufs_rmdir(inode, dentry));
5066747c2eeSKou Ishizaki 		if (affinity)
5076747c2eeSKou Ishizaki 			mutex_unlock(&gang->aff_mutex);
5086263203eSArnd Bergmann 		mutex_unlock(&inode->i_mutex);
5096263203eSArnd Bergmann 		spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
5106263203eSArnd Bergmann 		goto out;
5116263203eSArnd Bergmann 	}
5126263203eSArnd Bergmann 
5138e68e2f2SArnd Bergmann out_aff_unlock:
5148e68e2f2SArnd Bergmann 	if (affinity)
5158e68e2f2SArnd Bergmann 		mutex_unlock(&gang->aff_mutex);
5166263203eSArnd Bergmann out_unlock:
5176263203eSArnd Bergmann 	mutex_unlock(&inode->i_mutex);
5186263203eSArnd Bergmann out:
5196263203eSArnd Bergmann 	dput(dentry);
5206263203eSArnd Bergmann 	return ret;
5216263203eSArnd Bergmann }
5226263203eSArnd Bergmann 
5236263203eSArnd Bergmann static int
5246263203eSArnd Bergmann spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
5256263203eSArnd Bergmann {
5266263203eSArnd Bergmann 	int ret;
5276263203eSArnd Bergmann 	struct inode *inode;
5286263203eSArnd Bergmann 	struct spu_gang *gang;
5296263203eSArnd Bergmann 
5306263203eSArnd Bergmann 	ret = -ENOSPC;
5316263203eSArnd Bergmann 	inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
5326263203eSArnd Bergmann 	if (!inode)
5336263203eSArnd Bergmann 		goto out;
5346263203eSArnd Bergmann 
5356263203eSArnd Bergmann 	ret = 0;
5366263203eSArnd Bergmann 	if (dir->i_mode & S_ISGID) {
5376263203eSArnd Bergmann 		inode->i_gid = dir->i_gid;
5386263203eSArnd Bergmann 		inode->i_mode &= S_ISGID;
5396263203eSArnd Bergmann 	}
5406263203eSArnd Bergmann 	gang = alloc_spu_gang();
5416263203eSArnd Bergmann 	SPUFS_I(inode)->i_ctx = NULL;
5426263203eSArnd Bergmann 	SPUFS_I(inode)->i_gang = gang;
5436263203eSArnd Bergmann 	if (!gang)
5446263203eSArnd Bergmann 		goto out_iput;
5456263203eSArnd Bergmann 
546b8c295f9SJeremy Kerr 	inode->i_op = &simple_dir_inode_operations;
5476263203eSArnd Bergmann 	inode->i_fop = &simple_dir_operations;
5486263203eSArnd Bergmann 
5496263203eSArnd Bergmann 	d_instantiate(dentry, inode);
550ba0b996dSJeremy Kerr 	inc_nlink(dir);
551ba0b996dSJeremy Kerr 	inc_nlink(dentry->d_inode);
5526263203eSArnd Bergmann 	return ret;
5536263203eSArnd Bergmann 
5546263203eSArnd Bergmann out_iput:
5556263203eSArnd Bergmann 	iput(inode);
5566263203eSArnd Bergmann out:
5576263203eSArnd Bergmann 	return ret;
5586263203eSArnd Bergmann }
5596263203eSArnd Bergmann 
5606263203eSArnd Bergmann static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
5616263203eSArnd Bergmann {
5626263203eSArnd Bergmann 	int ret;
5636263203eSArnd Bergmann 	struct file *filp;
5646263203eSArnd Bergmann 
5656263203eSArnd Bergmann 	ret = get_unused_fd();
5666263203eSArnd Bergmann 	if (ret < 0) {
5676263203eSArnd Bergmann 		dput(dentry);
5686263203eSArnd Bergmann 		mntput(mnt);
5696263203eSArnd Bergmann 		goto out;
5706263203eSArnd Bergmann 	}
5716263203eSArnd Bergmann 
572745ca247SDavid Howells 	filp = dentry_open(dentry, mnt, O_RDONLY, current_cred());
5736263203eSArnd Bergmann 	if (IS_ERR(filp)) {
5746263203eSArnd Bergmann 		put_unused_fd(ret);
5756263203eSArnd Bergmann 		ret = PTR_ERR(filp);
5766263203eSArnd Bergmann 		goto out;
5776263203eSArnd Bergmann 	}
5786263203eSArnd Bergmann 
579877907d3SJeremy Kerr 	filp->f_op = &simple_dir_operations;
5806263203eSArnd Bergmann 	fd_install(ret, filp);
5816263203eSArnd Bergmann out:
5826263203eSArnd Bergmann 	return ret;
5836263203eSArnd Bergmann }
5846263203eSArnd Bergmann 
5856263203eSArnd Bergmann static int spufs_create_gang(struct inode *inode,
5866263203eSArnd Bergmann 			struct dentry *dentry,
5876263203eSArnd Bergmann 			struct vfsmount *mnt, int mode)
5886263203eSArnd Bergmann {
5896263203eSArnd Bergmann 	int ret;
5906263203eSArnd Bergmann 
5916263203eSArnd Bergmann 	ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
5926263203eSArnd Bergmann 	if (ret)
5936263203eSArnd Bergmann 		goto out;
5946263203eSArnd Bergmann 
5956263203eSArnd Bergmann 	/*
5966263203eSArnd Bergmann 	 * get references for dget and mntget, will be released
5976263203eSArnd Bergmann 	 * in error path of *_open().
5986263203eSArnd Bergmann 	 */
5996263203eSArnd Bergmann 	ret = spufs_gang_open(dget(dentry), mntget(mnt));
600877907d3SJeremy Kerr 	if (ret < 0) {
601877907d3SJeremy Kerr 		int err = simple_rmdir(inode, dentry);
602877907d3SJeremy Kerr 		WARN_ON(err);
603877907d3SJeremy Kerr 	}
6046263203eSArnd Bergmann 
6056263203eSArnd Bergmann out:
6066263203eSArnd Bergmann 	mutex_unlock(&inode->i_mutex);
6076263203eSArnd Bergmann 	dput(dentry);
6086263203eSArnd Bergmann 	return ret;
6096263203eSArnd Bergmann }
6106263203eSArnd Bergmann 
6116263203eSArnd Bergmann 
612346f4d3cSArnd Bergmann static struct file_system_type spufs_type;
613346f4d3cSArnd Bergmann 
614*1ba10681SAl Viro long spufs_create(struct path *path, struct dentry *dentry,
615*1ba10681SAl Viro 		unsigned int flags, mode_t mode, struct file *filp)
61667207b96SArnd Bergmann {
61767207b96SArnd Bergmann 	int ret;
61867207b96SArnd Bergmann 
61967207b96SArnd Bergmann 	ret = -EINVAL;
6206263203eSArnd Bergmann 	/* check if we are on spufs */
621*1ba10681SAl Viro 	if (path->dentry->d_sb->s_type != &spufs_type)
62267207b96SArnd Bergmann 		goto out;
62367207b96SArnd Bergmann 
6246263203eSArnd Bergmann 	/* don't accept undefined flags */
6259add11daSArnd Bergmann 	if (flags & (~SPU_CREATE_FLAG_ALL))
626c9832948Sarnd@arndb.de 		goto out;
627c9832948Sarnd@arndb.de 
6286263203eSArnd Bergmann 	/* only threads can be underneath a gang */
629*1ba10681SAl Viro 	if (path->dentry != path->dentry->d_sb->s_root) {
6306263203eSArnd Bergmann 		if ((flags & SPU_CREATE_GANG) ||
631*1ba10681SAl Viro 		    !SPUFS_I(path->dentry->d_inode)->i_gang)
6326263203eSArnd Bergmann 			goto out;
6336263203eSArnd Bergmann 	}
6346263203eSArnd Bergmann 
635ce3b0f8dSAl Viro 	mode &= ~current_umask();
63667207b96SArnd Bergmann 
6376263203eSArnd Bergmann 	if (flags & SPU_CREATE_GANG)
638*1ba10681SAl Viro 		ret = spufs_create_gang(path->dentry->d_inode,
639*1ba10681SAl Viro 					 dentry, path->mnt, mode);
6406263203eSArnd Bergmann 	else
641*1ba10681SAl Viro 		ret = spufs_create_context(path->dentry->d_inode,
642*1ba10681SAl Viro 					    dentry, path->mnt, flags, mode,
6434ac91378SJan Blunck 					    filp);
644826be063SChristoph Hellwig 	if (ret >= 0)
645*1ba10681SAl Viro 		fsnotify_mkdir(path->dentry->d_inode, dentry);
646826be063SChristoph Hellwig 	return ret;
64767207b96SArnd Bergmann 
64867207b96SArnd Bergmann out:
649*1ba10681SAl Viro 	mutex_unlock(&path->dentry->d_inode->i_mutex);
65067207b96SArnd Bergmann 	return ret;
65167207b96SArnd Bergmann }
65267207b96SArnd Bergmann 
65367207b96SArnd Bergmann /* File system initialization */
65467207b96SArnd Bergmann enum {
6552c3e4787SJeremy Kerr 	Opt_uid, Opt_gid, Opt_mode, Opt_debug, Opt_err,
65667207b96SArnd Bergmann };
65767207b96SArnd Bergmann 
658a447c093SSteven Whitehouse static const match_table_t spufs_tokens = {
65967207b96SArnd Bergmann 	{ Opt_uid,   "uid=%d" },
66067207b96SArnd Bergmann 	{ Opt_gid,   "gid=%d" },
661f11f5ee7SJeremy Kerr 	{ Opt_mode,  "mode=%o" },
6622c3e4787SJeremy Kerr 	{ Opt_debug, "debug" },
66367207b96SArnd Bergmann 	{ Opt_err,    NULL  },
66467207b96SArnd Bergmann };
66567207b96SArnd Bergmann 
66667207b96SArnd Bergmann static int
6672c3e4787SJeremy Kerr spufs_parse_options(struct super_block *sb, char *options, struct inode *root)
66867207b96SArnd Bergmann {
66967207b96SArnd Bergmann 	char *p;
67067207b96SArnd Bergmann 	substring_t args[MAX_OPT_ARGS];
67167207b96SArnd Bergmann 
67267207b96SArnd Bergmann 	while ((p = strsep(&options, ",")) != NULL) {
67367207b96SArnd Bergmann 		int token, option;
67467207b96SArnd Bergmann 
67567207b96SArnd Bergmann 		if (!*p)
67667207b96SArnd Bergmann 			continue;
67767207b96SArnd Bergmann 
67867207b96SArnd Bergmann 		token = match_token(p, spufs_tokens, args);
67967207b96SArnd Bergmann 		switch (token) {
68067207b96SArnd Bergmann 		case Opt_uid:
68167207b96SArnd Bergmann 			if (match_int(&args[0], &option))
68267207b96SArnd Bergmann 				return 0;
68367207b96SArnd Bergmann 			root->i_uid = option;
68467207b96SArnd Bergmann 			break;
68567207b96SArnd Bergmann 		case Opt_gid:
68667207b96SArnd Bergmann 			if (match_int(&args[0], &option))
68767207b96SArnd Bergmann 				return 0;
68867207b96SArnd Bergmann 			root->i_gid = option;
68967207b96SArnd Bergmann 			break;
690f11f5ee7SJeremy Kerr 		case Opt_mode:
691f11f5ee7SJeremy Kerr 			if (match_octal(&args[0], &option))
692f11f5ee7SJeremy Kerr 				return 0;
693f11f5ee7SJeremy Kerr 			root->i_mode = option | S_IFDIR;
694f11f5ee7SJeremy Kerr 			break;
6952c3e4787SJeremy Kerr 		case Opt_debug:
6962c3e4787SJeremy Kerr 			spufs_get_sb_info(sb)->debug = 1;
6972c3e4787SJeremy Kerr 			break;
69867207b96SArnd Bergmann 		default:
69967207b96SArnd Bergmann 			return 0;
70067207b96SArnd Bergmann 		}
70167207b96SArnd Bergmann 	}
70267207b96SArnd Bergmann 	return 1;
70367207b96SArnd Bergmann }
70467207b96SArnd Bergmann 
705db1384b4SAkinobu Mita static void spufs_exit_isolated_loader(void)
706db1384b4SAkinobu Mita {
7078b0d3121SSebastian Siewior 	free_pages((unsigned long) isolated_loader,
7088b0d3121SSebastian Siewior 			get_order(isolated_loader_size));
709db1384b4SAkinobu Mita }
710db1384b4SAkinobu Mita 
7110afacde3Sarnd@arndb.de static void
7120afacde3Sarnd@arndb.de spufs_init_isolated_loader(void)
7130afacde3Sarnd@arndb.de {
7140afacde3Sarnd@arndb.de 	struct device_node *dn;
7150afacde3Sarnd@arndb.de 	const char *loader;
7160afacde3Sarnd@arndb.de 	int size;
7170afacde3Sarnd@arndb.de 
7180afacde3Sarnd@arndb.de 	dn = of_find_node_by_path("/spu-isolation");
7190afacde3Sarnd@arndb.de 	if (!dn)
7200afacde3Sarnd@arndb.de 		return;
7210afacde3Sarnd@arndb.de 
722e2eb6392SStephen Rothwell 	loader = of_get_property(dn, "loader", &size);
7230afacde3Sarnd@arndb.de 	if (!loader)
7240afacde3Sarnd@arndb.de 		return;
7250afacde3Sarnd@arndb.de 
7268b0d3121SSebastian Siewior 	/* the loader must be align on a 16 byte boundary */
7278b0d3121SSebastian Siewior 	isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
7280afacde3Sarnd@arndb.de 	if (!isolated_loader)
7290afacde3Sarnd@arndb.de 		return;
7300afacde3Sarnd@arndb.de 
7318b0d3121SSebastian Siewior 	isolated_loader_size = size;
7320afacde3Sarnd@arndb.de 	memcpy(isolated_loader, loader, size);
7330afacde3Sarnd@arndb.de 	printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
7340afacde3Sarnd@arndb.de }
7350afacde3Sarnd@arndb.de 
73667207b96SArnd Bergmann static int
7378b3d6663SArnd Bergmann spufs_create_root(struct super_block *sb, void *data)
7388b3d6663SArnd Bergmann {
73967207b96SArnd Bergmann 	struct inode *inode;
74067207b96SArnd Bergmann 	int ret;
74167207b96SArnd Bergmann 
7428f18a158SArnd Bergmann 	ret = -ENODEV;
7438f18a158SArnd Bergmann 	if (!spu_management_ops)
7448f18a158SArnd Bergmann 		goto out;
7458f18a158SArnd Bergmann 
74667207b96SArnd Bergmann 	ret = -ENOMEM;
74767207b96SArnd Bergmann 	inode = spufs_new_inode(sb, S_IFDIR | 0775);
74867207b96SArnd Bergmann 	if (!inode)
74967207b96SArnd Bergmann 		goto out;
75067207b96SArnd Bergmann 
751b8c295f9SJeremy Kerr 	inode->i_op = &simple_dir_inode_operations;
75267207b96SArnd Bergmann 	inode->i_fop = &simple_dir_operations;
75367207b96SArnd Bergmann 	SPUFS_I(inode)->i_ctx = NULL;
754e2ed6e4dSJeremy Kerr 	inc_nlink(inode);
75567207b96SArnd Bergmann 
75667207b96SArnd Bergmann 	ret = -EINVAL;
7572c3e4787SJeremy Kerr 	if (!spufs_parse_options(sb, data, inode))
75867207b96SArnd Bergmann 		goto out_iput;
75967207b96SArnd Bergmann 
76067207b96SArnd Bergmann 	ret = -ENOMEM;
76167207b96SArnd Bergmann 	sb->s_root = d_alloc_root(inode);
76267207b96SArnd Bergmann 	if (!sb->s_root)
76367207b96SArnd Bergmann 		goto out_iput;
76467207b96SArnd Bergmann 
76567207b96SArnd Bergmann 	return 0;
76667207b96SArnd Bergmann out_iput:
76767207b96SArnd Bergmann 	iput(inode);
76867207b96SArnd Bergmann out:
76967207b96SArnd Bergmann 	return ret;
77067207b96SArnd Bergmann }
77167207b96SArnd Bergmann 
77267207b96SArnd Bergmann static int
77367207b96SArnd Bergmann spufs_fill_super(struct super_block *sb, void *data, int silent)
77467207b96SArnd Bergmann {
7752c3e4787SJeremy Kerr 	struct spufs_sb_info *info;
776b87221deSAlexey Dobriyan 	static const struct super_operations s_ops = {
77767207b96SArnd Bergmann 		.alloc_inode = spufs_alloc_inode,
77867207b96SArnd Bergmann 		.destroy_inode = spufs_destroy_inode,
77967207b96SArnd Bergmann 		.statfs = simple_statfs,
7800f3f63a4SAl Viro 		.evict_inode = spufs_evict_inode,
78190d09e14SMiklos Szeredi 		.show_options = generic_show_options,
78267207b96SArnd Bergmann 	};
78367207b96SArnd Bergmann 
78490d09e14SMiklos Szeredi 	save_mount_options(sb, data);
78590d09e14SMiklos Szeredi 
7862c3e4787SJeremy Kerr 	info = kzalloc(sizeof(*info), GFP_KERNEL);
7872c3e4787SJeremy Kerr 	if (!info)
7882c3e4787SJeremy Kerr 		return -ENOMEM;
7892c3e4787SJeremy Kerr 
79067207b96SArnd Bergmann 	sb->s_maxbytes = MAX_LFS_FILESIZE;
79167207b96SArnd Bergmann 	sb->s_blocksize = PAGE_CACHE_SIZE;
79267207b96SArnd Bergmann 	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
79367207b96SArnd Bergmann 	sb->s_magic = SPUFS_MAGIC;
79467207b96SArnd Bergmann 	sb->s_op = &s_ops;
7952c3e4787SJeremy Kerr 	sb->s_fs_info = info;
79667207b96SArnd Bergmann 
79767207b96SArnd Bergmann 	return spufs_create_root(sb, data);
79867207b96SArnd Bergmann }
79967207b96SArnd Bergmann 
800fc14f2feSAl Viro static struct dentry *
801fc14f2feSAl Viro spufs_mount(struct file_system_type *fstype, int flags,
802fc14f2feSAl Viro 		const char *name, void *data)
80367207b96SArnd Bergmann {
804fc14f2feSAl Viro 	return mount_single(fstype, flags, data, spufs_fill_super);
80567207b96SArnd Bergmann }
80667207b96SArnd Bergmann 
80767207b96SArnd Bergmann static struct file_system_type spufs_type = {
80867207b96SArnd Bergmann 	.owner = THIS_MODULE,
80967207b96SArnd Bergmann 	.name = "spufs",
810fc14f2feSAl Viro 	.mount = spufs_mount,
81167207b96SArnd Bergmann 	.kill_sb = kill_litter_super,
81267207b96SArnd Bergmann };
81367207b96SArnd Bergmann 
814e78b47a5SArnd Bergmann static int __init spufs_init(void)
81567207b96SArnd Bergmann {
81667207b96SArnd Bergmann 	int ret;
817bf1ab978SDwayne Grant McConnell 
818ccf17e9dSJeremy Kerr 	ret = -ENODEV;
819ccf17e9dSJeremy Kerr 	if (!spu_management_ops)
820ccf17e9dSJeremy Kerr 		goto out;
821ccf17e9dSJeremy Kerr 
82267207b96SArnd Bergmann 	ret = -ENOMEM;
82367207b96SArnd Bergmann 	spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
82467207b96SArnd Bergmann 			sizeof(struct spufs_inode_info), 0,
82520c2df83SPaul Mundt 			SLAB_HWCACHE_ALIGN, spufs_init_once);
82667207b96SArnd Bergmann 
82767207b96SArnd Bergmann 	if (!spufs_inode_cache)
82867207b96SArnd Bergmann 		goto out;
829c99c1994SAkinobu Mita 	ret = spu_sched_init();
83067207b96SArnd Bergmann 	if (ret)
83167207b96SArnd Bergmann 		goto out_cache;
832c99c1994SAkinobu Mita 	ret = register_filesystem(&spufs_type);
833c99c1994SAkinobu Mita 	if (ret)
834c99c1994SAkinobu Mita 		goto out_sched;
83567207b96SArnd Bergmann 	ret = register_spu_syscalls(&spufs_calls);
83667207b96SArnd Bergmann 	if (ret)
83767207b96SArnd Bergmann 		goto out_fs;
8380afacde3Sarnd@arndb.de 
8390afacde3Sarnd@arndb.de 	spufs_init_isolated_loader();
840bf1ab978SDwayne Grant McConnell 
84167207b96SArnd Bergmann 	return 0;
842c99c1994SAkinobu Mita 
84367207b96SArnd Bergmann out_fs:
84467207b96SArnd Bergmann 	unregister_filesystem(&spufs_type);
845c99c1994SAkinobu Mita out_sched:
846c99c1994SAkinobu Mita 	spu_sched_exit();
84767207b96SArnd Bergmann out_cache:
84867207b96SArnd Bergmann 	kmem_cache_destroy(spufs_inode_cache);
84967207b96SArnd Bergmann out:
85067207b96SArnd Bergmann 	return ret;
85167207b96SArnd Bergmann }
85267207b96SArnd Bergmann module_init(spufs_init);
85367207b96SArnd Bergmann 
854e78b47a5SArnd Bergmann static void __exit spufs_exit(void)
85567207b96SArnd Bergmann {
8568b3d6663SArnd Bergmann 	spu_sched_exit();
857db1384b4SAkinobu Mita 	spufs_exit_isolated_loader();
85867207b96SArnd Bergmann 	unregister_spu_syscalls(&spufs_calls);
85967207b96SArnd Bergmann 	unregister_filesystem(&spufs_type);
86067207b96SArnd Bergmann 	kmem_cache_destroy(spufs_inode_cache);
86167207b96SArnd Bergmann }
86267207b96SArnd Bergmann module_exit(spufs_exit);
86367207b96SArnd Bergmann 
86467207b96SArnd Bergmann MODULE_LICENSE("GPL");
86567207b96SArnd Bergmann MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
86667207b96SArnd Bergmann 
867