blob: c8751936672aa5bfaa73a519b3e25344afaeba6f [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>
37#include <asm/spu_priv1.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050038#include <asm/io.h>
39#include <asm/semaphore.h>
40#include <asm/spu.h>
41#include <asm/uaccess.h>
42
43#include "spufs.h"
44
45static kmem_cache_t *spufs_inode_cache;
arnd@arndb.de0afacde2006-10-24 18:31:18 +020046static char *isolated_loader;
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
53 ei = kmem_cache_alloc(spufs_inode_cache, SLAB_KERNEL);
54 if (!ei)
55 return NULL;
Arnd Bergmann62632032006-10-04 17:26:15 +020056
57 ei->i_gang = NULL;
58 ei->i_ctx = NULL;
59
Arnd Bergmann67207b92005-11-15 15:53:48 -050060 return &ei->vfs_inode;
61}
62
63static void
64spufs_destroy_inode(struct inode *inode)
65{
66 kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
67}
68
69static void
70spufs_init_once(void *p, kmem_cache_t * cachep, unsigned long flags)
71{
72 struct spufs_inode_info *ei = p;
73
74 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
75 SLAB_CTOR_CONSTRUCTOR) {
76 inode_init_once(&ei->vfs_inode);
77 }
78}
79
80static struct inode *
81spufs_new_inode(struct super_block *sb, int mode)
82{
83 struct inode *inode;
84
85 inode = new_inode(sb);
86 if (!inode)
87 goto out;
88
89 inode->i_mode = mode;
90 inode->i_uid = current->fsuid;
91 inode->i_gid = current->fsgid;
Arnd Bergmann67207b92005-11-15 15:53:48 -050092 inode->i_blocks = 0;
93 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
94out:
95 return inode;
96}
97
98static int
99spufs_setattr(struct dentry *dentry, struct iattr *attr)
100{
101 struct inode *inode = dentry->d_inode;
102
Arnd Bergmann67207b92005-11-15 15:53:48 -0500103 if ((attr->ia_valid & ATTR_SIZE) &&
104 (attr->ia_size != inode->i_size))
105 return -EINVAL;
106 return inode_setattr(inode, attr);
107}
108
109
110static int
111spufs_new_file(struct super_block *sb, struct dentry *dentry,
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800112 const struct file_operations *fops, int mode,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500113 struct spu_context *ctx)
114{
115 static struct inode_operations spufs_file_iops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500116 .setattr = spufs_setattr,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500117 };
118 struct inode *inode;
119 int ret;
120
121 ret = -ENOSPC;
122 inode = spufs_new_inode(sb, S_IFREG | mode);
123 if (!inode)
124 goto out;
125
126 ret = 0;
127 inode->i_op = &spufs_file_iops;
128 inode->i_fop = fops;
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700129 inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500130 d_add(dentry, inode);
131out:
132 return ret;
133}
134
135static void
136spufs_delete_inode(struct inode *inode)
137{
Arnd Bergmann62632032006-10-04 17:26:15 +0200138 struct spufs_inode_info *ei = SPUFS_I(inode);
139
140 if (ei->i_ctx)
141 put_spu_context(ei->i_ctx);
142 if (ei->i_gang)
143 put_spu_gang(ei->i_gang);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500144 clear_inode(inode);
145}
146
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100147static void spufs_prune_dir(struct dentry *dir)
148{
149 struct dentry *dentry, *tmp;
Arnd Bergmann62632032006-10-04 17:26:15 +0200150
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800151 mutex_lock(&dir->d_inode->i_mutex);
Andrew Mortonc3a9aea2006-01-09 20:51:24 -0800152 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100153 spin_lock(&dcache_lock);
154 spin_lock(&dentry->d_lock);
155 if (!(d_unhashed(dentry)) && dentry->d_inode) {
156 dget_locked(dentry);
157 __d_drop(dentry);
158 spin_unlock(&dentry->d_lock);
159 simple_unlink(dir->d_inode, dentry);
160 spin_unlock(&dcache_lock);
161 dput(dentry);
162 } else {
163 spin_unlock(&dentry->d_lock);
164 spin_unlock(&dcache_lock);
165 }
166 }
167 shrink_dcache_parent(dir);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800168 mutex_unlock(&dir->d_inode->i_mutex);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100169}
170
Arnd Bergmann62632032006-10-04 17:26:15 +0200171/* Caller must hold parent->i_mutex */
172static int spufs_rmdir(struct inode *parent, struct dentry *dir)
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100173{
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100174 /* remove all entries */
Arnd Bergmann62632032006-10-04 17:26:15 +0200175 spufs_prune_dir(dir);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100176
Arnd Bergmann62632032006-10-04 17:26:15 +0200177 return simple_rmdir(parent, dir);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100178}
179
180static int spufs_fill_dir(struct dentry *dir, struct tree_descr *files,
181 int mode, struct spu_context *ctx)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500182{
183 struct dentry *dentry;
184 int ret;
185
186 while (files->name && files->name[0]) {
187 ret = -ENOMEM;
188 dentry = d_alloc_name(dir, files->name);
189 if (!dentry)
190 goto out;
191 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
192 files->mode & mode, ctx);
193 if (ret)
194 goto out;
195 files++;
196 }
197 return 0;
198out:
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100199 spufs_prune_dir(dir);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500200 return ret;
201}
202
Arnd Bergmann67207b92005-11-15 15:53:48 -0500203static int spufs_dir_close(struct inode *inode, struct file *file)
204{
Michael Ellerman0309f022006-06-19 20:33:22 +0200205 struct spu_context *ctx;
Arnd Bergmann62632032006-10-04 17:26:15 +0200206 struct inode *parent;
207 struct dentry *dir;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500208 int ret;
209
Arnd Bergmann62632032006-10-04 17:26:15 +0200210 dir = file->f_dentry;
211 parent = dir->d_parent->d_inode;
212 ctx = SPUFS_I(dir->d_inode)->i_ctx;
Arnd Bergmannc8ca0632006-01-04 20:31:22 +0100213
Arnd Bergmann62632032006-10-04 17:26:15 +0200214 mutex_lock(&parent->i_mutex);
215 ret = spufs_rmdir(parent, dir);
216 mutex_unlock(&parent->i_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500217 WARN_ON(ret);
Arnd Bergmannc8ca0632006-01-04 20:31:22 +0100218
Michael Ellerman0309f022006-06-19 20:33:22 +0200219 /* We have to give up the mm_struct */
220 spu_forget(ctx);
221
Arnd Bergmann67207b92005-11-15 15:53:48 -0500222 return dcache_dir_close(inode, file);
223}
224
225struct inode_operations spufs_dir_inode_operations = {
226 .lookup = simple_lookup,
227};
228
Arnd Bergmanne80358a2006-01-04 20:31:23 +0100229struct file_operations spufs_context_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500230 .open = dcache_dir_open,
231 .release = spufs_dir_close,
232 .llseek = dcache_dir_lseek,
233 .read = generic_read_dir,
234 .readdir = dcache_readdir,
235 .fsync = simple_sync_file,
236};
237
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200238static int spu_setup_isolated(struct spu_context *ctx)
239{
240 int ret;
241 u64 __iomem *mfc_cntl;
242 u64 sr1;
243 u32 status;
244 unsigned long timeout;
245 const u32 status_loading = SPU_STATUS_RUNNING
246 | SPU_STATUS_ISOLATED_STATE | SPU_STATUS_ISOLATED_LOAD_STATUS;
247
248 if (!isolated_loader)
249 return -ENODEV;
250
251 if ((ret = spu_acquire_runnable(ctx)) != 0)
252 return ret;
253
254 mfc_cntl = &ctx->spu->priv2->mfc_control_RW;
255
256 /* purge the MFC DMA queue to ensure no spurious accesses before we
257 * enter kernel mode */
258 timeout = jiffies + HZ;
259 out_be64(mfc_cntl, MFC_CNTL_PURGE_DMA_REQUEST);
260 while ((in_be64(mfc_cntl) & MFC_CNTL_PURGE_DMA_STATUS_MASK)
261 != MFC_CNTL_PURGE_DMA_COMPLETE) {
262 if (time_after(jiffies, timeout)) {
263 printk(KERN_ERR "%s: timeout flushing MFC DMA queue\n",
264 __FUNCTION__);
265 ret = -EIO;
266 goto out_unlock;
267 }
268 cond_resched();
269 }
270
271 /* put the SPE in kernel mode to allow access to the loader */
272 sr1 = spu_mfc_sr1_get(ctx->spu);
273 sr1 &= ~MFC_STATE1_PROBLEM_STATE_MASK;
274 spu_mfc_sr1_set(ctx->spu, sr1);
275
276 /* start the loader */
277 ctx->ops->signal1_write(ctx, (unsigned long)isolated_loader >> 32);
278 ctx->ops->signal2_write(ctx,
279 (unsigned long)isolated_loader & 0xffffffff);
280
281 ctx->ops->runcntl_write(ctx,
282 SPU_RUNCNTL_RUNNABLE | SPU_RUNCNTL_ISOLATE);
283
284 ret = 0;
285 timeout = jiffies + HZ;
286 while (((status = ctx->ops->status_read(ctx)) & status_loading) ==
287 status_loading) {
288 if (time_after(jiffies, timeout)) {
289 printk(KERN_ERR "%s: timeout waiting for loader\n",
290 __FUNCTION__);
291 ret = -EIO;
292 goto out_drop_priv;
293 }
294 cond_resched();
295 }
296
297 if (!(status & SPU_STATUS_RUNNING)) {
298 /* If isolated LOAD has failed: run SPU, we will get a stop-and
299 * signal later. */
300 pr_debug("%s: isolated LOAD failed\n", __FUNCTION__);
301 ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_RUNNABLE);
302 ret = -EACCES;
303
304 } else if (!(status & SPU_STATUS_ISOLATED_STATE)) {
305 /* This isn't allowed by the CBEA, but check anyway */
306 pr_debug("%s: SPU fell out of isolated mode?\n", __FUNCTION__);
307 ctx->ops->runcntl_write(ctx, SPU_RUNCNTL_STOP);
308 ret = -EINVAL;
309 }
310
311out_drop_priv:
312 /* Finished accessing the loader. Drop kernel mode */
313 sr1 |= MFC_STATE1_PROBLEM_STATE_MASK;
314 spu_mfc_sr1_set(ctx->spu, sr1);
315
316out_unlock:
317 up_write(&ctx->state_sema);
318 return ret;
319}
320
Arnd Bergmann67207b92005-11-15 15:53:48 -0500321static int
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200322spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
323 int mode)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500324{
325 int ret;
326 struct inode *inode;
327 struct spu_context *ctx;
328
329 ret = -ENOSPC;
330 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
331 if (!inode)
332 goto out;
333
334 if (dir->i_mode & S_ISGID) {
335 inode->i_gid = dir->i_gid;
336 inode->i_mode &= S_ISGID;
337 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200338 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500339 SPUFS_I(inode)->i_ctx = ctx;
340 if (!ctx)
341 goto out_iput;
342
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200343 ctx->flags = flags;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200344 if (flags & SPU_CREATE_ISOLATE) {
345 ret = spu_setup_isolated(ctx);
346 if (ret)
347 goto out_iput;
348 }
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200349
Arnd Bergmann67207b92005-11-15 15:53:48 -0500350 inode->i_op = &spufs_dir_inode_operations;
351 inode->i_fop = &simple_dir_operations;
Mark Nutter5737edd2006-10-24 18:31:16 +0200352 if (flags & SPU_CREATE_NOSCHED)
353 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
354 mode, ctx);
355 else
356 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
357
Arnd Bergmann67207b92005-11-15 15:53:48 -0500358 if (ret)
359 goto out_free_ctx;
360
361 d_instantiate(dentry, inode);
362 dget(dentry);
363 dir->i_nlink++;
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100364 dentry->d_inode->i_nlink++;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500365 goto out;
366
367out_free_ctx:
368 put_spu_context(ctx);
369out_iput:
370 iput(inode);
371out:
372 return ret;
373}
374
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100375static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
376{
377 int ret;
378 struct file *filp;
379
380 ret = get_unused_fd();
381 if (ret < 0) {
382 dput(dentry);
383 mntput(mnt);
384 goto out;
385 }
386
387 filp = dentry_open(dentry, mnt, O_RDONLY);
388 if (IS_ERR(filp)) {
389 put_unused_fd(ret);
390 ret = PTR_ERR(filp);
391 goto out;
392 }
393
394 filp->f_op = &spufs_context_fops;
395 fd_install(ret, filp);
396out:
397 return ret;
398}
399
Arnd Bergmann62632032006-10-04 17:26:15 +0200400static int spufs_create_context(struct inode *inode,
401 struct dentry *dentry,
402 struct vfsmount *mnt, int flags, int mode)
403{
404 int ret;
405
Mark Nutter5737edd2006-10-24 18:31:16 +0200406 ret = -EPERM;
407 if ((flags & SPU_CREATE_NOSCHED) &&
408 !capable(CAP_SYS_NICE))
409 goto out_unlock;
410
411 ret = -EINVAL;
412 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
413 == SPU_CREATE_ISOLATE)
414 goto out_unlock;
415
Arnd Bergmann62632032006-10-04 17:26:15 +0200416 ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
417 if (ret)
418 goto out_unlock;
419
420 /*
421 * get references for dget and mntget, will be released
422 * in error path of *_open().
423 */
424 ret = spufs_context_open(dget(dentry), mntget(mnt));
425 if (ret < 0) {
426 WARN_ON(spufs_rmdir(inode, dentry));
427 mutex_unlock(&inode->i_mutex);
428 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
429 goto out;
430 }
431
432out_unlock:
433 mutex_unlock(&inode->i_mutex);
434out:
435 dput(dentry);
436 return ret;
437}
438
439static int spufs_rmgang(struct inode *root, struct dentry *dir)
440{
441 /* FIXME: this fails if the dir is not empty,
442 which causes a leak of gangs. */
443 return simple_rmdir(root, dir);
444}
445
446static int spufs_gang_close(struct inode *inode, struct file *file)
447{
448 struct inode *parent;
449 struct dentry *dir;
450 int ret;
451
452 dir = file->f_dentry;
453 parent = dir->d_parent->d_inode;
454
455 ret = spufs_rmgang(parent, dir);
456 WARN_ON(ret);
457
458 return dcache_dir_close(inode, file);
459}
460
461struct file_operations spufs_gang_fops = {
462 .open = dcache_dir_open,
463 .release = spufs_gang_close,
464 .llseek = dcache_dir_lseek,
465 .read = generic_read_dir,
466 .readdir = dcache_readdir,
467 .fsync = simple_sync_file,
468};
469
470static int
471spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
472{
473 int ret;
474 struct inode *inode;
475 struct spu_gang *gang;
476
477 ret = -ENOSPC;
478 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
479 if (!inode)
480 goto out;
481
482 ret = 0;
483 if (dir->i_mode & S_ISGID) {
484 inode->i_gid = dir->i_gid;
485 inode->i_mode &= S_ISGID;
486 }
487 gang = alloc_spu_gang();
488 SPUFS_I(inode)->i_ctx = NULL;
489 SPUFS_I(inode)->i_gang = gang;
490 if (!gang)
491 goto out_iput;
492
493 inode->i_op = &spufs_dir_inode_operations;
494 inode->i_fop = &simple_dir_operations;
495
496 d_instantiate(dentry, inode);
497 dget(dentry);
498 dir->i_nlink++;
499 dentry->d_inode->i_nlink++;
500 return ret;
501
502out_iput:
503 iput(inode);
504out:
505 return ret;
506}
507
508static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
509{
510 int ret;
511 struct file *filp;
512
513 ret = get_unused_fd();
514 if (ret < 0) {
515 dput(dentry);
516 mntput(mnt);
517 goto out;
518 }
519
520 filp = dentry_open(dentry, mnt, O_RDONLY);
521 if (IS_ERR(filp)) {
522 put_unused_fd(ret);
523 ret = PTR_ERR(filp);
524 goto out;
525 }
526
527 filp->f_op = &spufs_gang_fops;
528 fd_install(ret, filp);
529out:
530 return ret;
531}
532
533static int spufs_create_gang(struct inode *inode,
534 struct dentry *dentry,
535 struct vfsmount *mnt, int mode)
536{
537 int ret;
538
539 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
540 if (ret)
541 goto out;
542
543 /*
544 * get references for dget and mntget, will be released
545 * in error path of *_open().
546 */
547 ret = spufs_gang_open(dget(dentry), mntget(mnt));
548 if (ret < 0)
549 WARN_ON(spufs_rmgang(inode, dentry));
550
551out:
552 mutex_unlock(&inode->i_mutex);
553 dput(dentry);
554 return ret;
555}
556
557
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100558static struct file_system_type spufs_type;
559
Arnd Bergmann62632032006-10-04 17:26:15 +0200560long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500561{
562 struct dentry *dentry;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500563 int ret;
564
Arnd Bergmann67207b92005-11-15 15:53:48 -0500565 ret = -EINVAL;
Arnd Bergmann62632032006-10-04 17:26:15 +0200566 /* check if we are on spufs */
567 if (nd->dentry->d_sb->s_type != &spufs_type)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500568 goto out;
569
Arnd Bergmann62632032006-10-04 17:26:15 +0200570 /* don't accept undefined flags */
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200571 if (flags & (~SPU_CREATE_FLAG_ALL))
arnd@arndb.dec9832942006-06-19 20:33:34 +0200572 goto out;
573
Arnd Bergmann62632032006-10-04 17:26:15 +0200574 /* only threads can be underneath a gang */
575 if (nd->dentry != nd->dentry->d_sb->s_root) {
576 if ((flags & SPU_CREATE_GANG) ||
577 !SPUFS_I(nd->dentry->d_inode)->i_gang)
578 goto out;
579 }
580
Arnd Bergmann67207b92005-11-15 15:53:48 -0500581 dentry = lookup_create(nd, 1);
582 ret = PTR_ERR(dentry);
583 if (IS_ERR(dentry))
584 goto out_dir;
585
586 ret = -EEXIST;
587 if (dentry->d_inode)
588 goto out_dput;
589
590 mode &= ~current->fs->umask;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500591
Arnd Bergmann62632032006-10-04 17:26:15 +0200592 if (flags & SPU_CREATE_GANG)
593 return spufs_create_gang(nd->dentry->d_inode,
594 dentry, nd->mnt, mode);
595 else
596 return spufs_create_context(nd->dentry->d_inode,
597 dentry, nd->mnt, flags, mode);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500598
599out_dput:
600 dput(dentry);
601out_dir:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800602 mutex_unlock(&nd->dentry->d_inode->i_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500603out:
604 return ret;
605}
606
607/* File system initialization */
608enum {
609 Opt_uid, Opt_gid, Opt_err,
610};
611
612static match_table_t spufs_tokens = {
613 { Opt_uid, "uid=%d" },
614 { Opt_gid, "gid=%d" },
615 { Opt_err, NULL },
616};
617
618static int
619spufs_parse_options(char *options, struct inode *root)
620{
621 char *p;
622 substring_t args[MAX_OPT_ARGS];
623
624 while ((p = strsep(&options, ",")) != NULL) {
625 int token, option;
626
627 if (!*p)
628 continue;
629
630 token = match_token(p, spufs_tokens, args);
631 switch (token) {
632 case Opt_uid:
633 if (match_int(&args[0], &option))
634 return 0;
635 root->i_uid = option;
636 break;
637 case Opt_gid:
638 if (match_int(&args[0], &option))
639 return 0;
640 root->i_gid = option;
641 break;
642 default:
643 return 0;
644 }
645 }
646 return 1;
647}
648
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200649static void
650spufs_init_isolated_loader(void)
651{
652 struct device_node *dn;
653 const char *loader;
654 int size;
655
656 dn = of_find_node_by_path("/spu-isolation");
657 if (!dn)
658 return;
659
660 loader = get_property(dn, "loader", &size);
661 if (!loader)
662 return;
663
664 /* kmalloc should align on a 16 byte boundary..* */
665 isolated_loader = kmalloc(size, GFP_KERNEL);
666 if (!isolated_loader)
667 return;
668
669 memcpy(isolated_loader, loader, size);
670 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
671}
672
Arnd Bergmann67207b92005-11-15 15:53:48 -0500673static int
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500674spufs_create_root(struct super_block *sb, void *data)
675{
Arnd Bergmann67207b92005-11-15 15:53:48 -0500676 struct inode *inode;
677 int ret;
678
679 ret = -ENOMEM;
680 inode = spufs_new_inode(sb, S_IFDIR | 0775);
681 if (!inode)
682 goto out;
683
684 inode->i_op = &spufs_dir_inode_operations;
685 inode->i_fop = &simple_dir_operations;
686 SPUFS_I(inode)->i_ctx = NULL;
687
688 ret = -EINVAL;
689 if (!spufs_parse_options(data, inode))
690 goto out_iput;
691
692 ret = -ENOMEM;
693 sb->s_root = d_alloc_root(inode);
694 if (!sb->s_root)
695 goto out_iput;
696
697 return 0;
698out_iput:
699 iput(inode);
700out:
701 return ret;
702}
703
704static int
705spufs_fill_super(struct super_block *sb, void *data, int silent)
706{
707 static struct super_operations s_ops = {
708 .alloc_inode = spufs_alloc_inode,
709 .destroy_inode = spufs_destroy_inode,
710 .statfs = simple_statfs,
711 .delete_inode = spufs_delete_inode,
712 .drop_inode = generic_delete_inode,
713 };
714
715 sb->s_maxbytes = MAX_LFS_FILESIZE;
716 sb->s_blocksize = PAGE_CACHE_SIZE;
717 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
718 sb->s_magic = SPUFS_MAGIC;
719 sb->s_op = &s_ops;
720
721 return spufs_create_root(sb, data);
722}
723
David Howells454e2392006-06-23 02:02:57 -0700724static int
Arnd Bergmann67207b92005-11-15 15:53:48 -0500725spufs_get_sb(struct file_system_type *fstype, int flags,
David Howells454e2392006-06-23 02:02:57 -0700726 const char *name, void *data, struct vfsmount *mnt)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500727{
David Howells454e2392006-06-23 02:02:57 -0700728 return get_sb_single(fstype, flags, data, spufs_fill_super, mnt);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500729}
730
731static struct file_system_type spufs_type = {
732 .owner = THIS_MODULE,
733 .name = "spufs",
734 .get_sb = spufs_get_sb,
735 .kill_sb = kill_litter_super,
736};
737
Arnd Bergmanne78b47a2006-03-27 21:27:40 +0200738static int __init spufs_init(void)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500739{
740 int ret;
741 ret = -ENOMEM;
742 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
743 sizeof(struct spufs_inode_info), 0,
744 SLAB_HWCACHE_ALIGN, spufs_init_once, NULL);
745
746 if (!spufs_inode_cache)
747 goto out;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500748 if (spu_sched_init() != 0) {
749 kmem_cache_destroy(spufs_inode_cache);
750 goto out;
751 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500752 ret = register_filesystem(&spufs_type);
753 if (ret)
754 goto out_cache;
755 ret = register_spu_syscalls(&spufs_calls);
756 if (ret)
757 goto out_fs;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200758
759 spufs_init_isolated_loader();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500760 return 0;
761out_fs:
762 unregister_filesystem(&spufs_type);
763out_cache:
764 kmem_cache_destroy(spufs_inode_cache);
765out:
766 return ret;
767}
768module_init(spufs_init);
769
Arnd Bergmanne78b47a2006-03-27 21:27:40 +0200770static void __exit spufs_exit(void)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500771{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500772 spu_sched_exit();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500773 unregister_spu_syscalls(&spufs_calls);
774 unregister_filesystem(&spufs_type);
775 kmem_cache_destroy(spufs_inode_cache);
776}
777module_exit(spufs_exit);
778
779MODULE_LICENSE("GPL");
780MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
781