blob: 32625f366fb5bbd8befd32e25db1b0f35a0654fb [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
146spufs_delete_inode(struct inode *inode)
147{
Arnd Bergmann62632032006-10-04 17:26:15 +0200148 struct spufs_inode_info *ei = SPUFS_I(inode);
149
150 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 clear_inode(inode);
155}
156
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100157static void spufs_prune_dir(struct dentry *dir)
158{
159 struct dentry *dentry, *tmp;
Arnd Bergmann62632032006-10-04 17:26:15 +0200160
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800161 mutex_lock(&dir->d_inode->i_mutex);
Andrew Mortonc3a9aea2006-01-09 20:51:24 -0800162 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100163 spin_lock(&dcache_lock);
164 spin_lock(&dentry->d_lock);
165 if (!(d_unhashed(dentry)) && dentry->d_inode) {
166 dget_locked(dentry);
167 __d_drop(dentry);
168 spin_unlock(&dentry->d_lock);
169 simple_unlink(dir->d_inode, dentry);
170 spin_unlock(&dcache_lock);
171 dput(dentry);
172 } else {
173 spin_unlock(&dentry->d_lock);
174 spin_unlock(&dcache_lock);
175 }
176 }
177 shrink_dcache_parent(dir);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800178 mutex_unlock(&dir->d_inode->i_mutex);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100179}
180
Arnd Bergmann62632032006-10-04 17:26:15 +0200181/* Caller must hold parent->i_mutex */
182static int spufs_rmdir(struct inode *parent, struct dentry *dir)
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100183{
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100184 /* remove all entries */
Arnd Bergmann62632032006-10-04 17:26:15 +0200185 spufs_prune_dir(dir);
Jeremy Kerrc443aca2007-11-16 13:32:23 +1100186 d_drop(dir);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100187
Arnd Bergmann62632032006-10-04 17:26:15 +0200188 return simple_rmdir(parent, dir);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100189}
190
Jeremy Kerr74254642009-02-17 11:44:14 +1100191static int spufs_fill_dir(struct dentry *dir,
192 const struct spufs_tree_descr *files, int mode,
193 struct spu_context *ctx)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500194{
Sebastian Siewior87873c82007-06-06 14:03:58 +1000195 struct dentry *dentry, *tmp;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500196 int ret;
197
198 while (files->name && files->name[0]) {
199 ret = -ENOMEM;
200 dentry = d_alloc_name(dir, files->name);
201 if (!dentry)
202 goto out;
203 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
Jeremy Kerr23d893f2008-06-30 12:17:28 +1000204 files->mode & mode, files->size, ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500205 if (ret)
206 goto out;
207 files++;
208 }
209 return 0;
210out:
Sebastian Siewior87873c82007-06-06 14:03:58 +1000211 /*
212 * remove all children from dir. dir->inode is not set so don't
213 * just simply use spufs_prune_dir() and panic afterwards :)
214 * dput() looks like it will do the right thing:
215 * - dec parent's ref counter
216 * - remove child from parent's child list
217 * - free child's inode if possible
218 * - free child
219 */
220 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
221 dput(dentry);
222 }
223
224 shrink_dcache_parent(dir);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500225 return ret;
226}
227
Arnd Bergmann67207b92005-11-15 15:53:48 -0500228static int spufs_dir_close(struct inode *inode, struct file *file)
229{
Michael Ellerman0309f022006-06-19 20:33:22 +0200230 struct spu_context *ctx;
Arnd Bergmann62632032006-10-04 17:26:15 +0200231 struct inode *parent;
232 struct dentry *dir;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500233 int ret;
234
Josef Sipekb4d1ab52006-12-08 02:37:30 -0800235 dir = file->f_path.dentry;
Arnd Bergmann62632032006-10-04 17:26:15 +0200236 parent = dir->d_parent->d_inode;
237 ctx = SPUFS_I(dir->d_inode)->i_ctx;
Arnd Bergmannc8ca0632006-01-04 20:31:22 +0100238
Christoph Hellwig02539d72008-05-08 15:29:12 +1000239 mutex_lock_nested(&parent->i_mutex, I_MUTEX_PARENT);
Arnd Bergmann62632032006-10-04 17:26:15 +0200240 ret = spufs_rmdir(parent, dir);
241 mutex_unlock(&parent->i_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500242 WARN_ON(ret);
Arnd Bergmannc8ca0632006-01-04 20:31:22 +0100243
Michael Ellerman0309f022006-06-19 20:33:22 +0200244 /* We have to give up the mm_struct */
245 spu_forget(ctx);
246
Arnd Bergmann67207b92005-11-15 15:53:48 -0500247 return dcache_dir_close(inode, file);
248}
249
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800250const struct file_operations spufs_context_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500251 .open = dcache_dir_open,
252 .release = spufs_dir_close,
253 .llseek = dcache_dir_lseek,
254 .read = generic_read_dir,
255 .readdir = dcache_readdir,
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200256 .fsync = noop_fsync,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500257};
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100258EXPORT_SYMBOL_GPL(spufs_context_fops);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500259
260static int
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200261spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
262 int mode)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500263{
264 int ret;
265 struct inode *inode;
266 struct spu_context *ctx;
267
268 ret = -ENOSPC;
269 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
270 if (!inode)
271 goto out;
272
273 if (dir->i_mode & S_ISGID) {
274 inode->i_gid = dir->i_gid;
275 inode->i_mode &= S_ISGID;
276 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200277 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500278 SPUFS_I(inode)->i_ctx = ctx;
279 if (!ctx)
280 goto out_iput;
281
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200282 ctx->flags = flags;
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000283 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500284 inode->i_fop = &simple_dir_operations;
Mark Nutter5737edd2006-10-24 18:31:16 +0200285 if (flags & SPU_CREATE_NOSCHED)
286 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
287 mode, ctx);
288 else
289 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
290
Arnd Bergmann67207b92005-11-15 15:53:48 -0500291 if (ret)
292 goto out_free_ctx;
293
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000294 if (spufs_get_sb_info(dir->i_sb)->debug)
295 ret = spufs_fill_dir(dentry, spufs_dir_debug_contents,
296 mode, ctx);
297
298 if (ret)
299 goto out_free_ctx;
300
Arnd Bergmann67207b92005-11-15 15:53:48 -0500301 d_instantiate(dentry, inode);
302 dget(dentry);
Jeremy Kerrba0b9962008-10-09 10:39:21 +1100303 inc_nlink(dir);
304 inc_nlink(dentry->d_inode);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500305 goto out;
306
307out_free_ctx:
Sebastian Siewior89df0082007-06-04 23:26:51 +1000308 spu_forget(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500309 put_spu_context(ctx);
310out_iput:
311 iput(inode);
312out:
313 return ret;
314}
315
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100316static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
317{
318 int ret;
319 struct file *filp;
320
321 ret = get_unused_fd();
322 if (ret < 0) {
323 dput(dentry);
324 mntput(mnt);
325 goto out;
326 }
327
David Howells745ca242008-11-14 10:39:22 +1100328 filp = dentry_open(dentry, mnt, O_RDONLY, current_cred());
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100329 if (IS_ERR(filp)) {
330 put_unused_fd(ret);
331 ret = PTR_ERR(filp);
332 goto out;
333 }
334
335 filp->f_op = &spufs_context_fops;
336 fd_install(ret, filp);
337out:
338 return ret;
339}
340
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200341static struct spu_context *
342spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
343 struct file *filp)
344{
Andre Detsch58119062008-01-11 15:03:26 +1100345 struct spu_context *tmp, *neighbor, *err;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200346 int count, node;
347 int aff_supp;
348
349 aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
350 struct spu, cbe_list))->aff_list);
351
352 if (!aff_supp)
353 return ERR_PTR(-EINVAL);
354
355 if (flags & SPU_CREATE_GANG)
356 return ERR_PTR(-EINVAL);
357
358 if (flags & SPU_CREATE_AFFINITY_MEM &&
359 gang->aff_ref_ctx &&
360 gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
361 return ERR_PTR(-EEXIST);
362
363 if (gang->aff_flags & AFF_MERGED)
364 return ERR_PTR(-EBUSY);
365
366 neighbor = NULL;
367 if (flags & SPU_CREATE_AFFINITY_SPU) {
368 if (!filp || filp->f_op != &spufs_context_fops)
369 return ERR_PTR(-EINVAL);
370
371 neighbor = get_spu_context(
372 SPUFS_I(filp->f_dentry->d_inode)->i_ctx);
373
374 if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
375 !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
376 !list_entry(neighbor->aff_list.next, struct spu_context,
Andre Detsch58119062008-01-11 15:03:26 +1100377 aff_list)->aff_head) {
378 err = ERR_PTR(-EEXIST);
379 goto out_put_neighbor;
380 }
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200381
Andre Detsch58119062008-01-11 15:03:26 +1100382 if (gang != neighbor->gang) {
383 err = ERR_PTR(-EINVAL);
384 goto out_put_neighbor;
385 }
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200386
387 count = 1;
388 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
389 count++;
390 if (list_empty(&neighbor->aff_list))
391 count++;
392
393 for (node = 0; node < MAX_NUMNODES; node++) {
394 if ((cbe_spu_info[node].n_spus - atomic_read(
395 &cbe_spu_info[node].reserved_spus)) >= count)
396 break;
397 }
398
Andre Detsch58119062008-01-11 15:03:26 +1100399 if (node == MAX_NUMNODES) {
400 err = ERR_PTR(-EEXIST);
401 goto out_put_neighbor;
402 }
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200403 }
404
405 return neighbor;
Andre Detsch58119062008-01-11 15:03:26 +1100406
407out_put_neighbor:
408 put_spu_context(neighbor);
409 return err;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200410}
411
412static void
413spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
414 struct spu_context *neighbor)
415{
416 if (flags & SPU_CREATE_AFFINITY_MEM)
417 ctx->gang->aff_ref_ctx = ctx;
418
419 if (flags & SPU_CREATE_AFFINITY_SPU) {
420 if (list_empty(&neighbor->aff_list)) {
421 list_add_tail(&neighbor->aff_list,
422 &ctx->gang->aff_list_head);
423 neighbor->aff_head = 1;
424 }
425
426 if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
427 || list_entry(neighbor->aff_list.next, struct spu_context,
428 aff_list)->aff_head) {
429 list_add(&ctx->aff_list, &neighbor->aff_list);
430 } else {
431 list_add_tail(&ctx->aff_list, &neighbor->aff_list);
432 if (neighbor->aff_head) {
433 neighbor->aff_head = 0;
434 ctx->aff_head = 1;
435 }
436 }
437
438 if (!ctx->gang->aff_ref_ctx)
439 ctx->gang->aff_ref_ctx = ctx;
440 }
441}
442
443static int
444spufs_create_context(struct inode *inode, struct dentry *dentry,
445 struct vfsmount *mnt, int flags, int mode,
446 struct file *aff_filp)
Arnd Bergmann62632032006-10-04 17:26:15 +0200447{
448 int ret;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200449 int affinity;
450 struct spu_gang *gang;
451 struct spu_context *neighbor;
Arnd Bergmann62632032006-10-04 17:26:15 +0200452
Mark Nutter5737edd2006-10-24 18:31:16 +0200453 ret = -EPERM;
454 if ((flags & SPU_CREATE_NOSCHED) &&
455 !capable(CAP_SYS_NICE))
456 goto out_unlock;
457
458 ret = -EINVAL;
459 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
460 == SPU_CREATE_ISOLATE)
461 goto out_unlock;
462
Jeremy Kerrbd2e5f82006-11-27 19:18:52 +0100463 ret = -ENODEV;
464 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
465 goto out_unlock;
466
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200467 gang = NULL;
468 neighbor = NULL;
469 affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
470 if (affinity) {
471 gang = SPUFS_I(inode)->i_gang;
472 ret = -EINVAL;
473 if (!gang)
474 goto out_unlock;
475 mutex_lock(&gang->aff_mutex);
476 neighbor = spufs_assert_affinity(flags, gang, aff_filp);
477 if (IS_ERR(neighbor)) {
478 ret = PTR_ERR(neighbor);
479 goto out_aff_unlock;
480 }
481 }
482
Arnd Bergmann62632032006-10-04 17:26:15 +0200483 ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
484 if (ret)
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200485 goto out_aff_unlock;
486
Andre Detsch58119062008-01-11 15:03:26 +1100487 if (affinity) {
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200488 spufs_set_affinity(flags, SPUFS_I(dentry->d_inode)->i_ctx,
489 neighbor);
Andre Detsch58119062008-01-11 15:03:26 +1100490 if (neighbor)
491 put_spu_context(neighbor);
492 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200493
494 /*
495 * get references for dget and mntget, will be released
496 * in error path of *_open().
497 */
498 ret = spufs_context_open(dget(dentry), mntget(mnt));
499 if (ret < 0) {
500 WARN_ON(spufs_rmdir(inode, dentry));
Kou Ishizaki6747c2e2008-10-09 10:45:49 +1100501 if (affinity)
502 mutex_unlock(&gang->aff_mutex);
Arnd Bergmann62632032006-10-04 17:26:15 +0200503 mutex_unlock(&inode->i_mutex);
504 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
505 goto out;
506 }
507
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200508out_aff_unlock:
509 if (affinity)
510 mutex_unlock(&gang->aff_mutex);
Arnd Bergmann62632032006-10-04 17:26:15 +0200511out_unlock:
512 mutex_unlock(&inode->i_mutex);
513out:
514 dput(dentry);
515 return ret;
516}
517
Arnd Bergmann62632032006-10-04 17:26:15 +0200518static int
519spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
520{
521 int ret;
522 struct inode *inode;
523 struct spu_gang *gang;
524
525 ret = -ENOSPC;
526 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
527 if (!inode)
528 goto out;
529
530 ret = 0;
531 if (dir->i_mode & S_ISGID) {
532 inode->i_gid = dir->i_gid;
533 inode->i_mode &= S_ISGID;
534 }
535 gang = alloc_spu_gang();
536 SPUFS_I(inode)->i_ctx = NULL;
537 SPUFS_I(inode)->i_gang = gang;
538 if (!gang)
539 goto out_iput;
540
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000541 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann62632032006-10-04 17:26:15 +0200542 inode->i_fop = &simple_dir_operations;
543
544 d_instantiate(dentry, inode);
Jeremy Kerrba0b9962008-10-09 10:39:21 +1100545 inc_nlink(dir);
546 inc_nlink(dentry->d_inode);
Arnd Bergmann62632032006-10-04 17:26:15 +0200547 return ret;
548
549out_iput:
550 iput(inode);
551out:
552 return ret;
553}
554
555static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
556{
557 int ret;
558 struct file *filp;
559
560 ret = get_unused_fd();
561 if (ret < 0) {
562 dput(dentry);
563 mntput(mnt);
564 goto out;
565 }
566
David Howells745ca242008-11-14 10:39:22 +1100567 filp = dentry_open(dentry, mnt, O_RDONLY, current_cred());
Arnd Bergmann62632032006-10-04 17:26:15 +0200568 if (IS_ERR(filp)) {
569 put_unused_fd(ret);
570 ret = PTR_ERR(filp);
571 goto out;
572 }
573
Jeremy Kerr877907d2007-06-04 23:26:51 +1000574 filp->f_op = &simple_dir_operations;
Arnd Bergmann62632032006-10-04 17:26:15 +0200575 fd_install(ret, filp);
576out:
577 return ret;
578}
579
580static int spufs_create_gang(struct inode *inode,
581 struct dentry *dentry,
582 struct vfsmount *mnt, int mode)
583{
584 int ret;
585
586 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
587 if (ret)
588 goto out;
589
590 /*
591 * get references for dget and mntget, will be released
592 * in error path of *_open().
593 */
594 ret = spufs_gang_open(dget(dentry), mntget(mnt));
Jeremy Kerr877907d2007-06-04 23:26:51 +1000595 if (ret < 0) {
596 int err = simple_rmdir(inode, dentry);
597 WARN_ON(err);
598 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200599
600out:
601 mutex_unlock(&inode->i_mutex);
602 dput(dentry);
603 return ret;
604}
605
606
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100607static struct file_system_type spufs_type;
608
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200609long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode,
610 struct file *filp)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500611{
612 struct dentry *dentry;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500613 int ret;
614
Arnd Bergmann67207b92005-11-15 15:53:48 -0500615 ret = -EINVAL;
Arnd Bergmann62632032006-10-04 17:26:15 +0200616 /* check if we are on spufs */
Jan Blunck4ac91372008-02-14 19:34:32 -0800617 if (nd->path.dentry->d_sb->s_type != &spufs_type)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500618 goto out;
619
Arnd Bergmann62632032006-10-04 17:26:15 +0200620 /* don't accept undefined flags */
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200621 if (flags & (~SPU_CREATE_FLAG_ALL))
arnd@arndb.dec9832942006-06-19 20:33:34 +0200622 goto out;
623
Arnd Bergmann62632032006-10-04 17:26:15 +0200624 /* only threads can be underneath a gang */
Jan Blunck4ac91372008-02-14 19:34:32 -0800625 if (nd->path.dentry != nd->path.dentry->d_sb->s_root) {
Arnd Bergmann62632032006-10-04 17:26:15 +0200626 if ((flags & SPU_CREATE_GANG) ||
Jan Blunck4ac91372008-02-14 19:34:32 -0800627 !SPUFS_I(nd->path.dentry->d_inode)->i_gang)
Arnd Bergmann62632032006-10-04 17:26:15 +0200628 goto out;
629 }
630
Arnd Bergmann67207b92005-11-15 15:53:48 -0500631 dentry = lookup_create(nd, 1);
632 ret = PTR_ERR(dentry);
633 if (IS_ERR(dentry))
634 goto out_dir;
635
Al Viroce3b0f82009-03-29 19:08:22 -0400636 mode &= ~current_umask();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500637
Arnd Bergmann62632032006-10-04 17:26:15 +0200638 if (flags & SPU_CREATE_GANG)
Christoph Hellwig826be062008-05-06 09:24:24 +1000639 ret = spufs_create_gang(nd->path.dentry->d_inode,
Jan Blunck4ac91372008-02-14 19:34:32 -0800640 dentry, nd->path.mnt, mode);
Arnd Bergmann62632032006-10-04 17:26:15 +0200641 else
Christoph Hellwig826be062008-05-06 09:24:24 +1000642 ret = spufs_create_context(nd->path.dentry->d_inode,
Jan Blunck4ac91372008-02-14 19:34:32 -0800643 dentry, nd->path.mnt, flags, mode,
644 filp);
Christoph Hellwig826be062008-05-06 09:24:24 +1000645 if (ret >= 0)
646 fsnotify_mkdir(nd->path.dentry->d_inode, dentry);
647 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500648
Arnd Bergmann67207b92005-11-15 15:53:48 -0500649out_dir:
Jan Blunck4ac91372008-02-14 19:34:32 -0800650 mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500651out:
652 return ret;
653}
654
655/* File system initialization */
656enum {
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000657 Opt_uid, Opt_gid, Opt_mode, Opt_debug, Opt_err,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500658};
659
Steven Whitehousea447c092008-10-13 10:46:57 +0100660static const match_table_t spufs_tokens = {
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000661 { Opt_uid, "uid=%d" },
662 { Opt_gid, "gid=%d" },
663 { Opt_mode, "mode=%o" },
664 { Opt_debug, "debug" },
665 { Opt_err, NULL },
Arnd Bergmann67207b92005-11-15 15:53:48 -0500666};
667
668static int
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000669spufs_parse_options(struct super_block *sb, char *options, struct inode *root)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500670{
671 char *p;
672 substring_t args[MAX_OPT_ARGS];
673
674 while ((p = strsep(&options, ",")) != NULL) {
675 int token, option;
676
677 if (!*p)
678 continue;
679
680 token = match_token(p, spufs_tokens, args);
681 switch (token) {
682 case Opt_uid:
683 if (match_int(&args[0], &option))
684 return 0;
685 root->i_uid = option;
686 break;
687 case Opt_gid:
688 if (match_int(&args[0], &option))
689 return 0;
690 root->i_gid = option;
691 break;
Jeremy Kerrf11f5ee2007-04-23 21:08:23 +0200692 case Opt_mode:
693 if (match_octal(&args[0], &option))
694 return 0;
695 root->i_mode = option | S_IFDIR;
696 break;
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000697 case Opt_debug:
698 spufs_get_sb_info(sb)->debug = 1;
699 break;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500700 default:
701 return 0;
702 }
703 }
704 return 1;
705}
706
Akinobu Mitadb1384b402007-04-23 21:08:20 +0200707static void spufs_exit_isolated_loader(void)
708{
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000709 free_pages((unsigned long) isolated_loader,
710 get_order(isolated_loader_size));
Akinobu Mitadb1384b402007-04-23 21:08:20 +0200711}
712
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200713static void
714spufs_init_isolated_loader(void)
715{
716 struct device_node *dn;
717 const char *loader;
718 int size;
719
720 dn = of_find_node_by_path("/spu-isolation");
721 if (!dn)
722 return;
723
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000724 loader = of_get_property(dn, "loader", &size);
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200725 if (!loader)
726 return;
727
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000728 /* the loader must be align on a 16 byte boundary */
729 isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200730 if (!isolated_loader)
731 return;
732
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000733 isolated_loader_size = size;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200734 memcpy(isolated_loader, loader, size);
735 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
736}
737
Arnd Bergmann67207b92005-11-15 15:53:48 -0500738static int
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500739spufs_create_root(struct super_block *sb, void *data)
740{
Arnd Bergmann67207b92005-11-15 15:53:48 -0500741 struct inode *inode;
742 int ret;
743
Arnd Bergmann8f18a152007-06-04 23:26:51 +1000744 ret = -ENODEV;
745 if (!spu_management_ops)
746 goto out;
747
Arnd Bergmann67207b92005-11-15 15:53:48 -0500748 ret = -ENOMEM;
749 inode = spufs_new_inode(sb, S_IFDIR | 0775);
750 if (!inode)
751 goto out;
752
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000753 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500754 inode->i_fop = &simple_dir_operations;
755 SPUFS_I(inode)->i_ctx = NULL;
Jeremy Kerre2ed6e42008-10-07 18:26:55 +1100756 inc_nlink(inode);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500757
758 ret = -EINVAL;
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000759 if (!spufs_parse_options(sb, data, inode))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500760 goto out_iput;
761
762 ret = -ENOMEM;
763 sb->s_root = d_alloc_root(inode);
764 if (!sb->s_root)
765 goto out_iput;
766
767 return 0;
768out_iput:
769 iput(inode);
770out:
771 return ret;
772}
773
774static int
775spufs_fill_super(struct super_block *sb, void *data, int silent)
776{
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000777 struct spufs_sb_info *info;
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700778 static const struct super_operations s_ops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500779 .alloc_inode = spufs_alloc_inode,
780 .destroy_inode = spufs_destroy_inode,
781 .statfs = simple_statfs,
782 .delete_inode = spufs_delete_inode,
783 .drop_inode = generic_delete_inode,
Miklos Szeredi90d09e12008-02-08 04:21:47 -0800784 .show_options = generic_show_options,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500785 };
786
Miklos Szeredi90d09e12008-02-08 04:21:47 -0800787 save_mount_options(sb, data);
788
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000789 info = kzalloc(sizeof(*info), GFP_KERNEL);
790 if (!info)
791 return -ENOMEM;
792
Arnd Bergmann67207b92005-11-15 15:53:48 -0500793 sb->s_maxbytes = MAX_LFS_FILESIZE;
794 sb->s_blocksize = PAGE_CACHE_SIZE;
795 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
796 sb->s_magic = SPUFS_MAGIC;
797 sb->s_op = &s_ops;
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000798 sb->s_fs_info = info;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500799
800 return spufs_create_root(sb, data);
801}
802
David Howells454e2392006-06-23 02:02:57 -0700803static int
Arnd Bergmann67207b92005-11-15 15:53:48 -0500804spufs_get_sb(struct file_system_type *fstype, int flags,
David Howells454e2392006-06-23 02:02:57 -0700805 const char *name, void *data, struct vfsmount *mnt)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500806{
David Howells454e2392006-06-23 02:02:57 -0700807 return get_sb_single(fstype, flags, data, spufs_fill_super, mnt);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500808}
809
810static struct file_system_type spufs_type = {
811 .owner = THIS_MODULE,
812 .name = "spufs",
813 .get_sb = spufs_get_sb,
814 .kill_sb = kill_litter_super,
815};
816
Arnd Bergmanne78b47a2006-03-27 21:27:40 +0200817static int __init spufs_init(void)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500818{
819 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100820
Jeremy Kerrccf17e92007-04-23 21:08:29 +0200821 ret = -ENODEV;
822 if (!spu_management_ops)
823 goto out;
824
Arnd Bergmann67207b92005-11-15 15:53:48 -0500825 ret = -ENOMEM;
826 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
827 sizeof(struct spufs_inode_info), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900828 SLAB_HWCACHE_ALIGN, spufs_init_once);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500829
830 if (!spufs_inode_cache)
831 goto out;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200832 ret = spu_sched_init();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500833 if (ret)
834 goto out_cache;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200835 ret = register_filesystem(&spufs_type);
836 if (ret)
837 goto out_sched;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500838 ret = register_spu_syscalls(&spufs_calls);
839 if (ret)
840 goto out_fs;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200841
842 spufs_init_isolated_loader();
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100843
Arnd Bergmann67207b92005-11-15 15:53:48 -0500844 return 0;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200845
Arnd Bergmann67207b92005-11-15 15:53:48 -0500846out_fs:
847 unregister_filesystem(&spufs_type);
Akinobu Mitac99c1992007-04-23 21:08:19 +0200848out_sched:
849 spu_sched_exit();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500850out_cache:
851 kmem_cache_destroy(spufs_inode_cache);
852out:
853 return ret;
854}
855module_init(spufs_init);
856
Arnd Bergmanne78b47a2006-03-27 21:27:40 +0200857static void __exit spufs_exit(void)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500858{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500859 spu_sched_exit();
Akinobu Mitadb1384b402007-04-23 21:08:20 +0200860 spufs_exit_isolated_loader();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500861 unregister_spu_syscalls(&spufs_calls);
862 unregister_filesystem(&spufs_type);
863 kmem_cache_destroy(spufs_inode_cache);
864}
865module_exit(spufs_exit);
866
867MODULE_LICENSE("GPL");
868MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
869