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