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 kmem_cache_t
*spufs_inode_cache
;
46 spufs_alloc_inode(struct super_block
*sb
)
48 struct spufs_inode_info
*ei
;
50 ei
= kmem_cache_alloc(spufs_inode_cache
, SLAB_KERNEL
);
57 return &ei
->vfs_inode
;
61 spufs_destroy_inode(struct inode
*inode
)
63 kmem_cache_free(spufs_inode_cache
, SPUFS_I(inode
));
67 spufs_init_once(void *p
, kmem_cache_t
* cachep
, unsigned long flags
)
69 struct spufs_inode_info
*ei
= p
;
71 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
72 SLAB_CTOR_CONSTRUCTOR
) {
73 inode_init_once(&ei
->vfs_inode
);
78 spufs_new_inode(struct super_block
*sb
, int mode
)
82 inode
= new_inode(sb
);
87 inode
->i_uid
= current
->fsuid
;
88 inode
->i_gid
= current
->fsgid
;
90 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
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
))
103 return inode_setattr(inode
, attr
);
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
,
119 inode
= spufs_new_inode(sb
, S_IFREG
| mode
);
124 inode
->i_op
= &spufs_file_iops
;
126 inode
->i_private
= SPUFS_I(inode
)->i_ctx
= get_spu_context(ctx
);
127 d_add(dentry
, inode
);
133 spufs_delete_inode(struct inode
*inode
)
135 struct spufs_inode_info
*ei
= SPUFS_I(inode
);
138 put_spu_context(ei
->i_ctx
);
140 put_spu_gang(ei
->i_gang
);
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
) {
155 spin_unlock(&dentry
->d_lock
);
156 simple_unlink(dir
->d_inode
, dentry
);
157 spin_unlock(&dcache_lock
);
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
;
183 while (files
->name
&& files
->name
[0]) {
185 dentry
= d_alloc_name(dir
, files
->name
);
188 ret
= spufs_new_file(dir
->d_sb
, dentry
, files
->ops
,
189 files
->mode
& mode
, ctx
);
196 spufs_prune_dir(dir
);
200 static int spufs_dir_close(struct inode
*inode
, struct file
*file
)
202 struct spu_context
*ctx
;
203 struct inode
*parent
;
207 dir
= file
->f_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
);
216 /* We have to give up the mm_struct */
219 return dcache_dir_close(inode
, file
);
222 struct inode_operations spufs_dir_inode_operations
= {
223 .lookup
= simple_lookup
,
226 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
,
236 spufs_mkdir(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
,
241 struct spu_context
*ctx
;
244 inode
= spufs_new_inode(dir
->i_sb
, mode
| S_IFDIR
);
248 if (dir
->i_mode
& S_ISGID
) {
249 inode
->i_gid
= dir
->i_gid
;
250 inode
->i_mode
&= S_ISGID
;
252 ctx
= alloc_spu_context(SPUFS_I(dir
)->i_gang
); /* XXX gang */
253 SPUFS_I(inode
)->i_ctx
= ctx
;
259 inode
->i_op
= &spufs_dir_inode_operations
;
260 inode
->i_fop
= &simple_dir_operations
;
261 ret
= spufs_fill_dir(dentry
, spufs_dir_contents
, mode
, ctx
);
265 d_instantiate(dentry
, inode
);
268 dentry
->d_inode
->i_nlink
++;
272 put_spu_context(ctx
);
279 static int spufs_context_open(struct dentry
*dentry
, struct vfsmount
*mnt
)
284 ret
= get_unused_fd();
291 filp
= dentry_open(dentry
, mnt
, O_RDONLY
);
298 filp
->f_op
= &spufs_context_fops
;
299 fd_install(ret
, filp
);
304 static int spufs_create_context(struct inode
*inode
,
305 struct dentry
*dentry
,
306 struct vfsmount
*mnt
, int flags
, int mode
)
310 ret
= spufs_mkdir(inode
, dentry
, flags
, mode
& S_IRWXUGO
);
315 * get references for dget and mntget, will be released
316 * in error path of *_open().
318 ret
= spufs_context_open(dget(dentry
), mntget(mnt
));
320 WARN_ON(spufs_rmdir(inode
, dentry
));
321 mutex_unlock(&inode
->i_mutex
);
322 spu_forget(SPUFS_I(dentry
->d_inode
)->i_ctx
);
327 mutex_unlock(&inode
->i_mutex
);
333 static int spufs_rmgang(struct inode
*root
, struct dentry
*dir
)
335 /* FIXME: this fails if the dir is not empty,
336 which causes a leak of gangs. */
337 return simple_rmdir(root
, dir
);
340 static int spufs_gang_close(struct inode
*inode
, struct file
*file
)
342 struct inode
*parent
;
346 dir
= file
->f_dentry
;
347 parent
= dir
->d_parent
->d_inode
;
349 ret
= spufs_rmgang(parent
, dir
);
352 return dcache_dir_close(inode
, file
);
355 struct file_operations spufs_gang_fops
= {
356 .open
= dcache_dir_open
,
357 .release
= spufs_gang_close
,
358 .llseek
= dcache_dir_lseek
,
359 .read
= generic_read_dir
,
360 .readdir
= dcache_readdir
,
361 .fsync
= simple_sync_file
,
365 spufs_mkgang(struct inode
*dir
, struct dentry
*dentry
, int mode
)
369 struct spu_gang
*gang
;
372 inode
= spufs_new_inode(dir
->i_sb
, mode
| S_IFDIR
);
377 if (dir
->i_mode
& S_ISGID
) {
378 inode
->i_gid
= dir
->i_gid
;
379 inode
->i_mode
&= S_ISGID
;
381 gang
= alloc_spu_gang();
382 SPUFS_I(inode
)->i_ctx
= NULL
;
383 SPUFS_I(inode
)->i_gang
= gang
;
387 inode
->i_op
= &spufs_dir_inode_operations
;
388 inode
->i_fop
= &simple_dir_operations
;
390 d_instantiate(dentry
, inode
);
393 dentry
->d_inode
->i_nlink
++;
402 static int spufs_gang_open(struct dentry
*dentry
, struct vfsmount
*mnt
)
407 ret
= get_unused_fd();
414 filp
= dentry_open(dentry
, mnt
, O_RDONLY
);
421 filp
->f_op
= &spufs_gang_fops
;
422 fd_install(ret
, filp
);
427 static int spufs_create_gang(struct inode
*inode
,
428 struct dentry
*dentry
,
429 struct vfsmount
*mnt
, int mode
)
433 ret
= spufs_mkgang(inode
, dentry
, mode
& S_IRWXUGO
);
438 * get references for dget and mntget, will be released
439 * in error path of *_open().
441 ret
= spufs_gang_open(dget(dentry
), mntget(mnt
));
443 WARN_ON(spufs_rmgang(inode
, dentry
));
446 mutex_unlock(&inode
->i_mutex
);
452 static struct file_system_type spufs_type
;
454 long spufs_create(struct nameidata
*nd
, unsigned int flags
, mode_t mode
)
456 struct dentry
*dentry
;
460 /* check if we are on spufs */
461 if (nd
->dentry
->d_sb
->s_type
!= &spufs_type
)
464 /* don't accept undefined flags */
465 if (flags
& (~SPU_CREATE_FLAG_ALL
))
468 /* only threads can be underneath a gang */
469 if (nd
->dentry
!= nd
->dentry
->d_sb
->s_root
) {
470 if ((flags
& SPU_CREATE_GANG
) ||
471 !SPUFS_I(nd
->dentry
->d_inode
)->i_gang
)
475 dentry
= lookup_create(nd
, 1);
476 ret
= PTR_ERR(dentry
);
484 mode
&= ~current
->fs
->umask
;
486 if (flags
& SPU_CREATE_GANG
)
487 return spufs_create_gang(nd
->dentry
->d_inode
,
488 dentry
, nd
->mnt
, mode
);
490 return spufs_create_context(nd
->dentry
->d_inode
,
491 dentry
, nd
->mnt
, flags
, mode
);
496 mutex_unlock(&nd
->dentry
->d_inode
->i_mutex
);
501 /* File system initialization */
503 Opt_uid
, Opt_gid
, Opt_err
,
506 static match_table_t spufs_tokens
= {
507 { Opt_uid
, "uid=%d" },
508 { Opt_gid
, "gid=%d" },
513 spufs_parse_options(char *options
, struct inode
*root
)
516 substring_t args
[MAX_OPT_ARGS
];
518 while ((p
= strsep(&options
, ",")) != NULL
) {
524 token
= match_token(p
, spufs_tokens
, args
);
527 if (match_int(&args
[0], &option
))
529 root
->i_uid
= option
;
532 if (match_int(&args
[0], &option
))
534 root
->i_gid
= option
;
544 spufs_create_root(struct super_block
*sb
, void *data
)
550 inode
= spufs_new_inode(sb
, S_IFDIR
| 0775);
554 inode
->i_op
= &spufs_dir_inode_operations
;
555 inode
->i_fop
= &simple_dir_operations
;
556 SPUFS_I(inode
)->i_ctx
= NULL
;
559 if (!spufs_parse_options(data
, inode
))
563 sb
->s_root
= d_alloc_root(inode
);
575 spufs_fill_super(struct super_block
*sb
, void *data
, int silent
)
577 static struct super_operations s_ops
= {
578 .alloc_inode
= spufs_alloc_inode
,
579 .destroy_inode
= spufs_destroy_inode
,
580 .statfs
= simple_statfs
,
581 .delete_inode
= spufs_delete_inode
,
582 .drop_inode
= generic_delete_inode
,
585 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
586 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
587 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
588 sb
->s_magic
= SPUFS_MAGIC
;
591 return spufs_create_root(sb
, data
);
595 spufs_get_sb(struct file_system_type
*fstype
, int flags
,
596 const char *name
, void *data
, struct vfsmount
*mnt
)
598 return get_sb_single(fstype
, flags
, data
, spufs_fill_super
, mnt
);
601 static struct file_system_type spufs_type
= {
602 .owner
= THIS_MODULE
,
604 .get_sb
= spufs_get_sb
,
605 .kill_sb
= kill_litter_super
,
608 static int __init
spufs_init(void)
612 spufs_inode_cache
= kmem_cache_create("spufs_inode_cache",
613 sizeof(struct spufs_inode_info
), 0,
614 SLAB_HWCACHE_ALIGN
, spufs_init_once
, NULL
);
616 if (!spufs_inode_cache
)
618 if (spu_sched_init() != 0) {
619 kmem_cache_destroy(spufs_inode_cache
);
622 ret
= register_filesystem(&spufs_type
);
625 ret
= register_spu_syscalls(&spufs_calls
);
630 unregister_filesystem(&spufs_type
);
632 kmem_cache_destroy(spufs_inode_cache
);
636 module_init(spufs_init
);
638 static void __exit
spufs_exit(void)
641 unregister_spu_syscalls(&spufs_calls
);
642 unregister_filesystem(&spufs_type
);
643 kmem_cache_destroy(spufs_inode_cache
);
645 module_exit(spufs_exit
);
647 MODULE_LICENSE("GPL");
648 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");