1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /*
4  * SPU file system
5  *
6  * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
7  *
8  * Author: Arnd Bergmann <arndb@de.ibm.com>
9  */
10 
11 #include <linux/file.h>
12 #include <linux/fs.h>
13 #include <linux/fs_context.h>
14 #include <linux/fs_parser.h>
15 #include <linux/fsnotify.h>
16 #include <linux/backing-dev.h>
17 #include <linux/init.h>
18 #include <linux/ioctl.h>
19 #include <linux/module.h>
20 #include <linux/mount.h>
21 #include <linux/namei.h>
22 #include <linux/pagemap.h>
23 #include <linux/poll.h>
24 #include <linux/slab.h>
25 
26 #include <asm/prom.h>
27 #include <asm/spu.h>
28 #include <asm/spu_priv1.h>
29 #include <linux/uaccess.h>
30 
31 #include "spufs.h"
32 
33 struct spufs_sb_info {
34 	bool debug;
35 };
36 
37 static struct kmem_cache *spufs_inode_cache;
38 char *isolated_loader;
39 static int isolated_loader_size;
40 
41 static struct spufs_sb_info *spufs_get_sb_info(struct super_block *sb)
42 {
43 	return sb->s_fs_info;
44 }
45 
46 static struct inode *
47 spufs_alloc_inode(struct super_block *sb)
48 {
49 	struct spufs_inode_info *ei;
50 
51 	ei = kmem_cache_alloc(spufs_inode_cache, GFP_KERNEL);
52 	if (!ei)
53 		return NULL;
54 
55 	ei->i_gang = NULL;
56 	ei->i_ctx = NULL;
57 	ei->i_openers = 0;
58 
59 	return &ei->vfs_inode;
60 }
61 
62 static void spufs_free_inode(struct inode *inode)
63 {
64 	kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
65 }
66 
67 static void
68 spufs_init_once(void *p)
69 {
70 	struct spufs_inode_info *ei = p;
71 
72 	inode_init_once(&ei->vfs_inode);
73 }
74 
75 static struct inode *
76 spufs_new_inode(struct super_block *sb, umode_t mode)
77 {
78 	struct inode *inode;
79 
80 	inode = new_inode(sb);
81 	if (!inode)
82 		goto out;
83 
84 	inode->i_ino = get_next_ino();
85 	inode->i_mode = mode;
86 	inode->i_uid = current_fsuid();
87 	inode->i_gid = current_fsgid();
88 	inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
89 out:
90 	return inode;
91 }
92 
93 static int
94 spufs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
95 	      struct iattr *attr)
96 {
97 	struct inode *inode = d_inode(dentry);
98 
99 	if ((attr->ia_valid & ATTR_SIZE) &&
100 	    (attr->ia_size != inode->i_size))
101 		return -EINVAL;
102 	setattr_copy(&init_user_ns, inode, attr);
103 	mark_inode_dirty(inode);
104 	return 0;
105 }
106 
107 
108 static int
109 spufs_new_file(struct super_block *sb, struct dentry *dentry,
110 		const struct file_operations *fops, umode_t mode,
111 		size_t size, struct spu_context *ctx)
112 {
113 	static const struct inode_operations spufs_file_iops = {
114 		.setattr = spufs_setattr,
115 	};
116 	struct inode *inode;
117 	int ret;
118 
119 	ret = -ENOSPC;
120 	inode = spufs_new_inode(sb, S_IFREG | mode);
121 	if (!inode)
122 		goto out;
123 
124 	ret = 0;
125 	inode->i_op = &spufs_file_iops;
126 	inode->i_fop = fops;
127 	inode->i_size = size;
128 	inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
129 	d_add(dentry, inode);
130 out:
131 	return ret;
132 }
133 
134 static void
135 spufs_evict_inode(struct inode *inode)
136 {
137 	struct spufs_inode_info *ei = SPUFS_I(inode);
138 	clear_inode(inode);
139 	if (ei->i_ctx)
140 		put_spu_context(ei->i_ctx);
141 	if (ei->i_gang)
142 		put_spu_gang(ei->i_gang);
143 }
144 
145 static void spufs_prune_dir(struct dentry *dir)
146 {
147 	struct dentry *dentry, *tmp;
148 
149 	inode_lock(d_inode(dir));
150 	list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_child) {
151 		spin_lock(&dentry->d_lock);
152 		if (simple_positive(dentry)) {
153 			dget_dlock(dentry);
154 			__d_drop(dentry);
155 			spin_unlock(&dentry->d_lock);
156 			simple_unlink(d_inode(dir), dentry);
157 			/* XXX: what was dcache_lock protecting here? Other
158 			 * filesystems (IB, configfs) release dcache_lock
159 			 * before unlink */
160 			dput(dentry);
161 		} else {
162 			spin_unlock(&dentry->d_lock);
163 		}
164 	}
165 	shrink_dcache_parent(dir);
166 	inode_unlock(d_inode(dir));
167 }
168 
169 /* Caller must hold parent->i_mutex */
170 static int spufs_rmdir(struct inode *parent, struct dentry *dir)
171 {
172 	/* remove all entries */
173 	int res;
174 	spufs_prune_dir(dir);
175 	d_drop(dir);
176 	res = simple_rmdir(parent, dir);
177 	/* We have to give up the mm_struct */
178 	spu_forget(SPUFS_I(d_inode(dir))->i_ctx);
179 	return res;
180 }
181 
182 static int spufs_fill_dir(struct dentry *dir,
183 		const struct spufs_tree_descr *files, umode_t mode,
184 		struct spu_context *ctx)
185 {
186 	while (files->name && files->name[0]) {
187 		int ret;
188 		struct dentry *dentry = d_alloc_name(dir, files->name);
189 		if (!dentry)
190 			return -ENOMEM;
191 		ret = spufs_new_file(dir->d_sb, dentry, files->ops,
192 					files->mode & mode, files->size, ctx);
193 		if (ret)
194 			return ret;
195 		files++;
196 	}
197 	return 0;
198 }
199 
200 static int spufs_dir_close(struct inode *inode, struct file *file)
201 {
202 	struct inode *parent;
203 	struct dentry *dir;
204 	int ret;
205 
206 	dir = file->f_path.dentry;
207 	parent = d_inode(dir->d_parent);
208 
209 	inode_lock_nested(parent, I_MUTEX_PARENT);
210 	ret = spufs_rmdir(parent, dir);
211 	inode_unlock(parent);
212 	WARN_ON(ret);
213 
214 	return dcache_dir_close(inode, file);
215 }
216 
217 const struct file_operations spufs_context_fops = {
218 	.open		= dcache_dir_open,
219 	.release	= spufs_dir_close,
220 	.llseek		= dcache_dir_lseek,
221 	.read		= generic_read_dir,
222 	.iterate_shared	= dcache_readdir,
223 	.fsync		= noop_fsync,
224 };
225 EXPORT_SYMBOL_GPL(spufs_context_fops);
226 
227 static int
228 spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
229 		umode_t mode)
230 {
231 	int ret;
232 	struct inode *inode;
233 	struct spu_context *ctx;
234 
235 	inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
236 	if (!inode)
237 		return -ENOSPC;
238 
239 	if (dir->i_mode & S_ISGID) {
240 		inode->i_gid = dir->i_gid;
241 		inode->i_mode &= S_ISGID;
242 	}
243 	ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
244 	SPUFS_I(inode)->i_ctx = ctx;
245 	if (!ctx) {
246 		iput(inode);
247 		return -ENOSPC;
248 	}
249 
250 	ctx->flags = flags;
251 	inode->i_op = &simple_dir_inode_operations;
252 	inode->i_fop = &simple_dir_operations;
253 
254 	inode_lock(inode);
255 
256 	dget(dentry);
257 	inc_nlink(dir);
258 	inc_nlink(inode);
259 
260 	d_instantiate(dentry, inode);
261 
262 	if (flags & SPU_CREATE_NOSCHED)
263 		ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
264 					 mode, ctx);
265 	else
266 		ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
267 
268 	if (!ret && spufs_get_sb_info(dir->i_sb)->debug)
269 		ret = spufs_fill_dir(dentry, spufs_dir_debug_contents,
270 				mode, ctx);
271 
272 	if (ret)
273 		spufs_rmdir(dir, dentry);
274 
275 	inode_unlock(inode);
276 
277 	return ret;
278 }
279 
280 static int spufs_context_open(struct path *path)
281 {
282 	int ret;
283 	struct file *filp;
284 
285 	ret = get_unused_fd_flags(0);
286 	if (ret < 0)
287 		return ret;
288 
289 	filp = dentry_open(path, O_RDONLY, current_cred());
290 	if (IS_ERR(filp)) {
291 		put_unused_fd(ret);
292 		return PTR_ERR(filp);
293 	}
294 
295 	filp->f_op = &spufs_context_fops;
296 	fd_install(ret, filp);
297 	return ret;
298 }
299 
300 static struct spu_context *
301 spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
302 						struct file *filp)
303 {
304 	struct spu_context *tmp, *neighbor, *err;
305 	int count, node;
306 	int aff_supp;
307 
308 	aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
309 					struct spu, cbe_list))->aff_list);
310 
311 	if (!aff_supp)
312 		return ERR_PTR(-EINVAL);
313 
314 	if (flags & SPU_CREATE_GANG)
315 		return ERR_PTR(-EINVAL);
316 
317 	if (flags & SPU_CREATE_AFFINITY_MEM &&
318 	    gang->aff_ref_ctx &&
319 	    gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
320 		return ERR_PTR(-EEXIST);
321 
322 	if (gang->aff_flags & AFF_MERGED)
323 		return ERR_PTR(-EBUSY);
324 
325 	neighbor = NULL;
326 	if (flags & SPU_CREATE_AFFINITY_SPU) {
327 		if (!filp || filp->f_op != &spufs_context_fops)
328 			return ERR_PTR(-EINVAL);
329 
330 		neighbor = get_spu_context(
331 				SPUFS_I(file_inode(filp))->i_ctx);
332 
333 		if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
334 		    !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
335 		    !list_entry(neighbor->aff_list.next, struct spu_context,
336 		    aff_list)->aff_head) {
337 			err = ERR_PTR(-EEXIST);
338 			goto out_put_neighbor;
339 		}
340 
341 		if (gang != neighbor->gang) {
342 			err = ERR_PTR(-EINVAL);
343 			goto out_put_neighbor;
344 		}
345 
346 		count = 1;
347 		list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
348 			count++;
349 		if (list_empty(&neighbor->aff_list))
350 			count++;
351 
352 		for (node = 0; node < MAX_NUMNODES; node++) {
353 			if ((cbe_spu_info[node].n_spus - atomic_read(
354 				&cbe_spu_info[node].reserved_spus)) >= count)
355 				break;
356 		}
357 
358 		if (node == MAX_NUMNODES) {
359 			err = ERR_PTR(-EEXIST);
360 			goto out_put_neighbor;
361 		}
362 	}
363 
364 	return neighbor;
365 
366 out_put_neighbor:
367 	put_spu_context(neighbor);
368 	return err;
369 }
370 
371 static void
372 spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
373 					struct spu_context *neighbor)
374 {
375 	if (flags & SPU_CREATE_AFFINITY_MEM)
376 		ctx->gang->aff_ref_ctx = ctx;
377 
378 	if (flags & SPU_CREATE_AFFINITY_SPU) {
379 		if (list_empty(&neighbor->aff_list)) {
380 			list_add_tail(&neighbor->aff_list,
381 				&ctx->gang->aff_list_head);
382 			neighbor->aff_head = 1;
383 		}
384 
385 		if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
386 		    || list_entry(neighbor->aff_list.next, struct spu_context,
387 							aff_list)->aff_head) {
388 			list_add(&ctx->aff_list, &neighbor->aff_list);
389 		} else  {
390 			list_add_tail(&ctx->aff_list, &neighbor->aff_list);
391 			if (neighbor->aff_head) {
392 				neighbor->aff_head = 0;
393 				ctx->aff_head = 1;
394 			}
395 		}
396 
397 		if (!ctx->gang->aff_ref_ctx)
398 			ctx->gang->aff_ref_ctx = ctx;
399 	}
400 }
401 
402 static int
403 spufs_create_context(struct inode *inode, struct dentry *dentry,
404 			struct vfsmount *mnt, int flags, umode_t mode,
405 			struct file *aff_filp)
406 {
407 	int ret;
408 	int affinity;
409 	struct spu_gang *gang;
410 	struct spu_context *neighbor;
411 	struct path path = {.mnt = mnt, .dentry = dentry};
412 
413 	if ((flags & SPU_CREATE_NOSCHED) &&
414 	    !capable(CAP_SYS_NICE))
415 		return -EPERM;
416 
417 	if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
418 	    == SPU_CREATE_ISOLATE)
419 		return -EINVAL;
420 
421 	if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
422 		return -ENODEV;
423 
424 	gang = NULL;
425 	neighbor = NULL;
426 	affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
427 	if (affinity) {
428 		gang = SPUFS_I(inode)->i_gang;
429 		if (!gang)
430 			return -EINVAL;
431 		mutex_lock(&gang->aff_mutex);
432 		neighbor = spufs_assert_affinity(flags, gang, aff_filp);
433 		if (IS_ERR(neighbor)) {
434 			ret = PTR_ERR(neighbor);
435 			goto out_aff_unlock;
436 		}
437 	}
438 
439 	ret = spufs_mkdir(inode, dentry, flags, mode & 0777);
440 	if (ret)
441 		goto out_aff_unlock;
442 
443 	if (affinity) {
444 		spufs_set_affinity(flags, SPUFS_I(d_inode(dentry))->i_ctx,
445 								neighbor);
446 		if (neighbor)
447 			put_spu_context(neighbor);
448 	}
449 
450 	ret = spufs_context_open(&path);
451 	if (ret < 0)
452 		WARN_ON(spufs_rmdir(inode, dentry));
453 
454 out_aff_unlock:
455 	if (affinity)
456 		mutex_unlock(&gang->aff_mutex);
457 	return ret;
458 }
459 
460 static int
461 spufs_mkgang(struct inode *dir, struct dentry *dentry, umode_t mode)
462 {
463 	int ret;
464 	struct inode *inode;
465 	struct spu_gang *gang;
466 
467 	ret = -ENOSPC;
468 	inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
469 	if (!inode)
470 		goto out;
471 
472 	ret = 0;
473 	if (dir->i_mode & S_ISGID) {
474 		inode->i_gid = dir->i_gid;
475 		inode->i_mode &= S_ISGID;
476 	}
477 	gang = alloc_spu_gang();
478 	SPUFS_I(inode)->i_ctx = NULL;
479 	SPUFS_I(inode)->i_gang = gang;
480 	if (!gang) {
481 		ret = -ENOMEM;
482 		goto out_iput;
483 	}
484 
485 	inode->i_op = &simple_dir_inode_operations;
486 	inode->i_fop = &simple_dir_operations;
487 
488 	d_instantiate(dentry, inode);
489 	inc_nlink(dir);
490 	inc_nlink(d_inode(dentry));
491 	return ret;
492 
493 out_iput:
494 	iput(inode);
495 out:
496 	return ret;
497 }
498 
499 static int spufs_gang_open(struct path *path)
500 {
501 	int ret;
502 	struct file *filp;
503 
504 	ret = get_unused_fd_flags(0);
505 	if (ret < 0)
506 		return ret;
507 
508 	/*
509 	 * get references for dget and mntget, will be released
510 	 * in error path of *_open().
511 	 */
512 	filp = dentry_open(path, O_RDONLY, current_cred());
513 	if (IS_ERR(filp)) {
514 		put_unused_fd(ret);
515 		return PTR_ERR(filp);
516 	}
517 
518 	filp->f_op = &simple_dir_operations;
519 	fd_install(ret, filp);
520 	return ret;
521 }
522 
523 static int spufs_create_gang(struct inode *inode,
524 			struct dentry *dentry,
525 			struct vfsmount *mnt, umode_t mode)
526 {
527 	struct path path = {.mnt = mnt, .dentry = dentry};
528 	int ret;
529 
530 	ret = spufs_mkgang(inode, dentry, mode & 0777);
531 	if (!ret) {
532 		ret = spufs_gang_open(&path);
533 		if (ret < 0) {
534 			int err = simple_rmdir(inode, dentry);
535 			WARN_ON(err);
536 		}
537 	}
538 	return ret;
539 }
540 
541 
542 static struct file_system_type spufs_type;
543 
544 long spufs_create(struct path *path, struct dentry *dentry,
545 		unsigned int flags, umode_t mode, struct file *filp)
546 {
547 	struct inode *dir = d_inode(path->dentry);
548 	int ret;
549 
550 	/* check if we are on spufs */
551 	if (path->dentry->d_sb->s_type != &spufs_type)
552 		return -EINVAL;
553 
554 	/* don't accept undefined flags */
555 	if (flags & (~SPU_CREATE_FLAG_ALL))
556 		return -EINVAL;
557 
558 	/* only threads can be underneath a gang */
559 	if (path->dentry != path->dentry->d_sb->s_root)
560 		if ((flags & SPU_CREATE_GANG) || !SPUFS_I(dir)->i_gang)
561 			return -EINVAL;
562 
563 	mode &= ~current_umask();
564 
565 	if (flags & SPU_CREATE_GANG)
566 		ret = spufs_create_gang(dir, dentry, path->mnt, mode);
567 	else
568 		ret = spufs_create_context(dir, dentry, path->mnt, flags, mode,
569 					    filp);
570 	if (ret >= 0)
571 		fsnotify_mkdir(dir, dentry);
572 
573 	return ret;
574 }
575 
576 /* File system initialization */
577 struct spufs_fs_context {
578 	kuid_t	uid;
579 	kgid_t	gid;
580 	umode_t	mode;
581 };
582 
583 enum {
584 	Opt_uid, Opt_gid, Opt_mode, Opt_debug,
585 };
586 
587 static const struct fs_parameter_spec spufs_fs_parameters[] = {
588 	fsparam_u32	("gid",				Opt_gid),
589 	fsparam_u32oct	("mode",			Opt_mode),
590 	fsparam_u32	("uid",				Opt_uid),
591 	fsparam_flag	("debug",			Opt_debug),
592 	{}
593 };
594 
595 static int spufs_show_options(struct seq_file *m, struct dentry *root)
596 {
597 	struct spufs_sb_info *sbi = spufs_get_sb_info(root->d_sb);
598 	struct inode *inode = root->d_inode;
599 
600 	if (!uid_eq(inode->i_uid, GLOBAL_ROOT_UID))
601 		seq_printf(m, ",uid=%u",
602 			   from_kuid_munged(&init_user_ns, inode->i_uid));
603 	if (!gid_eq(inode->i_gid, GLOBAL_ROOT_GID))
604 		seq_printf(m, ",gid=%u",
605 			   from_kgid_munged(&init_user_ns, inode->i_gid));
606 	if ((inode->i_mode & S_IALLUGO) != 0775)
607 		seq_printf(m, ",mode=%o", inode->i_mode);
608 	if (sbi->debug)
609 		seq_puts(m, ",debug");
610 	return 0;
611 }
612 
613 static int spufs_parse_param(struct fs_context *fc, struct fs_parameter *param)
614 {
615 	struct spufs_fs_context *ctx = fc->fs_private;
616 	struct spufs_sb_info *sbi = fc->s_fs_info;
617 	struct fs_parse_result result;
618 	kuid_t uid;
619 	kgid_t gid;
620 	int opt;
621 
622 	opt = fs_parse(fc, spufs_fs_parameters, param, &result);
623 	if (opt < 0)
624 		return opt;
625 
626 	switch (opt) {
627 	case Opt_uid:
628 		uid = make_kuid(current_user_ns(), result.uint_32);
629 		if (!uid_valid(uid))
630 			return invalf(fc, "Unknown uid");
631 		ctx->uid = uid;
632 		break;
633 	case Opt_gid:
634 		gid = make_kgid(current_user_ns(), result.uint_32);
635 		if (!gid_valid(gid))
636 			return invalf(fc, "Unknown gid");
637 		ctx->gid = gid;
638 		break;
639 	case Opt_mode:
640 		ctx->mode = result.uint_32 & S_IALLUGO;
641 		break;
642 	case Opt_debug:
643 		sbi->debug = true;
644 		break;
645 	}
646 
647 	return 0;
648 }
649 
650 static void spufs_exit_isolated_loader(void)
651 {
652 	free_pages((unsigned long) isolated_loader,
653 			get_order(isolated_loader_size));
654 }
655 
656 static void
657 spufs_init_isolated_loader(void)
658 {
659 	struct device_node *dn;
660 	const char *loader;
661 	int size;
662 
663 	dn = of_find_node_by_path("/spu-isolation");
664 	if (!dn)
665 		return;
666 
667 	loader = of_get_property(dn, "loader", &size);
668 	if (!loader)
669 		return;
670 
671 	/* the loader must be align on a 16 byte boundary */
672 	isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
673 	if (!isolated_loader)
674 		return;
675 
676 	isolated_loader_size = size;
677 	memcpy(isolated_loader, loader, size);
678 	printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
679 }
680 
681 static int spufs_create_root(struct super_block *sb, struct fs_context *fc)
682 {
683 	struct spufs_fs_context *ctx = fc->fs_private;
684 	struct inode *inode;
685 
686 	if (!spu_management_ops)
687 		return -ENODEV;
688 
689 	inode = spufs_new_inode(sb, S_IFDIR | ctx->mode);
690 	if (!inode)
691 		return -ENOMEM;
692 
693 	inode->i_uid = ctx->uid;
694 	inode->i_gid = ctx->gid;
695 	inode->i_op = &simple_dir_inode_operations;
696 	inode->i_fop = &simple_dir_operations;
697 	SPUFS_I(inode)->i_ctx = NULL;
698 	inc_nlink(inode);
699 
700 	sb->s_root = d_make_root(inode);
701 	if (!sb->s_root)
702 		return -ENOMEM;
703 	return 0;
704 }
705 
706 static const struct super_operations spufs_ops = {
707 	.alloc_inode	= spufs_alloc_inode,
708 	.free_inode	= spufs_free_inode,
709 	.statfs		= simple_statfs,
710 	.evict_inode	= spufs_evict_inode,
711 	.show_options	= spufs_show_options,
712 };
713 
714 static int spufs_fill_super(struct super_block *sb, struct fs_context *fc)
715 {
716 	sb->s_maxbytes = MAX_LFS_FILESIZE;
717 	sb->s_blocksize = PAGE_SIZE;
718 	sb->s_blocksize_bits = PAGE_SHIFT;
719 	sb->s_magic = SPUFS_MAGIC;
720 	sb->s_op = &spufs_ops;
721 
722 	return spufs_create_root(sb, fc);
723 }
724 
725 static int spufs_get_tree(struct fs_context *fc)
726 {
727 	return get_tree_single(fc, spufs_fill_super);
728 }
729 
730 static void spufs_free_fc(struct fs_context *fc)
731 {
732 	kfree(fc->s_fs_info);
733 }
734 
735 static const struct fs_context_operations spufs_context_ops = {
736 	.free		= spufs_free_fc,
737 	.parse_param	= spufs_parse_param,
738 	.get_tree	= spufs_get_tree,
739 };
740 
741 static int spufs_init_fs_context(struct fs_context *fc)
742 {
743 	struct spufs_fs_context *ctx;
744 	struct spufs_sb_info *sbi;
745 
746 	ctx = kzalloc(sizeof(struct spufs_fs_context), GFP_KERNEL);
747 	if (!ctx)
748 		goto nomem;
749 
750 	sbi = kzalloc(sizeof(struct spufs_sb_info), GFP_KERNEL);
751 	if (!sbi)
752 		goto nomem_ctx;
753 
754 	ctx->uid = current_uid();
755 	ctx->gid = current_gid();
756 	ctx->mode = 0755;
757 
758 	fc->fs_private = ctx;
759 	fc->s_fs_info = sbi;
760 	fc->ops = &spufs_context_ops;
761 	return 0;
762 
763 nomem_ctx:
764 	kfree(ctx);
765 nomem:
766 	return -ENOMEM;
767 }
768 
769 static struct file_system_type spufs_type = {
770 	.owner = THIS_MODULE,
771 	.name = "spufs",
772 	.init_fs_context = spufs_init_fs_context,
773 	.parameters	= spufs_fs_parameters,
774 	.kill_sb = kill_litter_super,
775 };
776 MODULE_ALIAS_FS("spufs");
777 
778 static int __init spufs_init(void)
779 {
780 	int ret;
781 
782 	ret = -ENODEV;
783 	if (!spu_management_ops)
784 		goto out;
785 
786 	ret = -ENOMEM;
787 	spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
788 			sizeof(struct spufs_inode_info), 0,
789 			SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT, spufs_init_once);
790 
791 	if (!spufs_inode_cache)
792 		goto out;
793 	ret = spu_sched_init();
794 	if (ret)
795 		goto out_cache;
796 	ret = register_spu_syscalls(&spufs_calls);
797 	if (ret)
798 		goto out_sched;
799 	ret = register_filesystem(&spufs_type);
800 	if (ret)
801 		goto out_syscalls;
802 
803 	spufs_init_isolated_loader();
804 
805 	return 0;
806 
807 out_syscalls:
808 	unregister_spu_syscalls(&spufs_calls);
809 out_sched:
810 	spu_sched_exit();
811 out_cache:
812 	kmem_cache_destroy(spufs_inode_cache);
813 out:
814 	return ret;
815 }
816 module_init(spufs_init);
817 
818 static void __exit spufs_exit(void)
819 {
820 	spu_sched_exit();
821 	spufs_exit_isolated_loader();
822 	unregister_spu_syscalls(&spufs_calls);
823 	unregister_filesystem(&spufs_type);
824 	kmem_cache_destroy(spufs_inode_cache);
825 }
826 module_exit(spufs_exit);
827 
828 MODULE_LICENSE("GPL");
829 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
830 
831