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