[POWERPC] spufs: Free mm if spufs_fill_dir() failed
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / powerpc / platforms / cell / spufs / inode.c
blob08356813977103e173b76bea7213dcf7f9b8bab7
1 /*
2 * SPU file system
4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
6 * Author: Arnd Bergmann <arndb@de.ibm.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/file.h>
24 #include <linux/fs.h>
25 #include <linux/backing-dev.h>
26 #include <linux/init.h>
27 #include <linux/ioctl.h>
28 #include <linux/module.h>
29 #include <linux/mount.h>
30 #include <linux/namei.h>
31 #include <linux/pagemap.h>
32 #include <linux/poll.h>
33 #include <linux/slab.h>
34 #include <linux/parser.h>
36 #include <asm/prom.h>
37 #include <asm/semaphore.h>
38 #include <asm/spu.h>
39 #include <asm/spu_priv1.h>
40 #include <asm/uaccess.h>
42 #include "spufs.h"
44 static struct kmem_cache *spufs_inode_cache;
45 char *isolated_loader;
47 static struct inode *
48 spufs_alloc_inode(struct super_block *sb)
50 struct spufs_inode_info *ei;
52 ei = kmem_cache_alloc(spufs_inode_cache, GFP_KERNEL);
53 if (!ei)
54 return NULL;
56 ei->i_gang = NULL;
57 ei->i_ctx = NULL;
58 ei->i_openers = 0;
60 return &ei->vfs_inode;
63 static void
64 spufs_destroy_inode(struct inode *inode)
66 kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
69 static void
70 spufs_init_once(void *p, struct kmem_cache * cachep, unsigned long flags)
72 struct spufs_inode_info *ei = p;
74 inode_init_once(&ei->vfs_inode);
77 static struct inode *
78 spufs_new_inode(struct super_block *sb, int mode)
80 struct inode *inode;
82 inode = new_inode(sb);
83 if (!inode)
84 goto out;
86 inode->i_mode = mode;
87 inode->i_uid = current->fsuid;
88 inode->i_gid = current->fsgid;
89 inode->i_blocks = 0;
90 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
91 out:
92 return inode;
95 static int
96 spufs_setattr(struct dentry *dentry, struct iattr *attr)
98 struct inode *inode = dentry->d_inode;
100 if ((attr->ia_valid & ATTR_SIZE) &&
101 (attr->ia_size != inode->i_size))
102 return -EINVAL;
103 return inode_setattr(inode, attr);
107 static int
108 spufs_new_file(struct super_block *sb, struct dentry *dentry,
109 const struct file_operations *fops, int mode,
110 struct spu_context *ctx)
112 static struct inode_operations spufs_file_iops = {
113 .setattr = spufs_setattr,
115 struct inode *inode;
116 int ret;
118 ret = -ENOSPC;
119 inode = spufs_new_inode(sb, S_IFREG | mode);
120 if (!inode)
121 goto out;
123 ret = 0;
124 inode->i_op = &spufs_file_iops;
125 inode->i_fop = fops;
126 inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
127 d_add(dentry, inode);
128 out:
129 return ret;
132 static void
133 spufs_delete_inode(struct inode *inode)
135 struct spufs_inode_info *ei = SPUFS_I(inode);
137 if (ei->i_ctx)
138 put_spu_context(ei->i_ctx);
139 if (ei->i_gang)
140 put_spu_gang(ei->i_gang);
141 clear_inode(inode);
144 static void spufs_prune_dir(struct dentry *dir)
146 struct dentry *dentry, *tmp;
148 mutex_lock(&dir->d_inode->i_mutex);
149 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
150 spin_lock(&dcache_lock);
151 spin_lock(&dentry->d_lock);
152 if (!(d_unhashed(dentry)) && dentry->d_inode) {
153 dget_locked(dentry);
154 __d_drop(dentry);
155 spin_unlock(&dentry->d_lock);
156 simple_unlink(dir->d_inode, dentry);
157 spin_unlock(&dcache_lock);
158 dput(dentry);
159 } else {
160 spin_unlock(&dentry->d_lock);
161 spin_unlock(&dcache_lock);
164 shrink_dcache_parent(dir);
165 mutex_unlock(&dir->d_inode->i_mutex);
168 /* Caller must hold parent->i_mutex */
169 static int spufs_rmdir(struct inode *parent, struct dentry *dir)
171 /* remove all entries */
172 spufs_prune_dir(dir);
174 return simple_rmdir(parent, dir);
177 static int spufs_fill_dir(struct dentry *dir, struct tree_descr *files,
178 int mode, struct spu_context *ctx)
180 struct dentry *dentry;
181 int ret;
183 while (files->name && files->name[0]) {
184 ret = -ENOMEM;
185 dentry = d_alloc_name(dir, files->name);
186 if (!dentry)
187 goto out;
188 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
189 files->mode & mode, ctx);
190 if (ret)
191 goto out;
192 files++;
194 return 0;
195 out:
196 spufs_prune_dir(dir);
197 return ret;
200 static int spufs_dir_close(struct inode *inode, struct file *file)
202 struct spu_context *ctx;
203 struct inode *parent;
204 struct dentry *dir;
205 int ret;
207 dir = file->f_path.dentry;
208 parent = dir->d_parent->d_inode;
209 ctx = SPUFS_I(dir->d_inode)->i_ctx;
211 mutex_lock(&parent->i_mutex);
212 ret = spufs_rmdir(parent, dir);
213 mutex_unlock(&parent->i_mutex);
214 WARN_ON(ret);
216 /* We have to give up the mm_struct */
217 spu_forget(ctx);
219 return dcache_dir_close(inode, file);
222 const struct inode_operations spufs_dir_inode_operations = {
223 .lookup = simple_lookup,
226 const struct file_operations spufs_context_fops = {
227 .open = dcache_dir_open,
228 .release = spufs_dir_close,
229 .llseek = dcache_dir_lseek,
230 .read = generic_read_dir,
231 .readdir = dcache_readdir,
232 .fsync = simple_sync_file,
234 EXPORT_SYMBOL_GPL(spufs_context_fops);
236 static int
237 spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
238 int mode)
240 int ret;
241 struct inode *inode;
242 struct spu_context *ctx;
244 ret = -ENOSPC;
245 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
246 if (!inode)
247 goto out;
249 if (dir->i_mode & S_ISGID) {
250 inode->i_gid = dir->i_gid;
251 inode->i_mode &= S_ISGID;
253 ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
254 SPUFS_I(inode)->i_ctx = ctx;
255 if (!ctx)
256 goto out_iput;
258 ctx->flags = flags;
259 inode->i_op = &spufs_dir_inode_operations;
260 inode->i_fop = &simple_dir_operations;
261 if (flags & SPU_CREATE_NOSCHED)
262 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
263 mode, ctx);
264 else
265 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
267 if (ret)
268 goto out_free_ctx;
270 d_instantiate(dentry, inode);
271 dget(dentry);
272 dir->i_nlink++;
273 dentry->d_inode->i_nlink++;
274 goto out;
276 out_free_ctx:
277 spu_forget(ctx);
278 put_spu_context(ctx);
279 out_iput:
280 iput(inode);
281 out:
282 return ret;
285 static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
287 int ret;
288 struct file *filp;
290 ret = get_unused_fd();
291 if (ret < 0) {
292 dput(dentry);
293 mntput(mnt);
294 goto out;
297 filp = dentry_open(dentry, mnt, O_RDONLY);
298 if (IS_ERR(filp)) {
299 put_unused_fd(ret);
300 ret = PTR_ERR(filp);
301 goto out;
304 filp->f_op = &spufs_context_fops;
305 fd_install(ret, filp);
306 out:
307 return ret;
310 static int spufs_create_context(struct inode *inode,
311 struct dentry *dentry,
312 struct vfsmount *mnt, int flags, int mode)
314 int ret;
316 ret = -EPERM;
317 if ((flags & SPU_CREATE_NOSCHED) &&
318 !capable(CAP_SYS_NICE))
319 goto out_unlock;
321 ret = -EINVAL;
322 if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
323 == SPU_CREATE_ISOLATE)
324 goto out_unlock;
326 ret = -ENODEV;
327 if ((flags & SPU_CREATE_ISOLATE) && !isolated_loader)
328 goto out_unlock;
330 ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
331 if (ret)
332 goto out_unlock;
335 * get references for dget and mntget, will be released
336 * in error path of *_open().
338 ret = spufs_context_open(dget(dentry), mntget(mnt));
339 if (ret < 0) {
340 WARN_ON(spufs_rmdir(inode, dentry));
341 mutex_unlock(&inode->i_mutex);
342 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
343 goto out;
346 out_unlock:
347 mutex_unlock(&inode->i_mutex);
348 out:
349 dput(dentry);
350 return ret;
353 static int
354 spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
356 int ret;
357 struct inode *inode;
358 struct spu_gang *gang;
360 ret = -ENOSPC;
361 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
362 if (!inode)
363 goto out;
365 ret = 0;
366 if (dir->i_mode & S_ISGID) {
367 inode->i_gid = dir->i_gid;
368 inode->i_mode &= S_ISGID;
370 gang = alloc_spu_gang();
371 SPUFS_I(inode)->i_ctx = NULL;
372 SPUFS_I(inode)->i_gang = gang;
373 if (!gang)
374 goto out_iput;
376 inode->i_op = &spufs_dir_inode_operations;
377 inode->i_fop = &simple_dir_operations;
379 d_instantiate(dentry, inode);
380 dir->i_nlink++;
381 dentry->d_inode->i_nlink++;
382 return ret;
384 out_iput:
385 iput(inode);
386 out:
387 return ret;
390 static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
392 int ret;
393 struct file *filp;
395 ret = get_unused_fd();
396 if (ret < 0) {
397 dput(dentry);
398 mntput(mnt);
399 goto out;
402 filp = dentry_open(dentry, mnt, O_RDONLY);
403 if (IS_ERR(filp)) {
404 put_unused_fd(ret);
405 ret = PTR_ERR(filp);
406 goto out;
409 filp->f_op = &simple_dir_operations;
410 fd_install(ret, filp);
411 out:
412 return ret;
415 static int spufs_create_gang(struct inode *inode,
416 struct dentry *dentry,
417 struct vfsmount *mnt, int mode)
419 int ret;
421 ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
422 if (ret)
423 goto out;
426 * get references for dget and mntget, will be released
427 * in error path of *_open().
429 ret = spufs_gang_open(dget(dentry), mntget(mnt));
430 if (ret < 0) {
431 int err = simple_rmdir(inode, dentry);
432 WARN_ON(err);
435 out:
436 mutex_unlock(&inode->i_mutex);
437 dput(dentry);
438 return ret;
442 static struct file_system_type spufs_type;
444 long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode)
446 struct dentry *dentry;
447 int ret;
449 ret = -EINVAL;
450 /* check if we are on spufs */
451 if (nd->dentry->d_sb->s_type != &spufs_type)
452 goto out;
454 /* don't accept undefined flags */
455 if (flags & (~SPU_CREATE_FLAG_ALL))
456 goto out;
458 /* only threads can be underneath a gang */
459 if (nd->dentry != nd->dentry->d_sb->s_root) {
460 if ((flags & SPU_CREATE_GANG) ||
461 !SPUFS_I(nd->dentry->d_inode)->i_gang)
462 goto out;
465 dentry = lookup_create(nd, 1);
466 ret = PTR_ERR(dentry);
467 if (IS_ERR(dentry))
468 goto out_dir;
470 ret = -EEXIST;
471 if (dentry->d_inode)
472 goto out_dput;
474 mode &= ~current->fs->umask;
476 if (flags & SPU_CREATE_GANG)
477 return spufs_create_gang(nd->dentry->d_inode,
478 dentry, nd->mnt, mode);
479 else
480 return spufs_create_context(nd->dentry->d_inode,
481 dentry, nd->mnt, flags, mode);
483 out_dput:
484 dput(dentry);
485 out_dir:
486 mutex_unlock(&nd->dentry->d_inode->i_mutex);
487 out:
488 return ret;
491 /* File system initialization */
492 enum {
493 Opt_uid, Opt_gid, Opt_mode, Opt_err,
496 static match_table_t spufs_tokens = {
497 { Opt_uid, "uid=%d" },
498 { Opt_gid, "gid=%d" },
499 { Opt_mode, "mode=%o" },
500 { Opt_err, NULL },
503 static int
504 spufs_parse_options(char *options, struct inode *root)
506 char *p;
507 substring_t args[MAX_OPT_ARGS];
509 while ((p = strsep(&options, ",")) != NULL) {
510 int token, option;
512 if (!*p)
513 continue;
515 token = match_token(p, spufs_tokens, args);
516 switch (token) {
517 case Opt_uid:
518 if (match_int(&args[0], &option))
519 return 0;
520 root->i_uid = option;
521 break;
522 case Opt_gid:
523 if (match_int(&args[0], &option))
524 return 0;
525 root->i_gid = option;
526 break;
527 case Opt_mode:
528 if (match_octal(&args[0], &option))
529 return 0;
530 root->i_mode = option | S_IFDIR;
531 break;
532 default:
533 return 0;
536 return 1;
539 static void spufs_exit_isolated_loader(void)
541 kfree(isolated_loader);
544 static void
545 spufs_init_isolated_loader(void)
547 struct device_node *dn;
548 const char *loader;
549 int size;
551 dn = of_find_node_by_path("/spu-isolation");
552 if (!dn)
553 return;
555 loader = of_get_property(dn, "loader", &size);
556 if (!loader)
557 return;
559 /* kmalloc should align on a 16 byte boundary..* */
560 isolated_loader = kmalloc(size, GFP_KERNEL);
561 if (!isolated_loader)
562 return;
564 memcpy(isolated_loader, loader, size);
565 printk(KERN_INFO "spufs: SPU isolation mode enabled\n");
568 static int
569 spufs_create_root(struct super_block *sb, void *data)
571 struct inode *inode;
572 int ret;
574 ret = -ENODEV;
575 if (!spu_management_ops)
576 goto out;
578 ret = -ENOMEM;
579 inode = spufs_new_inode(sb, S_IFDIR | 0775);
580 if (!inode)
581 goto out;
583 inode->i_op = &spufs_dir_inode_operations;
584 inode->i_fop = &simple_dir_operations;
585 SPUFS_I(inode)->i_ctx = NULL;
587 ret = -EINVAL;
588 if (!spufs_parse_options(data, inode))
589 goto out_iput;
591 ret = -ENOMEM;
592 sb->s_root = d_alloc_root(inode);
593 if (!sb->s_root)
594 goto out_iput;
596 return 0;
597 out_iput:
598 iput(inode);
599 out:
600 return ret;
603 static int
604 spufs_fill_super(struct super_block *sb, void *data, int silent)
606 static struct super_operations s_ops = {
607 .alloc_inode = spufs_alloc_inode,
608 .destroy_inode = spufs_destroy_inode,
609 .statfs = simple_statfs,
610 .delete_inode = spufs_delete_inode,
611 .drop_inode = generic_delete_inode,
614 sb->s_maxbytes = MAX_LFS_FILESIZE;
615 sb->s_blocksize = PAGE_CACHE_SIZE;
616 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
617 sb->s_magic = SPUFS_MAGIC;
618 sb->s_op = &s_ops;
620 return spufs_create_root(sb, data);
623 static int
624 spufs_get_sb(struct file_system_type *fstype, int flags,
625 const char *name, void *data, struct vfsmount *mnt)
627 return get_sb_single(fstype, flags, data, spufs_fill_super, mnt);
630 static struct file_system_type spufs_type = {
631 .owner = THIS_MODULE,
632 .name = "spufs",
633 .get_sb = spufs_get_sb,
634 .kill_sb = kill_litter_super,
637 static int __init spufs_init(void)
639 int ret;
641 ret = -ENODEV;
642 if (!spu_management_ops)
643 goto out;
645 ret = -ENOMEM;
646 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
647 sizeof(struct spufs_inode_info), 0,
648 SLAB_HWCACHE_ALIGN, spufs_init_once, NULL);
650 if (!spufs_inode_cache)
651 goto out;
652 ret = spu_sched_init();
653 if (ret)
654 goto out_cache;
655 ret = register_filesystem(&spufs_type);
656 if (ret)
657 goto out_sched;
658 ret = register_spu_syscalls(&spufs_calls);
659 if (ret)
660 goto out_fs;
661 ret = register_arch_coredump_calls(&spufs_coredump_calls);
662 if (ret)
663 goto out_syscalls;
665 spufs_init_isolated_loader();
667 return 0;
669 out_syscalls:
670 unregister_spu_syscalls(&spufs_calls);
671 out_fs:
672 unregister_filesystem(&spufs_type);
673 out_sched:
674 spu_sched_exit();
675 out_cache:
676 kmem_cache_destroy(spufs_inode_cache);
677 out:
678 return ret;
680 module_init(spufs_init);
682 static void __exit spufs_exit(void)
684 spu_sched_exit();
685 spufs_exit_isolated_loader();
686 unregister_arch_coredump_calls(&spufs_coredump_calls);
687 unregister_spu_syscalls(&spufs_calls);
688 unregister_filesystem(&spufs_type);
689 kmem_cache_destroy(spufs_inode_cache);
691 module_exit(spufs_exit);
693 MODULE_LICENSE("GPL");
694 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");