linuxvfs.c 24.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7
/*
 * linux/fs/befs/linuxvfs.c
 *
 * Copyright (C) 2001 Will Dyson <will_dyson@pobox.com
 *
 */

F
Fabian Frederick 已提交
8 9
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

L
Linus Torvalds 已提交
10 11 12 13 14 15 16 17 18 19
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/stat.h>
#include <linux/nls.h>
#include <linux/buffer_head.h>
#include <linux/vfs.h>
#include <linux/parser.h>
#include <linux/namei.h>
20
#include <linux/sched.h>
L
Linus Torvalds 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

#include "befs.h"
#include "btree.h"
#include "inode.h"
#include "datastream.h"
#include "super.h"
#include "io.h"

MODULE_DESCRIPTION("BeOS File System (BeFS) driver");
MODULE_AUTHOR("Will Dyson");
MODULE_LICENSE("GPL");

/* The units the vfs expects inode->i_blocks to be in */
#define VFS_BLOCK_SIZE 512

A
Al Viro 已提交
36
static int befs_readdir(struct file *, struct dir_context *);
L
Linus Torvalds 已提交
37 38 39
static int befs_get_block(struct inode *, sector_t, struct buffer_head *, int);
static int befs_readpage(struct file *file, struct page *page);
static sector_t befs_bmap(struct address_space *mapping, sector_t block);
A
Al Viro 已提交
40
static struct dentry *befs_lookup(struct inode *, struct dentry *, unsigned int);
41
static struct inode *befs_iget(struct super_block *, unsigned long);
L
Linus Torvalds 已提交
42 43 44
static struct inode *befs_alloc_inode(struct super_block *sb);
static void befs_destroy_inode(struct inode *inode);
static void befs_destroy_inodecache(void);
45
static void *befs_follow_link(struct dentry *, struct nameidata *);
46
static void *befs_fast_follow_link(struct dentry *, struct nameidata *);
L
Linus Torvalds 已提交
47 48 49 50 51 52
static int befs_utf2nls(struct super_block *sb, const char *in, int in_len,
			char **out, int *out_len);
static int befs_nls2utf(struct super_block *sb, const char *in, int in_len,
			char **out, int *out_len);
static void befs_put_super(struct super_block *);
static int befs_remount(struct super_block *, int *, char *);
53
static int befs_statfs(struct dentry *, struct kstatfs *);
L
Linus Torvalds 已提交
54 55 56 57 58 59 60 61
static int parse_options(char *, befs_mount_options *);

static const struct super_operations befs_sops = {
	.alloc_inode	= befs_alloc_inode,	/* allocate a new inode */
	.destroy_inode	= befs_destroy_inode, /* deallocate an inode */
	.put_super	= befs_put_super,	/* uninit super */
	.statfs		= befs_statfs,	/* statfs */
	.remount_fs	= befs_remount,
M
Miklos Szeredi 已提交
62
	.show_options	= generic_show_options,
L
Linus Torvalds 已提交
63 64 65
};

/* slab cache for befs_inode_info objects */
66
static struct kmem_cache *befs_inode_cachep;
L
Linus Torvalds 已提交
67

68
static const struct file_operations befs_dir_operations = {
L
Linus Torvalds 已提交
69
	.read		= generic_read_dir,
A
Al Viro 已提交
70
	.iterate	= befs_readdir,
71
	.llseek		= generic_file_llseek,
L
Linus Torvalds 已提交
72 73
};

74
static const struct inode_operations befs_dir_inode_operations = {
L
Linus Torvalds 已提交
75 76 77
	.lookup		= befs_lookup,
};

78
static const struct address_space_operations befs_aops = {
L
Linus Torvalds 已提交
79 80 81 82
	.readpage	= befs_readpage,
	.bmap		= befs_bmap,
};

83 84 85 86 87
static const struct inode_operations befs_fast_symlink_inode_operations = {
	.readlink	= generic_readlink,
	.follow_link	= befs_fast_follow_link,
};

88
static const struct inode_operations befs_symlink_inode_operations = {
L
Linus Torvalds 已提交
89 90
	.readlink	= generic_readlink,
	.follow_link	= befs_follow_link,
91
	.put_link	= kfree_put_link,
L
Linus Torvalds 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
};

/* 
 * Called by generic_file_read() to read a page of data
 * 
 * In turn, simply calls a generic block read function and
 * passes it the address of befs_get_block, for mapping file
 * positions to disk blocks.
 */
static int
befs_readpage(struct file *file, struct page *page)
{
	return block_read_full_page(page, befs_get_block);
}

static sector_t
befs_bmap(struct address_space *mapping, sector_t block)
{
	return generic_block_bmap(mapping, block, befs_get_block);
}

/* 
 * Generic function to map a file position (block) to a 
 * disk offset (passed back in bh_result).
 *
 * Used by many higher level functions.
 *
 * Calls befs_fblock2brun() in datastream.c to do the real work.
 *
 * -WD 10-26-01
 */

static int
befs_get_block(struct inode *inode, sector_t block,
	       struct buffer_head *bh_result, int create)
{
	struct super_block *sb = inode->i_sb;
	befs_data_stream *ds = &BEFS_I(inode)->i_data.ds;
	befs_block_run run = BAD_IADDR;
	int res = 0;
	ulong disk_off;

