5 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
7 * Author: Arnd Bergmann <arndb@de.ibm.com>
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)
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.
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.
24 #include <linux/file.h>
26 #include <linux/fsnotify.h>
27 #include <linux/backing-dev.h>
28 #include <linux/init.h>
29 #include <linux/ioctl.h>
30 #include <linux/module.h>
31 #include <linux/mount.h>
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>
40 #include <asm/spu_priv1.h>
41 #include <asm/uaccess.h>
45 struct spufs_sb_info
{
49 static struct kmem_cache
*spufs_inode_cache
;
50 char *isolated_loader
;
51 static int isolated_loader_size
;
53 static struct spufs_sb_info
*spufs_get_sb_info(struct super_block
*sb
)
59 spufs_alloc_inode(struct super_block
*sb
)
61 struct spufs_inode_info
*ei
;
63 ei
= kmem_cache_alloc(spufs_inode_cache
, GFP_KERNEL
);
71 return &ei
->vfs_inode
;
74 static void spufs_i_callback(struct rcu_head
*head
)
76 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
77 kmem_cache_free(spufs_inode_cache
, SPUFS_I(inode
));
80 static void spufs_destroy_inode(struct inode
*inode
)
82 call_rcu(&inode
->i_rcu
, spufs_i_callback
);
86 spufs_init_once(void *p
)
88 struct spufs_inode_info
*ei
= p
;
90 inode_init_once(&ei
->vfs_inode
);
94 spufs_new_inode(struct super_block
*sb
, umode_t mode
)
98 inode
= new_inode(sb
);
102 inode
->i_ino
= get_next_ino();
103 inode
->i_mode
= mode
;
104 inode
->i_uid
= current_fsuid();
105 inode
->i_gid
= current_fsgid();
106 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
112 spufs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
114 struct inode
*inode
= dentry
->d_inode
;
116 if ((attr
->ia_valid
& ATTR_SIZE
) &&
117 (attr
->ia_size
!= inode
->i_size
))
119 setattr_copy(inode
, attr
);
120 mark_inode_dirty(inode
);
126 spufs_new_file(struct super_block
*sb
, struct dentry
*dentry
,
127 const struct file_operations
*fops
, umode_t mode
,
128 size_t size
, struct spu_context
*ctx
)
130 static const struct inode_operations spufs_file_iops
= {
131 .setattr
= spufs_setattr
,
137 inode
= spufs_new_inode(sb
, S_IFREG
| mode
);
142 inode
->i_op
= &spufs_file_iops
;
144 inode
->i_size
= size
;
145 inode
->i_private
= SPUFS_I(inode
)->i_ctx
= get_spu_context(ctx
);
146 d_add(dentry
, inode
);
152 spufs_evict_inode(struct inode
*inode
)
154 struct spufs_inode_info
*ei
= SPUFS_I(inode
);
157 put_spu_context(ei
->i_ctx
);
159 put_spu_gang(ei
->i_gang
);
162 static void spufs_prune_dir(struct dentry
*dir
)
164 struct dentry
*dentry
, *tmp
;
166 mutex_lock(&dir
->d_inode
->i_mutex
);
167 list_for_each_entry_safe(dentry
, tmp
, &dir
->d_subdirs
, d_u
.d_child
) {
168 spin_lock(&dentry
->d_lock
);
169 if (!(d_unhashed(dentry
)) && dentry
->d_inode
) {
172 spin_unlock(&dentry
->d_lock
);
173 simple_unlink(dir
->d_inode
, dentry
);
174 /* XXX: what was dcache_lock protecting here? Other
175 * filesystems (IB, configfs) release dcache_lock
179 spin_unlock(&dentry
->d_lock
);
182 shrink_dcache_parent(dir
);
183 mutex_unlock(&dir
->d_inode
->i_mutex
);
186 /* Caller must hold parent->i_mutex */
187 static int spufs_rmdir(struct inode
*parent
, struct dentry
*dir
)
189 /* remove all entries */
191 spufs_prune_dir(dir
);
193 res
= simple_rmdir(parent
, dir
);
194 /* We have to give up the mm_struct */
195 spu_forget(SPUFS_I(dir
->d_inode
)->i_ctx
);
199 static int spufs_fill_dir(struct dentry
*dir
,
200 const struct spufs_tree_descr
*files
, umode_t mode
,
201 struct spu_context
*ctx
)
203 while (files
->name
&& files
->name
[0]) {
205 struct dentry
*dentry
= d_alloc_name(dir
, files
->name
);
208 ret
= spufs_new_file(dir
->d_sb
, dentry
, files
->ops
,
209 files
->mode
& mode
, files
->size
, ctx
);
217 static int spufs_dir_close(struct inode
*inode
, struct file
*file
)
219 struct spu_context
*ctx
;
220 struct inode
*parent
;
224 dir
= file
->f_path
.dentry
;
225 parent
= dir
->d_parent
->d_inode
;
226 ctx
= SPUFS_I(dir
->d_inode
)->i_ctx
;
228 mutex_lock_nested(&parent
->i_mutex
, I_MUTEX_PARENT
);
229 ret
= spufs_rmdir(parent
, dir
);
230 mutex_unlock(&parent
->i_mutex
);
233 return dcache_dir_close(inode
, file
);
236 const struct file_operations spufs_context_fops
= {
237 .open
= dcache_dir_open
,
238 .release
= spufs_dir_close
,
239 .llseek
= dcache_dir_lseek
,
240 .read
= generic_read_dir
,
241 .iterate
= dcache_readdir
,
244 EXPORT_SYMBOL_GPL(spufs_context_fops
);
247 spufs_mkdir(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
,
252 struct spu_context
*ctx
;
254 inode
= spufs_new_inode(dir
->i_sb
, mode
| S_IFDIR
);
258 if (dir
->i_mode
& S_ISGID
) {
259 inode
->i_gid
= dir
->i_gid
;
260 inode
->i_mode
&= S_ISGID
;
262 ctx
= alloc_spu_context(SPUFS_I(dir
)->i_gang
); /* XXX gang */
263 SPUFS_I(inode
)->i_ctx
= ctx
;
270 inode
->i_op
= &simple_dir_inode_operations
;
271 inode
->i_fop
= &simple_dir_operations
;
273 mutex_lock(&inode
->i_mutex
);
279 d_instantiate(dentry
, inode
);
281 if (flags
& SPU_CREATE_NOSCHED
)
282 ret
= spufs_fill_dir(dentry
, spufs_dir_nosched_contents
,
285 ret
= spufs_fill_dir(dentry
, spufs_dir_contents
, mode
, ctx
);
287 if (!ret
&& spufs_get_sb_info(dir
->i_sb
)->debug
)
288 ret
= spufs_fill_dir(dentry
, spufs_dir_debug_contents
,
292 spufs_rmdir(dir
, dentry
);
294 mutex_unlock(&inode
->i_mutex
);
299 static int spufs_context_open(struct path
*path
)
304 ret
= get_unused_fd();
308 filp
= dentry_open(path
, O_RDONLY
, current_cred());
311 return PTR_ERR(filp
);
314 filp
->f_op
= &spufs_context_fops
;
315 fd_install(ret
, filp
);
319 static struct spu_context
*
320 spufs_assert_affinity(unsigned int flags
, struct spu_gang
*gang
,
323 struct spu_context
*tmp
, *neighbor
, *err
;
327 aff_supp
= !list_empty(&(list_entry(cbe_spu_info
[0].spus
.next
,
328 struct spu
, cbe_list
))->aff_list
);
331 return ERR_PTR(-EINVAL
);
333 if (flags
& SPU_CREATE_GANG
)
334 return ERR_PTR(-EINVAL
);
336 if (flags
& SPU_CREATE_AFFINITY_MEM
&&
338 gang
->aff_ref_ctx
->flags
& SPU_CREATE_AFFINITY_MEM
)
339 return ERR_PTR(-EEXIST
);
341 if (gang
->aff_flags
& AFF_MERGED
)
342 return ERR_PTR(-EBUSY
);
345 if (flags
& SPU_CREATE_AFFINITY_SPU
) {
346 if (!filp
|| filp
->f_op
!= &spufs_context_fops
)
347 return ERR_PTR(-EINVAL
);
349 neighbor
= get_spu_context(
350 SPUFS_I(file_inode(filp
))->i_ctx
);
352 if (!list_empty(&neighbor
->aff_list
) && !(neighbor
->aff_head
) &&
353 !list_is_last(&neighbor
->aff_list
, &gang
->aff_list_head
) &&
354 !list_entry(neighbor
->aff_list
.next
, struct spu_context
,
355 aff_list
)->aff_head
) {
356 err
= ERR_PTR(-EEXIST
);
357 goto out_put_neighbor
;
360 if (gang
!= neighbor
->gang
) {
361 err
= ERR_PTR(-EINVAL
);
362 goto out_put_neighbor
;
366 list_for_each_entry(tmp
, &gang
->aff_list_head
, aff_list
)
368 if (list_empty(&neighbor
->aff_list
))
371 for (node
= 0; node
< MAX_NUMNODES
; node
++) {
372 if ((cbe_spu_info
[node
].n_spus
- atomic_read(
373 &cbe_spu_info
[node
].reserved_spus
)) >= count
)
377 if (node
== MAX_NUMNODES
) {
378 err
= ERR_PTR(-EEXIST
);
379 goto out_put_neighbor
;
386 put_spu_context(neighbor
);
391 spufs_set_affinity(unsigned int flags
, struct spu_context
*ctx
,
392 struct spu_context
*neighbor
)
394 if (flags
& SPU_CREATE_AFFINITY_MEM
)
395 ctx
->gang
->aff_ref_ctx
= ctx
;
397 if (flags
& SPU_CREATE_AFFINITY_SPU
) {
398 if (list_empty(&neighbor
->aff_list
)) {
399 list_add_tail(&neighbor
->aff_list
,
400 &ctx
->gang
->aff_list_head
);
401 neighbor
->aff_head
= 1;
404 if (list_is_last(&neighbor
->aff_list
, &ctx
->gang
->aff_list_head
)
405 || list_entry(neighbor
->aff_list
.next
, struct spu_context
,
406 aff_list
)->aff_head
) {
407 list_add(&ctx
->aff_list
, &neighbor
->aff_list
);
409 list_add_tail(&ctx
->aff_list
, &neighbor
->aff_list
);
410 if (neighbor
->aff_head
) {
411 neighbor
->aff_head
= 0;
416 if (!ctx
->gang
->aff_ref_ctx
)
417 ctx
->gang
->aff_ref_ctx
= ctx
;
422 spufs_create_context(struct inode
*inode
, struct dentry
*dentry
,
423 struct vfsmount
*mnt
, int flags
, umode_t mode
,
424 struct file
*aff_filp
)
428 struct spu_gang
*gang
;
429 struct spu_context
*neighbor
;
430 struct path path
= {.mnt
= mnt
, .dentry
= dentry
};
432 if ((flags
& SPU_CREATE_NOSCHED
) &&
433 !capable(CAP_SYS_NICE
))
436 if ((flags
& (SPU_CREATE_NOSCHED
| SPU_CREATE_ISOLATE
))
437 == SPU_CREATE_ISOLATE
)
440 if ((flags
& SPU_CREATE_ISOLATE
) && !isolated_loader
)
445 affinity
= flags
& (SPU_CREATE_AFFINITY_MEM
| SPU_CREATE_AFFINITY_SPU
);
447 gang
= SPUFS_I(inode
)->i_gang
;
450 mutex_lock(&gang
->aff_mutex
);
451 neighbor
= spufs_assert_affinity(flags
, gang
, aff_filp
);
452 if (IS_ERR(neighbor
)) {
453 ret
= PTR_ERR(neighbor
);
458 ret
= spufs_mkdir(inode
, dentry
, flags
, mode
& S_IRWXUGO
);
463 spufs_set_affinity(flags
, SPUFS_I(dentry
->d_inode
)->i_ctx
,
466 put_spu_context(neighbor
);
469 ret
= spufs_context_open(&path
);
471 WARN_ON(spufs_rmdir(inode
, dentry
));
475 mutex_unlock(&gang
->aff_mutex
);
480 spufs_mkgang(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
484 struct spu_gang
*gang
;
487 inode
= spufs_new_inode(dir
->i_sb
, mode
| S_IFDIR
);
492 if (dir
->i_mode
& S_ISGID
) {
493 inode
->i_gid
= dir
->i_gid
;
494 inode
->i_mode
&= S_ISGID
;
496 gang
= alloc_spu_gang();
497 SPUFS_I(inode
)->i_ctx
= NULL
;
498 SPUFS_I(inode
)->i_gang
= gang
;
502 inode
->i_op
= &simple_dir_inode_operations
;
503 inode
->i_fop
= &simple_dir_operations
;
505 d_instantiate(dentry
, inode
);
507 inc_nlink(dentry
->d_inode
);
516 static int spufs_gang_open(struct path
*path
)
521 ret
= get_unused_fd();
526 * get references for dget and mntget, will be released
527 * in error path of *_open().
529 filp
= dentry_open(path
, O_RDONLY
, current_cred());
532 return PTR_ERR(filp
);
535 filp
->f_op
= &simple_dir_operations
;
536 fd_install(ret
, filp
);
540 static int spufs_create_gang(struct inode
*inode
,
541 struct dentry
*dentry
,
542 struct vfsmount
*mnt
, umode_t mode
)
544 struct path path
= {.mnt
= mnt
, .dentry
= dentry
};
547 ret
= spufs_mkgang(inode
, dentry
, mode
& S_IRWXUGO
);
549 ret
= spufs_gang_open(&path
);
551 int err
= simple_rmdir(inode
, dentry
);
559 static struct file_system_type spufs_type
;
561 long spufs_create(struct path
*path
, struct dentry
*dentry
,
562 unsigned int flags
, umode_t mode
, struct file
*filp
)
564 struct inode
*dir
= path
->dentry
->d_inode
;
567 /* check if we are on spufs */
568 if (path
->dentry
->d_sb
->s_type
!= &spufs_type
)
571 /* don't accept undefined flags */
572 if (flags
& (~SPU_CREATE_FLAG_ALL
))
575 /* only threads can be underneath a gang */
576 if (path
->dentry
!= path
->dentry
->d_sb
->s_root
)
577 if ((flags
& SPU_CREATE_GANG
) || !SPUFS_I(dir
)->i_gang
)
580 mode
&= ~current_umask();
582 if (flags
& SPU_CREATE_GANG
)
583 ret
= spufs_create_gang(dir
, dentry
, path
->mnt
, mode
);
585 ret
= spufs_create_context(dir
, dentry
, path
->mnt
, flags
, mode
,
588 fsnotify_mkdir(dir
, dentry
);
593 /* File system initialization */
595 Opt_uid
, Opt_gid
, Opt_mode
, Opt_debug
, Opt_err
,
598 static const match_table_t spufs_tokens
= {
599 { Opt_uid
, "uid=%d" },
600 { Opt_gid
, "gid=%d" },
601 { Opt_mode
, "mode=%o" },
602 { Opt_debug
, "debug" },
607 spufs_parse_options(struct super_block
*sb
, char *options
, struct inode
*root
)
610 substring_t args
[MAX_OPT_ARGS
];
612 while ((p
= strsep(&options
, ",")) != NULL
) {
618 token
= match_token(p
, spufs_tokens
, args
);
621 if (match_int(&args
[0], &option
))
623 root
->i_uid
= make_kuid(current_user_ns(), option
);
624 if (!uid_valid(root
->i_uid
))
628 if (match_int(&args
[0], &option
))
630 root
->i_gid
= make_kgid(current_user_ns(), option
);
631 if (!gid_valid(root
->i_gid
))
635 if (match_octal(&args
[0], &option
))
637 root
->i_mode
= option
| S_IFDIR
;
640 spufs_get_sb_info(sb
)->debug
= 1;
649 static void spufs_exit_isolated_loader(void)
651 free_pages((unsigned long) isolated_loader
,
652 get_order(isolated_loader_size
));
656 spufs_init_isolated_loader(void)
658 struct device_node
*dn
;
662 dn
= of_find_node_by_path("/spu-isolation");
666 loader
= of_get_property(dn
, "loader", &size
);
670 /* the loader must be align on a 16 byte boundary */
671 isolated_loader
= (char *)__get_free_pages(GFP_KERNEL
, get_order(size
));
672 if (!isolated_loader
)
675 isolated_loader_size
= size
;
676 memcpy(isolated_loader
, loader
, size
);
677 printk(KERN_INFO
"spufs: SPU isolation mode enabled\n");
681 spufs_create_root(struct super_block
*sb
, void *data
)
687 if (!spu_management_ops
)
691 inode
= spufs_new_inode(sb
, S_IFDIR
| 0775);
695 inode
->i_op
= &simple_dir_inode_operations
;
696 inode
->i_fop
= &simple_dir_operations
;
697 SPUFS_I(inode
)->i_ctx
= NULL
;
701 if (!spufs_parse_options(sb
, data
, inode
))
705 sb
->s_root
= d_make_root(inode
);
717 spufs_fill_super(struct super_block
*sb
, void *data
, int silent
)
719 struct spufs_sb_info
*info
;
720 static const struct super_operations s_ops
= {
721 .alloc_inode
= spufs_alloc_inode
,
722 .destroy_inode
= spufs_destroy_inode
,
723 .statfs
= simple_statfs
,
724 .evict_inode
= spufs_evict_inode
,
725 .show_options
= generic_show_options
,
728 save_mount_options(sb
, data
);
730 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
734 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
735 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
736 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
737 sb
->s_magic
= SPUFS_MAGIC
;
739 sb
->s_fs_info
= info
;
741 return spufs_create_root(sb
, data
);
744 static struct dentry
*
745 spufs_mount(struct file_system_type
*fstype
, int flags
,
746 const char *name
, void *data
)
748 return mount_single(fstype
, flags
, data
, spufs_fill_super
);
751 static struct file_system_type spufs_type
= {
752 .owner
= THIS_MODULE
,
754 .mount
= spufs_mount
,
755 .kill_sb
= kill_litter_super
,
757 MODULE_ALIAS_FS("spufs");
759 static int __init
spufs_init(void)
764 if (!spu_management_ops
)
768 spufs_inode_cache
= kmem_cache_create("spufs_inode_cache",
769 sizeof(struct spufs_inode_info
), 0,
770 SLAB_HWCACHE_ALIGN
, spufs_init_once
);
772 if (!spufs_inode_cache
)
774 ret
= spu_sched_init();
777 ret
= register_spu_syscalls(&spufs_calls
);
780 ret
= register_filesystem(&spufs_type
);
784 spufs_init_isolated_loader();
789 unregister_spu_syscalls(&spufs_calls
);
793 kmem_cache_destroy(spufs_inode_cache
);
797 module_init(spufs_init
);
799 static void __exit
spufs_exit(void)
802 spufs_exit_isolated_loader();
803 unregister_spu_syscalls(&spufs_calls
);
804 unregister_filesystem(&spufs_type
);
805 kmem_cache_destroy(spufs_inode_cache
);
807 module_exit(spufs_exit
);
809 MODULE_LICENSE("GPL");
810 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");