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);
53 return &ei->vfs_inode;
57 spufs_destroy_inode(struct inode *inode)
59 kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
63 spufs_init_once(void *p, kmem_cache_t * cachep, unsigned long flags)
65 struct spufs_inode_info *ei = p;
67 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
68 SLAB_CTOR_CONSTRUCTOR) {
69 inode_init_once(&ei->vfs_inode);
74 spufs_new_inode(struct super_block *sb, int mode)
78 inode = new_inode(sb);
83 inode->i_uid = current->fsuid;
84 inode->i_gid = current->fsgid;
86 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
92 spufs_setattr(struct dentry *dentry, struct iattr *attr)
94 struct inode *inode = dentry->d_inode;
96 if ((attr->ia_valid & ATTR_SIZE) &&
97 (attr->ia_size != inode->i_size))
99 return inode_setattr(inode, attr);
104 spufs_new_file(struct super_block *sb, struct dentry *dentry,
105 const struct file_operations *fops, int mode,
106 struct spu_context *ctx)
108 static struct inode_operations spufs_file_iops = {
109 .setattr = spufs_setattr,
115 inode = spufs_new_inode(sb, S_IFREG | mode);
120 inode->i_op = &spufs_file_iops;
122 inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
123 d_add(dentry, inode);
129 spufs_delete_inode(struct inode *inode)
131 if (SPUFS_I(inode)->i_ctx)
132 put_spu_context(SPUFS_I(inode)->i_ctx);
136 static void spufs_prune_dir(struct dentry *dir)
138 struct dentry *dentry, *tmp;
139 mutex_lock(&dir->d_inode->i_mutex);
140 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_u.d_child) {
141 spin_lock(&dcache_lock);
142 spin_lock(&dentry->d_lock);
143 if (!(d_unhashed(dentry)) && dentry->d_inode) {
146 spin_unlock(&dentry->d_lock);
147 simple_unlink(dir->d_inode, dentry);
148 spin_unlock(&dcache_lock);
151 spin_unlock(&dentry->d_lock);
152 spin_unlock(&dcache_lock);
155 shrink_dcache_parent(dir);
156 mutex_unlock(&dir->d_inode->i_mutex);
159 /* Caller must hold root->i_mutex */
160 static int spufs_rmdir(struct inode *root, struct dentry *dir_dentry)
162 /* remove all entries */
163 spufs_prune_dir(dir_dentry);
165 return simple_rmdir(root, dir_dentry);
168 static int spufs_fill_dir(struct dentry *dir, struct tree_descr *files,
169 int mode, struct spu_context *ctx)
171 struct dentry *dentry;
174 while (files->name && files->name[0]) {
176 dentry = d_alloc_name(dir, files->name);
179 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
180 files->mode & mode, ctx);
187 spufs_prune_dir(dir);
191 static int spufs_dir_close(struct inode *inode, struct file *file)
193 struct spu_context *ctx;
195 struct dentry *dentry;
198 dentry = file->f_dentry;
199 dir = dentry->d_parent->d_inode;
200 ctx = SPUFS_I(dentry->d_inode)->i_ctx;
202 mutex_lock(&dir->i_mutex);
203 ret = spufs_rmdir(dir, dentry);
204 mutex_unlock(&dir->i_mutex);
207 /* We have to give up the mm_struct */
210 return dcache_dir_close(inode, file);
213 struct inode_operations spufs_dir_inode_operations = {
214 .lookup = simple_lookup,
217 struct file_operations spufs_context_fops = {
218 .open = dcache_dir_open,
219 .release = spufs_dir_close,
220 .llseek = dcache_dir_lseek,
221 .read = generic_read_dir,
222 .readdir = dcache_readdir,
223 .fsync = simple_sync_file,
227 spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
232 struct spu_context *ctx;
235 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
239 if (dir->i_mode & S_ISGID) {
240 inode->i_gid = dir->i_gid;
241 inode->i_mode &= S_ISGID;
243 ctx = alloc_spu_context();
244 SPUFS_I(inode)->i_ctx = ctx;
250 inode->i_op = &spufs_dir_inode_operations;
251 inode->i_fop = &simple_dir_operations;
252 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
256 d_instantiate(dentry, inode);
259 dentry->d_inode->i_nlink++;
263 put_spu_context(ctx);
270 static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
275 ret = get_unused_fd();
282 filp = dentry_open(dentry, mnt, O_RDONLY);
289 filp->f_op = &spufs_context_fops;
290 fd_install(ret, filp);
295 static struct file_system_type spufs_type;
297 long spufs_create_thread(struct nameidata *nd,
298 unsigned int flags, mode_t mode)
300 struct dentry *dentry;
303 /* need to be at the root of spufs */
305 if (nd->dentry->d_sb->s_type != &spufs_type ||
306 nd->dentry != nd->dentry->d_sb->s_root)
309 /* all flags are reserved */
310 if (flags & (~SPU_CREATE_FLAG_ALL))
313 dentry = lookup_create(nd, 1);
314 ret = PTR_ERR(dentry);
322 mode &= ~current->fs->umask;
323 ret = spufs_mkdir(nd->dentry->d_inode, dentry, flags, mode & S_IRWXUGO);
328 * get references for dget and mntget, will be released
329 * in error path of *_open().
331 ret = spufs_context_open(dget(dentry), mntget(nd->mnt));
333 WARN_ON(spufs_rmdir(nd->dentry->d_inode, dentry));
334 mutex_unlock(&nd->dentry->d_inode->i_mutex);
335 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
343 mutex_unlock(&nd->dentry->d_inode->i_mutex);
348 /* File system initialization */
350 Opt_uid, Opt_gid, Opt_err,
353 static match_table_t spufs_tokens = {
354 { Opt_uid, "uid=%d" },
355 { Opt_gid, "gid=%d" },
360 spufs_parse_options(char *options, struct inode *root)
363 substring_t args[MAX_OPT_ARGS];
365 while ((p = strsep(&options, ",")) != NULL) {
371 token = match_token(p, spufs_tokens, args);
374 if (match_int(&args[0], &option))
376 root->i_uid = option;
379 if (match_int(&args[0], &option))
381 root->i_gid = option;
391 spufs_create_root(struct super_block *sb, void *data)
397 inode = spufs_new_inode(sb, S_IFDIR | 0775);
401 inode->i_op = &spufs_dir_inode_operations;
402 inode->i_fop = &simple_dir_operations;
403 SPUFS_I(inode)->i_ctx = NULL;
406 if (!spufs_parse_options(data, inode))
410 sb->s_root = d_alloc_root(inode);
422 spufs_fill_super(struct super_block *sb, void *data, int silent)
424 static struct super_operations s_ops = {
425 .alloc_inode = spufs_alloc_inode,
426 .destroy_inode = spufs_destroy_inode,
427 .statfs = simple_statfs,
428 .delete_inode = spufs_delete_inode,
429 .drop_inode = generic_delete_inode,
432 sb->s_maxbytes = MAX_LFS_FILESIZE;
433 sb->s_blocksize = PAGE_CACHE_SIZE;
434 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
435 sb->s_magic = SPUFS_MAGIC;
438 return spufs_create_root(sb, data);
442 spufs_get_sb(struct file_system_type *fstype, int flags,
443 const char *name, void *data, struct vfsmount *mnt)
445 return get_sb_single(fstype, flags, data, spufs_fill_super, mnt);
448 static struct file_system_type spufs_type = {
449 .owner = THIS_MODULE,
451 .get_sb = spufs_get_sb,
452 .kill_sb = kill_litter_super,
455 static int __init spufs_init(void)
459 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
460 sizeof(struct spufs_inode_info), 0,
461 SLAB_HWCACHE_ALIGN, spufs_init_once, NULL);
463 if (!spufs_inode_cache)
465 if (spu_sched_init() != 0) {
466 kmem_cache_destroy(spufs_inode_cache);
469 ret = register_filesystem(&spufs_type);
472 ret = register_spu_syscalls(&spufs_calls);
477 unregister_filesystem(&spufs_type);
479 kmem_cache_destroy(spufs_inode_cache);
483 module_init(spufs_init);
485 static void __exit spufs_exit(void)
488 unregister_spu_syscalls(&spufs_calls);
489 unregister_filesystem(&spufs_type);
490 kmem_cache_destroy(spufs_inode_cache);
492 module_exit(spufs_exit);
494 MODULE_LICENSE("GPL");
495 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");