	befs_debug(sb, "---> befs_get_block() for inode %lu, block %ld",
F
Fabian Frederick 已提交
135
		   (unsigned long)inode->i_ino, (long)block);
L
Linus Torvalds 已提交
136 137
	if (create) {
		befs_error(sb, "befs_get_block() was asked to write to "
F
Fabian Frederick 已提交
138 139
			   "block %ld in inode %lu", (long)block,
			   (unsigned long)inode->i_ino);
L
Linus Torvalds 已提交
140 141 142 143 144 145
		return -EPERM;
	}

	res = befs_fblock2brun(sb, ds, block, &run);
	if (res != BEFS_OK) {
		befs_error(sb,
F
Fabian Frederick 已提交
146 147 148
			   "<--- %s for inode %lu, block %ld ERROR",
			   __func__, (unsigned long)inode->i_ino,
			   (long)block);
L
Linus Torvalds 已提交
149 150 151 152 153 154 155
		return -EFBIG;
	}

	disk_off = (ulong) iaddr2blockno(sb, &run);

	map_bh(bh_result, inode->i_sb, disk_off);

F
Fabian Frederick 已提交
156 157 158
	befs_debug(sb, "<--- %s for inode %lu, block %ld, disk address %lu",
		  __func__, (unsigned long)inode->i_ino, (long)block,
		  (unsigned long)disk_off);
L
Linus Torvalds 已提交
159 160 161 162 163

	return 0;
}

static struct dentry *
A
Al Viro 已提交
164
befs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
L
Linus Torvalds 已提交
165 166 167 168 169 170 171 172 173 174
{
	struct inode *inode = NULL;
	struct super_block *sb = dir->i_sb;
	befs_data_stream *ds = &BEFS_I(dir)->i_data.ds;
	befs_off_t offset;
	int ret;
	int utfnamelen;
	char *utfname;
	const char *name = dentry->d_name.name;

A
Al Viro 已提交
175 176
	befs_debug(sb, "---> %s name %pd inode %ld", __func__,
		   dentry, dir->i_ino);
L
Linus Torvalds 已提交
177 178 179 180 181 182

	/* Convert to UTF-8 */
	if (BEFS_SB(sb)->nls) {
		ret =
		    befs_nls2utf(sb, name, strlen(name), &utfname, &utfnamelen);
		if (ret < 0) {
F
Fabian Frederick 已提交
183
			befs_debug(sb, "<--- %s ERROR", __func__);
L
Linus Torvalds 已提交
184 185 186 187 188 189 190 191 192 193
			return ERR_PTR(ret);
		}
		ret = befs_btree_find(sb, ds, utfname, &offset);
		kfree(utfname);

	} else {
		ret = befs_btree_find(sb, ds, dentry->d_name.name, &offset);
	}

	if (ret == BEFS_BT_NOT_FOUND) {
A
Al Viro 已提交
194
		befs_debug(sb, "<--- %s %pd not found", __func__, dentry);
L
Linus Torvalds 已提交
195 196 197
		return ERR_PTR(-ENOENT);

	} else if (ret != BEFS_OK || offset == 0) {
F
Fabian Frederick 已提交
198
		befs_warning(sb, "<--- %s Error", __func__);
L
Linus Torvalds 已提交
199 200 201
		return ERR_PTR(-ENODATA);
	}

202 203 204
	inode = befs_iget(dir->i_sb, (ino_t) offset);
	if (IS_ERR(inode))
		return ERR_CAST(inode);
L
Linus Torvalds 已提交
205 206 207

	d_add(dentry, inode);

F
Fabian Frederick 已提交
208
	befs_debug(sb, "<--- %s", __func__);
L
Linus Torvalds 已提交
209 210 211 212 213

	return NULL;
}

static int
A
Al Viro 已提交
214
befs_readdir(struct file *file, struct dir_context *ctx)
L
Linus Torvalds 已提交
215
{
A
Al Viro 已提交
216
	struct inode *inode = file_inode(file);
L
Linus Torvalds 已提交
217 218 219 220 221 222 223 224
	struct super_block *sb = inode->i_sb;
	befs_data_stream *ds = &BEFS_I(inode)->i_data.ds;
	befs_off_t value;
	int result;
	size_t keysize;
	unsigned char d_type;
	char keybuf[BEFS_NAME_LEN + 1];

A
Al Viro 已提交
225 226
	befs_debug(sb, "---> %s name %pD, inode %ld, ctx->pos %lld",
		  __func__, file, inode->i_ino, ctx->pos);
L
Linus Torvalds 已提交
227

A
Al Viro 已提交
228 229
more:
	result = befs_btree_read(sb, ds, ctx->pos, BEFS_NAME_LEN + 1,
L
Linus Torvalds 已提交
230 231 232
				 keybuf, &keysize, &value);

	if (result == BEFS_ERR) {
F
Fabian Frederick 已提交
233
		befs_debug(sb, "<--- %s ERROR", __func__);
A
Al Viro 已提交
234 235
		befs_error(sb, "IO error reading %pD (inode %lu)",
			   file, inode->i_ino);
L
Linus Torvalds 已提交
236 237 238
		return -EIO;

	} else if (result == BEFS_BT_END) {
F
Fabian Frederick 已提交
239
		befs_debug(sb, "<--- %s END", __func__);
L
Linus Torvalds 已提交
240 241 242
		return 0;

	} else if (result == BEFS_BT_EMPTY) {
F
Fabian Frederick 已提交
243
		befs_debug(sb, "<--- %s Empty directory", __func__);
L
Linus Torvalds 已提交
244 245 246 247 248 249 250
		return 0;
	}

	d_type = DT_UNKNOWN;

	/* Convert to NLS */
	if (BEFS_SB(sb)->nls) {
A
Al Viro 已提交
251 252
		char *nlsname;
		int nlsnamelen;
L
Linus Torvalds 已提交
253 254 255
		result =
		    befs_utf2nls(sb, keybuf, keysize, &nlsname, &nlsnamelen);
		if (result < 0) {
F
Fabian Frederick 已提交
256
			befs_debug(sb, "<--- %s ERROR", __func__);
L
Linus Torvalds 已提交
257 258
			return result;
		}
A
Al Viro 已提交
259 260 261 262 263
		if (!dir_emit(ctx, nlsname, nlsnamelen,
				 (ino_t) value, d_type)) {
			kfree(nlsname);
			return 0;
		}
L
Linus Torvalds 已提交
264 265
		kfree(nlsname);
	} else {
A
Al Viro 已提交
266 267 268
		if (!dir_emit(ctx, keybuf, keysize,
				 (ino_t) value, d_type))
			return 0;
L
Linus Torvalds 已提交
269
	}
A
Al Viro 已提交
270 271
	ctx->pos++;
	goto more;
L
Linus Torvalds 已提交
272

F
Fabian Frederick 已提交
273
	befs_debug(sb, "<--- %s pos %lld", __func__, ctx->pos);
L
Linus Torvalds 已提交
274 275 276 277 278 279 280 281 282

	return 0;
}

