Merge with 2.5.75.
[linux-2.6/linux-mips.git] / fs / befs / linuxvfs.c
blobd7846d65b3616b596bcf9ea8984316e57e7d2d4e
1 /*
2 * linux/fs/befs/linuxvfs.c
4 * Copyright (C) 2001 Will Dyson <will_dyson@pobox.com
6 */
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/fs.h>
11 #include <linux/errno.h>
12 #include <linux/stat.h>
13 #include <linux/nls.h>
14 #include <linux/buffer_head.h>
15 #include <linux/vfs.h>
17 #include "befs.h"
18 #include "btree.h"
19 #include "inode.h"
20 #include "datastream.h"
21 #include "super.h"
22 #include "io.h"
23 #include "endian.h"
25 MODULE_DESCRIPTION("BeOS File System (BeFS) driver");
26 MODULE_AUTHOR("Will Dyson");
27 MODULE_LICENSE("GPL");
29 /* The units the vfs expects inode->i_blocks to be in */
30 #define VFS_BLOCK_SIZE 512
32 static int befs_readdir(struct file *, void *, filldir_t);
33 static int befs_get_block(struct inode *, sector_t, struct buffer_head *, int);
34 static int befs_readpage(struct file *file, struct page *page);
35 static sector_t befs_bmap(struct address_space *mapping, sector_t block);
36 static struct dentry *befs_lookup(struct inode *, struct dentry *, struct nameidata *);
37 static void befs_read_inode(struct inode *ino);
38 static struct inode *befs_alloc_inode(struct super_block *sb);
39 static void befs_destroy_inode(struct inode *inode);
40 static int befs_init_inodecache(void);
41 static void befs_destroy_inodecache(void);
42 static int befs_readlink(struct dentry *, char *, int);
43 static int befs_follow_link(struct dentry *, struct nameidata *nd);
44 static int befs_utf2nls(struct super_block *sb, const char *in, int in_len,
45 char **out, int *out_len);
46 static int befs_nls2utf(struct super_block *sb, const char *in, int in_len,
47 char **out, int *out_len);
48 static void befs_put_super(struct super_block *);
49 static int befs_remount(struct super_block *, int *, char *);
50 static int befs_statfs(struct super_block *, struct kstatfs *);
51 static int parse_options(char *, befs_mount_options *);
53 static const struct super_operations befs_sops = {
54 .read_inode = befs_read_inode, /* initialize & read inode */
55 .alloc_inode = befs_alloc_inode, /* allocate a new inode */
56 .destroy_inode = befs_destroy_inode, /* deallocate an inode */
57 .put_super = befs_put_super, /* uninit super */
58 .statfs = befs_statfs, /* statfs */
59 .remount_fs = befs_remount,
62 /* slab cache for befs_inode_info objects */
63 static kmem_cache_t *befs_inode_cachep;
65 struct file_operations befs_dir_operations = {
66 .read = generic_read_dir,
67 .readdir = befs_readdir,
70 struct inode_operations befs_dir_inode_operations = {
71 .lookup = befs_lookup,
74 struct file_operations befs_file_operations = {
75 .llseek = default_llseek,
76 .read = generic_file_read,
77 .mmap = generic_file_readonly_mmap,
80 struct address_space_operations befs_aops = {
81 .readpage = befs_readpage,
82 .sync_page = block_sync_page,
83 .bmap = befs_bmap,
86 static struct inode_operations befs_symlink_inode_operations = {
87 .readlink = befs_readlink,
88 .follow_link = befs_follow_link,
91 /*
92 * Called by generic_file_read() to read a page of data
94 * In turn, simply calls a generic block read function and
95 * passes it the address of befs_get_block, for mapping file
96 * positions to disk blocks.
98 static int
99 befs_readpage(struct file *file, struct page *page)
101 return block_read_full_page(page, befs_get_block);
104 static sector_t
105 befs_bmap(struct address_space *mapping, sector_t block)
107 return generic_block_bmap(mapping, block, befs_get_block);
111 * Generic function to map a file position (block) to a
112 * disk offset (passed back in bh_result).
114 * Used by many higher level functions.
116 * Calls befs_fblock2brun() in datastream.c to do the real work.
118 * -WD 10-26-01
121 static int
122 befs_get_block(struct inode *inode, sector_t block,
123 struct buffer_head *bh_result, int create)
125 struct super_block *sb = inode->i_sb;
126 befs_data_stream *ds = &BEFS_I(inode)->i_data.ds;
127 befs_block_run run = BAD_IADDR;
128 int res = 0;
129 ulong disk_off;
131 befs_debug(sb, "---> befs_get_block() for inode %lu, block %ld",
132 inode->i_ino, block);
134 if (block < 0) {
135 befs_error(sb, "befs_get_block() was asked for a block "
136 "number less than zero: block %ld in inode %lu",
137 block, inode->i_ino);
138 return -EIO;
141 if (create) {
142 befs_error(sb, "befs_get_block() was asked to write to "
143 "block %ld in inode %lu", block, inode->i_ino);
144 return -EPERM;
147 res = befs_fblock2brun(sb, ds, block, &run);
148 if (res != BEFS_OK) {
149 befs_error(sb,
150 "<--- befs_get_block() for inode %lu, block "
151 "%ld ERROR", inode->i_ino, block);
152 return -EFBIG;
155 disk_off = (ulong) iaddr2blockno(sb, &run);
157 map_bh(bh_result, inode->i_sb, disk_off);
159 befs_debug(sb, "<--- befs_get_block() for inode %lu, block %ld, "
160 "disk address %lu", inode->i_ino, block, disk_off);
162 return 0;
165 static struct dentry *
166 befs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
168 struct inode *inode = NULL;
169 struct super_block *sb = dir->i_sb;
170 befs_data_stream *ds = &BEFS_I(dir)->i_data.ds;
171 befs_off_t offset;
172 int ret;
173 int utfnamelen;
174 char *utfname;
175 const char *name = dentry->d_name.name;
177 befs_debug(sb, "---> befs_lookup() "
178 "name %s inode %ld", dentry->d_name.name, dir->i_ino);
180 /* Convert to UTF-8 */
181 if (BEFS_SB(sb)->nls) {
182 ret =
183 befs_nls2utf(sb, name, strlen(name), &utfname, &utfnamelen);
184 if (ret < 0) {
185 befs_debug(sb, "<--- befs_lookup() ERROR");
186 return ERR_PTR(ret);
188 ret = befs_btree_find(sb, ds, utfname, &offset);
189 kfree(utfname);
191 } else {
192 ret = befs_btree_find(sb, ds, dentry->d_name.name, &offset);
195 if (ret == BEFS_BT_NOT_FOUND) {
196 befs_debug(sb, "<--- befs_lookup() %s not found",
197 dentry->d_name.name);
198 return ERR_PTR(-ENOENT);
200 } else if (ret != BEFS_OK || offset == 0) {
201 befs_warning(sb, "<--- befs_lookup() Error");
202 return ERR_PTR(-ENODATA);
205 inode = iget(dir->i_sb, (ino_t) offset);
206 if (!inode)
207 return ERR_PTR(-EACCES);
209 d_add(dentry, inode);
211 befs_debug(sb, "<--- befs_lookup()");
213 return NULL;
216 static int
217 befs_readdir(struct file *filp, void *dirent, filldir_t filldir)
219 struct inode *inode = filp->f_dentry->d_inode;
220 struct super_block *sb = inode->i_sb;
221 befs_data_stream *ds = &BEFS_I(inode)->i_data.ds;
222 befs_off_t value;
223 int result;
224 size_t keysize;
225 unsigned char d_type;
226 char keybuf[BEFS_NAME_LEN + 1];
227 char *nlsname;
228 int nlsnamelen;
229 const char *dirname = filp->f_dentry->d_name.name;
231 befs_debug(sb, "---> befs_readdir() "
232 "name %s, inode %ld, filp->f_pos %Ld",
233 dirname, inode->i_ino, filp->f_pos);
235 result = befs_btree_read(sb, ds, filp->f_pos, BEFS_NAME_LEN + 1,
236 keybuf, &keysize, &value);
238 if (result == BEFS_ERR) {
239 befs_debug(sb, "<--- befs_readdir() ERROR");
240 befs_error(sb, "IO error reading %s (inode %lu)",
241 dirname, inode->i_ino);
242 return -EIO;
244 } else if (result == BEFS_BT_END) {
245 befs_debug(sb, "<--- befs_readdir() END");
246 return 0;
248 } else if (result == BEFS_BT_EMPTY) {
249 befs_debug(sb, "<--- befs_readdir() Empty directory");
250 return 0;
253 d_type = DT_UNKNOWN;
255 /* Convert to NLS */
256 if (BEFS_SB(sb)->nls) {
257 result =
258 befs_utf2nls(sb, keybuf, keysize, &nlsname, &nlsnamelen);
259 if (result < 0) {
260 befs_debug(sb, "<--- befs_readdir() ERROR");
261 return result;
263 result = filldir(dirent, nlsname, nlsnamelen, filp->f_pos,
264 (ino_t) value, d_type);
265 kfree(nlsname);
267 } else {
268 result = filldir(dirent, keybuf, keysize, filp->f_pos,
269 (ino_t) value, d_type);
272 filp->f_pos++;
274 befs_debug(sb, "<--- befs_readdir() filp->f_pos %Ld", filp->f_pos);
276 return 0;
279 static struct inode *
280 befs_alloc_inode(struct super_block *sb)
282 struct befs_inode_info *bi;
283 bi = (struct befs_inode_info *)kmem_cache_alloc(befs_inode_cachep,
284 SLAB_KERNEL);
285 if (!bi)
286 return NULL;
287 return &bi->vfs_inode;
290 static void
291 befs_destroy_inode(struct inode *inode)
293 kmem_cache_free(befs_inode_cachep, BEFS_I(inode));
296 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
298 struct befs_inode_info *bi = (struct befs_inode_info *) foo;
300 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
301 SLAB_CTOR_CONSTRUCTOR) {
302 inode_init_once(&bi->vfs_inode);
306 static void
307 befs_read_inode(struct inode *inode)
309 struct buffer_head *bh = NULL;
310 befs_inode *raw_inode = NULL;
312 struct super_block *sb = inode->i_sb;
313 befs_sb_info *befs_sb = BEFS_SB(sb);
314 befs_inode_info *befs_ino = NULL;
316 befs_debug(sb, "---> befs_read_inode() " "inode = %lu", inode->i_ino);
318 befs_ino = BEFS_I(inode);
320 /* convert from vfs's inode number to befs's inode number */
321 befs_ino->i_inode_num = blockno2iaddr(sb, inode->i_ino);
323 befs_debug(sb, " real inode number [%u, %hu, %hu]",
324 befs_ino->i_inode_num.allocation_group,
325 befs_ino->i_inode_num.start, befs_ino->i_inode_num.len);
327 bh = befs_bread_iaddr(sb, befs_ino->i_inode_num);
328 if (!bh) {
329 befs_error(sb, "unable to read inode block - "
330 "inode = %lu", inode->i_ino);
331 goto unaquire_none;
334 raw_inode = (befs_inode *) bh->b_data;
336 befs_dump_inode(sb, raw_inode);
338 if (befs_check_inode(sb, raw_inode, inode->i_ino) != BEFS_OK) {
339 befs_error(sb, "Bad inode: %lu", inode->i_ino);
340 goto unaquire_bh;
343 inode->i_mode = (umode_t) fs32_to_cpu(sb, raw_inode->mode);
346 * set uid and gid. But since current BeOS is single user OS, so
347 * you can change by "uid" or "gid" options.
350 inode->i_uid = befs_sb->mount_opts.use_uid ?
351 befs_sb->mount_opts.uid : (uid_t) fs32_to_cpu(sb, raw_inode->uid);
352 inode->i_gid = befs_sb->mount_opts.use_gid ?
353 befs_sb->mount_opts.gid : (gid_t) fs32_to_cpu(sb, raw_inode->gid);
355 inode->i_nlink = 1;
358 * BEFS's time is 64 bits, but current VFS is 32 bits...
359 * BEFS don't have access time. Nor inode change time. VFS
360 * doesn't have creation time.
361 * Also, the lower 16 bits of the last_modified_time and
362 * create_time are just a counter to help ensure uniqueness
363 * for indexing purposes. (PFD, page 54)
366 inode->i_mtime.tv_sec =
367 fs64_to_cpu(sb, raw_inode->last_modified_time) >> 16;
368 inode->i_mtime.tv_nsec = 0; /* lower 16 bits are not a time */
369 inode->i_ctime = inode->i_mtime;
370 inode->i_atime = inode->i_mtime;
371 inode->i_blksize = befs_sb->block_size;
373 befs_ino->i_inode_num = fsrun_to_cpu(sb, raw_inode->inode_num);
374 befs_ino->i_parent = fsrun_to_cpu(sb, raw_inode->parent);
375 befs_ino->i_attribute = fsrun_to_cpu(sb, raw_inode->attributes);
376 befs_ino->i_flags = fs32_to_cpu(sb, raw_inode->flags);
378 if (S_ISLNK(inode->i_mode) && !(inode->i_flags & BEFS_LONG_SYMLINK)) {
379 inode->i_size = 0;
380 inode->i_blocks = befs_sb->block_size / VFS_BLOCK_SIZE;
381 strncpy(befs_ino->i_data.symlink, raw_inode->data.symlink,
382 BEFS_SYMLINK_LEN);
383 } else {
384 int num_blks;
386 befs_ino->i_data.ds =
387 fsds_to_cpu(sb, raw_inode->data.datastream);
389 num_blks = befs_count_blocks(sb, &befs_ino->i_data.ds);
390 inode->i_blocks =
391 num_blks * (befs_sb->block_size / VFS_BLOCK_SIZE);
392 inode->i_size = befs_ino->i_data.ds.size;
395 inode->i_mapping->a_ops = &befs_aops;
397 if (S_ISREG(inode->i_mode)) {
398 inode->i_fop = &befs_file_operations;
399 } else if (S_ISDIR(inode->i_mode)) {
400 inode->i_op = &befs_dir_inode_operations;
401 inode->i_fop = &befs_dir_operations;
402 } else if (S_ISLNK(inode->i_mode)) {
403 inode->i_op = &befs_symlink_inode_operations;
404 } else {
405 befs_error(sb, "Inode %lu is not a regular file, "
406 "directory or symlink. THAT IS WRONG! BeFS has no "
407 "on disk special files", inode->i_ino);
408 goto unaquire_bh;
411 brelse(bh);
412 befs_debug(sb, "<--- befs_read_inode()");
413 return;
415 unaquire_bh:
416 brelse(bh);
418 unaquire_none:
419 make_bad_inode(inode);
420 befs_debug(sb, "<--- befs_read_inode() - Bad inode");
421 return;
424 /* Initialize the inode cache. Called at fs setup.
426 * Taken from NFS implementation by Al Viro.
428 static int
429 befs_init_inodecache(void)
431 befs_inode_cachep = kmem_cache_create("befs_inode_cache",
432 sizeof (struct befs_inode_info),
433 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
434 init_once, NULL);
435 if (befs_inode_cachep == NULL) {
436 printk(KERN_ERR "befs_init_inodecache: "
437 "Couldn't initalize inode slabcache\n");
438 return -ENOMEM;
441 return 0;
444 /* Called at fs teardown.
446 * Taken from NFS implementation by Al Viro.
448 static void
449 befs_destroy_inodecache(void)
451 if (kmem_cache_destroy(befs_inode_cachep))
452 printk(KERN_ERR "befs_destroy_inodecache: "
453 "not all structures were freed\n");
457 * The inode of symbolic link is different to data stream.
458 * The data stream become link name. Unless the LONG_SYMLINK
459 * flag is set.
461 static int
462 befs_follow_link(struct dentry *dentry, struct nameidata *nd)
464 struct super_block *sb = dentry->d_sb;
465 befs_inode_info *befs_ino = BEFS_I(dentry->d_inode);
466 char *link;
467 int res;
469 if (befs_ino->i_flags & BEFS_LONG_SYMLINK) {
470 befs_data_stream *data = &befs_ino->i_data.ds;
471 befs_off_t linklen = data->size;
473 befs_debug(sb, "Follow long symlink");
475 link = kmalloc(linklen, GFP_NOFS);
476 if (link == NULL)
477 return -ENOMEM;
479 if (befs_read_lsymlink(sb, data, link, linklen) != linklen) {
480 kfree(link);
481 befs_error(sb, "Failed to read entire long symlink");
482 return -EIO;
485 res = vfs_follow_link(nd, link);
487 kfree(link);
488 } else {
489 link = befs_ino->i_data.symlink;
490 res = vfs_follow_link(nd, link);
493 return res;
496 static int
497 befs_readlink(struct dentry *dentry, char *buffer, int buflen)
499 struct super_block *sb = dentry->d_sb;
500 befs_inode_info *befs_ino = BEFS_I(dentry->d_inode);
501 char *link;
502 int res;
504 if (befs_ino->i_flags & BEFS_LONG_SYMLINK) {
505 befs_data_stream *data = &befs_ino->i_data.ds;
506 befs_off_t linklen = data->size;
508 befs_debug(sb, "Read long symlink");
510 link = kmalloc(linklen, GFP_NOFS);
511 if (link == NULL)
512 return -ENOMEM;
514 if (befs_read_lsymlink(sb, data, link, linklen) != linklen) {
515 kfree(link);
516 befs_error(sb, "Failed to read entire long symlink");
517 return -EIO;
520 res = vfs_readlink(dentry, buffer, buflen, link);
522 kfree(link);
523 } else {
524 link = befs_ino->i_data.symlink;
525 res = vfs_readlink(dentry, buffer, buflen, link);
528 return res;
532 * UTF-8 to NLS charset convert routine
535 * Changed 8/10/01 by Will Dyson. Now use uni2char() / char2uni() rather than
536 * the nls tables directly
539 static int
540 befs_utf2nls(struct super_block *sb, const char *in,
541 int in_len, char **out, int *out_len)
543 struct nls_table *nls = BEFS_SB(sb)->nls;
544 int i, o;
545 wchar_t uni;
546 int unilen, utflen;
547 char *result;
548 int maxlen = in_len; /* The utf8->nls conversion can't make more chars */
550 befs_debug(sb, "---> utf2nls()");
552 if (!nls) {
553 befs_error(sb, "befs_utf2nls called with no NLS table loaded");
554 return -EINVAL;
557 *out = result = kmalloc(maxlen, GFP_NOFS);
558 if (!*out) {
559 befs_error(sb, "befs_utf2nls() cannot allocate memory");
560 *out_len = 0;
561 return -ENOMEM;
564 for (i = o = 0; i < in_len; i += utflen, o += unilen) {
566 /* convert from UTF-8 to Unicode */
567 utflen = utf8_mbtowc(&uni, &in[i], in_len - i);
568 if (utflen < 0) {
569 goto conv_err;
572 /* convert from Unicode to nls */
573 unilen = nls->uni2char(uni, &result[o], 1);
574 if (unilen < 0) {
575 goto conv_err;
578 result[o] = '\0';
579 *out_len = o;
581 befs_debug(sb, "<--- utf2nls()");
583 return o;
585 conv_err:
586 befs_error(sb, "Name using charecter set %s contains a charecter that "
587 "cannot be converted to unicode.", nls->charset);
588 befs_debug(sb, "<--- utf2nls()");
589 kfree(result);
590 return -EILSEQ;
594 * befs_nls2utf - Convert NLS string to utf8 encodeing
595 * @sb: Superblock
596 * @src: Input string buffer in NLS format
597 * @srclen: Length of input string in bytes
598 * @dest: The output string in UTF8 format
599 * @destlen: Length of the output buffer
601 * Converts input string @src, which is in the format of the loaded NLS map,
602 * into a utf8 string.
604 * The destination string @dest is allocated by this function and the caller is
605 * responsible for freeing it with kfree()
607 * On return, *@destlen is the length of @dest in bytes.
609 * On success, the return value is the number of utf8 characters written to
610 * the output buffer @dest.
612 * On Failure, a negative number coresponding to the error code is returned.
615 static int
616 befs_nls2utf(struct super_block *sb, const char *in,
617 int in_len, char **out, int *out_len)
619 struct nls_table *nls = BEFS_SB(sb)->nls;
620 int i, o;
621 wchar_t uni;
622 int unilen, utflen;
623 char *result;
624 int maxlen = 3 * in_len;
626 befs_debug(sb, "---> nls2utf()\n");
628 if (!nls) {
629 befs_error(sb, "befs_nls2utf called with no NLS table loaded.");
630 return -EINVAL;
633 *out = result = kmalloc(maxlen, GFP_NOFS);
634 if (!*out) {
635 befs_error(sb, "befs_nls2utf() cannot allocate memory");
636 *out_len = 0;
637 return -ENOMEM;
640 for (i = o = 0; i < in_len; i += unilen, o += utflen) {
642 /* convert from nls to unicode */
643 unilen = nls->char2uni(&in[i], in_len - i, &uni);
644 if (unilen < 0) {
645 goto conv_err;
648 /* convert from unicode to UTF-8 */
649 utflen = utf8_wctomb(&result[o], uni, 3);
650 if (utflen <= 0) {
651 goto conv_err;
655 result[o] = '\0';
656 *out_len = o;
658 befs_debug(sb, "<--- nls2utf()");
660 return i;
662 conv_err:
663 befs_error(sb, "Name using charecter set %s contains a charecter that "
664 "cannot be converted to unicode.", nls->charset);
665 befs_debug(sb, "<--- nls2utf()");
666 kfree(result);
667 return -EILSEQ;
670 static int
671 parse_options(char *options, befs_mount_options * opts)
673 char *this_char;
674 char *value;
675 int ret = 1;
677 /* Initialize options */
678 opts->uid = 0;
679 opts->gid = 0;
680 opts->use_uid = 0;
681 opts->use_gid = 0;
682 opts->iocharset = NULL;
683 opts->debug = 0;
685 if (!options)
686 return ret;
688 while ((this_char = strsep(&options, ",")) != NULL) {
690 if ((value = strchr(this_char, '=')) != NULL)
691 *value++ = 0;
693 if (!strcmp(this_char, "uid")) {
694 if (!value || !*value) {
695 ret = 0;
696 } else {
697 opts->uid = simple_strtoul(value, &value, 0);
698 opts->use_uid = 1;
699 if (*value) {
700 printk(KERN_ERR "BEFS: Invalid uid "
701 "option: %s\n", value);
702 ret = 0;
705 } else if (!strcmp(this_char, "gid")) {
706 if (!value || !*value)
707 ret = 0;
708 else {
709 opts->gid = simple_strtoul(value, &value, 0);
710 opts->use_gid = 1;
711 if (*value) {
712 printk(KERN_ERR
713 "BEFS: Invalid gid option: "
714 "%s\n", value);
715 ret = 0;
718 } else if (!strcmp(this_char, "iocharset") && value) {
719 char *p = value;
720 int len;
722 while (*value && *value != ',')
723 value++;
724 len = value - p;
725 if (len) {
726 char *buffer = kmalloc(len + 1, GFP_NOFS);
727 if (buffer) {
728 opts->iocharset = buffer;
729 memcpy(buffer, p, len);
730 buffer[len] = 0;
732 } else {
733 printk(KERN_ERR "BEFS: "
734 "cannot allocate memory\n");
735 ret = 0;
738 } else if (!strcmp(this_char, "debug")) {
739 opts->debug = 1;
743 return ret;
746 /* This function has the responsibiltiy of getting the
747 * filesystem ready for unmounting.
748 * Basicly, we free everything that we allocated in
749 * befs_read_inode
751 static void
752 befs_put_super(struct super_block *sb)
754 if (BEFS_SB(sb)->mount_opts.iocharset) {
755 kfree(BEFS_SB(sb)->mount_opts.iocharset);
756 BEFS_SB(sb)->mount_opts.iocharset = NULL;
759 if (BEFS_SB(sb)->nls) {
760 unload_nls(BEFS_SB(sb)->nls);
761 BEFS_SB(sb)->nls = NULL;
764 if (sb->s_fs_info) {
765 kfree(sb->s_fs_info);
766 sb->s_fs_info = NULL;
768 return;
771 /* Allocate private field of the superblock, fill it.
773 * Finish filling the public superblock fields
774 * Make the root directory
775 * Load a set of NLS translations if needed.
777 static int
778 befs_fill_super(struct super_block *sb, void *data, int silent)
780 struct buffer_head *bh;
781 befs_sb_info *befs_sb;
782 befs_super_block *disk_sb;
784 const unsigned long sb_block = 0;
785 const off_t x86_sb_off = 512;
787 sb->s_fs_info = kmalloc(sizeof (*befs_sb), GFP_KERNEL);
788 if (sb->s_fs_info == NULL) {
789 printk(KERN_ERR
790 "BeFS(%s): Unable to allocate memory for private "
791 "portion of superblock. Bailing.\n", sb->s_id);
792 goto unaquire_none;
794 befs_sb = BEFS_SB(sb);
795 memset(befs_sb, 0, sizeof(befs_sb_info));
797 if (!parse_options((char *) data, &befs_sb->mount_opts)) {
798 befs_error(sb, "cannot parse mount options");
799 goto unaquire_priv_sbp;
802 befs_debug(sb, "---> befs_fill_super()");
804 #ifndef CONFIG_BEFS_RW
805 if (!(sb->s_flags & MS_RDONLY)) {
806 befs_warning(sb,
807 "No write support. Marking filesystem read-only");
808 sb->s_flags |= MS_RDONLY;
810 #endif /* CONFIG_BEFS_RW */
813 * Set dummy blocksize to read super block.
814 * Will be set to real fs blocksize later.
816 * Linux 2.4.10 and later refuse to read blocks smaller than
817 * the hardsect size for the device. But we also need to read at
818 * least 1k to get the second 512 bytes of the volume.
819 * -WD 10-26-01
821 sb_min_blocksize(sb, 1024);
823 if (!(bh = sb_bread(sb, sb_block))) {
824 befs_error(sb, "unable to read superblock");
825 goto unaquire_priv_sbp;
828 /* account for offset of super block on x86 */
829 disk_sb = (befs_super_block *) bh->b_data;
830 if ((le32_to_cpu(disk_sb->magic1) == BEFS_SUPER_MAGIC1) ||
831 (be32_to_cpu(disk_sb->magic1) == BEFS_SUPER_MAGIC1)) {
832 befs_debug(sb, "Using PPC superblock location");
833 } else {
834 befs_debug(sb, "Using x86 superblock location");
835 disk_sb =
836 (befs_super_block *) ((void *) bh->b_data + x86_sb_off);
839 if (befs_load_sb(sb, disk_sb) != BEFS_OK)
840 goto unaquire_bh;
842 befs_dump_super_block(sb, disk_sb);
844 brelse(bh);
846 if (befs_check_sb(sb) != BEFS_OK)
847 goto unaquire_priv_sbp;
850 * set up enough so that it can read an inode
851 * Fill in kernel superblock fields from private sb
853 sb->s_magic = BEFS_SUPER_MAGIC;
854 /* Set real blocksize of fs */
855 sb_set_blocksize(sb, (ulong) befs_sb->block_size);
856 sb->s_op = (struct super_operations *) &befs_sops;
857 sb->s_root =
858 d_alloc_root(iget(sb, iaddr2blockno(sb, &(befs_sb->root_dir))));
859 if (!sb->s_root) {
860 befs_error(sb, "get root inode failed");
861 goto unaquire_priv_sbp;
864 /* load nls library */
865 if (befs_sb->mount_opts.iocharset) {
866 befs_debug(sb, "Loading nls: %s",
867 befs_sb->mount_opts.iocharset);
868 befs_sb->nls = load_nls(befs_sb->mount_opts.iocharset);
869 if (!befs_sb->nls) {
870 befs_warning(sb, "Cannot load nls %s"
871 "loding default nls",
872 befs_sb->mount_opts.iocharset);
873 befs_sb->nls = load_nls_default();
877 return 0;
878 /*****************/
879 unaquire_bh:
880 brelse(bh);
882 unaquire_priv_sbp:
883 kfree(sb->s_fs_info);
885 unaquire_none:
886 sb->s_fs_info = NULL;
887 return -EINVAL;
890 static int
891 befs_remount(struct super_block *sb, int *flags, char *data)
893 if (!(*flags & MS_RDONLY))
894 return -EINVAL;
895 return 0;
898 static int
899 befs_statfs(struct super_block *sb, struct kstatfs *buf)
902 befs_debug(sb, "---> befs_statfs()");
904 buf->f_type = BEFS_SUPER_MAGIC;
905 buf->f_bsize = sb->s_blocksize;
906 buf->f_blocks = BEFS_SB(sb)->num_blocks;
907 buf->f_bfree = BEFS_SB(sb)->num_blocks - BEFS_SB(sb)->used_blocks;
908 buf->f_bavail = buf->f_bfree;
909 buf->f_files = 0; /* UNKNOWN */
910 buf->f_ffree = 0; /* UNKNOWN */
911 buf->f_namelen = BEFS_NAME_LEN;
913 befs_debug(sb, "<--- befs_statfs()");
915 return 0;
918 static struct super_block *
919 befs_get_sb(struct file_system_type *fs_type, int flags, const char *dev_name,
920 void *data)
922 return get_sb_bdev(fs_type, flags, dev_name, data, befs_fill_super);
925 static struct file_system_type befs_fs_type = {
926 .owner = THIS_MODULE,
927 .name = "befs",
928 .get_sb = befs_get_sb,
929 .kill_sb = kill_block_super,
930 .fs_flags = FS_REQUIRES_DEV,
933 static int __init
934 init_befs_fs(void)
936 int err;
938 printk(KERN_INFO "BeFS version: %s\n", BEFS_VERSION);
940 err = befs_init_inodecache();
941 if (err)
942 return err;
944 return register_filesystem(&befs_fs_type);
947 static void __exit
948 exit_befs_fs(void)
950 befs_destroy_inodecache();
952 unregister_filesystem(&befs_fs_type);
956 Macros that typecheck the init and exit functions,
957 ensures that they are called at init and cleanup,
958 and eliminates warnings about unused functions.
960 module_init(init_befs_fs)
961 module_exit(exit_befs_fs)