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/namei.h>
30 #include <linux/pagemap.h>
31 #include <linux/poll.h>
32 #include <linux/slab.h>
33 #include <linux/parser.h>
36 #include <asm/semaphore.h>
38 #include <asm/uaccess.h>
42 static kmem_cache_t
*spufs_inode_cache
;
44 /* Information about the backing dev, same as ramfs */
46 static struct backing_dev_info spufs_backing_dev_info
= {
47 .ra_pages
= 0, /* No readahead */
48 .capabilities
= BDI_CAP_NO_ACCT_DIRTY
| BDI_CAP_NO_WRITEBACK
|
49 BDI_CAP_MAP_DIRECT
| BDI_CAP_MAP_COPY
| BDI_CAP_READ_MAP
|
53 static struct address_space_operations spufs_aops
= {
54 .readpage
= simple_readpage
,
55 .prepare_write
= simple_prepare_write
,
56 .commit_write
= simple_commit_write
,
60 /* Inode operations */
63 spufs_alloc_inode(struct super_block
*sb
)
65 struct spufs_inode_info
*ei
;
67 ei
= kmem_cache_alloc(spufs_inode_cache
, SLAB_KERNEL
);
70 return &ei
->vfs_inode
;
74 spufs_destroy_inode(struct inode
*inode
)
76 kmem_cache_free(spufs_inode_cache
, SPUFS_I(inode
));
80 spufs_init_once(void *p
, kmem_cache_t
* cachep
, unsigned long flags
)
82 struct spufs_inode_info
*ei
= p
;
84 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
85 SLAB_CTOR_CONSTRUCTOR
) {
86 inode_init_once(&ei
->vfs_inode
);
91 spufs_new_inode(struct super_block
*sb
, int mode
)
95 inode
= new_inode(sb
);
100 inode
->i_uid
= current
->fsuid
;
101 inode
->i_gid
= current
->fsgid
;
102 inode
->i_blksize
= PAGE_CACHE_SIZE
;
104 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
110 spufs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
112 struct inode
*inode
= dentry
->d_inode
;
115 pr_debug("ia_size %lld, i_size:%lld\n", attr->ia_size, inode->i_size);
117 if ((attr
->ia_valid
& ATTR_SIZE
) &&
118 (attr
->ia_size
!= inode
->i_size
))
120 return inode_setattr(inode
, attr
);
125 spufs_new_file(struct super_block
*sb
, struct dentry
*dentry
,
126 struct file_operations
*fops
, int mode
,
127 struct spu_context
*ctx
)
129 static struct inode_operations spufs_file_iops
= {
130 .getattr
= simple_getattr
,
131 .setattr
= spufs_setattr
,
132 .unlink
= simple_unlink
,
138 inode
= spufs_new_inode(sb
, S_IFREG
| mode
);
143 inode
->i_op
= &spufs_file_iops
;
145 inode
->u
.generic_ip
= SPUFS_I(inode
)->i_ctx
= get_spu_context(ctx
);
146 d_add(dentry
, inode
);
152 spufs_delete_inode(struct inode
*inode
)
154 if (SPUFS_I(inode
)->i_ctx
)
155 put_spu_context(SPUFS_I(inode
)->i_ctx
);
160 spufs_fill_dir(struct dentry
*dir
, struct tree_descr
*files
,
161 int mode
, struct spu_context
*ctx
)
163 struct dentry
*dentry
;
166 while (files
->name
&& files
->name
[0]) {
168 dentry
= d_alloc_name(dir
, files
->name
);
171 ret
= spufs_new_file(dir
->d_sb
, dentry
, files
->ops
,
172 files
->mode
& mode
, ctx
);
179 // FIXME: remove all files that are left
184 static int spufs_rmdir(struct inode
*root
, struct dentry
*dir_dentry
)
186 struct dentry
*dentry
;
189 spin_lock(&dcache_lock
);
190 /* remove all entries */
192 list_for_each_entry(dentry
, &dir_dentry
->d_subdirs
, d_child
) {
193 if (d_unhashed(dentry
) || !dentry
->d_inode
)
195 atomic_dec(&dentry
->d_count
);
196 spin_lock(&dentry
->d_lock
);
198 spin_unlock(&dentry
->d_lock
);
200 spin_unlock(&dcache_lock
);
202 shrink_dcache_parent(dir_dentry
);
203 err
= simple_rmdir(root
, dir_dentry
);
208 static int spufs_dir_close(struct inode
*inode
, struct file
*file
)
211 struct dentry
*dentry
;
214 dentry
= file
->f_dentry
;
215 dir
= dentry
->d_parent
->d_inode
;
217 ret
= spufs_rmdir(dir
, file
->f_dentry
);
220 return dcache_dir_close(inode
, file
);
223 struct inode_operations spufs_dir_inode_operations
= {
224 .lookup
= simple_lookup
,
227 struct file_operations spufs_autodelete_dir_operations
= {
228 .open
= dcache_dir_open
,
229 .release
= spufs_dir_close
,
230 .llseek
= dcache_dir_lseek
,
231 .read
= generic_read_dir
,
232 .readdir
= dcache_readdir
,
233 .fsync
= simple_sync_file
,
237 spufs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
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();
253 SPUFS_I(inode
)->i_ctx
= ctx
;
257 inode
->i_op
= &spufs_dir_inode_operations
;
258 inode
->i_fop
= &simple_dir_operations
;
259 ret
= spufs_fill_dir(dentry
, spufs_dir_contents
, mode
, ctx
);
263 d_instantiate(dentry
, inode
);
269 put_spu_context(ctx
);
277 spufs_create_thread(struct nameidata
*nd
, const char *name
,
278 unsigned int flags
, mode_t mode
)
280 struct dentry
*dentry
;
284 /* need to be at the root of spufs */
286 if (nd
->dentry
->d_sb
->s_magic
!= SPUFS_MAGIC
||
287 nd
->dentry
!= nd
->dentry
->d_sb
->s_root
)
290 dentry
= lookup_create(nd
, 1);
291 ret
= PTR_ERR(dentry
);
299 mode
&= ~current
->fs
->umask
;
300 ret
= spufs_mkdir(nd
->dentry
->d_inode
, dentry
, mode
& S_IRWXUGO
);
304 ret
= get_unused_fd();
308 dentry
->d_inode
->i_nlink
++;
310 filp
= filp_open(name
, O_RDONLY
, mode
);
312 // FIXME: remove directory again
316 filp
->f_op
= &spufs_autodelete_dir_operations
;
317 fd_install(ret
, filp
);
323 up(&nd
->dentry
->d_inode
->i_sem
);
328 /* File system initialization */
330 Opt_uid
, Opt_gid
, Opt_err
,
333 static match_table_t spufs_tokens
= {
334 { Opt_uid
, "uid=%d" },
335 { Opt_gid
, "gid=%d" },
340 spufs_parse_options(char *options
, struct inode
*root
)
343 substring_t args
[MAX_OPT_ARGS
];
345 while ((p
= strsep(&options
, ",")) != NULL
) {
351 token
= match_token(p
, spufs_tokens
, args
);
354 if (match_int(&args
[0], &option
))
356 root
->i_uid
= option
;
359 if (match_int(&args
[0], &option
))
361 root
->i_gid
= option
;
371 spufs_create_root(struct super_block
*sb
, void *data
) {
376 inode
= spufs_new_inode(sb
, S_IFDIR
| 0775);
380 inode
->i_op
= &spufs_dir_inode_operations
;
381 inode
->i_fop
= &simple_dir_operations
;
382 SPUFS_I(inode
)->i_ctx
= NULL
;
385 if (!spufs_parse_options(data
, inode
))
389 sb
->s_root
= d_alloc_root(inode
);
401 spufs_fill_super(struct super_block
*sb
, void *data
, int silent
)
403 static struct super_operations s_ops
= {
404 .alloc_inode
= spufs_alloc_inode
,
405 .destroy_inode
= spufs_destroy_inode
,
406 .statfs
= simple_statfs
,
407 .delete_inode
= spufs_delete_inode
,
408 .drop_inode
= generic_delete_inode
,
411 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
412 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
413 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
414 sb
->s_magic
= SPUFS_MAGIC
;
417 return spufs_create_root(sb
, data
);
420 static struct super_block
*
421 spufs_get_sb(struct file_system_type
*fstype
, int flags
,
422 const char *name
, void *data
)
424 return get_sb_single(fstype
, flags
, data
, spufs_fill_super
);
427 static struct file_system_type spufs_type
= {
428 .owner
= THIS_MODULE
,
430 .get_sb
= spufs_get_sb
,
431 .kill_sb
= kill_litter_super
,
434 static int spufs_init(void)
438 spufs_inode_cache
= kmem_cache_create("spufs_inode_cache",
439 sizeof(struct spufs_inode_info
), 0,
440 SLAB_HWCACHE_ALIGN
, spufs_init_once
, NULL
);
442 if (!spufs_inode_cache
)
444 ret
= register_filesystem(&spufs_type
);
447 ret
= register_spu_syscalls(&spufs_calls
);
452 unregister_filesystem(&spufs_type
);
454 kmem_cache_destroy(spufs_inode_cache
);
458 module_init(spufs_init
);
460 static void spufs_exit(void)
462 unregister_spu_syscalls(&spufs_calls
);
463 unregister_filesystem(&spufs_type
);
464 kmem_cache_destroy(spufs_inode_cache
);
466 module_exit(spufs_exit
);
468 MODULE_LICENSE("GPL");
469 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");