blob: 5aef1a7f5e4b4e5a8722afc614e6d612d9397c44 [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;
Christoph Hellwig10257742010-06-04 11:30:02 +0200113 setattr_copy(inode, attr);
114 mark_inode_dirty(inode);
115 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500116}
117
118
119static int
120spufs_new_file(struct super_block *sb, struct dentry *dentry,
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800121 const struct file_operations *fops, int mode,
Jeremy Kerr23d893f2008-06-30 12:17:28 +1000122 size_t size, struct spu_context *ctx)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500123{
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700124 static const struct inode_operations spufs_file_iops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500125 .setattr = spufs_setattr,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500126 };
127 struct inode *inode;
128 int ret;
129
130 ret = -ENOSPC;
131 inode = spufs_new_inode(sb, S_IFREG | mode);
132 if (!inode)
133 goto out;
134
135 ret = 0;
136 inode->i_op = &spufs_file_iops;
137 inode->i_fop = fops;
Jeremy Kerr23d893f2008-06-30 12:17:28 +1000138 inode->i_size = size;
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700139 inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500140 d_add(dentry, inode);
141out:
142 return ret;
143}
144
145static void
Al Viro0f3f63a2010-06-05 21:20:32 -0400146spufs_evict_inode(struct inode *inode)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500147{
Arnd Bergmann62632032006-10-04 17:26:15 +0200148 struct spufs_inode_info *ei = SPUFS_I(inode);
Al Viro0f3f63a2010-06-05 21:20:32 -0400149 end_writeback(inode);
Arnd Bergmann62632032006-10-04 17:26:15 +0200150 if (ei->i_ctx)
151 put_spu_context(ei->i_ctx);
152 if (ei->i_gang)
153 put_spu_gang(ei->i_gang);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500154}
155
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100156static void spufs_prune_dir(struct dentry *dir)
157{
158 struct dentry *dentry, *tmp;
Arnd Bergmann62632032006-10-04 17:26:15 +0200159
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800160 mutex_lock(&dir->d_inode->i_mutex);
Andrew Mortonc3a9aea2006-01-09 20:51:24 -0800161 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100162 spin_lock(&dcache_lock);
163 spin_lock(&dentry->d_lock);
164 if (!(d_unhashed(dentry)) && dentry->d_inode) {
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100165 dget_locked_dlock(dentry);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100166 __d_drop(dentry);
167 spin_unlock(&dentry->d_lock);
168 simple_unlink(dir->d_inode, dentry);
Nick Pigginda502952011-01-07 17:49:33 +1100169 /* XXX: what is dcache_lock protecting here? Other
170 * filesystems (IB, configfs) release dcache_lock
171 * before unlink */
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100172 spin_unlock(&dcache_lock);
173 dput(dentry);
174 } else {
175 spin_unlock(&dentry->d_lock);
176 spin_unlock(&dcache_lock);
177 }
178 }
179 shrink_dcache_parent(dir);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800180 mutex_unlock(&dir->d_inode->i_mutex);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100181}
182
Arnd Bergmann62632032006-10-04 17:26:15 +0200183/* Caller must hold parent->i_mutex */
184static int spufs_rmdir(struct inode *parent, struct dentry *dir)
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100185{
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100186 /* remove all entries */
Arnd Bergmann62632032006-10-04 17:26:15 +0200187 spufs_prune_dir(dir);
Jeremy Kerrc443aca2007-11-16 13:32:23 +1100188 d_drop(dir);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100189
Arnd Bergmann62632032006-10-04 17:26:15 +0200190 return simple_rmdir(parent, dir);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100191}
192
Jeremy Kerr74254642009-02-17 11:44:14 +1100193static int spufs_fill_dir(struct dentry *dir,
194 const struct spufs_tree_descr *files, int mode,
195 struct spu_context *ctx)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500196{
Sebastian Siewior87873c82007-06-06 14:03:58 +1000197 struct dentry *dentry, *tmp;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500198 int ret;
199
200 while (files->name && files->name[0]) {
201 ret = -ENOMEM;
202 dentry = d_alloc_name(dir, files->name);
203 if (!dentry)
204 goto out;
205 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
Jeremy Kerr23d893f2008-06-30 12:17:28 +1000206 files->mode & mode, files->size, ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500207 if (ret)
208 goto out;
209 files++;
210 }
211 return 0;
212out:
Sebastian Siewior87873c82007-06-06 14:03:58 +1000213 /*
214 * remove all children from dir. dir->inode is not set so don't
215 * just simply use spufs_prune_dir() and panic afterwards :)
216 * dput() looks like it will do the right thing:
217 * - dec parent's ref counter
218 * - remove child from parent's child list
219 * - free child's inode if possible
220 * - free child
221 */
222 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
223 dput(dentry);
224 }
225
226 shrink_dcache_parent(dir);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500227 return ret;
228}
229
Arnd Bergmann67207b92005-11-15 15:53:48 -0500230static int spufs_dir_close(struct inode *inode, struct file *file)
231{
Michael Ellerman0309f022006-06-19 20:33:22 +0200232 struct spu_context *ctx;
Arnd Bergmann62632032006-10-04 17:26:15 +0200233 struct inode *parent;
234 struct dentry *dir;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500235 int ret;
236
Josef Sipekb4d1ab52006-12-08 02:37:30 -0800237 dir = file->f_path.dentry;
Arnd Bergmann62632032006-10-04 17:26:15 +0200238 parent = dir->d_parent->d_inode;
239 ctx = SPUFS_I(dir->d_inode)->i_ctx;
Arnd Bergmannc8ca0632006-01-04 20:31:22 +0100240
Christoph Hellwig02539d72008-05-08 15:29:12 +1000241 mutex_lock_nested(&parent->i_mutex, I_MUTEX_PARENT);
Arnd Bergmann62632032006-10-04 17:26:15 +0200242 ret = spufs_rmdir(parent, dir);
243 mutex_unlock(&parent->i_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500244 WARN_ON(ret);
Arnd Bergmannc8ca0632006-01-04 20:31:22 +0100245
Michael Ellerman0309f022006-06-19 20:33:22 +0200246 /* We have to give up the mm_struct */
247 spu_forget(ctx);
248
Arnd Bergmann67207b92005-11-15 15:53:48 -0500249 return dcache_dir_close(inode, file);
250}
251
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800252const struct file_operations spufs_context_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500253 .open = dcache_dir_open,
254 .release = spufs_dir_close,
255 .llseek = dcache_dir_lseek,
256 .read = generic_read_dir,
257 .readdir = dcache_readdir,
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200258 .fsync = noop_fsync,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500259};
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100260EXPORT_SYMBOL_GPL(spufs_context_fops);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500261
262static int
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200263spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
264 int mode)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500265{
266 int ret;
267 struct inode *inode;
268 struct spu_context *ctx;
269
270 ret = -ENOSPC;
271 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
272 if (!inode)
273 goto out;
274
275 if (dir->i_mode & S_ISGID) {
276 inode->i_gid = dir->i_gid;
277 inode->i_mode &= S_ISGID;
278 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200279 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500280 SPUFS_I(inode)->i_ctx = ctx;
281 if (!ctx)
282 goto out_iput;
283
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200284 ctx->flags = flags;
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000285 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500286 inode->i_fop = &simple_dir_operations;
Mark Nutter5737edd2006-10-24 18:31:16 +0200287 if (flags & SPU_CREATE_NOSCHED)
288 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
289 mode, ctx);
290 else
291 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
292
Arnd Bergmann67207b92005-11-15 15:53:48 -0500293 if (ret)
294 goto out_free_ctx;
295
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000296 if (spufs_get_sb_info(dir->i_sb)->debug)
297 ret = spufs_fill_dir(dentry, spufs_dir_debug_contents,
298 mode, ctx);
299
300 if (ret)
301 goto out_free_ctx;
302
Arnd Bergmann67207b92005-11-15 15:53:48 -0500303 d_instantiate(dentry, inode);
304 dget(dentry);
Jeremy Kerrba0b9962008-10-09 10:39:21 +1100305 inc_nlink(dir);
306 inc_nlink(dentry->d_inode);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500307 goto out;
308
309out_free_ctx:
Sebastian Siewior89df0082007-06-04 23:26:51 +1000310 spu_forget(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500311 put_spu_context(ctx);
312out_iput:
313 iput(inode);
314out:
315 return ret;
316}
317
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100318static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
319{
320 int ret;
321 struct file *filp;
322
323 ret = get_unused_fd();
324 if (ret < 0) {
325 dput(dentry);
326 mntput(mnt);
327 goto out;
328 }
329
David Howells745ca242008-11-14 10:39:22 +1100330 filp = dentry_open(dentry, mnt, O_RDONLY, current_cred());
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100331 if (IS_ERR(filp)) {
332 put_unused_fd(ret);
333 ret = PTR_ERR(filp);
334 goto out;
335 }
336
337 filp->f_op = &spufs_context_fops;
338 fd_install(ret, filp);
339out:
340 return ret;
341}
342
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200343static struct spu_context *
344spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
345 struct file *filp)
346{
Andre Detsch58119062008-01-11 15:03:26 +1100347 struct spu_context *tmp, *neighbor, *err;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200348 int count, node;
349 int aff_supp;
350
351 aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
352 struct spu, cbe_list))->aff_list);
353
354 if (!aff_supp)
355 return ERR_PTR(-EINVAL);
356
357 if (flags & SPU_CREATE_GANG)
358 return ERR_PTR(-EINVAL);
359
360 if (flags & SPU_CREATE_AFFINITY_MEM &&
361 gang->aff_ref_ctx &&
362 gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
363 return ERR_PTR(-EEXIST);
364
365 if (gang->aff_flags & AFF_MERGED)
366 return ERR_PTR(-EBUSY);
367
368 neighbor = NULL;
369 if (flags & SPU_CREATE_AFFINITY_SPU) {
370 if (!filp || filp->f_op != &spufs_context_fops)
371 return ERR_PTR(-EINVAL);
372
373 neighbor = get_spu_context(
374 SPUFS_I(filp->f_dentry->d_inode)->i_ctx);
375
376 if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
377 !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
378 !list_entry(neighbor->aff_list.next, struct spu_context,
Andre Detsch58119062008-01-11 15:03:26 +1100379 aff_list)->aff_head) {
380 err = ERR_PTR(-EEXIST);
381 goto out_put_neighbor;
382 }
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200383
Andre Detsch58119062008-01-11 15:03:26 +1100384 if (gang != neighbor->gang) {
385 err = ERR_PTR(-EINVAL);
386 goto out_put_neighbor;
387 }
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200388
389 count = 1;
390 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
391 count++;
392 if (list_empty(&neighbor->aff_list))
393 count++;
394
395 for (node = 0; node < MAX_NUMNODES; node++) {
396 if ((cbe_spu_info[node].n_spus - atomic_read(
397 &cbe_spu_info[node].reserved_spus)) >= count)
398 break;
399 }
400
Andre Detsch58119062008-01-11 15:03:26 +1100401 if (node == MAX_NUMNODES) {
402 err = ERR_PTR(-EEXIST);
403 goto out_put_neighbor;
404 }
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200405 }
406
407 return neighbor;
Andre Detsch58119062008-01-11 15:03:26 +1100408
409out_put_neighbor:
410 put_spu_context(neighbor);
411 return err;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200412}
413
414static void
415spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
416 struct spu_context *neighbor)
417{
418 if (flags & SPU_CREATE_AFFINITY_MEM)
419 ctx->gang->aff_ref_ctx = ctx;
420
421 if (flags & SPU_CREATE_AFFINITY_SPU) {
422 if (list_empty(&neighbor->aff_list)) {
423 list_add_tail(&neighbor->aff_list,
424 &ctx->gang->aff_list_head);
425 neighbor->aff_head = 1;
426 }
427
428 if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
429 || list_entry(neighbor->aff_list.next, struct spu_context,
430 aff_list)->aff_head) {
431 list_add(&ctx->aff_list, &neighbor->aff_list);
432 } else {
433 list_add_tail(&ctx->aff_list, &neighbor->aff_list);
434 if (neighbor->aff_head) {
435 neighbor->aff_head = 0;
436 ctx->aff_head = 1;
437 }
438 }
439
440 if (!ctx->gang->aff_ref_ctx)
441 ctx->gang->aff_ref_ctx = ctx;
442 }
443}
444
445static int
446spufs_create_context(struct inode *inode, struct dentry *dentry,
447 struct vfsmount *mnt, int flags, int mode,
448 struct file *aff_filp)
Arnd Bergmann62632032006-10-04 17:26:15 +0200449{
450 int ret;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200451 int affinity;
452 struct spu_gang *gang;
453 struct spu_context *neighbor;
Arnd Bergmann62632032006-10-04 17:26:15 +0200454
Mark Nutter5737edd2006-10-24 18:31:16 +0200455 ret = -EPERM;
456 if ((flags & SPU_CREATE_NOSCHED) &&
457 !capable(CAP_SYS_NICE))
458 goto out_unlock;
459
460 ret = -EINVAL;
461 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
462 == SPU_CREATE_ISOLATE)
463 goto out_unlock;
464
Jeremy Kerrbd2e5f82006-11-27 19:18:52 +0100465 ret = -ENODEV;
466 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
467 goto out_unlock;
468
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200469 gang = NULL;
470 neighbor = NULL;
471 affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
472 if (affinity) {
473 gang = SPUFS_I(inode)->i_gang;
474 ret = -EINVAL;
475 if (!gang)
476 goto out_unlock;
477 mutex_lock(&gang->aff_mutex);
478 neighbor = spufs_assert_affinity(flags, gang, aff_filp);
479 if (IS_ERR(neighbor)) {
480 ret = PTR_ERR(neighbor);
481 goto out_aff_unlock;
482 }
483 }
484
Arnd Bergmann62632032006-10-04 17:26:15 +0200485 ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
486 if (ret)
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200487 goto out_aff_unlock;
488
Andre Detsch58119062008-01-11 15:03:26 +1100489 if (affinity) {
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200490 spufs_set_affinity(flags, SPUFS_I(dentry->d_inode)->i_ctx,
491 neighbor);
Andre Detsch58119062008-01-11 15:03:26 +1100492 if (neighbor)
493 put_spu_context(neighbor);
494 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200495
496 /*
497 * get references for dget and mntget, will be released
498 * in error path of *_open().
499 */
500 ret = spufs_context_open(dget(dentry), mntget(mnt));
501 if (ret < 0) {
502 WARN_ON(spufs_rmdir(inode, dentry));
Kou Ishizaki6747c2e2008-10-09 10:45:49 +1100503 if (affinity)
504 mutex_unlock(&gang->aff_mutex);
Arnd Bergmann62632032006-10-04 17:26:15 +0200505 mutex_unlock(&inode->i_mutex);
506 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
507 goto out;
508 }
509
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200510out_aff_unlock:
511 if (affinity)
512 mutex_unlock(&gang->aff_mutex);
Arnd Bergmann62632032006-10-04 17:26:15 +0200513out_unlock:
514 mutex_unlock(&inode->i_mutex);
515out:
516 dput(dentry);
517 return ret;
518}
519
Arnd Bergmann62632032006-10-04 17:26:15 +0200520static int
521spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
522{
523 int ret;
524 struct inode *inode;
525 struct spu_gang *gang;
526
527 ret = -ENOSPC;
528 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
529 if (!inode)
530 goto out;
531
532 ret = 0;
533 if (dir->i_mode & S_ISGID) {
534 inode->i_gid = dir->i_gid;
535 inode->i_mode &= S_ISGID;
536 }
537 gang = alloc_spu_gang();
538 SPUFS_I(inode)->i_ctx = NULL;
539 SPUFS_I(inode)->i_gang = gang;
540 if (!gang)
541 goto out_iput;
542
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000543 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann62632032006-10-04 17:26:15 +0200544 inode->i_fop = &simple_dir_operations;
545
546 d_instantiate(dentry, inode);
Jeremy Kerrba0b9962008-10-09 10:39:21 +1100547 inc_nlink(dir);
548 inc_nlink(dentry->d_inode);
Arnd Bergmann62632032006-10-04 17:26:15 +0200549 return ret;
550
551out_iput:
552 iput(inode);
553out:
554 return ret;
555}
556
557static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
558{
559 int ret;
560 struct file *filp;
561
562 ret = get_unused_fd();
563 if (ret < 0) {
564 dput(dentry);
565 mntput(mnt);
566 goto out;
567 }
568
David Howells745ca242008-11-14 10:39:22 +1100569 filp = dentry_open(dentry, mnt, O_RDONLY, current_cred());
Arnd Bergmann62632032006-10-04 17:26:15 +0200570 if (IS_ERR(filp)) {
571 put_unused_fd(ret);
572 ret = PTR_ERR(filp);
573 goto out;
574 }
575
Jeremy Kerr877907d2007-06-04 23:26:51 +1000576 filp->f_op = &simple_dir_operations;
Arnd Bergmann62632032006-10-04 17:26:15 +0200577 fd_install(ret, filp);
578out:
579 return ret;
580}
581
582static int spufs_create_gang(struct inode *inode,
583 struct dentry *dentry,
584 struct vfsmount *mnt, int mode)
585{
586 int ret;
587
588 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
589 if (ret)
590 goto out;
591
592 /*
593 * get references for dget and mntget, will be released
594 * in error path of *_open().
595 */
596 ret = spufs_gang_open(dget(dentry), mntget(mnt));
Jeremy Kerr877907d2007-06-04 23:26:51 +1000597 if (ret < 0) {
598 int err = simple_rmdir(inode, dentry);
599 WARN_ON(err);
600 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200601
602out:
603 mutex_unlock(&inode->i_mutex);
604 dput(dentry);
605 return ret;
606}
607
608
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100609static struct file_system_type spufs_type;
610
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200611long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode,
612 struct file *filp)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500613{
614 struct dentry *dentry;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500615 int ret;
616
Arnd Bergmann67207b92005-11-15 15:53:48 -0500617 ret = -EINVAL;
Arnd Bergmann62632032006-10-04 17:26:15 +0200618 /* check if we are on spufs */
Jan Blunck4ac91372008-02-14 19:34:32 -0800619 if (nd->path.dentry->d_sb->s_type != &spufs_type)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500620 goto out;
621
Arnd Bergmann62632032006-10-04 17:26:15 +0200622 /* don't accept undefined flags */
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200623 if (flags & (~SPU_CREATE_FLAG_ALL))
arnd@arndb.dec9832942006-06-19 20:33:34 +0200624 goto out;
625
Arnd Bergmann62632032006-10-04 17:26:15 +0200626 /* only threads can be underneath a gang */
Jan Blunck4ac91372008-02-14 19:34:32 -0800627 if (nd->path.dentry != nd->path.dentry->d_sb->s_root) {
Arnd Bergmann62632032006-10-04 17:26:15 +0200628 if ((flags & SPU_CREATE_GANG) ||
Jan Blunck4ac91372008-02-14 19:34:32 -0800629 !SPUFS_I(nd->path.dentry->d_inode)->i_gang)
Arnd Bergmann62632032006-10-04 17:26:15 +0200630 goto out;
631 }
632
Arnd Bergmann67207b92005-11-15 15:53:48 -0500633 dentry = lookup_create(nd, 1);
634 ret = PTR_ERR(dentry);
635 if (IS_ERR(dentry))
636 goto out_dir;
637
Al Viroce3b0f82009-03-29 19:08:22 -0400638 mode &= ~current_umask();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500639
Arnd Bergmann62632032006-10-04 17:26:15 +0200640 if (flags & SPU_CREATE_GANG)
Christoph Hellwig826be062008-05-06 09:24:24 +1000641 ret = spufs_create_gang(nd->path.dentry->d_inode,
Jan Blunck4ac91372008-02-14 19:34:32 -0800642 dentry, nd->path.mnt, mode);
Arnd Bergmann62632032006-10-04 17:26:15 +0200643 else
Christoph Hellwig826be062008-05-06 09:24:24 +1000644 ret = spufs_create_context(nd->path.dentry->d_inode,
Jan Blunck4ac91372008-02-14 19:34:32 -0800645 dentry, nd->path.mnt, flags, mode,
646 filp);
Christoph Hellwig826be062008-05-06 09:24:24 +1000647 if (ret >= 0)
648 fsnotify_mkdir(nd->path.dentry->d_inode, dentry);
649 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500650
Arnd Bergmann67207b92005-11-15 15:53:48 -0500651out_dir:
Jan Blunck4ac91372008-02-14 19:34:32 -0800652 mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500653out:
654 return ret;
655}
656
657/* File system initialization */
658enum {
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000659 Opt_uid, Opt_gid, Opt_mode, Opt_debug, Opt_err,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500660};
661
Steven Whitehousea447c092008-10-13 10:46:57 +0100662static const match_table_t spufs_tokens = {
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000663 { Opt_uid, "uid=%d" },
664 { Opt_gid, "gid=%d" },
665 { Opt_mode, "mode=%o" },
666 { Opt_debug, "debug" },
667 { Opt_err, NULL },
Arnd Bergmann67207b92005-11-15 15:53:48 -0500668};
669
670static int
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000671spufs_parse_options(struct super_block *sb, char *options, struct inode *root)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500672{
673 char *p;
674 substring_t args[MAX_OPT_ARGS];
675
676 while ((p = strsep(&options, ",")) != NULL) {
677 int token, option;
678
679 if (!*p)
680 continue;
681
682 token = match_token(p, spufs_tokens, args);
683 switch (token) {
684 case Opt_uid:
685 if (match_int(&args[0], &option))
686 return 0;
687 root->i_uid = option;
688 break;
689 case Opt_gid:
690 if (match_int(&args[0], &option))
691 return 0;
692 root->i_gid = option;
693 break;
Jeremy Kerrf11f5ee2007-04-23 21:08:23 +0200694 case Opt_mode:
695 if (match_octal(&args[0], &option))
696 return 0;
697 root->i_mode = option | S_IFDIR;
698 break;
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000699 case Opt_debug:
700 spufs_get_sb_info(sb)->debug = 1;
701 break;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500702 default:
703 return 0;
704 }
705 }
706 return 1;
707}
708
Akinobu Mitadb1384b2007-04-23 21:08:20 +0200709static void spufs_exit_isolated_loader(void)
710{
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000711 free_pages((unsigned long) isolated_loader,
712 get_order(isolated_loader_size));
Akinobu Mitadb1384b2007-04-23 21:08:20 +0200713}
714
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200715static void
716spufs_init_isolated_loader(void)
717{
718 struct device_node *dn;
719 const char *loader;
720 int size;
721
722 dn = of_find_node_by_path("/spu-isolation");
723 if (!dn)
724 return;
725
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000726 loader = of_get_property(dn, "loader", &size);
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200727 if (!loader)
728 return;
729
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000730 /* the loader must be align on a 16 byte boundary */
731 isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200732 if (!isolated_loader)
733 return;
734
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000735 isolated_loader_size = size;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200736 memcpy(isolated_loader, loader, size);
737 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
738}
739
Arnd Bergmann67207b92005-11-15 15:53:48 -0500740static int
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500741spufs_create_root(struct super_block *sb, void *data)
742{
Arnd Bergmann67207b92005-11-15 15:53:48 -0500743 struct inode *inode;
744 int ret;
745
Arnd Bergmann8f18a152007-06-04 23:26:51 +1000746 ret = -ENODEV;
747 if (!spu_management_ops)
748 goto out;
749
Arnd Bergmann67207b92005-11-15 15:53:48 -0500750 ret = -ENOMEM;
751 inode = spufs_new_inode(sb, S_IFDIR | 0775);
752 if (!inode)
753 goto out;
754
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000755 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500756 inode->i_fop = &simple_dir_operations;
757 SPUFS_I(inode)->i_ctx = NULL;
Jeremy Kerre2ed6e42008-10-07 18:26:55 +1100758 inc_nlink(inode);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500759
760 ret = -EINVAL;
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000761 if (!spufs_parse_options(sb, data, inode))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500762 goto out_iput;
763
764 ret = -ENOMEM;
765 sb->s_root = d_alloc_root(inode);
766 if (!sb->s_root)
767 goto out_iput;
768
769 return 0;
770out_iput:
771 iput(inode);
772out:
773 return ret;
774}
775
776static int
777spufs_fill_super(struct super_block *sb, void *data, int silent)
778{
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000779 struct spufs_sb_info *info;
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700780 static const struct super_operations s_ops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500781 .alloc_inode = spufs_alloc_inode,
782 .destroy_inode = spufs_destroy_inode,
783 .statfs = simple_statfs,
Al Viro0f3f63a2010-06-05 21:20:32 -0400784 .evict_inode = spufs_evict_inode,
Miklos Szeredi90d09e12008-02-08 04:21:47 -0800785 .show_options = generic_show_options,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500786 };
787
Miklos Szeredi90d09e12008-02-08 04:21:47 -0800788 save_mount_options(sb, data);
789
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000790 info = kzalloc(sizeof(*info), GFP_KERNEL);
791 if (!info)
792 return -ENOMEM;
793
Arnd Bergmann67207b92005-11-15 15:53:48 -0500794 sb->s_maxbytes = MAX_LFS_FILESIZE;
795 sb->s_blocksize = PAGE_CACHE_SIZE;
796 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
797 sb->s_magic = SPUFS_MAGIC;
798 sb->s_op = &s_ops;
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000799 sb->s_fs_info = info;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500800
801 return spufs_create_root(sb, data);
802}
803
Al Virofc14f2f2010-07-25 01:48:30 +0400804static struct dentry *
805spufs_mount(struct file_system_type *fstype, int flags,
806 const char *name, void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500807{
Al Virofc14f2f2010-07-25 01:48:30 +0400808 return mount_single(fstype, flags, data, spufs_fill_super);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500809}
810
811static struct file_system_type spufs_type = {
812 .owner = THIS_MODULE,
813 .name = "spufs",
Al Virofc14f2f2010-07-25 01:48:30 +0400814 .mount = spufs_mount,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500815 .kill_sb = kill_litter_super,
816};
817
Arnd Bergmanne78b47a2006-03-27 21:27:40 +0200818static int __init spufs_init(void)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500819{
820 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100821
Jeremy Kerrccf17e92007-04-23 21:08:29 +0200822 ret = -ENODEV;
823 if (!spu_management_ops)
824 goto out;
825
Arnd Bergmann67207b92005-11-15 15:53:48 -0500826 ret = -ENOMEM;
827 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
828 sizeof(struct spufs_inode_info), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900829 SLAB_HWCACHE_ALIGN, spufs_init_once);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500830
831 if (!spufs_inode_cache)
832 goto out;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200833 ret = spu_sched_init();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500834 if (ret)
835 goto out_cache;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200836 ret = register_filesystem(&spufs_type);
837 if (ret)
838 goto out_sched;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500839 ret = register_spu_syscalls(&spufs_calls);
840 if (ret)
841 goto out_fs;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200842
843 spufs_init_isolated_loader();
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100844
Arnd Bergmann67207b92005-11-15 15:53:48 -0500845 return 0;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200846
Arnd Bergmann67207b92005-11-15 15:53:48 -0500847out_fs:
848 unregister_filesystem(&spufs_type);
Akinobu Mitac99c1992007-04-23 21:08:19 +0200849out_sched:
850 spu_sched_exit();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500851out_cache:
852 kmem_cache_destroy(spufs_inode_cache);
853out:
854 return ret;
855}
856module_init(spufs_init);
857
Arnd Bergmanne78b47a2006-03-27 21:27:40 +0200858static void __exit spufs_exit(void)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500859{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500860 spu_sched_exit();
Akinobu Mitadb1384b2007-04-23 21:08:20 +0200861 spufs_exit_isolated_loader();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500862 unregister_spu_syscalls(&spufs_calls);
863 unregister_filesystem(&spufs_type);
864 kmem_cache_destroy(spufs_inode_cache);
865}
866module_exit(spufs_exit);
867
868MODULE_LICENSE("GPL");
869MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
870