blob: 687f80d09f416f4bd40e4b51bff3d0a7ee95f7b7 [file] [log] [blame]
Arnd Bergmann67207b92005-11-15 15:53:48 -05001/*
2 * SPU file system
3 *
4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
5 *
6 * Author: Arnd Bergmann <arndb@de.ibm.com>
7 *
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)
11 * any later version.
12 *
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.
17 *
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.
21 */
22
23#include <linux/file.h>
24#include <linux/fs.h>
25#include <linux/backing-dev.h>
26#include <linux/init.h>
27#include <linux/ioctl.h>
28#include <linux/module.h>
Arnd Bergmann346f4d32006-01-04 20:31:26 +010029#include <linux/mount.h>
Arnd Bergmann67207b92005-11-15 15:53:48 -050030#include <linux/namei.h>
31#include <linux/pagemap.h>
32#include <linux/poll.h>
33#include <linux/slab.h>
34#include <linux/parser.h>
35
36#include <asm/io.h>
37#include <asm/semaphore.h>
38#include <asm/spu.h>
39#include <asm/uaccess.h>
40
41#include "spufs.h"
42
43static kmem_cache_t *spufs_inode_cache;
44
Arnd Bergmann67207b92005-11-15 15:53:48 -050045static struct inode *
46spufs_alloc_inode(struct super_block *sb)
47{
48 struct spufs_inode_info *ei;
49
50 ei = kmem_cache_alloc(spufs_inode_cache, SLAB_KERNEL);
51 if (!ei)
52 return NULL;
53 return &ei->vfs_inode;
54}
55
56static void
57spufs_destroy_inode(struct inode *inode)
58{
59 kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
60}
61
62static void
63spufs_init_once(void *p, kmem_cache_t * cachep, unsigned long flags)
64{
65 struct spufs_inode_info *ei = p;
66
67 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
68 SLAB_CTOR_CONSTRUCTOR) {
69 inode_init_once(&ei->vfs_inode);
70 }
71}
72
73static struct inode *
74spufs_new_inode(struct super_block *sb, int mode)
75{
76 struct inode *inode;
77
78 inode = new_inode(sb);
79 if (!inode)
80 goto out;
81
82 inode->i_mode = mode;
83 inode->i_uid = current->fsuid;
84 inode->i_gid = current->fsgid;
85 inode->i_blksize = PAGE_CACHE_SIZE;
86 inode->i_blocks = 0;
87 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
88out:
89 return inode;
90}
91
92static int
93spufs_setattr(struct dentry *dentry, struct iattr *attr)
94{
95 struct inode *inode = dentry->d_inode;
96
Arnd Bergmann67207b92005-11-15 15:53:48 -050097 if ((attr->ia_valid & ATTR_SIZE) &&
98 (attr->ia_size != inode->i_size))
99 return -EINVAL;
100 return inode_setattr(inode, attr);
101}
102
103
104static int
105spufs_new_file(struct super_block *sb, struct dentry *dentry,
106 struct file_operations *fops, int mode,
107 struct spu_context *ctx)
108{
109 static struct inode_operations spufs_file_iops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500110 .setattr = spufs_setattr,
Arnd Bergmann67207b92005-11-15 15:53:48 -0500111 };
112 struct inode *inode;
113 int ret;
114
115 ret = -ENOSPC;
116 inode = spufs_new_inode(sb, S_IFREG | mode);
117 if (!inode)
118 goto out;
119
120 ret = 0;
121 inode->i_op = &spufs_file_iops;
122 inode->i_fop = fops;
123 inode->u.generic_ip = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
124 d_add(dentry, inode);
125out:
126 return ret;
127}
128
129static void
130spufs_delete_inode(struct inode *inode)
131{
132 if (SPUFS_I(inode)->i_ctx)
133 put_spu_context(SPUFS_I(inode)->i_ctx);
134 clear_inode(inode);
135}
136
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100137static void spufs_prune_dir(struct dentry *dir)
138{
139 struct dentry *dentry, *tmp;
140 down(&dir->d_inode->i_sem);
141 list_for_each_entry_safe(dentry, tmp, &dir->d_subdirs, d_child) {
142 spin_lock(&dcache_lock);
143 spin_lock(&dentry->d_lock);
144 if (!(d_unhashed(dentry)) && dentry->d_inode) {
145 dget_locked(dentry);
146 __d_drop(dentry);
147 spin_unlock(&dentry->d_lock);
148 simple_unlink(dir->d_inode, dentry);
149 spin_unlock(&dcache_lock);
150 dput(dentry);
151 } else {
152 spin_unlock(&dentry->d_lock);
153 spin_unlock(&dcache_lock);
154 }
155 }
156 shrink_dcache_parent(dir);
157 up(&dir->d_inode->i_sem);
158}
159
160static int spufs_rmdir(struct inode *root, struct dentry *dir_dentry)
161{
162 struct spu_context *ctx;
163
164 /* remove all entries */
165 down(&root->i_sem);
166 spufs_prune_dir(dir_dentry);
167 up(&root->i_sem);
168
169 /* We have to give up the mm_struct */
170 ctx = SPUFS_I(dir_dentry->d_inode)->i_ctx;
171 spu_forget(ctx);
172
173 /* XXX Do we need to hold i_sem here ? */
174 return simple_rmdir(root, dir_dentry);
175}
176
177static int spufs_fill_dir(struct dentry *dir, struct tree_descr *files,
178 int mode, struct spu_context *ctx)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500179{
180 struct dentry *dentry;
181 int ret;
182
183 while (files->name && files->name[0]) {
184 ret = -ENOMEM;
185 dentry = d_alloc_name(dir, files->name);
186 if (!dentry)
187 goto out;
188 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
189 files->mode & mode, ctx);
190 if (ret)
191 goto out;
192 files++;
193 }
194 return 0;
195out:
Arnd Bergmann3f51dd92006-01-04 20:31:27 +0100196 spufs_prune_dir(dir);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500197 return ret;
198}
199
Arnd Bergmann67207b92005-11-15 15:53:48 -0500200static int spufs_dir_close(struct inode *inode, struct file *file)
201{
202 struct inode *dir;
203 struct dentry *dentry;
204 int ret;
205
206 dentry = file->f_dentry;
207 dir = dentry->d_parent->d_inode;
Arnd Bergmannc8ca0632006-01-04 20:31:22 +0100208
209 ret = spufs_rmdir(dir, dentry);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500210 WARN_ON(ret);
Arnd Bergmannc8ca0632006-01-04 20:31:22 +0100211
Arnd Bergmann67207b92005-11-15 15:53:48 -0500212 return dcache_dir_close(inode, file);
213}
214
215struct inode_operations spufs_dir_inode_operations = {
216 .lookup = simple_lookup,
217};
218
Arnd Bergmanne80358a2006-01-04 20:31:23 +0100219struct file_operations spufs_context_fops = {
Arnd Bergmann67207b92005-11-15 15:53:48 -0500220 .open = dcache_dir_open,
221 .release = spufs_dir_close,
222 .llseek = dcache_dir_lseek,
223 .read = generic_read_dir,
224 .readdir = dcache_readdir,
225 .fsync = simple_sync_file,
226};
227
228static int
229spufs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
230{
231 int ret;
232 struct inode *inode;
233 struct spu_context *ctx;
234
235 ret = -ENOSPC;
236 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
237 if (!inode)
238 goto out;
239
240 if (dir->i_mode & S_ISGID) {
241 inode->i_gid = dir->i_gid;
242 inode->i_mode &= S_ISGID;
243 }
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500244 ctx = alloc_spu_context(inode->i_mapping);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500245 SPUFS_I(inode)->i_ctx = ctx;
246 if (!ctx)
247 goto out_iput;
248
249 inode->i_op = &spufs_dir_inode_operations;
250 inode->i_fop = &simple_dir_operations;
251 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
252 if (ret)
253 goto out_free_ctx;
254
255 d_instantiate(dentry, inode);
256 dget(dentry);
257 dir->i_nlink++;
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100258 dentry->d_inode->i_nlink++;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500259 goto out;
260
261out_free_ctx:
262 put_spu_context(ctx);
263out_iput:
264 iput(inode);
265out:
266 return ret;
267}
268
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100269static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
270{
271 int ret;
272 struct file *filp;
273
274 ret = get_unused_fd();
275 if (ret < 0) {
276 dput(dentry);
277 mntput(mnt);
278 goto out;
279 }
280
281 filp = dentry_open(dentry, mnt, O_RDONLY);
282 if (IS_ERR(filp)) {
283 put_unused_fd(ret);
284 ret = PTR_ERR(filp);
285 goto out;
286 }
287
288 filp->f_op = &spufs_context_fops;
289 fd_install(ret, filp);
290out:
291 return ret;
292}
293
294static struct file_system_type spufs_type;
295
Arnd Bergmann67207b92005-11-15 15:53:48 -0500296long
297spufs_create_thread(struct nameidata *nd, const char *name,
298 unsigned int flags, mode_t mode)
299{
300 struct dentry *dentry;
Arnd Bergmann67207b92005-11-15 15:53:48 -0500301 int ret;
302
303 /* need to be at the root of spufs */
304 ret = -EINVAL;
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100305 if (nd->dentry->d_sb->s_type != &spufs_type ||
306 nd->dentry != nd->dentry->d_sb->s_root)
Arnd Bergmann67207b92005-11-15 15:53:48 -0500307 goto out;
308
309 dentry = lookup_create(nd, 1);
310 ret = PTR_ERR(dentry);
311 if (IS_ERR(dentry))
312 goto out_dir;
313
314 ret = -EEXIST;
315 if (dentry->d_inode)
316 goto out_dput;
317
318 mode &= ~current->fs->umask;
319 ret = spufs_mkdir(nd->dentry->d_inode, dentry, mode & S_IRWXUGO);
320 if (ret)
321 goto out_dput;
322
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100323 /*
324 * get references for dget and mntget, will be released
325 * in error path of *_open().
326 */
327 ret = spufs_context_open(dget(dentry), mntget(nd->mnt));
Arnd Bergmann67207b92005-11-15 15:53:48 -0500328 if (ret < 0)
Arnd Bergmann346f4d32006-01-04 20:31:26 +0100329 spufs_rmdir(nd->dentry->d_inode, dentry);
Arnd Bergmann67207b92005-11-15 15:53:48 -0500330
331out_dput:
332 dput(dentry);
333out_dir:
334 up(&nd->dentry->d_inode->i_sem);
335out:
336 return ret;
337}
338
339/* File system initialization */
340enum {
341 Opt_uid, Opt_gid, Opt_err,
342};
343
344static match_table_t spufs_tokens = {
345 { Opt_uid, "uid=%d" },
346 { Opt_gid, "gid=%d" },
347 { Opt_err, NULL },
348};
349
350static int
351spufs_parse_options(char *options, struct inode *root)
352{
353 char *p;
354 substring_t args[MAX_OPT_ARGS];
355
356 while ((p = strsep(&options, ",")) != NULL) {
357 int token, option;
358
359 if (!*p)
360 continue;
361
362 token = match_token(p, spufs_tokens, args);
363 switch (token) {
364 case Opt_uid:
365 if (match_int(&args[0], &option))
366 return 0;
367 root->i_uid = option;
368 break;
369 case Opt_gid:
370 if (match_int(&args[0], &option))
371 return 0;
372 root->i_gid = option;
373 break;
374 default:
375 return 0;
376 }
377 }
378 return 1;
379}
380
381static int
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500382spufs_create_root(struct super_block *sb, void *data)
383{
Arnd Bergmann67207b92005-11-15 15:53:48 -0500384 struct inode *inode;
385 int ret;
386
387 ret = -ENOMEM;
388 inode = spufs_new_inode(sb, S_IFDIR | 0775);
389 if (!inode)
390 goto out;
391
392 inode->i_op = &spufs_dir_inode_operations;
393 inode->i_fop = &simple_dir_operations;
394 SPUFS_I(inode)->i_ctx = NULL;
395
396 ret = -EINVAL;
397 if (!spufs_parse_options(data, inode))
398 goto out_iput;
399
400 ret = -ENOMEM;
401 sb->s_root = d_alloc_root(inode);
402 if (!sb->s_root)
403 goto out_iput;
404
405 return 0;
406out_iput:
407 iput(inode);
408out:
409 return ret;
410}
411
412static int
413spufs_fill_super(struct super_block *sb, void *data, int silent)
414{
415 static struct super_operations s_ops = {
416 .alloc_inode = spufs_alloc_inode,
417 .destroy_inode = spufs_destroy_inode,
418 .statfs = simple_statfs,
419 .delete_inode = spufs_delete_inode,
420 .drop_inode = generic_delete_inode,
421 };
422
423 sb->s_maxbytes = MAX_LFS_FILESIZE;
424 sb->s_blocksize = PAGE_CACHE_SIZE;
425 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
426 sb->s_magic = SPUFS_MAGIC;
427 sb->s_op = &s_ops;
428
429 return spufs_create_root(sb, data);
430}
431
432static struct super_block *
433spufs_get_sb(struct file_system_type *fstype, int flags,
434 const char *name, void *data)
435{
436 return get_sb_single(fstype, flags, data, spufs_fill_super);
437}
438
439static struct file_system_type spufs_type = {
440 .owner = THIS_MODULE,
441 .name = "spufs",
442 .get_sb = spufs_get_sb,
443 .kill_sb = kill_litter_super,
444};
445
446static int spufs_init(void)
447{
448 int ret;
449 ret = -ENOMEM;
450 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
451 sizeof(struct spufs_inode_info), 0,
452 SLAB_HWCACHE_ALIGN, spufs_init_once, NULL);
453
454 if (!spufs_inode_cache)
455 goto out;
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500456 if (spu_sched_init() != 0) {
457 kmem_cache_destroy(spufs_inode_cache);
458 goto out;
459 }
Arnd Bergmann67207b92005-11-15 15:53:48 -0500460 ret = register_filesystem(&spufs_type);
461 if (ret)
462 goto out_cache;
463 ret = register_spu_syscalls(&spufs_calls);
464 if (ret)
465 goto out_fs;
466 return 0;
467out_fs:
468 unregister_filesystem(&spufs_type);
469out_cache:
470 kmem_cache_destroy(spufs_inode_cache);
471out:
472 return ret;
473}
474module_init(spufs_init);
475
476static void spufs_exit(void)
477{
Arnd Bergmann8b3d6662005-11-15 15:53:52 -0500478 spu_sched_exit();
Arnd Bergmann67207b92005-11-15 15:53:48 -0500479 unregister_spu_syscalls(&spufs_calls);
480 unregister_filesystem(&spufs_type);
481 kmem_cache_destroy(spufs_inode_cache);
482}
483module_exit(spufs_exit);
484
485MODULE_LICENSE("GPL");
486MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
487