OSDN Git Service

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