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