static struct inode *
befs_alloc_inode(struct super_block *sb)
{
        struct befs_inode_info *bi;
        bi = (struct befs_inode_info *)kmem_cache_alloc(befs_inode_cachep,
283
							GFP_KERNEL);
L
Linus Torvalds 已提交
284 285 286 287 288
        if (!bi)
                return NULL;
        return &bi->vfs_inode;
}

N
Nick Piggin 已提交
289
static void befs_i_callback(struct rcu_head *head)
L
Linus Torvalds 已提交
290
{
N
Nick Piggin 已提交
291
	struct inode *inode = container_of(head, struct inode, i_rcu);
L
Linus Torvalds 已提交
292 293 294
        kmem_cache_free(befs_inode_cachep, BEFS_I(inode));
}

N
Nick Piggin 已提交
295 296 297 298 299
static void befs_destroy_inode(struct inode *inode)
{
	call_rcu(&inode->i_rcu, befs_i_callback);
}

300
static void init_once(void *foo)
L
Linus Torvalds 已提交
301 302
{
        struct befs_inode_info *bi = (struct befs_inode_info *) foo;
C
Christoph Lameter 已提交
303 304

	inode_init_once(&bi->vfs_inode);
L
Linus Torvalds 已提交
305 306
}

307
static struct inode *befs_iget(struct super_block *sb, unsigned long ino)
L
Linus Torvalds 已提交
308 309 310 311 312 313
{
	struct buffer_head *bh = NULL;
	befs_inode *raw_inode = NULL;

	befs_sb_info *befs_sb = BEFS_SB(sb);
	befs_inode_info *befs_ino = NULL;
314 315
	struct inode *inode;
	long ret = -EIO;
L
Linus Torvalds 已提交
316

F
Fabian Frederick 已提交
317
	befs_debug(sb, "---> %s inode = %lu", __func__, ino);
318 319

	inode = iget_locked(sb, ino);
320 321
	if (!inode)
		return ERR_PTR(-ENOMEM);
322 323
	if (!(inode->i_state & I_NEW))
		return inode;
L
Linus Torvalds 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337

	befs_ino = BEFS_I(inode);

	/* convert from vfs's inode number to befs's inode number */
	befs_ino->i_inode_num = blockno2iaddr(sb, inode->i_ino);

	befs_debug(sb, "  real inode number [%u, %hu, %hu]",
		   befs_ino->i_inode_num.allocation_group,
		   befs_ino->i_inode_num.start, befs_ino->i_inode_num.len);

	bh = befs_bread(sb, inode->i_ino);
	if (!bh) {
		befs_error(sb, "unable to read inode block - "
			   "inode = %lu", inode->i_ino);
A
Adrian Bunk 已提交
338
		goto unacquire_none;
L
Linus Torvalds 已提交
339 340 341 342 343 344 345 346
	}

	raw_inode = (befs_inode *) bh->b_data;

	befs_dump_inode(sb, raw_inode);

	if (befs_check_inode(sb, raw_inode, inode->i_ino) != BEFS_OK) {
		befs_error(sb, "Bad inode: %lu", inode->i_ino);
A
Adrian Bunk 已提交
347
		goto unacquire_bh;
L
Linus Torvalds 已提交
348 349 350 351 352 353 354 355 356 357
	}

	inode->i_mode = (umode_t) fs32_to_cpu(sb, raw_inode->mode);

	/*
	 * set uid and gid.  But since current BeOS is single user OS, so
	 * you can change by "uid" or "gid" options.
	 */   

	inode->i_uid = befs_sb->mount_opts.use_uid ?
358 359
		befs_sb->mount_opts.uid :
		make_kuid(&init_user_ns, fs32_to_cpu(sb, raw_inode->uid));
L
Linus Torvalds 已提交
360
	inode->i_gid = befs_sb->mount_opts.use_gid ?
361 362
		befs_sb->mount_opts.gid :
		make_kgid(&init_user_ns, fs32_to_cpu(sb, raw_inode->gid));
L
Linus Torvalds 已提交
363

M
Miklos Szeredi 已提交
364
	set_nlink(inode, 1);
L
Linus Torvalds 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388

	/*
	 * BEFS's time is 64 bits, but current VFS is 32 bits...
	 * BEFS don't have access time. Nor inode change time. VFS
	 * doesn't have creation time.
	 * Also, the lower 16 bits of the last_modified_time and 
	 * create_time are just a counter to help ensure uniqueness
	 * for indexing purposes. (PFD, page 54)
	 */

	inode->i_mtime.tv_sec =
	    fs64_to_cpu(sb, raw_inode->last_modified_time) >> 16;
	inode->i_mtime.tv_nsec = 0;   /* lower 16 bits are not a time */	
	inode->i_ctime = inode->i_mtime;
	inode->i_atime = inode->i_mtime;

	befs_ino->i_inode_num = fsrun_to_cpu(sb, raw_inode->inode_num);
	befs_ino->i_parent = fsrun_to_cpu(sb, raw_inode->parent);
	befs_ino->i_attribute = fsrun_to_cpu(sb, raw_inode->attributes);
	befs_ino->i_flags = fs32_to_cpu(sb, raw_inode->flags);

	if (S_ISLNK(inode->i_mode) && !(befs_ino->i_flags & BEFS_LONG_SYMLINK)){
		inode->i_size = 0;
		inode->i_blocks = befs_sb->block_size / VFS_BLOCK_SIZE;
389 390
		strlcpy(befs_ino->i_data.symlink, raw_inode->data.symlink,
			BEFS_SYMLINK_LEN);
L
Linus Torvalds 已提交
391 392 393 394
	} else {
		int num_blks;

		befs_ino->i_data.ds =
395
		    fsds_to_cpu(sb, &raw_inode->data.datastream);
L
Linus Torvalds 已提交
396 397 398 399 400 401 402 403 404 405

		num_blks = befs_count_blocks(sb, &befs_ino->i_data.ds);
		inode->i_blocks =
		    num_blks * (befs_sb->block_size / VFS_BLOCK_SIZE);
		inode->i_size = befs_ino->i_data.ds.size;
	}

	inode->i_mapping->a_ops = &befs_aops;

	if (S_ISREG(inode->i_mode)) {
406
		inode->i_fop = &generic_ro_fops;
L
Linus Torvalds 已提交
407 408 409 410
	} else if (S_ISDIR(inode->i_mode)) {
		inode->i_op = &befs_dir_inode_operations;
		inode->i_fop = &befs_dir_operations;
	} else if (S_ISLNK(inode->i_mode)) {
411 412 413 414
		if (befs_ino->i_flags & BEFS_LONG_SYMLINK)
			inode->i_op = &befs_symlink_inode_operations;
		else
			inode->i_op = &befs_fast_symlink_inode_operations;
L
Linus Torvalds 已提交
415 416 417 418
	} else {
		befs_error(sb, "Inode %lu is not a regular file, "
			   "directory or symlink. THAT IS WRONG! BeFS has no "
			   "on disk special files", inode->i_ino);
A
Adrian Bunk 已提交
419
		goto unacquire_bh;
L
Linus Torvalds 已提交
420 421 422
	}

	brelse(bh);
F
Fabian Frederick 已提交
423
	befs_debug(sb, "<--- %s", __func__);
424 425
	unlock_new_inode(inode);
	return inode;
L
Linus Torvalds 已提交
426

A
Adrian Bunk 已提交
427
      unacquire_bh:
L
Linus Torvalds 已提交
428 429
	brelse(bh);

A
Adrian Bunk 已提交
430
      unacquire_none:
431
	iget_failed(inode);
F
Fabian Frederick 已提交
432
	befs_debug(sb, "<--- %s - Bad inode", __func__);
433
	return ERR_PTR(ret);
L
Linus Torvalds 已提交
434 435 436
}

