inode.c 10.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * SPU file system
 *
 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
 *
 * Author: Arnd Bergmann <arndb@de.ibm.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/file.h>
#include <linux/fs.h>
#include <linux/backing-dev.h>
#include <linux/init.h>
#include <linux/ioctl.h>
#include <linux/module.h>
29
#include <linux/mount.h>
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
#include <linux/namei.h>
#include <linux/pagemap.h>
#include <linux/poll.h>
#include <linux/slab.h>
#include <linux/parser.h>

#include <asm/io.h>
#include <asm/semaphore.h>
#include <asm/spu.h>
#include <asm/uaccess.h>

#include "spufs.h"

static kmem_cache_t *spufs_inode_cache;

static struct inode *
spufs_alloc_inode(struct super_block *sb)
{
	struct spufs_inode_info *ei;

	ei = kmem_cache_alloc(spufs_inode_cache, SLAB_KERNEL);
	if (!ei)
		return NULL;
	return &ei->vfs_inode;
}

static void
spufs_destroy_inode(struct inode *inode)
{
	kmem_cache_free(spufs_inode_cache, SPUFS_I(inode));
}

static void
spufs_init_once(void *p, kmem_cache_t * cachep, unsigned long flags)
{
	struct spufs_inode_info *ei = p;

	if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
	    SLAB_CTOR_CONSTRUCTOR) {
		inode_init_once(&ei->vfs_inode);
	}
}

static struct inode *
spufs_new_inode(struct super_block *sb, int mode)
{
	struct inode *inode;

	inode = new_inode(sb);
	if (!inode)
		goto out;

	inode->i_mode = mode;
	inode->i_uid = current->fsuid;
	inode->i_gid = current->fsgid;
	inode->i_blocks = 0;
	inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
out:
	return inode;
}

static int
spufs_setattr(struct dentry *dentry, struct iattr *attr)
{
	struct inode *inode = dentry->d_inode;

	if ((attr->ia_valid & ATTR_SIZE) &&
	    (attr->ia_size != inode->i_size))
		return -EINVAL;
	return inode_setattr(inode, attr);
}


static int
spufs_new_file(struct super_block *sb, struct dentry *dentry,
105
		const struct file_operations *fops, int mode,
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
		struct spu_context *ctx)
{
	static struct inode_operations spufs_file_iops = {
		.setattr = spufs_setattr,
	};
	struct inode *inode;
	int ret;

	ret = -ENOSPC;
	inode = spufs_new_inode(sb, S_IFREG | mode);
	if (!inode)
		goto out;

	ret = 0;
	inode->i_op = &spufs_file_iops;
	inode->i_fop = fops;
122
	inode->i_private = SPUFS_I(inode)->i_ctx = get_spu_context(ctx);
123 124 125 126 127 128 129 130 131 132 133 134 135
	d_add(dentry, inode);
out:
	return ret;
}

static void
spufs_delete_inode(struct inode *inode)
{
	if (SPUFS_I(inode)->i_ctx)
		put_spu_context(SPUFS_I(inode)->i_ctx);
	clear_inode(inode);
}

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 144 145 146
		if (!(d_unhashed(dentry)) && dentry->d_inode) {
			dget_locked(dentry);
			__d_drop(dentry);
			spin_unlock(&dentry->d_lock);
147
			simple_unlink(dir->d_inode, dentry);
148 149 150 151 152 153
			spin_unlock(&dcache_lock);
			dput(dentry);
		} else {
			spin_unlock(&dentry->d_lock);
			spin_unlock(&dcache_lock);
		}
154
	}
155
	shrink_dcache_parent(dir);
156
	mutex_unlock(&dir->d_inode->i_mutex);
157 158
}

159
/* Caller must hold root->i_mutex */
160 161 162 163
static int spufs_rmdir(struct inode *root, struct dentry *dir_dentry)
{
	/* remove all entries */
	spufs_prune_dir(dir_dentry);
164

165
	return simple_rmdir(root, dir_dentry);
166 167
}

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
static int spufs_fill_dir(struct dentry *dir, struct tree_descr *files,
			  int mode, struct spu_context *ctx)
{
	struct dentry *dentry;
	int ret;

	while (files->name && files->name[0]) {
		ret = -ENOMEM;
		dentry = d_alloc_name(dir, files->name);
		if (!dentry)
			goto out;
		ret = spufs_new_file(dir->d_sb, dentry, files->ops,
					files->mode & mode, ctx);
		if (ret)
			goto out;
		files++;
	}
	return 0;
out:
	spufs_prune_dir(dir);
	return ret;
}

