blob: e309ef70a53140b4e156fbfb1f38f06863b7eee2 [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
Jeremy Kerr2c3e4782008-07-03 11:42:20 +100045struct spufs_sb_info {
46 int debug;
47};
48
Christoph Lametere18b8902006-12-06 20:33:20 -080049static struct kmem_cache *spufs_inode_cache;
Jeremy Kerrc6730ed2006-11-20 18:45:10 +010050char *isolated_loader;
Sebastian Siewior8b0d3122007-09-19 14:38:12 +100051static int isolated_loader_size;
Arnd Bergmann67207b92005-11-15 15:53:48 -050052
Jeremy Kerr2c3e4782008-07-03 11:42:20 +100053static struct spufs_sb_info *spufs_get_sb_info(struct super_block *sb)
54{
55 return sb->s_fs_info;
56}
57
Arnd Bergmann67207b92005-11-15 15:53:48 -050058static struct inode *
59spufs_alloc_inode(struct super_block *sb)
60{
61 struct spufs_inode_info *ei;
62
Christoph Lametere94b1762006-12-06 20:33:17 -080063 ei = kmem_cache_alloc(spufs_inode_cache, GFP_KERNEL);
Arnd Bergmann67207b92005-11-15 15:53:48 -050064 if (!ei)
65 return NULL;
Arnd Bergmann62632032006-10-04 17:26:15 +020066
67 ei->i_gang = NULL;
68 ei->i_ctx = NULL;
Christoph Hellwig43c2bbd2007-04-23 21:08:07 +020069 ei->i_openers = 0;
Arnd Bergmann62632032006-10-04 17:26:15 +020070
Arnd Bergmann67207b92005-11-15 15:53:48 -050071 return &ei->vfs_inode;
72}
73
74static void
75spufs_destroy_inode(struct inode *inode)
76{
77 kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
78}
79
80static void
Alexey Dobriyan51cc5062008-07-25 19:45:34 -070081spufs_init_once(void *p)
Arnd Bergmann67207b92005-11-15 15:53:48 -050082{
83 struct spufs_inode_info *ei = p;
84
Christoph Lametera35afb82007-05-16 22:10:57 -070085 inode_init_once(&ei->vfs_inode);
Arnd Bergmann67207b92005-11-15 15:53:48 -050086}
87
88static struct inode *
89spufs_new_inode(struct super_block *sb, int mode)
90{
91 struct inode *inode;
92
93 inode = new_inode(sb);
94 if (!inode)
95 goto out;
96
97 inode->i_mode = mode;
David Howells1330deb2008-11-14 10:38:39 +110098 inode->i_uid = current_fsuid();
99 inode->i_gid = current_fsgid();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500100 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
101out:
102 return inode;
103}
104
105static int
106spufs_setattr(struct dentry *dentry, struct iattr *attr)
107{
108 struct inode *inode = dentry->d_inode;
109
Arnd Bergmann67207b92005-11-15 15:53:48 -0500110 if ((attr->ia_valid & ATTR_SIZE) &&
111 (attr->ia_size != inode->i_size))
112 return -EINVAL;
113 return inode_setattr(inode, attr);
114}
115
116
117static int
118spufs_new_file(struct super_block *sb, struct dentry *dentry,
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800119 const struct file_operations *fops, int mode,
Jeremy Kerr23d893f2008-06-30 12:17:28 +1000120 size_t size, struct spu_context *ctx)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500121{
122 static struct inode_operations spufs_file_iops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500123 .setattr = spufs_setattr,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500124 };
125 struct inode *inode;
126 int ret;
127
128 ret = -ENOSPC;
129 inode = spufs_new_inode(sb, S_IFREG | mode);
130 if (!inode)
131 goto out;
132
133 ret = 0;
134 inode->i_op = &spufs_file_iops;
135 inode->i_fop = fops;
Jeremy Kerr23d893f2008-06-30 12:17:28 +1000136 inode->i_size = size;
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700137 inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500138 d_add(dentry, inode);
139out:
140 return ret;
141}
142
143static void
144spufs_delete_inode(struct inode *inode)
145{
Arnd Bergmann62632032006-10-04 17:26:15 +0200146 struct spufs_inode_info *ei = SPUFS_I(inode);
147
148 if (ei->i_ctx)
149 put_spu_context(ei->i_ctx);
150 if (ei->i_gang)
151 put_spu_gang(ei->i_gang);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500152 clear_inode(inode);
153}
154
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100155static void spufs_prune_dir(struct dentry *dir)
156{
157 struct dentry *dentry, *tmp;
Arnd Bergmann62632032006-10-04 17:26:15 +0200158
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800159 mutex_lock(&dir->d_inode->i_mutex);
Andrew Mortonc3a9aea2006-01-09 20:51:24 -0800160 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100161 spin_lock(&dcache_lock);
162 spin_lock(&dentry->d_lock);
163 if (!(d_unhashed(dentry)) && dentry->d_inode) {
164 dget_locked(dentry);
165 __d_drop(dentry);
166 spin_unlock(&dentry->d_lock);
167 simple_unlink(dir->d_inode, dentry);
168 spin_unlock(&dcache_lock);
169 dput(dentry);
170 } else {
171 spin_unlock(&dentry->d_lock);
172 spin_unlock(&dcache_lock);
173 }
174 }
175 shrink_dcache_parent(dir);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800176 mutex_unlock(&dir->d_inode->i_mutex);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100177}
178
Arnd Bergmann62632032006-10-04 17:26:15 +0200179/* Caller must hold parent->i_mutex */
180static int spufs_rmdir(struct inode *parent, struct dentry *dir)
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100181{
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100182 /* remove all entries */
Arnd Bergmann62632032006-10-04 17:26:15 +0200183 spufs_prune_dir(dir);
Jeremy Kerrc443aca2007-11-16 13:32:23 +1100184 d_drop(dir);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100185
Arnd Bergmann62632032006-10-04 17:26:15 +0200186 return simple_rmdir(parent, dir);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100187}
188
Jeremy Kerr23d893f2008-06-30 12:17:28 +1000189static int spufs_fill_dir(struct dentry *dir, struct spufs_tree_descr *files,
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100190 int mode, struct spu_context *ctx)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500191{
Sebastian Siewior87873c82007-06-06 14:03:58 +1000192 struct dentry *dentry, *tmp;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500193 int ret;
194
195 while (files->name && files->name[0]) {
196 ret = -ENOMEM;
197 dentry = d_alloc_name(dir, files->name);
198 if (!dentry)
199 goto out;
200 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
Jeremy Kerr23d893f2008-06-30 12:17:28 +1000201 files->mode & mode, files->size, ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500202 if (ret)
203 goto out;
204 files++;
205 }
206 return 0;
207out:
Sebastian Siewior87873c82007-06-06 14:03:58 +1000208 /*
209 * remove all children from dir. dir->inode is not set so don't
210 * just simply use spufs_prune_dir() and panic afterwards :)
211 * dput() looks like it will do the right thing:
212 * - dec parent's ref counter
213 * - remove child from parent's child list
214 * - free child's inode if possible
215 * - free child
216 */
217 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
218 dput(dentry);
219 }
220
221 shrink_dcache_parent(dir);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500222 return ret;
223}
224
Arnd Bergmann67207b92005-11-15 15:53:48 -0500225static int spufs_dir_close(struct inode *inode, struct file *file)
226{
Michael Ellerman0309f022006-06-19 20:33:22 +0200227 struct spu_context *ctx;
Arnd Bergmann62632032006-10-04 17:26:15 +0200228 struct inode *parent;
229 struct dentry *dir;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500230 int ret;
231
Josef Sipekb4d1ab52006-12-08 02:37:30 -0800232 dir = file->f_path.dentry;
Arnd Bergmann62632032006-10-04 17:26:15 +0200233 parent = dir->d_parent->d_inode;
234 ctx = SPUFS_I(dir->d_inode)->i_ctx;
Arnd Bergmannc8ca0632006-01-04 20:31:22 +0100235
Christoph Hellwig02539d72008-05-08 15:29:12 +1000236 mutex_lock_nested(&parent->i_mutex, I_MUTEX_PARENT);
Arnd Bergmann62632032006-10-04 17:26:15 +0200237 ret = spufs_rmdir(parent, dir);
238 mutex_unlock(&parent->i_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500239 WARN_ON(ret);
Arnd Bergmannc8ca0632006-01-04 20:31:22 +0100240
Michael Ellerman0309f022006-06-19 20:33:22 +0200241 /* We have to give up the mm_struct */
242 spu_forget(ctx);
243
Arnd Bergmann67207b92005-11-15 15:53:48 -0500244 return dcache_dir_close(inode, file);
245}
246
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800247const struct file_operations spufs_context_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500248 .open = dcache_dir_open,
249 .release = spufs_dir_close,
250 .llseek = dcache_dir_lseek,
251 .read = generic_read_dir,
252 .readdir = dcache_readdir,
253 .fsync = simple_sync_file,
254};
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100255EXPORT_SYMBOL_GPL(spufs_context_fops);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500256
257static int
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200258spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
259 int mode)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500260{
261 int ret;
262 struct inode *inode;
263 struct spu_context *ctx;
264
265 ret = -ENOSPC;
266 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
267 if (!inode)
268 goto out;
269
270 if (dir->i_mode & S_ISGID) {
271 inode->i_gid = dir->i_gid;
272 inode->i_mode &= S_ISGID;
273 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200274 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500275 SPUFS_I(inode)->i_ctx = ctx;
276 if (!ctx)
277 goto out_iput;
278
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200279 ctx->flags = flags;
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000280 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500281 inode->i_fop = &simple_dir_operations;
Mark Nutter5737edd2006-10-24 18:31:16 +0200282 if (flags & SPU_CREATE_NOSCHED)
283 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
284 mode, ctx);
285 else
286 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
287
Arnd Bergmann67207b92005-11-15 15:53:48 -0500288 if (ret)
289 goto out_free_ctx;
290
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000291 if (spufs_get_sb_info(dir->i_sb)->debug)
292 ret = spufs_fill_dir(dentry, spufs_dir_debug_contents,
293 mode, ctx);
294
295 if (ret)
296 goto out_free_ctx;
297
Arnd Bergmann67207b92005-11-15 15:53:48 -0500298 d_instantiate(dentry, inode);
299 dget(dentry);
Jeremy Kerrba0b9962008-10-09 10:39:21 +1100300 inc_nlink(dir);
301 inc_nlink(dentry->d_inode);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500302 goto out;
303
304out_free_ctx:
Sebastian Siewior89df0082007-06-04 23:26:51 +1000305 spu_forget(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500306 put_spu_context(ctx);
307out_iput:
308 iput(inode);
309out:
310 return ret;
311}
312
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100313static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
314{
315 int ret;
316 struct file *filp;
317
318 ret = get_unused_fd();
319 if (ret < 0) {
320 dput(dentry);
321 mntput(mnt);
322 goto out;
323 }
324
David Howells745ca242008-11-14 10:39:22 +1100325 filp = dentry_open(dentry, mnt, O_RDONLY, current_cred());
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100326 if (IS_ERR(filp)) {
327 put_unused_fd(ret);
328 ret = PTR_ERR(filp);
329 goto out;
330 }
331
332 filp->f_op = &spufs_context_fops;
333 fd_install(ret, filp);
334out:
335 return ret;
336}
337
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200338static struct spu_context *
339spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
340 struct file *filp)
341{
Andre Detsch58119062008-01-11 15:03:26 +1100342 struct spu_context *tmp, *neighbor, *err;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200343 int count, node;
344 int aff_supp;
345
346 aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
347 struct spu, cbe_list))->aff_list);
348
349 if (!aff_supp)
350 return ERR_PTR(-EINVAL);
351
352 if (flags & SPU_CREATE_GANG)
353 return ERR_PTR(-EINVAL);
354
355 if (flags & SPU_CREATE_AFFINITY_MEM &&
356 gang->aff_ref_ctx &&
357 gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
358 return ERR_PTR(-EEXIST);
359
360 if (gang->aff_flags & AFF_MERGED)
361 return ERR_PTR(-EBUSY);
362
363 neighbor = NULL;
364 if (flags & SPU_CREATE_AFFINITY_SPU) {
365 if (!filp || filp->f_op != &spufs_context_fops)
366 return ERR_PTR(-EINVAL);
367
368 neighbor = get_spu_context(
369 SPUFS_I(filp->f_dentry->d_inode)->i_ctx);
370
371 if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
372 !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
373 !list_entry(neighbor->aff_list.next, struct spu_context,
Andre Detsch58119062008-01-11 15:03:26 +1100374 aff_list)->aff_head) {
375 err = ERR_PTR(-EEXIST);
376 goto out_put_neighbor;
377 }
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200378
Andre Detsch58119062008-01-11 15:03:26 +1100379 if (gang != neighbor->gang) {
380 err = ERR_PTR(-EINVAL);
381 goto out_put_neighbor;
382 }
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200383
384 count = 1;
385 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
386 count++;
387 if (list_empty(&neighbor->aff_list))
388 count++;
389
390 for (node = 0; node < MAX_NUMNODES; node++) {
391 if ((cbe_spu_info[node].n_spus - atomic_read(
392 &cbe_spu_info[node].reserved_spus)) >= count)
393 break;
394 }
395
Andre Detsch58119062008-01-11 15:03:26 +1100396 if (node == MAX_NUMNODES) {
397 err = ERR_PTR(-EEXIST);
398 goto out_put_neighbor;
399 }
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200400 }
401
402 return neighbor;
Andre Detsch58119062008-01-11 15:03:26 +1100403
404out_put_neighbor:
405 put_spu_context(neighbor);
406 return err;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200407}
408
409static void
410spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
411 struct spu_context *neighbor)
412{
413 if (flags & SPU_CREATE_AFFINITY_MEM)
414 ctx->gang->aff_ref_ctx = ctx;
415
416 if (flags & SPU_CREATE_AFFINITY_SPU) {
417 if (list_empty(&neighbor->aff_list)) {
418 list_add_tail(&neighbor->aff_list,
419 &ctx->gang->aff_list_head);
420 neighbor->aff_head = 1;
421 }
422
423 if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
424 || list_entry(neighbor->aff_list.next, struct spu_context,
425 aff_list)->aff_head) {
426 list_add(&ctx->aff_list, &neighbor->aff_list);
427 } else {
428 list_add_tail(&ctx->aff_list, &neighbor->aff_list);
429 if (neighbor->aff_head) {
430 neighbor->aff_head = 0;
431 ctx->aff_head = 1;
432 }
433 }
434
435 if (!ctx->gang->aff_ref_ctx)
436 ctx->gang->aff_ref_ctx = ctx;
437 }
438}
439
440static int
441spufs_create_context(struct inode *inode, struct dentry *dentry,
442 struct vfsmount *mnt, int flags, int mode,
443 struct file *aff_filp)
Arnd Bergmann62632032006-10-04 17:26:15 +0200444{
445 int ret;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200446 int affinity;
447 struct spu_gang *gang;
448 struct spu_context *neighbor;
Arnd Bergmann62632032006-10-04 17:26:15 +0200449
Mark Nutter5737edd2006-10-24 18:31:16 +0200450 ret = -EPERM;
451 if ((flags & SPU_CREATE_NOSCHED) &&
452 !capable(CAP_SYS_NICE))
453 goto out_unlock;
454
455 ret = -EINVAL;
456 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
457 == SPU_CREATE_ISOLATE)
458 goto out_unlock;
459
Jeremy Kerrbd2e5f82006-11-27 19:18:52 +0100460 ret = -ENODEV;
461 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
462 goto out_unlock;
463
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200464 gang = NULL;
465 neighbor = NULL;
466 affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
467 if (affinity) {
468 gang = SPUFS_I(inode)->i_gang;
469 ret = -EINVAL;
470 if (!gang)
471 goto out_unlock;
472 mutex_lock(&gang->aff_mutex);
473 neighbor = spufs_assert_affinity(flags, gang, aff_filp);
474 if (IS_ERR(neighbor)) {
475 ret = PTR_ERR(neighbor);
476 goto out_aff_unlock;
477 }
478 }
479
Arnd Bergmann62632032006-10-04 17:26:15 +0200480 ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
481 if (ret)
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200482 goto out_aff_unlock;
483
Andre Detsch58119062008-01-11 15:03:26 +1100484 if (affinity) {
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200485 spufs_set_affinity(flags, SPUFS_I(dentry->d_inode)->i_ctx,
486 neighbor);
Andre Detsch58119062008-01-11 15:03:26 +1100487 if (neighbor)
488 put_spu_context(neighbor);
489 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200490
491 /*
492 * get references for dget and mntget, will be released
493 * in error path of *_open().
494 */
495 ret = spufs_context_open(dget(dentry), mntget(mnt));
496 if (ret < 0) {
497 WARN_ON(spufs_rmdir(inode, dentry));
Kou Ishizaki6747c2e2008-10-09 10:45:49 +1100498 if (affinity)
499 mutex_unlock(&gang->aff_mutex);
Arnd Bergmann62632032006-10-04 17:26:15 +0200500 mutex_unlock(&inode->i_mutex);
501 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
502 goto out;
503 }
504
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200505out_aff_unlock:
506 if (affinity)
507 mutex_unlock(&gang->aff_mutex);
Arnd Bergmann62632032006-10-04 17:26:15 +0200508out_unlock:
509 mutex_unlock(&inode->i_mutex);
510out:
511 dput(dentry);
512 return ret;
513}
514
Arnd Bergmann62632032006-10-04 17:26:15 +0200515static int
516spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
517{
518 int ret;
519 struct inode *inode;
520 struct spu_gang *gang;
521
522 ret = -ENOSPC;
523 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
524 if (!inode)
525 goto out;
526
527 ret = 0;
528 if (dir->i_mode & S_ISGID) {
529 inode->i_gid = dir->i_gid;
530 inode->i_mode &= S_ISGID;
531 }
532 gang = alloc_spu_gang();
533 SPUFS_I(inode)->i_ctx = NULL;
534 SPUFS_I(inode)->i_gang = gang;
535 if (!gang)
536 goto out_iput;
537
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000538 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann62632032006-10-04 17:26:15 +0200539 inode->i_fop = &simple_dir_operations;
540
541 d_instantiate(dentry, inode);
Jeremy Kerrba0b9962008-10-09 10:39:21 +1100542 inc_nlink(dir);
543 inc_nlink(dentry->d_inode);
Arnd Bergmann62632032006-10-04 17:26:15 +0200544 return ret;
545
546out_iput:
547 iput(inode);
548out:
549 return ret;
550}
551
552static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
553{
554 int ret;
555 struct file *filp;
556
557 ret = get_unused_fd();
558 if (ret < 0) {
559 dput(dentry);
560 mntput(mnt);
561 goto out;
562 }
563
David Howells745ca242008-11-14 10:39:22 +1100564 filp = dentry_open(dentry, mnt, O_RDONLY, current_cred());
Arnd Bergmann62632032006-10-04 17:26:15 +0200565 if (IS_ERR(filp)) {
566 put_unused_fd(ret);
567 ret = PTR_ERR(filp);
568 goto out;
569 }
570
Jeremy Kerr877907d2007-06-04 23:26:51 +1000571 filp->f_op = &simple_dir_operations;
Arnd Bergmann62632032006-10-04 17:26:15 +0200572 fd_install(ret, filp);
573out:
574 return ret;
575}
576
577static int spufs_create_gang(struct inode *inode,
578 struct dentry *dentry,
579 struct vfsmount *mnt, int mode)
580{
581 int ret;
582
583 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
584 if (ret)
585 goto out;
586
587 /*
588 * get references for dget and mntget, will be released
589 * in error path of *_open().
590 */
591 ret = spufs_gang_open(dget(dentry), mntget(mnt));
Jeremy Kerr877907d2007-06-04 23:26:51 +1000592 if (ret < 0) {
593 int err = simple_rmdir(inode, dentry);
594 WARN_ON(err);
595 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200596
597out:
598 mutex_unlock(&inode->i_mutex);
599 dput(dentry);
600 return ret;
601}
602
603
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100604static struct file_system_type spufs_type;
605
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200606long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode,
607 struct file *filp)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500608{
609 struct dentry *dentry;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500610 int ret;
611
Arnd Bergmann67207b92005-11-15 15:53:48 -0500612 ret = -EINVAL;
Arnd Bergmann62632032006-10-04 17:26:15 +0200613 /* check if we are on spufs */
Jan Blunck4ac91372008-02-14 19:34:32 -0800614 if (nd->path.dentry->d_sb->s_type != &spufs_type)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500615 goto out;
616
Arnd Bergmann62632032006-10-04 17:26:15 +0200617 /* don't accept undefined flags */
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200618 if (flags & (~SPU_CREATE_FLAG_ALL))
arnd@arndb.dec9832942006-06-19 20:33:34 +0200619 goto out;
620
Arnd Bergmann62632032006-10-04 17:26:15 +0200621 /* only threads can be underneath a gang */
Jan Blunck4ac91372008-02-14 19:34:32 -0800622 if (nd->path.dentry != nd->path.dentry->d_sb->s_root) {
Arnd Bergmann62632032006-10-04 17:26:15 +0200623 if ((flags & SPU_CREATE_GANG) ||
Jan Blunck4ac91372008-02-14 19:34:32 -0800624 !SPUFS_I(nd->path.dentry->d_inode)->i_gang)
Arnd Bergmann62632032006-10-04 17:26:15 +0200625 goto out;
626 }
627
Arnd Bergmann67207b92005-11-15 15:53:48 -0500628 dentry = lookup_create(nd, 1);
629 ret = PTR_ERR(dentry);
630 if (IS_ERR(dentry))
631 goto out_dir;
632
633 ret = -EEXIST;
634 if (dentry->d_inode)
635 goto out_dput;
636
637 mode &= ~current->fs->umask;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500638
Arnd Bergmann62632032006-10-04 17:26:15 +0200639 if (flags & SPU_CREATE_GANG)
Christoph Hellwig826be062008-05-06 09:24:24 +1000640 ret = spufs_create_gang(nd->path.dentry->d_inode,
Jan Blunck4ac91372008-02-14 19:34:32 -0800641 dentry, nd->path.mnt, mode);
Arnd Bergmann62632032006-10-04 17:26:15 +0200642 else
Christoph Hellwig826be062008-05-06 09:24:24 +1000643 ret = spufs_create_context(nd->path.dentry->d_inode,
Jan Blunck4ac91372008-02-14 19:34:32 -0800644 dentry, nd->path.mnt, flags, mode,
645 filp);
Christoph Hellwig826be062008-05-06 09:24:24 +1000646 if (ret >= 0)
647 fsnotify_mkdir(nd->path.dentry->d_inode, dentry);
648 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500649
650out_dput:
651 dput(dentry);
652out_dir:
Jan Blunck4ac91372008-02-14 19:34:32 -0800653 mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500654out:
655 return ret;
656}
657
658/* File system initialization */
659enum {
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000660 Opt_uid, Opt_gid, Opt_mode, Opt_debug, Opt_err,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500661};
662
Steven Whitehousea447c092008-10-13 10:46:57 +0100663static const match_table_t spufs_tokens = {
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000664 { Opt_uid, "uid=%d" },
665 { Opt_gid, "gid=%d" },
666 { Opt_mode, "mode=%o" },
667 { Opt_debug, "debug" },
668 { Opt_err, NULL },
Arnd Bergmann67207b92005-11-15 15:53:48 -0500669};
670
671static int
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000672spufs_parse_options(struct super_block *sb, char *options, struct inode *root)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500673{
674 char *p;
675 substring_t args[MAX_OPT_ARGS];
676
677 while ((p = strsep(&options, ",")) != NULL) {
678 int token, option;
679
680 if (!*p)
681 continue;
682
683 token = match_token(p, spufs_tokens, args);
684 switch (token) {
685 case Opt_uid:
686 if (match_int(&args[0], &option))
687 return 0;
688 root->i_uid = option;
689 break;
690 case Opt_gid:
691 if (match_int(&args[0], &option))
692 return 0;
693 root->i_gid = option;
694 break;
Jeremy Kerrf11f5ee2007-04-23 21:08:23 +0200695 case Opt_mode:
696 if (match_octal(&args[0], &option))
697 return 0;
698 root->i_mode = option | S_IFDIR;
699 break;
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000700 case Opt_debug:
701 spufs_get_sb_info(sb)->debug = 1;
702 break;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500703 default:
704 return 0;
705 }
706 }
707 return 1;
708}
709
Akinobu Mitadb1384b2007-04-23 21:08:20 +0200710static void spufs_exit_isolated_loader(void)
711{
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000712 free_pages((unsigned long) isolated_loader,
713 get_order(isolated_loader_size));
Akinobu Mitadb1384b2007-04-23 21:08:20 +0200714}
715
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200716static void
717spufs_init_isolated_loader(void)
718{
719 struct device_node *dn;
720 const char *loader;
721 int size;
722
723 dn = of_find_node_by_path("/spu-isolation");
724 if (!dn)
725 return;
726
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000727 loader = of_get_property(dn, "loader", &size);
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200728 if (!loader)
729 return;
730
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000731 /* the loader must be align on a 16 byte boundary */
732 isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200733 if (!isolated_loader)
734 return;
735
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000736 isolated_loader_size = size;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200737 memcpy(isolated_loader, loader, size);
738 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
739}
740
Arnd Bergmann67207b92005-11-15 15:53:48 -0500741static int
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500742spufs_create_root(struct super_block *sb, void *data)
743{
Arnd Bergmann67207b92005-11-15 15:53:48 -0500744 struct inode *inode;
745 int ret;
746
Arnd Bergmann8f18a152007-06-04 23:26:51 +1000747 ret = -ENODEV;
748 if (!spu_management_ops)
749 goto out;
750
Arnd Bergmann67207b92005-11-15 15:53:48 -0500751 ret = -ENOMEM;
752 inode = spufs_new_inode(sb, S_IFDIR | 0775);
753 if (!inode)
754 goto out;
755
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000756 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500757 inode->i_fop = &simple_dir_operations;
758 SPUFS_I(inode)->i_ctx = NULL;
Jeremy Kerre2ed6e42008-10-07 18:26:55 +1100759 inc_nlink(inode);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500760
761 ret = -EINVAL;
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000762 if (!spufs_parse_options(sb, data, inode))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500763 goto out_iput;
764
765 ret = -ENOMEM;
766 sb->s_root = d_alloc_root(inode);
767 if (!sb->s_root)
768 goto out_iput;
769
770 return 0;
771out_iput:
772 iput(inode);
773out:
774 return ret;
775}
776
777static int
778spufs_fill_super(struct super_block *sb, void *data, int silent)
779{
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000780 struct spufs_sb_info *info;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500781 static struct super_operations s_ops = {
782 .alloc_inode = spufs_alloc_inode,
783 .destroy_inode = spufs_destroy_inode,
784 .statfs = simple_statfs,
785 .delete_inode = spufs_delete_inode,
786 .drop_inode = generic_delete_inode,
Miklos Szeredi90d09e12008-02-08 04:21:47 -0800787 .show_options = generic_show_options,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500788 };
789
Miklos Szeredi90d09e12008-02-08 04:21:47 -0800790 save_mount_options(sb, data);
791
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000792 info = kzalloc(sizeof(*info), GFP_KERNEL);
793 if (!info)
794 return -ENOMEM;
795
Arnd Bergmann67207b92005-11-15 15:53:48 -0500796 sb->s_maxbytes = MAX_LFS_FILESIZE;
797 sb->s_blocksize = PAGE_CACHE_SIZE;
798 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
799 sb->s_magic = SPUFS_MAGIC;
800 sb->s_op = &s_ops;
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000801 sb->s_fs_info = info;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500802
803 return spufs_create_root(sb, data);
804}
805
David Howells454e2392006-06-23 02:02:57 -0700806static int
Arnd Bergmann67207b92005-11-15 15:53:48 -0500807spufs_get_sb(struct file_system_type *fstype, int flags,
David Howells454e2392006-06-23 02:02:57 -0700808 const char *name, void *data, struct vfsmount *mnt)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500809{
David Howells454e2392006-06-23 02:02:57 -0700810 return get_sb_single(fstype, flags, data, spufs_fill_super, mnt);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500811}
812
813static struct file_system_type spufs_type = {
814 .owner = THIS_MODULE,
815 .name = "spufs",
816 .get_sb = spufs_get_sb,
817 .kill_sb = kill_litter_super,
818};
819
Arnd Bergmanne78b47a2006-03-27 21:27:40 +0200820static int __init spufs_init(void)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500821{
822 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100823
Jeremy Kerrccf17e92007-04-23 21:08:29 +0200824 ret = -ENODEV;
825 if (!spu_management_ops)
826 goto out;
827
Arnd Bergmann67207b92005-11-15 15:53:48 -0500828 ret = -ENOMEM;
829 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
830 sizeof(struct spufs_inode_info), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900831 SLAB_HWCACHE_ALIGN, spufs_init_once);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500832
833 if (!spufs_inode_cache)
834 goto out;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200835 ret = spu_sched_init();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500836 if (ret)
837 goto out_cache;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200838 ret = register_filesystem(&spufs_type);
839 if (ret)
840 goto out_sched;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500841 ret = register_spu_syscalls(&spufs_calls);
842 if (ret)
843 goto out_fs;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200844
845 spufs_init_isolated_loader();
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100846
Arnd Bergmann67207b92005-11-15 15:53:48 -0500847 return 0;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200848
Arnd Bergmann67207b92005-11-15 15:53:48 -0500849out_fs:
850 unregister_filesystem(&spufs_type);
Akinobu Mitac99c1992007-04-23 21:08:19 +0200851out_sched:
852 spu_sched_exit();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500853out_cache:
854 kmem_cache_destroy(spufs_inode_cache);
855out:
856 return ret;
857}
858module_init(spufs_init);
859
Arnd Bergmanne78b47a2006-03-27 21:27:40 +0200860static void __exit spufs_exit(void)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500861{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500862 spu_sched_exit();
Akinobu Mitadb1384b2007-04-23 21:08:20 +0200863 spufs_exit_isolated_loader();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500864 unregister_spu_syscalls(&spufs_calls);
865 unregister_filesystem(&spufs_type);
866 kmem_cache_destroy(spufs_inode_cache);
867}
868module_exit(spufs_exit);
869
870MODULE_LICENSE("GPL");
871MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
872