]>
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 | ||
e18b890b | 45 | static struct kmem_cache *spufs_inode_cache; |
c6730ed4 | 46 | char *isolated_loader; |
8b0d3121 | 47 | static int isolated_loader_size; |
67207b96 | 48 | |
67207b96 AB |
49 | static struct inode * |
50 | spufs_alloc_inode(struct super_block *sb) | |
51 | { | |
52 | struct spufs_inode_info *ei; | |
53 | ||
e94b1766 | 54 | ei = kmem_cache_alloc(spufs_inode_cache, GFP_KERNEL); |
67207b96 AB |
55 | if (!ei) |
56 | return NULL; | |
6263203e AB |
57 | |
58 | ei->i_gang = NULL; | |
59 | ei->i_ctx = NULL; | |
43c2bbd9 | 60 | ei->i_openers = 0; |
6263203e | 61 | |
67207b96 AB |
62 | return &ei->vfs_inode; |
63 | } | |
64 | ||
65 | static void | |
66 | spufs_destroy_inode(struct inode *inode) | |
67 | { | |
68 | kmem_cache_free(spufs_inode_cache, SPUFS_I(inode)); | |
69 | } | |
70 | ||
71 | static void | |
4ba9b9d0 | 72 | spufs_init_once(struct kmem_cache *cachep, void *p) |
67207b96 AB |
73 | { |
74 | struct spufs_inode_info *ei = p; | |
75 | ||
a35afb83 | 76 | inode_init_once(&ei->vfs_inode); |
67207b96 AB |
77 | } |
78 | ||
79 | static struct inode * | |
80 | spufs_new_inode(struct super_block *sb, int mode) | |
81 | { | |
82 | struct inode *inode; | |
83 | ||
84 | inode = new_inode(sb); | |
85 | if (!inode) | |
86 | goto out; | |
87 | ||
88 | inode->i_mode = mode; | |
89 | inode->i_uid = current->fsuid; | |
90 | inode->i_gid = current->fsgid; | |
67207b96 AB |
91 | inode->i_blocks = 0; |
92 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; | |
93 | out: | |
94 | return inode; | |
95 | } | |
96 | ||
97 | static int | |
98 | spufs_setattr(struct dentry *dentry, struct iattr *attr) | |
99 | { | |
100 | struct inode *inode = dentry->d_inode; | |
101 | ||
67207b96 AB |
102 | if ((attr->ia_valid & ATTR_SIZE) && |
103 | (attr->ia_size != inode->i_size)) | |
104 | return -EINVAL; | |
105 | return inode_setattr(inode, attr); | |
106 | } | |
107 | ||
108 | ||
109 | static int | |
110 | spufs_new_file(struct super_block *sb, struct dentry *dentry, | |
99ac48f5 | 111 | const struct file_operations *fops, int mode, |
23d893f5 | 112 | size_t size, struct spu_context *ctx) |
67207b96 AB |
113 | { |
114 | static struct inode_operations spufs_file_iops = { | |
67207b96 | 115 | .setattr = spufs_setattr, |
67207b96 AB |
116 | }; |
117 | struct inode *inode; | |
118 | int ret; | |
119 | ||
120 | ret = -ENOSPC; | |
121 | inode = spufs_new_inode(sb, S_IFREG | mode); | |
122 | if (!inode) | |
123 | goto out; | |
124 | ||
125 | ret = 0; | |
126 | inode->i_op = &spufs_file_iops; | |
127 | inode->i_fop = fops; | |
23d893f5 | 128 | inode->i_size = size; |
8e18e294 | 129 | inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx); |
67207b96 AB |
130 | d_add(dentry, inode); |
131 | out: | |
132 | return ret; | |
133 | } | |
134 | ||
135 | static void | |
136 | spufs_delete_inode(struct inode *inode) | |
137 | { | |
6263203e AB |
138 | struct spufs_inode_info *ei = SPUFS_I(inode); |
139 | ||
140 | if (ei->i_ctx) | |
141 | put_spu_context(ei->i_ctx); | |
142 | if (ei->i_gang) | |
143 | put_spu_gang(ei->i_gang); | |
67207b96 AB |
144 | clear_inode(inode); |
145 | } | |
146 | ||
3f51dd91 | 147 | static void spufs_prune_dir(struct dentry *dir) |
67207b96 | 148 | { |
8b3d6663 | 149 | struct dentry *dentry, *tmp; |
6263203e | 150 | |
1b1dcc1b | 151 | mutex_lock(&dir->d_inode->i_mutex); |
c3a9aea7 | 152 | list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) { |
8b3d6663 | 153 | spin_lock(&dcache_lock); |
67207b96 | 154 | spin_lock(&dentry->d_lock); |
8b3d6663 AB |
155 | if (!(d_unhashed(dentry)) && dentry->d_inode) { |
156 | dget_locked(dentry); | |
157 | __d_drop(dentry); | |
158 | spin_unlock(&dentry->d_lock); | |
3f51dd91 | 159 | simple_unlink(dir->d_inode, dentry); |
8b3d6663 AB |
160 | spin_unlock(&dcache_lock); |
161 | dput(dentry); | |
162 | } else { | |
163 | spin_unlock(&dentry->d_lock); | |
164 | spin_unlock(&dcache_lock); | |
165 | } | |
67207b96 | 166 | } |
3f51dd91 | 167 | shrink_dcache_parent(dir); |
1b1dcc1b | 168 | mutex_unlock(&dir->d_inode->i_mutex); |
3f51dd91 AB |
169 | } |
170 | ||
6263203e AB |
171 | /* Caller must hold parent->i_mutex */ |
172 | static int spufs_rmdir(struct inode *parent, struct dentry *dir) | |
3f51dd91 | 173 | { |
3f51dd91 | 174 | /* remove all entries */ |
6263203e | 175 | spufs_prune_dir(dir); |
c443acab | 176 | d_drop(dir); |
8b3d6663 | 177 | |
6263203e | 178 | return simple_rmdir(parent, dir); |
67207b96 AB |
179 | } |
180 | ||
23d893f5 | 181 | static int spufs_fill_dir(struct dentry *dir, struct spufs_tree_descr *files, |
3f51dd91 AB |
182 | int mode, struct spu_context *ctx) |
183 | { | |
87873c86 | 184 | struct dentry *dentry, *tmp; |
3f51dd91 AB |
185 | int ret; |
186 | ||
187 | while (files->name && files->name[0]) { | |
188 | ret = -ENOMEM; | |
189 | dentry = d_alloc_name(dir, files->name); | |
190 | if (!dentry) | |
191 | goto out; | |
192 | ret = spufs_new_file(dir->d_sb, dentry, files->ops, | |
23d893f5 | 193 | files->mode & mode, files->size, ctx); |
3f51dd91 AB |
194 | if (ret) |
195 | goto out; | |
196 | files++; | |
197 | } | |
198 | return 0; | |
199 | out: | |
87873c86 SS |
200 | /* |
201 | * remove all children from dir. dir->inode is not set so don't | |
202 | * just simply use spufs_prune_dir() and panic afterwards :) | |
203 | * dput() looks like it will do the right thing: | |
204 | * - dec parent's ref counter | |
205 | * - remove child from parent's child list | |
206 | * - free child's inode if possible | |
207 | * - free child | |
208 | */ | |
209 | list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) { | |
210 | dput(dentry); | |
211 | } | |
212 | ||
213 | shrink_dcache_parent(dir); | |
3f51dd91 AB |
214 | return ret; |
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; |
6263203e AB |
225 | parent = dir->d_parent->d_inode; |
226 | ctx = SPUFS_I(dir->d_inode)->i_ctx; | |
c8ca0633 | 227 | |
02539d71 | 228 | mutex_lock_nested(&parent->i_mutex, I_MUTEX_PARENT); |
6263203e AB |
229 | ret = spufs_rmdir(parent, dir); |
230 | mutex_unlock(&parent->i_mutex); | |
67207b96 | 231 | WARN_ON(ret); |
c8ca0633 | 232 | |
0309f02d ME |
233 | /* We have to give up the mm_struct */ |
234 | spu_forget(ctx); | |
235 | ||
67207b96 AB |
236 | return dcache_dir_close(inode, file); |
237 | } | |
238 | ||
5dfe4c96 | 239 | const struct file_operations spufs_context_fops = { |
67207b96 AB |
240 | .open = dcache_dir_open, |
241 | .release = spufs_dir_close, | |
242 | .llseek = dcache_dir_lseek, | |
243 | .read = generic_read_dir, | |
244 | .readdir = dcache_readdir, | |
245 | .fsync = simple_sync_file, | |
246 | }; | |
bf1ab978 | 247 | EXPORT_SYMBOL_GPL(spufs_context_fops); |
67207b96 AB |
248 | |
249 | static int | |
9add11da AB |
250 | spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags, |
251 | int mode) | |
67207b96 AB |
252 | { |
253 | int ret; | |
254 | struct inode *inode; | |
255 | struct spu_context *ctx; | |
256 | ||
257 | ret = -ENOSPC; | |
258 | inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR); | |
259 | if (!inode) | |
260 | goto out; | |
261 | ||
262 | if (dir->i_mode & S_ISGID) { | |
263 | inode->i_gid = dir->i_gid; | |
264 | inode->i_mode &= S_ISGID; | |
265 | } | |
6263203e | 266 | ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */ |
67207b96 AB |
267 | SPUFS_I(inode)->i_ctx = ctx; |
268 | if (!ctx) | |
269 | goto out_iput; | |
270 | ||
9add11da | 271 | ctx->flags = flags; |
b8c295f9 | 272 | inode->i_op = &simple_dir_inode_operations; |
67207b96 | 273 | inode->i_fop = &simple_dir_operations; |
5737edd1 MN |
274 | if (flags & SPU_CREATE_NOSCHED) |
275 | ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents, | |
276 | mode, ctx); | |
277 | else | |
278 | ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx); | |
279 | ||
67207b96 AB |
280 | if (ret) |
281 | goto out_free_ctx; | |
282 | ||
283 | d_instantiate(dentry, inode); | |
284 | dget(dentry); | |
285 | dir->i_nlink++; | |
346f4d3c | 286 | dentry->d_inode->i_nlink++; |
67207b96 AB |
287 | goto out; |
288 | ||
289 | out_free_ctx: | |
89df0085 | 290 | spu_forget(ctx); |
67207b96 AB |
291 | put_spu_context(ctx); |
292 | out_iput: | |
293 | iput(inode); | |
294 | out: | |
295 | return ret; | |
296 | } | |
297 | ||
346f4d3c AB |
298 | static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt) |
299 | { | |
300 | int ret; | |
301 | struct file *filp; | |
302 | ||
303 | ret = get_unused_fd(); | |
304 | if (ret < 0) { | |
305 | dput(dentry); | |
306 | mntput(mnt); | |
307 | goto out; | |
308 | } | |
309 | ||
310 | filp = dentry_open(dentry, mnt, O_RDONLY); | |
311 | if (IS_ERR(filp)) { | |
312 | put_unused_fd(ret); | |
313 | ret = PTR_ERR(filp); | |
314 | goto out; | |
315 | } | |
316 | ||
317 | filp->f_op = &spufs_context_fops; | |
318 | fd_install(ret, filp); | |
319 | out: | |
320 | return ret; | |
321 | } | |
322 | ||
8e68e2f2 AB |
323 | static struct spu_context * |
324 | spufs_assert_affinity(unsigned int flags, struct spu_gang *gang, | |
325 | struct file *filp) | |
326 | { | |
58119068 | 327 | struct spu_context *tmp, *neighbor, *err; |
8e68e2f2 AB |
328 | int count, node; |
329 | int aff_supp; | |
330 | ||
331 | aff_supp = !list_empty(&(list_entry(cbe_spu_info[0].spus.next, | |
332 | struct spu, cbe_list))->aff_list); | |
333 | ||
334 | if (!aff_supp) | |
335 | return ERR_PTR(-EINVAL); | |
336 | ||
337 | if (flags & SPU_CREATE_GANG) | |
338 | return ERR_PTR(-EINVAL); | |
339 | ||
340 | if (flags & SPU_CREATE_AFFINITY_MEM && | |
341 | gang->aff_ref_ctx && | |
342 | gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM) | |
343 | return ERR_PTR(-EEXIST); | |
344 | ||
345 | if (gang->aff_flags & AFF_MERGED) | |
346 | return ERR_PTR(-EBUSY); | |
347 | ||
348 | neighbor = NULL; | |
349 | if (flags & SPU_CREATE_AFFINITY_SPU) { | |
350 | if (!filp || filp->f_op != &spufs_context_fops) | |
351 | return ERR_PTR(-EINVAL); | |
352 | ||
353 | neighbor = get_spu_context( | |
354 | SPUFS_I(filp->f_dentry->d_inode)->i_ctx); | |
355 | ||
356 | if (!list_empty(&neighbor->aff_list) && !(neighbor->aff_head) && | |
357 | !list_is_last(&neighbor->aff_list, &gang->aff_list_head) && | |
358 | !list_entry(neighbor->aff_list.next, struct spu_context, | |
58119068 AD |
359 | aff_list)->aff_head) { |
360 | err = ERR_PTR(-EEXIST); | |
361 | goto out_put_neighbor; | |
362 | } | |
8e68e2f2 | 363 | |
58119068 AD |
364 | if (gang != neighbor->gang) { |
365 | err = ERR_PTR(-EINVAL); | |
366 | goto out_put_neighbor; | |
367 | } | |
8e68e2f2 AB |
368 | |
369 | count = 1; | |
370 | list_for_each_entry(tmp, &gang->aff_list_head, aff_list) | |
371 | count++; | |
372 | if (list_empty(&neighbor->aff_list)) | |
373 | count++; | |
374 | ||
375 | for (node = 0; node < MAX_NUMNODES; node++) { | |
376 | if ((cbe_spu_info[node].n_spus - atomic_read( | |
377 | &cbe_spu_info[node].reserved_spus)) >= count) | |
378 | break; | |
379 | } | |
380 | ||
58119068 AD |
381 | if (node == MAX_NUMNODES) { |
382 | err = ERR_PTR(-EEXIST); | |
383 | goto out_put_neighbor; | |
384 | } | |
8e68e2f2 AB |
385 | } |
386 | ||
387 | return neighbor; | |
58119068 AD |
388 | |
389 | out_put_neighbor: | |
390 | put_spu_context(neighbor); | |
391 | return err; | |
8e68e2f2 AB |
392 | } |
393 | ||
394 | static void | |
395 | spufs_set_affinity(unsigned int flags, struct spu_context *ctx, | |
396 | struct spu_context *neighbor) | |
397 | { | |
398 | if (flags & SPU_CREATE_AFFINITY_MEM) | |
399 | ctx->gang->aff_ref_ctx = ctx; | |
400 | ||
401 | if (flags & SPU_CREATE_AFFINITY_SPU) { | |
402 | if (list_empty(&neighbor->aff_list)) { | |
403 | list_add_tail(&neighbor->aff_list, | |
404 | &ctx->gang->aff_list_head); | |
405 | neighbor->aff_head = 1; | |
406 | } | |
407 | ||
408 | if (list_is_last(&neighbor->aff_list, &ctx->gang->aff_list_head) | |
409 | || list_entry(neighbor->aff_list.next, struct spu_context, | |
410 | aff_list)->aff_head) { | |
411 | list_add(&ctx->aff_list, &neighbor->aff_list); | |
412 | } else { | |
413 | list_add_tail(&ctx->aff_list, &neighbor->aff_list); | |
414 | if (neighbor->aff_head) { | |
415 | neighbor->aff_head = 0; | |
416 | ctx->aff_head = 1; | |
417 | } | |
418 | } | |
419 | ||
420 | if (!ctx->gang->aff_ref_ctx) | |
421 | ctx->gang->aff_ref_ctx = ctx; | |
422 | } | |
423 | } | |
424 | ||
425 | static int | |
426 | spufs_create_context(struct inode *inode, struct dentry *dentry, | |
427 | struct vfsmount *mnt, int flags, int mode, | |
428 | struct file *aff_filp) | |
6263203e AB |
429 | { |
430 | int ret; | |
8e68e2f2 AB |
431 | int affinity; |
432 | struct spu_gang *gang; | |
433 | struct spu_context *neighbor; | |
6263203e | 434 | |
5737edd1 MN |
435 | ret = -EPERM; |
436 | if ((flags & SPU_CREATE_NOSCHED) && | |
437 | !capable(CAP_SYS_NICE)) | |
438 | goto out_unlock; | |
439 | ||
440 | ret = -EINVAL; | |
441 | if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE)) | |
442 | == SPU_CREATE_ISOLATE) | |
443 | goto out_unlock; | |
444 | ||
bd2e5f82 JK |
445 | ret = -ENODEV; |
446 | if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader) | |
447 | goto out_unlock; | |
448 | ||
8e68e2f2 AB |
449 | gang = NULL; |
450 | neighbor = NULL; | |
451 | affinity = flags & (SPU_CREATE_AFFINITY_MEM | SPU_CREATE_AFFINITY_SPU); | |
452 | if (affinity) { | |
453 | gang = SPUFS_I(inode)->i_gang; | |
454 | ret = -EINVAL; | |
455 | if (!gang) | |
456 | goto out_unlock; | |
457 | mutex_lock(&gang->aff_mutex); | |
458 | neighbor = spufs_assert_affinity(flags, gang, aff_filp); | |
459 | if (IS_ERR(neighbor)) { | |
460 | ret = PTR_ERR(neighbor); | |
461 | goto out_aff_unlock; | |
462 | } | |
463 | } | |
464 | ||
6263203e AB |
465 | ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO); |
466 | if (ret) | |
8e68e2f2 AB |
467 | goto out_aff_unlock; |
468 | ||
58119068 | 469 | if (affinity) { |
8e68e2f2 AB |
470 | spufs_set_affinity(flags, SPUFS_I(dentry->d_inode)->i_ctx, |
471 | neighbor); | |
58119068 AD |
472 | if (neighbor) |
473 | put_spu_context(neighbor); | |
474 | } | |
6263203e AB |
475 | |
476 | /* | |
477 | * get references for dget and mntget, will be released | |
478 | * in error path of *_open(). | |
479 | */ | |
480 | ret = spufs_context_open(dget(dentry), mntget(mnt)); | |
481 | if (ret < 0) { | |
482 | WARN_ON(spufs_rmdir(inode, dentry)); | |
483 | mutex_unlock(&inode->i_mutex); | |
484 | spu_forget(SPUFS_I(dentry->d_inode)->i_ctx); | |
485 | goto out; | |
486 | } | |
487 | ||
8e68e2f2 AB |
488 | out_aff_unlock: |
489 | if (affinity) | |
490 | mutex_unlock(&gang->aff_mutex); | |
6263203e AB |
491 | out_unlock: |
492 | mutex_unlock(&inode->i_mutex); | |
493 | out: | |
494 | dput(dentry); | |
495 | return ret; | |
496 | } | |
497 | ||
6263203e AB |
498 | static int |
499 | spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode) | |
500 | { | |
501 | int ret; | |
502 | struct inode *inode; | |
503 | struct spu_gang *gang; | |
504 | ||
505 | ret = -ENOSPC; | |
506 | inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR); | |
507 | if (!inode) | |
508 | goto out; | |
509 | ||
510 | ret = 0; | |
511 | if (dir->i_mode & S_ISGID) { | |
512 | inode->i_gid = dir->i_gid; | |
513 | inode->i_mode &= S_ISGID; | |
514 | } | |
515 | gang = alloc_spu_gang(); | |
516 | SPUFS_I(inode)->i_ctx = NULL; | |
517 | SPUFS_I(inode)->i_gang = gang; | |
518 | if (!gang) | |
519 | goto out_iput; | |
520 | ||
b8c295f9 | 521 | inode->i_op = &simple_dir_inode_operations; |
6263203e AB |
522 | inode->i_fop = &simple_dir_operations; |
523 | ||
524 | d_instantiate(dentry, inode); | |
6263203e AB |
525 | dir->i_nlink++; |
526 | dentry->d_inode->i_nlink++; | |
527 | return ret; | |
528 | ||
529 | out_iput: | |
530 | iput(inode); | |
531 | out: | |
532 | return ret; | |
533 | } | |
534 | ||
535 | static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt) | |
536 | { | |
537 | int ret; | |
538 | struct file *filp; | |
539 | ||
540 | ret = get_unused_fd(); | |
541 | if (ret < 0) { | |
542 | dput(dentry); | |
543 | mntput(mnt); | |
544 | goto out; | |
545 | } | |
546 | ||
547 | filp = dentry_open(dentry, mnt, O_RDONLY); | |
548 | if (IS_ERR(filp)) { | |
549 | put_unused_fd(ret); | |
550 | ret = PTR_ERR(filp); | |
551 | goto out; | |
552 | } | |
553 | ||
877907d3 | 554 | filp->f_op = &simple_dir_operations; |
6263203e AB |
555 | fd_install(ret, filp); |
556 | out: | |
557 | return ret; | |
558 | } | |
559 | ||
560 | static int spufs_create_gang(struct inode *inode, | |
561 | struct dentry *dentry, | |
562 | struct vfsmount *mnt, int mode) | |
563 | { | |
564 | int ret; | |
565 | ||
566 | ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO); | |
567 | if (ret) | |
568 | goto out; | |
569 | ||
570 | /* | |
571 | * get references for dget and mntget, will be released | |
572 | * in error path of *_open(). | |
573 | */ | |
574 | ret = spufs_gang_open(dget(dentry), mntget(mnt)); | |
877907d3 JK |
575 | if (ret < 0) { |
576 | int err = simple_rmdir(inode, dentry); | |
577 | WARN_ON(err); | |
578 | } | |
6263203e AB |
579 | |
580 | out: | |
581 | mutex_unlock(&inode->i_mutex); | |
582 | dput(dentry); | |
583 | return ret; | |
584 | } | |
585 | ||
586 | ||
346f4d3c AB |
587 | static struct file_system_type spufs_type; |
588 | ||
8e68e2f2 AB |
589 | long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode, |
590 | struct file *filp) | |
67207b96 AB |
591 | { |
592 | struct dentry *dentry; | |
67207b96 AB |
593 | int ret; |
594 | ||
67207b96 | 595 | ret = -EINVAL; |
6263203e | 596 | /* check if we are on spufs */ |
4ac91378 | 597 | if (nd->path.dentry->d_sb->s_type != &spufs_type) |
67207b96 AB |
598 | goto out; |
599 | ||
6263203e | 600 | /* don't accept undefined flags */ |
9add11da | 601 | if (flags & (~SPU_CREATE_FLAG_ALL)) |
c9832948 AB |
602 | goto out; |
603 | ||
6263203e | 604 | /* only threads can be underneath a gang */ |
4ac91378 | 605 | if (nd->path.dentry != nd->path.dentry->d_sb->s_root) { |
6263203e | 606 | if ((flags & SPU_CREATE_GANG) || |
4ac91378 | 607 | !SPUFS_I(nd->path.dentry->d_inode)->i_gang) |
6263203e AB |
608 | goto out; |
609 | } | |
610 | ||
67207b96 AB |
611 | dentry = lookup_create(nd, 1); |
612 | ret = PTR_ERR(dentry); | |
613 | if (IS_ERR(dentry)) | |
614 | goto out_dir; | |
615 | ||
616 | ret = -EEXIST; | |
617 | if (dentry->d_inode) | |
618 | goto out_dput; | |
619 | ||
620 | mode &= ~current->fs->umask; | |
67207b96 | 621 | |
6263203e | 622 | if (flags & SPU_CREATE_GANG) |
826be063 | 623 | ret = spufs_create_gang(nd->path.dentry->d_inode, |
4ac91378 | 624 | dentry, nd->path.mnt, mode); |
6263203e | 625 | else |
826be063 | 626 | ret = spufs_create_context(nd->path.dentry->d_inode, |
4ac91378 JB |
627 | dentry, nd->path.mnt, flags, mode, |
628 | filp); | |
826be063 CH |
629 | if (ret >= 0) |
630 | fsnotify_mkdir(nd->path.dentry->d_inode, dentry); | |
631 | return ret; | |
67207b96 AB |
632 | |
633 | out_dput: | |
634 | dput(dentry); | |
635 | out_dir: | |
4ac91378 | 636 | mutex_unlock(&nd->path.dentry->d_inode->i_mutex); |
67207b96 AB |
637 | out: |
638 | return ret; | |
639 | } | |
640 | ||
641 | /* File system initialization */ | |
642 | enum { | |
f11f5ee7 | 643 | Opt_uid, Opt_gid, Opt_mode, Opt_err, |
67207b96 AB |
644 | }; |
645 | ||
646 | static match_table_t spufs_tokens = { | |
f11f5ee7 JK |
647 | { Opt_uid, "uid=%d" }, |
648 | { Opt_gid, "gid=%d" }, | |
649 | { Opt_mode, "mode=%o" }, | |
650 | { Opt_err, NULL }, | |
67207b96 AB |
651 | }; |
652 | ||
653 | static int | |
654 | spufs_parse_options(char *options, struct inode *root) | |
655 | { | |
656 | char *p; | |
657 | substring_t args[MAX_OPT_ARGS]; | |
658 | ||
659 | while ((p = strsep(&options, ",")) != NULL) { | |
660 | int token, option; | |
661 | ||
662 | if (!*p) | |
663 | continue; | |
664 | ||
665 | token = match_token(p, spufs_tokens, args); | |
666 | switch (token) { | |
667 | case Opt_uid: | |
668 | if (match_int(&args[0], &option)) | |
669 | return 0; | |
670 | root->i_uid = option; | |
671 | break; | |
672 | case Opt_gid: | |
673 | if (match_int(&args[0], &option)) | |
674 | return 0; | |
675 | root->i_gid = option; | |
676 | break; | |
f11f5ee7 JK |
677 | case Opt_mode: |
678 | if (match_octal(&args[0], &option)) | |
679 | return 0; | |
680 | root->i_mode = option | S_IFDIR; | |
681 | break; | |
67207b96 AB |
682 | default: |
683 | return 0; | |
684 | } | |
685 | } | |
686 | return 1; | |
687 | } | |
688 | ||
db1384b4 AM |
689 | static void spufs_exit_isolated_loader(void) |
690 | { | |
8b0d3121 SS |
691 | free_pages((unsigned long) isolated_loader, |
692 | get_order(isolated_loader_size)); | |
db1384b4 AM |
693 | } |
694 | ||
0afacde3 AB |
695 | static void |
696 | spufs_init_isolated_loader(void) | |
697 | { | |
698 | struct device_node *dn; | |
699 | const char *loader; | |
700 | int size; | |
701 | ||
702 | dn = of_find_node_by_path("/spu-isolation"); | |
703 | if (!dn) | |
704 | return; | |
705 | ||
e2eb6392 | 706 | loader = of_get_property(dn, "loader", &size); |
0afacde3 AB |
707 | if (!loader) |
708 | return; | |
709 | ||
8b0d3121 SS |
710 | /* the loader must be align on a 16 byte boundary */ |
711 | isolated_loader = (char *)__get_free_pages(GFP_KERNEL, get_order(size)); | |
0afacde3 AB |
712 | if (!isolated_loader) |
713 | return; | |
714 | ||
8b0d3121 | 715 | isolated_loader_size = size; |
0afacde3 AB |
716 | memcpy(isolated_loader, loader, size); |
717 | printk(KERN_INFO "spufs: SPU isolation mode enabled\n"); | |
718 | } | |
719 | ||
67207b96 | 720 | static int |
8b3d6663 AB |
721 | spufs_create_root(struct super_block *sb, void *data) |
722 | { | |
67207b96 AB |
723 | struct inode *inode; |
724 | int ret; | |
725 | ||
8f18a158 AB |
726 | ret = -ENODEV; |
727 | if (!spu_management_ops) | |
728 | goto out; | |
729 | ||
67207b96 AB |
730 | ret = -ENOMEM; |
731 | inode = spufs_new_inode(sb, S_IFDIR | 0775); | |
732 | if (!inode) | |
733 | goto out; | |
734 | ||
b8c295f9 | 735 | inode->i_op = &simple_dir_inode_operations; |
67207b96 AB |
736 | inode->i_fop = &simple_dir_operations; |
737 | SPUFS_I(inode)->i_ctx = NULL; | |
738 | ||
739 | ret = -EINVAL; | |
740 | if (!spufs_parse_options(data, inode)) | |
741 | goto out_iput; | |
742 | ||
743 | ret = -ENOMEM; | |
744 | sb->s_root = d_alloc_root(inode); | |
745 | if (!sb->s_root) | |
746 | goto out_iput; | |
747 | ||
748 | return 0; | |
749 | out_iput: | |
750 | iput(inode); | |
751 | out: | |
752 | return ret; | |
753 | } | |
754 | ||
755 | static int | |
756 | spufs_fill_super(struct super_block *sb, void *data, int silent) | |
757 | { | |
758 | static struct super_operations s_ops = { | |
759 | .alloc_inode = spufs_alloc_inode, | |
760 | .destroy_inode = spufs_destroy_inode, | |
761 | .statfs = simple_statfs, | |
762 | .delete_inode = spufs_delete_inode, | |
763 | .drop_inode = generic_delete_inode, | |
90d09e14 | 764 | .show_options = generic_show_options, |
67207b96 AB |
765 | }; |
766 | ||
90d09e14 MS |
767 | save_mount_options(sb, data); |
768 | ||
67207b96 AB |
769 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
770 | sb->s_blocksize = PAGE_CACHE_SIZE; | |
771 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; | |
772 | sb->s_magic = SPUFS_MAGIC; | |
773 | sb->s_op = &s_ops; | |
774 | ||
775 | return spufs_create_root(sb, data); | |
776 | } | |
777 | ||
454e2398 | 778 | static int |
67207b96 | 779 | spufs_get_sb(struct file_system_type *fstype, int flags, |
454e2398 | 780 | const char *name, void *data, struct vfsmount *mnt) |
67207b96 | 781 | { |
454e2398 | 782 | return get_sb_single(fstype, flags, data, spufs_fill_super, mnt); |
67207b96 AB |
783 | } |
784 | ||
785 | static struct file_system_type spufs_type = { | |
786 | .owner = THIS_MODULE, | |
787 | .name = "spufs", | |
788 | .get_sb = spufs_get_sb, | |
789 | .kill_sb = kill_litter_super, | |
790 | }; | |
791 | ||
e78b47a5 | 792 | static int __init spufs_init(void) |
67207b96 AB |
793 | { |
794 | int ret; | |
bf1ab978 | 795 | |
ccf17e9d JK |
796 | ret = -ENODEV; |
797 | if (!spu_management_ops) | |
798 | goto out; | |
799 | ||
67207b96 AB |
800 | ret = -ENOMEM; |
801 | spufs_inode_cache = kmem_cache_create("spufs_inode_cache", | |
802 | sizeof(struct spufs_inode_info), 0, | |
20c2df83 | 803 | SLAB_HWCACHE_ALIGN, spufs_init_once); |
67207b96 AB |
804 | |
805 | if (!spufs_inode_cache) | |
806 | goto out; | |
c99c1994 | 807 | ret = spu_sched_init(); |
67207b96 AB |
808 | if (ret) |
809 | goto out_cache; | |
c99c1994 AM |
810 | ret = register_filesystem(&spufs_type); |
811 | if (ret) | |
812 | goto out_sched; | |
67207b96 | 813 | ret = register_spu_syscalls(&spufs_calls); |
bf1ab978 DGM |
814 | if (ret) |
815 | goto out_fs; | |
0afacde3 AB |
816 | |
817 | spufs_init_isolated_loader(); | |
bf1ab978 | 818 | |
67207b96 | 819 | return 0; |
c99c1994 | 820 | |
67207b96 AB |
821 | out_fs: |
822 | unregister_filesystem(&spufs_type); | |
c99c1994 AM |
823 | out_sched: |
824 | spu_sched_exit(); | |
67207b96 AB |
825 | out_cache: |
826 | kmem_cache_destroy(spufs_inode_cache); | |
827 | out: | |
828 | return ret; | |
829 | } | |
830 | module_init(spufs_init); | |
831 | ||
e78b47a5 | 832 | static void __exit spufs_exit(void) |
67207b96 | 833 | { |
8b3d6663 | 834 | spu_sched_exit(); |
db1384b4 | 835 | spufs_exit_isolated_loader(); |
67207b96 AB |
836 | unregister_spu_syscalls(&spufs_calls); |
837 | unregister_filesystem(&spufs_type); | |
838 | kmem_cache_destroy(spufs_inode_cache); | |
839 | } | |
840 | module_exit(spufs_exit); | |
841 | ||
842 | MODULE_LICENSE("GPL"); | |
843 | MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>"); | |
844 |