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