]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - arch/powerpc/platforms/cell/spufs/inode.c
[PATCH] spufs: dont leak directories in failed spu_create
[mirror_ubuntu-artful-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_blksize = PAGE_CACHE_SIZE;
86 inode->i_blocks = 0;
87 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
88 out:
89 return inode;
90 }
91
92 static int
93 spufs_setattr(struct dentry *dentry, struct iattr *attr)
94 {
95 struct inode *inode = dentry->d_inode;
96
97 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
104 static int
105 spufs_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 = {
110 .setattr = spufs_setattr,
111 };
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);
125 out:
126 return ret;
127 }
128
129 static void
130 spufs_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
137 static int
138 spufs_fill_dir(struct dentry *dir, struct tree_descr *files,
139 int mode, struct spu_context *ctx)
140 {
141 struct dentry *dentry;
142 int ret;
143
144 while (files->name && files->name[0]) {
145 ret = -ENOMEM;
146 dentry = d_alloc_name(dir, files->name);
147 if (!dentry)
148 goto out;
149 ret = spufs_new_file(dir->d_sb, dentry, files->ops,
150 files->mode & mode, ctx);
151 if (ret)
152 goto out;
153 files++;
154 }
155 return 0;
156 out:
157 // FIXME: remove all files that are left
158
159 return ret;
160 }
161
162 static int spufs_rmdir(struct inode *root, struct dentry *dir_dentry)
163 {
164 struct dentry *dentry, *tmp;
165 struct spu_context *ctx;
166
167 /* remove all entries */
168 down(&root->i_sem);
169 down(&dir_dentry->d_inode->i_sem);
170 list_for_each_entry_safe(dentry, tmp, &dir_dentry->d_subdirs, d_child) {
171 spin_lock(&dcache_lock);
172 spin_lock(&dentry->d_lock);
173 if (!(d_unhashed(dentry)) && dentry->d_inode) {
174 dget_locked(dentry);
175 __d_drop(dentry);
176 spin_unlock(&dentry->d_lock);
177 simple_unlink(dir_dentry->d_inode, dentry);
178 spin_unlock(&dcache_lock);
179 dput(dentry);
180 } else {
181 spin_unlock(&dentry->d_lock);
182 spin_unlock(&dcache_lock);
183 }
184 }
185 shrink_dcache_parent(dir_dentry);
186 up(&dir_dentry->d_inode->i_sem);
187 up(&root->i_sem);
188
189 /* We have to give up the mm_struct */
190 ctx = SPUFS_I(dir_dentry->d_inode)->i_ctx;
191 spu_forget(ctx);
192
193 /* XXX Do we need to hold i_sem here ? */
194 return simple_rmdir(root, dir_dentry);
195 }
196
197 static int spufs_dir_close(struct inode *inode, struct file *file)
198 {
199 struct inode *dir;
200 struct dentry *dentry;
201 int ret;
202
203 dentry = file->f_dentry;
204 dir = dentry->d_parent->d_inode;
205
206 ret = spufs_rmdir(dir, dentry);
207 WARN_ON(ret);
208
209 return dcache_dir_close(inode, file);
210 }
211
212 struct inode_operations spufs_dir_inode_operations = {
213 .lookup = simple_lookup,
214 };
215
216 struct file_operations spufs_context_fops = {
217 .open = dcache_dir_open,
218 .release = spufs_dir_close,
219 .llseek = dcache_dir_lseek,
220 .read = generic_read_dir,
221 .readdir = dcache_readdir,
222 .fsync = simple_sync_file,
223 };
224
225 static int
226 spufs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
227 {
228 int ret;
229 struct inode *inode;
230 struct spu_context *ctx;
231
232 ret = -ENOSPC;
233 inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
234 if (!inode)
235 goto out;
236
237 if (dir->i_mode & S_ISGID) {
238 inode->i_gid = dir->i_gid;
239 inode->i_mode &= S_ISGID;
240 }
241 ctx = alloc_spu_context(inode->i_mapping);
242 SPUFS_I(inode)->i_ctx = ctx;
243 if (!ctx)
244 goto out_iput;
245
246 inode->i_op = &spufs_dir_inode_operations;
247 inode->i_fop = &simple_dir_operations;
248 ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
249 if (ret)
250 goto out_free_ctx;
251
252 d_instantiate(dentry, inode);
253 dget(dentry);
254 dir->i_nlink++;
255 dentry->d_inode->i_nlink++;
256 goto out;
257
258 out_free_ctx:
259 put_spu_context(ctx);
260 out_iput:
261 iput(inode);
262 out:
263 return ret;
264 }
265
266 static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
267 {
268 int ret;
269 struct file *filp;
270
271 ret = get_unused_fd();
272 if (ret < 0) {
273 dput(dentry);
274 mntput(mnt);
275 goto out;
276 }
277
278 filp = dentry_open(dentry, mnt, O_RDONLY);
279 if (IS_ERR(filp)) {
280 put_unused_fd(ret);
281 ret = PTR_ERR(filp);
282 goto out;
283 }
284
285 filp->f_op = &spufs_context_fops;
286 fd_install(ret, filp);
287 out:
288 return ret;
289 }
290
291 static struct file_system_type spufs_type;
292
293 long
294 spufs_create_thread(struct nameidata *nd, const char *name,
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 dentry = lookup_create(nd, 1);
307 ret = PTR_ERR(dentry);
308 if (IS_ERR(dentry))
309 goto out_dir;
310
311 ret = -EEXIST;
312 if (dentry->d_inode)
313 goto out_dput;
314
315 mode &= ~current->fs->umask;
316 ret = spufs_mkdir(nd->dentry->d_inode, dentry, mode & S_IRWXUGO);
317 if (ret)
318 goto out_dput;
319
320 /*
321 * get references for dget and mntget, will be released
322 * in error path of *_open().
323 */
324 ret = spufs_context_open(dget(dentry), mntget(nd->mnt));
325 if (ret < 0)
326 spufs_rmdir(nd->dentry->d_inode, dentry);
327
328 out_dput:
329 dput(dentry);
330 out_dir:
331 up(&nd->dentry->d_inode->i_sem);
332 out:
333 return ret;
334 }
335
336 /* File system initialization */
337 enum {
338 Opt_uid, Opt_gid, Opt_err,
339 };
340
341 static match_table_t spufs_tokens = {
342 { Opt_uid, "uid=%d" },
343 { Opt_gid, "gid=%d" },
344 { Opt_err, NULL },
345 };
346
347 static int
348 spufs_parse_options(char *options, struct inode *root)
349 {
350 char *p;
351 substring_t args[MAX_OPT_ARGS];
352
353 while ((p = strsep(&options, ",")) != NULL) {
354 int token, option;
355
356 if (!*p)
357 continue;
358
359 token = match_token(p, spufs_tokens, args);
360 switch (token) {
361 case Opt_uid:
362 if (match_int(&args[0], &option))
363 return 0;
364 root->i_uid = option;
365 break;
366 case Opt_gid:
367 if (match_int(&args[0], &option))
368 return 0;
369 root->i_gid = option;
370 break;
371 default:
372 return 0;
373 }
374 }
375 return 1;
376 }
377
378 static int
379 spufs_create_root(struct super_block *sb, void *data)
380 {
381 struct inode *inode;
382 int ret;
383
384 ret = -ENOMEM;
385 inode = spufs_new_inode(sb, S_IFDIR | 0775);
386 if (!inode)
387 goto out;
388
389 inode->i_op = &spufs_dir_inode_operations;
390 inode->i_fop = &simple_dir_operations;
391 SPUFS_I(inode)->i_ctx = NULL;
392
393 ret = -EINVAL;
394 if (!spufs_parse_options(data, inode))
395 goto out_iput;
396
397 ret = -ENOMEM;
398 sb->s_root = d_alloc_root(inode);
399 if (!sb->s_root)
400 goto out_iput;
401
402 return 0;
403 out_iput:
404 iput(inode);
405 out:
406 return ret;
407 }
408
409 static int
410 spufs_fill_super(struct super_block *sb, void *data, int silent)
411 {
412 static struct super_operations s_ops = {
413 .alloc_inode = spufs_alloc_inode,
414 .destroy_inode = spufs_destroy_inode,
415 .statfs = simple_statfs,
416 .delete_inode = spufs_delete_inode,
417 .drop_inode = generic_delete_inode,
418 };
419
420 sb->s_maxbytes = MAX_LFS_FILESIZE;
421 sb->s_blocksize = PAGE_CACHE_SIZE;
422 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
423 sb->s_magic = SPUFS_MAGIC;
424 sb->s_op = &s_ops;
425
426 return spufs_create_root(sb, data);
427 }
428
429 static struct super_block *
430 spufs_get_sb(struct file_system_type *fstype, int flags,
431 const char *name, void *data)
432 {
433 return get_sb_single(fstype, flags, data, spufs_fill_super);
434 }
435
436 static struct file_system_type spufs_type = {
437 .owner = THIS_MODULE,
438 .name = "spufs",
439 .get_sb = spufs_get_sb,
440 .kill_sb = kill_litter_super,
441 };
442
443 static int spufs_init(void)
444 {
445 int ret;
446 ret = -ENOMEM;
447 spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
448 sizeof(struct spufs_inode_info), 0,
449 SLAB_HWCACHE_ALIGN, spufs_init_once, NULL);
450
451 if (!spufs_inode_cache)
452 goto out;
453 if (spu_sched_init() != 0) {
454 kmem_cache_destroy(spufs_inode_cache);
455 goto out;
456 }
457 ret = register_filesystem(&spufs_type);
458 if (ret)
459 goto out_cache;
460 ret = register_spu_syscalls(&spufs_calls);
461 if (ret)
462 goto out_fs;
463 return 0;
464 out_fs:
465 unregister_filesystem(&spufs_type);
466 out_cache:
467 kmem_cache_destroy(spufs_inode_cache);
468 out:
469 return ret;
470 }
471 module_init(spufs_init);
472
473 static void spufs_exit(void)
474 {
475 spu_sched_exit();
476 unregister_spu_syscalls(&spufs_calls);
477 unregister_filesystem(&spufs_type);
478 kmem_cache_destroy(spufs_inode_cache);
479 }
480 module_exit(spufs_exit);
481
482 MODULE_LICENSE("GPL");
483 MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");
484