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