]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/powerpc/platforms/cell/spufs/inode.c
787ae71a6859c63cdacee241a9168fb5ed669bca
[linux-2.6-omap-h63xx.git] / arch / powerpc / platforms / cell / spufs / inode.c
1 /*
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>
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>
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
43 static kmem_cache_t *spufs_inode_cache;
44
45 static struct inode *
46 spufs_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
54         ei->i_gang = NULL;
55         ei->i_ctx = NULL;
56
57         return &ei->vfs_inode;
58 }
59
60 static void
61 spufs_destroy_inode(struct inode *inode)
62 {
63         kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
64 }
65
66 static void
67 spufs_init_once(void *p, kmem_cache_t * cachep, unsigned long flags)
68 {
69         struct spufs_inode_info *ei = p;
70
71         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
72             SLAB_CTOR_CONSTRUCTOR) {
73                 inode_init_once(&ei->vfs_inode);
74         }
75 }
76
77 static struct inode *
78 spufs_new_inode(struct super_block *sb, int mode)
79 {
80         struct inode *inode;
81
82         inode = new_inode(sb);
83         if (!inode)
84                 goto out;
85
86         inode->i_mode = mode;
87         inode->i_uid = current->fsuid;
88         inode->i_gid = current->fsgid;
89         inode->i_blocks = 0;
90         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
91 out:
92         return inode;
93 }
94
95 static int
96 spufs_setattr(struct dentry *dentry, struct iattr *attr)
97 {
98         struct inode *inode = dentry->d_inode;
99
100         if ((attr->ia_valid & ATTR_SIZE) &&
101             (attr->ia_size != inode->i_size))
102                 return -EINVAL;
103         return inode_setattr(inode, attr);
104 }
105
106
107 static int
108 spufs_new_file(struct super_block *sb, struct dentry *dentry,
109                 const struct file_operations *fops, int mode,
110                 struct spu_context *ctx)
111 {
112         static struct inode_operations spufs_file_iops = {
113                 .setattr = spufs_setattr,
114         };
115         struct inode *inode;
116         int ret;
117
118         ret = -ENOSPC;
119         inode = spufs_new_inode(sb, S_IFREG | mode);
120         if (!inode)
121                 goto out;
122
123         ret = 0;
124         inode->i_op = &spufs_file_iops;
125         inode->i_fop = fops;
126         inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
127         d_add(dentry, inode);
128 out:
129         return ret;
130 }
131
132 static void
133 spufs_delete_inode(struct inode *inode)
134 {
135         struct spufs_inode_info *ei = SPUFS_I(inode);
136
137         if (ei->i_ctx)
138                 put_spu_context(ei->i_ctx);
139         if (ei->i_gang)
140                 put_spu_gang(ei->i_gang);
141         clear_inode(inode);
142 }
143
144 static void spufs_prune_dir(struct dentry *dir)
145 {
146         struct dentry *dentry, *tmp;
147
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) {
153                         dget_locked(dentry);
154                         __d_drop(dentry);
155                         spin_unlock(&dentry->d_lock);
156                         simple_unlink(dir->d_inode, dentry);
157                         spin_unlock(&dcache_lock);
158                         dput(dentry);
159                 } else {
160                         spin_unlock(&dentry->d_lock);
161                         spin_unlock(&dcache_lock);
162                 }
163         }
164         shrink_dcache_parent(dir);
165         mutex_unlock(&dir->d_inode->i_mutex);
166 }
167
168 /* Caller must hold parent->i_mutex */
169 static int spufs_rmdir(struct inode *parent, struct dentry *dir)
170 {
171         /* remove all entries */
172         spufs_prune_dir(dir);
173
174         return simple_rmdir(parent, dir);
175 }
176
177 static int spufs_fill_dir(struct dentry *dir, struct tree_descr *files,
178                           int mode, struct spu_context *ctx)
179 {
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;
195 out:
196         spufs_prune_dir(dir);
197         return ret;
198 }
199
200 static int spufs_dir_close(struct inode *inode, struct file *file)
201 {
202         struct spu_context *ctx;
203         struct inode *parent;
204         struct dentry *dir;
205         int ret;
206
207         dir = file->f_dentry;
208         parent = dir->d_parent->d_inode;
209         ctx = SPUFS_I(dir->d_inode)->i_ctx;
210
211         mutex_lock(&parent->i_mutex);
212         ret = spufs_rmdir(parent, dir);
213         mutex_unlock(&parent->i_mutex);
214         WARN_ON(ret);
215
216         /* We have to give up the mm_struct */
217         spu_forget(ctx);
218
219         return dcache_dir_close(inode, file);
220 }
221
222 struct inode_operations spufs_dir_inode_operations = {
223         .lookup = simple_lookup,
224 };
225
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,
233 };
234
235 static int
236 spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
237                 int mode)
238 {
239         int ret;
240         struct inode *inode;
241         struct spu_context *ctx;
242
243         ret = -ENOSPC;
244         inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
245         if (!inode)
246                 goto out;
247
248         if (dir->i_mode & S_ISGID) {
249                 inode->i_gid = dir->i_gid;
250                 inode->i_mode &= S_ISGID;
251         }
252         ctx = alloc_spu_context(SPUFS_I(dir)->i_gang); /* XXX gang */
253         SPUFS_I(inode)->i_ctx = ctx;
254         if (!ctx)
255                 goto out_iput;
256
257         ctx->flags = flags;
258
259         inode->i_op = &spufs_dir_inode_operations;
260         inode->i_fop = &simple_dir_operations;
261         if (flags & SPU_CREATE_NOSCHED)
262                 ret = spufs_fill_dir(dentry, spufs_dir_nosched_contents,
263                                          mode, ctx);
264         else
265                 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
266
267         if (ret)
268                 goto out_free_ctx;
269
270         d_instantiate(dentry, inode);
271         dget(dentry);
272         dir->i_nlink++;
273         dentry->d_inode->i_nlink++;
274         goto out;
275
276 out_free_ctx:
277         put_spu_context(ctx);
278 out_iput:
279         iput(inode);
280 out:
281         return ret;
282 }
283
284 static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
285 {
286         int ret;
287         struct file *filp;
288
289         ret = get_unused_fd();
290         if (ret < 0) {
291                 dput(dentry);
292                 mntput(mnt);
293                 goto out;
294         }
295
296         filp = dentry_open(dentry, mnt, O_RDONLY);
297         if (IS_ERR(filp)) {
298                 put_unused_fd(ret);
299                 ret = PTR_ERR(filp);
300                 goto out;
301         }
302
303         filp->f_op = &spufs_context_fops;
304         fd_install(ret, filp);
305 out:
306         return ret;
307 }
308
309 static int spufs_create_context(struct inode *inode,
310                         struct dentry *dentry,
311                         struct vfsmount *mnt, int flags, int mode)
312 {
313         int ret;
314
315         ret = -EPERM;
316         if ((flags & SPU_CREATE_NOSCHED) &&
317             !capable(CAP_SYS_NICE))
318                 goto out_unlock;
319
320         ret = -EINVAL;
321         if ((flags & (SPU_CREATE_NOSCHED | SPU_CREATE_ISOLATE))
322             == SPU_CREATE_ISOLATE)
323                 goto out_unlock;
324
325         ret = spufs_mkdir(inode, dentry, flags, mode & S_IRWXUGO);
326         if (ret)
327                 goto out_unlock;
328
329         /*
330          * get references for dget and mntget, will be released
331          * in error path of *_open().
332          */
333         ret = spufs_context_open(dget(dentry), mntget(mnt));
334         if (ret < 0) {
335                 WARN_ON(spufs_rmdir(inode, dentry));
336                 mutex_unlock(&inode->i_mutex);
337                 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
338                 goto out;
339         }
340
341 out_unlock:
342         mutex_unlock(&inode->i_mutex);
343 out:
344         dput(dentry);
345         return ret;
346 }
347
348 static int spufs_rmgang(struct inode *root, struct dentry *dir)
349 {
350         /* FIXME: this fails if the dir is not empty,
351                   which causes a leak of gangs. */
352         return simple_rmdir(root, dir);
353 }
354
355 static int spufs_gang_close(struct inode *inode, struct file *file)
356 {
357         struct inode *parent;
358         struct dentry *dir;
359         int ret;
360
361         dir = file->f_dentry;
362         parent = dir->d_parent->d_inode;
363
364         ret = spufs_rmgang(parent, dir);
365         WARN_ON(ret);
366
367         return dcache_dir_close(inode, file);
368 }
369
370 struct file_operations spufs_gang_fops = {
371         .open           = dcache_dir_open,
372         .release        = spufs_gang_close,
373         .llseek         = dcache_dir_lseek,
374         .read           = generic_read_dir,
375         .readdir        = dcache_readdir,
376         .fsync          = simple_sync_file,
377 };
378
379 static int
380 spufs_mkgang(struct inode *dir, struct dentry *dentry, int mode)
381 {
382         int ret;
383         struct inode *inode;
384         struct spu_gang *gang;
385
386         ret = -ENOSPC;
387         inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
388         if (!inode)
389                 goto out;
390
391         ret = 0;
392         if (dir->i_mode & S_ISGID) {
393                 inode->i_gid = dir->i_gid;
394                 inode->i_mode &= S_ISGID;
395         }
396         gang = alloc_spu_gang();
397         SPUFS_I(inode)->i_ctx = NULL;
398         SPUFS_I(inode)->i_gang = gang;
399         if (!gang)
400                 goto out_iput;
401
402         inode->i_op = &spufs_dir_inode_operations;
403         inode->i_fop = &simple_dir_operations;
404
405         d_instantiate(dentry, inode);
406         dget(dentry);
407         dir->i_nlink++;
408         dentry->d_inode->i_nlink++;
409         return ret;
410
411 out_iput:
412         iput(inode);
413 out:
414         return ret;
415 }
416
417 static int spufs_gang_open(struct dentry *dentry, struct vfsmount *mnt)
418 {
419         int ret;
420         struct file *filp;
421
422         ret = get_unused_fd();
423         if (ret < 0) {
424                 dput(dentry);
425                 mntput(mnt);
426                 goto out;
427         }
428
429         filp = dentry_open(dentry, mnt, O_RDONLY);
430         if (IS_ERR(filp)) {
431                 put_unused_fd(ret);
432                 ret = PTR_ERR(filp);
433                 goto out;
434         }
435
436         filp->f_op = &spufs_gang_fops;
437         fd_install(ret, filp);
438 out:
439         return ret;
440 }
441
442 static int spufs_create_gang(struct inode *inode,
443                         struct dentry *dentry,
444                         struct vfsmount *mnt, int mode)
445 {
446         int ret;
447
448         ret = spufs_mkgang(inode, dentry, mode & S_IRWXUGO);
449         if (ret)
450                 goto out;
451
452         /*
453          * get references for dget and mntget, will be released
454          * in error path of *_open().
455          */
456         ret = spufs_gang_open(dget(dentry), mntget(mnt));
457         if (ret < 0)
458                 WARN_ON(spufs_rmgang(inode, dentry));
459
460 out:
461         mutex_unlock(&inode->i_mutex);
462         dput(dentry);
463         return ret;
464 }
465
466
467 static struct file_system_type spufs_type;
468
469 long spufs_create(struct nameidata *nd, unsigned int flags, mode_t mode)
470 {
471         struct dentry *dentry;
472         int ret;
473
474         ret = -EINVAL;
475         /* check if we are on spufs */
476         if (nd->dentry->d_sb->s_type != &spufs_type)
477                 goto out;
478
479         /* don't accept undefined flags */
480         if (flags & (~SPU_CREATE_FLAG_ALL))
481                 goto out;
482
483         /* only threads can be underneath a gang */
484         if (nd->dentry != nd->dentry->d_sb->s_root) {
485                 if ((flags & SPU_CREATE_GANG) ||
486                     !SPUFS_I(nd->dentry->d_inode)->i_gang)
487                         goto out;
488         }
489
490         dentry = lookup_create(nd, 1);
491         ret = PTR_ERR(dentry);
492         if (IS_ERR(dentry))
493                 goto out_dir;
494
495         ret = -EEXIST;
496         if (dentry->d_inode)
497                 goto out_dput;
498
499         mode &= ~current->fs->umask;
500
501         if (flags & SPU_CREATE_GANG)
502                 return spufs_create_gang(nd->dentry->d_inode,
503                                         dentry, nd->mnt, mode);
504         else
505                 return spufs_create_context(nd->dentry->d_inode,
506                                         dentry, nd->mnt, flags, mode);
507
508 out_dput:
509         dput(dentry);
510 out_dir:
511         mutex_unlock(&nd->dentry->d_inode->i_mutex);
512 out:
513         return ret;
514 }
515
516 /* File system initialization */
517 enum {
518         Opt_uid, Opt_gid, Opt_err,
519 };
520
521 static match_table_t spufs_tokens = {
522         { Opt_uid, "uid=%d" },
523         { Opt_gid, "gid=%d" },
524         { Opt_err, NULL  },
525 };
526
527 static int
528 spufs_parse_options(char *options, struct inode *root)
529 {
530         char *p;
531         substring_t args[MAX_OPT_ARGS];
532
533         while ((p = strsep(&options, ",")) != NULL) {
534                 int token, option;
535
536                 if (!*p)
537                         continue;
538
539                 token = match_token(p, spufs_tokens, args);
540                 switch (token) {
541                 case Opt_uid:
542                         if (match_int(&args[0], &option))
543                                 return 0;
544                         root->i_uid = option;
545                         break;
546                 case Opt_gid:
547                         if (match_int(&args[0], &option))
548                                 return 0;
549                         root->i_gid = option;
550                         break;
551                 default:
552                         return 0;
553                 }
554         }
555         return 1;
556 }
557
558 static int
559 spufs_create_root(struct super_block *sb, void *data)
560 {
561         struct inode *inode;
562         int ret;
563
564         ret = -ENOMEM;
565         inode = spufs_new_inode(sb, S_IFDIR | 0775);
566         if (!inode)
567                 goto out;
568
569         inode->i_op = &spufs_dir_inode_operations;
570         inode->i_fop = &simple_dir_operations;
571         SPUFS_I(inode)->i_ctx = NULL;
572
573         ret = -EINVAL;
574         if (!spufs_parse_options(data, inode))
575                 goto out_iput;
576
577         ret = -ENOMEM;
578         sb->s_root = d_alloc_root(inode);
579         if (!sb->s_root)
580                 goto out_iput;
581
582         return 0;
583 out_iput:
584         iput(inode);
585 out:
586         return ret;
587 }
588
589 static int
590 spufs_fill_super(struct super_block *sb, void *data, int silent)
591 {
592         static struct super_operations s_ops = {
593                 .alloc_inode = spufs_alloc_inode,
594                 .destroy_inode = spufs_destroy_inode,
595                 .statfs = simple_statfs,
596                 .delete_inode = spufs_delete_inode,
597                 .drop_inode = generic_delete_inode,
598         };
599
600         sb->s_maxbytes = MAX_LFS_FILESIZE;
601         sb->s_blocksize = PAGE_CACHE_SIZE;
602         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
603         sb->s_magic = SPUFS_MAGIC;
604         sb->s_op = &s_ops;
605
606         return spufs_create_root(sb, data);
607 }
608
609 static int
610 spufs_get_sb(struct file_system_type *fstype, int flags,
611                 const char *name, void *data, struct vfsmount *mnt)
612 {
613         return get_sb_single(fstype, flags, data, spufs_fill_super, mnt);
614 }
615
616 static struct file_system_type spufs_type = {
617         .owner = THIS_MODULE,
618         .name = "spufs",
619         .get_sb = spufs_get_sb,
620         .kill_sb = kill_litter_super,
621 };
622
623 static int __init spufs_init(void)
624 {
625         int ret;
626         ret = -ENOMEM;
627         spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
628                         sizeof(struct spufs_inode_info), 0,
629                         SLAB_HWCACHE_ALIGN, spufs_init_once, NULL);
630
631         if (!spufs_inode_cache)
632                 goto out;
633         if (spu_sched_init() != 0) {
634                 kmem_cache_destroy(spufs_inode_cache);
635                 goto out;
636         }
637         ret = register_filesystem(&spufs_type);
638         if (ret)
639                 goto out_cache;
640         ret = register_spu_syscalls(&spufs_calls);
641         if (ret)
642                 goto out_fs;
643         return 0;
644 out_fs:
645         unregister_filesystem(&spufs_type);
646 out_cache:
647         kmem_cache_destroy(spufs_inode_cache);
648 out:
649         return ret;
650 }
651 module_init(spufs_init);
652
653 static void __exit spufs_exit(void)
654 {
655         spu_sched_exit();
656         unregister_spu_syscalls(&spufs_calls);
657         unregister_filesystem(&spufs_type);
658         kmem_cache_destroy(spufs_inode_cache);
659 }
660 module_exit(spufs_exit);
661
662 MODULE_LICENSE("GPL");
663 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
664