/* Initialize the inode cache. Called at fs setup.
437
 *
L
Linus Torvalds 已提交
438 439
 * Taken from NFS implementation by Al Viro.
 */
440
static int __init
L
Linus Torvalds 已提交
441 442 443 444
befs_init_inodecache(void)
{
	befs_inode_cachep = kmem_cache_create("befs_inode_cache",
					      sizeof (struct befs_inode_info),
445 446
					      0, (SLAB_RECLAIM_ACCOUNT|
						SLAB_MEM_SPREAD),
447
					      init_once);
L
Linus Torvalds 已提交
448
	if (befs_inode_cachep == NULL) {
F
Fabian Frederick 已提交
449
		pr_err("%s: Couldn't initialize inode slabcache\n", __func__);
L
Linus Torvalds 已提交
450 451 452 453 454 455 456 457 458 459 460 461
		return -ENOMEM;
	}
	return 0;
}

/* Called at fs teardown.
 * 
 * Taken from NFS implementation by Al Viro.
 */
static void
befs_destroy_inodecache(void)
{
462 463 464 465 466
	/*
	 * Make sure all delayed rcu free inodes are flushed before we
	 * destroy cache.
	 */
	rcu_barrier();
467
	kmem_cache_destroy(befs_inode_cachep);
L
Linus Torvalds 已提交
468 469 470 471 472 473 474
}

/*
 * The inode of symbolic link is different to data stream.
 * The data stream become link name. Unless the LONG_SYMLINK
 * flag is set.
 */
