blob: 5364d4a542493880bc7b14d75c57b04cf2a5469d [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
Michael Ellerman6747e832013-04-23 15:13:14 +0000102 inode->i_ino = get_next_ino();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500103 inode->i_mode = mode;
David Howells1330deb2008-11-14 10:38:39 +1100104 inode->i_uid = current_fsuid();
105 inode->i_gid = current_fsgid();
Deepa Dinamani078cd822016-09-14 07:48:04 -0700106 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500107out:
108 return inode;
109}
110
111static int
112spufs_setattr(struct dentry *dentry, struct iattr *attr)
113{
David Howells75c3cfa2015-03-17 22:26:12 +0000114 struct inode *inode = d_inode(dentry);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500115
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,
Al Viroc6684b22011-07-26 04:47:14 -0400127 const struct file_operations *fops, umode_t 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);
Jan Karadbd57682012-05-03 14:48:02 +0200155 clear_inode(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
Al Viro59551022016-01-22 15:40:57 -0500166 inode_lock(d_inode(dir));
Al Viro946e51f2014-10-26 19:19:16 -0400167 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_child) {
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100168 spin_lock(&dentry->d_lock);
Al Virodc3f4192015-05-18 10:10:34 -0400169 if (simple_positive(dentry)) {
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);
David Howells75c3cfa2015-03-17 22:26:12 +0000173 simple_unlink(d_inode(dir), 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);
Al Viro59551022016-01-22 15:40:57 -0500183 inode_unlock(d_inode(dir));
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 */
Al Viro67cba9f2012-07-19 16:03:21 +0400190 int res;
Arnd Bergmann62632032006-10-04 17:26:15 +0200191 spufs_prune_dir(dir);
Jeremy Kerrc443aca2007-11-16 13:32:23 +1100192 d_drop(dir);
Al Viro67cba9f2012-07-19 16:03:21 +0400193 res = simple_rmdir(parent, dir);
194 /* We have to give up the mm_struct */
David Howells75c3cfa2015-03-17 22:26:12 +0000195 spu_forget(SPUFS_I(d_inode(dir))->i_ctx);
Al Viro67cba9f2012-07-19 16:03:21 +0400196 return res;
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100197}
198
Jeremy Kerr74254642009-02-17 11:44:14 +1100199static int spufs_fill_dir(struct dentry *dir,
Al Viroc6684b22011-07-26 04:47:14 -0400200 const struct spufs_tree_descr *files, umode_t mode,
Jeremy Kerr74254642009-02-17 11:44:14 +1100201 struct spu_context *ctx)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500202{
Arnd Bergmann67207b92005-11-15 15:53:48 -0500203 while (files->name && files->name[0]) {
Al Viro2248b872013-01-28 20:37:21 -0500204 int ret;
205 struct dentry *dentry = d_alloc_name(dir, files->name);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500206 if (!dentry)
Al Viro2248b872013-01-28 20:37:21 -0500207 return -ENOMEM;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500208 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)
Al Viro2248b872013-01-28 20:37:21 -0500211 return ret;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500212 files++;
213 }
214 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500215}
216
Arnd Bergmann67207b92005-11-15 15:53:48 -0500217static int spufs_dir_close(struct inode *inode, struct file *file)
218{
Michael Ellerman0309f022006-06-19 20:33:22 +0200219 struct spu_context *ctx;
Arnd Bergmann62632032006-10-04 17:26:15 +0200220 struct inode *parent;
221 struct dentry *dir;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500222 int ret;
223
Josef Sipekb4d1ab52006-12-08 02:37:30 -0800224 dir = file->f_path.dentry;
David Howells75c3cfa2015-03-17 22:26:12 +0000225 parent = d_inode(dir->d_parent);
226 ctx = SPUFS_I(d_inode(dir))->i_ctx;
Arnd Bergmannc8ca0632006-01-04 20:31:22 +0100227
Al Viro59551022016-01-22 15:40:57 -0500228 inode_lock_nested(parent, I_MUTEX_PARENT);
Arnd Bergmann62632032006-10-04 17:26:15 +0200229 ret = spufs_rmdir(parent, dir);
Al Viro59551022016-01-22 15:40:57 -0500230 inode_unlock(parent);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500231 WARN_ON(ret);
Arnd Bergmannc8ca0632006-01-04 20:31:22 +0100232
Arnd Bergmann67207b92005-11-15 15:53:48 -0500233 return dcache_dir_close(inode, file);
234}
235
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800236const struct file_operations spufs_context_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500237 .open = dcache_dir_open,
238 .release = spufs_dir_close,
239 .llseek = dcache_dir_lseek,
240 .read = generic_read_dir,
Al Viro4e829012016-04-20 19:52:15 -0400241 .iterate_shared = dcache_readdir,
Christoph Hellwig1b061d92010-05-26 17:53:41 +0200242 .fsync = noop_fsync,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500243};
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100244EXPORT_SYMBOL_GPL(spufs_context_fops);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500245
246static int
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200247spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
Al Viroc6684b22011-07-26 04:47:14 -0400248 umode_t mode)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500249{
250 int ret;
251 struct inode *inode;
252 struct spu_context *ctx;
253
Arnd Bergmann67207b92005-11-15 15:53:48 -0500254 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
255 if (!inode)
Al Viro2248b872013-01-28 20:37:21 -0500256 return -ENOSPC;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500257
258 if (dir->i_mode & S_ISGID) {
259 inode->i_gid = dir->i_gid;
260 inode->i_mode &= S_ISGID;
261 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200262 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
Arnd Bergmann67207b92005-11-15 15:53:48 -0500263 SPUFS_I(inode)->i_ctx = ctx;
Al Viro2248b872013-01-28 20:37:21 -0500264 if (!ctx) {
265 iput(inode);
266 return -ENOSPC;
267 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500268
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200269 ctx->flags = flags;
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000270 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500271 inode->i_fop = &simple_dir_operations;
Al Viro2248b872013-01-28 20:37:21 -0500272
Al Viro59551022016-01-22 15:40:57 -0500273 inode_lock(inode);
Al Viro2248b872013-01-28 20:37:21 -0500274
275 dget(dentry);
276 inc_nlink(dir);
277 inc_nlink(inode);
278
279 d_instantiate(dentry, inode);
280
Mark Nutter5737edd2006-10-24 18:31:16 +0200281 if (flags & SPU_CREATE_NOSCHED)
282 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
283 mode, ctx);
284 else
285 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
286
Al Viro2248b872013-01-28 20:37:21 -0500287 if (!ret && spufs_get_sb_info(dir->i_sb)->debug)
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000288 ret = spufs_fill_dir(dentry, spufs_dir_debug_contents,
289 mode, ctx);
290
291 if (ret)
Al Viro2248b872013-01-28 20:37:21 -0500292 spufs_rmdir(dir, dentry);
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000293
Al Viro59551022016-01-22 15:40:57 -0500294 inode_unlock(inode);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500295
Arnd Bergmann67207b92005-11-15 15:53:48 -0500296 return ret;
297}
298
Al Viro765927b2012-06-26 21:58:53 +0400299static int spufs_context_open(struct path *path)
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100300{
301 int ret;
302 struct file *filp;
303
Yann Droneaud6b9cdf32014-12-10 15:45:39 -0800304 ret = get_unused_fd_flags(0);
Al Virobf349a42012-06-25 11:46:13 +0400305 if (ret < 0)
306 return ret;
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100307
Al Viro765927b2012-06-26 21:58:53 +0400308 filp = dentry_open(path, O_RDONLY, current_cred());
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100309 if (IS_ERR(filp)) {
310 put_unused_fd(ret);
Al Virobf349a42012-06-25 11:46:13 +0400311 return PTR_ERR(filp);
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100312 }
313
314 filp->f_op = &spufs_context_fops;
315 fd_install(ret, filp);
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100316 return ret;
317}
318
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200319static struct spu_context *
320spufs_assert_affinity(unsigned int flags, struct spu_gang *gang,
321 struct file *filp)
322{
Andre Detsch58119062008-01-11 15:03:26 +1100323 struct spu_context *tmp, *neighbor, *err;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200324 int count, node;
325 int aff_supp;
326
327 aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next,
328 struct spu, cbe_list))->aff_list);
329
330 if (!aff_supp)
331 return ERR_PTR(-EINVAL);
332
333 if (flags & SPU_CREATE_GANG)
334 return ERR_PTR(-EINVAL);
335
336 if (flags & SPU_CREATE_AFFINITY_MEM &&
337 gang->aff_ref_ctx &&
338 gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM)
339 return ERR_PTR(-EEXIST);
340
341 if (gang->aff_flags & AFF_MERGED)
342 return ERR_PTR(-EBUSY);
343
344 neighbor = NULL;
345 if (flags & SPU_CREATE_AFFINITY_SPU) {
346 if (!filp || filp->f_op != &spufs_context_fops)
347 return ERR_PTR(-EINVAL);
348
349 neighbor = get_spu_context(
Al Viro496ad9a2013-01-23 17:07:38 -0500350 SPUFS_I(file_inode(filp))->i_ctx);
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200351
352 if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) &&
353 !list_is_last(&neighbor->aff_list, &gang->aff_list_head) &&
354 !list_entry(neighbor->aff_list.next, struct spu_context,
Andre Detsch58119062008-01-11 15:03:26 +1100355 aff_list)->aff_head) {
356 err = ERR_PTR(-EEXIST);
357 goto out_put_neighbor;
358 }
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200359
Andre Detsch58119062008-01-11 15:03:26 +1100360 if (gang != neighbor->gang) {
361 err = ERR_PTR(-EINVAL);
362 goto out_put_neighbor;
363 }
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200364
365 count = 1;
366 list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
367 count++;
368 if (list_empty(&neighbor->aff_list))
369 count++;
370
371 for (node = 0; node < MAX_NUMNODES; node++) {
372 if ((cbe_spu_info[node].n_spus - atomic_read(
373 &cbe_spu_info[node].reserved_spus)) >= count)
374 break;
375 }
376
Andre Detsch58119062008-01-11 15:03:26 +1100377 if (node == MAX_NUMNODES) {
378 err = ERR_PTR(-EEXIST);
379 goto out_put_neighbor;
380 }
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200381 }
382
383 return neighbor;
Andre Detsch58119062008-01-11 15:03:26 +1100384
385out_put_neighbor:
386 put_spu_context(neighbor);
387 return err;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200388}
389
390static void
391spufs_set_affinity(unsigned int flags, struct spu_context *ctx,
392 struct spu_context *neighbor)
393{
394 if (flags & SPU_CREATE_AFFINITY_MEM)
395 ctx->gang->aff_ref_ctx = ctx;
396
397 if (flags & SPU_CREATE_AFFINITY_SPU) {
398 if (list_empty(&neighbor->aff_list)) {
399 list_add_tail(&neighbor->aff_list,
400 &ctx->gang->aff_list_head);
401 neighbor->aff_head = 1;
402 }
403
404 if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head)
405 || list_entry(neighbor->aff_list.next, struct spu_context,
406 aff_list)->aff_head) {
407 list_add(&ctx->aff_list, &neighbor->aff_list);
408 } else {
409 list_add_tail(&ctx->aff_list, &neighbor->aff_list);
410 if (neighbor->aff_head) {
411 neighbor->aff_head = 0;
412 ctx->aff_head = 1;
413 }
414 }
415
416 if (!ctx->gang->aff_ref_ctx)
417 ctx->gang->aff_ref_ctx = ctx;
418 }
419}
420
421static int
422spufs_create_context(struct inode *inode, struct dentry *dentry,
Al Viroc6684b22011-07-26 04:47:14 -0400423 struct vfsmount *mnt, int flags, umode_t mode,
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200424 struct file *aff_filp)
Arnd Bergmann62632032006-10-04 17:26:15 +0200425{
426 int ret;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200427 int affinity;
428 struct spu_gang *gang;
429 struct spu_context *neighbor;
Al Viro765927b2012-06-26 21:58:53 +0400430 struct path path = {.mnt = mnt, .dentry = dentry};
Arnd Bergmann62632032006-10-04 17:26:15 +0200431
Mark Nutter5737edd2006-10-24 18:31:16 +0200432 if ((flags & SPU_CREATE_NOSCHED) &&
433 !capable(CAP_SYS_NICE))
Al Viro1ba44cc2012-07-19 16:12:22 +0400434 return -EPERM;
Mark Nutter5737edd2006-10-24 18:31:16 +0200435
Mark Nutter5737edd2006-10-24 18:31:16 +0200436 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
437 == SPU_CREATE_ISOLATE)
Al Viro1ba44cc2012-07-19 16:12:22 +0400438 return -EINVAL;
Mark Nutter5737edd2006-10-24 18:31:16 +0200439
Jeremy Kerrbd2e5f82006-11-27 19:18:52 +0100440 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
Al Viro1ba44cc2012-07-19 16:12:22 +0400441 return -ENODEV;
Jeremy Kerrbd2e5f82006-11-27 19:18:52 +0100442
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200443 gang = NULL;
444 neighbor = NULL;
445 affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU);
446 if (affinity) {
447 gang = SPUFS_I(inode)->i_gang;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200448 if (!gang)
Al Viro1ba44cc2012-07-19 16:12:22 +0400449 return -EINVAL;
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200450 mutex_lock(&gang->aff_mutex);
451 neighbor = spufs_assert_affinity(flags, gang, aff_filp);
452 if (IS_ERR(neighbor)) {
453 ret = PTR_ERR(neighbor);
454 goto out_aff_unlock;
455 }
456 }
457
Arnd Bergmann62632032006-10-04 17:26:15 +0200458 ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
459 if (ret)
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200460 goto out_aff_unlock;
461
Andre Detsch58119062008-01-11 15:03:26 +1100462 if (affinity) {
David Howells75c3cfa2015-03-17 22:26:12 +0000463 spufs_set_affinity(flags, SPUFS_I(d_inode(dentry))->i_ctx,
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200464 neighbor);
Andre Detsch58119062008-01-11 15:03:26 +1100465 if (neighbor)
466 put_spu_context(neighbor);
467 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200468
Al Viro765927b2012-06-26 21:58:53 +0400469 ret = spufs_context_open(&path);
Al Viro66ec7b22012-07-19 16:07:30 +0400470 if (ret < 0)
Arnd Bergmann62632032006-10-04 17:26:15 +0200471 WARN_ON(spufs_rmdir(inode, dentry));
Arnd Bergmann62632032006-10-04 17:26:15 +0200472
Arnd Bergmann8e68e2f2007-07-20 21:39:47 +0200473out_aff_unlock:
474 if (affinity)
475 mutex_unlock(&gang->aff_mutex);
Arnd Bergmann62632032006-10-04 17:26:15 +0200476 return ret;
477}
478
Arnd Bergmann62632032006-10-04 17:26:15 +0200479static int
Al Viroc6684b22011-07-26 04:47:14 -0400480spufs_mkgang(struct inode *dir, struct dentry *dentry, umode_t mode)
Arnd Bergmann62632032006-10-04 17:26:15 +0200481{
482 int ret;
483 struct inode *inode;
484 struct spu_gang *gang;
485
486 ret = -ENOSPC;
487 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
488 if (!inode)
489 goto out;
490
491 ret = 0;
492 if (dir->i_mode & S_ISGID) {
493 inode->i_gid = dir->i_gid;
494 inode->i_mode &= S_ISGID;
495 }
496 gang = alloc_spu_gang();
497 SPUFS_I(inode)->i_ctx = NULL;
498 SPUFS_I(inode)->i_gang = gang;
Dan Carpenter54a94fc2016-08-04 08:37:03 +0300499 if (!gang) {
500 ret = -ENOMEM;
Arnd Bergmann62632032006-10-04 17:26:15 +0200501 goto out_iput;
Dan Carpenter54a94fc2016-08-04 08:37:03 +0300502 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200503
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000504 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann62632032006-10-04 17:26:15 +0200505 inode->i_fop = &simple_dir_operations;
506
507 d_instantiate(dentry, inode);
Jeremy Kerrba0b9962008-10-09 10:39:21 +1100508 inc_nlink(dir);
David Howells75c3cfa2015-03-17 22:26:12 +0000509 inc_nlink(d_inode(dentry));
Arnd Bergmann62632032006-10-04 17:26:15 +0200510 return ret;
511
512out_iput:
513 iput(inode);
514out:
515 return ret;
516}
517
Al Viro765927b2012-06-26 21:58:53 +0400518static int spufs_gang_open(struct path *path)
Arnd Bergmann62632032006-10-04 17:26:15 +0200519{
520 int ret;
521 struct file *filp;
522
Yann Droneaud6b9cdf32014-12-10 15:45:39 -0800523 ret = get_unused_fd_flags(0);
Al Virobf349a42012-06-25 11:46:13 +0400524 if (ret < 0)
525 return ret;
Arnd Bergmann62632032006-10-04 17:26:15 +0200526
Al Virobf349a42012-06-25 11:46:13 +0400527 /*
528 * get references for dget and mntget, will be released
529 * in error path of *_open().
530 */
Al Viro765927b2012-06-26 21:58:53 +0400531 filp = dentry_open(path, O_RDONLY, current_cred());
Arnd Bergmann62632032006-10-04 17:26:15 +0200532 if (IS_ERR(filp)) {
533 put_unused_fd(ret);
Al Virobf349a42012-06-25 11:46:13 +0400534 return PTR_ERR(filp);
Arnd Bergmann62632032006-10-04 17:26:15 +0200535 }
536
Jeremy Kerr877907d2007-06-04 23:26:51 +1000537 filp->f_op = &simple_dir_operations;
Arnd Bergmann62632032006-10-04 17:26:15 +0200538 fd_install(ret, filp);
Arnd Bergmann62632032006-10-04 17:26:15 +0200539 return ret;
540}
541
542static int spufs_create_gang(struct inode *inode,
543 struct dentry *dentry,
Al Viroc6684b22011-07-26 04:47:14 -0400544 struct vfsmount *mnt, umode_t mode)
Arnd Bergmann62632032006-10-04 17:26:15 +0200545{
Al Viro765927b2012-06-26 21:58:53 +0400546 struct path path = {.mnt = mnt, .dentry = dentry};
Arnd Bergmann62632032006-10-04 17:26:15 +0200547 int ret;
548
549 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
Al Viro1ba44cc2012-07-19 16:12:22 +0400550 if (!ret) {
551 ret = spufs_gang_open(&path);
552 if (ret < 0) {
553 int err = simple_rmdir(inode, dentry);
554 WARN_ON(err);
555 }
Jeremy Kerr877907d2007-06-04 23:26:51 +1000556 }
Arnd Bergmann62632032006-10-04 17:26:15 +0200557 return ret;
558}
559
560
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100561static struct file_system_type spufs_type;
562
Al Viro1ba10682011-06-26 11:54:58 -0400563long spufs_create(struct path *path, struct dentry *dentry,
Al Viroc6684b22011-07-26 04:47:14 -0400564 unsigned int flags, umode_t mode, struct file *filp)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500565{
David Howells75c3cfa2015-03-17 22:26:12 +0000566 struct inode *dir = d_inode(path->dentry);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500567 int ret;
568
Arnd Bergmann62632032006-10-04 17:26:15 +0200569 /* check if we are on spufs */
Al Viro1ba10682011-06-26 11:54:58 -0400570 if (path->dentry->d_sb->s_type != &spufs_type)
Al Viro25b26922012-07-19 16:23:13 +0400571 return -EINVAL;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500572
Arnd Bergmann62632032006-10-04 17:26:15 +0200573 /* don't accept undefined flags */
Arnd Bergmann9add11d2006-10-04 17:26:14 +0200574 if (flags & (~SPU_CREATE_FLAG_ALL))
Al Viro25b26922012-07-19 16:23:13 +0400575 return -EINVAL;
arnd@arndb.dec9832942006-06-19 20:33:34 +0200576
Arnd Bergmann62632032006-10-04 17:26:15 +0200577 /* only threads can be underneath a gang */
Al Viro25b26922012-07-19 16:23:13 +0400578 if (path->dentry != path->dentry->d_sb->s_root)
579 if ((flags & SPU_CREATE_GANG) || !SPUFS_I(dir)->i_gang)
580 return -EINVAL;
Arnd Bergmann62632032006-10-04 17:26:15 +0200581
Al Viroce3b0f82009-03-29 19:08:22 -0400582 mode &= ~current_umask();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500583
Arnd Bergmann62632032006-10-04 17:26:15 +0200584 if (flags & SPU_CREATE_GANG)
Al Viro25b26922012-07-19 16:23:13 +0400585 ret = spufs_create_gang(dir, dentry, path->mnt, mode);
Arnd Bergmann62632032006-10-04 17:26:15 +0200586 else
Al Viro25b26922012-07-19 16:23:13 +0400587 ret = spufs_create_context(dir, dentry, path->mnt, flags, mode,
Jan Blunck4ac91372008-02-14 19:34:32 -0800588 filp);
Christoph Hellwig826be062008-05-06 09:24:24 +1000589 if (ret >= 0)
Al Viro25b26922012-07-19 16:23:13 +0400590 fsnotify_mkdir(dir, dentry);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500591
Arnd Bergmann67207b92005-11-15 15:53:48 -0500592 return ret;
593}
594
595/* File system initialization */
596enum {
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000597 Opt_uid, Opt_gid, Opt_mode, Opt_debug, Opt_err,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500598};
599
Steven Whitehousea447c092008-10-13 10:46:57 +0100600static const match_table_t spufs_tokens = {
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000601 { Opt_uid, "uid=%d" },
602 { Opt_gid, "gid=%d" },
603 { Opt_mode, "mode=%o" },
604 { Opt_debug, "debug" },
605 { Opt_err, NULL },
Arnd Bergmann67207b92005-11-15 15:53:48 -0500606};
607
608static int
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000609spufs_parse_options(struct super_block *sb, char *options, struct inode *root)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500610{
611 char *p;
612 substring_t args[MAX_OPT_ARGS];
613
614 while ((p = strsep(&options, ",")) != NULL) {
615 int token, option;
616
617 if (!*p)
618 continue;
619
620 token = match_token(p, spufs_tokens, args);
621 switch (token) {
622 case Opt_uid:
623 if (match_int(&args[0], &option))
624 return 0;
Dwight Engened56f342013-08-21 14:33:51 -0400625 root->i_uid = make_kuid(current_user_ns(), option);
626 if (!uid_valid(root->i_uid))
627 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500628 break;
629 case Opt_gid:
630 if (match_int(&args[0], &option))
631 return 0;
Dwight Engened56f342013-08-21 14:33:51 -0400632 root->i_gid = make_kgid(current_user_ns(), option);
633 if (!gid_valid(root->i_gid))
634 return 0;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500635 break;
Jeremy Kerrf11f5ee2007-04-23 21:08:23 +0200636 case Opt_mode:
637 if (match_octal(&args[0], &option))
638 return 0;
639 root->i_mode = option | S_IFDIR;
640 break;
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000641 case Opt_debug:
642 spufs_get_sb_info(sb)->debug = 1;
643 break;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500644 default:
645 return 0;
646 }
647 }
648 return 1;
649}
650
Akinobu Mitadb1384b2007-04-23 21:08:20 +0200651static void spufs_exit_isolated_loader(void)
652{
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000653 free_pages((unsigned long) isolated_loader,
654 get_order(isolated_loader_size));
Akinobu Mitadb1384b2007-04-23 21:08:20 +0200655}
656
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200657static void
658spufs_init_isolated_loader(void)
659{
660 struct device_node *dn;
661 const char *loader;
662 int size;
663
664 dn = of_find_node_by_path("/spu-isolation");
665 if (!dn)
666 return;
667
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000668 loader = of_get_property(dn, "loader", &size);
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200669 if (!loader)
670 return;
671
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000672 /* the loader must be align on a 16 byte boundary */
673 isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size));
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200674 if (!isolated_loader)
675 return;
676
Sebastian Siewior8b0d3122007-09-19 14:38:12 +1000677 isolated_loader_size = size;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200678 memcpy(isolated_loader, loader, size);
679 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
680}
681
Arnd Bergmann67207b92005-11-15 15:53:48 -0500682static int
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500683spufs_create_root(struct super_block *sb, void *data)
684{
Arnd Bergmann67207b92005-11-15 15:53:48 -0500685 struct inode *inode;
686 int ret;
687
Arnd Bergmann8f18a152007-06-04 23:26:51 +1000688 ret = -ENODEV;
689 if (!spu_management_ops)
690 goto out;
691
Arnd Bergmann67207b92005-11-15 15:53:48 -0500692 ret = -ENOMEM;
693 inode = spufs_new_inode(sb, S_IFDIR | 0775);
694 if (!inode)
695 goto out;
696
Jeremy Kerrb8c295f2007-06-29 10:57:59 +1000697 inode->i_op = &simple_dir_inode_operations;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500698 inode->i_fop = &simple_dir_operations;
699 SPUFS_I(inode)->i_ctx = NULL;
Jeremy Kerre2ed6e42008-10-07 18:26:55 +1100700 inc_nlink(inode);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500701
702 ret = -EINVAL;
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000703 if (!spufs_parse_options(sb, data, inode))
Arnd Bergmann67207b92005-11-15 15:53:48 -0500704 goto out_iput;
705
706 ret = -ENOMEM;
Al Viro48fde702012-01-08 22:15:13 -0500707 sb->s_root = d_make_root(inode);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500708 if (!sb->s_root)
Al Viro48fde702012-01-08 22:15:13 -0500709 goto out;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500710
711 return 0;
712out_iput:
713 iput(inode);
714out:
715 return ret;
716}
717
718static int
719spufs_fill_super(struct super_block *sb, void *data, int silent)
720{
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000721 struct spufs_sb_info *info;
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700722 static const struct super_operations s_ops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500723 .alloc_inode = spufs_alloc_inode,
724 .destroy_inode = spufs_destroy_inode,
725 .statfs = simple_statfs,
Al Viro0f3f63a2010-06-05 21:20:32 -0400726 .evict_inode = spufs_evict_inode,
Miklos Szeredi90d09e12008-02-08 04:21:47 -0800727 .show_options = generic_show_options,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500728 };
729
Miklos Szeredi90d09e12008-02-08 04:21:47 -0800730 save_mount_options(sb, data);
731
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000732 info = kzalloc(sizeof(*info), GFP_KERNEL);
733 if (!info)
734 return -ENOMEM;
735
Arnd Bergmann67207b92005-11-15 15:53:48 -0500736 sb->s_maxbytes = MAX_LFS_FILESIZE;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300737 sb->s_blocksize = PAGE_SIZE;
738 sb->s_blocksize_bits = PAGE_SHIFT;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500739 sb->s_magic = SPUFS_MAGIC;
740 sb->s_op = &s_ops;
Jeremy Kerr2c3e4782008-07-03 11:42:20 +1000741 sb->s_fs_info = info;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500742
743 return spufs_create_root(sb, data);
744}
745
Al Virofc14f2f2010-07-25 01:48:30 +0400746static struct dentry *
747spufs_mount(struct file_system_type *fstype, int flags,
748 const char *name, void *data)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500749{
Al Virofc14f2f2010-07-25 01:48:30 +0400750 return mount_single(fstype, flags, data, spufs_fill_super);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500751}
752
753static struct file_system_type spufs_type = {
754 .owner = THIS_MODULE,
755 .name = "spufs",
Al Virofc14f2f2010-07-25 01:48:30 +0400756 .mount = spufs_mount,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500757 .kill_sb = kill_litter_super,
758};
Eric W. Biederman7f78e032013-03-02 19:39:14 -0800759MODULE_ALIAS_FS("spufs");
Arnd Bergmann67207b92005-11-15 15:53:48 -0500760
Arnd Bergmanne78b47a2006-03-27 21:27:40 +0200761static int __init spufs_init(void)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500762{
763 int ret;
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100764
Jeremy Kerrccf17e92007-04-23 21:08:29 +0200765 ret = -ENODEV;
766 if (!spu_management_ops)
767 goto out;
768
Arnd Bergmann67207b92005-11-15 15:53:48 -0500769 ret = -ENOMEM;
770 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
771 sizeof(struct spufs_inode_info), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -0800772 SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT, spufs_init_once);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500773
774 if (!spufs_inode_cache)
775 goto out;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200776 ret = spu_sched_init();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500777 if (ret)
778 goto out_cache;
779 ret = register_spu_syscalls(&spufs_calls);
780 if (ret)
Al Viro640045a2012-03-17 01:37:51 -0400781 goto out_sched;
782 ret = register_filesystem(&spufs_type);
783 if (ret)
784 goto out_syscalls;
arnd@arndb.de0afacde2006-10-24 18:31:18 +0200785
786 spufs_init_isolated_loader();
Dwayne Grant McConnellbf1ab972006-11-23 00:46:37 +0100787
Arnd Bergmann67207b92005-11-15 15:53:48 -0500788 return 0;
Akinobu Mitac99c1992007-04-23 21:08:19 +0200789
Al Viro640045a2012-03-17 01:37:51 -0400790out_syscalls:
791 unregister_spu_syscalls(&spufs_calls);
Akinobu Mitac99c1992007-04-23 21:08:19 +0200792out_sched:
793 spu_sched_exit();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500794out_cache:
795 kmem_cache_destroy(spufs_inode_cache);
796out:
797 return ret;
798}
799module_init(spufs_init);
800
Arnd Bergmanne78b47a2006-03-27 21:27:40 +0200801static void __exit spufs_exit(void)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500802{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500803 spu_sched_exit();
Akinobu Mitadb1384b2007-04-23 21:08:20 +0200804 spufs_exit_isolated_loader();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500805 unregister_spu_syscalls(&spufs_calls);
806 unregister_filesystem(&spufs_type);
807 kmem_cache_destroy(spufs_inode_cache);
808}
809module_exit(spufs_exit);
810
811MODULE_LICENSE("GPL");
812MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
813