xref: /linux/arch/powerpc/platforms/cell/spufs/inode.c (revision be239684b18e1cdcafcf8c7face4a2f562c745ad)
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/of.h>
25 #include <linux/seq_file.h>
26 #include <linux/slab.h>
27 
28 #include <asm/spu.h>
29 #include <asm/spu_priv1.h>
30 #include <linux/uaccess.h>
31 
32 #include "spufs.h"
33 
34 struct spufs_sb_info {
35 	bool debug;
36 };
37 
38 static struct kmem_cache *spufs_inode_cache;
39 char *isolated_loader;
40 static int isolated_loader_size;
41 
42 static struct spufs_sb_info *spufs_get_sb_info(struct super_block *sb)
43 {
44 	return sb->s_fs_info;
45 }
46 
47 static struct inode *
48 spufs_alloc_inode(struct super_block *sb)
49 {
50 	struct spufs_inode_info *ei;
51 
52 	ei = kmem_cache_alloc(spufs_inode_cache, GFP_KERNEL);
53 	if (!ei)
54 		return NULL;
55 
56 	ei->i_gang = NULL;
57 	ei->i_ctx = NULL;
58 	ei->i_openers = 0;
59 
60 	return &ei->vfs_inode;
61 }
62 
63 static void spufs_free_inode(struct inode *inode)
64 {
65 	kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
66 }
67 
68 static void
69 spufs_init_once(void *p)
70 {
71 	struct spufs_inode_info *ei = p;
72 
73 	inode_init_once(&ei->vfs_inode);
74 }
75 
76 static struct inode *
77 spufs_new_inode(struct super_block *sb, umode_t mode)
78 {
79 	struct inode *inode;
80 
81 	inode = new_inode(sb);
82 	if (!inode)
83 		goto out;
84 
85 	inode->i_ino = get_next_ino();
86 	inode->i_mode = mode;
87 	inode->i_uid = current_fsuid();
88 	inode->i_gid = current_fsgid();
89 	simple_inode_init_ts(inode);
90 out:
91 	return inode;
92 }
93 
94 static int
95 spufs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
96 	      struct iattr *attr)
97 {
98 	struct inode *inode = d_inode(dentry);
99 
100 	if ((attr->ia_valid & ATTR_SIZE) &&
101 	    (attr->ia_size != inode->i_size))
102 		return -EINVAL;
103 	setattr_copy(&nop_mnt_idmap, inode, attr);
104 	mark_inode_dirty(inode);
105 	return 0;
106 }
107 
108 
109 static int
110 spufs_new_file(struct super_block *sb, struct dentry *dentry,
111 		const struct file_operations *fops, umode_t mode,
112 		size_t size, struct spu_context *ctx)
113 {
114 	static const struct inode_operations spufs_file_iops = {
115 		.setattr = spufs_setattr,
116 	};
117 	struct inode *inode;
118 	int ret;
119 
120 	ret = -ENOSPC;
121 	inode = spufs_new_inode(sb, S_IFREG | mode);
122 	if (!inode)
123 		goto out;
124 
125 	ret = 0;
126 	inode->i_op = &spufs_file_iops;
127 	inode->i_fop = fops;
128 	inode->i_size = size;
129 	inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
130 	d_add(dentry, inode);
131 out:
132 	return ret;
133 }
134 
135 static void
136 spufs_evict_inode(struct inode *inode)
137 {
138 	struct spufs_inode_info *ei = SPUFS_I(inode);
139 	clear_inode(inode);
140 	if (ei->i_ctx)
141 		put_spu_context(ei->i_ctx);
142 	if (ei->i_gang)
143 		put_spu_gang(ei->i_gang);
144 }
145 
146 static void spufs_prune_dir(struct dentry *dir)
147 {
148 	struct dentry *dentry;
149 	struct hlist_node *n;
150 
151 	inode_lock(d_inode(dir));
152 	hlist_for_each_entry_safe(dentry, n, &dir->d_children, d_sib) {
153 		spin_lock(&dentry->d_lock);
154 		if (simple_positive(dentry)) {
155 			dget_dlock(dentry);
156 			__d_drop(dentry);
157 			spin_unlock(&dentry->d_lock);
158 			simple_unlink(d_inode(dir), dentry);
159 			/* XXX: what was dcache_lock protecting here? Other
160 			 * filesystems (IB, configfs) release dcache_lock
161 			 * before unlink */
162 			dput(dentry);
163 		} else {
164 			spin_unlock(&dentry->d_lock);
165 		}
166 	}
167 	shrink_dcache_parent(dir);
168 	inode_unlock(d_inode(dir));
169 }
170 
171 /* Caller must hold parent->i_mutex */
172 static int spufs_rmdir(struct inode *parent, struct dentry *dir)
173 {
174 	/* remove all entries */
175 	int res;
176 	spufs_prune_dir(dir);
177 	d_drop(dir);
178 	res = simple_rmdir(parent, dir);
179 	/* We have to give up the mm_struct */
180 	spu_forget(SPUFS_I(d_inode(dir))->i_ctx);
181 	return res;
182 }
183 
184 static int spufs_fill_dir(struct dentry *dir,
185 		const struct spufs_tree_descr *files, umode_t mode,
186 		struct spu_context *ctx)
187 {
188 	while (files->name && files->name[0]) {
189 		int ret;
190 		struct dentry *dentry = d_alloc_name(dir, files->name);
191 		if (!dentry)
192 			return -ENOMEM;
193 		ret = spufs_new_file(dir->d_sb, dentry, files->ops,
194 					files->mode & mode, files->size, ctx);
195 		if (ret)
196 			return ret;
197 		files++;
198 	}
199 	return 0;
200 }
201 
202 static int spufs_dir_close(struct inode *inode, struct file *file)
203 {
204 	struct inode *parent;
205 	struct dentry *dir;
206 	int ret;
207 
208 	dir = file->f_path.dentry;
209 	parent = d_inode(dir->d_parent);
210 
211 	inode_lock_nested(parent, I_MUTEX_PARENT);
212 	ret = spufs_rmdir(parent, dir);
213 	inode_unlock(parent);
214 	WARN_ON(ret);
215 
216 	return dcache_dir_close(inode, file);
217 }
218 
219 const struct file_operations spufs_context_fops = {
220 	.open		= dcache_dir_open,
221 	.release	= spufs_dir_close,
222 	.llseek		= dcache_dir_lseek,
223 	.read		= generic_read_dir,
224 	.iterate_shared	= dcache_readdir,
225 	.fsync		= noop_fsync,
226 };
227 EXPORT_SYMBOL_GPL(spufs_context_fops);
228 
229 static int
230 spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
231 		umode_t mode)
232 {
233 	int ret;
234 	struct inode *inode;
235 	struct spu_context *ctx;
236 
237 	inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
238 	if (!inode)
239 		return -ENOSPC;
240 
241 	inode_init_owner(&nop_mnt_idmap, inode, dir, mode | S_IFDIR);
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(const 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 	inode_init_owner(&nop_mnt_idmap, inode, dir, mode | S_IFDIR);
473 	gang = alloc_spu_gang();
474 	SPUFS_I(inode)->i_ctx = NULL;
475 	SPUFS_I(inode)->i_gang = gang;
476 	if (!gang) {
477 		ret = -ENOMEM;
478 		goto out_iput;
479 	}
480 
481 	inode->i_op = &simple_dir_inode_operations;
482 	inode->i_fop = &simple_dir_operations;
483 
484 	d_instantiate(dentry, inode);
485 	inc_nlink(dir);
486 	inc_nlink(d_inode(dentry));
487 	return ret;
488 
489 out_iput:
490 	iput(inode);
491 out:
492 	return ret;
493 }
494 
495 static int spufs_gang_open(const struct path *path)
496 {
497 	int ret;
498 	struct file *filp;
499 
500 	ret = get_unused_fd_flags(0);
501 	if (ret < 0)
502 		return ret;
503 
504 	/*
505 	 * get references for dget and mntget, will be released
506 	 * in error path of *_open().
507 	 */
508 	filp = dentry_open(path, O_RDONLY, current_cred());
509 	if (IS_ERR(filp)) {
510 		put_unused_fd(ret);
511 		return PTR_ERR(filp);
512 	}
513 
514 	filp->f_op = &simple_dir_operations;
515 	fd_install(ret, filp);
516 	return ret;
517 }
518 
519 static int spufs_create_gang(struct inode *inode,
520 			struct dentry *dentry,
521 			struct vfsmount *mnt, umode_t mode)
522 {
523 	struct path path = {.mnt = mnt, .dentry = dentry};
524 	int ret;
525 
526 	ret = spufs_mkgang(inode, dentry, mode & 0777);
527 	if (!ret) {
528 		ret = spufs_gang_open(&path);
529 		if (ret < 0) {
530 			int err = simple_rmdir(inode, dentry);
531 			WARN_ON(err);
532 		}
533 	}
534 	return ret;
535 }
536 
537 
538 static struct file_system_type spufs_type;
539 
540 long spufs_create(const struct path *path, struct dentry *dentry,
541 		unsigned int flags, umode_t mode, struct file *filp)
542 {
543 	struct inode *dir = d_inode(path->dentry);
544 	int ret;
545 
546 	/* check if we are on spufs */
547 	if (path->dentry->d_sb->s_type != &spufs_type)
548 		return -EINVAL;
549 
550 	/* don't accept undefined flags */
551 	if (flags & (~SPU_CREATE_FLAG_ALL))
552 		return -EINVAL;
553 
554 	/* only threads can be underneath a gang */
555 	if (path->dentry != path->dentry->d_sb->s_root)
556 		if ((flags & SPU_CREATE_GANG) || !SPUFS_I(dir)->i_gang)
557 			return -EINVAL;
558 
559 	mode &= ~current_umask();
560 
561 	if (flags & SPU_CREATE_GANG)
562 		ret = spufs_create_gang(dir, dentry, path->mnt, mode);
563 	else
564 		ret = spufs_create_context(dir, dentry, path->mnt, flags, mode,
565 					    filp);
566 	if (ret >= 0)
567 		fsnotify_mkdir(dir, dentry);
568 
569 	return ret;
570 }
571 
572 /* File system initialization */
573 struct spufs_fs_context {
574 	kuid_t	uid;
575 	kgid_t	gid;
576 	umode_t	mode;
577 };
578 
579 enum {
580 	Opt_uid, Opt_gid, Opt_mode, Opt_debug,
581 };
582 
583 static const struct fs_parameter_spec spufs_fs_parameters[] = {
584 	fsparam_u32	("gid",				Opt_gid),
585 	fsparam_u32oct	("mode",			Opt_mode),
586 	fsparam_u32	("uid",				Opt_uid),
587 	fsparam_flag	("debug",			Opt_debug),
588 	{}
589 };
590 
591 static int spufs_show_options(struct seq_file *m, struct dentry *root)
592 {
593 	struct spufs_sb_info *sbi = spufs_get_sb_info(root->d_sb);
594 	struct inode *inode = root->d_inode;
595 
596 	if (!uid_eq(inode->i_uid, GLOBAL_ROOT_UID))
597 		seq_printf(m, ",uid=%u",
598 			   from_kuid_munged(&init_user_ns, inode->i_uid));
599 	if (!gid_eq(inode->i_gid, GLOBAL_ROOT_GID))
600 		seq_printf(m, ",gid=%u",
601 			   from_kgid_munged(&init_user_ns, inode->i_gid));
602 	if ((inode->i_mode & S_IALLUGO) != 0775)
603 		seq_printf(m, ",mode=%o", inode->i_mode);
604 	if (sbi->debug)
605 		seq_puts(m, ",debug");
606 	return 0;
607 }
608 
609 static int spufs_parse_param(struct fs_context *fc, struct fs_parameter *param)
610 {
611 	struct spufs_fs_context *ctx = fc->fs_private;
612 	struct spufs_sb_info *sbi = fc->s_fs_info;
613 	struct fs_parse_result result;
614 	kuid_t uid;
615 	kgid_t gid;
616 	int opt;
617 
618 	opt = fs_parse(fc, spufs_fs_parameters, param, &result);
619 	if (opt < 0)
620 		return opt;
621 
622 	switch (opt) {
623 	case Opt_uid:
624 		uid = make_kuid(current_user_ns(), result.uint_32);
625 		if (!uid_valid(uid))
626 			return invalf(fc, "Unknown uid");
627 		ctx->uid = uid;
628 		break;
629 	case Opt_gid:
630 		gid = make_kgid(current_user_ns(), result.uint_32);
631 		if (!gid_valid(gid))
632 			return invalf(fc, "Unknown gid");
633 		ctx->gid = gid;
634 		break;
635 	case Opt_mode:
636 		ctx->mode = result.uint_32 & S_IALLUGO;
637 		break;
638 	case Opt_debug:
639 		sbi->debug = true;
640 		break;
641 	}
642 
643 	return 0;
644 }
645 
646 static void spufs_exit_isolated_loader(void)
647 {
648 	free_pages((unsigned long) isolated_loader,
649 			get_order(isolated_loader_size));
650 }
651 
652 static void __init
653 spufs_init_isolated_loader(void)
654 {
655 	struct device_node *dn;
656 	const char *loader;
657 	int size;
658 
659 	dn = of_find_node_by_path("/spu-isolation");
660 	if (!dn)
661 		return;
662 
663 	loader = of_get_property(dn, "loader", &size);
664 	of_node_put(dn);
665 	if (!loader)
666 		return;
667 
668 	/* the loader must be align on a 16 byte boundary */
669 	isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
670 	if (!isolated_loader)
671 		return;
672 
673 	isolated_loader_size = size;
674 	memcpy(isolated_loader, loader, size);
675 	printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
676 }
677 
678 static int spufs_create_root(struct super_block *sb, struct fs_context *fc)
679 {
680 	struct spufs_fs_context *ctx = fc->fs_private;
681 	struct inode *inode;
682 
683 	if (!spu_management_ops)
684 		return -ENODEV;
685 
686 	inode = spufs_new_inode(sb, S_IFDIR | ctx->mode);
687 	if (!inode)
688 		return -ENOMEM;
689 
690 	inode->i_uid = ctx->uid;
691 	inode->i_gid = ctx->gid;
692 	inode->i_op = &simple_dir_inode_operations;
693 	inode->i_fop = &simple_dir_operations;
694 	SPUFS_I(inode)->i_ctx = NULL;
695 	inc_nlink(inode);
696 
697 	sb->s_root = d_make_root(inode);
698 	if (!sb->s_root)
699 		return -ENOMEM;
700 	return 0;
701 }
702 
703 static const struct super_operations spufs_ops = {
704 	.alloc_inode	= spufs_alloc_inode,
705 	.free_inode	= spufs_free_inode,
706 	.statfs		= simple_statfs,
707 	.evict_inode	= spufs_evict_inode,
708 	.show_options	= spufs_show_options,
709 };
710 
711 static int spufs_fill_super(struct super_block *sb, struct fs_context *fc)
712 {
713 	sb->s_maxbytes = MAX_LFS_FILESIZE;
714 	sb->s_blocksize = PAGE_SIZE;
715 	sb->s_blocksize_bits = PAGE_SHIFT;
716 	sb->s_magic = SPUFS_MAGIC;
717 	sb->s_op = &spufs_ops;
718 
719 	return spufs_create_root(sb, fc);
720 }
721 
722 static int spufs_get_tree(struct fs_context *fc)
723 {
724 	return get_tree_single(fc, spufs_fill_super);
725 }
726 
727 static void spufs_free_fc(struct fs_context *fc)
728 {
729 	kfree(fc->s_fs_info);
730 }
731 
732 static const struct fs_context_operations spufs_context_ops = {
733 	.free		= spufs_free_fc,
734 	.parse_param	= spufs_parse_param,
735 	.get_tree	= spufs_get_tree,
736 };
737 
738 static int spufs_init_fs_context(struct fs_context *fc)
739 {
740 	struct spufs_fs_context *ctx;
741 	struct spufs_sb_info *sbi;
742 
743 	ctx = kzalloc(sizeof(struct spufs_fs_context), GFP_KERNEL);
744 	if (!ctx)
745 		goto nomem;
746 
747 	sbi = kzalloc(sizeof(struct spufs_sb_info), GFP_KERNEL);
748 	if (!sbi)
749 		goto nomem_ctx;
750 
751 	ctx->uid = current_uid();
752 	ctx->gid = current_gid();
753 	ctx->mode = 0755;
754 
755 	fc->fs_private = ctx;
756 	fc->s_fs_info = sbi;
757 	fc->ops = &spufs_context_ops;
758 	return 0;
759 
760 nomem_ctx:
761 	kfree(ctx);
762 nomem:
763 	return -ENOMEM;
764 }
765 
766 static struct file_system_type spufs_type = {
767 	.owner = THIS_MODULE,
768 	.name = "spufs",
769 	.init_fs_context = spufs_init_fs_context,
770 	.parameters	= spufs_fs_parameters,
771 	.kill_sb = kill_litter_super,
772 };
773 MODULE_ALIAS_FS("spufs");
774 
775 static int __init spufs_init(void)
776 {
777 	int ret;
778 
779 	ret = -ENODEV;
780 	if (!spu_management_ops)
781 		goto out;
782 
783 	ret = -ENOMEM;
784 	spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
785 			sizeof(struct spufs_inode_info), 0,
786 			SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT, spufs_init_once);
787 
788 	if (!spufs_inode_cache)
789 		goto out;
790 	ret = spu_sched_init();
791 	if (ret)
792 		goto out_cache;
793 	ret = register_spu_syscalls(&spufs_calls);
794 	if (ret)
795 		goto out_sched;
796 	ret = register_filesystem(&spufs_type);
797 	if (ret)
798 		goto out_syscalls;
799 
800 	spufs_init_isolated_loader();
801 
802 	return 0;
803 
804 out_syscalls:
805 	unregister_spu_syscalls(&spufs_calls);
806 out_sched:
807 	spu_sched_exit();
808 out_cache:
809 	kmem_cache_destroy(spufs_inode_cache);
810 out:
811 	return ret;
812 }
813 module_init(spufs_init);
814 
815 static void __exit spufs_exit(void)
816 {
817 	spu_sched_exit();
818 	spufs_exit_isolated_loader();
819 	unregister_spu_syscalls(&spufs_calls);
820 	unregister_filesystem(&spufs_type);
821 	kmem_cache_destroy(spufs_inode_cache);
822 }
823 module_exit(spufs_exit);
824 
825 MODULE_LICENSE("GPL");
826 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
827 
828