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