475
static void *
L
Linus Torvalds 已提交
476 477
befs_follow_link(struct dentry *dentry, struct nameidata *nd)
{
478
	struct super_block *sb = dentry->d_sb;
L
Linus Torvalds 已提交
479
	befs_inode_info *befs_ino = BEFS_I(dentry->d_inode);
480 481
	befs_data_stream *data = &befs_ino->i_data.ds;
	befs_off_t len = data->size;
L
Linus Torvalds 已提交
482 483
	char *link;

484 485 486 487 488
	if (len == 0) {
		befs_error(sb, "Long symlink with illegal length");
		link = ERR_PTR(-EIO);
	} else {
		befs_debug(sb, "Follow long symlink");
L
Linus Torvalds 已提交
489

490 491 492 493 494 495
		link = kmalloc(len, GFP_NOFS);
		if (!link) {
			link = ERR_PTR(-ENOMEM);
		} else if (befs_read_lsymlink(sb, data, link, len) != len) {
			kfree(link);
			befs_error(sb, "Failed to read entire long symlink");
L
Linus Torvalds 已提交
496
			link = ERR_PTR(-EIO);
497
		} else {
498
			link[len - 1] = '\0';
L
Linus Torvalds 已提交
499 500 501
		}
	}
	nd_set_link(nd, link);
502
	return NULL;
L
Linus Torvalds 已提交
503 504
}

505 506 507

static void *
befs_fast_follow_link(struct dentry *dentry, struct nameidata *nd)
L
Linus Torvalds 已提交
508 509
{
	befs_inode_info *befs_ino = BEFS_I(dentry->d_inode);
510 511
	nd_set_link(nd, befs_ino->i_data.symlink);
	return NULL;
L
Linus Torvalds 已提交
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
}

/*
 * UTF-8 to NLS charset  convert routine
 * 
 *
 * Changed 8/10/01 by Will Dyson. Now use uni2char() / char2uni() rather than
 * the nls tables directly
 */

static int
befs_utf2nls(struct super_block *sb, const char *in,
	     int in_len, char **out, int *out_len)
{
	struct nls_table *nls = BEFS_SB(sb)->nls;
	int i, o;
A
Alan Stern 已提交
528
	unicode_t uni;
L
Linus Torvalds 已提交
529 530
	int unilen, utflen;
	char *result;
D
Diego Calleja 已提交
531 532 533 534 535
	/* The utf8->nls conversion won't make the final nls string bigger
	 * than the utf one, but if the string is pure ascii they'll have the
	 * same width and an extra char is needed to save the additional \0
	 */
	int maxlen = in_len + 1;
L
Linus Torvalds 已提交
536

F
Fabian Frederick 已提交
537
	befs_debug(sb, "---> %s", __func__);
L
Linus Torvalds 已提交
538 539

	if (!nls) {
F
Fabian Frederick 已提交
540
		befs_error(sb, "%s called with no NLS table loaded", __func__);
L
Linus Torvalds 已提交
541 542 543 544 545
		return -EINVAL;
	}

	*out = result = kmalloc(maxlen, GFP_NOFS);
	if (!*out) {
F
Fabian Frederick 已提交
546
		befs_error(sb, "%s cannot allocate memory", __func__);
L
Linus Torvalds 已提交
547 548 549 550 551 552 553
		*out_len = 0;
		return -ENOMEM;
	}

	for (i = o = 0; i < in_len; i += utflen, o += unilen) {

		/* convert from UTF-8 to Unicode */
A
Alan Stern 已提交
554 555
		utflen = utf8_to_utf32(&in[i], in_len - i, &uni);
		if (utflen < 0)
L
Linus Torvalds 已提交
556 557 558
			goto conv_err;

		/* convert from Unicode to nls */
A
Alan Stern 已提交
559 560
		if (uni > MAX_WCHAR_T)
			goto conv_err;
L
Linus Torvalds 已提交
561
		unilen = nls->uni2char(uni, &result[o], in_len - o);
A
Alan Stern 已提交
562
		if (unilen < 0)
L
Linus Torvalds 已提交
563 564 565 566 567
			goto conv_err;
	}
	result[o] = '\0';
	*out_len = o;

F
Fabian Frederick 已提交
568
	befs_debug(sb, "<--- %s", __func__);
L
Linus Torvalds 已提交
569 570 571 572 573 574

	return o;

      conv_err:
	befs_error(sb, "Name using character set %s contains a character that "
		   "cannot be converted to unicode.", nls->charset);
F
Fabian Frederick 已提交
575
	befs_debug(sb, "<--- %s", __func__);
L
Linus Torvalds 已提交
576 577 578 579 580 581 582
	kfree(result);
	return -EILSEQ;
}

/**
 * befs_nls2utf - Convert NLS string to utf8 encodeing
 * @sb: Superblock
F
Fabian Frederick 已提交
583 584 585 586
 * @in: Input string buffer in NLS format
 * @in_len: Length of input string in bytes
 * @out: The output string in UTF-8 format
 * @out_len: Length of the output buffer
L
Linus Torvalds 已提交
587
 * 
F
Fabian Frederick 已提交
588
 * Converts input string @in, which is in the format of the loaded NLS map,
L
Linus Torvalds 已提交
589 590
 * into a utf8 string.
 * 
F
Fabian Frederick 已提交
591
 * The destination string @out is allocated by this function and the caller is
L
Linus Torvalds 已提交
592 593
 * responsible for freeing it with kfree()
 * 
F
Fabian Frederick 已提交
594
 * On return, *@out_len is the length of @out in bytes.
L
Linus Torvalds 已提交
595 596
 *
 * On success, the return value is the number of utf8 characters written to
F
Fabian Frederick 已提交
597
 * the output buffer @out.
L
Linus Torvalds 已提交
598 599 600 601 602 603 604 605 606 607 608 609 610
 *  
 * On Failure, a negative number coresponding to the error code is returned.
 */

