This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / fs / befs / linuxvfs.c
1 /*
2  * linux/fs/befs/linuxvfs.c
3  *
4  * Copyright (C) 2001 Will Dyson <will_dyson@pobox.com
5  *
6  */
7
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>
16 #include <linux/parser.h>
17
18 #include "befs.h"
19 #include "btree.h"
20 #include "inode.h"
21 #include "datastream.h"
22 #include "super.h"
23 #include "io.h"
24 #include "endian.h"
25
26 MODULE_DESCRIPTION("BeOS File System (BeFS) driver");
27 MODULE_AUTHOR("Will Dyson");
28 MODULE_LICENSE("GPL");
29
30 /* The units the vfs expects inode->i_blocks to be in */
31 #define VFS_BLOCK_SIZE 512
32
33 static int befs_readdir(struct file *, void *, filldir_t);
34 static int befs_get_block(struct inode *, sector_t, struct buffer_head *, int);
35 static int befs_readpage(struct file *file, struct page *page);
36 static sector_t befs_bmap(struct address_space *mapping, sector_t block);
37 static struct dentry *befs_lookup(struct inode *, struct dentry *, struct nameidata *);
38 static void befs_read_inode(struct inode *ino);
39 static struct inode *befs_alloc_inode(struct super_block *sb);
40 static void befs_destroy_inode(struct inode *inode);
41 static int befs_init_inodecache(void);
42 static void befs_destroy_inodecache(void);
43 static int befs_follow_link(struct dentry *, struct nameidata *);
44 static void befs_put_link(struct dentry *, struct nameidata *);
45 static int befs_utf2nls(struct super_block *sb, const char *in, int in_len,
46                         char **out, int *out_len);
47 static int befs_nls2utf(struct super_block *sb, const char *in, int in_len,
48                         char **out, int *out_len);
49 static void befs_put_super(struct super_block *);
50 static int befs_remount(struct super_block *, int *, char *);
51 static int befs_statfs(struct super_block *, struct kstatfs *);
52 static int parse_options(char *, befs_mount_options *);
53
54 static const struct super_operations befs_sops = {
55         .read_inode     = befs_read_inode,      /* initialize & read inode */
56         .alloc_inode    = befs_alloc_inode,     /* allocate a new inode */
57         .destroy_inode  = befs_destroy_inode, /* deallocate an inode */
58         .put_super      = befs_put_super,       /* uninit super */
59         .statfs         = befs_statfs,  /* statfs */
60         .remount_fs     = befs_remount,
61 };
62
63 /* slab cache for befs_inode_info objects */
64 static kmem_cache_t *befs_inode_cachep;
65
66 struct file_operations befs_dir_operations = {
67         .read           = generic_read_dir,
68         .readdir        = befs_readdir,
69 };
70
71 struct inode_operations befs_dir_inode_operations = {
72         .lookup         = befs_lookup,
73 };
74
75 struct file_operations befs_file_operations = {
76         .llseek         = default_llseek,
77         .read           = generic_file_read,
78         .mmap           = generic_file_readonly_mmap,
79 };
80
81 struct address_space_operations befs_aops = {
82         .readpage       = befs_readpage,
83         .sync_page      = block_sync_page,
84         .bmap           = befs_bmap,
85 };
86
87 static struct inode_operations befs_symlink_inode_operations = {
88         .readlink       = generic_readlink,
89         .follow_link    = befs_follow_link,
90         .put_link       = befs_put_link,
91 };
92
93 /* 
94  * Called by generic_file_read() to read a page of data
95  * 
96  * In turn, simply calls a generic block read function and
97  * passes it the address of befs_get_block, for mapping file
98  * positions to disk blocks.
99  */
100 static int
101 befs_readpage(struct file *file, struct page *page)
102 {
103         return block_read_full_page(page, befs_get_block);
104 }
105
106 static sector_t
107 befs_bmap(struct address_space *mapping, sector_t block)
108 {
109         return generic_block_bmap(mapping, block, befs_get_block);
110 }
111
112 /* 
113  * Generic function to map a file position (block) to a 
114  * disk offset (passed back in bh_result).
115  *
116  * Used by many higher level functions.
117  *
118  * Calls befs_fblock2brun() in datastream.c to do the real work.
119  *
120  * -WD 10-26-01
121  */
122
123 static int
124 befs_get_block(struct inode *inode, sector_t block,
125                struct buffer_head *bh_result, int create)
126 {
127         struct super_block *sb = inode->i_sb;
128         befs_data_stream *ds = &BEFS_I(inode)->i_data.ds;
129         befs_block_run run = BAD_IADDR;
130         int res = 0;
131         ulong disk_off;
132
133         befs_debug(sb, "---> befs_get_block() for inode %lu, block %ld",
134                    inode->i_ino, block);
135
136         if (block < 0) {
137                 befs_error(sb, "befs_get_block() was asked for a block "
138                            "number less than zero: block %ld in inode %lu",
139                            block, inode->i_ino);
140                 return -EIO;
141         }
142
143         if (create) {
144                 befs_error(sb, "befs_get_block() was asked to write to "
145                            "block %ld in inode %lu", block, inode->i_ino);
146                 return -EPERM;
147         }
148
149         res = befs_fblock2brun(sb, ds, block, &run);
150         if (res != BEFS_OK) {
151                 befs_error(sb,
152                            "<--- befs_get_block() for inode %lu, block "
153                            "%ld ERROR", inode->i_ino, block);
154                 return -EFBIG;
155         }
156
157         disk_off = (ulong) iaddr2blockno(sb, &run);
158
159         map_bh(bh_result, inode->i_sb, disk_off);
160
161         befs_debug(sb, "<--- befs_get_block() for inode %lu, block %ld, "
162                    "disk address %lu", inode->i_ino, block, disk_off);
163
164         return 0;
165 }
166
167 static struct dentry *
168 befs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
169 {
170         struct inode *inode = NULL;
171         struct super_block *sb = dir->i_sb;
172         befs_data_stream *ds = &BEFS_I(dir)->i_data.ds;
173         befs_off_t offset;
174         int ret;
175         int utfnamelen;
176         char *utfname;
177         const char *name = dentry->d_name.name;
178
179         befs_debug(sb, "---> befs_lookup() "
180                    "name %s inode %ld", dentry->d_name.name, dir->i_ino);
181
182         /* Convert to UTF-8 */
183         if (BEFS_SB(sb)->nls) {
184                 ret =
185                     befs_nls2utf(sb, name, strlen(name), &utfname, &utfnamelen);
186                 if (ret < 0) {
187                         befs_debug(sb, "<--- befs_lookup() ERROR");
188                         return ERR_PTR(ret);
189                 }
190                 ret = befs_btree_find(sb, ds, utfname, &offset);
191                 kfree(utfname);
192
193         } else {
194                 ret = befs_btree_find(sb, ds, dentry->d_name.name, &offset);
195         }
196
197         if (ret == BEFS_BT_NOT_FOUND) {
198                 befs_debug(sb, "<--- befs_lookup() %s not found",
199                            dentry->d_name.name);
200                 return ERR_PTR(-ENOENT);
201
202         } else if (ret != BEFS_OK || offset == 0) {
203                 befs_warning(sb, "<--- befs_lookup() Error");
204                 return ERR_PTR(-ENODATA);
205         }
206
207         inode = iget(dir->i_sb, (ino_t) offset);
208         if (!inode)
209                 return ERR_PTR(-EACCES);
210
211         d_add(dentry, inode);
212
213         befs_debug(sb, "<--- befs_lookup()");
214
215         return NULL;
216 }
217
218 static int
219 befs_readdir(struct file *filp, void *dirent, filldir_t filldir)
220 {
221         struct inode *inode = filp->f_dentry->d_inode;
222         struct super_block *sb = inode->i_sb;
223         befs_data_stream *ds = &BEFS_I(inode)->i_data.ds;
224         befs_off_t value;
225         int result;
226         size_t keysize;
227         unsigned char d_type;
228         char keybuf[BEFS_NAME_LEN + 1];
229         char *nlsname;
230         int nlsnamelen;
231         const char *dirname = filp->f_dentry->d_name.name;
232
233         befs_debug(sb, "---> befs_readdir() "
234                    "name %s, inode %ld, filp->f_pos %Ld",
235                    dirname, inode->i_ino, filp->f_pos);
236
237         result = befs_btree_read(sb, ds, filp->f_pos, BEFS_NAME_LEN + 1,
238                                  keybuf, &keysize, &value);
239
240         if (result == BEFS_ERR) {
241                 befs_debug(sb, "<--- befs_readdir() ERROR");
242                 befs_error(sb, "IO error reading %s (inode %lu)",
243                            dirname, inode->i_ino);
244                 return -EIO;
245
246         } else if (result == BEFS_BT_END) {
247                 befs_debug(sb, "<--- befs_readdir() END");
248                 return 0;
249
250         } else if (result == BEFS_BT_EMPTY) {
251                 befs_debug(sb, "<--- befs_readdir() Empty directory");
252                 return 0;
253         }
254
255         d_type = DT_UNKNOWN;
256
257         /* Convert to NLS */
258         if (BEFS_SB(sb)->nls) {
259                 result =
260                     befs_utf2nls(sb, keybuf, keysize, &nlsname, &nlsnamelen);
261                 if (result < 0) {
262                         befs_debug(sb, "<--- befs_readdir() ERROR");
263                         return result;
264                 }
265                 result = filldir(dirent, nlsname, nlsnamelen, filp->f_pos,
266                                  (ino_t) value, d_type);
267                 kfree(nlsname);
268
269         } else {
270                 result = filldir(dirent, keybuf, keysize, filp->f_pos,
271                                  (ino_t) value, d_type);
272         }
273
274         filp->f_pos++;
275
276         befs_debug(sb, "<--- befs_readdir() filp->f_pos %Ld", filp->f_pos);
277
278         return 0;
279 }
280
281 static struct inode *
282 befs_alloc_inode(struct super_block *sb)
283 {
284         struct befs_inode_info *bi;
285         bi = (struct befs_inode_info *)kmem_cache_alloc(befs_inode_cachep,
286                                                         SLAB_KERNEL);
287         if (!bi)
288                 return NULL;
289         return &bi->vfs_inode;
290 }
291
292 static void
293 befs_destroy_inode(struct inode *inode)
294 {
295         kmem_cache_free(befs_inode_cachep, BEFS_I(inode));
296 }
297
298 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
299 {
300         struct befs_inode_info *bi = (struct befs_inode_info *) foo;
301         
302                 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
303                             SLAB_CTOR_CONSTRUCTOR) {
304                         inode_init_once(&bi->vfs_inode);
305                 }
306 }
307
308 static void
309 befs_read_inode(struct inode *inode)
310 {
311         struct buffer_head *bh = NULL;
312         befs_inode *raw_inode = NULL;
313
314         struct super_block *sb = inode->i_sb;
315         befs_sb_info *befs_sb = BEFS_SB(sb);
316         befs_inode_info *befs_ino = NULL;
317
318         befs_debug(sb, "---> befs_read_inode() " "inode = %lu", inode->i_ino);
319
320         befs_ino = BEFS_I(inode);
321
322         /* convert from vfs's inode number to befs's inode number */
323         befs_ino->i_inode_num = blockno2iaddr(sb, inode->i_ino);
324
325         befs_debug(sb, "  real inode number [%u, %hu, %hu]",
326                    befs_ino->i_inode_num.allocation_group,
327                    befs_ino->i_inode_num.start, befs_ino->i_inode_num.len);
328
329         bh = befs_bread(sb, inode->i_ino);
330         if (!bh) {
331                 befs_error(sb, "unable to read inode block - "
332                            "inode = %lu", inode->i_ino);
333                 goto unaquire_none;
334         }
335
336         raw_inode = (befs_inode *) bh->b_data;
337
338         befs_dump_inode(sb, raw_inode);
339
340         if (befs_check_inode(sb, raw_inode, inode->i_ino) != BEFS_OK) {
341                 befs_error(sb, "Bad inode: %lu", inode->i_ino);
342                 goto unaquire_bh;
343         }
344
345         inode->i_mode = (umode_t) fs32_to_cpu(sb, raw_inode->mode);
346
347         /*
348          * set uid and gid.  But since current BeOS is single user OS, so
349          * you can change by "uid" or "gid" options.
350          */   
351
352         inode->i_uid = befs_sb->mount_opts.use_uid ?
353             befs_sb->mount_opts.uid : (uid_t) fs32_to_cpu(sb, raw_inode->uid);
354         inode->i_gid = befs_sb->mount_opts.use_gid ?
355             befs_sb->mount_opts.gid : (gid_t) fs32_to_cpu(sb, raw_inode->gid);
356
357         inode->i_nlink = 1;
358
359         /*
360          * BEFS's time is 64 bits, but current VFS is 32 bits...
361          * BEFS don't have access time. Nor inode change time. VFS
362          * doesn't have creation time.
363          * Also, the lower 16 bits of the last_modified_time and 
364          * create_time are just a counter to help ensure uniqueness
365          * for indexing purposes. (PFD, page 54)
366          */
367
368         inode->i_mtime.tv_sec =
369             fs64_to_cpu(sb, raw_inode->last_modified_time) >> 16;
370         inode->i_mtime.tv_nsec = 0;   /* lower 16 bits are not a time */        
371         inode->i_ctime = inode->i_mtime;
372         inode->i_atime = inode->i_mtime;
373         inode->i_blksize = befs_sb->block_size;
374
375         befs_ino->i_inode_num = fsrun_to_cpu(sb, raw_inode->inode_num);
376         befs_ino->i_parent = fsrun_to_cpu(sb, raw_inode->parent);
377         befs_ino->i_attribute = fsrun_to_cpu(sb, raw_inode->attributes);
378         befs_ino->i_flags = fs32_to_cpu(sb, raw_inode->flags);
379
380         if (S_ISLNK(inode->i_mode) && !(befs_ino->i_flags & BEFS_LONG_SYMLINK)){
381                 inode->i_size = 0;
382                 inode->i_blocks = befs_sb->block_size / VFS_BLOCK_SIZE;
383                 strncpy(befs_ino->i_data.symlink, raw_inode->data.symlink,
384                         BEFS_SYMLINK_LEN);
385         } else {
386                 int num_blks;
387
388                 befs_ino->i_data.ds =
389                     fsds_to_cpu(sb, raw_inode->data.datastream);
390
391                 num_blks = befs_count_blocks(sb, &befs_ino->i_data.ds);
392                 inode->i_blocks =
393                     num_blks * (befs_sb->block_size / VFS_BLOCK_SIZE);
394                 inode->i_size = befs_ino->i_data.ds.size;
395         }
396
397         inode->i_mapping->a_ops = &befs_aops;
398
399         if (S_ISREG(inode->i_mode)) {
400                 inode->i_fop = &befs_file_operations;
401         } else if (S_ISDIR(inode->i_mode)) {
402                 inode->i_op = &befs_dir_inode_operations;
403                 inode->i_fop = &befs_dir_operations;
404         } else if (S_ISLNK(inode->i_mode)) {
405                 inode->i_op = &befs_symlink_inode_operations;
406         } else {
407                 befs_error(sb, "Inode %lu is not a regular file, "
408                            "directory or symlink. THAT IS WRONG! BeFS has no "
409                            "on disk special files", inode->i_ino);
410                 goto unaquire_bh;
411         }
412
413         brelse(bh);
414         befs_debug(sb, "<--- befs_read_inode()");
415         return;
416
417       unaquire_bh:
418         brelse(bh);
419
420       unaquire_none:
421         make_bad_inode(inode);
422         befs_debug(sb, "<--- befs_read_inode() - Bad inode");
423         return;
424 }
425
426 /* Initialize the inode cache. Called at fs setup.
427  * 
428  * Taken from NFS implementation by Al Viro.
429  */
430 static int
431 befs_init_inodecache(void)
432 {
433         befs_inode_cachep = kmem_cache_create("befs_inode_cache",
434                                               sizeof (struct befs_inode_info),
435                                               0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
436                                               init_once, NULL);
437         if (befs_inode_cachep == NULL) {
438                 printk(KERN_ERR "befs_init_inodecache: "
439                        "Couldn't initalize inode slabcache\n");
440                 return -ENOMEM;
441         }
442
443         return 0;
444 }
445
446 /* Called at fs teardown.
447  * 
448  * Taken from NFS implementation by Al Viro.
449  */
450 static void
451 befs_destroy_inodecache(void)
452 {
453         if (kmem_cache_destroy(befs_inode_cachep))
454                 printk(KERN_ERR "befs_destroy_inodecache: "
455                        "not all structures were freed\n");
456 }
457
458 /*
459  * The inode of symbolic link is different to data stream.
460  * The data stream become link name. Unless the LONG_SYMLINK
461  * flag is set.
462  */
463 static int
464 befs_follow_link(struct dentry *dentry, struct nameidata *nd)
465 {
466         befs_inode_info *befs_ino = BEFS_I(dentry->d_inode);
467         char *link;
468
469         if (befs_ino->i_flags & BEFS_LONG_SYMLINK) {
470                 struct super_block *sb = dentry->d_sb;
471                 befs_data_stream *data = &befs_ino->i_data.ds;
472                 befs_off_t len = data->size;
473
474                 befs_debug(sb, "Follow long symlink");
475
476                 link = kmalloc(len, GFP_NOFS);
477                 if (!link) {
478                         link = ERR_PTR(-ENOMEM);
479                 } else if (befs_read_lsymlink(sb, data, link, len) != len) {
480                         kfree(link);
481                         befs_error(sb, "Failed to read entire long symlink");
482                         link = ERR_PTR(-EIO);
483                 }
484         } else {
485                 link = befs_ino->i_data.symlink;
486         }
487
488         nd_set_link(nd, link);
489         return 0;
490 }
491
492 static void befs_put_link(struct dentry *dentry, struct nameidata *nd)
493 {
494         befs_inode_info *befs_ino = BEFS_I(dentry->d_inode);
495         if (befs_ino->i_flags & BEFS_LONG_SYMLINK) {
496                 char *p = nd_get_link(nd);
497                 if (!IS_ERR(p))
498                         kfree(p);
499         }
500 }
501
502 /*
503  * UTF-8 to NLS charset  convert routine
504  * 
505  *
506  * Changed 8/10/01 by Will Dyson. Now use uni2char() / char2uni() rather than
507  * the nls tables directly
508  */
509
510 static int
511 befs_utf2nls(struct super_block *sb, const char *in,
512              int in_len, char **out, int *out_len)
513 {
514         struct nls_table *nls = BEFS_SB(sb)->nls;
515         int i, o;
516         wchar_t uni;
517         int unilen, utflen;
518         char *result;
519         int maxlen = in_len; /* The utf8->nls conversion can't make more chars */
520
521         befs_debug(sb, "---> utf2nls()");
522
523         if (!nls) {
524                 befs_error(sb, "befs_utf2nls called with no NLS table loaded");
525                 return -EINVAL;
526         }
527
528         *out = result = kmalloc(maxlen, GFP_NOFS);
529         if (!*out) {
530                 befs_error(sb, "befs_utf2nls() cannot allocate memory");
531                 *out_len = 0;
532                 return -ENOMEM;
533         }
534
535         for (i = o = 0; i < in_len; i += utflen, o += unilen) {
536
537                 /* convert from UTF-8 to Unicode */
538                 utflen = utf8_mbtowc(&uni, &in[i], in_len - i);
539                 if (utflen < 0) {
540                         goto conv_err;
541                 }
542
543                 /* convert from Unicode to nls */
544                 unilen = nls->uni2char(uni, &result[o], in_len - o);
545                 if (unilen < 0) {
546                         goto conv_err;
547                 }
548         }
549         result[o] = '\0';
550         *out_len = o;
551
552         befs_debug(sb, "<--- utf2nls()");
553
554         return o;
555
556       conv_err:
557         befs_error(sb, "Name using character set %s contains a character that "
558                    "cannot be converted to unicode.", nls->charset);
559         befs_debug(sb, "<--- utf2nls()");
560         kfree(result);
561         return -EILSEQ;
562 }
563
564 /**
565  * befs_nls2utf - Convert NLS string to utf8 encodeing
566  * @sb: Superblock
567  * @src: Input string buffer in NLS format
568  * @srclen: Length of input string in bytes
569  * @dest: The output string in UTF8 format
570  * @destlen: Length of the output buffer
571  * 
572  * Converts input string @src, which is in the format of the loaded NLS map,
573  * into a utf8 string.
574  * 
575  * The destination string @dest is allocated by this function and the caller is
576  * responsible for freeing it with kfree()
577  * 
578  * On return, *@destlen is the length of @dest in bytes.
579  *
580  * On success, the return value is the number of utf8 characters written to
581  * the output buffer @dest.
582  *  
583  * On Failure, a negative number coresponding to the error code is returned.
584  */
585
586 static int
587 befs_nls2utf(struct super_block *sb, const char *in,
588              int in_len, char **out, int *out_len)
589 {
590         struct nls_table *nls = BEFS_SB(sb)->nls;
591         int i, o;
592         wchar_t uni;
593         int unilen, utflen;
594         char *result;
595         int maxlen = 3 * in_len;
596
597         befs_debug(sb, "---> nls2utf()\n");
598
599         if (!nls) {
600                 befs_error(sb, "befs_nls2utf called with no NLS table loaded.");
601                 return -EINVAL;
602         }
603
604         *out = result = kmalloc(maxlen, GFP_NOFS);
605         if (!*out) {
606                 befs_error(sb, "befs_nls2utf() cannot allocate memory");
607                 *out_len = 0;
608                 return -ENOMEM;
609         }
610
611         for (i = o = 0; i < in_len; i += unilen, o += utflen) {
612
613                 /* convert from nls to unicode */
614                 unilen = nls->char2uni(&in[i], in_len - i, &uni);
615                 if (unilen < 0) {
616                         goto conv_err;
617                 }
618
619                 /* convert from unicode to UTF-8 */
620                 utflen = utf8_wctomb(&result[o], uni, 3);
621                 if (utflen <= 0) {
622                         goto conv_err;
623                 }
624         }
625
626         result[o] = '\0';
627         *out_len = o;
628
629         befs_debug(sb, "<--- nls2utf()");
630
631         return i;
632
633       conv_err:
634         befs_error(sb, "Name using charecter set %s contains a charecter that "
635                    "cannot be converted to unicode.", nls->charset);
636         befs_debug(sb, "<--- nls2utf()");
637         kfree(result);
638         return -EILSEQ;
639 }
640
641 /**
642  * Use the
643  *
644  */
645 enum {
646         Opt_uid, Opt_gid, Opt_charset, Opt_debug, Opt_err,
647 };
648
649 static match_table_t befs_tokens = {
650         {Opt_uid, "uid=%d"},
651         {Opt_gid, "gid=%d"},
652         {Opt_charset, "iocharset=%s"},
653         {Opt_debug, "debug"},
654         {Opt_err, NULL}
655 };
656
657 static int
658 parse_options(char *options, befs_mount_options * opts)
659 {
660         char *p;
661         substring_t args[MAX_OPT_ARGS];
662         int option;
663
664         /* Initialize options */
665         opts->uid = 0;
666         opts->gid = 0;
667         opts->use_uid = 0;
668         opts->use_gid = 0;
669         opts->iocharset = NULL;
670         opts->debug = 0;
671
672         if (!options)
673                 return 1;
674
675         while ((p = strsep(&options, ",")) != NULL) {
676                 int token;
677                 if (!*p)
678                         continue;
679
680                 token = match_token(p, befs_tokens, args);
681                 switch (token) {
682                 case Opt_uid:
683                         if (match_int(&args[0], &option))
684                                 return 0;
685                         if (option < 0) {
686                                 printk(KERN_ERR "BeFS: Invalid uid %d, "
687                                                 "using default\n", option);
688                                 break;
689                         }
690                         opts->uid = option;
691                         opts->use_uid = 1;
692                         break;
693                 case Opt_gid:
694                         if (match_int(&args[0], &option))
695                                 return 0;
696                         if (option < 0) {
697                                 printk(KERN_ERR "BeFS: Invalid gid %d, "
698                                                 "using default\n", option);
699                                 break;
700                         }
701                         opts->gid = option;
702                         opts->use_gid = 1;
703                         break;
704                 case Opt_charset:
705                         kfree(opts->iocharset);
706                         opts->iocharset = match_strdup(&args[0]);
707                         if (!opts->iocharset) {
708                                 printk(KERN_ERR "BeFS: allocation failure for "
709                                                 "iocharset string\n");
710                                 return 0;
711                         }
712                         break;
713                 case Opt_debug:
714                         opts->debug = 1;
715                         break;
716                 default:
717                         printk(KERN_ERR "BeFS: Unrecognized mount option \"%s\" "
718                                         "or missing value\n", p);
719                         return 0;
720                 }
721         }
722         return 1;
723 }
724
725 /* This function has the responsibiltiy of getting the
726  * filesystem ready for unmounting. 
727  * Basicly, we free everything that we allocated in
728  * befs_read_inode
729  */
730 static void
731 befs_put_super(struct super_block *sb)
732 {
733         if (BEFS_SB(sb)->mount_opts.iocharset) {
734                 kfree(BEFS_SB(sb)->mount_opts.iocharset);
735                 BEFS_SB(sb)->mount_opts.iocharset = NULL;
736         }
737
738         if (BEFS_SB(sb)->nls) {
739                 unload_nls(BEFS_SB(sb)->nls);
740                 BEFS_SB(sb)->nls = NULL;
741         }
742
743         if (sb->s_fs_info) {
744                 kfree(sb->s_fs_info);
745                 sb->s_fs_info = NULL;
746         }
747         return;
748 }
749
750 /* Allocate private field of the superblock, fill it.
751  *
752  * Finish filling the public superblock fields
753  * Make the root directory
754  * Load a set of NLS translations if needed.
755  */
756 static int
757 befs_fill_super(struct super_block *sb, void *data, int silent)
758 {
759         struct buffer_head *bh;
760         befs_sb_info *befs_sb;
761         befs_super_block *disk_sb;
762         struct inode *root;
763
764         const unsigned long sb_block = 0;
765         const off_t x86_sb_off = 512;
766
767         sb->s_fs_info = kmalloc(sizeof (*befs_sb), GFP_KERNEL);
768         if (sb->s_fs_info == NULL) {
769                 printk(KERN_ERR
770                        "BeFS(%s): Unable to allocate memory for private "
771                        "portion of superblock. Bailing.\n", sb->s_id);
772                 goto unaquire_none;
773         }
774         befs_sb = BEFS_SB(sb);
775         memset(befs_sb, 0, sizeof(befs_sb_info));
776
777         if (!parse_options((char *) data, &befs_sb->mount_opts)) {
778                 befs_error(sb, "cannot parse mount options");
779                 goto unaquire_priv_sbp;
780         }
781
782         befs_debug(sb, "---> befs_fill_super()");
783
784 #ifndef CONFIG_BEFS_RW
785         if (!(sb->s_flags & MS_RDONLY)) {
786                 befs_warning(sb,
787                              "No write support. Marking filesystem read-only");
788                 sb->s_flags |= MS_RDONLY;
789         }
790 #endif                          /* CONFIG_BEFS_RW */
791
792         /*
793          * Set dummy blocksize to read super block.
794          * Will be set to real fs blocksize later.
795          *
796          * Linux 2.4.10 and later refuse to read blocks smaller than
797          * the hardsect size for the device. But we also need to read at 
798          * least 1k to get the second 512 bytes of the volume.
799          * -WD 10-26-01
800          */ 
801         sb_min_blocksize(sb, 1024);
802
803         if (!(bh = sb_bread(sb, sb_block))) {
804                 befs_error(sb, "unable to read superblock");
805                 goto unaquire_priv_sbp;
806         }
807
808         /* account for offset of super block on x86 */
809         disk_sb = (befs_super_block *) bh->b_data;
810         if ((le32_to_cpu(disk_sb->magic1) == BEFS_SUPER_MAGIC1) ||
811             (be32_to_cpu(disk_sb->magic1) == BEFS_SUPER_MAGIC1)) {
812                 befs_debug(sb, "Using PPC superblock location");
813         } else {
814                 befs_debug(sb, "Using x86 superblock location");
815                 disk_sb =
816                     (befs_super_block *) ((void *) bh->b_data + x86_sb_off);
817         }
818
819         if (befs_load_sb(sb, disk_sb) != BEFS_OK)
820                 goto unaquire_bh;
821
822         befs_dump_super_block(sb, disk_sb);
823
824         brelse(bh);
825
826         if (befs_check_sb(sb) != BEFS_OK)
827                 goto unaquire_priv_sbp;
828
829         if( befs_sb->num_blocks > ~((sector_t)0) ) {
830                 befs_error(sb, "blocks count: %Lu "
831                         "is larger than the host can use",
832                         befs_sb->num_blocks);
833                 goto unaquire_priv_sbp;
834         }
835
836         /*
837          * set up enough so that it can read an inode
838          * Fill in kernel superblock fields from private sb
839          */
840         sb->s_magic = BEFS_SUPER_MAGIC;
841         /* Set real blocksize of fs */
842         sb_set_blocksize(sb, (ulong) befs_sb->block_size);
843         sb->s_op = (struct super_operations *) &befs_sops;
844         root = iget(sb, iaddr2blockno(sb, &(befs_sb->root_dir)));
845         sb->s_root = d_alloc_root(root);
846         if (!sb->s_root) {
847                 iput(root);
848                 befs_error(sb, "get root inode failed");
849                 goto unaquire_priv_sbp;
850         }
851
852         /* load nls library */
853         if (befs_sb->mount_opts.iocharset) {
854                 befs_debug(sb, "Loading nls: %s",
855                            befs_sb->mount_opts.iocharset);
856                 befs_sb->nls = load_nls(befs_sb->mount_opts.iocharset);
857                 if (!befs_sb->nls) {
858                         befs_warning(sb, "Cannot load nls %s"
859                                      "loding default nls",
860                                      befs_sb->mount_opts.iocharset);
861                         befs_sb->nls = load_nls_default();
862                 }
863         }
864
865         return 0;
866 /*****************/
867       unaquire_bh:
868         brelse(bh);
869
870       unaquire_priv_sbp:
871         kfree(sb->s_fs_info);
872
873       unaquire_none:
874         sb->s_fs_info = NULL;
875         return -EINVAL;
876 }
877
878 static int
879 befs_remount(struct super_block *sb, int *flags, char *data)
880 {
881         if (!(*flags & MS_RDONLY))
882                 return -EINVAL;
883         return 0;
884 }
885
886 static int
887 befs_statfs(struct super_block *sb, struct kstatfs *buf)
888 {
889
890         befs_debug(sb, "---> befs_statfs()");
891
892         buf->f_type = BEFS_SUPER_MAGIC;
893         buf->f_bsize = sb->s_blocksize;
894         buf->f_blocks = BEFS_SB(sb)->num_blocks;
895         buf->f_bfree = BEFS_SB(sb)->num_blocks - BEFS_SB(sb)->used_blocks;
896         buf->f_bavail = buf->f_bfree;
897         buf->f_files = 0;       /* UNKNOWN */
898         buf->f_ffree = 0;       /* UNKNOWN */
899         buf->f_namelen = BEFS_NAME_LEN;
900
901         befs_debug(sb, "<--- befs_statfs()");
902
903         return 0;
904 }
905
906 static struct super_block *
907 befs_get_sb(struct file_system_type *fs_type, int flags, const char *dev_name,
908             void *data)
909 {
910         return get_sb_bdev(fs_type, flags, dev_name, data, befs_fill_super);
911 }
912
913 static struct file_system_type befs_fs_type = {
914         .owner          = THIS_MODULE,
915         .name           = "befs",
916         .get_sb         = befs_get_sb,
917         .kill_sb        = kill_block_super,
918         .fs_flags       = FS_REQUIRES_DEV,      
919 };
920
921 static int __init
922 init_befs_fs(void)
923 {
924         int err;
925
926         printk(KERN_INFO "BeFS version: %s\n", BEFS_VERSION);
927
928         err = befs_init_inodecache();
929         if (err)
930                 goto unaquire_none;
931
932         err = register_filesystem(&befs_fs_type);
933         if (err)
934                 goto unaquire_inodecache;
935
936         return 0;
937
938 unaquire_inodecache:
939         befs_destroy_inodecache();
940
941 unaquire_none:
942         return err;
943 }
944
945 static void __exit
946 exit_befs_fs(void)
947 {
948         befs_destroy_inodecache();
949
950         unregister_filesystem(&befs_fs_type);
951 }
952
953 /*
954 Macros that typecheck the init and exit functions,
955 ensures that they are called at init and cleanup,
956 and eliminates warnings about unused functions.
957 */
958 module_init(init_befs_fs)
959 module_exit(exit_befs_fs)