blob: 5dec408d670353a9f4651a1b72a244e2a695d381 [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) {
165 dget_locked(dentry);
166 __d_drop(dentry);
167 spin_unlock(&dentry->d_lock);
168 simple_unlink(dir->d_inode, dentry);
169 spin_unlock(&dcache_lock);
170 dput(dentry);
171 } else {
172 spin_unlock(&dentry->d_lock);
173 spin_unlock(&dcache_lock);
174 }
175 }
176 shrink_dcache_parent(dir);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800177 mutex_unlock(&dir->d_inode->i_mutex);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100178}
179
Arnd Bergmann62632032006-10-04 17:26:15 +0200180/* Caller must hold parent->i_mutex */
181static int spufs_rmdir(struct inode *parent, struct dentry *dir)
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100182{
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100183 /* remove all entries */
Arnd Bergmann62632032006-10-04 17:26:15 +0200184 spufs_prune_dir(dir);
Jeremy Kerrc443aca2007-11-16 13:32:23 +1100185 d_drop(dir);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100186
Arnd Bergmann62632032006-10-04 17:26:15 +0200187 return simple_rmdir(parent, dir);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100188}
189
Jeremy Kerr74254642009-02-17 11:44:14 +1100190static int spufs_fill_dir(struct dentry *dir,
191 const struct spufs_tree_descr *files, int mode,
192 struct spu_context *ctx)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500193{
Sebastian Siewior87873c82007-06-06 14:03:58 +1000194 struct dentry *dentry, *tmp;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500195 int ret;
196
197 while (files->name && files->name[0]) {
198 ret = -ENOMEM;
199 dentry = d_alloc_name(dir, files->name);
200 if (!dentry)
201 goto out;
202 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
Jeremy Kerr23d893f2008-06-30 12:17:28 +1000203 files->mode & mode, files->size, ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500204 if (ret)
205 goto out;
206 files++;
207 }
208 return 0;
209out:
Sebastian Siewior87873c82007-06-06 14:03:58 +1000210 /*
211 * remove all children from dir. dir->inode is not set so don't
212 * just simply use spufs_prune_dir() and panic afterwards :)
213 * dput() looks like it will do the right thing:
214 * - dec parent's ref counter
215 * - remove child from parent's child list
216 * - free child's inode if possible
217 * - free child
218 */
219 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
220 dput(dentry);
221 }
222
223 shrink_dcache_parent(dir);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500224 return ret;
225}
226
Arnd Bergmann67207b92005-11-15 15:53:48 -0500227static int spufs_dir_close(struct inode *inode, struct file *file)
228{
Michael Ellerman0309f022006-06-19 20:33:22 +0200229 struct spu_context *ctx;
Arnd Bergmann62632032006-10-04 17:26:15 +0200230 struct inode *parent;
231 struct dentry *dir;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500232 int ret;
233
Josef Sipekb4d1ab52006-12-08 02:37:30 -0800234 dir = file->f_path.dentry;
Arnd Bergmann62632032006-10-04 17:26:15 +0200235 parent = dir->d_parent->d_inode;
236 ctx = SPUFS_I(dir->d_inode)->i_ctx;
Arnd Bergmannc8ca0632006-01-04 20:31:22 +0100237
Christoph Hellwig02539d72008-05-08 15:29:12 +1000238 mutex_lock_nested(&parent->i_mutex, I_MUTEX_PARENT);
Arnd Bergmann62632032006-10-04 17:26:15 +0200239 ret = spufs_rmdir(parent, dir);
240 mutex_unlock(&parent->i_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500241 WARN_ON(ret);
Arnd Bergmannc8ca0632006-01-04 20:31:22 +0100242
Michael Ellerman0309f022006-06-19 20:33:22 +0200243 /* We have to give up the mm_struct */
244 spu_forget(ctx);
245
Arnd Bergmann67207b92005-11-15 15:53:48 -0500246 return dcache_dir_close(inode, file);
247}
248
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800249const struct file_operations spufs_context_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500250 .open = dcache_dir_open,
251 .release = spufs_dir_close,
252 .llseek = dcache_dir_lseek,
253 .read = generic_read_dir,
254 .readdir = dcache_readdir,
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200255 .fsync = noop_fsync,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500256};
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100257EXPORT_SYMBOL_GPL(spufs_context_fops);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500258
259static int
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200260spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
261 int mode)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500262{
263 int ret;
264 struct inode *inode;
265 struct spu_context *ctx;
266
267 ret = -ENOSPC;
268 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
269 if (!inode)
270 goto out;
271
272 if (dir->i_mode & S_ISGID) {
273 inode->i_gid = dir->i_gid;
274 inode->i_mode &= S_ISGID;
275 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200276 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500277 SPUFS_I(inode)->i_ctx = ctx;
278 if (!ctx)
279 goto out_iput;
280
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200281 ctx->flags = flags;
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000282 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500283 inode->i_fop = &simple_dir_operations;
Mark Nutter5737edd2006-10-24 18:31:16 +0200284 if (flags & SPU_CREATE_NOSCHED)
285 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
286 mode, ctx);
287 else
288 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
289
Arnd Bergmann67207b92005-11-15 15:53:48 -0500290 if (ret)
291 goto out_free_ctx;
292
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000293 if (spufs_get_sb_info(dir->i_sb)->debug)
294 ret = spufs_fill_dir(dentry, spufs_dir_debug_contents,
295 mode, ctx);
296
297 if (ret)
298 goto out_free_ctx;
299
Arnd Bergmann67207b92005-11-15 15:53:48 -0500300 d_instantiate(dentry, inode);
301 dget(dentry);
Jeremy Kerrba0b9962008-10-09 10:39:21 +1100302 inc_nlink(dir);
303 inc_nlink(dentry->d_inode);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500304 goto out;
305
306out_free_ctx:
Sebastian Siewior89df0082007-06-04 23:26:51 +1000307 spu_forget(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500308 put_spu_context(ctx);
309out_iput:
310 iput(inode);
311out:
312 return ret;
313}
314
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100315static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
316{
317 int ret;
318 struct file *filp;
319
320 ret = get_unused_fd();
321 if (ret < 0) {
322 dput(dentry);
323 mntput(mnt);
324 goto out;
325 }
326
David Howells745ca242008-11-14 10:39:22 +1100327 filp = dentry_open(dentry, mnt, O_RDONLY, current_cred());
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100328 if (IS_ERR(filp)) {
329 put_unused_fd(ret);
330 ret = PTR_ERR(filp);
331 goto out;
332 }
333
334 filp->f_op = &spufs_context_fops;
335 fd_install(ret, filp);
336out:
337 return ret;
338}
339
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200340static struct spu_context *
341spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
342 struct file *filp)
343{
Andre Detsch58119062008-01-11 15:03:26 +1100344 struct spu_context *tmp, *neighbor, *err;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200345 int count, node;
346 int aff_supp;
347
348 aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
349 struct spu, cbe_list))->aff_list);
350
351 if (!aff_supp)
352 return ERR_PTR(-EINVAL);
353
354 if (flags & SPU_CREATE_GANG)
355 return ERR_PTR(-EINVAL);
356
357 if (flags & SPU_CREATE_AFFINITY_MEM &&
358 gang->aff_ref_ctx &&
359 gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
360 return ERR_PTR(-EEXIST);
361
362 if (gang->aff_flags & AFF_MERGED)
363 return ERR_PTR(-EBUSY);
364
365 neighbor = NULL;
366 if (flags & SPU_CREATE_AFFINITY_SPU) {
367 if (!filp || filp->f_op != &spufs_context_fops)
368 return ERR_PTR(-EINVAL);
369
370 neighbor = get_spu_context(
371 SPUFS_I(filp->f_dentry->d_inode)->i_ctx);
372
373 if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
374 !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
375 !list_entry(neighbor->aff_list.next, struct spu_context,
Andre Detsch58119062008-01-11 15:03:26 +1100376 aff_list)->aff_head) {
377 err = ERR_PTR(-EEXIST);
378 goto out_put_neighbor;
379 }
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200380
Andre Detsch58119062008-01-11 15:03:26 +1100381 if (gang != neighbor->gang) {
382 err = ERR_PTR(-EINVAL);
383 goto out_put_neighbor;
384 }
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200385
386 count = 1;
387 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
388 count++;
389 if (list_empty(&neighbor->aff_list))
390 count++;
391
392 for (node = 0; node < MAX_NUMNODES; node++) {
393 if ((cbe_spu_info[node].n_spus - atomic_read(
394 &cbe_spu_info[node].reserved_spus)) >= count)
395 break;
396 }
397
Andre Detsch58119062008-01-11 15:03:26 +1100398 if (node == MAX_NUMNODES) {
399 err = ERR_PTR(-EEXIST);
400 goto out_put_neighbor;
401 }
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200402 }
403
404 return neighbor;
Andre Detsch58119062008-01-11 15:03:26 +1100405
406out_put_neighbor:
407 put_spu_context(neighbor);
408 return err;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200409}
410
411static void
412spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
413 struct spu_context *neighbor)
414{
415 if (flags & SPU_CREATE_AFFINITY_MEM)
416 ctx->gang->aff_ref_ctx = ctx;
417
418 if (flags & SPU_CREATE_AFFINITY_SPU) {
419 if (list_empty(&neighbor->aff_list)) {
420 list_add_tail(&neighbor->aff_list,
421 &ctx->gang->aff_list_head);
422 neighbor->aff_head = 1;
423 }
424
425 if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
426 || list_entry(neighbor->aff_list.next, struct spu_context,
427 aff_list)->aff_head) {
428 list_add(&ctx->aff_list, &neighbor->aff_list);
429 } else {
430 list_add_tail(&ctx->aff_list, &neighbor->aff_list);
431 if (neighbor->aff_head) {
432 neighbor->aff_head = 0;
433 ctx->aff_head = 1;
434 }
435 }
436
437 if (!ctx->gang->aff_ref_ctx)
438 ctx->gang->aff_ref_ctx = ctx;
439 }
440}
441
442static int
443spufs_create_context(struct inode *inode, struct dentry *dentry,
444 struct vfsmount *mnt, int flags, int mode,
445 struct file *aff_filp)
Arnd Bergmann62632032006-10-04 17:26:15 +0200446{
447 int ret;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200448 int affinity;
449 struct spu_gang *gang;
450 struct spu_context *neighbor;
Arnd Bergmann62632032006-10-04 17:26:15 +0200451
Mark Nutter5737edd2006-10-24 18:31:16 +0200452 ret = -EPERM;
453 if ((flags & SPU_CREATE_NOSCHED) &&
454 !capable(CAP_SYS_NICE))
455 goto out_unlock;
456
457 ret = -EINVAL;
458 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
459 == SPU_CREATE_ISOLATE)
460 goto out_unlock;
461
Jeremy Kerrbd2e5f82006-11-27 19:18:52 +0100462 ret = -ENODEV;
463 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
464 goto out_unlock;
465
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200466 gang = NULL;
467 neighbor = NULL;
468 affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
469 if (affinity) {
470 gang = SPUFS_I(inode)->i_gang;
471 ret = -EINVAL;
472 if (!gang)
473 goto out_unlock;
474 mutex_lock(&gang->aff_mutex);
475 neighbor = spufs_assert_affinity(flags, gang, aff_filp);
476 if (IS_ERR(neighbor)) {
477 ret = PTR_ERR(neighbor);
478 goto out_aff_unlock;
479 }
480 }
481
Arnd Bergmann62632032006-10-04 17:26:15 +0200482 ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
483 if (ret)
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200484 goto out_aff_unlock;
485
Andre Detsch58119062008-01-11 15:03:26 +1100486 if (affinity) {
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200487 spufs_set_affinity(flags, SPUFS_I(dentry->d_inode)->i_ctx,
488 neighbor);
Andre Detsch58119062008-01-11 15:03:26 +1100489 if (neighbor)
490 put_spu_context(neighbor);
491 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200492
493 /*
494 * get references for dget and mntget, will be released
495 * in error path of *_open().
496 */
497 ret = spufs_context_open(dget(dentry), mntget(mnt));
498 if (ret < 0) {
499 WARN_ON(spufs_rmdir(inode, dentry));
Kou Ishizaki6747c2e2008-10-09 10:45:49 +1100500 if (affinity)
501 mutex_unlock(&gang->aff_mutex);
Arnd Bergmann62632032006-10-04 17:26:15 +0200502 mutex_unlock(&inode->i_mutex);
503 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
504 goto out;
505 }
506
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200507out_aff_unlock:
508 if (affinity)
509 mutex_unlock(&gang->aff_mutex);
Arnd Bergmann62632032006-10-04 17:26:15 +0200510out_unlock:
511 mutex_unlock(&inode->i_mutex);
512out:
513 dput(dentry);
514 return ret;
515}
516
Arnd Bergmann62632032006-10-04 17:26:15 +0200517static int
518spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
519{
520 int ret;
521 struct inode *inode;
522 struct spu_gang *gang;
523
524 ret = -ENOSPC;
525 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
526 if (!inode)
527 goto out;
528
529 ret = 0;
530 if (dir->i_mode & S_ISGID) {
531 inode->i_gid = dir->i_gid;
532 inode->i_mode &= S_ISGID;
533 }
534 gang = alloc_spu_gang();
535 SPUFS_I(inode)->i_ctx = NULL;
536 SPUFS_I(inode)->i_gang = gang;
537 if (!gang)
538 goto out_iput;
539
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000540 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann62632032006-10-04 17:26:15 +0200541 inode->i_fop = &simple_dir_operations;
542
543 d_instantiate(dentry, inode);
Jeremy Kerrba0b9962008-10-09 10:39:21 +1100544 inc_nlink(dir);
545 inc_nlink(dentry->d_inode);
Arnd Bergmann62632032006-10-04 17:26:15 +0200546 return ret;
547
548out_iput:
549 iput(inode);
550out:
551 return ret;
552}
553
554static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
555{
556 int ret;
557 struct file *filp;
558
559 ret = get_unused_fd();
560 if (ret < 0) {
561 dput(dentry);
562 mntput(mnt);
563 goto out;
564 }
565
David Howells745ca242008-11-14 10:39:22 +1100566 filp = dentry_open(dentry, mnt, O_RDONLY, current_cred());
Arnd Bergmann62632032006-10-04 17:26:15 +0200567 if (IS_ERR(filp)) {
568 put_unused_fd(ret);
569 ret = PTR_ERR(filp);
570 goto out;
571 }
572
Jeremy Kerr877907d2007-06-04 23:26:51 +1000573 filp->f_op = &simple_dir_operations;
Arnd Bergmann62632032006-10-04 17:26:15 +0200574 fd_install(ret, filp);
575out:
576 return ret;
577}
578
579static int spufs_create_gang(struct inode *inode,
580 struct dentry *dentry,
581 struct vfsmount *mnt, int mode)
582{
583 int ret;
584
585 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
586 if (ret)
587 goto out;
588
589 /*
590 * get references for dget and mntget, will be released
591 * in error path of *_open().
592 */
593 ret = spufs_gang_open(dget(dentry), mntget(mnt));
Jeremy Kerr877907d2007-06-04 23:26:51 +1000594 if (ret < 0) {
595 int err = simple_rmdir(inode, dentry);
596 WARN_ON(err);
597 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200598
599out:
600 mutex_unlock(&inode->i_mutex);
601 dput(dentry);
602 return ret;
603}
604
605
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100606static struct file_system_type spufs_type;
607
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200608long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode,
609 struct file *filp)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500610{
611 struct dentry *dentry;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500612 int ret;
613
Arnd Bergmann67207b92005-11-15 15:53:48 -0500614 ret = -EINVAL;
Arnd Bergmann62632032006-10-04 17:26:15 +0200615 /* check if we are on spufs */
Jan Blunck4ac91372008-02-14 19:34:32 -0800616 if (nd->path.dentry->d_sb->s_type != &spufs_type)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500617 goto out;
618
Arnd Bergmann62632032006-10-04 17:26:15 +0200619 /* don't accept undefined flags */
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200620 if (flags & (~SPU_CREATE_FLAG_ALL))
arnd@arndb.dec9832942006-06-19 20:33:34 +0200621 goto out;
622
Arnd Bergmann62632032006-10-04 17:26:15 +0200623 /* only threads can be underneath a gang */
Jan Blunck4ac91372008-02-14 19:34:32 -0800624 if (nd->path.dentry != nd->path.dentry->d_sb->s_root) {
Arnd Bergmann62632032006-10-04 17:26:15 +0200625 if ((flags & SPU_CREATE_GANG) ||
Jan Blunck4ac91372008-02-14 19:34:32 -0800626 !SPUFS_I(nd->path.dentry->d_inode)->i_gang)
Arnd Bergmann62632032006-10-04 17:26:15 +0200627 goto out;
628 }
629
Arnd Bergmann67207b92005-11-15 15:53:48 -0500630 dentry = lookup_create(nd, 1);
631 ret = PTR_ERR(dentry);
632 if (IS_ERR(dentry))
633 goto out_dir;
634
Al Viroce3b0f82009-03-29 19:08:22 -0400635 mode &= ~current_umask();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500636
Arnd Bergmann62632032006-10-04 17:26:15 +0200637 if (flags & SPU_CREATE_GANG)
Christoph Hellwig826be062008-05-06 09:24:24 +1000638 ret = spufs_create_gang(nd->path.dentry->d_inode,
Jan Blunck4ac91372008-02-14 19:34:32 -0800639 dentry, nd->path.mnt, mode);
Arnd Bergmann62632032006-10-04 17:26:15 +0200640 else
Christoph Hellwig826be062008-05-06 09:24:24 +1000641 ret = spufs_create_context(nd->path.dentry->d_inode,
Jan Blunck4ac91372008-02-14 19:34:32 -0800642 dentry, nd->path.mnt, flags, mode,
643 filp);
Christoph Hellwig826be062008-05-06 09:24:24 +1000644 if (ret >= 0)
645 fsnotify_mkdir(nd->path.dentry->d_inode, dentry);
646 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500647
Arnd Bergmann67207b92005-11-15 15:53:48 -0500648out_dir:
Jan Blunck4ac91372008-02-14 19:34:32 -0800649 mutex_unlock(&nd->path.dentry->d_inode->i_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500650out:
651 return ret;
652}
653
654/* File system initialization */
655enum {
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000656 Opt_uid, Opt_gid, Opt_mode, Opt_debug, Opt_err,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500657};
658
Steven Whitehousea447c092008-10-13 10:46:57 +0100659static const match_table_t spufs_tokens = {
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000660 { Opt_uid, "uid=%d" },
661 { Opt_gid, "gid=%d" },
662 { Opt_mode, "mode=%o" },
663 { Opt_debug, "debug" },
664 { Opt_err, NULL },
Arnd Bergmann67207b92005-11-15 15:53:48 -0500665};
666
667static int
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000668spufs_parse_options(struct super_block *sb, char *options, struct inode *root)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500669{
670 char *p;
671 substring_t args[MAX_OPT_ARGS];
672
673 while ((p = strsep(&options, ",")) != NULL) {
674 int token, option;
675
676 if (!*p)
677 continue;
678
679 token = match_token(p, spufs_tokens, args);
680 switch (token) {
681 case Opt_uid:
682 if (match_int(&args[0], &option))
683 return 0;
684 root->i_uid = option;
685 break;
686 case Opt_gid:
687 if (match_int(&args[0], &option))
688 return 0;
689 root->i_gid = option;
690 break;
Jeremy Kerrf11f5ee2007-04-23 21:08:23 +0200691 case Opt_mode:
692 if (match_octal(&args[0], &option))
693 return 0;
694 root->i_mode = option | S_IFDIR;
695 break;
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000696 case Opt_debug:
697 spufs_get_sb_info(sb)->debug = 1;
698 break;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500699 default:
700 return 0;
701 }
702 }
703 return 1;
704}
705
Akinobu Mitadb1384b2007-04-23 21:08:20 +0200706static void spufs_exit_isolated_loader(void)
707{
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000708 free_pages((unsigned long) isolated_loader,
709 get_order(isolated_loader_size));
Akinobu Mitadb1384b2007-04-23 21:08:20 +0200710}
711
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200712static void
713spufs_init_isolated_loader(void)
714{
715 struct device_node *dn;
716 const char *loader;
717 int size;
718
719 dn = of_find_node_by_path("/spu-isolation");
720 if (!dn)
721 return;
722
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000723 loader = of_get_property(dn, "loader", &size);
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200724 if (!loader)
725 return;
726
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000727 /* the loader must be align on a 16 byte boundary */
728 isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200729 if (!isolated_loader)
730 return;
731
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000732 isolated_loader_size = size;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200733 memcpy(isolated_loader, loader, size);
734 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
735}
736
Arnd Bergmann67207b92005-11-15 15:53:48 -0500737static int
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500738spufs_create_root(struct super_block *sb, void *data)
739{
Arnd Bergmann67207b92005-11-15 15:53:48 -0500740 struct inode *inode;
741 int ret;
742
Arnd Bergmann8f18a152007-06-04 23:26:51 +1000743 ret = -ENODEV;
744 if (!spu_management_ops)
745 goto out;
746
Arnd Bergmann67207b92005-11-15 15:53:48 -0500747 ret = -ENOMEM;
748 inode = spufs_new_inode(sb, S_IFDIR | 0775);
749 if (!inode)
750 goto out;
751
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000752 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500753 inode->i_fop = &simple_dir_operations;
754 SPUFS_I(inode)->i_ctx = NULL;
Jeremy Kerre2ed6e42008-10-07 18:26:55 +1100755 inc_nlink(inode);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500756
757 ret = -EINVAL;
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000758 if (!spufs_parse_options(sb, data, inode))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500759 goto out_iput;
760
761 ret = -ENOMEM;
762 sb->s_root = d_alloc_root(inode);
763 if (!sb->s_root)
764 goto out_iput;
765
766 return 0;
767out_iput:
768 iput(inode);
769out:
770 return ret;
771}
772
773static int
774spufs_fill_super(struct super_block *sb, void *data, int silent)
775{
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000776 struct spufs_sb_info *info;
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700777 static const struct super_operations s_ops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500778 .alloc_inode = spufs_alloc_inode,
779 .destroy_inode = spufs_destroy_inode,
780 .statfs = simple_statfs,
Al Viro0f3f63a2010-06-05 21:20:32 -0400781 .evict_inode = spufs_evict_inode,
Miklos Szeredi90d09e12008-02-08 04:21:47 -0800782 .show_options = generic_show_options,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500783 };
784
Miklos Szeredi90d09e12008-02-08 04:21:47 -0800785 save_mount_options(sb, data);
786
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000787 info = kzalloc(sizeof(*info), GFP_KERNEL);
788 if (!info)
789 return -ENOMEM;
790
Arnd Bergmann67207b92005-11-15 15:53:48 -0500791 sb->s_maxbytes = MAX_LFS_FILESIZE;
792 sb->s_blocksize = PAGE_CACHE_SIZE;
793 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
794 sb->s_magic = SPUFS_MAGIC;
795 sb->s_op = &s_ops;
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000796 sb->s_fs_info = info;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500797
798 return spufs_create_root(sb, data);
799}
800
David Howells454e2392006-06-23 02:02:57 -0700801static int
Arnd Bergmann67207b92005-11-15 15:53:48 -0500802spufs_get_sb(struct file_system_type *fstype, int flags,
David Howells454e2392006-06-23 02:02:57 -0700803 const char *name, void *data, struct vfsmount *mnt)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500804{
David Howells454e2392006-06-23 02:02:57 -0700805 return get_sb_single(fstype, flags, data, spufs_fill_super, mnt);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500806}
807
808static struct file_system_type spufs_type = {
809 .owner = THIS_MODULE,
810 .name = "spufs",
811 .get_sb = spufs_get_sb,
812 .kill_sb = kill_litter_super,
813};
814
Arnd Bergmanne78b47a2006-03-27 21:27:40 +0200815static int __init spufs_init(void)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500816{
817 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100818
Jeremy Kerrccf17e92007-04-23 21:08:29 +0200819 ret = -ENODEV;
820 if (!spu_management_ops)
821 goto out;
822
Arnd Bergmann67207b92005-11-15 15:53:48 -0500823 ret = -ENOMEM;
824 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
825 sizeof(struct spufs_inode_info), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900826 SLAB_HWCACHE_ALIGN, spufs_init_once);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500827
828 if (!spufs_inode_cache)
829 goto out;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200830 ret = spu_sched_init();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500831 if (ret)
832 goto out_cache;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200833 ret = register_filesystem(&spufs_type);
834 if (ret)
835 goto out_sched;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500836 ret = register_spu_syscalls(&spufs_calls);
837 if (ret)
838 goto out_fs;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200839
840 spufs_init_isolated_loader();
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100841
Arnd Bergmann67207b92005-11-15 15:53:48 -0500842 return 0;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200843
Arnd Bergmann67207b92005-11-15 15:53:48 -0500844out_fs:
845 unregister_filesystem(&spufs_type);
Akinobu Mitac99c1992007-04-23 21:08:19 +0200846out_sched:
847 spu_sched_exit();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500848out_cache:
849 kmem_cache_destroy(spufs_inode_cache);
850out:
851 return ret;
852}
853module_init(spufs_init);
854
Arnd Bergmanne78b47a2006-03-27 21:27:40 +0200855static void __exit spufs_exit(void)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500856{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500857 spu_sched_exit();
Akinobu Mitadb1384b2007-04-23 21:08:20 +0200858 spufs_exit_isolated_loader();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500859 unregister_spu_syscalls(&spufs_calls);
860 unregister_filesystem(&spufs_type);
861 kmem_cache_destroy(spufs_inode_cache);
862}
863module_exit(spufs_exit);
864
865MODULE_LICENSE("GPL");
866MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
867