blob: 11098747d09b42445d090423f04e82e44025be28 [file] [log] [blame]
Arnd Bergmann67207b92005-11-15 15:53:48 -05001/*
2 * SPU file system
3 *
4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
5 *
6 * Author: Arnd Bergmann <arndb@de.ibm.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/file.h>
24#include <linux/fs.h>
25#include <linux/backing-dev.h>
26#include <linux/init.h>
27#include <linux/ioctl.h>
28#include <linux/module.h>
Arnd Bergmann346f4d32006-01-04 20:31:26 +010029#include <linux/mount.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050030#include <linux/namei.h>
31#include <linux/pagemap.h>
32#include <linux/poll.h>
33#include <linux/slab.h>
34#include <linux/parser.h>
35
arnd@arndb.de0afacde2006-10-24 18:31:18 +020036#include <asm/prom.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050037#include <asm/semaphore.h>
38#include <asm/spu.h>
Jeremy Kerrccf17e92007-04-23 21:08:29 +020039#include <asm/spu_priv1.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050040#include <asm/uaccess.h>
41
42#include "spufs.h"
43
Christoph Lametere18b8902006-12-06 20:33:20 -080044static struct kmem_cache *spufs_inode_cache;
Jeremy Kerrc6730ed2006-11-20 18:45:10 +010045char *isolated_loader;
Sebastian Siewior8b0d3122007-09-19 14:38:12 +100046static int isolated_loader_size;
Arnd Bergmann67207b92005-11-15 15:53:48 -050047
Arnd Bergmann67207b92005-11-15 15:53:48 -050048static struct inode *
49spufs_alloc_inode(struct super_block *sb)
50{
51 struct spufs_inode_info *ei;
52
Christoph Lametere94b1762006-12-06 20:33:17 -080053 ei = kmem_cache_alloc(spufs_inode_cache, GFP_KERNEL);
Arnd Bergmann67207b92005-11-15 15:53:48 -050054 if (!ei)
55 return NULL;
Arnd Bergmann62632032006-10-04 17:26:15 +020056
57 ei->i_gang = NULL;
58 ei->i_ctx = NULL;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +020059 ei->i_openers = 0;
Arnd Bergmann62632032006-10-04 17:26:15 +020060
Arnd Bergmann67207b92005-11-15 15:53:48 -050061 return &ei->vfs_inode;
62}
63
64static void
65spufs_destroy_inode(struct inode *inode)
66{
67 kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
68}
69
70static void
Christoph Lametere18b8902006-12-06 20:33:20 -080071spufs_init_once(void *p, struct kmem_cache * cachep, unsigned long flags)
Arnd Bergmann67207b92005-11-15 15:53:48 -050072{
73 struct spufs_inode_info *ei = p;
74
Christoph Lametera35afb82007-05-16 22:10:57 -070075 inode_init_once(&ei->vfs_inode);
Arnd Bergmann67207b92005-11-15 15:53:48 -050076}
77
78static struct inode *
79spufs_new_inode(struct super_block *sb, int mode)
80{
81 struct inode *inode;
82
83 inode = new_inode(sb);
84 if (!inode)
85 goto out;
86
87 inode->i_mode = mode;
88 inode->i_uid = current->fsuid;
89 inode->i_gid = current->fsgid;
Arnd Bergmann67207b92005-11-15 15:53:48 -050090 inode->i_blocks = 0;
91 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
92out:
93 return inode;
94}
95
96static int
97spufs_setattr(struct dentry *dentry, struct iattr *attr)
98{
99 struct inode *inode = dentry->d_inode;
100
Arnd Bergmann67207b92005-11-15 15:53:48 -0500101 if ((attr->ia_valid & ATTR_SIZE) &&
102 (attr->ia_size != inode->i_size))
103 return -EINVAL;
104 return inode_setattr(inode, attr);
105}
106
107
108static int
109spufs_new_file(struct super_block *sb, struct dentry *dentry,
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800110 const struct file_operations *fops, int mode,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500111 struct spu_context *ctx)
112{
113 static struct inode_operations spufs_file_iops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500114 .setattr = spufs_setattr,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500115 };
116 struct inode *inode;
117 int ret;
118
119 ret = -ENOSPC;
120 inode = spufs_new_inode(sb, S_IFREG | mode);
121 if (!inode)
122 goto out;
123
124 ret = 0;
125 inode->i_op = &spufs_file_iops;
126 inode->i_fop = fops;
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700127 inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500128 d_add(dentry, inode);
129out:
130 return ret;
131}
132
133static void
134spufs_delete_inode(struct inode *inode)
135{
Arnd Bergmann62632032006-10-04 17:26:15 +0200136 struct spufs_inode_info *ei = SPUFS_I(inode);
137
138 if (ei->i_ctx)
139 put_spu_context(ei->i_ctx);
140 if (ei->i_gang)
141 put_spu_gang(ei->i_gang);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500142 clear_inode(inode);
143}
144
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100145static void spufs_prune_dir(struct dentry *dir)
146{
147 struct dentry *dentry, *tmp;
Arnd Bergmann62632032006-10-04 17:26:15 +0200148
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800149 mutex_lock(&dir->d_inode->i_mutex);
Andrew Mortonc3a9aea2006-01-09 20:51:24 -0800150 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100151 spin_lock(&dcache_lock);
152 spin_lock(&dentry->d_lock);
153 if (!(d_unhashed(dentry)) && dentry->d_inode) {
154 dget_locked(dentry);
155 __d_drop(dentry);
156 spin_unlock(&dentry->d_lock);
157 simple_unlink(dir->d_inode, dentry);
158 spin_unlock(&dcache_lock);
159 dput(dentry);
160 } else {
161 spin_unlock(&dentry->d_lock);
162 spin_unlock(&dcache_lock);
163 }
164 }
165 shrink_dcache_parent(dir);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800166 mutex_unlock(&dir->d_inode->i_mutex);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100167}
168
Arnd Bergmann62632032006-10-04 17:26:15 +0200169/* Caller must hold parent->i_mutex */
170static int spufs_rmdir(struct inode *parent, struct dentry *dir)
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100171{
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100172 /* remove all entries */
Arnd Bergmann62632032006-10-04 17:26:15 +0200173 spufs_prune_dir(dir);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100174
Arnd Bergmann62632032006-10-04 17:26:15 +0200175 return simple_rmdir(parent, dir);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100176}
177
178static int spufs_fill_dir(struct dentry *dir, struct tree_descr *files,
179 int mode, struct spu_context *ctx)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500180{
Sebastian Siewior87873c82007-06-06 14:03:58 +1000181 struct dentry *dentry, *tmp;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500182 int ret;
183
184 while (files->name && files->name[0]) {
185 ret = -ENOMEM;
186 dentry = d_alloc_name(dir, files->name);
187 if (!dentry)
188 goto out;
189 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
190 files->mode & mode, ctx);
191 if (ret)
192 goto out;
193 files++;
194 }
195 return 0;
196out:
Sebastian Siewior87873c82007-06-06 14:03:58 +1000197 /*
198 * remove all children from dir. dir->inode is not set so don't
199 * just simply use spufs_prune_dir() and panic afterwards :)
200 * dput() looks like it will do the right thing:
201 * - dec parent's ref counter
202 * - remove child from parent's child list
203 * - free child's inode if possible
204 * - free child
205 */
206 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
207 dput(dentry);
208 }
209
210 shrink_dcache_parent(dir);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500211 return ret;
212}
213
Arnd Bergmann67207b92005-11-15 15:53:48 -0500214static int spufs_dir_close(struct inode *inode, struct file *file)
215{
Michael Ellerman0309f022006-06-19 20:33:22 +0200216 struct spu_context *ctx;
Arnd Bergmann62632032006-10-04 17:26:15 +0200217 struct inode *parent;
218 struct dentry *dir;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500219 int ret;
220
Josef Sipekb4d1ab52006-12-08 02:37:30 -0800221 dir = file->f_path.dentry;
Arnd Bergmann62632032006-10-04 17:26:15 +0200222 parent = dir->d_parent->d_inode;
223 ctx = SPUFS_I(dir->d_inode)->i_ctx;
Arnd Bergmannc8ca0632006-01-04 20:31:22 +0100224
Arnd Bergmann62632032006-10-04 17:26:15 +0200225 mutex_lock(&parent->i_mutex);
226 ret = spufs_rmdir(parent, dir);
227 mutex_unlock(&parent->i_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500228 WARN_ON(ret);
Arnd Bergmannc8ca0632006-01-04 20:31:22 +0100229
Michael Ellerman0309f022006-06-19 20:33:22 +0200230 /* We have to give up the mm_struct */
231 spu_forget(ctx);
232
Arnd Bergmann67207b92005-11-15 15:53:48 -0500233 return dcache_dir_close(inode, file);
234}
235
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800236const struct file_operations spufs_context_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500237 .open = dcache_dir_open,
238 .release = spufs_dir_close,
239 .llseek = dcache_dir_lseek,
240 .read = generic_read_dir,
241 .readdir = dcache_readdir,
242 .fsync = simple_sync_file,
243};
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100244EXPORT_SYMBOL_GPL(spufs_context_fops);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500245
246static int
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200247spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
248 int mode)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500249{
250 int ret;
251 struct inode *inode;
252 struct spu_context *ctx;
253
254 ret = -ENOSPC;
255 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
256 if (!inode)
257 goto out;
258
259 if (dir->i_mode & S_ISGID) {
260 inode->i_gid = dir->i_gid;
261 inode->i_mode &= S_ISGID;
262 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200263 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500264 SPUFS_I(inode)->i_ctx = ctx;
265 if (!ctx)
266 goto out_iput;
267
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200268 ctx->flags = flags;
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000269 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500270 inode->i_fop = &simple_dir_operations;
Mark Nutter5737edd2006-10-24 18:31:16 +0200271 if (flags & SPU_CREATE_NOSCHED)
272 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
273 mode, ctx);
274 else
275 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
276
Arnd Bergmann67207b92005-11-15 15:53:48 -0500277 if (ret)
278 goto out_free_ctx;
279
280 d_instantiate(dentry, inode);
281 dget(dentry);
282 dir->i_nlink++;
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100283 dentry->d_inode->i_nlink++;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500284 goto out;
285
286out_free_ctx:
Sebastian Siewior89df0082007-06-04 23:26:51 +1000287 spu_forget(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500288 put_spu_context(ctx);
289out_iput:
290 iput(inode);
291out:
292 return ret;
293}
294
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100295static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
296{
297 int ret;
298 struct file *filp;
299
300 ret = get_unused_fd();
301 if (ret < 0) {
302 dput(dentry);
303 mntput(mnt);
304 goto out;
305 }
306
307 filp = dentry_open(dentry, mnt, O_RDONLY);
308 if (IS_ERR(filp)) {
309 put_unused_fd(ret);
310 ret = PTR_ERR(filp);
311 goto out;
312 }
313
314 filp->f_op = &spufs_context_fops;
315 fd_install(ret, filp);
316out:
317 return ret;
318}
319
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200320static struct spu_context *
321spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
322 struct file *filp)
323{
324 struct spu_context *tmp, *neighbor;
325 int count, node;
326 int aff_supp;
327
328 aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
329 struct spu, cbe_list))->aff_list);
330
331 if (!aff_supp)
332 return ERR_PTR(-EINVAL);
333
334 if (flags & SPU_CREATE_GANG)
335 return ERR_PTR(-EINVAL);
336
337 if (flags & SPU_CREATE_AFFINITY_MEM &&
338 gang->aff_ref_ctx &&
339 gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
340 return ERR_PTR(-EEXIST);
341
342 if (gang->aff_flags & AFF_MERGED)
343 return ERR_PTR(-EBUSY);
344
345 neighbor = NULL;
346 if (flags & SPU_CREATE_AFFINITY_SPU) {
347 if (!filp || filp->f_op != &spufs_context_fops)
348 return ERR_PTR(-EINVAL);
349
350 neighbor = get_spu_context(
351 SPUFS_I(filp->f_dentry->d_inode)->i_ctx);
352
353 if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
354 !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
355 !list_entry(neighbor->aff_list.next, struct spu_context,
356 aff_list)->aff_head)
357 return ERR_PTR(-EEXIST);
358
359 if (gang != neighbor->gang)
360 return ERR_PTR(-EINVAL);
361
362 count = 1;
363 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
364 count++;
365 if (list_empty(&neighbor->aff_list))
366 count++;
367
368 for (node = 0; node < MAX_NUMNODES; node++) {
369 if ((cbe_spu_info[node].n_spus - atomic_read(
370 &cbe_spu_info[node].reserved_spus)) >= count)
371 break;
372 }
373
374 if (node == MAX_NUMNODES)
375 return ERR_PTR(-EEXIST);
376 }
377
378 return neighbor;
379}
380
381static void
382spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
383 struct spu_context *neighbor)
384{
385 if (flags & SPU_CREATE_AFFINITY_MEM)
386 ctx->gang->aff_ref_ctx = ctx;
387
388 if (flags & SPU_CREATE_AFFINITY_SPU) {
389 if (list_empty(&neighbor->aff_list)) {
390 list_add_tail(&neighbor->aff_list,
391 &ctx->gang->aff_list_head);
392 neighbor->aff_head = 1;
393 }
394
395 if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
396 || list_entry(neighbor->aff_list.next, struct spu_context,
397 aff_list)->aff_head) {
398 list_add(&ctx->aff_list, &neighbor->aff_list);
399 } else {
400 list_add_tail(&ctx->aff_list, &neighbor->aff_list);
401 if (neighbor->aff_head) {
402 neighbor->aff_head = 0;
403 ctx->aff_head = 1;
404 }
405 }
406
407 if (!ctx->gang->aff_ref_ctx)
408 ctx->gang->aff_ref_ctx = ctx;
409 }
410}
411
412static int
413spufs_create_context(struct inode *inode, struct dentry *dentry,
414 struct vfsmount *mnt, int flags, int mode,
415 struct file *aff_filp)
Arnd Bergmann62632032006-10-04 17:26:15 +0200416{
417 int ret;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200418 int affinity;
419 struct spu_gang *gang;
420 struct spu_context *neighbor;
Arnd Bergmann62632032006-10-04 17:26:15 +0200421
Mark Nutter5737edd2006-10-24 18:31:16 +0200422 ret = -EPERM;
423 if ((flags & SPU_CREATE_NOSCHED) &&
424 !capable(CAP_SYS_NICE))
425 goto out_unlock;
426
427 ret = -EINVAL;
428 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
429 == SPU_CREATE_ISOLATE)
430 goto out_unlock;
431
Jeremy Kerrbd2e5f82006-11-27 19:18:52 +0100432 ret = -ENODEV;
433 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
434 goto out_unlock;
435
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200436 gang = NULL;
437 neighbor = NULL;
438 affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
439 if (affinity) {
440 gang = SPUFS_I(inode)->i_gang;
441 ret = -EINVAL;
442 if (!gang)
443 goto out_unlock;
444 mutex_lock(&gang->aff_mutex);
445 neighbor = spufs_assert_affinity(flags, gang, aff_filp);
446 if (IS_ERR(neighbor)) {
447 ret = PTR_ERR(neighbor);
448 goto out_aff_unlock;
449 }
450 }
451
Arnd Bergmann62632032006-10-04 17:26:15 +0200452 ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
453 if (ret)
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200454 goto out_aff_unlock;
455
456 if (affinity)
457 spufs_set_affinity(flags, SPUFS_I(dentry->d_inode)->i_ctx,
458 neighbor);
Arnd Bergmann62632032006-10-04 17:26:15 +0200459
460 /*
461 * get references for dget and mntget, will be released
462 * in error path of *_open().
463 */
464 ret = spufs_context_open(dget(dentry), mntget(mnt));
465 if (ret < 0) {
466 WARN_ON(spufs_rmdir(inode, dentry));
467 mutex_unlock(&inode->i_mutex);
468 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
469 goto out;
470 }
471
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200472out_aff_unlock:
473 if (affinity)
474 mutex_unlock(&gang->aff_mutex);
Arnd Bergmann62632032006-10-04 17:26:15 +0200475out_unlock:
476 mutex_unlock(&inode->i_mutex);
477out:
478 dput(dentry);
479 return ret;
480}
481
Arnd Bergmann62632032006-10-04 17:26:15 +0200482static int
483spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
484{
485 int ret;
486 struct inode *inode;
487 struct spu_gang *gang;
488
489 ret = -ENOSPC;
490 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
491 if (!inode)
492 goto out;
493
494 ret = 0;
495 if (dir->i_mode & S_ISGID) {
496 inode->i_gid = dir->i_gid;
497 inode->i_mode &= S_ISGID;
498 }
499 gang = alloc_spu_gang();
500 SPUFS_I(inode)->i_ctx = NULL;
501 SPUFS_I(inode)->i_gang = gang;
502 if (!gang)
503 goto out_iput;
504
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000505 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann62632032006-10-04 17:26:15 +0200506 inode->i_fop = &simple_dir_operations;
507
508 d_instantiate(dentry, inode);
Arnd Bergmann62632032006-10-04 17:26:15 +0200509 dir->i_nlink++;
510 dentry->d_inode->i_nlink++;
511 return ret;
512
513out_iput:
514 iput(inode);
515out:
516 return ret;
517}
518
519static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
520{
521 int ret;
522 struct file *filp;
523
524 ret = get_unused_fd();
525 if (ret < 0) {
526 dput(dentry);
527 mntput(mnt);
528 goto out;
529 }
530
531 filp = dentry_open(dentry, mnt, O_RDONLY);
532 if (IS_ERR(filp)) {
533 put_unused_fd(ret);
534 ret = PTR_ERR(filp);
535 goto out;
536 }
537
Jeremy Kerr877907d2007-06-04 23:26:51 +1000538 filp->f_op = &simple_dir_operations;
Arnd Bergmann62632032006-10-04 17:26:15 +0200539 fd_install(ret, filp);
540out:
541 return ret;
542}
543
544static int spufs_create_gang(struct inode *inode,
545 struct dentry *dentry,
546 struct vfsmount *mnt, int mode)
547{
548 int ret;
549
550 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
551 if (ret)
552 goto out;
553
554 /*
555 * get references for dget and mntget, will be released
556 * in error path of *_open().
557 */
558 ret = spufs_gang_open(dget(dentry), mntget(mnt));
Jeremy Kerr877907d2007-06-04 23:26:51 +1000559 if (ret < 0) {
560 int err = simple_rmdir(inode, dentry);
561 WARN_ON(err);
562 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200563
564out:
565 mutex_unlock(&inode->i_mutex);
566 dput(dentry);
567 return ret;
568}
569
570
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100571static struct file_system_type spufs_type;
572
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200573long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode,
574 struct file *filp)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500575{
576 struct dentry *dentry;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500577 int ret;
578
Arnd Bergmann67207b92005-11-15 15:53:48 -0500579 ret = -EINVAL;
Arnd Bergmann62632032006-10-04 17:26:15 +0200580 /* check if we are on spufs */
581 if (nd->dentry->d_sb->s_type != &spufs_type)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500582 goto out;
583
Arnd Bergmann62632032006-10-04 17:26:15 +0200584 /* don't accept undefined flags */
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200585 if (flags & (~SPU_CREATE_FLAG_ALL))
arnd@arndb.dec9832942006-06-19 20:33:34 +0200586 goto out;
587
Arnd Bergmann62632032006-10-04 17:26:15 +0200588 /* only threads can be underneath a gang */
589 if (nd->dentry != nd->dentry->d_sb->s_root) {
590 if ((flags & SPU_CREATE_GANG) ||
591 !SPUFS_I(nd->dentry->d_inode)->i_gang)
592 goto out;
593 }
594
Arnd Bergmann67207b92005-11-15 15:53:48 -0500595 dentry = lookup_create(nd, 1);
596 ret = PTR_ERR(dentry);
597 if (IS_ERR(dentry))
598 goto out_dir;
599
600 ret = -EEXIST;
601 if (dentry->d_inode)
602 goto out_dput;
603
604 mode &= ~current->fs->umask;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500605
Arnd Bergmann62632032006-10-04 17:26:15 +0200606 if (flags & SPU_CREATE_GANG)
607 return spufs_create_gang(nd->dentry->d_inode,
608 dentry, nd->mnt, mode);
609 else
610 return spufs_create_context(nd->dentry->d_inode,
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200611 dentry, nd->mnt, flags, mode, filp);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500612
613out_dput:
614 dput(dentry);
615out_dir:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800616 mutex_unlock(&nd->dentry->d_inode->i_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500617out:
618 return ret;
619}
620
621/* File system initialization */
622enum {
Jeremy Kerrf11f5ee2007-04-23 21:08:23 +0200623 Opt_uid, Opt_gid, Opt_mode, Opt_err,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500624};
625
626static match_table_t spufs_tokens = {
Jeremy Kerrf11f5ee2007-04-23 21:08:23 +0200627 { Opt_uid, "uid=%d" },
628 { Opt_gid, "gid=%d" },
629 { Opt_mode, "mode=%o" },
630 { Opt_err, NULL },
Arnd Bergmann67207b92005-11-15 15:53:48 -0500631};
632
633static int
634spufs_parse_options(char *options, struct inode *root)
635{
636 char *p;
637 substring_t args[MAX_OPT_ARGS];
638
639 while ((p = strsep(&options, ",")) != NULL) {
640 int token, option;
641
642 if (!*p)
643 continue;
644
645 token = match_token(p, spufs_tokens, args);
646 switch (token) {
647 case Opt_uid:
648 if (match_int(&args[0], &option))
649 return 0;
650 root->i_uid = option;
651 break;
652 case Opt_gid:
653 if (match_int(&args[0], &option))
654 return 0;
655 root->i_gid = option;
656 break;
Jeremy Kerrf11f5ee2007-04-23 21:08:23 +0200657 case Opt_mode:
658 if (match_octal(&args[0], &option))
659 return 0;
660 root->i_mode = option | S_IFDIR;
661 break;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500662 default:
663 return 0;
664 }
665 }
666 return 1;
667}
668
Akinobu Mitadb1384b402007-04-23 21:08:20 +0200669static void spufs_exit_isolated_loader(void)
670{
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000671 free_pages((unsigned long) isolated_loader,
672 get_order(isolated_loader_size));
Akinobu Mitadb1384b402007-04-23 21:08:20 +0200673}
674
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200675static void
676spufs_init_isolated_loader(void)
677{
678 struct device_node *dn;
679 const char *loader;
680 int size;
681
682 dn = of_find_node_by_path("/spu-isolation");
683 if (!dn)
684 return;
685
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000686 loader = of_get_property(dn, "loader", &size);
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200687 if (!loader)
688 return;
689
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000690 /* the loader must be align on a 16 byte boundary */
691 isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200692 if (!isolated_loader)
693 return;
694
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000695 isolated_loader_size = size;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200696 memcpy(isolated_loader, loader, size);
697 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
698}
699
Arnd Bergmann67207b92005-11-15 15:53:48 -0500700static int
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500701spufs_create_root(struct super_block *sb, void *data)
702{
Arnd Bergmann67207b92005-11-15 15:53:48 -0500703 struct inode *inode;
704 int ret;
705
Arnd Bergmann8f18a152007-06-04 23:26:51 +1000706 ret = -ENODEV;
707 if (!spu_management_ops)
708 goto out;
709
Arnd Bergmann67207b92005-11-15 15:53:48 -0500710 ret = -ENOMEM;
711 inode = spufs_new_inode(sb, S_IFDIR | 0775);
712 if (!inode)
713 goto out;
714
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000715 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500716 inode->i_fop = &simple_dir_operations;
717 SPUFS_I(inode)->i_ctx = NULL;
718
719 ret = -EINVAL;
720 if (!spufs_parse_options(data, inode))
721 goto out_iput;
722
723 ret = -ENOMEM;
724 sb->s_root = d_alloc_root(inode);
725 if (!sb->s_root)
726 goto out_iput;
727
728 return 0;
729out_iput:
730 iput(inode);
731out:
732 return ret;
733}
734
735static int
736spufs_fill_super(struct super_block *sb, void *data, int silent)
737{
738 static struct super_operations s_ops = {
739 .alloc_inode = spufs_alloc_inode,
740 .destroy_inode = spufs_destroy_inode,
741 .statfs = simple_statfs,
742 .delete_inode = spufs_delete_inode,
743 .drop_inode = generic_delete_inode,
744 };
745
746 sb->s_maxbytes = MAX_LFS_FILESIZE;
747 sb->s_blocksize = PAGE_CACHE_SIZE;
748 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
749 sb->s_magic = SPUFS_MAGIC;
750 sb->s_op = &s_ops;
751
752 return spufs_create_root(sb, data);
753}
754
David Howells454e2392006-06-23 02:02:57 -0700755static int
Arnd Bergmann67207b92005-11-15 15:53:48 -0500756spufs_get_sb(struct file_system_type *fstype, int flags,
David Howells454e2392006-06-23 02:02:57 -0700757 const char *name, void *data, struct vfsmount *mnt)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500758{
David Howells454e2392006-06-23 02:02:57 -0700759 return get_sb_single(fstype, flags, data, spufs_fill_super, mnt);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500760}
761
762static struct file_system_type spufs_type = {
763 .owner = THIS_MODULE,
764 .name = "spufs",
765 .get_sb = spufs_get_sb,
766 .kill_sb = kill_litter_super,
767};
768
Arnd Bergmanne78b47a2006-03-27 21:27:40 +0200769static int __init spufs_init(void)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500770{
771 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100772
Jeremy Kerrccf17e92007-04-23 21:08:29 +0200773 ret = -ENODEV;
774 if (!spu_management_ops)
775 goto out;
776
Arnd Bergmann67207b92005-11-15 15:53:48 -0500777 ret = -ENOMEM;
778 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
779 sizeof(struct spufs_inode_info), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900780 SLAB_HWCACHE_ALIGN, spufs_init_once);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500781
782 if (!spufs_inode_cache)
783 goto out;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200784 ret = spu_sched_init();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500785 if (ret)
786 goto out_cache;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200787 ret = register_filesystem(&spufs_type);
788 if (ret)
789 goto out_sched;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500790 ret = register_spu_syscalls(&spufs_calls);
791 if (ret)
792 goto out_fs;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200793
794 spufs_init_isolated_loader();
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100795
Arnd Bergmann67207b92005-11-15 15:53:48 -0500796 return 0;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200797
Arnd Bergmann67207b92005-11-15 15:53:48 -0500798out_fs:
799 unregister_filesystem(&spufs_type);
Akinobu Mitac99c1992007-04-23 21:08:19 +0200800out_sched:
801 spu_sched_exit();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500802out_cache:
803 kmem_cache_destroy(spufs_inode_cache);
804out:
805 return ret;
806}
807module_init(spufs_init);
808
Arnd Bergmanne78b47a2006-03-27 21:27:40 +0200809static void __exit spufs_exit(void)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500810{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500811 spu_sched_exit();
Akinobu Mitadb1384b402007-04-23 21:08:20 +0200812 spufs_exit_isolated_loader();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500813 unregister_spu_syscalls(&spufs_calls);
814 unregister_filesystem(&spufs_type);
815 kmem_cache_destroy(spufs_inode_cache);
816}
817module_exit(spufs_exit);
818
819MODULE_LICENSE("GPL");
820MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
821