Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[pandora-kernel.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         return &ei->vfs_inode;
54 }
55
56 static void
57 spufs_destroy_inode(struct inode *inode)
58 {
59         kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
60 }
61
62 static void
63 spufs_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
73 static struct inode *
74 spufs_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_blocks = 0;
86         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
87 out:
88         return inode;
89 }
90
91 static int
92 spufs_setattr(struct dentry *dentry, struct iattr *attr)
93 {
94         struct inode *inode = dentry->d_inode;
95
96         if ((attr->ia_valid & ATTR_SIZE) &&
97             (attr->ia_size != inode->i_size))
98                 return -EINVAL;
99         return inode_setattr(inode, attr);
100 }
101
102
103 static int
104 spufs_new_file(struct super_block *sb, struct dentry *dentry,
105                 const struct file_operations *fops, int mode,
106                 struct spu_context *ctx)
107 {
108         static struct inode_operations spufs_file_iops = {
109                 .setattr = spufs_setattr,
110         };
111         struct inode *inode;
112         int ret;
113
114         ret = -ENOSPC;
115         inode = spufs_new_inode(sb, S_IFREG | mode);
116         if (!inode)
117                 goto out;
118
119         ret = 0;
120         inode->i_op = &spufs_file_iops;
121         inode->i_fop = fops;
122         inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
123         d_add(dentry, inode);
124 out:
125         return ret;
126 }
127
128 static void
129 spufs_delete_inode(struct inode *inode)
130 {
131         if (SPUFS_I(inode)->i_ctx)
132                 put_spu_context(SPUFS_I(inode)->i_ctx);
133         clear_inode(inode);
134 }
135
136 static void spufs_prune_dir(struct dentry *dir)
137 {
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) {
144                         dget_locked(dentry);
145                         __d_drop(dentry);
146                         spin_unlock(&dentry->d_lock);
147                         simple_unlink(dir->d_inode, dentry);
148                         spin_unlock(&dcache_lock);
149                         dput(dentry);
150                 } else {
151                         spin_unlock(&dentry->d_lock);
152                         spin_unlock(&dcache_lock);
153                 }
154         }
155         shrink_dcache_parent(dir);
156         mutex_unlock(&dir->d_inode->i_mutex);
157 }
158
159 /* Caller must hold root->i_mutex */
160 static int spufs_rmdir(struct inode *root, struct dentry *dir_dentry)
161 {
162         /* remove all entries */
163         spufs_prune_dir(dir_dentry);
164
165         return simple_rmdir(root, dir_dentry);
166 }
167
168 static int spufs_fill_dir(struct dentry *dir, struct tree_descr *files,
169                           int mode, struct spu_context *ctx)
170 {
171         struct dentry *dentry;
172         int ret;
173
174         while (files->name && files->name[0]) {
175                 ret = -ENOMEM;
176                 dentry = d_alloc_name(dir, files->name);
177                 if (!dentry)
178                         goto out;
179                 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
180                                         files->mode & mode, ctx);
181                 if (ret)
182                         goto out;
183                 files++;
184         }
185         return 0;
186 out:
187         spufs_prune_dir(dir);
188         return ret;
189 }
190
191 static int spufs_dir_close(struct inode *inode, struct file *file)
192 {
193         struct spu_context *ctx;
194         struct inode *dir;
195         struct dentry *dentry;
196         int ret;
197
198         dentry = file->f_dentry;
199         dir = dentry->d_parent->d_inode;
200         ctx = SPUFS_I(dentry->d_inode)->i_ctx;
201
202         mutex_lock(&dir->i_mutex);
203         ret = spufs_rmdir(dir, dentry);
204         mutex_unlock(&dir->i_mutex);
205         WARN_ON(ret);
206
207         /* We have to give up the mm_struct */
208         spu_forget(ctx);
209
210         return dcache_dir_close(inode, file);
211 }
212
213 struct inode_operations spufs_dir_inode_operations = {
214         .lookup = simple_lookup,
215 };
216
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,
224 };
225
226 static int
227 spufs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
228 {
229         int ret;
230         struct inode *inode;
231         struct spu_context *ctx;
232
233         ret = -ENOSPC;
234         inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
235         if (!inode)
236                 goto out;
237
238         if (dir->i_mode & S_ISGID) {
239                 inode->i_gid = dir->i_gid;
240                 inode->i_mode &= S_ISGID;
241         }
242         ctx = alloc_spu_context();
243         SPUFS_I(inode)->i_ctx = ctx;
244         if (!ctx)
245                 goto out_iput;
246
247         inode->i_op = &spufs_dir_inode_operations;
248         inode->i_fop = &simple_dir_operations;
249         ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
250         if (ret)
251                 goto out_free_ctx;
252
253         d_instantiate(dentry, inode);
254         dget(dentry);
255         dir->i_nlink++;
256         dentry->d_inode->i_nlink++;
257         goto out;
258
259 out_free_ctx:
260         put_spu_context(ctx);
261 out_iput:
262         iput(inode);
263 out:
264         return ret;
265 }
266
267 static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
268 {
269         int ret;
270         struct file *filp;
271
272         ret = get_unused_fd();
273         if (ret < 0) {
274                 dput(dentry);
275                 mntput(mnt);
276                 goto out;
277         }
278
279         filp = dentry_open(dentry, mnt, O_RDONLY);
280         if (IS_ERR(filp)) {
281                 put_unused_fd(ret);
282                 ret = PTR_ERR(filp);
283                 goto out;
284         }
285
286         filp->f_op = &spufs_context_fops;
287         fd_install(ret, filp);
288 out:
289         return ret;
290 }
291
292 static struct file_system_type spufs_type;
293
294 long spufs_create_thread(struct nameidata *nd,
295                          unsigned int flags, mode_t mode)
296 {
297         struct dentry *dentry;
298         int ret;
299
300         /* need to be at the root of spufs */
301         ret = -EINVAL;
302         if (nd->dentry->d_sb->s_type != &spufs_type ||
303             nd->dentry != nd->dentry->d_sb->s_root)
304                 goto out;
305
306         /* all flags are reserved */
307         if (flags)
308                 goto out;
309
310         dentry = lookup_create(nd, 1);
311         ret = PTR_ERR(dentry);
312         if (IS_ERR(dentry))
313                 goto out_dir;
314
315         ret = -EEXIST;
316         if (dentry->d_inode)
317                 goto out_dput;
318
319         mode &= ~current->fs->umask;
320         ret = spufs_mkdir(nd->dentry->d_inode, dentry, mode & S_IRWXUGO);
321         if (ret)
322                 goto out_dput;
323
324         /*
325          * get references for dget and mntget, will be released
326          * in error path of *_open().
327          */
328         ret = spufs_context_open(dget(dentry), mntget(nd->mnt));
329         if (ret < 0) {
330                 WARN_ON(spufs_rmdir(nd->dentry->d_inode, dentry));
331                 mutex_unlock(&nd->dentry->d_inode->i_mutex);
332                 spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
333                 dput(dentry);
334                 goto out;
335         }
336
337 out_dput:
338         dput(dentry);
339 out_dir:
340         mutex_unlock(&nd->dentry->d_inode->i_mutex);
341 out:
342         return ret;
343 }
344
345 /* File system initialization */
346 enum {
347         Opt_uid, Opt_gid, Opt_err,
348 };
349
350 static match_table_t spufs_tokens = {
351         { Opt_uid, "uid=%d" },
352         { Opt_gid, "gid=%d" },
353         { Opt_err, NULL  },
354 };
355
356 static int
357 spufs_parse_options(char *options, struct inode *root)
358 {
359         char *p;
360         substring_t args[MAX_OPT_ARGS];
361
362         while ((p = strsep(&options, ",")) != NULL) {
363                 int token, option;
364
365                 if (!*p)
366                         continue;
367
368                 token = match_token(p, spufs_tokens, args);
369                 switch (token) {
370                 case Opt_uid:
371                         if (match_int(&args[0], &option))
372                                 return 0;
373                         root->i_uid = option;
374                         break;
375                 case Opt_gid:
376                         if (match_int(&args[0], &option))
377                                 return 0;
378                         root->i_gid = option;
379                         break;
380                 default:
381                         return 0;
382                 }
383         }
384         return 1;
385 }
386
387 static int
388 spufs_create_root(struct super_block *sb, void *data)
389 {
390         struct inode *inode;
391         int ret;
392
393         ret = -ENOMEM;
394         inode = spufs_new_inode(sb, S_IFDIR | 0775);
395         if (!inode)
396                 goto out;
397
398         inode->i_op = &spufs_dir_inode_operations;
399         inode->i_fop = &simple_dir_operations;
400         SPUFS_I(inode)->i_ctx = NULL;
401
402         ret = -EINVAL;
403         if (!spufs_parse_options(data, inode))
404                 goto out_iput;
405
406         ret = -ENOMEM;
407         sb->s_root = d_alloc_root(inode);
408         if (!sb->s_root)
409                 goto out_iput;
410
411         return 0;
412 out_iput:
413         iput(inode);
414 out:
415         return ret;
416 }
417
418 static int
419 spufs_fill_super(struct super_block *sb, void *data, int silent)
420 {
421         static struct super_operations s_ops = {
422                 .alloc_inode = spufs_alloc_inode,
423                 .destroy_inode = spufs_destroy_inode,
424                 .statfs = simple_statfs,
425                 .delete_inode = spufs_delete_inode,
426                 .drop_inode = generic_delete_inode,
427         };
428
429         sb->s_maxbytes = MAX_LFS_FILESIZE;
430         sb->s_blocksize = PAGE_CACHE_SIZE;
431         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
432         sb->s_magic = SPUFS_MAGIC;
433         sb->s_op = &s_ops;
434
435         return spufs_create_root(sb, data);
436 }
437
438 static int
439 spufs_get_sb(struct file_system_type *fstype, int flags,
440                 const char *name, void *data, struct vfsmount *mnt)
441 {
442         return get_sb_single(fstype, flags, data, spufs_fill_super, mnt);
443 }
444
445 static struct file_system_type spufs_type = {
446         .owner = THIS_MODULE,
447         .name = "spufs",
448         .get_sb = spufs_get_sb,
449         .kill_sb = kill_litter_super,
450 };
451
452 static int __init spufs_init(void)
453 {
454         int ret;
455         ret = -ENOMEM;
456         spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
457                         sizeof(struct spufs_inode_info), 0,
458                         SLAB_HWCACHE_ALIGN, spufs_init_once, NULL);
459
460         if (!spufs_inode_cache)
461                 goto out;
462         if (spu_sched_init() != 0) {
463                 kmem_cache_destroy(spufs_inode_cache);
464                 goto out;
465         }
466         ret = register_filesystem(&spufs_type);
467         if (ret)
468                 goto out_cache;
469         ret = register_spu_syscalls(&spufs_calls);
470         if (ret)
471                 goto out_fs;
472         return 0;
473 out_fs:
474         unregister_filesystem(&spufs_type);
475 out_cache:
476         kmem_cache_destroy(spufs_inode_cache);
477 out:
478         return ret;
479 }
480 module_init(spufs_init);
481
482 static void __exit spufs_exit(void)
483 {
484         spu_sched_exit();
485         unregister_spu_syscalls(&spufs_calls);
486         unregister_filesystem(&spufs_type);
487         kmem_cache_destroy(spufs_inode_cache);
488 }
489 module_exit(spufs_exit);
490
491 MODULE_LICENSE("GPL");
492 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
493