static int
befs_nls2utf(struct super_block *sb, const char *in,
	     int in_len, char **out, int *out_len)
{
	struct nls_table *nls = BEFS_SB(sb)->nls;
	int i, o;
	wchar_t uni;
	int unilen, utflen;
	char *result;
D
Diego Calleja 已提交
611 612 613 614
	/* There're nls characters that will translate to 3-chars-wide UTF-8
	 * characters, a additional byte is needed to save the final \0
	 * in special cases */
	int maxlen = (3 * in_len) + 1;
L
Linus Torvalds 已提交
615

F
Fabian Frederick 已提交
616
	befs_debug(sb, "---> %s\n", __func__);
L
Linus Torvalds 已提交
617 618

	if (!nls) {
F
Fabian Frederick 已提交
619 620
		befs_error(sb, "%s called with no NLS table loaded.",
			   __func__);
L
Linus Torvalds 已提交
621 622 623 624 625
		return -EINVAL;
	}

	*out = result = kmalloc(maxlen, GFP_NOFS);
	if (!*out) {
F
Fabian Frederick 已提交
626
		befs_error(sb, "%s cannot allocate memory", __func__);
L
Linus Torvalds 已提交
627 628 629 630 631 632 633 634
		*out_len = 0;
		return -ENOMEM;
	}

	for (i = o = 0; i < in_len; i += unilen, o += utflen) {

		/* convert from nls to unicode */
		unilen = nls->char2uni(&in[i], in_len - i, &uni);
A
Alan Stern 已提交
635
		if (unilen < 0)
L
Linus Torvalds 已提交
636 637 638
			goto conv_err;

		/* convert from unicode to UTF-8 */
A
Alan Stern 已提交
639 640
		utflen = utf32_to_utf8(uni, &result[o], 3);
		if (utflen <= 0)
L
Linus Torvalds 已提交
641 642 643 644 645 646
			goto conv_err;
	}

	result[o] = '\0';
	*out_len = o;

F
Fabian Frederick 已提交
647
	befs_debug(sb, "<--- %s", __func__);
L
Linus Torvalds 已提交
648 649 650 651 652 653

	return i;

      conv_err:
	befs_error(sb, "Name using charecter set %s contains a charecter that "
		   "cannot be converted to unicode.", nls->charset);
F
Fabian Frederick 已提交
654
	befs_debug(sb, "<--- %s", __func__);
L
Linus Torvalds 已提交
655 656 657 658 659 660 661 662 663 664 665 666
	kfree(result);
	return -EILSEQ;
}

/**
 * Use the
 *
 */
enum {
	Opt_uid, Opt_gid, Opt_charset, Opt_debug, Opt_err,
};

667
static const match_table_t befs_tokens = {
L
Linus Torvalds 已提交
668 669 670 671 672 673 674 675 676 677 678 679 680
	{Opt_uid, "uid=%d"},
	{Opt_gid, "gid=%d"},
	{Opt_charset, "iocharset=%s"},
	{Opt_debug, "debug"},
	{Opt_err, NULL}
};

static int
parse_options(char *options, befs_mount_options * opts)
{
	char *p;
	substring_t args[MAX_OPT_ARGS];
	int option;
681 682
	kuid_t uid;
	kgid_t gid;
L
Linus Torvalds 已提交
683 684

	/* Initialize options */
685 686
	opts->uid = GLOBAL_ROOT_UID;
	opts->gid = GLOBAL_ROOT_GID;
L
Linus Torvalds 已提交
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
	opts->use_uid = 0;
	opts->use_gid = 0;
	opts->iocharset = NULL;
	opts->debug = 0;

	if (!options)
		return 1;

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

		token = match_token(p, befs_tokens, args);
		switch (token) {
		case Opt_uid:
			if (match_int(&args[0], &option))
				return 0;
705 706 707 708
			uid = INVALID_UID;
			if (option >= 0)
				uid = make_kuid(current_user_ns(), option);
			if (!uid_valid(uid)) {
F
Fabian Frederick 已提交
709 710
				pr_err("Invalid uid %d, "
				       "using default\n", option);
L
Linus Torvalds 已提交
711 712
				break;
			}
713
			opts->uid = uid;
L
Linus Torvalds 已提交
714 715 716 717 718
			opts->use_uid = 1;
			break;
		case Opt_gid:
			if (match_int(&args[0], &option))
				return 0;
719 720 721 722
			gid = INVALID_GID;
			if (option >= 0)
				gid = make_kgid(current_user_ns(), option);
			if (!gid_valid(gid)) {
F
Fabian Frederick 已提交
723 724
				pr_err("Invalid gid %d, "
				       "using default\n", option);
L
Linus Torvalds 已提交
725 726
				break;
			}
727
			opts->gid = gid;
L
Linus Torvalds 已提交
728 729 730 731 732 733
			opts->use_gid = 1;
			break;
		case Opt_charset:
			kfree(opts->iocharset);
			opts->iocharset = match_strdup(&args[0]);
			if (!opts->iocharset) {
F
Fabian Frederick 已提交
734 735
				pr_err("allocation failure for "
				       "iocharset string\n");
L
Linus Torvalds 已提交
736 737 738 739 740 741 742
				return 0;
			}
			break;
		case Opt_debug:
			opts->debug = 1;
			break;
		default:
F
Fabian Frederick 已提交
743 744
			pr_err("Unrecognized mount option \"%s\" "
			       "or missing value\n", p);
L
Linus Torvalds 已提交
745 746 747 748 749 750 751 752
			return 0;
		}
	}
	return 1;
}

