blob: 6d1228c66c5e59bbff35e1abcc75b94f23e0740d [file] [log] [blame]
Jan Blunck4ac91372008-02-14 19:34:32 -08001
Arnd Bergmann67207b92005-11-15 15:53:48 -05002/*
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/backing-dev.h>
27#include <linux/init.h>
28#include <linux/ioctl.h>
29#include <linux/module.h>
Arnd Bergmann346f4d32006-01-04 20:31:26 +010030#include <linux/mount.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050031#include <linux/namei.h>
32#include <linux/pagemap.h>
33#include <linux/poll.h>
34#include <linux/slab.h>
35#include <linux/parser.h>
36
arnd@arndb.de0afacde2006-10-24 18:31:18 +020037#include <asm/prom.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050038#include <asm/semaphore.h>
39#include <asm/spu.h>
Jeremy Kerrccf17e92007-04-23 21:08:29 +020040#include <asm/spu_priv1.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050041#include <asm/uaccess.h>
42
43#include "spufs.h"
44
Christoph Lametere18b8902006-12-06 20:33:20 -080045static struct kmem_cache *spufs_inode_cache;
Jeremy Kerrc6730ed2006-11-20 18:45:10 +010046char *isolated_loader;
Sebastian Siewior8b0d3122007-09-19 14:38:12 +100047static int isolated_loader_size;
Arnd Bergmann67207b92005-11-15 15:53:48 -050048
Arnd Bergmann67207b92005-11-15 15:53:48 -050049static struct inode *
50spufs_alloc_inode(struct super_block *sb)
51{
52 struct spufs_inode_info *ei;
53
Christoph Lametere94b1762006-12-06 20:33:17 -080054 ei = kmem_cache_alloc(spufs_inode_cache, GFP_KERNEL);
Arnd Bergmann67207b92005-11-15 15:53:48 -050055 if (!ei)
56 return NULL;
Arnd Bergmann62632032006-10-04 17:26:15 +020057
58 ei->i_gang = NULL;
59 ei->i_ctx = NULL;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +020060 ei->i_openers = 0;
Arnd Bergmann62632032006-10-04 17:26:15 +020061
Arnd Bergmann67207b92005-11-15 15:53:48 -050062 return &ei->vfs_inode;
63}
64
65static void
66spufs_destroy_inode(struct inode *inode)
67{
68 kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
69}
70
71static void
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -070072spufs_init_once(struct kmem_cache *cachep, void *p)
Arnd Bergmann67207b92005-11-15 15:53:48 -050073{
74 struct spufs_inode_info *ei = p;
75
Christoph Lametera35afb82007-05-16 22:10:57 -070076 inode_init_once(&ei->vfs_inode);
Arnd Bergmann67207b92005-11-15 15:53:48 -050077}
78
79static struct inode *
80spufs_new_inode(struct super_block *sb, int mode)
81{
82 struct inode *inode;
83
84 inode = new_inode(sb);
85 if (!inode)
86 goto out;
87
88 inode->i_mode = mode;
89 inode->i_uid = current->fsuid;
90 inode->i_gid = current->fsgid;
Arnd Bergmann67207b92005-11-15 15:53:48 -050091 inode->i_blocks = 0;
92 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
93out:
94 return inode;
95}
96
97static int
98spufs_setattr(struct dentry *dentry, struct iattr *attr)
99{
100 struct inode *inode = dentry->d_inode;
101
Arnd Bergmann67207b92005-11-15 15:53:48 -0500102 if ((attr->ia_valid & ATTR_SIZE) &&
103 (attr->ia_size != inode->i_size))
104 return -EINVAL;
105 return inode_setattr(inode, attr);
106}
107
108
109static int
110spufs_new_file(struct super_block *sb, struct dentry *dentry,
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800111 const struct file_operations *fops, int mode,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500112 struct spu_context *ctx)
113{
114 static struct inode_operations spufs_file_iops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500115 .setattr = spufs_setattr,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500116 };
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;
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700128 inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500129 d_add(dentry, inode);
130out:
131 return ret;
132}
133
134static void
135spufs_delete_inode(struct inode *inode)
136{
Arnd Bergmann62632032006-10-04 17:26:15 +0200137 struct spufs_inode_info *ei = SPUFS_I(inode);
138
139 if (ei->i_ctx)
140 put_spu_context(ei->i_ctx);
141 if (ei->i_gang)
142 put_spu_gang(ei->i_gang);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500143 clear_inode(inode);
144}
145
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100146static void spufs_prune_dir(struct dentry *dir)
147{
148 struct dentry *dentry, *tmp;
Arnd Bergmann62632032006-10-04 17:26:15 +0200149
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800150 mutex_lock(&dir->d_inode->i_mutex);
Andrew Mortonc3a9aea2006-01-09 20:51:24 -0800151 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100152 spin_lock(&dcache_lock);
153 spin_lock(&dentry->d_lock);
154 if (!(d_unhashed(dentry)) && dentry->d_inode) {
155 dget_locked(dentry);
156 __d_drop(dentry);
157 spin_unlock(&dentry->d_lock);
158 simple_unlink(dir->d_inode, dentry);
159 spin_unlock(&dcache_lock);
160 dput(dentry);
161 } else {
162 spin_unlock(&dentry->d_lock);
163 spin_unlock(&dcache_lock);
164 }
165 }
166 shrink_dcache_parent(dir);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800167 mutex_unlock(&dir->d_inode->i_mutex);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100168}
169
Arnd Bergmann62632032006-10-04 17:26:15 +0200170/* Caller must hold parent->i_mutex */
171static int spufs_rmdir(struct inode *parent, struct dentry *dir)
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100172{
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100173 /* remove all entries */
Arnd Bergmann62632032006-10-04 17:26:15 +0200174 spufs_prune_dir(dir);
Jeremy Kerrc443aca2007-11-16 13:32:23 +1100175 d_drop(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{
Sebastian Siewior87873c82007-06-06 14:03:58 +1000183 struct dentry *dentry, *tmp;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500184 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:
Sebastian Siewior87873c82007-06-06 14:03:58 +1000199 /*
200 * remove all children from dir. dir->inode is not set so don't
201 * just simply use spufs_prune_dir() and panic afterwards :)
202 * dput() looks like it will do the right thing:
203 * - dec parent's ref counter
204 * - remove child from parent's child list
205 * - free child's inode if possible
206 * - free child
207 */
208 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
209 dput(dentry);
210 }
211
212 shrink_dcache_parent(dir);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500213 return ret;
214}
215
Arnd Bergmann67207b92005-11-15 15:53:48 -0500216static int spufs_dir_close(struct inode *inode, struct file *file)
217{
Michael Ellerman0309f022006-06-19 20:33:22 +0200218 struct spu_context *ctx;
Arnd Bergmann62632032006-10-04 17:26:15 +0200219 struct inode *parent;
220 struct dentry *dir;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500221 int ret;
222
Josef Sipekb4d1ab52006-12-08 02:37:30 -0800223 dir = file->f_path.dentry;
Arnd Bergmann62632032006-10-04 17:26:15 +0200224 parent = dir->d_parent->d_inode;
225 ctx = SPUFS_I(dir->d_inode)->i_ctx;
Arnd Bergmannc8ca0632006-01-04 20:31:22 +0100226
Arnd Bergmann62632032006-10-04 17:26:15 +0200227 mutex_lock(&parent->i_mutex);
228 ret = spufs_rmdir(parent, dir);
229 mutex_unlock(&parent->i_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500230 WARN_ON(ret);
Arnd Bergmannc8ca0632006-01-04 20:31:22 +0100231
Michael Ellerman0309f022006-06-19 20:33:22 +0200232 /* We have to give up the mm_struct */
233 spu_forget(ctx);
234
Arnd Bergmann67207b92005-11-15 15:53:48 -0500235 return dcache_dir_close(inode, file);
236}
237
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800238const struct file_operations spufs_context_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500239 .open = dcache_dir_open,
240 .release = spufs_dir_close,
241 .llseek = dcache_dir_lseek,
242 .read = generic_read_dir,
243 .readdir = dcache_readdir,
244 .fsync = simple_sync_file,
245};
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100246EXPORT_SYMBOL_GPL(spufs_context_fops);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500247
248static int
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200249spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
250 int mode)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500251{
252 int ret;
253 struct inode *inode;
254 struct spu_context *ctx;
255
256 ret = -ENOSPC;
257 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
258 if (!inode)
259 goto out;
260
261 if (dir->i_mode & S_ISGID) {
262 inode->i_gid = dir->i_gid;
263 inode->i_mode &= S_ISGID;
264 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200265 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500266 SPUFS_I(inode)->i_ctx = ctx;
267 if (!ctx)
268 goto out_iput;
269
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200270 ctx->flags = flags;
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000271 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500272 inode->i_fop = &simple_dir_operations;
Mark Nutter5737edd2006-10-24 18:31:16 +0200273 if (flags & SPU_CREATE_NOSCHED)
274 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
275 mode, ctx);
276 else
277 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
278
Arnd Bergmann67207b92005-11-15 15:53:48 -0500279 if (ret)
280 goto out_free_ctx;
281
282 d_instantiate(dentry, inode);
283 dget(dentry);
284 dir->i_nlink++;
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100285 dentry->d_inode->i_nlink++;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500286 goto out;
287
288out_free_ctx:
Sebastian Siewior89df0082007-06-04 23:26:51 +1000289 spu_forget(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500290 put_spu_context(ctx);
291out_iput:
292 iput(inode);
293out:
294 return ret;
295}
296
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100297static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
298{
299 int ret;
300 struct file *filp;
301
302 ret = get_unused_fd();
303 if (ret < 0) {
304 dput(dentry);
305 mntput(mnt);
306 goto out;
307 }
308
309 filp = dentry_open(dentry, mnt, O_RDONLY);
310 if (IS_ERR(filp)) {
311 put_unused_fd(ret);
312 ret = PTR_ERR(filp);
313 goto out;
314 }
315
316 filp->f_op = &spufs_context_fops;
317 fd_install(ret, filp);
318out:
319 return ret;
320}
321
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200322static struct spu_context *
323spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
324 struct file *filp)
325{
Andre Detsch58119062008-01-11 15:03:26 +1100326 struct spu_context *tmp, *neighbor, *err;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200327 int count, node;
328 int aff_supp;
329
330 aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
331 struct spu, cbe_list))->aff_list);
332
333 if (!aff_supp)
334 return ERR_PTR(-EINVAL);
335
336 if (flags & SPU_CREATE_GANG)
337 return ERR_PTR(-EINVAL);
338
339 if (flags & SPU_CREATE_AFFINITY_MEM &&
340 gang->aff_ref_ctx &&
341 gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
342 return ERR_PTR(-EEXIST);
343
344 if (gang->aff_flags & AFF_MERGED)
345 return ERR_PTR(-EBUSY);
346
347 neighbor = NULL;
348 if (flags & SPU_CREATE_AFFINITY_SPU) {
349 if (!filp || filp->f_op != &spufs_context_fops)
350 return ERR_PTR(-EINVAL);
351
352 neighbor = get_spu_context(
353 SPUFS_I(filp->f_dentry->d_inode)->i_ctx);
354
355 if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
356 !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
357 !list_entry(neighbor->aff_list.next, struct spu_context,
Andre Detsch58119062008-01-11 15:03:26 +1100358 aff_list)->aff_head) {
359 err = ERR_PTR(-EEXIST);
360 goto out_put_neighbor;
361 }
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200362
Andre Detsch58119062008-01-11 15:03:26 +1100363 if (gang != neighbor->gang) {
364 err = ERR_PTR(-EINVAL);
365 goto out_put_neighbor;
366 }
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200367
368 count = 1;
369 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
370 count++;
371 if (list_empty(&neighbor->aff_list))
372 count++;
373
374 for (node = 0; node < MAX_NUMNODES; node++) {
375 if ((cbe_spu_info[node].n_spus - atomic_read(
376 &cbe_spu_info[node].reserved_spus)) >= count)
377 break;
378 }
379
Andre Detsch58119062008-01-11 15:03:26 +1100380 if (node == MAX_NUMNODES) {
381 err = ERR_PTR(-EEXIST);
382 goto out_put_neighbor;
383 }
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200384 }
385
386 return neighbor;
Andre Detsch58119062008-01-11 15:03:26 +1100387
388out_put_neighbor:
389 put_spu_context(neighbor);
390 return err;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200391}
392
393static void
394spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
395 struct spu_context *neighbor)
396{
397 if (flags & SPU_CREATE_AFFINITY_MEM)
398 ctx->gang->aff_ref_ctx = ctx;
399
400 if (flags & SPU_CREATE_AFFINITY_SPU) {
401 if (list_empty(&neighbor->aff_list)) {
402 list_add_tail(&neighbor->aff_list,
403 &ctx->gang->aff_list_head);
404 neighbor->aff_head = 1;
405 }
406
407 if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
408 || list_entry(neighbor->aff_list.next, struct spu_context,
409 aff_list)->aff_head) {
410 list_add(&ctx->aff_list, &neighbor->aff_list);
411 } else {
412 list_add_tail(&ctx->aff_list, &neighbor->aff_list);
413 if (neighbor->aff_head) {
414 neighbor->aff_head = 0;
415 ctx->aff_head = 1;
416 }
417 }
418
419 if (!ctx->gang->aff_ref_ctx)
420 ctx->gang->aff_ref_ctx = ctx;
421 }
422}
423
424static int
425spufs_create_context(struct inode *inode, struct dentry *dentry,
426 struct vfsmount *mnt, int flags, int mode,
427 struct file *aff_filp)
Arnd Bergmann62632032006-10-04 17:26:15 +0200428{
429 int ret;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200430 int affinity;
431 struct spu_gang *gang;
432 struct spu_context *neighbor;
Arnd Bergmann62632032006-10-04 17:26:15 +0200433
Mark Nutter5737edd2006-10-24 18:31:16 +0200434 ret = -EPERM;
435 if ((flags & SPU_CREATE_NOSCHED) &&
436 !capable(CAP_SYS_NICE))
437 goto out_unlock;
438
439 ret = -EINVAL;
440 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
441 == SPU_CREATE_ISOLATE)
442 goto out_unlock;
443
Jeremy Kerrbd2e5f82006-11-27 19:18:52 +0100444 ret = -ENODEV;
445 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
446 goto out_unlock;
447
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200448 gang = NULL;
449 neighbor = NULL;
450 affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
451 if (affinity) {
452 gang = SPUFS_I(inode)->i_gang;
453 ret = -EINVAL;
454 if (!gang)
455 goto out_unlock;
456 mutex_lock(&gang->aff_mutex);
457 neighbor = spufs_assert_affinity(flags, gang, aff_filp);
458 if (IS_ERR(neighbor)) {
459 ret = PTR_ERR(neighbor);
460 goto out_aff_unlock;
461 }
462 }
463
Arnd Bergmann62632032006-10-04 17:26:15 +0200464 ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
465 if (ret)
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200466 goto out_aff_unlock;
467
Andre Detsch58119062008-01-11 15:03:26 +1100468 if (affinity) {
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200469 spufs_set_affinity(flags, SPUFS_I(dentry->d_inode)->i_ctx,
470 neighbor);
Andre Detsch58119062008-01-11 15:03:26 +1100471 if (neighbor)
472 put_spu_context(neighbor);
473 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200474
475 /*
476 * get references for dget and mntget, will be released
477 * in error path of *_open().
478 */
479 ret = spufs_context_open(dget(dentry), mntget(mnt));
480 if (ret < 0) {
481 WARN_ON(spufs_rmdir(inode, dentry));
482 mutex_unlock(&inode->i_mutex);
483 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
484 goto out;
485 }
486
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200487out_aff_unlock:
488 if (affinity)
489 mutex_unlock(&gang->aff_mutex);
Arnd Bergmann62632032006-10-04 17:26:15 +0200490out_unlock:
491 mutex_unlock(&inode->i_mutex);
492out:
493 dput(dentry);
494 return ret;
495}
496
Arnd Bergmann62632032006-10-04 17:26:15 +0200497static int
498spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
499{
500 int ret;
501 struct inode *inode;
502 struct spu_gang *gang;
503
504 ret = -ENOSPC;
505 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
506 if (!inode)
507 goto out;
508
509 ret = 0;
510 if (dir->i_mode & S_ISGID) {
511 inode->i_gid = dir->i_gid;
512 inode->i_mode &= S_ISGID;
513 }
514 gang = alloc_spu_gang();
515 SPUFS_I(inode)->i_ctx = NULL;
516 SPUFS_I(inode)->i_gang = gang;
517 if (!gang)
518 goto out_iput;
519
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000520 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann62632032006-10-04 17:26:15 +0200521 inode->i_fop = &simple_dir_operations;
522
523 d_instantiate(dentry, inode);
Arnd Bergmann62632032006-10-04 17:26:15 +0200524 dir->i_nlink++;
525 dentry->d_inode->i_nlink++;
526 return ret;
527
528out_iput:
529 iput(inode);
530out:
531 return ret;
532}
533
534static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
535{
536 int ret;
537 struct file *filp;
538
539 ret = get_unused_fd();
540 if (ret < 0) {
541 dput(dentry);
542 mntput(mnt);
543 goto out;
544 }
545
546 filp = dentry_open(dentry, mnt, O_RDONLY);
547 if (IS_ERR(filp)) {
548 put_unused_fd(ret);
549 ret = PTR_ERR(filp);
550 goto out;
551 }
552
Jeremy Kerr877907d2007-06-04 23:26:51 +1000553 filp->f_op = &simple_dir_operations;
Arnd Bergmann62632032006-10-04 17:26:15 +0200554 fd_install(ret, filp);
555out:
556 return ret;
557}
558
559static int spufs_create_gang(struct inode *inode,
560 struct dentry *dentry,
561 struct vfsmount *mnt, int mode)
562{
563 int ret;
564
565 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
566 if (ret)
567 goto out;
568
569 /*
570 * get references for dget and mntget, will be released
571 * in error path of *_open().
572 */
573 ret = spufs_gang_open(dget(dentry), mntget(mnt));
Jeremy Kerr877907d2007-06-04 23:26:51 +1000574 if (ret < 0) {
575 int err = simple_rmdir(inode, dentry);
576 WARN_ON(err);
577 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200578
579out:
580 mutex_unlock(&inode->i_mutex);
581 dput(dentry);
582 return ret;
583}
584
585
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100586static struct file_system_type spufs_type;
587
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200588long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode,
589 struct file *filp)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500590{
591 struct dentry *dentry;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500592 int ret;
593
Arnd Bergmann67207b92005-11-15 15:53:48 -0500594 ret = -EINVAL;
Arnd Bergmann62632032006-10-04 17:26:15 +0200595 /* check if we are on spufs */
Jan Blunck4ac91372008-02-14 19:34:32 -0800596 if (nd->path.dentry->d_sb->s_type != &spufs_type)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500597 goto out;
598
Arnd Bergmann62632032006-10-04 17:26:15 +0200599 /* don't accept undefined flags */
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200600 if (flags & (~SPU_CREATE_FLAG_ALL))
arnd@arndb.dec9832942006-06-19 20:33:34 +0200601 goto out;
602
Arnd Bergmann62632032006-10-04 17:26:15 +0200603 /* only threads can be underneath a gang */
Jan Blunck4ac91372008-02-14 19:34:32 -0800604 if (nd->path.dentry != nd->path.dentry->d_sb->s_root) {
Arnd Bergmann62632032006-10-04 17:26:15 +0200605 if ((flags & SPU_CREATE_GANG) ||
Jan Blunck4ac91372008-02-14 19:34:32 -0800606 !SPUFS_I(nd->path.dentry->d_inode)->i_gang)
Arnd Bergmann62632032006-10-04 17:26:15 +0200607 goto out;
608 }
609
Arnd Bergmann67207b92005-11-15 15:53:48 -0500610 dentry = lookup_create(nd, 1);
611 ret = PTR_ERR(dentry);
612 if (IS_ERR(dentry))
613 goto out_dir;
614
615 ret = -EEXIST;
616 if (dentry->d_inode)
617 goto out_dput;
618
619 mode &= ~current->fs->umask;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500620
Arnd Bergmann62632032006-10-04 17:26:15 +0200621 if (flags & SPU_CREATE_GANG)
Jan Blunck4ac91372008-02-14 19:34:32 -0800622 return spufs_create_gang(nd->path.dentry->d_inode,
623 dentry, nd->path.mnt, mode);
Arnd Bergmann62632032006-10-04 17:26:15 +0200624 else
Jan Blunck4ac91372008-02-14 19:34:32 -0800625 return spufs_create_context(nd->path.dentry->d_inode,
626 dentry, nd->path.mnt, flags, mode,
627 filp);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500628
629out_dput:
630 dput(dentry);
631out_dir:
Jan Blunck4ac91372008-02-14 19:34:32 -0800632 mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500633out:
634 return ret;
635}
636
637/* File system initialization */
638enum {
Jeremy Kerrf11f5ee2007-04-23 21:08:23 +0200639 Opt_uid, Opt_gid, Opt_mode, Opt_err,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500640};
641
642static match_table_t spufs_tokens = {
Jeremy Kerrf11f5ee2007-04-23 21:08:23 +0200643 { Opt_uid, "uid=%d" },
644 { Opt_gid, "gid=%d" },
645 { Opt_mode, "mode=%o" },
646 { Opt_err, NULL },
Arnd Bergmann67207b92005-11-15 15:53:48 -0500647};
648
649static int
650spufs_parse_options(char *options, struct inode *root)
651{
652 char *p;
653 substring_t args[MAX_OPT_ARGS];
654
655 while ((p = strsep(&options, ",")) != NULL) {
656 int token, option;
657
658 if (!*p)
659 continue;
660
661 token = match_token(p, spufs_tokens, args);
662 switch (token) {
663 case Opt_uid:
664 if (match_int(&args[0], &option))
665 return 0;
666 root->i_uid = option;
667 break;
668 case Opt_gid:
669 if (match_int(&args[0], &option))
670 return 0;
671 root->i_gid = option;
672 break;
Jeremy Kerrf11f5ee2007-04-23 21:08:23 +0200673 case Opt_mode:
674 if (match_octal(&args[0], &option))
675 return 0;
676 root->i_mode = option | S_IFDIR;
677 break;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500678 default:
679 return 0;
680 }
681 }
682 return 1;
683}
684
Akinobu Mitadb1384b2007-04-23 21:08:20 +0200685static void spufs_exit_isolated_loader(void)
686{
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000687 free_pages((unsigned long) isolated_loader,
688 get_order(isolated_loader_size));
Akinobu Mitadb1384b2007-04-23 21:08:20 +0200689}
690
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200691static void
692spufs_init_isolated_loader(void)
693{
694 struct device_node *dn;
695 const char *loader;
696 int size;
697
698 dn = of_find_node_by_path("/spu-isolation");
699 if (!dn)
700 return;
701
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000702 loader = of_get_property(dn, "loader", &size);
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200703 if (!loader)
704 return;
705
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000706 /* the loader must be align on a 16 byte boundary */
707 isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200708 if (!isolated_loader)
709 return;
710
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000711 isolated_loader_size = size;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200712 memcpy(isolated_loader, loader, size);
713 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
714}
715
Arnd Bergmann67207b92005-11-15 15:53:48 -0500716static int
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500717spufs_create_root(struct super_block *sb, void *data)
718{
Arnd Bergmann67207b92005-11-15 15:53:48 -0500719 struct inode *inode;
720 int ret;
721
Arnd Bergmann8f18a152007-06-04 23:26:51 +1000722 ret = -ENODEV;
723 if (!spu_management_ops)
724 goto out;
725
Arnd Bergmann67207b92005-11-15 15:53:48 -0500726 ret = -ENOMEM;
727 inode = spufs_new_inode(sb, S_IFDIR | 0775);
728 if (!inode)
729 goto out;
730
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000731 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500732 inode->i_fop = &simple_dir_operations;
733 SPUFS_I(inode)->i_ctx = NULL;
734
735 ret = -EINVAL;
736 if (!spufs_parse_options(data, inode))
737 goto out_iput;
738
739 ret = -ENOMEM;
740 sb->s_root = d_alloc_root(inode);
741 if (!sb->s_root)
742 goto out_iput;
743
744 return 0;
745out_iput:
746 iput(inode);
747out:
748 return ret;
749}
750
751static int
752spufs_fill_super(struct super_block *sb, void *data, int silent)
753{
754 static struct super_operations s_ops = {
755 .alloc_inode = spufs_alloc_inode,
756 .destroy_inode = spufs_destroy_inode,
757 .statfs = simple_statfs,
758 .delete_inode = spufs_delete_inode,
759 .drop_inode = generic_delete_inode,
Miklos Szeredi90d09e12008-02-08 04:21:47 -0800760 .show_options = generic_show_options,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500761 };
762
Miklos Szeredi90d09e12008-02-08 04:21:47 -0800763 save_mount_options(sb, data);
764
Arnd Bergmann67207b92005-11-15 15:53:48 -0500765 sb->s_maxbytes = MAX_LFS_FILESIZE;
766 sb->s_blocksize = PAGE_CACHE_SIZE;
767 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
768 sb->s_magic = SPUFS_MAGIC;
769 sb->s_op = &s_ops;
770
771 return spufs_create_root(sb, data);
772}
773
David Howells454e2392006-06-23 02:02:57 -0700774static int
Arnd Bergmann67207b92005-11-15 15:53:48 -0500775spufs_get_sb(struct file_system_type *fstype, int flags,
David Howells454e2392006-06-23 02:02:57 -0700776 const char *name, void *data, struct vfsmount *mnt)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500777{
David Howells454e2392006-06-23 02:02:57 -0700778 return get_sb_single(fstype, flags, data, spufs_fill_super, mnt);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500779}
780
781static struct file_system_type spufs_type = {
782 .owner = THIS_MODULE,
783 .name = "spufs",
784 .get_sb = spufs_get_sb,
785 .kill_sb = kill_litter_super,
786};
787
Arnd Bergmanne78b47a2006-03-27 21:27:40 +0200788static int __init spufs_init(void)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500789{
790 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100791
Jeremy Kerrccf17e92007-04-23 21:08:29 +0200792 ret = -ENODEV;
793 if (!spu_management_ops)
794 goto out;
795
Arnd Bergmann67207b92005-11-15 15:53:48 -0500796 ret = -ENOMEM;
797 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
798 sizeof(struct spufs_inode_info), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900799 SLAB_HWCACHE_ALIGN, spufs_init_once);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500800
801 if (!spufs_inode_cache)
802 goto out;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200803 ret = spu_sched_init();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500804 if (ret)
805 goto out_cache;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200806 ret = register_filesystem(&spufs_type);
807 if (ret)
808 goto out_sched;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500809 ret = register_spu_syscalls(&spufs_calls);
810 if (ret)
811 goto out_fs;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200812
813 spufs_init_isolated_loader();
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100814
Arnd Bergmann67207b92005-11-15 15:53:48 -0500815 return 0;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200816
Arnd Bergmann67207b92005-11-15 15:53:48 -0500817out_fs:
818 unregister_filesystem(&spufs_type);
Akinobu Mitac99c1992007-04-23 21:08:19 +0200819out_sched:
820 spu_sched_exit();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500821out_cache:
822 kmem_cache_destroy(spufs_inode_cache);
823out:
824 return ret;
825}
826module_init(spufs_init);
827
Arnd Bergmanne78b47a2006-03-27 21:27:40 +0200828static void __exit spufs_exit(void)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500829{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500830 spu_sched_exit();
Akinobu Mitadb1384b2007-04-23 21:08:20 +0200831 spufs_exit_isolated_loader();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500832 unregister_spu_syscalls(&spufs_calls);
833 unregister_filesystem(&spufs_type);
834 kmem_cache_destroy(spufs_inode_cache);
835}
836module_exit(spufs_exit);
837
838MODULE_LICENSE("GPL");
839MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
840