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