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)
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>
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>
37 #include <asm/semaphore.h>
39 #include <asm/uaccess.h>
43 static struct kmem_cache
*spufs_inode_cache
;
44 char *isolated_loader
;
47 spufs_alloc_inode(struct super_block
*sb
)
49 struct spufs_inode_info
*ei
;
51 ei
= kmem_cache_alloc(spufs_inode_cache
, GFP_KERNEL
);
59 return &ei
->vfs_inode
;
63 spufs_destroy_inode(struct inode
*inode
)
65 kmem_cache_free(spufs_inode_cache
, SPUFS_I(inode
));
69 spufs_init_once(void *p
, struct kmem_cache
* cachep
, unsigned long flags
)
71 struct spufs_inode_info
*ei
= p
;
73 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
74 SLAB_CTOR_CONSTRUCTOR
) {
75 inode_init_once(&ei
->vfs_inode
);
80 spufs_new_inode(struct super_block
*sb
, int mode
)
84 inode
= new_inode(sb
);
89 inode
->i_uid
= current
->fsuid
;
90 inode
->i_gid
= current
->fsgid
;
92 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
98 spufs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
100 struct inode
*inode
= dentry
->d_inode
;
102 if ((attr
->ia_valid
& ATTR_SIZE
) &&
103 (attr
->ia_size
!= inode
->i_size
))
105 return inode_setattr(inode
, attr
);
110 spufs_new_file(struct super_block
*sb
, struct dentry
*dentry
,
111 const struct file_operations
*fops
, int mode
,
112 struct spu_context
*ctx
)
114 static struct inode_operations spufs_file_iops
= {
115 .setattr
= spufs_setattr
,
121 inode
= spufs_new_inode(sb
, S_IFREG
| mode
);
126 inode
->i_op
= &spufs_file_iops
;
128 inode
->i_private
= SPUFS_I(inode
)->i_ctx
= get_spu_context(ctx
);
129 d_add(dentry
, inode
);
135 spufs_delete_inode(struct inode
*inode
)
137 struct spufs_inode_info
*ei
= SPUFS_I(inode
);
140 put_spu_context(ei
->i_ctx
);
142 put_spu_gang(ei
->i_gang
);
146 static void spufs_prune_dir(struct dentry
*dir
)
148 struct dentry
*dentry
, *tmp
;
150 mutex_lock(&dir
->d_inode
->i_mutex
);
151 list_for_each_entry_safe(dentry
, tmp
, &dir
->d_subdirs
, d_u
.d_child
) {
152 spin_lock(&dcache_lock
);
153 spin_lock(&dentry
->d_lock
);
154 if (!(d_unhashed(dentry
)) && dentry
->d_inode
) {
157 spin_unlock(&dentry
->d_lock
);
158 simple_unlink(dir
->d_inode
, dentry
);
159 spin_unlock(&dcache_lock
);
162 spin_unlock(&dentry
->d_lock
);
163 spin_unlock(&dcache_lock
);
166 shrink_dcache_parent(dir
);
167 mutex_unlock(&dir
->d_inode
->i_mutex
);
170 /* Caller must hold parent->i_mutex */
171 static int spufs_rmdir(struct inode
*parent
, struct dentry
*dir
)
173 /* remove all entries */
174 spufs_prune_dir(dir
);
176 return simple_rmdir(parent
, dir
);
179 static int spufs_fill_dir(struct dentry
*dir
, struct tree_descr
*files
,
180 int mode
, struct spu_context
*ctx
)
182 struct dentry
*dentry
;
185 while (files
->name
&& files
->name
[0]) {
187 dentry
= d_alloc_name(dir
, files
->name
);
190 ret
= spufs_new_file(dir
->d_sb
, dentry
, files
->ops
,
191 files
->mode
& mode
, ctx
);
198 spufs_prune_dir(dir
);
202 static int spufs_dir_close(struct inode
*inode
, struct file
*file
)
204 struct spu_context
*ctx
;
205 struct inode
*parent
;
209 dir
= file
->f_path
.dentry
;
210 parent
= dir
->d_parent
->d_inode
;
211 ctx
= SPUFS_I(dir
->d_inode
)->i_ctx
;
213 mutex_lock(&parent
->i_mutex
);
214 ret
= spufs_rmdir(parent
, dir
);
215 mutex_unlock(&parent
->i_mutex
);
218 /* We have to give up the mm_struct */
221 return dcache_dir_close(inode
, file
);
224 const struct inode_operations spufs_dir_inode_operations
= {
225 .lookup
= simple_lookup
,
228 const struct file_operations spufs_context_fops
= {
229 .open
= dcache_dir_open
,
230 .release
= spufs_dir_close
,
231 .llseek
= dcache_dir_lseek
,
232 .read
= generic_read_dir
,
233 .readdir
= dcache_readdir
,
234 .fsync
= simple_sync_file
,
236 EXPORT_SYMBOL_GPL(spufs_context_fops
);
239 spufs_mkdir(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
,
244 struct spu_context
*ctx
;
247 inode
= spufs_new_inode(dir
->i_sb
, mode
| S_IFDIR
);
251 if (dir
->i_mode
& S_ISGID
) {
252 inode
->i_gid
= dir
->i_gid
;
253 inode
->i_mode
&= S_ISGID
;
255 ctx
= alloc_spu_context(SPUFS_I(dir
)->i_gang
); /* XXX gang */
256 SPUFS_I(inode
)->i_ctx
= ctx
;
261 inode
->i_op
= &spufs_dir_inode_operations
;
262 inode
->i_fop
= &simple_dir_operations
;
263 if (flags
& SPU_CREATE_NOSCHED
)
264 ret
= spufs_fill_dir(dentry
, spufs_dir_nosched_contents
,
267 ret
= spufs_fill_dir(dentry
, spufs_dir_contents
, mode
, ctx
);
272 d_instantiate(dentry
, inode
);
275 dentry
->d_inode
->i_nlink
++;
279 put_spu_context(ctx
);
286 static int spufs_context_open(struct dentry
*dentry
, struct vfsmount
*mnt
)
291 ret
= get_unused_fd();
298 filp
= dentry_open(dentry
, mnt
, O_RDONLY
);
305 filp
->f_op
= &spufs_context_fops
;
306 fd_install(ret
, filp
);
311 static int spufs_create_context(struct inode
*inode
,
312 struct dentry
*dentry
,
313 struct vfsmount
*mnt
, int flags
, int mode
)
318 if ((flags
& SPU_CREATE_NOSCHED
) &&
319 !capable(CAP_SYS_NICE
))
323 if ((flags
& (SPU_CREATE_NOSCHED
| SPU_CREATE_ISOLATE
))
324 == SPU_CREATE_ISOLATE
)
328 if ((flags
& SPU_CREATE_ISOLATE
) && !isolated_loader
)
331 ret
= spufs_mkdir(inode
, dentry
, flags
, mode
& S_IRWXUGO
);
336 * get references for dget and mntget, will be released
337 * in error path of *_open().
339 ret
= spufs_context_open(dget(dentry
), mntget(mnt
));
341 WARN_ON(spufs_rmdir(inode
, dentry
));
342 mutex_unlock(&inode
->i_mutex
);
343 spu_forget(SPUFS_I(dentry
->d_inode
)->i_ctx
);
348 mutex_unlock(&inode
->i_mutex
);
354 static int spufs_rmgang(struct inode
*root
, struct dentry
*dir
)
356 /* FIXME: this fails if the dir is not empty,
357 which causes a leak of gangs. */
358 return simple_rmdir(root
, dir
);
361 static int spufs_gang_close(struct inode
*inode
, struct file
*file
)
363 struct inode
*parent
;
367 dir
= file
->f_path
.dentry
;
368 parent
= dir
->d_parent
->d_inode
;
370 ret
= spufs_rmgang(parent
, dir
);
373 return dcache_dir_close(inode
, file
);
376 const struct file_operations spufs_gang_fops
= {
377 .open
= dcache_dir_open
,
378 .release
= spufs_gang_close
,
379 .llseek
= dcache_dir_lseek
,
380 .read
= generic_read_dir
,
381 .readdir
= dcache_readdir
,
382 .fsync
= simple_sync_file
,
386 spufs_mkgang(struct inode
*dir
, struct dentry
*dentry
, int mode
)
390 struct spu_gang
*gang
;
393 inode
= spufs_new_inode(dir
->i_sb
, mode
| S_IFDIR
);
398 if (dir
->i_mode
& S_ISGID
) {
399 inode
->i_gid
= dir
->i_gid
;
400 inode
->i_mode
&= S_ISGID
;
402 gang
= alloc_spu_gang();
403 SPUFS_I(inode
)->i_ctx
= NULL
;
404 SPUFS_I(inode
)->i_gang
= gang
;
408 inode
->i_op
= &spufs_dir_inode_operations
;
409 inode
->i_fop
= &simple_dir_operations
;
411 d_instantiate(dentry
, inode
);
414 dentry
->d_inode
->i_nlink
++;
423 static int spufs_gang_open(struct dentry
*dentry
, struct vfsmount
*mnt
)
428 ret
= get_unused_fd();
435 filp
= dentry_open(dentry
, mnt
, O_RDONLY
);
442 filp
->f_op
= &spufs_gang_fops
;
443 fd_install(ret
, filp
);
448 static int spufs_create_gang(struct inode
*inode
,
449 struct dentry
*dentry
,
450 struct vfsmount
*mnt
, int mode
)
454 ret
= spufs_mkgang(inode
, dentry
, mode
& S_IRWXUGO
);
459 * get references for dget and mntget, will be released
460 * in error path of *_open().
462 ret
= spufs_gang_open(dget(dentry
), mntget(mnt
));
464 WARN_ON(spufs_rmgang(inode
, dentry
));
467 mutex_unlock(&inode
->i_mutex
);
473 static struct file_system_type spufs_type
;
475 long spufs_create(struct nameidata
*nd
, unsigned int flags
, mode_t mode
)
477 struct dentry
*dentry
;
481 /* check if we are on spufs */
482 if (nd
->dentry
->d_sb
->s_type
!= &spufs_type
)
485 /* don't accept undefined flags */
486 if (flags
& (~SPU_CREATE_FLAG_ALL
))
489 /* only threads can be underneath a gang */
490 if (nd
->dentry
!= nd
->dentry
->d_sb
->s_root
) {
491 if ((flags
& SPU_CREATE_GANG
) ||
492 !SPUFS_I(nd
->dentry
->d_inode
)->i_gang
)
496 dentry
= lookup_create(nd
, 1);
497 ret
= PTR_ERR(dentry
);
505 mode
&= ~current
->fs
->umask
;
507 if (flags
& SPU_CREATE_GANG
)
508 return spufs_create_gang(nd
->dentry
->d_inode
,
509 dentry
, nd
->mnt
, mode
);
511 return spufs_create_context(nd
->dentry
->d_inode
,
512 dentry
, nd
->mnt
, flags
, mode
);
517 mutex_unlock(&nd
->dentry
->d_inode
->i_mutex
);
522 /* File system initialization */
524 Opt_uid
, Opt_gid
, Opt_err
,
527 static match_table_t spufs_tokens
= {
528 { Opt_uid
, "uid=%d" },
529 { Opt_gid
, "gid=%d" },
534 spufs_parse_options(char *options
, struct inode
*root
)
537 substring_t args
[MAX_OPT_ARGS
];
539 while ((p
= strsep(&options
, ",")) != NULL
) {
545 token
= match_token(p
, spufs_tokens
, args
);
548 if (match_int(&args
[0], &option
))
550 root
->i_uid
= option
;
553 if (match_int(&args
[0], &option
))
555 root
->i_gid
= option
;
565 spufs_init_isolated_loader(void)
567 struct device_node
*dn
;
571 dn
= of_find_node_by_path("/spu-isolation");
575 loader
= of_get_property(dn
, "loader", &size
);
579 /* kmalloc should align on a 16 byte boundary..* */
580 isolated_loader
= kmalloc(size
, GFP_KERNEL
);
581 if (!isolated_loader
)
584 memcpy(isolated_loader
, loader
, size
);
585 printk(KERN_INFO
"spufs: SPU isolation mode enabled\n");
589 spufs_create_root(struct super_block
*sb
, void *data
)
595 inode
= spufs_new_inode(sb
, S_IFDIR
| 0775);
599 inode
->i_op
= &spufs_dir_inode_operations
;
600 inode
->i_fop
= &simple_dir_operations
;
601 SPUFS_I(inode
)->i_ctx
= NULL
;
604 if (!spufs_parse_options(data
, inode
))
608 sb
->s_root
= d_alloc_root(inode
);
620 spufs_fill_super(struct super_block
*sb
, void *data
, int silent
)
622 static struct super_operations s_ops
= {
623 .alloc_inode
= spufs_alloc_inode
,
624 .destroy_inode
= spufs_destroy_inode
,
625 .statfs
= simple_statfs
,
626 .delete_inode
= spufs_delete_inode
,
627 .drop_inode
= generic_delete_inode
,
630 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
631 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
632 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
633 sb
->s_magic
= SPUFS_MAGIC
;
636 return spufs_create_root(sb
, data
);
640 spufs_get_sb(struct file_system_type
*fstype
, int flags
,
641 const char *name
, void *data
, struct vfsmount
*mnt
)
643 return get_sb_single(fstype
, flags
, data
, spufs_fill_super
, mnt
);
646 static struct file_system_type spufs_type
= {
647 .owner
= THIS_MODULE
,
649 .get_sb
= spufs_get_sb
,
650 .kill_sb
= kill_litter_super
,
653 static int __init
spufs_init(void)
658 spufs_inode_cache
= kmem_cache_create("spufs_inode_cache",
659 sizeof(struct spufs_inode_info
), 0,
660 SLAB_HWCACHE_ALIGN
, spufs_init_once
, NULL
);
662 if (!spufs_inode_cache
)
664 ret
= spu_sched_init();
667 ret
= register_filesystem(&spufs_type
);
670 ret
= register_spu_syscalls(&spufs_calls
);
673 ret
= register_arch_coredump_calls(&spufs_coredump_calls
);
677 spufs_init_isolated_loader();
682 unregister_spu_syscalls(&spufs_calls
);
684 unregister_filesystem(&spufs_type
);
688 kmem_cache_destroy(spufs_inode_cache
);
692 module_init(spufs_init
);
694 static void __exit
spufs_exit(void)
697 unregister_arch_coredump_calls(&spufs_coredump_calls
);
698 unregister_spu_syscalls(&spufs_calls
);
699 unregister_filesystem(&spufs_type
);
700 kmem_cache_destroy(spufs_inode_cache
);
702 module_exit(spufs_exit
);
704 MODULE_LICENSE("GPL");
705 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");