blob: 856e9c39806861768f1dff7d2d8ec4f1a0fb5076 [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
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +110074static void spufs_i_callback(struct rcu_head *head)
Arnd Bergmann67207b92005-11-15 15:53:48 -050075{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +110076 struct inode *inode = container_of(head, struct inode, i_rcu);
77 INIT_LIST_HEAD(&inode->i_dentry);
Arnd Bergmann67207b92005-11-15 15:53:48 -050078 kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
79}
80
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +110081static void spufs_destroy_inode(struct inode *inode)
82{
83 call_rcu(&inode->i_rcu, spufs_i_callback);
84}
85
Arnd Bergmann67207b92005-11-15 15:53:48 -050086static void
Alexey Dobriyan51cc5062008-07-25 19:45:34 -070087spufs_init_once(void *p)
Arnd Bergmann67207b92005-11-15 15:53:48 -050088{
89 struct spufs_inode_info *ei = p;
90
Christoph Lametera35afb82007-05-16 22:10:57 -070091 inode_init_once(&ei->vfs_inode);
Arnd Bergmann67207b92005-11-15 15:53:48 -050092}
93
94static struct inode *
95spufs_new_inode(struct super_block *sb, int mode)
96{
97 struct inode *inode;
98
99 inode = new_inode(sb);
100 if (!inode)
101 goto out;
102
103 inode->i_mode = mode;
David Howells1330deb2008-11-14 10:38:39 +1100104 inode->i_uid = current_fsuid();
105 inode->i_gid = current_fsgid();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500106 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
107out:
108 return inode;
109}
110
111static int
112spufs_setattr(struct dentry *dentry, struct iattr *attr)
113{
114 struct inode *inode = dentry->d_inode;
115
Arnd Bergmann67207b92005-11-15 15:53:48 -0500116 if ((attr->ia_valid & ATTR_SIZE) &&
117 (attr->ia_size != inode->i_size))
118 return -EINVAL;
Christoph Hellwig10257742010-06-04 11:30:02 +0200119 setattr_copy(inode, attr);
120 mark_inode_dirty(inode);
121 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500122}
123
124
125static int
126spufs_new_file(struct super_block *sb, struct dentry *dentry,
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800127 const struct file_operations *fops, int mode,
Jeremy Kerr23d893f2008-06-30 12:17:28 +1000128 size_t size, struct spu_context *ctx)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500129{
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700130 static const struct inode_operations spufs_file_iops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500131 .setattr = spufs_setattr,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500132 };
133 struct inode *inode;
134 int ret;
135
136 ret = -ENOSPC;
137 inode = spufs_new_inode(sb, S_IFREG | mode);
138 if (!inode)
139 goto out;
140
141 ret = 0;
142 inode->i_op = &spufs_file_iops;
143 inode->i_fop = fops;
Jeremy Kerr23d893f2008-06-30 12:17:28 +1000144 inode->i_size = size;
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700145 inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500146 d_add(dentry, inode);
147out:
148 return ret;
149}
150
151static void
Al Viro0f3f63a2010-06-05 21:20:32 -0400152spufs_evict_inode(struct inode *inode)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500153{
Arnd Bergmann62632032006-10-04 17:26:15 +0200154 struct spufs_inode_info *ei = SPUFS_I(inode);
Al Viro0f3f63a2010-06-05 21:20:32 -0400155 end_writeback(inode);
Arnd Bergmann62632032006-10-04 17:26:15 +0200156 if (ei->i_ctx)
157 put_spu_context(ei->i_ctx);
158 if (ei->i_gang)
159 put_spu_gang(ei->i_gang);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500160}
161
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100162static void spufs_prune_dir(struct dentry *dir)
163{
164 struct dentry *dentry, *tmp;
Arnd Bergmann62632032006-10-04 17:26:15 +0200165
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800166 mutex_lock(&dir->d_inode->i_mutex);
Andrew Mortonc3a9aea2006-01-09 20:51:24 -0800167 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100168 spin_lock(&dentry->d_lock);
169 if (!(d_unhashed(dentry)) && dentry->d_inode) {
Nick Piggindc0474b2011-01-07 17:49:43 +1100170 dget_dlock(dentry);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100171 __d_drop(dentry);
172 spin_unlock(&dentry->d_lock);
173 simple_unlink(dir->d_inode, dentry);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100174 /* XXX: what was dcache_lock protecting here? Other
Nick Pigginda502952011-01-07 17:49:33 +1100175 * filesystems (IB, configfs) release dcache_lock
176 * before unlink */
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100177 dput(dentry);
178 } else {
179 spin_unlock(&dentry->d_lock);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100180 }
181 }
182 shrink_dcache_parent(dir);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800183 mutex_unlock(&dir->d_inode->i_mutex);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100184}
185
Arnd Bergmann62632032006-10-04 17:26:15 +0200186/* Caller must hold parent->i_mutex */
187static int spufs_rmdir(struct inode *parent, struct dentry *dir)
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100188{
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100189 /* remove all entries */
Arnd Bergmann62632032006-10-04 17:26:15 +0200190 spufs_prune_dir(dir);
Jeremy Kerrc443aca2007-11-16 13:32:23 +1100191 d_drop(dir);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100192
Arnd Bergmann62632032006-10-04 17:26:15 +0200193 return simple_rmdir(parent, dir);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100194}
195
Jeremy Kerr74254642009-02-17 11:44:14 +1100196static int spufs_fill_dir(struct dentry *dir,
197 const struct spufs_tree_descr *files, int mode,
198 struct spu_context *ctx)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500199{
Sebastian Siewior87873c82007-06-06 14:03:58 +1000200 struct dentry *dentry, *tmp;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500201 int ret;
202
203 while (files->name && files->name[0]) {
204 ret = -ENOMEM;
205 dentry = d_alloc_name(dir, files->name);
206 if (!dentry)
207 goto out;
208 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
Jeremy Kerr23d893f2008-06-30 12:17:28 +1000209 files->mode & mode, files->size, ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500210 if (ret)
211 goto out;
212 files++;
213 }
214 return 0;
215out:
Sebastian Siewior87873c82007-06-06 14:03:58 +1000216 /*
217 * remove all children from dir. dir->inode is not set so don't
218 * just simply use spufs_prune_dir() and panic afterwards :)
219 * dput() looks like it will do the right thing:
220 * - dec parent's ref counter
221 * - remove child from parent's child list
222 * - free child's inode if possible
223 * - free child
224 */
225 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
226 dput(dentry);
227 }
228
229 shrink_dcache_parent(dir);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500230 return ret;
231}
232
Arnd Bergmann67207b92005-11-15 15:53:48 -0500233static int spufs_dir_close(struct inode *inode, struct file *file)
234{
Michael Ellerman0309f022006-06-19 20:33:22 +0200235 struct spu_context *ctx;
Arnd Bergmann62632032006-10-04 17:26:15 +0200236 struct inode *parent;
237 struct dentry *dir;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500238 int ret;
239
Josef Sipekb4d1ab52006-12-08 02:37:30 -0800240 dir = file->f_path.dentry;
Arnd Bergmann62632032006-10-04 17:26:15 +0200241 parent = dir->d_parent->d_inode;
242 ctx = SPUFS_I(dir->d_inode)->i_ctx;
Arnd Bergmannc8ca0632006-01-04 20:31:22 +0100243
Christoph Hellwig02539d72008-05-08 15:29:12 +1000244 mutex_lock_nested(&parent->i_mutex, I_MUTEX_PARENT);
Arnd Bergmann62632032006-10-04 17:26:15 +0200245 ret = spufs_rmdir(parent, dir);
246 mutex_unlock(&parent->i_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500247 WARN_ON(ret);
Arnd Bergmannc8ca0632006-01-04 20:31:22 +0100248
Michael Ellerman0309f022006-06-19 20:33:22 +0200249 /* We have to give up the mm_struct */
250 spu_forget(ctx);
251
Arnd Bergmann67207b92005-11-15 15:53:48 -0500252 return dcache_dir_close(inode, file);
253}
254
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800255const struct file_operations spufs_context_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500256 .open = dcache_dir_open,
257 .release = spufs_dir_close,
258 .llseek = dcache_dir_lseek,
259 .read = generic_read_dir,
260 .readdir = dcache_readdir,
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200261 .fsync = noop_fsync,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500262};
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100263EXPORT_SYMBOL_GPL(spufs_context_fops);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500264
265static int
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200266spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
267 int mode)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500268{
269 int ret;
270 struct inode *inode;
271 struct spu_context *ctx;
272
273 ret = -ENOSPC;
274 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
275 if (!inode)
276 goto out;
277
278 if (dir->i_mode & S_ISGID) {
279 inode->i_gid = dir->i_gid;
280 inode->i_mode &= S_ISGID;
281 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200282 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500283 SPUFS_I(inode)->i_ctx = ctx;
284 if (!ctx)
285 goto out_iput;
286
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200287 ctx->flags = flags;
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000288 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500289 inode->i_fop = &simple_dir_operations;
Mark Nutter5737edd2006-10-24 18:31:16 +0200290 if (flags & SPU_CREATE_NOSCHED)
291 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
292 mode, ctx);
293 else
294 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
295
Arnd Bergmann67207b92005-11-15 15:53:48 -0500296 if (ret)
297 goto out_free_ctx;
298
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000299 if (spufs_get_sb_info(dir->i_sb)->debug)
300 ret = spufs_fill_dir(dentry, spufs_dir_debug_contents,
301 mode, ctx);
302
303 if (ret)
304 goto out_free_ctx;
305
Arnd Bergmann67207b92005-11-15 15:53:48 -0500306 d_instantiate(dentry, inode);
307 dget(dentry);
Jeremy Kerrba0b9962008-10-09 10:39:21 +1100308 inc_nlink(dir);
309 inc_nlink(dentry->d_inode);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500310 goto out;
311
312out_free_ctx:
Sebastian Siewior89df0082007-06-04 23:26:51 +1000313 spu_forget(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500314 put_spu_context(ctx);
315out_iput:
316 iput(inode);
317out:
318 return ret;
319}
320
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100321static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
322{
323 int ret;
324 struct file *filp;
325
326 ret = get_unused_fd();
327 if (ret < 0) {
328 dput(dentry);
329 mntput(mnt);
330 goto out;
331 }
332
David Howells745ca242008-11-14 10:39:22 +1100333 filp = dentry_open(dentry, mnt, O_RDONLY, current_cred());
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100334 if (IS_ERR(filp)) {
335 put_unused_fd(ret);
336 ret = PTR_ERR(filp);
337 goto out;
338 }
339
340 filp->f_op = &spufs_context_fops;
341 fd_install(ret, filp);
342out:
343 return ret;
344}
345
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200346static struct spu_context *
347spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
348 struct file *filp)
349{
Andre Detsch58119062008-01-11 15:03:26 +1100350 struct spu_context *tmp, *neighbor, *err;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200351 int count, node;
352 int aff_supp;
353
354 aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
355 struct spu, cbe_list))->aff_list);
356
357 if (!aff_supp)
358 return ERR_PTR(-EINVAL);
359
360 if (flags & SPU_CREATE_GANG)
361 return ERR_PTR(-EINVAL);
362
363 if (flags & SPU_CREATE_AFFINITY_MEM &&
364 gang->aff_ref_ctx &&
365 gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
366 return ERR_PTR(-EEXIST);
367
368 if (gang->aff_flags & AFF_MERGED)
369 return ERR_PTR(-EBUSY);
370
371 neighbor = NULL;
372 if (flags & SPU_CREATE_AFFINITY_SPU) {
373 if (!filp || filp->f_op != &spufs_context_fops)
374 return ERR_PTR(-EINVAL);
375
376 neighbor = get_spu_context(
377 SPUFS_I(filp->f_dentry->d_inode)->i_ctx);
378
379 if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
380 !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
381 !list_entry(neighbor->aff_list.next, struct spu_context,
Andre Detsch58119062008-01-11 15:03:26 +1100382 aff_list)->aff_head) {
383 err = ERR_PTR(-EEXIST);
384 goto out_put_neighbor;
385 }
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200386
Andre Detsch58119062008-01-11 15:03:26 +1100387 if (gang != neighbor->gang) {
388 err = ERR_PTR(-EINVAL);
389 goto out_put_neighbor;
390 }
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200391
392 count = 1;
393 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
394 count++;
395 if (list_empty(&neighbor->aff_list))
396 count++;
397
398 for (node = 0; node < MAX_NUMNODES; node++) {
399 if ((cbe_spu_info[node].n_spus - atomic_read(
400 &cbe_spu_info[node].reserved_spus)) >= count)
401 break;
402 }
403
Andre Detsch58119062008-01-11 15:03:26 +1100404 if (node == MAX_NUMNODES) {
405 err = ERR_PTR(-EEXIST);
406 goto out_put_neighbor;
407 }
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200408 }
409
410 return neighbor;
Andre Detsch58119062008-01-11 15:03:26 +1100411
412out_put_neighbor:
413 put_spu_context(neighbor);
414 return err;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200415}
416
417static void
418spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
419 struct spu_context *neighbor)
420{
421 if (flags & SPU_CREATE_AFFINITY_MEM)
422 ctx->gang->aff_ref_ctx = ctx;
423
424 if (flags & SPU_CREATE_AFFINITY_SPU) {
425 if (list_empty(&neighbor->aff_list)) {
426 list_add_tail(&neighbor->aff_list,
427 &ctx->gang->aff_list_head);
428 neighbor->aff_head = 1;
429 }
430
431 if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
432 || list_entry(neighbor->aff_list.next, struct spu_context,
433 aff_list)->aff_head) {
434 list_add(&ctx->aff_list, &neighbor->aff_list);
435 } else {
436 list_add_tail(&ctx->aff_list, &neighbor->aff_list);
437 if (neighbor->aff_head) {
438 neighbor->aff_head = 0;
439 ctx->aff_head = 1;
440 }
441 }
442
443 if (!ctx->gang->aff_ref_ctx)
444 ctx->gang->aff_ref_ctx = ctx;
445 }
446}
447
448static int
449spufs_create_context(struct inode *inode, struct dentry *dentry,
450 struct vfsmount *mnt, int flags, int mode,
451 struct file *aff_filp)
Arnd Bergmann62632032006-10-04 17:26:15 +0200452{
453 int ret;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200454 int affinity;
455 struct spu_gang *gang;
456 struct spu_context *neighbor;
Arnd Bergmann62632032006-10-04 17:26:15 +0200457
Mark Nutter5737edd2006-10-24 18:31:16 +0200458 ret = -EPERM;
459 if ((flags & SPU_CREATE_NOSCHED) &&
460 !capable(CAP_SYS_NICE))
461 goto out_unlock;
462
463 ret = -EINVAL;
464 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
465 == SPU_CREATE_ISOLATE)
466 goto out_unlock;
467
Jeremy Kerrbd2e5f82006-11-27 19:18:52 +0100468 ret = -ENODEV;
469 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
470 goto out_unlock;
471
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200472 gang = NULL;
473 neighbor = NULL;
474 affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
475 if (affinity) {
476 gang = SPUFS_I(inode)->i_gang;
477 ret = -EINVAL;
478 if (!gang)
479 goto out_unlock;
480 mutex_lock(&gang->aff_mutex);
481 neighbor = spufs_assert_affinity(flags, gang, aff_filp);
482 if (IS_ERR(neighbor)) {
483 ret = PTR_ERR(neighbor);
484 goto out_aff_unlock;
485 }
486 }
487
Arnd Bergmann62632032006-10-04 17:26:15 +0200488 ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
489 if (ret)
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200490 goto out_aff_unlock;
491
Andre Detsch58119062008-01-11 15:03:26 +1100492 if (affinity) {
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200493 spufs_set_affinity(flags, SPUFS_I(dentry->d_inode)->i_ctx,
494 neighbor);
Andre Detsch58119062008-01-11 15:03:26 +1100495 if (neighbor)
496 put_spu_context(neighbor);
497 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200498
499 /*
500 * get references for dget and mntget, will be released
501 * in error path of *_open().
502 */
503 ret = spufs_context_open(dget(dentry), mntget(mnt));
504 if (ret < 0) {
505 WARN_ON(spufs_rmdir(inode, dentry));
Kou Ishizaki6747c2e2008-10-09 10:45:49 +1100506 if (affinity)
507 mutex_unlock(&gang->aff_mutex);
Arnd Bergmann62632032006-10-04 17:26:15 +0200508 mutex_unlock(&inode->i_mutex);
509 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
510 goto out;
511 }
512
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200513out_aff_unlock:
514 if (affinity)
515 mutex_unlock(&gang->aff_mutex);
Arnd Bergmann62632032006-10-04 17:26:15 +0200516out_unlock:
517 mutex_unlock(&inode->i_mutex);
518out:
519 dput(dentry);
520 return ret;
521}
522
Arnd Bergmann62632032006-10-04 17:26:15 +0200523static int
524spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
525{
526 int ret;
527 struct inode *inode;
528 struct spu_gang *gang;
529
530 ret = -ENOSPC;
531 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
532 if (!inode)
533 goto out;
534
535 ret = 0;
536 if (dir->i_mode & S_ISGID) {
537 inode->i_gid = dir->i_gid;
538 inode->i_mode &= S_ISGID;
539 }
540 gang = alloc_spu_gang();
541 SPUFS_I(inode)->i_ctx = NULL;
542 SPUFS_I(inode)->i_gang = gang;
543 if (!gang)
544 goto out_iput;
545
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000546 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann62632032006-10-04 17:26:15 +0200547 inode->i_fop = &simple_dir_operations;
548
549 d_instantiate(dentry, inode);
Jeremy Kerrba0b9962008-10-09 10:39:21 +1100550 inc_nlink(dir);
551 inc_nlink(dentry->d_inode);
Arnd Bergmann62632032006-10-04 17:26:15 +0200552 return ret;
553
554out_iput:
555 iput(inode);
556out:
557 return ret;
558}
559
560static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
561{
562 int ret;
563 struct file *filp;
564
565 ret = get_unused_fd();
566 if (ret < 0) {
567 dput(dentry);
568 mntput(mnt);
569 goto out;
570 }
571
David Howells745ca242008-11-14 10:39:22 +1100572 filp = dentry_open(dentry, mnt, O_RDONLY, current_cred());
Arnd Bergmann62632032006-10-04 17:26:15 +0200573 if (IS_ERR(filp)) {
574 put_unused_fd(ret);
575 ret = PTR_ERR(filp);
576 goto out;
577 }
578
Jeremy Kerr877907d2007-06-04 23:26:51 +1000579 filp->f_op = &simple_dir_operations;
Arnd Bergmann62632032006-10-04 17:26:15 +0200580 fd_install(ret, filp);
581out:
582 return ret;
583}
584
585static int spufs_create_gang(struct inode *inode,
586 struct dentry *dentry,
587 struct vfsmount *mnt, int mode)
588{
589 int ret;
590
591 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
592 if (ret)
593 goto out;
594
595 /*
596 * get references for dget and mntget, will be released
597 * in error path of *_open().
598 */
599 ret = spufs_gang_open(dget(dentry), mntget(mnt));
Jeremy Kerr877907d2007-06-04 23:26:51 +1000600 if (ret < 0) {
601 int err = simple_rmdir(inode, dentry);
602 WARN_ON(err);
603 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200604
605out:
606 mutex_unlock(&inode->i_mutex);
607 dput(dentry);
608 return ret;
609}
610
611
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100612static struct file_system_type spufs_type;
613
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200614long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode,
615 struct file *filp)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500616{
617 struct dentry *dentry;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500618 int ret;
619
Arnd Bergmann67207b92005-11-15 15:53:48 -0500620 ret = -EINVAL;
Arnd Bergmann62632032006-10-04 17:26:15 +0200621 /* check if we are on spufs */
Jan Blunck4ac91372008-02-14 19:34:32 -0800622 if (nd->path.dentry->d_sb->s_type != &spufs_type)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500623 goto out;
624
Arnd Bergmann62632032006-10-04 17:26:15 +0200625 /* don't accept undefined flags */
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200626 if (flags & (~SPU_CREATE_FLAG_ALL))
arnd@arndb.dec9832942006-06-19 20:33:34 +0200627 goto out;
628
Arnd Bergmann62632032006-10-04 17:26:15 +0200629 /* only threads can be underneath a gang */
Jan Blunck4ac91372008-02-14 19:34:32 -0800630 if (nd->path.dentry != nd->path.dentry->d_sb->s_root) {
Arnd Bergmann62632032006-10-04 17:26:15 +0200631 if ((flags & SPU_CREATE_GANG) ||
Jan Blunck4ac91372008-02-14 19:34:32 -0800632 !SPUFS_I(nd->path.dentry->d_inode)->i_gang)
Arnd Bergmann62632032006-10-04 17:26:15 +0200633 goto out;
634 }
635
Arnd Bergmann67207b92005-11-15 15:53:48 -0500636 dentry = lookup_create(nd, 1);
637 ret = PTR_ERR(dentry);
638 if (IS_ERR(dentry))
639 goto out_dir;
640
Al Viroce3b0f82009-03-29 19:08:22 -0400641 mode &= ~current_umask();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500642
Arnd Bergmann62632032006-10-04 17:26:15 +0200643 if (flags & SPU_CREATE_GANG)
Christoph Hellwig826be062008-05-06 09:24:24 +1000644 ret = spufs_create_gang(nd->path.dentry->d_inode,
Jan Blunck4ac91372008-02-14 19:34:32 -0800645 dentry, nd->path.mnt, mode);
Arnd Bergmann62632032006-10-04 17:26:15 +0200646 else
Christoph Hellwig826be062008-05-06 09:24:24 +1000647 ret = spufs_create_context(nd->path.dentry->d_inode,
Jan Blunck4ac91372008-02-14 19:34:32 -0800648 dentry, nd->path.mnt, flags, mode,
649 filp);
Christoph Hellwig826be062008-05-06 09:24:24 +1000650 if (ret >= 0)
651 fsnotify_mkdir(nd->path.dentry->d_inode, dentry);
652 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500653
Arnd Bergmann67207b92005-11-15 15:53:48 -0500654out_dir:
Jan Blunck4ac91372008-02-14 19:34:32 -0800655 mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500656out:
657 return ret;
658}
659
660/* File system initialization */
661enum {
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000662 Opt_uid, Opt_gid, Opt_mode, Opt_debug, Opt_err,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500663};
664
Steven Whitehousea447c092008-10-13 10:46:57 +0100665static const match_table_t spufs_tokens = {
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000666 { Opt_uid, "uid=%d" },
667 { Opt_gid, "gid=%d" },
668 { Opt_mode, "mode=%o" },
669 { Opt_debug, "debug" },
670 { Opt_err, NULL },
Arnd Bergmann67207b92005-11-15 15:53:48 -0500671};
672
673static int
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000674spufs_parse_options(struct super_block *sb, char *options, struct inode *root)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500675{
676 char *p;
677 substring_t args[MAX_OPT_ARGS];
678
679 while ((p = strsep(&options, ",")) != NULL) {
680 int token, option;
681
682 if (!*p)
683 continue;
684
685 token = match_token(p, spufs_tokens, args);
686 switch (token) {
687 case Opt_uid:
688 if (match_int(&args[0], &option))
689 return 0;
690 root->i_uid = option;
691 break;
692 case Opt_gid:
693 if (match_int(&args[0], &option))
694 return 0;
695 root->i_gid = option;
696 break;
Jeremy Kerrf11f5ee2007-04-23 21:08:23 +0200697 case Opt_mode:
698 if (match_octal(&args[0], &option))
699 return 0;
700 root->i_mode = option | S_IFDIR;
701 break;
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000702 case Opt_debug:
703 spufs_get_sb_info(sb)->debug = 1;
704 break;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500705 default:
706 return 0;
707 }
708 }
709 return 1;
710}
711
Akinobu Mitadb1384b402007-04-23 21:08:20 +0200712static void spufs_exit_isolated_loader(void)
713{
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000714 free_pages((unsigned long) isolated_loader,
715 get_order(isolated_loader_size));
Akinobu Mitadb1384b402007-04-23 21:08:20 +0200716}
717
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200718static void
719spufs_init_isolated_loader(void)
720{
721 struct device_node *dn;
722 const char *loader;
723 int size;
724
725 dn = of_find_node_by_path("/spu-isolation");
726 if (!dn)
727 return;
728
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000729 loader = of_get_property(dn, "loader", &size);
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200730 if (!loader)
731 return;
732
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000733 /* the loader must be align on a 16 byte boundary */
734 isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200735 if (!isolated_loader)
736 return;
737
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000738 isolated_loader_size = size;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200739 memcpy(isolated_loader, loader, size);
740 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
741}
742
Arnd Bergmann67207b92005-11-15 15:53:48 -0500743static int
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500744spufs_create_root(struct super_block *sb, void *data)
745{
Arnd Bergmann67207b92005-11-15 15:53:48 -0500746 struct inode *inode;
747 int ret;
748
Arnd Bergmann8f18a152007-06-04 23:26:51 +1000749 ret = -ENODEV;
750 if (!spu_management_ops)
751 goto out;
752
Arnd Bergmann67207b92005-11-15 15:53:48 -0500753 ret = -ENOMEM;
754 inode = spufs_new_inode(sb, S_IFDIR | 0775);
755 if (!inode)
756 goto out;
757
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000758 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500759 inode->i_fop = &simple_dir_operations;
760 SPUFS_I(inode)->i_ctx = NULL;
Jeremy Kerre2ed6e42008-10-07 18:26:55 +1100761 inc_nlink(inode);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500762
763 ret = -EINVAL;
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000764 if (!spufs_parse_options(sb, data, inode))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500765 goto out_iput;
766
767 ret = -ENOMEM;
768 sb->s_root = d_alloc_root(inode);
769 if (!sb->s_root)
770 goto out_iput;
771
772 return 0;
773out_iput:
774 iput(inode);
775out:
776 return ret;
777}
778
779static int
780spufs_fill_super(struct super_block *sb, void *data, int silent)
781{
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000782 struct spufs_sb_info *info;
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700783 static const struct super_operations s_ops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500784 .alloc_inode = spufs_alloc_inode,
785 .destroy_inode = spufs_destroy_inode,
786 .statfs = simple_statfs,
Al Viro0f3f63a2010-06-05 21:20:32 -0400787 .evict_inode = spufs_evict_inode,
Miklos Szeredi90d09e12008-02-08 04:21:47 -0800788 .show_options = generic_show_options,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500789 };
790
Miklos Szeredi90d09e12008-02-08 04:21:47 -0800791 save_mount_options(sb, data);
792
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000793 info = kzalloc(sizeof(*info), GFP_KERNEL);
794 if (!info)
795 return -ENOMEM;
796
Arnd Bergmann67207b92005-11-15 15:53:48 -0500797 sb->s_maxbytes = MAX_LFS_FILESIZE;
798 sb->s_blocksize = PAGE_CACHE_SIZE;
799 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
800 sb->s_magic = SPUFS_MAGIC;
801 sb->s_op = &s_ops;
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000802 sb->s_fs_info = info;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500803
804 return spufs_create_root(sb, data);
805}
806
Al Virofc14f2f2010-07-25 01:48:30 +0400807static struct dentry *
808spufs_mount(struct file_system_type *fstype, int flags,
809 const char *name, void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500810{
Al Virofc14f2f2010-07-25 01:48:30 +0400811 return mount_single(fstype, flags, data, spufs_fill_super);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500812}
813
814static struct file_system_type spufs_type = {
815 .owner = THIS_MODULE,
816 .name = "spufs",
Al Virofc14f2f2010-07-25 01:48:30 +0400817 .mount = spufs_mount,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500818 .kill_sb = kill_litter_super,
819};
820
Arnd Bergmanne78b47a2006-03-27 21:27:40 +0200821static int __init spufs_init(void)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500822{
823 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100824
Jeremy Kerrccf17e92007-04-23 21:08:29 +0200825 ret = -ENODEV;
826 if (!spu_management_ops)
827 goto out;
828
Arnd Bergmann67207b92005-11-15 15:53:48 -0500829 ret = -ENOMEM;
830 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
831 sizeof(struct spufs_inode_info), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900832 SLAB_HWCACHE_ALIGN, spufs_init_once);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500833
834 if (!spufs_inode_cache)
835 goto out;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200836 ret = spu_sched_init();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500837 if (ret)
838 goto out_cache;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200839 ret = register_filesystem(&spufs_type);
840 if (ret)
841 goto out_sched;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500842 ret = register_spu_syscalls(&spufs_calls);
843 if (ret)
844 goto out_fs;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200845
846 spufs_init_isolated_loader();
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100847
Arnd Bergmann67207b92005-11-15 15:53:48 -0500848 return 0;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200849
Arnd Bergmann67207b92005-11-15 15:53:48 -0500850out_fs:
851 unregister_filesystem(&spufs_type);
Akinobu Mitac99c1992007-04-23 21:08:19 +0200852out_sched:
853 spu_sched_exit();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500854out_cache:
855 kmem_cache_destroy(spufs_inode_cache);
856out:
857 return ret;
858}
859module_init(spufs_init);
860
Arnd Bergmanne78b47a2006-03-27 21:27:40 +0200861static void __exit spufs_exit(void)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500862{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500863 spu_sched_exit();
Akinobu Mitadb1384b402007-04-23 21:08:20 +0200864 spufs_exit_isolated_loader();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500865 unregister_spu_syscalls(&spufs_calls);
866 unregister_filesystem(&spufs_type);
867 kmem_cache_destroy(spufs_inode_cache);
868}
869module_exit(spufs_exit);
870
871MODULE_LICENSE("GPL");
872MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
873