blob: c662ca7836015fd8a396e25b9cd41492aaeefcf5 [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>
Christoph Hellwig826be062008-05-06 09:24:24 +100026#include <linux/fsnotify.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050027#include <linux/backing-dev.h>
28#include <linux/init.h>
29#include <linux/ioctl.h>
30#include <linux/module.h>
Arnd Bergmann346f4d32006-01-04 20:31:26 +010031#include <linux/mount.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050032#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
arnd@arndb.de0afacde2006-10-24 18:31:18 +020038#include <asm/prom.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050039#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,
Jeremy Kerr23d893f2008-06-30 12:17:28 +1000112 size_t size, struct spu_context *ctx)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500113{
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;
Jeremy Kerr23d893f2008-06-30 12:17:28 +1000128 inode->i_size = size;
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);
Jeremy Kerrc443aca2007-11-16 13:32:23 +1100176 d_drop(dir);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100177
Arnd Bergmann62632032006-10-04 17:26:15 +0200178 return simple_rmdir(parent, dir);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100179}
180
Jeremy Kerr23d893f2008-06-30 12:17:28 +1000181static int spufs_fill_dir(struct dentry *dir, struct spufs_tree_descr *files,
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100182 int mode, struct spu_context *ctx)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500183{
Sebastian Siewior87873c82007-06-06 14:03:58 +1000184 struct dentry *dentry, *tmp;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500185 int ret;
186
187 while (files->name && files->name[0]) {
188 ret = -ENOMEM;
189 dentry = d_alloc_name(dir, files->name);
190 if (!dentry)
191 goto out;
192 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
Jeremy Kerr23d893f2008-06-30 12:17:28 +1000193 files->mode & mode, files->size, ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500194 if (ret)
195 goto out;
196 files++;
197 }
198 return 0;
199out:
Sebastian Siewior87873c82007-06-06 14:03:58 +1000200 /*
201 * remove all children from dir. dir->inode is not set so don't
202 * just simply use spufs_prune_dir() and panic afterwards :)
203 * dput() looks like it will do the right thing:
204 * - dec parent's ref counter
205 * - remove child from parent's child list
206 * - free child's inode if possible
207 * - free child
208 */
209 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
210 dput(dentry);
211 }
212
213 shrink_dcache_parent(dir);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500214 return ret;
215}
216
Arnd Bergmann67207b92005-11-15 15:53:48 -0500217static int spufs_dir_close(struct inode *inode, struct file *file)
218{
Michael Ellerman0309f022006-06-19 20:33:22 +0200219 struct spu_context *ctx;
Arnd Bergmann62632032006-10-04 17:26:15 +0200220 struct inode *parent;
221 struct dentry *dir;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500222 int ret;
223
Josef Sipekb4d1ab52006-12-08 02:37:30 -0800224 dir = file->f_path.dentry;
Arnd Bergmann62632032006-10-04 17:26:15 +0200225 parent = dir->d_parent->d_inode;
226 ctx = SPUFS_I(dir->d_inode)->i_ctx;
Arnd Bergmannc8ca0632006-01-04 20:31:22 +0100227
Christoph Hellwig02539d72008-05-08 15:29:12 +1000228 mutex_lock_nested(&parent->i_mutex, I_MUTEX_PARENT);
Arnd Bergmann62632032006-10-04 17:26:15 +0200229 ret = spufs_rmdir(parent, dir);
230 mutex_unlock(&parent->i_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500231 WARN_ON(ret);
Arnd Bergmannc8ca0632006-01-04 20:31:22 +0100232
Michael Ellerman0309f022006-06-19 20:33:22 +0200233 /* We have to give up the mm_struct */
234 spu_forget(ctx);
235
Arnd Bergmann67207b92005-11-15 15:53:48 -0500236 return dcache_dir_close(inode, file);
237}
238
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800239const struct file_operations spufs_context_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500240 .open = dcache_dir_open,
241 .release = spufs_dir_close,
242 .llseek = dcache_dir_lseek,
243 .read = generic_read_dir,
244 .readdir = dcache_readdir,
245 .fsync = simple_sync_file,
246};
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100247EXPORT_SYMBOL_GPL(spufs_context_fops);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500248
249static int
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200250spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
251 int mode)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500252{
253 int ret;
254 struct inode *inode;
255 struct spu_context *ctx;
256
257 ret = -ENOSPC;
258 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
259 if (!inode)
260 goto out;
261
262 if (dir->i_mode & S_ISGID) {
263 inode->i_gid = dir->i_gid;
264 inode->i_mode &= S_ISGID;
265 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200266 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500267 SPUFS_I(inode)->i_ctx = ctx;
268 if (!ctx)
269 goto out_iput;
270
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200271 ctx->flags = flags;
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000272 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500273 inode->i_fop = &simple_dir_operations;
Mark Nutter5737edd2006-10-24 18:31:16 +0200274 if (flags & SPU_CREATE_NOSCHED)
275 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
276 mode, ctx);
277 else
278 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
279
Arnd Bergmann67207b92005-11-15 15:53:48 -0500280 if (ret)
281 goto out_free_ctx;
282
283 d_instantiate(dentry, inode);
284 dget(dentry);
285 dir->i_nlink++;
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100286 dentry->d_inode->i_nlink++;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500287 goto out;
288
289out_free_ctx:
Sebastian Siewior89df0082007-06-04 23:26:51 +1000290 spu_forget(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500291 put_spu_context(ctx);
292out_iput:
293 iput(inode);
294out:
295 return ret;
296}
297
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100298static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
299{
300 int ret;
301 struct file *filp;
302
303 ret = get_unused_fd();
304 if (ret < 0) {
305 dput(dentry);
306 mntput(mnt);
307 goto out;
308 }
309
310 filp = dentry_open(dentry, mnt, O_RDONLY);
311 if (IS_ERR(filp)) {
312 put_unused_fd(ret);
313 ret = PTR_ERR(filp);
314 goto out;
315 }
316
317 filp->f_op = &spufs_context_fops;
318 fd_install(ret, filp);
319out:
320 return ret;
321}
322
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200323static struct spu_context *
324spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
325 struct file *filp)
326{
Andre Detsch58119062008-01-11 15:03:26 +1100327 struct spu_context *tmp, *neighbor, *err;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200328 int count, node;
329 int aff_supp;
330
331 aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
332 struct spu, cbe_list))->aff_list);
333
334 if (!aff_supp)
335 return ERR_PTR(-EINVAL);
336
337 if (flags & SPU_CREATE_GANG)
338 return ERR_PTR(-EINVAL);
339
340 if (flags & SPU_CREATE_AFFINITY_MEM &&
341 gang->aff_ref_ctx &&
342 gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
343 return ERR_PTR(-EEXIST);
344
345 if (gang->aff_flags & AFF_MERGED)
346 return ERR_PTR(-EBUSY);
347
348 neighbor = NULL;
349 if (flags & SPU_CREATE_AFFINITY_SPU) {
350 if (!filp || filp->f_op != &spufs_context_fops)
351 return ERR_PTR(-EINVAL);
352
353 neighbor = get_spu_context(
354 SPUFS_I(filp->f_dentry->d_inode)->i_ctx);
355
356 if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
357 !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
358 !list_entry(neighbor->aff_list.next, struct spu_context,
Andre Detsch58119062008-01-11 15:03:26 +1100359 aff_list)->aff_head) {
360 err = ERR_PTR(-EEXIST);
361 goto out_put_neighbor;
362 }
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200363
Andre Detsch58119062008-01-11 15:03:26 +1100364 if (gang != neighbor->gang) {
365 err = ERR_PTR(-EINVAL);
366 goto out_put_neighbor;
367 }
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200368
369 count = 1;
370 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
371 count++;
372 if (list_empty(&neighbor->aff_list))
373 count++;
374
375 for (node = 0; node < MAX_NUMNODES; node++) {
376 if ((cbe_spu_info[node].n_spus - atomic_read(
377 &cbe_spu_info[node].reserved_spus)) >= count)
378 break;
379 }
380
Andre Detsch58119062008-01-11 15:03:26 +1100381 if (node == MAX_NUMNODES) {
382 err = ERR_PTR(-EEXIST);
383 goto out_put_neighbor;
384 }
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200385 }
386
387 return neighbor;
Andre Detsch58119062008-01-11 15:03:26 +1100388
389out_put_neighbor:
390 put_spu_context(neighbor);
391 return err;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200392}
393
394static void
395spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
396 struct spu_context *neighbor)
397{
398 if (flags & SPU_CREATE_AFFINITY_MEM)
399 ctx->gang->aff_ref_ctx = ctx;
400
401 if (flags & SPU_CREATE_AFFINITY_SPU) {
402 if (list_empty(&neighbor->aff_list)) {
403 list_add_tail(&neighbor->aff_list,
404 &ctx->gang->aff_list_head);
405 neighbor->aff_head = 1;
406 }
407
408 if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
409 || list_entry(neighbor->aff_list.next, struct spu_context,
410 aff_list)->aff_head) {
411 list_add(&ctx->aff_list, &neighbor->aff_list);
412 } else {
413 list_add_tail(&ctx->aff_list, &neighbor->aff_list);
414 if (neighbor->aff_head) {
415 neighbor->aff_head = 0;
416 ctx->aff_head = 1;
417 }
418 }
419
420 if (!ctx->gang->aff_ref_ctx)
421 ctx->gang->aff_ref_ctx = ctx;
422 }
423}
424
425static int
426spufs_create_context(struct inode *inode, struct dentry *dentry,
427 struct vfsmount *mnt, int flags, int mode,
428 struct file *aff_filp)
Arnd Bergmann62632032006-10-04 17:26:15 +0200429{
430 int ret;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200431 int affinity;
432 struct spu_gang *gang;
433 struct spu_context *neighbor;
Arnd Bergmann62632032006-10-04 17:26:15 +0200434
Mark Nutter5737edd2006-10-24 18:31:16 +0200435 ret = -EPERM;
436 if ((flags & SPU_CREATE_NOSCHED) &&
437 !capable(CAP_SYS_NICE))
438 goto out_unlock;
439
440 ret = -EINVAL;
441 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
442 == SPU_CREATE_ISOLATE)
443 goto out_unlock;
444
Jeremy Kerrbd2e5f82006-11-27 19:18:52 +0100445 ret = -ENODEV;
446 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
447 goto out_unlock;
448
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200449 gang = NULL;
450 neighbor = NULL;
451 affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
452 if (affinity) {
453 gang = SPUFS_I(inode)->i_gang;
454 ret = -EINVAL;
455 if (!gang)
456 goto out_unlock;
457 mutex_lock(&gang->aff_mutex);
458 neighbor = spufs_assert_affinity(flags, gang, aff_filp);
459 if (IS_ERR(neighbor)) {
460 ret = PTR_ERR(neighbor);
461 goto out_aff_unlock;
462 }
463 }
464
Arnd Bergmann62632032006-10-04 17:26:15 +0200465 ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
466 if (ret)
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200467 goto out_aff_unlock;
468
Andre Detsch58119062008-01-11 15:03:26 +1100469 if (affinity) {
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200470 spufs_set_affinity(flags, SPUFS_I(dentry->d_inode)->i_ctx,
471 neighbor);
Andre Detsch58119062008-01-11 15:03:26 +1100472 if (neighbor)
473 put_spu_context(neighbor);
474 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200475
476 /*
477 * get references for dget and mntget, will be released
478 * in error path of *_open().
479 */
480 ret = spufs_context_open(dget(dentry), mntget(mnt));
481 if (ret < 0) {
482 WARN_ON(spufs_rmdir(inode, dentry));
483 mutex_unlock(&inode->i_mutex);
484 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
485 goto out;
486 }
487
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200488out_aff_unlock:
489 if (affinity)
490 mutex_unlock(&gang->aff_mutex);
Arnd Bergmann62632032006-10-04 17:26:15 +0200491out_unlock:
492 mutex_unlock(&inode->i_mutex);
493out:
494 dput(dentry);
495 return ret;
496}
497
Arnd Bergmann62632032006-10-04 17:26:15 +0200498static int
499spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
500{
501 int ret;
502 struct inode *inode;
503 struct spu_gang *gang;
504
505 ret = -ENOSPC;
506 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
507 if (!inode)
508 goto out;
509
510 ret = 0;
511 if (dir->i_mode & S_ISGID) {
512 inode->i_gid = dir->i_gid;
513 inode->i_mode &= S_ISGID;
514 }
515 gang = alloc_spu_gang();
516 SPUFS_I(inode)->i_ctx = NULL;
517 SPUFS_I(inode)->i_gang = gang;
518 if (!gang)
519 goto out_iput;
520
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000521 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann62632032006-10-04 17:26:15 +0200522 inode->i_fop = &simple_dir_operations;
523
524 d_instantiate(dentry, inode);
Arnd Bergmann62632032006-10-04 17:26:15 +0200525 dir->i_nlink++;
526 dentry->d_inode->i_nlink++;
527 return ret;
528
529out_iput:
530 iput(inode);
531out:
532 return ret;
533}
534
535static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
536{
537 int ret;
538 struct file *filp;
539
540 ret = get_unused_fd();
541 if (ret < 0) {
542 dput(dentry);
543 mntput(mnt);
544 goto out;
545 }
546
547 filp = dentry_open(dentry, mnt, O_RDONLY);
548 if (IS_ERR(filp)) {
549 put_unused_fd(ret);
550 ret = PTR_ERR(filp);
551 goto out;
552 }
553
Jeremy Kerr877907d2007-06-04 23:26:51 +1000554 filp->f_op = &simple_dir_operations;
Arnd Bergmann62632032006-10-04 17:26:15 +0200555 fd_install(ret, filp);
556out:
557 return ret;
558}
559
560static int spufs_create_gang(struct inode *inode,
561 struct dentry *dentry,
562 struct vfsmount *mnt, int mode)
563{
564 int ret;
565
566 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
567 if (ret)
568 goto out;
569
570 /*
571 * get references for dget and mntget, will be released
572 * in error path of *_open().
573 */
574 ret = spufs_gang_open(dget(dentry), mntget(mnt));
Jeremy Kerr877907d2007-06-04 23:26:51 +1000575 if (ret < 0) {
576 int err = simple_rmdir(inode, dentry);
577 WARN_ON(err);
578 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200579
580out:
581 mutex_unlock(&inode->i_mutex);
582 dput(dentry);
583 return ret;
584}
585
586
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100587static struct file_system_type spufs_type;
588
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200589long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode,
590 struct file *filp)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500591{
592 struct dentry *dentry;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500593 int ret;
594
Arnd Bergmann67207b92005-11-15 15:53:48 -0500595 ret = -EINVAL;
Arnd Bergmann62632032006-10-04 17:26:15 +0200596 /* check if we are on spufs */
Jan Blunck4ac91372008-02-14 19:34:32 -0800597 if (nd->path.dentry->d_sb->s_type != &spufs_type)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500598 goto out;
599
Arnd Bergmann62632032006-10-04 17:26:15 +0200600 /* don't accept undefined flags */
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200601 if (flags & (~SPU_CREATE_FLAG_ALL))
arnd@arndb.dec9832942006-06-19 20:33:34 +0200602 goto out;
603
Arnd Bergmann62632032006-10-04 17:26:15 +0200604 /* only threads can be underneath a gang */
Jan Blunck4ac91372008-02-14 19:34:32 -0800605 if (nd->path.dentry != nd->path.dentry->d_sb->s_root) {
Arnd Bergmann62632032006-10-04 17:26:15 +0200606 if ((flags & SPU_CREATE_GANG) ||
Jan Blunck4ac91372008-02-14 19:34:32 -0800607 !SPUFS_I(nd->path.dentry->d_inode)->i_gang)
Arnd Bergmann62632032006-10-04 17:26:15 +0200608 goto out;
609 }
610
Arnd Bergmann67207b92005-11-15 15:53:48 -0500611 dentry = lookup_create(nd, 1);
612 ret = PTR_ERR(dentry);
613 if (IS_ERR(dentry))
614 goto out_dir;
615
616 ret = -EEXIST;
617 if (dentry->d_inode)
618 goto out_dput;
619
620 mode &= ~current->fs->umask;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500621
Arnd Bergmann62632032006-10-04 17:26:15 +0200622 if (flags & SPU_CREATE_GANG)
Christoph Hellwig826be062008-05-06 09:24:24 +1000623 ret = spufs_create_gang(nd->path.dentry->d_inode,
Jan Blunck4ac91372008-02-14 19:34:32 -0800624 dentry, nd->path.mnt, mode);
Arnd Bergmann62632032006-10-04 17:26:15 +0200625 else
Christoph Hellwig826be062008-05-06 09:24:24 +1000626 ret = spufs_create_context(nd->path.dentry->d_inode,
Jan Blunck4ac91372008-02-14 19:34:32 -0800627 dentry, nd->path.mnt, flags, mode,
628 filp);
Christoph Hellwig826be062008-05-06 09:24:24 +1000629 if (ret >= 0)
630 fsnotify_mkdir(nd->path.dentry->d_inode, dentry);
631 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500632
633out_dput:
634 dput(dentry);
635out_dir:
Jan Blunck4ac91372008-02-14 19:34:32 -0800636 mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500637out:
638 return ret;
639}
640
641/* File system initialization */
642enum {
Jeremy Kerrf11f5ee2007-04-23 21:08:23 +0200643 Opt_uid, Opt_gid, Opt_mode, Opt_err,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500644};
645
646static match_table_t spufs_tokens = {
Jeremy Kerrf11f5ee2007-04-23 21:08:23 +0200647 { Opt_uid, "uid=%d" },
648 { Opt_gid, "gid=%d" },
649 { Opt_mode, "mode=%o" },
650 { Opt_err, NULL },
Arnd Bergmann67207b92005-11-15 15:53:48 -0500651};
652
653static int
654spufs_parse_options(char *options, struct inode *root)
655{
656 char *p;
657 substring_t args[MAX_OPT_ARGS];
658
659 while ((p = strsep(&options, ",")) != NULL) {
660 int token, option;
661
662 if (!*p)
663 continue;
664
665 token = match_token(p, spufs_tokens, args);
666 switch (token) {
667 case Opt_uid:
668 if (match_int(&args[0], &option))
669 return 0;
670 root->i_uid = option;
671 break;
672 case Opt_gid:
673 if (match_int(&args[0], &option))
674 return 0;
675 root->i_gid = option;
676 break;
Jeremy Kerrf11f5ee2007-04-23 21:08:23 +0200677 case Opt_mode:
678 if (match_octal(&args[0], &option))
679 return 0;
680 root->i_mode = option | S_IFDIR;
681 break;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500682 default:
683 return 0;
684 }
685 }
686 return 1;
687}
688
Akinobu Mitadb1384b402007-04-23 21:08:20 +0200689static void spufs_exit_isolated_loader(void)
690{
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000691 free_pages((unsigned long) isolated_loader,
692 get_order(isolated_loader_size));
Akinobu Mitadb1384b402007-04-23 21:08:20 +0200693}
694
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200695static void
696spufs_init_isolated_loader(void)
697{
698 struct device_node *dn;
699 const char *loader;
700 int size;
701
702 dn = of_find_node_by_path("/spu-isolation");
703 if (!dn)
704 return;
705
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000706 loader = of_get_property(dn, "loader", &size);
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200707 if (!loader)
708 return;
709
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000710 /* the loader must be align on a 16 byte boundary */
711 isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200712 if (!isolated_loader)
713 return;
714
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000715 isolated_loader_size = size;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200716 memcpy(isolated_loader, loader, size);
717 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
718}
719
Arnd Bergmann67207b92005-11-15 15:53:48 -0500720static int
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500721spufs_create_root(struct super_block *sb, void *data)
722{
Arnd Bergmann67207b92005-11-15 15:53:48 -0500723 struct inode *inode;
724 int ret;
725
Arnd Bergmann8f18a152007-06-04 23:26:51 +1000726 ret = -ENODEV;
727 if (!spu_management_ops)
728 goto out;
729
Arnd Bergmann67207b92005-11-15 15:53:48 -0500730 ret = -ENOMEM;
731 inode = spufs_new_inode(sb, S_IFDIR | 0775);
732 if (!inode)
733 goto out;
734
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000735 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500736 inode->i_fop = &simple_dir_operations;
737 SPUFS_I(inode)->i_ctx = NULL;
738
739 ret = -EINVAL;
740 if (!spufs_parse_options(data, inode))
741 goto out_iput;
742
743 ret = -ENOMEM;
744 sb->s_root = d_alloc_root(inode);
745 if (!sb->s_root)
746 goto out_iput;
747
748 return 0;
749out_iput:
750 iput(inode);
751out:
752 return ret;
753}
754
755static int
756spufs_fill_super(struct super_block *sb, void *data, int silent)
757{
758 static struct super_operations s_ops = {
759 .alloc_inode = spufs_alloc_inode,
760 .destroy_inode = spufs_destroy_inode,
761 .statfs = simple_statfs,
762 .delete_inode = spufs_delete_inode,
763 .drop_inode = generic_delete_inode,
Miklos Szeredi90d09e12008-02-08 04:21:47 -0800764 .show_options = generic_show_options,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500765 };
766
Miklos Szeredi90d09e12008-02-08 04:21:47 -0800767 save_mount_options(sb, data);
768
Arnd Bergmann67207b92005-11-15 15:53:48 -0500769 sb->s_maxbytes = MAX_LFS_FILESIZE;
770 sb->s_blocksize = PAGE_CACHE_SIZE;
771 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
772 sb->s_magic = SPUFS_MAGIC;
773 sb->s_op = &s_ops;
774
775 return spufs_create_root(sb, data);
776}
777
David Howells454e2392006-06-23 02:02:57 -0700778static int
Arnd Bergmann67207b92005-11-15 15:53:48 -0500779spufs_get_sb(struct file_system_type *fstype, int flags,
David Howells454e2392006-06-23 02:02:57 -0700780 const char *name, void *data, struct vfsmount *mnt)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500781{
David Howells454e2392006-06-23 02:02:57 -0700782 return get_sb_single(fstype, flags, data, spufs_fill_super, mnt);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500783}
784
785static struct file_system_type spufs_type = {
786 .owner = THIS_MODULE,
787 .name = "spufs",
788 .get_sb = spufs_get_sb,
789 .kill_sb = kill_litter_super,
790};
791
Arnd Bergmanne78b47a2006-03-27 21:27:40 +0200792static int __init spufs_init(void)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500793{
794 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100795
Jeremy Kerrccf17e92007-04-23 21:08:29 +0200796 ret = -ENODEV;
797 if (!spu_management_ops)
798 goto out;
799
Arnd Bergmann67207b92005-11-15 15:53:48 -0500800 ret = -ENOMEM;
801 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
802 sizeof(struct spufs_inode_info), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900803 SLAB_HWCACHE_ALIGN, spufs_init_once);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500804
805 if (!spufs_inode_cache)
806 goto out;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200807 ret = spu_sched_init();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500808 if (ret)
809 goto out_cache;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200810 ret = register_filesystem(&spufs_type);
811 if (ret)
812 goto out_sched;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500813 ret = register_spu_syscalls(&spufs_calls);
814 if (ret)
815 goto out_fs;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200816
817 spufs_init_isolated_loader();
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100818
Arnd Bergmann67207b92005-11-15 15:53:48 -0500819 return 0;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200820
Arnd Bergmann67207b92005-11-15 15:53:48 -0500821out_fs:
822 unregister_filesystem(&spufs_type);
Akinobu Mitac99c1992007-04-23 21:08:19 +0200823out_sched:
824 spu_sched_exit();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500825out_cache:
826 kmem_cache_destroy(spufs_inode_cache);
827out:
828 return ret;
829}
830module_init(spufs_init);
831
Arnd Bergmanne78b47a2006-03-27 21:27:40 +0200832static void __exit spufs_exit(void)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500833{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500834 spu_sched_exit();
Akinobu Mitadb1384b402007-04-23 21:08:20 +0200835 spufs_exit_isolated_loader();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500836 unregister_spu_syscalls(&spufs_calls);
837 unregister_filesystem(&spufs_type);
838 kmem_cache_destroy(spufs_inode_cache);
839}
840module_exit(spufs_exit);
841
842MODULE_LICENSE("GPL");
843MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
844