]> bbs.cooldavid.org Git - net-next-2.6.git/blame - arch/powerpc/platforms/cell/spufs/inode.c
[PATCH] spufs: fix locking in spu_acquire_runnable
[net-next-2.6.git] / arch / powerpc / platforms / cell / spufs / inode.c
CommitLineData
67207b96
AB
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/namei.h>
30#include <linux/pagemap.h>
31#include <linux/poll.h>
32#include <linux/slab.h>
33#include <linux/parser.h>
34
35#include <asm/io.h>
36#include <asm/semaphore.h>
37#include <asm/spu.h>
38#include <asm/uaccess.h>
39
40#include "spufs.h"
41
42static kmem_cache_t *spufs_inode_cache;
43
67207b96
AB
44static struct inode *
45spufs_alloc_inode(struct super_block *sb)
46{
47 struct spufs_inode_info *ei;
48
49 ei = kmem_cache_alloc(spufs_inode_cache, SLAB_KERNEL);
50 if (!ei)
51 return NULL;
52 return &ei->vfs_inode;
53}
54
55static void
56spufs_destroy_inode(struct inode *inode)
57{
58 kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
59}
60
61static void
62spufs_init_once(void *p, kmem_cache_t * cachep, unsigned long flags)
63{
64 struct spufs_inode_info *ei = p;
65
66 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
67 SLAB_CTOR_CONSTRUCTOR) {
68 inode_init_once(&ei->vfs_inode);
69 }
70}
71
72static struct inode *
73spufs_new_inode(struct super_block *sb, int mode)
74{
75 struct inode *inode;
76
77 inode = new_inode(sb);
78 if (!inode)
79 goto out;
80
81 inode->i_mode = mode;
82 inode->i_uid = current->fsuid;
83 inode->i_gid = current->fsgid;
84 inode->i_blksize = PAGE_CACHE_SIZE;
85 inode->i_blocks = 0;
86 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
87out:
88 return inode;
89}
90
91static int
92spufs_setattr(struct dentry *dentry, struct iattr *attr)
93{
94 struct inode *inode = dentry->d_inode;
95
67207b96
AB
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
103static int
104spufs_new_file(struct super_block *sb, struct dentry *dentry,
105 struct file_operations *fops, int mode,
106 struct spu_context *ctx)
107{
108 static struct inode_operations spufs_file_iops = {
67207b96 109 .setattr = spufs_setattr,
67207b96
AB
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->u.generic_ip = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
123 d_add(dentry, inode);
124out:
125 return ret;
126}
127
128static void
129spufs_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
136static int
137spufs_fill_dir(struct dentry *dir, struct tree_descr *files,
138 int mode, struct spu_context *ctx)
139{
140 struct dentry *dentry;
141 int ret;
142
143 while (files->name && files->name[0]) {
144 ret = -ENOMEM;
145 dentry = d_alloc_name(dir, files->name);
146 if (!dentry)
147 goto out;
148 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
149 files->mode & mode, ctx);
150 if (ret)
151 goto out;
152 files++;
153 }
154 return 0;
155out:
156 // FIXME: remove all files that are left
157
158 return ret;
159}
160
161static int spufs_rmdir(struct inode *root, struct dentry *dir_dentry)
162{
8b3d6663
AB
163 struct dentry *dentry, *tmp;
164 struct spu_context *ctx;
67207b96
AB
165 int err;
166
67207b96
AB
167 /* remove all entries */
168 err = 0;
8b3d6663
AB
169 list_for_each_entry_safe(dentry, tmp, &dir_dentry->d_subdirs, d_child) {
170 spin_lock(&dcache_lock);
67207b96 171 spin_lock(&dentry->d_lock);
8b3d6663
AB
172 if (!(d_unhashed(dentry)) && dentry->d_inode) {
173 dget_locked(dentry);
174 __d_drop(dentry);
175 spin_unlock(&dentry->d_lock);
176 simple_unlink(dir_dentry->d_inode, dentry);
177 spin_unlock(&dcache_lock);
178 dput(dentry);
179 } else {
180 spin_unlock(&dentry->d_lock);
181 spin_unlock(&dcache_lock);
182 }
67207b96 183 }
8b3d6663
AB
184
185 /* We have to give up the mm_struct */
186 ctx = SPUFS_I(dir_dentry->d_inode)->i_ctx;
187 spu_forget(ctx);
188
67207b96
AB
189 if (!err) {
190 shrink_dcache_parent(dir_dentry);
191 err = simple_rmdir(root, dir_dentry);
192 }
193 return err;
194}
195
196static int spufs_dir_close(struct inode *inode, struct file *file)
197{
198 struct inode *dir;
199 struct dentry *dentry;
200 int ret;
201
202 dentry = file->f_dentry;
203 dir = dentry->d_parent->d_inode;
204 down(&dir->i_sem);
205 ret = spufs_rmdir(dir, file->f_dentry);
206 WARN_ON(ret);
207 up(&dir->i_sem);
208 return dcache_dir_close(inode, file);
209}
210
211struct inode_operations spufs_dir_inode_operations = {
212 .lookup = simple_lookup,
213};
214
215struct file_operations spufs_autodelete_dir_operations = {
216 .open = dcache_dir_open,
217 .release = spufs_dir_close,
218 .llseek = dcache_dir_lseek,
219 .read = generic_read_dir,
220 .readdir = dcache_readdir,
221 .fsync = simple_sync_file,
222};
223
224static int
225spufs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
226{
227 int ret;
228 struct inode *inode;
229 struct spu_context *ctx;
230
231 ret = -ENOSPC;
232 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
233 if (!inode)
234 goto out;
235
236 if (dir->i_mode & S_ISGID) {
237 inode->i_gid = dir->i_gid;
238 inode->i_mode &= S_ISGID;
239 }
8b3d6663 240 ctx = alloc_spu_context(inode->i_mapping);
67207b96
AB
241 SPUFS_I(inode)->i_ctx = ctx;
242 if (!ctx)
243 goto out_iput;
244
245 inode->i_op = &spufs_dir_inode_operations;
246 inode->i_fop = &simple_dir_operations;
247 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
248 if (ret)
249 goto out_free_ctx;
250
251 d_instantiate(dentry, inode);
252 dget(dentry);
253 dir->i_nlink++;
254 goto out;
255
256out_free_ctx:
257 put_spu_context(ctx);
258out_iput:
259 iput(inode);
260out:
261 return ret;
262}
263
264long
265spufs_create_thread(struct nameidata *nd, const char *name,
266 unsigned int flags, mode_t mode)
267{
268 struct dentry *dentry;
269 struct file *filp;
270 int ret;
271
272 /* need to be at the root of spufs */
273 ret = -EINVAL;
274 if (nd->dentry->d_sb->s_magic != SPUFS_MAGIC ||
275 nd->dentry != nd->dentry->d_sb->s_root)
276 goto out;
277
278 dentry = lookup_create(nd, 1);
279 ret = PTR_ERR(dentry);
280 if (IS_ERR(dentry))
281 goto out_dir;
282
283 ret = -EEXIST;
284 if (dentry->d_inode)
285 goto out_dput;
286
287 mode &= ~current->fs->umask;
288 ret = spufs_mkdir(nd->dentry->d_inode, dentry, mode & S_IRWXUGO);
289 if (ret)
290 goto out_dput;
291
292 ret = get_unused_fd();
293 if (ret < 0)
294 goto out_dput;
295
296 dentry->d_inode->i_nlink++;
297
298 filp = filp_open(name, O_RDONLY, mode);
299 if (IS_ERR(filp)) {
300 // FIXME: remove directory again
301 put_unused_fd(ret);
302 ret = PTR_ERR(filp);
303 } else {
304 filp->f_op = &spufs_autodelete_dir_operations;
305 fd_install(ret, filp);
306 }
307
308out_dput:
309 dput(dentry);
310out_dir:
311 up(&nd->dentry->d_inode->i_sem);
312out:
313 return ret;
314}
315
316/* File system initialization */
317enum {
318 Opt_uid, Opt_gid, Opt_err,
319};
320
321static match_table_t spufs_tokens = {
322 { Opt_uid, "uid=%d" },
323 { Opt_gid, "gid=%d" },
324 { Opt_err, NULL },
325};
326
327static int
328spufs_parse_options(char *options, struct inode *root)
329{
330 char *p;
331 substring_t args[MAX_OPT_ARGS];
332
333 while ((p = strsep(&options, ",")) != NULL) {
334 int token, option;
335
336 if (!*p)
337 continue;
338
339 token = match_token(p, spufs_tokens, args);
340 switch (token) {
341 case Opt_uid:
342 if (match_int(&args[0], &option))
343 return 0;
344 root->i_uid = option;
345 break;
346 case Opt_gid:
347 if (match_int(&args[0], &option))
348 return 0;
349 root->i_gid = option;
350 break;
351 default:
352 return 0;
353 }
354 }
355 return 1;
356}
357
358static int
8b3d6663
AB
359spufs_create_root(struct super_block *sb, void *data)
360{
67207b96
AB
361 struct inode *inode;
362 int ret;
363
364 ret = -ENOMEM;
365 inode = spufs_new_inode(sb, S_IFDIR | 0775);
366 if (!inode)
367 goto out;
368
369 inode->i_op = &spufs_dir_inode_operations;
370 inode->i_fop = &simple_dir_operations;
371 SPUFS_I(inode)->i_ctx = NULL;
372
373 ret = -EINVAL;
374 if (!spufs_parse_options(data, inode))
375 goto out_iput;
376
377 ret = -ENOMEM;
378 sb->s_root = d_alloc_root(inode);
379 if (!sb->s_root)
380 goto out_iput;
381
382 return 0;
383out_iput:
384 iput(inode);
385out:
386 return ret;
387}
388
389static int
390spufs_fill_super(struct super_block *sb, void *data, int silent)
391{
392 static struct super_operations s_ops = {
393 .alloc_inode = spufs_alloc_inode,
394 .destroy_inode = spufs_destroy_inode,
395 .statfs = simple_statfs,
396 .delete_inode = spufs_delete_inode,
397 .drop_inode = generic_delete_inode,
398 };
399
400 sb->s_maxbytes = MAX_LFS_FILESIZE;
401 sb->s_blocksize = PAGE_CACHE_SIZE;
402 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
403 sb->s_magic = SPUFS_MAGIC;
404 sb->s_op = &s_ops;
405
406 return spufs_create_root(sb, data);
407}
408
409static struct super_block *
410spufs_get_sb(struct file_system_type *fstype, int flags,
411 const char *name, void *data)
412{
413 return get_sb_single(fstype, flags, data, spufs_fill_super);
414}
415
416static struct file_system_type spufs_type = {
417 .owner = THIS_MODULE,
418 .name = "spufs",
419 .get_sb = spufs_get_sb,
420 .kill_sb = kill_litter_super,
421};
422
423static int spufs_init(void)
424{
425 int ret;
426 ret = -ENOMEM;
427 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
428 sizeof(struct spufs_inode_info), 0,
429 SLAB_HWCACHE_ALIGN, spufs_init_once, NULL);
430
431 if (!spufs_inode_cache)
432 goto out;
8b3d6663
AB
433 if (spu_sched_init() != 0) {
434 kmem_cache_destroy(spufs_inode_cache);
435 goto out;
436 }
67207b96
AB
437 ret = register_filesystem(&spufs_type);
438 if (ret)
439 goto out_cache;
440 ret = register_spu_syscalls(&spufs_calls);
441 if (ret)
442 goto out_fs;
443 return 0;
444out_fs:
445 unregister_filesystem(&spufs_type);
446out_cache:
447 kmem_cache_destroy(spufs_inode_cache);
448out:
449 return ret;
450}
451module_init(spufs_init);
452
453static void spufs_exit(void)
454{
8b3d6663 455 spu_sched_exit();
67207b96
AB
456 unregister_spu_syscalls(&spufs_calls);
457 unregister_filesystem(&spufs_type);
458 kmem_cache_destroy(spufs_inode_cache);
459}
460module_exit(spufs_exit);
461
462MODULE_LICENSE("GPL");
463MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
464