blob: d544d7816df3229780e4f2c58a1a28355c5c4aae [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);
Arnd Bergmann67207b92005-11-15 15:53:48 -050077 kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
78}
79
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +110080static void spufs_destroy_inode(struct inode *inode)
81{
82 call_rcu(&inode->i_rcu, spufs_i_callback);
83}
84
Arnd Bergmann67207b92005-11-15 15:53:48 -050085static void
Alexey Dobriyan51cc5062008-07-25 19:45:34 -070086spufs_init_once(void *p)
Arnd Bergmann67207b92005-11-15 15:53:48 -050087{
88 struct spufs_inode_info *ei = p;
89
Christoph Lametera35afb82007-05-16 22:10:57 -070090 inode_init_once(&ei->vfs_inode);
Arnd Bergmann67207b92005-11-15 15:53:48 -050091}
92
93static struct inode *
Al Viroc6684b22011-07-26 04:47:14 -040094spufs_new_inode(struct super_block *sb, umode_t mode)
Arnd Bergmann67207b92005-11-15 15:53:48 -050095{
96 struct inode *inode;
97
98 inode = new_inode(sb);
99 if (!inode)
100 goto out;
101
102 inode->i_mode = mode;
David Howells1330deb2008-11-14 10:38:39 +1100103 inode->i_uid = current_fsuid();
104 inode->i_gid = current_fsgid();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500105 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
106out:
107 return inode;
108}
109
110static int
111spufs_setattr(struct dentry *dentry, struct iattr *attr)
112{
113 struct inode *inode = dentry->d_inode;
114
Arnd Bergmann67207b92005-11-15 15:53:48 -0500115 if ((attr->ia_valid & ATTR_SIZE) &&
116 (attr->ia_size != inode->i_size))
117 return -EINVAL;
Christoph Hellwig10257742010-06-04 11:30:02 +0200118 setattr_copy(inode, attr);
119 mark_inode_dirty(inode);
120 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500121}
122
123
124static int
125spufs_new_file(struct super_block *sb, struct dentry *dentry,
Al Viroc6684b22011-07-26 04:47:14 -0400126 const struct file_operations *fops, umode_t mode,
Jeremy Kerr23d893f2008-06-30 12:17:28 +1000127 size_t size, struct spu_context *ctx)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500128{
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700129 static const struct inode_operations spufs_file_iops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500130 .setattr = spufs_setattr,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500131 };
132 struct inode *inode;
133 int ret;
134
135 ret = -ENOSPC;
136 inode = spufs_new_inode(sb, S_IFREG | mode);
137 if (!inode)
138 goto out;
139
140 ret = 0;
141 inode->i_op = &spufs_file_iops;
142 inode->i_fop = fops;
Jeremy Kerr23d893f2008-06-30 12:17:28 +1000143 inode->i_size = size;
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700144 inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500145 d_add(dentry, inode);
146out:
147 return ret;
148}
149
150static void
Al Viro0f3f63a2010-06-05 21:20:32 -0400151spufs_evict_inode(struct inode *inode)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500152{
Arnd Bergmann62632032006-10-04 17:26:15 +0200153 struct spufs_inode_info *ei = SPUFS_I(inode);
Jan Karadbd57682012-05-03 14:48:02 +0200154 clear_inode(inode);
Arnd Bergmann62632032006-10-04 17:26:15 +0200155 if (ei->i_ctx)
156 put_spu_context(ei->i_ctx);
157 if (ei->i_gang)
158 put_spu_gang(ei->i_gang);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500159}
160
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100161static void spufs_prune_dir(struct dentry *dir)
162{
163 struct dentry *dentry, *tmp;
Arnd Bergmann62632032006-10-04 17:26:15 +0200164
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800165 mutex_lock(&dir->d_inode->i_mutex);
Andrew Mortonc3a9aea2006-01-09 20:51:24 -0800166 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100167 spin_lock(&dentry->d_lock);
168 if (!(d_unhashed(dentry)) && dentry->d_inode) {
Nick Piggindc0474b2011-01-07 17:49:43 +1100169 dget_dlock(dentry);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100170 __d_drop(dentry);
171 spin_unlock(&dentry->d_lock);
172 simple_unlink(dir->d_inode, dentry);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100173 /* XXX: what was dcache_lock protecting here? Other
Nick Pigginda502952011-01-07 17:49:33 +1100174 * filesystems (IB, configfs) release dcache_lock
175 * before unlink */
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100176 dput(dentry);
177 } else {
178 spin_unlock(&dentry->d_lock);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100179 }
180 }
181 shrink_dcache_parent(dir);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800182 mutex_unlock(&dir->d_inode->i_mutex);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100183}
184
Arnd Bergmann62632032006-10-04 17:26:15 +0200185/* Caller must hold parent->i_mutex */
186static int spufs_rmdir(struct inode *parent, struct dentry *dir)
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100187{
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100188 /* remove all entries */
Arnd Bergmann62632032006-10-04 17:26:15 +0200189 spufs_prune_dir(dir);
Jeremy Kerrc443aca2007-11-16 13:32:23 +1100190 d_drop(dir);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100191
Arnd Bergmann62632032006-10-04 17:26:15 +0200192 return simple_rmdir(parent, dir);
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100193}
194
Jeremy Kerr74254642009-02-17 11:44:14 +1100195static int spufs_fill_dir(struct dentry *dir,
Al Viroc6684b22011-07-26 04:47:14 -0400196 const struct spufs_tree_descr *files, umode_t mode,
Jeremy Kerr74254642009-02-17 11:44:14 +1100197 struct spu_context *ctx)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500198{
Sebastian Siewior87873c82007-06-06 14:03:58 +1000199 struct dentry *dentry, *tmp;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500200 int ret;
201
202 while (files->name && files->name[0]) {
203 ret = -ENOMEM;
204 dentry = d_alloc_name(dir, files->name);
205 if (!dentry)
206 goto out;
207 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
Jeremy Kerr23d893f2008-06-30 12:17:28 +1000208 files->mode & mode, files->size, ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500209 if (ret)
210 goto out;
211 files++;
212 }
213 return 0;
214out:
Sebastian Siewior87873c82007-06-06 14:03:58 +1000215 /*
216 * remove all children from dir. dir->inode is not set so don't
217 * just simply use spufs_prune_dir() and panic afterwards :)
218 * dput() looks like it will do the right thing:
219 * - dec parent's ref counter
220 * - remove child from parent's child list
221 * - free child's inode if possible
222 * - free child
223 */
224 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
225 dput(dentry);
226 }
227
228 shrink_dcache_parent(dir);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500229 return ret;
230}
231
Arnd Bergmann67207b92005-11-15 15:53:48 -0500232static int spufs_dir_close(struct inode *inode, struct file *file)
233{
Michael Ellerman0309f022006-06-19 20:33:22 +0200234 struct spu_context *ctx;
Arnd Bergmann62632032006-10-04 17:26:15 +0200235 struct inode *parent;
236 struct dentry *dir;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500237 int ret;
238
Josef Sipekb4d1ab52006-12-08 02:37:30 -0800239 dir = file->f_path.dentry;
Arnd Bergmann62632032006-10-04 17:26:15 +0200240 parent = dir->d_parent->d_inode;
241 ctx = SPUFS_I(dir->d_inode)->i_ctx;
Arnd Bergmannc8ca0632006-01-04 20:31:22 +0100242
Christoph Hellwig02539d72008-05-08 15:29:12 +1000243 mutex_lock_nested(&parent->i_mutex, I_MUTEX_PARENT);
Arnd Bergmann62632032006-10-04 17:26:15 +0200244 ret = spufs_rmdir(parent, dir);
245 mutex_unlock(&parent->i_mutex);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500246 WARN_ON(ret);
Arnd Bergmannc8ca0632006-01-04 20:31:22 +0100247
Michael Ellerman0309f022006-06-19 20:33:22 +0200248 /* We have to give up the mm_struct */
249 spu_forget(ctx);
250
Arnd Bergmann67207b92005-11-15 15:53:48 -0500251 return dcache_dir_close(inode, file);
252}
253
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800254const struct file_operations spufs_context_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500255 .open = dcache_dir_open,
256 .release = spufs_dir_close,
257 .llseek = dcache_dir_lseek,
258 .read = generic_read_dir,
259 .readdir = dcache_readdir,
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200260 .fsync = noop_fsync,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500261};
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100262EXPORT_SYMBOL_GPL(spufs_context_fops);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500263
264static int
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200265spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
Al Viroc6684b22011-07-26 04:47:14 -0400266 umode_t mode)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500267{
268 int ret;
269 struct inode *inode;
270 struct spu_context *ctx;
271
272 ret = -ENOSPC;
273 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
274 if (!inode)
275 goto out;
276
277 if (dir->i_mode & S_ISGID) {
278 inode->i_gid = dir->i_gid;
279 inode->i_mode &= S_ISGID;
280 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200281 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500282 SPUFS_I(inode)->i_ctx = ctx;
283 if (!ctx)
284 goto out_iput;
285
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200286 ctx->flags = flags;
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000287 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500288 inode->i_fop = &simple_dir_operations;
Mark Nutter5737edd2006-10-24 18:31:16 +0200289 if (flags & SPU_CREATE_NOSCHED)
290 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
291 mode, ctx);
292 else
293 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
294
Arnd Bergmann67207b92005-11-15 15:53:48 -0500295 if (ret)
296 goto out_free_ctx;
297
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000298 if (spufs_get_sb_info(dir->i_sb)->debug)
299 ret = spufs_fill_dir(dentry, spufs_dir_debug_contents,
300 mode, ctx);
301
302 if (ret)
303 goto out_free_ctx;
304
Arnd Bergmann67207b92005-11-15 15:53:48 -0500305 d_instantiate(dentry, inode);
306 dget(dentry);
Jeremy Kerrba0b9962008-10-09 10:39:21 +1100307 inc_nlink(dir);
308 inc_nlink(dentry->d_inode);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500309 goto out;
310
311out_free_ctx:
Sebastian Siewior89df0082007-06-04 23:26:51 +1000312 spu_forget(ctx);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500313 put_spu_context(ctx);
314out_iput:
315 iput(inode);
316out:
317 return ret;
318}
319
Al Viro765927b2012-06-26 21:58:53 +0400320static int spufs_context_open(struct path *path)
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100321{
322 int ret;
323 struct file *filp;
324
325 ret = get_unused_fd();
Al Virobf349a42012-06-25 11:46:13 +0400326 if (ret < 0)
327 return ret;
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100328
Al Viro765927b2012-06-26 21:58:53 +0400329 filp = dentry_open(path, O_RDONLY, current_cred());
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100330 if (IS_ERR(filp)) {
331 put_unused_fd(ret);
Al Virobf349a42012-06-25 11:46:13 +0400332 return PTR_ERR(filp);
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100333 }
334
335 filp->f_op = &spufs_context_fops;
336 fd_install(ret, filp);
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100337 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,
Al Viroc6684b22011-07-26 04:47:14 -0400444 struct vfsmount *mnt, int flags, umode_t mode,
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200445 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;
Al Viro765927b2012-06-26 21:58:53 +0400451 struct path path = {.mnt = mnt, .dentry = dentry};
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
Al Viro765927b2012-06-26 21:58:53 +0400494 ret = spufs_context_open(&path);
Arnd Bergmann62632032006-10-04 17:26:15 +0200495 if (ret < 0) {
496 WARN_ON(spufs_rmdir(inode, dentry));
Kou Ishizaki6747c2e2008-10-09 10:45:49 +1100497 if (affinity)
498 mutex_unlock(&gang->aff_mutex);
Arnd Bergmann62632032006-10-04 17:26:15 +0200499 mutex_unlock(&inode->i_mutex);
500 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
501 goto out;
502 }
503
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200504out_aff_unlock:
505 if (affinity)
506 mutex_unlock(&gang->aff_mutex);
Arnd Bergmann62632032006-10-04 17:26:15 +0200507out_unlock:
508 mutex_unlock(&inode->i_mutex);
509out:
510 dput(dentry);
511 return ret;
512}
513
Arnd Bergmann62632032006-10-04 17:26:15 +0200514static int
Al Viroc6684b22011-07-26 04:47:14 -0400515spufs_mkgang(struct inode *dir, struct dentry *dentry, umode_t mode)
Arnd Bergmann62632032006-10-04 17:26:15 +0200516{
517 int ret;
518 struct inode *inode;
519 struct spu_gang *gang;
520
521 ret = -ENOSPC;
522 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
523 if (!inode)
524 goto out;
525
526 ret = 0;
527 if (dir->i_mode & S_ISGID) {
528 inode->i_gid = dir->i_gid;
529 inode->i_mode &= S_ISGID;
530 }
531 gang = alloc_spu_gang();
532 SPUFS_I(inode)->i_ctx = NULL;
533 SPUFS_I(inode)->i_gang = gang;
534 if (!gang)
535 goto out_iput;
536
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000537 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann62632032006-10-04 17:26:15 +0200538 inode->i_fop = &simple_dir_operations;
539
540 d_instantiate(dentry, inode);
Jeremy Kerrba0b9962008-10-09 10:39:21 +1100541 inc_nlink(dir);
542 inc_nlink(dentry->d_inode);
Arnd Bergmann62632032006-10-04 17:26:15 +0200543 return ret;
544
545out_iput:
546 iput(inode);
547out:
548 return ret;
549}
550
Al Viro765927b2012-06-26 21:58:53 +0400551static int spufs_gang_open(struct path *path)
Arnd Bergmann62632032006-10-04 17:26:15 +0200552{
553 int ret;
554 struct file *filp;
555
556 ret = get_unused_fd();
Al Virobf349a42012-06-25 11:46:13 +0400557 if (ret < 0)
558 return ret;
Arnd Bergmann62632032006-10-04 17:26:15 +0200559
Al Virobf349a42012-06-25 11:46:13 +0400560 /*
561 * get references for dget and mntget, will be released
562 * in error path of *_open().
563 */
Al Viro765927b2012-06-26 21:58:53 +0400564 filp = dentry_open(path, O_RDONLY, current_cred());
Arnd Bergmann62632032006-10-04 17:26:15 +0200565 if (IS_ERR(filp)) {
566 put_unused_fd(ret);
Al Virobf349a42012-06-25 11:46:13 +0400567 return PTR_ERR(filp);
Arnd Bergmann62632032006-10-04 17:26:15 +0200568 }
569
Jeremy Kerr877907d2007-06-04 23:26:51 +1000570 filp->f_op = &simple_dir_operations;
Arnd Bergmann62632032006-10-04 17:26:15 +0200571 fd_install(ret, filp);
Arnd Bergmann62632032006-10-04 17:26:15 +0200572 return ret;
573}
574
575static int spufs_create_gang(struct inode *inode,
576 struct dentry *dentry,
Al Viroc6684b22011-07-26 04:47:14 -0400577 struct vfsmount *mnt, umode_t mode)
Arnd Bergmann62632032006-10-04 17:26:15 +0200578{
Al Viro765927b2012-06-26 21:58:53 +0400579 struct path path = {.mnt = mnt, .dentry = dentry};
Arnd Bergmann62632032006-10-04 17:26:15 +0200580 int ret;
581
582 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
583 if (ret)
584 goto out;
585
Al Viro765927b2012-06-26 21:58:53 +0400586 ret = spufs_gang_open(&path);
Jeremy Kerr877907d2007-06-04 23:26:51 +1000587 if (ret < 0) {
588 int err = simple_rmdir(inode, dentry);
589 WARN_ON(err);
590 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200591
592out:
593 mutex_unlock(&inode->i_mutex);
594 dput(dentry);
595 return ret;
596}
597
598
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100599static struct file_system_type spufs_type;
600
Al Viro1ba10682011-06-26 11:54:58 -0400601long spufs_create(struct path *path, struct dentry *dentry,
Al Viroc6684b22011-07-26 04:47:14 -0400602 unsigned int flags, umode_t mode, struct file *filp)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500603{
Arnd Bergmann67207b92005-11-15 15:53:48 -0500604 int ret;
605
Arnd Bergmann67207b92005-11-15 15:53:48 -0500606 ret = -EINVAL;
Arnd Bergmann62632032006-10-04 17:26:15 +0200607 /* check if we are on spufs */
Al Viro1ba10682011-06-26 11:54:58 -0400608 if (path->dentry->d_sb->s_type != &spufs_type)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500609 goto out;
610
Arnd Bergmann62632032006-10-04 17:26:15 +0200611 /* don't accept undefined flags */
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200612 if (flags & (~SPU_CREATE_FLAG_ALL))
arnd@arndb.dec9832942006-06-19 20:33:34 +0200613 goto out;
614
Arnd Bergmann62632032006-10-04 17:26:15 +0200615 /* only threads can be underneath a gang */
Al Viro1ba10682011-06-26 11:54:58 -0400616 if (path->dentry != path->dentry->d_sb->s_root) {
Arnd Bergmann62632032006-10-04 17:26:15 +0200617 if ((flags & SPU_CREATE_GANG) ||
Al Viro1ba10682011-06-26 11:54:58 -0400618 !SPUFS_I(path->dentry->d_inode)->i_gang)
Arnd Bergmann62632032006-10-04 17:26:15 +0200619 goto out;
620 }
621
Al Viroce3b0f82009-03-29 19:08:22 -0400622 mode &= ~current_umask();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500623
Arnd Bergmann62632032006-10-04 17:26:15 +0200624 if (flags & SPU_CREATE_GANG)
Al Viro1ba10682011-06-26 11:54:58 -0400625 ret = spufs_create_gang(path->dentry->d_inode,
626 dentry, path->mnt, mode);
Arnd Bergmann62632032006-10-04 17:26:15 +0200627 else
Al Viro1ba10682011-06-26 11:54:58 -0400628 ret = spufs_create_context(path->dentry->d_inode,
629 dentry, path->mnt, flags, mode,
Jan Blunck4ac91372008-02-14 19:34:32 -0800630 filp);
Christoph Hellwig826be062008-05-06 09:24:24 +1000631 if (ret >= 0)
Al Viro1ba10682011-06-26 11:54:58 -0400632 fsnotify_mkdir(path->dentry->d_inode, dentry);
Christoph Hellwig826be062008-05-06 09:24:24 +1000633 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500634
Arnd Bergmann67207b92005-11-15 15:53:48 -0500635out:
Al Viro1ba10682011-06-26 11:54:58 -0400636 mutex_unlock(&path->dentry->d_inode->i_mutex);
Benjamin Herrenschmidtfb700d32012-03-07 11:01:35 +0000637 dput(dentry);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500638 return ret;
639}
640
641/* File system initialization */
642enum {
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000643 Opt_uid, Opt_gid, Opt_mode, Opt_debug, Opt_err,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500644};
645
Steven Whitehousea447c092008-10-13 10:46:57 +0100646static const match_table_t spufs_tokens = {
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000647 { Opt_uid, "uid=%d" },
648 { Opt_gid, "gid=%d" },
649 { Opt_mode, "mode=%o" },
650 { Opt_debug, "debug" },
651 { Opt_err, NULL },
Arnd Bergmann67207b92005-11-15 15:53:48 -0500652};
653
654static int
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000655spufs_parse_options(struct super_block *sb, char *options, struct inode *root)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500656{
657 char *p;
658 substring_t args[MAX_OPT_ARGS];
659
660 while ((p = strsep(&options, ",")) != NULL) {
661 int token, option;
662
663 if (!*p)
664 continue;
665
666 token = match_token(p, spufs_tokens, args);
667 switch (token) {
668 case Opt_uid:
669 if (match_int(&args[0], &option))
670 return 0;
671 root->i_uid = option;
672 break;
673 case Opt_gid:
674 if (match_int(&args[0], &option))
675 return 0;
676 root->i_gid = option;
677 break;
Jeremy Kerrf11f5ee2007-04-23 21:08:23 +0200678 case Opt_mode:
679 if (match_octal(&args[0], &option))
680 return 0;
681 root->i_mode = option | S_IFDIR;
682 break;
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000683 case Opt_debug:
684 spufs_get_sb_info(sb)->debug = 1;
685 break;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500686 default:
687 return 0;
688 }
689 }
690 return 1;
691}
692
Akinobu Mitadb1384b2007-04-23 21:08:20 +0200693static void spufs_exit_isolated_loader(void)
694{
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000695 free_pages((unsigned long) isolated_loader,
696 get_order(isolated_loader_size));
Akinobu Mitadb1384b2007-04-23 21:08:20 +0200697}
698
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200699static void
700spufs_init_isolated_loader(void)
701{
702 struct device_node *dn;
703 const char *loader;
704 int size;
705
706 dn = of_find_node_by_path("/spu-isolation");
707 if (!dn)
708 return;
709
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000710 loader = of_get_property(dn, "loader", &size);
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200711 if (!loader)
712 return;
713
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000714 /* the loader must be align on a 16 byte boundary */
715 isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200716 if (!isolated_loader)
717 return;
718
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000719 isolated_loader_size = size;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200720 memcpy(isolated_loader, loader, size);
721 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
722}
723
Arnd Bergmann67207b92005-11-15 15:53:48 -0500724static int
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500725spufs_create_root(struct super_block *sb, void *data)
726{
Arnd Bergmann67207b92005-11-15 15:53:48 -0500727 struct inode *inode;
728 int ret;
729
Arnd Bergmann8f18a152007-06-04 23:26:51 +1000730 ret = -ENODEV;
731 if (!spu_management_ops)
732 goto out;
733
Arnd Bergmann67207b92005-11-15 15:53:48 -0500734 ret = -ENOMEM;
735 inode = spufs_new_inode(sb, S_IFDIR | 0775);
736 if (!inode)
737 goto out;
738
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000739 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500740 inode->i_fop = &simple_dir_operations;
741 SPUFS_I(inode)->i_ctx = NULL;
Jeremy Kerre2ed6e42008-10-07 18:26:55 +1100742 inc_nlink(inode);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500743
744 ret = -EINVAL;
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000745 if (!spufs_parse_options(sb, data, inode))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500746 goto out_iput;
747
748 ret = -ENOMEM;
Al Viro48fde702012-01-08 22:15:13 -0500749 sb->s_root = d_make_root(inode);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500750 if (!sb->s_root)
Al Viro48fde702012-01-08 22:15:13 -0500751 goto out;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500752
753 return 0;
754out_iput:
755 iput(inode);
756out:
757 return ret;
758}
759
760static int
761spufs_fill_super(struct super_block *sb, void *data, int silent)
762{
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000763 struct spufs_sb_info *info;
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700764 static const struct super_operations s_ops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500765 .alloc_inode = spufs_alloc_inode,
766 .destroy_inode = spufs_destroy_inode,
767 .statfs = simple_statfs,
Al Viro0f3f63a2010-06-05 21:20:32 -0400768 .evict_inode = spufs_evict_inode,
Miklos Szeredi90d09e12008-02-08 04:21:47 -0800769 .show_options = generic_show_options,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500770 };
771
Miklos Szeredi90d09e12008-02-08 04:21:47 -0800772 save_mount_options(sb, data);
773
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000774 info = kzalloc(sizeof(*info), GFP_KERNEL);
775 if (!info)
776 return -ENOMEM;
777
Arnd Bergmann67207b92005-11-15 15:53:48 -0500778 sb->s_maxbytes = MAX_LFS_FILESIZE;
779 sb->s_blocksize = PAGE_CACHE_SIZE;
780 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
781 sb->s_magic = SPUFS_MAGIC;
782 sb->s_op = &s_ops;
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000783 sb->s_fs_info = info;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500784
785 return spufs_create_root(sb, data);
786}
787
Al Virofc14f2f2010-07-25 01:48:30 +0400788static struct dentry *
789spufs_mount(struct file_system_type *fstype, int flags,
790 const char *name, void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500791{
Al Virofc14f2f2010-07-25 01:48:30 +0400792 return mount_single(fstype, flags, data, spufs_fill_super);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500793}
794
795static struct file_system_type spufs_type = {
796 .owner = THIS_MODULE,
797 .name = "spufs",
Al Virofc14f2f2010-07-25 01:48:30 +0400798 .mount = spufs_mount,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500799 .kill_sb = kill_litter_super,
800};
801
Arnd Bergmanne78b47a2006-03-27 21:27:40 +0200802static int __init spufs_init(void)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500803{
804 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100805
Jeremy Kerrccf17e92007-04-23 21:08:29 +0200806 ret = -ENODEV;
807 if (!spu_management_ops)
808 goto out;
809
Arnd Bergmann67207b92005-11-15 15:53:48 -0500810 ret = -ENOMEM;
811 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
812 sizeof(struct spufs_inode_info), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900813 SLAB_HWCACHE_ALIGN, spufs_init_once);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500814
815 if (!spufs_inode_cache)
816 goto out;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200817 ret = spu_sched_init();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500818 if (ret)
819 goto out_cache;
820 ret = register_spu_syscalls(&spufs_calls);
821 if (ret)
Al Viro640045a2012-03-17 01:37:51 -0400822 goto out_sched;
823 ret = register_filesystem(&spufs_type);
824 if (ret)
825 goto out_syscalls;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200826
827 spufs_init_isolated_loader();
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100828
Arnd Bergmann67207b92005-11-15 15:53:48 -0500829 return 0;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200830
Al Viro640045a2012-03-17 01:37:51 -0400831out_syscalls:
832 unregister_spu_syscalls(&spufs_calls);
Akinobu Mitac99c1992007-04-23 21:08:19 +0200833out_sched:
834 spu_sched_exit();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500835out_cache:
836 kmem_cache_destroy(spufs_inode_cache);
837out:
838 return ret;
839}
840module_init(spufs_init);
841
Arnd Bergmanne78b47a2006-03-27 21:27:40 +0200842static void __exit spufs_exit(void)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500843{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500844 spu_sched_exit();
Akinobu Mitadb1384b2007-04-23 21:08:20 +0200845 spufs_exit_isolated_loader();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500846 unregister_spu_syscalls(&spufs_calls);
847 unregister_filesystem(&spufs_type);
848 kmem_cache_destroy(spufs_inode_cache);
849}
850module_exit(spufs_exit);
851
852MODULE_LICENSE("GPL");
853MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
854