/* This function has the responsibiltiy of getting the
 * filesystem ready for unmounting. 
L
Lucas De Marchi 已提交
753
 * Basically, we free everything that we allocated in
L
Linus Torvalds 已提交
754 755 756 757 758
 * befs_read_inode
 */
static void
befs_put_super(struct super_block *sb)
{
J
Jesper Juhl 已提交
759 760
	kfree(BEFS_SB(sb)->mount_opts.iocharset);
	BEFS_SB(sb)->mount_opts.iocharset = NULL;
761
	unload_nls(BEFS_SB(sb)->nls);
J
Jesper Juhl 已提交
762 763
	kfree(sb->s_fs_info);
	sb->s_fs_info = NULL;
L
Linus Torvalds 已提交
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
}

/* Allocate private field of the superblock, fill it.
 *
 * Finish filling the public superblock fields
 * Make the root directory
 * Load a set of NLS translations if needed.
 */
static int
befs_fill_super(struct super_block *sb, void *data, int silent)
{
	struct buffer_head *bh;
	befs_sb_info *befs_sb;
	befs_super_block *disk_sb;
	struct inode *root;
779
	long ret = -EINVAL;
L
Linus Torvalds 已提交
780 781 782
	const unsigned long sb_block = 0;
	const off_t x86_sb_off = 512;

M
Miklos Szeredi 已提交
783 784
	save_mount_options(sb, data);

785
	sb->s_fs_info = kzalloc(sizeof(*befs_sb), GFP_KERNEL);
L
Linus Torvalds 已提交
786
	if (sb->s_fs_info == NULL) {
F
Fabian Frederick 已提交
787
		pr_err("(%s): Unable to allocate memory for private "
L
Linus Torvalds 已提交
788
		       "portion of superblock. Bailing.\n", sb->s_id);
A
Adrian Bunk 已提交
789
		goto unacquire_none;
L
Linus Torvalds 已提交
790 791 792 793 794
	}
	befs_sb = BEFS_SB(sb);

	if (!parse_options((char *) data, &befs_sb->mount_opts)) {
		befs_error(sb, "cannot parse mount options");
A
Adrian Bunk 已提交
795
		goto unacquire_priv_sbp;
L
Linus Torvalds 已提交
796 797
	}

F
Fabian Frederick 已提交
798
	befs_debug(sb, "---> %s", __func__);
L
Linus Torvalds 已提交
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818

	if (!(sb->s_flags & MS_RDONLY)) {
		befs_warning(sb,
			     "No write support. Marking filesystem read-only");
		sb->s_flags |= MS_RDONLY;
	}

	/*
	 * Set dummy blocksize to read super block.
	 * Will be set to real fs blocksize later.
	 *
	 * Linux 2.4.10 and later refuse to read blocks smaller than
	 * the hardsect size for the device. But we also need to read at 
	 * least 1k to get the second 512 bytes of the volume.
	 * -WD 10-26-01
	 */ 
	sb_min_blocksize(sb, 1024);

	if (!(bh = sb_bread(sb, sb_block))) {
		befs_error(sb, "unable to read superblock");
A
Adrian Bunk 已提交
819
		goto unacquire_priv_sbp;
L
Linus Torvalds 已提交
820 821 822 823
	}

	/* account for offset of super block on x86 */
	disk_sb = (befs_super_block *) bh->b_data;
824 825
	if ((disk_sb->magic1 == BEFS_SUPER_MAGIC1_LE) ||
	    (disk_sb->magic1 == BEFS_SUPER_MAGIC1_BE)) {
L
Linus Torvalds 已提交
826 827 828 829 830 831 832
		befs_debug(sb, "Using PPC superblock location");
	} else {
		befs_debug(sb, "Using x86 superblock location");
		disk_sb =
		    (befs_super_block *) ((void *) bh->b_data + x86_sb_off);
	}

833 834
	if ((befs_load_sb(sb, disk_sb) != BEFS_OK) ||
	    (befs_check_sb(sb) != BEFS_OK))
A
Adrian Bunk 已提交
835
		goto unacquire_bh;
L
Linus Torvalds 已提交
836 837 838 839 840 841

	befs_dump_super_block(sb, disk_sb);

	brelse(bh);

	if( befs_sb->num_blocks > ~((sector_t)0) ) {
F
Fabian Frederick 已提交
842
		befs_error(sb, "blocks count: %llu "
L
Linus Torvalds 已提交
843 844
			"is larger than the host can use",
			befs_sb->num_blocks);
A
Adrian Bunk 已提交
845
		goto unacquire_priv_sbp;
L
Linus Torvalds 已提交
846 847 848 849 850 851 852 853 854
	}

	/*
	 * set up enough so that it can read an inode
	 * Fill in kernel superblock fields from private sb
	 */
	sb->s_magic = BEFS_SUPER_MAGIC;
	/* Set real blocksize of fs */
	sb_set_blocksize(sb, (ulong) befs_sb->block_size);
855
	sb->s_op = &befs_sops;
856 857 858 859 860
	root = befs_iget(sb, iaddr2blockno(sb, &(befs_sb->root_dir)));
	if (IS_ERR(root)) {
		ret = PTR_ERR(root);
		goto unacquire_priv_sbp;
	}
861
	sb->s_root = d_make_root(root);
L
Linus Torvalds 已提交
862 863
	if (!sb->s_root) {
		befs_error(sb, "get root inode failed");
A
Adrian Bunk 已提交
864
		goto unacquire_priv_sbp;
L
Linus Torvalds 已提交
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
	}

	/* load nls library */
	if (befs_sb->mount_opts.iocharset) {
		befs_debug(sb, "Loading nls: %s",
			   befs_sb->mount_opts.iocharset);
		befs_sb->nls = load_nls(befs_sb->mount_opts.iocharset);
		if (!befs_sb->nls) {
			befs_warning(sb, "Cannot load nls %s"
					" loading default nls",
					befs_sb->mount_opts.iocharset);
			befs_sb->nls = load_nls_default();
		}
	/* load default nls if none is specified  in mount options */
	} else {
		befs_debug(sb, "Loading default nls");
		befs_sb->nls = load_nls_default();
	}

	return 0;
/*****************/
A
Adrian Bunk 已提交
886
      unacquire_bh:
L
Linus Torvalds 已提交
887 888
	brelse(bh);

A
Adrian Bunk 已提交
889
      unacquire_priv_sbp:
A
Al Viro 已提交
890
	kfree(befs_sb->mount_opts.iocharset);
L
Linus Torvalds 已提交
891 892
	kfree(sb->s_fs_info);

A
Adrian Bunk 已提交
893
      unacquire_none:
L
Linus Torvalds 已提交
894
	sb->s_fs_info = NULL;
895
	return ret;
L
Linus Torvalds 已提交
896 897 898 899 900
}