191 192
static int spufs_dir_close(struct inode *inode, struct file *file)
{
193
	struct spu_context *ctx;
194 195 196 197 198 199
	struct inode *dir;
	struct dentry *dentry;
	int ret;

	dentry = file->f_dentry;
	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 208 209
	/* We have to give up the mm_struct */
	spu_forget(ctx);

210 211 212 213 214 215 216
	return dcache_dir_close(inode, file);
}

struct inode_operations spufs_dir_inode_operations = {
	.lookup = simple_lookup,
};

217
struct file_operations spufs_context_fops = {
218 219 220 221 222 223 224 225 226
	.open		= dcache_dir_open,
	.release	= spufs_dir_close,
	.llseek		= dcache_dir_lseek,
	.read		= generic_read_dir,
	.readdir	= dcache_readdir,
	.fsync		= simple_sync_file,
};

static int
227 228
spufs_mkdir(struct inode *dir, struct dentry *dentry, unsigned int flags,
		int mode)
229 230 231 232 233 234 235 236 237 238 239 240 241 242
{
	int ret;
	struct inode *inode;
	struct spu_context *ctx;

	ret = -ENOSPC;
	inode = spufs_new_inode(dir->i_sb, mode | S_IFDIR);
	if (!inode)
		goto out;

	if (dir->i_mode & S_ISGID) {
		inode->i_gid = dir->i_gid;
		inode->i_mode &= S_ISGID;
	}
243
	ctx = alloc_spu_context();
244 245 246 247
	SPUFS_I(inode)->i_ctx = ctx;
	if (!ctx)
		goto out_iput;

248 249
	ctx->flags = flags;

250 251 252 253 254 255 256 257 258
	inode->i_op = &spufs_dir_inode_operations;
	inode->i_fop = &simple_dir_operations;
	ret = spufs_fill_dir(dentry, spufs_dir_contents, mode, ctx);
	if (ret)
		goto out_free_ctx;

	d_instantiate(dentry, inode);
	dget(dentry);
	dir->i_nlink++;
259
	dentry->d_inode->i_nlink++;
260 261 262 263 264 265 266 267 268 269
	goto out;

out_free_ctx:
	put_spu_context(ctx);
out_iput:
	iput(inode);
out:
	return ret;
}

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
static int spufs_context_open(struct dentry *dentry, struct vfsmount *mnt)
{
	int ret;
	struct file *filp;

	ret = get_unused_fd();
	if (ret < 0) {
		dput(dentry);
		mntput(mnt);
		goto out;
	}

	filp = dentry_open(dentry, mnt, O_RDONLY);
	if (IS_ERR(filp)) {
		put_unused_fd(ret);
		ret = PTR_ERR(filp);
		goto out;
	}

	filp->f_op = &spufs_context_fops;
	fd_install(ret, filp);
out:
	return ret;
}

static struct file_system_type spufs_type;

297 298
long spufs_create_thread(struct nameidata *nd,
			 unsigned int flags, mode_t mode)
299 300 301 302 303 304
{
	struct dentry *dentry;
	int ret;

	/* need to be at the root of spufs */
	ret = -EINVAL;
305 306
	if (nd->dentry->d_sb->s_type != &spufs_type ||
	    nd->dentry != nd->dentry->d_sb->s_root)
307 308
		goto out;

309
	/* all flags are reserved */
310
	if (flags & (~SPU_CREATE_FLAG_ALL))
311 312
		goto out;

313 314 315 316 317 318 319 320 321 322
	dentry = lookup_create(nd, 1);
	ret = PTR_ERR(dentry);
	if (IS_ERR(dentry))
		goto out_dir;

	ret = -EEXIST;
	if (dentry->d_inode)
		goto out_dput;

	mode &= ~current->fs->umask;
323
	ret = spufs_mkdir(nd->dentry->d_inode, dentry, flags, mode & S_IRWXUGO);
324 325 326
	if (ret)
		goto out_dput;

327 328 329 330 331
	/*
	 * get references for dget and mntget, will be released
	 * in error path of *_open().
	 */
	ret = spufs_context_open(dget(dentry), mntget(nd->mnt));
332 333 334 335 336 337 338
	if (ret < 0) {
		WARN_ON(spufs_rmdir(nd->dentry->d_inode, dentry));
		mutex_unlock(&nd->dentry->d_inode->i_mutex);
		spu_forget(SPUFS_I(dentry->d_inode)->i_ctx);
		dput(dentry);
		goto out;
	}
339 340 341 342

out_dput:
	dput(dentry);
out_dir:
343
	mutex_unlock(&nd->dentry->d_inode->i_mutex);
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
out:
	return ret;
}

