]>
Commit | Line | Data |
---|---|---|
4ac91378 | 1 | |
67207b96 AB |
2 | /* |
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> | |
826be063 | 26 | #include <linux/fsnotify.h> |
67207b96 AB |
27 | #include <linux/backing-dev.h> |
28 | #include <linux/init.h> | |
29 | #include <linux/ioctl.h> | |
30 | #include <linux/module.h> | |
346f4d3c | 31 | #include <linux/mount.h> |
67207b96 AB |
32 | #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 | ||
0afacde3 | 38 | #include <asm/prom.h> |
67207b96 | 39 | #include <asm/spu.h> |
ccf17e9d | 40 | #include <asm/spu_priv1.h> |
67207b96 AB |
41 | #include <asm/uaccess.h> |
42 | ||
43 | #include "spufs.h" | |
44 | ||
2c3e4787 JK |
45 | struct spufs_sb_info { |
46 | int debug; | |
47 | }; | |
48 | ||
e18b890b | 49 | static struct kmem_cache *spufs_inode_cache; |
c6730ed4 | 50 | char *isolated_loader; |
8b0d3121 | 51 | static int isolated_loader_size; |
67207b96 | 52 | |
2c3e4787 JK |
53 | static struct spufs_sb_info *spufs_get_sb_info(struct super_block *sb) |
54 | { | |
55 | return sb->s_fs_info; | |
56 | } | |
57 | ||
67207b96 AB |
58 | static struct inode * |
59 | spufs_alloc_inode(struct super_block *sb) | |
60 | { | |
61 | struct spufs_inode_info *ei; | |
62 | ||
e94b1766 | 63 | ei = kmem_cache_alloc(spufs_inode_cache, GFP_KERNEL); |
67207b96 AB |
64 | if (!ei) |
65 | return NULL; | |
6263203e AB |
66 | |
67 | ei->i_gang = NULL; | |
68 | ei->i_ctx = NULL; | |
43c2bbd9 | 69 | ei->i_openers = 0; |
6263203e | 70 | |
67207b96 AB |
71 | return &ei->vfs_inode; |
72 | } | |
73 | ||
fa0d7e3d | 74 | static void spufs_i_callback(struct rcu_head *head) |
67207b96 | 75 | { |
fa0d7e3d | 76 | struct inode *inode = container_of(head, struct inode, i_rcu); |
67207b96 AB |
77 | kmem_cache_free(spufs_inode_cache, SPUFS_I(inode)); |
78 | } | |
79 | ||
fa0d7e3d NP |
80 | static void spufs_destroy_inode(struct inode *inode) |
81 | { | |
82 | call_rcu(&inode->i_rcu, spufs_i_callback); | |
83 | } | |
84 | ||
67207b96 | 85 | static void |
51cc5068 | 86 | spufs_init_once(void *p) |
67207b96 AB |
87 | { |
88 | struct spufs_inode_info *ei = p; | |
89 | ||
a35afb83 | 90 | inode_init_once(&ei->vfs_inode); |
67207b96 AB |
91 | } |
92 | ||
93 | static struct inode * | |
c6684b26 | 94 | spufs_new_inode(struct super_block *sb, umode_t mode) |
67207b96 AB |
95 | { |
96 | struct inode *inode; | |
97 | ||
98 | inode = new_inode(sb); | |
99 | if (!inode) | |
100 | goto out; | |
101 | ||
6747e832 | 102 | inode->i_ino = get_next_ino(); |
67207b96 | 103 | inode->i_mode = mode; |
1330deb0 DH |
104 | inode->i_uid = current_fsuid(); |
105 | inode->i_gid = current_fsgid(); | |
078cd827 | 106 | inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); |
67207b96 AB |
107 | out: |
108 | return inode; | |
109 | } | |
110 | ||
111 | static int | |
112 | spufs_setattr(struct dentry *dentry, struct iattr *attr) | |
113 | { | |
75c3cfa8 | 114 | struct inode *inode = d_inode(dentry); |
67207b96 | 115 | |
67207b96 AB |
116 | if ((attr->ia_valid & ATTR_SIZE) && |
117 | (attr->ia_size != inode->i_size)) | |
118 | return -EINVAL; | |
1025774c CH |
119 | setattr_copy(inode, attr); |
120 | mark_inode_dirty(inode); | |
121 | return 0; | |
67207b96 AB |
122 | } |
123 | ||
124 | ||
125 | static int | |
126 | spufs_new_file(struct super_block *sb, struct dentry *dentry, | |
c6684b26 | 127 | const struct file_operations *fops, umode_t mode, |
23d893f5 | 128 | size_t size, struct spu_context *ctx) |
67207b96 | 129 | { |
6e1d5dcc | 130 | static const struct inode_operations spufs_file_iops = { |
67207b96 | 131 | .setattr = spufs_setattr, |
67207b96 AB |
132 | }; |
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; | |
23d893f5 | 144 | inode->i_size = size; |
8e18e294 | 145 | inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx); |
67207b96 AB |
146 | d_add(dentry, inode); |
147 | out: | |
148 | return ret; | |
149 | } | |
150 | ||
151 | static void | |
0f3f63a4 | 152 | spufs_evict_inode(struct inode *inode) |
67207b96 | 153 | { |
6263203e | 154 | struct spufs_inode_info *ei = SPUFS_I(inode); |
dbd5768f | 155 | clear_inode(inode); |
6263203e AB |
156 | if (ei->i_ctx) |
157 | put_spu_context(ei->i_ctx); | |
158 | if (ei->i_gang) | |
159 | put_spu_gang(ei->i_gang); | |
67207b96 AB |
160 | } |
161 | ||
3f51dd91 | 162 | static void spufs_prune_dir(struct dentry *dir) |
67207b96 | 163 | { |
8b3d6663 | 164 | struct dentry *dentry, *tmp; |
6263203e | 165 | |
5955102c | 166 | inode_lock(d_inode(dir)); |
946e51f2 | 167 | list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_child) { |
67207b96 | 168 | spin_lock(&dentry->d_lock); |
dc3f4198 | 169 | if (simple_positive(dentry)) { |
dc0474be | 170 | dget_dlock(dentry); |
8b3d6663 AB |
171 | __d_drop(dentry); |
172 | spin_unlock(&dentry->d_lock); | |
75c3cfa8 | 173 | simple_unlink(d_inode(dir), dentry); |
b5c84bf6 | 174 | /* XXX: what was dcache_lock protecting here? Other |
da502956 NP |
175 | * filesystems (IB, configfs) release dcache_lock |
176 | * before unlink */ | |
8b3d6663 AB |
177 | dput(dentry); |
178 | } else { | |
179 | spin_unlock(&dentry->d_lock); | |
8b3d6663 | 180 | } |
67207b96 | 181 | } |
3f51dd91 | 182 | shrink_dcache_parent(dir); |
5955102c | 183 | inode_unlock(d_inode(dir)); |
3f51dd91 AB |
184 | } |
185 | ||
6263203e AB |
186 | /* Caller must hold parent->i_mutex */ |
187 | static int spufs_rmdir(struct inode *parent, struct dentry *dir) | |
3f51dd91 | 188 | { |
3f51dd91 | 189 | /* remove all entries */ |
67cba9fd | 190 | int res; |
6263203e | 191 | spufs_prune_dir(dir); |
c443acab | 192 | d_drop(dir); |
67cba9fd AV |
193 | res = simple_rmdir(parent, dir); |
194 | /* We have to give up the mm_struct */ | |
75c3cfa8 | 195 | spu_forget(SPUFS_I(d_inode(dir))->i_ctx); |
67cba9fd | 196 | return res; |
67207b96 AB |
197 | } |
198 | ||
74254647 | 199 | static int spufs_fill_dir(struct dentry *dir, |
c6684b26 | 200 | const struct spufs_tree_descr *files, umode_t mode, |
74254647 | 201 | struct spu_context *ctx) |
3f51dd91 | 202 | { |
3f51dd91 | 203 | while (files->name && files->name[0]) { |
2248b87e AV |
204 | int ret; |
205 | struct dentry *dentry = d_alloc_name(dir, files->name); | |
3f51dd91 | 206 | if (!dentry) |
2248b87e | 207 | return -ENOMEM; |
3f51dd91 | 208 | ret = spufs_new_file(dir->d_sb, dentry, files->ops, |
23d893f5 | 209 | files->mode & mode, files->size, ctx); |
3f51dd91 | 210 | if (ret) |
2248b87e | 211 | return ret; |
3f51dd91 AB |
212 | files++; |
213 | } | |
214 | return 0; | |
3f51dd91 AB |
215 | } |
216 | ||
67207b96 AB |
217 | static int spufs_dir_close(struct inode *inode, struct file *file) |
218 | { | |
0309f02d | 219 | struct spu_context *ctx; |
6263203e AB |
220 | struct inode *parent; |
221 | struct dentry *dir; | |
67207b96 AB |
222 | int ret; |
223 | ||
b4d1ab58 | 224 | dir = file->f_path.dentry; |
75c3cfa8 DH |
225 | parent = d_inode(dir->d_parent); |
226 | ctx = SPUFS_I(d_inode(dir))->i_ctx; | |
c8ca0633 | 227 | |
5955102c | 228 | inode_lock_nested(parent, I_MUTEX_PARENT); |
6263203e | 229 | ret = spufs_rmdir(parent, dir); |
5955102c | 230 | inode_unlock(parent); |
67207b96 | 231 | WARN_ON(ret); |
c8ca0633 | 232 | |
67207b96 AB |
233 | return dcache_dir_close(inode, file); |
234 | } | |
235 | ||
5dfe4c96 | 236 | const struct file_operations spufs_context_fops = { |
67207b96 AB |
237 | .open = dcache_dir_open, |
238 | .release = spufs_dir_close, | |
239 | .llseek = dcache_dir_lseek, | |
240 | .read = generic_read_dir, | |
4e82901c | 241 | .iterate_shared = dcache_readdir, |
1b061d92 | 242 | .fsync = noop_fsync, |
67207b96 | 243 | }; |
bf1ab978 | 244 | EXPORT_SYMBOL_GPL(spufs_context_fops); |
67207b96 AB |
245 | |
246 | static int | |
9add11da | 247 | spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags, |
c6684b26 | 248 | umode_t mode) |
67207b96 AB |
249 | { |
250 | int ret; | |
251 | struct inode *inode; | |
252 | struct spu_context *ctx; | |
253 | ||
67207b96 AB |
254 | inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR); |
255 | if (!inode) | |
2248b87e | 256 | return -ENOSPC; |
67207b96 AB |
257 | |
258 | if (dir->i_mode & S_ISGID) { | |
259 | inode->i_gid = dir->i_gid; | |
260 | inode->i_mode &= S_ISGID; | |
261 | } | |
6263203e | 262 | ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */ |
67207b96 | 263 | SPUFS_I(inode)->i_ctx = ctx; |
2248b87e AV |
264 | if (!ctx) { |
265 | iput(inode); | |
266 | return -ENOSPC; | |
267 | } | |
67207b96 | 268 | |
9add11da | 269 | ctx->flags = flags; |
b8c295f9 | 270 | inode->i_op = &simple_dir_inode_operations; |
67207b96 | 271 | inode->i_fop = &simple_dir_operations; |
2248b87e | 272 | |
5955102c | 273 | inode_lock(inode); |
2248b87e AV |
274 | |
275 | dget(dentry); | |
276 | inc_nlink(dir); | |
277 | inc_nlink(inode); | |
278 | ||
279 | d_instantiate(dentry, inode); | |
280 | ||
5737edd1 MN |
281 | 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 | ||
2248b87e | 287 | if (!ret && spufs_get_sb_info(dir->i_sb)->debug) |
2c3e4787 JK |
288 | ret = spufs_fill_dir(dentry, spufs_dir_debug_contents, |
289 | mode, ctx); | |
290 | ||
291 | if (ret) | |
2248b87e AV |
292 | spufs_rmdir(dir, dentry); |
293 | ||
5955102c | 294 | inode_unlock(inode); |
2c3e4787 | 295 | |
67207b96 AB |
296 | return ret; |
297 | } | |
298 | ||
765927b2 | 299 | static int spufs_context_open(struct path *path) |
346f4d3c AB |
300 | { |
301 | int ret; | |
302 | struct file *filp; | |
303 | ||
6b9cdf39 | 304 | ret = get_unused_fd_flags(0); |
bf349a44 AV |
305 | if (ret < 0) |
306 | return ret; | |
346f4d3c | 307 | |
765927b2 | 308 | filp = dentry_open(path, O_RDONLY, current_cred()); |
346f4d3c AB |
309 | if (IS_ERR(filp)) { |
310 | put_unused_fd(ret); | |
bf349a44 | 311 | return PTR_ERR(filp); |
346f4d3c AB |
312 | } |
313 | ||
314 | filp->f_op = &spufs_context_fops; | |
315 | fd_install(ret, filp); | |
346f4d3c AB |
316 | return ret; |
317 | } | |
318 | ||
8e68e2f2 AB |
319 | static struct spu_context * |
320 | spufs_assert_affinity(unsigned int flags, struct spu_gang *gang, | |
321 | struct file *filp) | |
322 | { | |
58119068 | 323 | struct spu_context *tmp, *neighbor, *err; |
8e68e2f2 AB |
324 | 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( | |
496ad9aa | 350 | SPUFS_I(file_inode(filp))->i_ctx); |
8e68e2f2 AB |
351 | |
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, | |
58119068 AD |
355 | aff_list)->aff_head) { |
356 | err = ERR_PTR(-EEXIST); | |
357 | goto out_put_neighbor; | |
358 | } | |
8e68e2f2 | 359 | |
58119068 AD |
360 | if (gang != neighbor->gang) { |
361 | err = ERR_PTR(-EINVAL); | |
362 | goto out_put_neighbor; | |
363 | } | |
8e68e2f2 AB |
364 | |
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 | ||
58119068 AD |
377 | if (node == MAX_NUMNODES) { |
378 | err = ERR_PTR(-EEXIST); | |
379 | goto out_put_neighbor; | |
380 | } | |
8e68e2f2 AB |
381 | } |
382 | ||
383 | return neighbor; | |
58119068 AD |
384 | |
385 | out_put_neighbor: | |
386 | put_spu_context(neighbor); | |
387 | return err; | |
8e68e2f2 AB |
388 | } |
389 | ||
390 | static void | |
391 | spufs_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 | ||
421 | static int | |
422 | spufs_create_context(struct inode *inode, struct dentry *dentry, | |
c6684b26 | 423 | struct vfsmount *mnt, int flags, umode_t mode, |
8e68e2f2 | 424 | struct file *aff_filp) |
6263203e AB |
425 | { |
426 | int ret; | |
8e68e2f2 AB |
427 | int affinity; |
428 | struct spu_gang *gang; | |
429 | struct spu_context *neighbor; | |
765927b2 | 430 | struct path path = {.mnt = mnt, .dentry = dentry}; |
6263203e | 431 | |
5737edd1 MN |
432 | if ((flags & SPU_CREATE_NOSCHED) && |
433 | !capable(CAP_SYS_NICE)) | |
1ba44cc9 | 434 | return -EPERM; |
5737edd1 | 435 | |
5737edd1 MN |
436 | if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE)) |
437 | == SPU_CREATE_ISOLATE) | |
1ba44cc9 | 438 | return -EINVAL; |
5737edd1 | 439 | |
bd2e5f82 | 440 | if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader) |
1ba44cc9 | 441 | return -ENODEV; |
bd2e5f82 | 442 | |
8e68e2f2 AB |
443 | 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; | |
8e68e2f2 | 448 | if (!gang) |
1ba44cc9 | 449 | return -EINVAL; |
8e68e2f2 AB |
450 | 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 | ||
6263203e AB |
458 | ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO); |
459 | if (ret) | |
8e68e2f2 AB |
460 | goto out_aff_unlock; |
461 | ||
58119068 | 462 | if (affinity) { |
75c3cfa8 | 463 | spufs_set_affinity(flags, SPUFS_I(d_inode(dentry))->i_ctx, |
8e68e2f2 | 464 | neighbor); |
58119068 AD |
465 | if (neighbor) |
466 | put_spu_context(neighbor); | |
467 | } | |
6263203e | 468 | |
765927b2 | 469 | ret = spufs_context_open(&path); |
66ec7b2c | 470 | if (ret < 0) |
6263203e | 471 | WARN_ON(spufs_rmdir(inode, dentry)); |
6263203e | 472 | |
8e68e2f2 AB |
473 | out_aff_unlock: |
474 | if (affinity) | |
475 | mutex_unlock(&gang->aff_mutex); | |
6263203e AB |
476 | return ret; |
477 | } | |
478 | ||
6263203e | 479 | static int |
c6684b26 | 480 | spufs_mkgang(struct inode *dir, struct dentry *dentry, umode_t mode) |
6263203e AB |
481 | { |
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; | |
54a94fcf DC |
499 | if (!gang) { |
500 | ret = -ENOMEM; | |
6263203e | 501 | goto out_iput; |
54a94fcf | 502 | } |
6263203e | 503 | |
b8c295f9 | 504 | inode->i_op = &simple_dir_inode_operations; |
6263203e AB |
505 | inode->i_fop = &simple_dir_operations; |
506 | ||
507 | d_instantiate(dentry, inode); | |
ba0b996d | 508 | inc_nlink(dir); |
75c3cfa8 | 509 | inc_nlink(d_inode(dentry)); |
6263203e AB |
510 | return ret; |
511 | ||
512 | out_iput: | |
513 | iput(inode); | |
514 | out: | |
515 | return ret; | |
516 | } | |
517 | ||
765927b2 | 518 | static int spufs_gang_open(struct path *path) |
6263203e AB |
519 | { |
520 | int ret; | |
521 | struct file *filp; | |
522 | ||
6b9cdf39 | 523 | ret = get_unused_fd_flags(0); |
bf349a44 AV |
524 | if (ret < 0) |
525 | return ret; | |
6263203e | 526 | |
bf349a44 AV |
527 | /* |
528 | * get references for dget and mntget, will be released | |
529 | * in error path of *_open(). | |
530 | */ | |
765927b2 | 531 | filp = dentry_open(path, O_RDONLY, current_cred()); |
6263203e AB |
532 | if (IS_ERR(filp)) { |
533 | put_unused_fd(ret); | |
bf349a44 | 534 | return PTR_ERR(filp); |
6263203e AB |
535 | } |
536 | ||
877907d3 | 537 | filp->f_op = &simple_dir_operations; |
6263203e | 538 | fd_install(ret, filp); |
6263203e AB |
539 | return ret; |
540 | } | |
541 | ||
542 | static int spufs_create_gang(struct inode *inode, | |
543 | struct dentry *dentry, | |
c6684b26 | 544 | struct vfsmount *mnt, umode_t mode) |
6263203e | 545 | { |
765927b2 | 546 | struct path path = {.mnt = mnt, .dentry = dentry}; |
6263203e AB |
547 | int ret; |
548 | ||
549 | ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO); | |
1ba44cc9 AV |
550 | if (!ret) { |
551 | ret = spufs_gang_open(&path); | |
552 | if (ret < 0) { | |
553 | int err = simple_rmdir(inode, dentry); | |
554 | WARN_ON(err); | |
555 | } | |
877907d3 | 556 | } |
6263203e AB |
557 | return ret; |
558 | } | |
559 | ||
560 | ||
346f4d3c AB |
561 | static struct file_system_type spufs_type; |
562 | ||
1ba10681 | 563 | long spufs_create(struct path *path, struct dentry *dentry, |
c6684b26 | 564 | unsigned int flags, umode_t mode, struct file *filp) |
67207b96 | 565 | { |
75c3cfa8 | 566 | struct inode *dir = d_inode(path->dentry); |
67207b96 AB |
567 | int ret; |
568 | ||
6263203e | 569 | /* check if we are on spufs */ |
1ba10681 | 570 | if (path->dentry->d_sb->s_type != &spufs_type) |
25b2692a | 571 | return -EINVAL; |
67207b96 | 572 | |
6263203e | 573 | /* don't accept undefined flags */ |
9add11da | 574 | if (flags & (~SPU_CREATE_FLAG_ALL)) |
25b2692a | 575 | return -EINVAL; |
c9832948 | 576 | |
6263203e | 577 | /* only threads can be underneath a gang */ |
25b2692a AV |
578 | if (path->dentry != path->dentry->d_sb->s_root) |
579 | if ((flags & SPU_CREATE_GANG) || !SPUFS_I(dir)->i_gang) | |
580 | return -EINVAL; | |
6263203e | 581 | |
ce3b0f8d | 582 | mode &= ~current_umask(); |
67207b96 | 583 | |
6263203e | 584 | if (flags & SPU_CREATE_GANG) |
25b2692a | 585 | ret = spufs_create_gang(dir, dentry, path->mnt, mode); |
6263203e | 586 | else |
25b2692a | 587 | ret = spufs_create_context(dir, dentry, path->mnt, flags, mode, |
4ac91378 | 588 | filp); |
826be063 | 589 | if (ret >= 0) |
25b2692a | 590 | fsnotify_mkdir(dir, dentry); |
67207b96 | 591 | |
67207b96 AB |
592 | return ret; |
593 | } | |
594 | ||
595 | /* File system initialization */ | |
596 | enum { | |
2c3e4787 | 597 | Opt_uid, Opt_gid, Opt_mode, Opt_debug, Opt_err, |
67207b96 AB |
598 | }; |
599 | ||
a447c093 | 600 | static const match_table_t spufs_tokens = { |
2c3e4787 JK |
601 | { Opt_uid, "uid=%d" }, |
602 | { Opt_gid, "gid=%d" }, | |
603 | { Opt_mode, "mode=%o" }, | |
604 | { Opt_debug, "debug" }, | |
605 | { Opt_err, NULL }, | |
67207b96 AB |
606 | }; |
607 | ||
608 | static int | |
2c3e4787 | 609 | spufs_parse_options(struct super_block *sb, char *options, struct inode *root) |
67207b96 AB |
610 | { |
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; | |
ed56f34f DE |
625 | root->i_uid = make_kuid(current_user_ns(), option); |
626 | if (!uid_valid(root->i_uid)) | |
627 | return 0; | |
67207b96 AB |
628 | break; |
629 | case Opt_gid: | |
630 | if (match_int(&args[0], &option)) | |
631 | return 0; | |
ed56f34f DE |
632 | root->i_gid = make_kgid(current_user_ns(), option); |
633 | if (!gid_valid(root->i_gid)) | |
634 | return 0; | |
67207b96 | 635 | break; |
f11f5ee7 JK |
636 | case Opt_mode: |
637 | if (match_octal(&args[0], &option)) | |
638 | return 0; | |
639 | root->i_mode = option | S_IFDIR; | |
640 | break; | |
2c3e4787 JK |
641 | case Opt_debug: |
642 | spufs_get_sb_info(sb)->debug = 1; | |
643 | break; | |
67207b96 AB |
644 | default: |
645 | return 0; | |
646 | } | |
647 | } | |
648 | return 1; | |
649 | } | |
650 | ||
db1384b4 AM |
651 | static void spufs_exit_isolated_loader(void) |
652 | { | |
8b0d3121 SS |
653 | free_pages((unsigned long) isolated_loader, |
654 | get_order(isolated_loader_size)); | |
db1384b4 AM |
655 | } |
656 | ||
0afacde3 AB |
657 | static void |
658 | spufs_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 | ||
e2eb6392 | 668 | loader = of_get_property(dn, "loader", &size); |
0afacde3 AB |
669 | if (!loader) |
670 | return; | |
671 | ||
8b0d3121 SS |
672 | /* the loader must be align on a 16 byte boundary */ |
673 | isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size)); | |
0afacde3 AB |
674 | if (!isolated_loader) |
675 | return; | |
676 | ||
8b0d3121 | 677 | isolated_loader_size = size; |
0afacde3 AB |
678 | memcpy(isolated_loader, loader, size); |
679 | printk(KERN_INFO "spufs: SPU isolation mode enabled\n"); | |
680 | } | |
681 | ||
67207b96 | 682 | static int |
8b3d6663 AB |
683 | spufs_create_root(struct super_block *sb, void *data) |
684 | { | |
67207b96 AB |
685 | struct inode *inode; |
686 | int ret; | |
687 | ||
8f18a158 AB |
688 | ret = -ENODEV; |
689 | if (!spu_management_ops) | |
690 | goto out; | |
691 | ||
67207b96 AB |
692 | ret = -ENOMEM; |
693 | inode = spufs_new_inode(sb, S_IFDIR | 0775); | |
694 | if (!inode) | |
695 | goto out; | |
696 | ||
b8c295f9 | 697 | inode->i_op = &simple_dir_inode_operations; |
67207b96 AB |
698 | inode->i_fop = &simple_dir_operations; |
699 | SPUFS_I(inode)->i_ctx = NULL; | |
e2ed6e4d | 700 | inc_nlink(inode); |
67207b96 AB |
701 | |
702 | ret = -EINVAL; | |
2c3e4787 | 703 | if (!spufs_parse_options(sb, data, inode)) |
67207b96 AB |
704 | goto out_iput; |
705 | ||
706 | ret = -ENOMEM; | |
48fde701 | 707 | sb->s_root = d_make_root(inode); |
67207b96 | 708 | if (!sb->s_root) |
48fde701 | 709 | goto out; |
67207b96 AB |
710 | |
711 | return 0; | |
712 | out_iput: | |
713 | iput(inode); | |
714 | out: | |
715 | return ret; | |
716 | } | |
717 | ||
718 | static int | |
719 | spufs_fill_super(struct super_block *sb, void *data, int silent) | |
720 | { | |
2c3e4787 | 721 | struct spufs_sb_info *info; |
b87221de | 722 | static const struct super_operations s_ops = { |
67207b96 AB |
723 | .alloc_inode = spufs_alloc_inode, |
724 | .destroy_inode = spufs_destroy_inode, | |
725 | .statfs = simple_statfs, | |
0f3f63a4 | 726 | .evict_inode = spufs_evict_inode, |
90d09e14 | 727 | .show_options = generic_show_options, |
67207b96 AB |
728 | }; |
729 | ||
90d09e14 MS |
730 | save_mount_options(sb, data); |
731 | ||
2c3e4787 JK |
732 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
733 | if (!info) | |
734 | return -ENOMEM; | |
735 | ||
67207b96 | 736 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
09cbfeaf KS |
737 | sb->s_blocksize = PAGE_SIZE; |
738 | sb->s_blocksize_bits = PAGE_SHIFT; | |
67207b96 AB |
739 | sb->s_magic = SPUFS_MAGIC; |
740 | sb->s_op = &s_ops; | |
2c3e4787 | 741 | sb->s_fs_info = info; |
67207b96 AB |
742 | |
743 | return spufs_create_root(sb, data); | |
744 | } | |
745 | ||
fc14f2fe AV |
746 | static struct dentry * |
747 | spufs_mount(struct file_system_type *fstype, int flags, | |
748 | const char *name, void *data) | |
67207b96 | 749 | { |
fc14f2fe | 750 | return mount_single(fstype, flags, data, spufs_fill_super); |
67207b96 AB |
751 | } |
752 | ||
753 | static struct file_system_type spufs_type = { | |
754 | .owner = THIS_MODULE, | |
755 | .name = "spufs", | |
fc14f2fe | 756 | .mount = spufs_mount, |
67207b96 AB |
757 | .kill_sb = kill_litter_super, |
758 | }; | |
7f78e035 | 759 | MODULE_ALIAS_FS("spufs"); |
67207b96 | 760 | |
e78b47a5 | 761 | static int __init spufs_init(void) |
67207b96 AB |
762 | { |
763 | int ret; | |
bf1ab978 | 764 | |
ccf17e9d JK |
765 | ret = -ENODEV; |
766 | if (!spu_management_ops) | |
767 | goto out; | |
768 | ||
67207b96 AB |
769 | ret = -ENOMEM; |
770 | spufs_inode_cache = kmem_cache_create("spufs_inode_cache", | |
771 | sizeof(struct spufs_inode_info), 0, | |
5d097056 | 772 | SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT, spufs_init_once); |
67207b96 AB |
773 | |
774 | if (!spufs_inode_cache) | |
775 | goto out; | |
c99c1994 | 776 | ret = spu_sched_init(); |
67207b96 AB |
777 | if (ret) |
778 | goto out_cache; | |
640045a1 | 779 | ret = register_spu_syscalls(&spufs_calls); |
c99c1994 AM |
780 | if (ret) |
781 | goto out_sched; | |
640045a1 | 782 | ret = register_filesystem(&spufs_type); |
bf1ab978 | 783 | if (ret) |
640045a1 | 784 | goto out_syscalls; |
0afacde3 AB |
785 | |
786 | spufs_init_isolated_loader(); | |
bf1ab978 | 787 | |
67207b96 | 788 | return 0; |
c99c1994 | 789 | |
640045a1 AV |
790 | out_syscalls: |
791 | unregister_spu_syscalls(&spufs_calls); | |
c99c1994 AM |
792 | out_sched: |
793 | spu_sched_exit(); | |
67207b96 AB |
794 | out_cache: |
795 | kmem_cache_destroy(spufs_inode_cache); | |
796 | out: | |
797 | return ret; | |
798 | } | |
799 | module_init(spufs_init); | |
800 | ||
e78b47a5 | 801 | static void __exit spufs_exit(void) |
67207b96 | 802 | { |
8b3d6663 | 803 | spu_sched_exit(); |
db1384b4 | 804 | spufs_exit_isolated_loader(); |
67207b96 AB |
805 | unregister_spu_syscalls(&spufs_calls); |
806 | unregister_filesystem(&spufs_type); | |
807 | kmem_cache_destroy(spufs_inode_cache); | |
808 | } | |
809 | module_exit(spufs_exit); | |
810 | ||
811 | MODULE_LICENSE("GPL"); | |
812 | MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>"); | |
813 |