static int
befs_remount(struct super_block *sb, int *flags, char *data)
{
901
	sync_filesystem(sb);
L
Linus Torvalds 已提交
902 903 904 905 906 907
	if (!(*flags & MS_RDONLY))
		return -EINVAL;
	return 0;
}

static int
908
befs_statfs(struct dentry *dentry, struct kstatfs *buf)
L
Linus Torvalds 已提交
909
{
910
	struct super_block *sb = dentry->d_sb;
C
Coly Li 已提交
911
	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
L
Linus Torvalds 已提交
912

F
Fabian Frederick 已提交
913
	befs_debug(sb, "---> %s", __func__);
L
Linus Torvalds 已提交
914 915 916 917 918 919 920 921

	buf->f_type = BEFS_SUPER_MAGIC;
	buf->f_bsize = sb->s_blocksize;
	buf->f_blocks = BEFS_SB(sb)->num_blocks;
	buf->f_bfree = BEFS_SB(sb)->num_blocks - BEFS_SB(sb)->used_blocks;
	buf->f_bavail = buf->f_bfree;
	buf->f_files = 0;	/* UNKNOWN */
	buf->f_ffree = 0;	/* UNKNOWN */
C
Coly Li 已提交
922 923
	buf->f_fsid.val[0] = (u32)id;
	buf->f_fsid.val[1] = (u32)(id >> 32);
L
Linus Torvalds 已提交
924 925
	buf->f_namelen = BEFS_NAME_LEN;

F
Fabian Frederick 已提交
926
	befs_debug(sb, "<--- %s", __func__);
L
Linus Torvalds 已提交
927 928 929 930

	return 0;
}

A
Al Viro 已提交
931 932 933
static struct dentry *
befs_mount(struct file_system_type *fs_type, int flags, const char *dev_name,
	    void *data)
L
Linus Torvalds 已提交
934
{
A
Al Viro 已提交
935
	return mount_bdev(fs_type, flags, dev_name, data, befs_fill_super);
L
Linus Torvalds 已提交
936 937 938 939 940
}

static struct file_system_type befs_fs_type = {
	.owner		= THIS_MODULE,
	.name		= "befs",
A
Al Viro 已提交
941
	.mount		= befs_mount,
L
Linus Torvalds 已提交
942 943 944
	.kill_sb	= kill_block_super,
	.fs_flags	= FS_REQUIRES_DEV,	
};
945
MODULE_ALIAS_FS("befs");
L
Linus Torvalds 已提交
946 947 948 949 950 951

static int __init
init_befs_fs(void)
{
	int err;

F
Fabian Frederick 已提交
952
	pr_info("version: %s\n", BEFS_VERSION);
L
Linus Torvalds 已提交
953 954 955

	err = befs_init_inodecache();
	if (err)
A
Adrian Bunk 已提交
956
		goto unacquire_none;
L
Linus Torvalds 已提交
957 958 959

	err = register_filesystem(&befs_fs_type);
	if (err)
A
Adrian Bunk 已提交
960
		goto unacquire_inodecache;
L
Linus Torvalds 已提交
961 962 963

	return 0;

A
Adrian Bunk 已提交
964
unacquire_inodecache:
L
Linus Torvalds 已提交
965 966
	befs_destroy_inodecache();

A
Adrian Bunk 已提交
967
unacquire_none:
L
Linus Torvalds 已提交
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
	return err;
}

static void __exit
exit_befs_fs(void)
{
	befs_destroy_inodecache();

	unregister_filesystem(&befs_fs_type);
}

/*
Macros that typecheck the init and exit functions,
ensures that they are called at init and cleanup,
and eliminates warnings about unused functions.
*/
module_init(init_befs_fs)
module_exit(exit_befs_fs)