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