/* File system initialization */
enum {
	Opt_uid, Opt_gid, Opt_err,
};

static match_table_t spufs_tokens = {
	{ Opt_uid, "uid=%d" },
	{ Opt_gid, "gid=%d" },
	{ Opt_err, NULL  },
};

static int
spufs_parse_options(char *options, struct inode *root)
{
	char *p;
	substring_t args[MAX_OPT_ARGS];

	while ((p = strsep(&options, ",")) != NULL) {
		int token, option;

		if (!*p)
			continue;

		token = match_token(p, spufs_tokens, args);
		switch (token) {
		case Opt_uid:
			if (match_int(&args[0], &option))
				return 0;
			root->i_uid = option;
			break;
		case Opt_gid:
			if (match_int(&args[0], &option))
				return 0;
			root->i_gid = option;
			break;
		default:
			return 0;
		}
	}
	return 1;
}

static int
391 392
spufs_create_root(struct super_block *sb, void *data)
{
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
	struct inode *inode;
	int ret;

	ret = -ENOMEM;
	inode = spufs_new_inode(sb, S_IFDIR | 0775);
	if (!inode)
		goto out;

	inode->i_op = &spufs_dir_inode_operations;
	inode->i_fop = &simple_dir_operations;
	SPUFS_I(inode)->i_ctx = NULL;

	ret = -EINVAL;
	if (!spufs_parse_options(data, inode))
		goto out_iput;

	ret = -ENOMEM;
	sb->s_root = d_alloc_root(inode);
	if (!sb->s_root)
		goto out_iput;

	return 0;
out_iput:
	iput(inode);
out:
	return ret;
}

static int
spufs_fill_super(struct super_block *sb, void *data, int silent)
{
	static struct super_operations s_ops = {
		.alloc_inode = spufs_alloc_inode,
		.destroy_inode = spufs_destroy_inode,
		.statfs = simple_statfs,
		.delete_inode = spufs_delete_inode,
		.drop_inode = generic_delete_inode,
	};

	sb->s_maxbytes = MAX_LFS_FILESIZE;
	sb->s_blocksize = PAGE_CACHE_SIZE;
	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
	sb->s_magic = SPUFS_MAGIC;
	sb->s_op = &s_ops;

	return spufs_create_root(sb, data);
}

441
static int
442
spufs_get_sb(struct file_system_type *fstype, int flags,
443
		const char *name, void *data, struct vfsmount *mnt)
444
{
445
	return get_sb_single(fstype, flags, data, spufs_fill_super, mnt);
446 447 448 449 450 451 452 453 454
}

static struct file_system_type spufs_type = {
	.owner = THIS_MODULE,
	.name = "spufs",
	.get_sb = spufs_get_sb,
	.kill_sb = kill_litter_super,
};

455
static int __init spufs_init(void)
456 457 458 459 460 461 462 463 464
{
	int ret;
	ret = -ENOMEM;
	spufs_inode_cache = kmem_cache_create("spufs_inode_cache",
			sizeof(struct spufs_inode_info), 0,
			SLAB_HWCACHE_ALIGN, spufs_init_once, NULL);

	if (!spufs_inode_cache)
		goto out;
465 466 467 468
	if (spu_sched_init() != 0) {
		kmem_cache_destroy(spufs_inode_cache);
		goto out;
	}
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
	ret = register_filesystem(&spufs_type);
	if (ret)
		goto out_cache;
	ret = register_spu_syscalls(&spufs_calls);
	if (ret)
		goto out_fs;
	return 0;
out_fs:
	unregister_filesystem(&spufs_type);
out_cache:
	kmem_cache_destroy(spufs_inode_cache);
out:
	return ret;
}
module_init(spufs_init);

485
static void __exit spufs_exit(void)
486
{
487
	spu_sched_exit();
488 489 490 491 492 493 494 495 496
	unregister_spu_syscalls(&spufs_calls);
	unregister_filesystem(&spufs_type);
	kmem_cache_destroy(spufs_inode_cache);
}
module_exit(spufs_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");