more changes on original files
[linux-2.4.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/blkdev.h>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/errno.h>
13 #include <linux/fs.h>
14 #include <linux/stat.h>
15 #include <linux/string.h>
16 #include <linux/nls.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 EXPORT_NO_SYMBOLS;
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 *, void *, filldir_t);
35 static int befs_get_block(struct inode *, long, struct buffer_head *, int);
36 static int befs_readpage(struct file *file, struct page *page);
37 static int befs_bmap(struct address_space *mapping, long block);
38 static struct dentry *befs_lookup(struct inode *, struct dentry *);
39 static void befs_read_inode(struct inode *ino);
40 static void befs_clear_inode(struct inode *ino);
41 static int befs_init_inodecache(void);
42 static void befs_destroy_inodecache(void);
43
44 static int befs_readlink(struct dentry *, char *, int);
45 static int befs_follow_link(struct dentry *, struct nameidata *nd);
46
47 static int befs_utf2nls(struct super_block *sb, const char *in, int in_len,
48                         char **out, int *out_len);
49 static int befs_nls2utf(struct super_block *sb, const char *in, int in_len,
50                         char **out, int *out_len);
51
52 static void befs_put_super(struct super_block *);
53 static struct super_block *befs_read_super(struct super_block *, void *, int);
54 static int befs_remount(struct super_block *, int *, char *);
55 static int befs_statfs(struct super_block *, struct statfs *);
56 static int parse_options(char *, befs_mount_options *);
57
58 /* slab cache for befs_inode_info objects */
59 static kmem_cache_t *befs_inode_cachep;
60
61 static const struct super_operations befs_sops = {
62         read_inode:befs_read_inode,     /* initialize & read inode */
63         clear_inode:befs_clear_inode,   /* uninit inode */
64         put_super:befs_put_super,       /* uninit super */
65         statfs:befs_statfs,     /* statfs */
66         remount_fs:befs_remount,
67 };
68
69 struct file_operations befs_dir_operations = {
70         read:generic_read_dir,
71         readdir:befs_readdir,
72 };
73
74 struct inode_operations befs_dir_inode_operations = {
75         lookup:befs_lookup,
76 };
77
78 struct file_operations befs_file_operations = {
79         llseek:default_llseek,
80         read:generic_file_read,
81         mmap:generic_file_mmap,
82 };
83
84 struct inode_operations befs_file_inode_operations = {
85 };
86
87 struct address_space_operations befs_aops = {
88         readpage:befs_readpage,
89         sync_page:block_sync_page,
90         bmap:befs_bmap,
91 };
92
93 static struct inode_operations befs_symlink_inode_operations = {
94         readlink:befs_readlink,
95         follow_link:befs_follow_link,
96 };
97
98 /* 
99  * Called by generic_file_read() to read a page of data
100  * 
101  * In turn, simply calls a generic block read function and
102  * passes it the address of befs_get_block, for mapping file
103  * positions to disk blocks.
104  */
105 static int
106 befs_readpage(struct file *file, struct page *page)
107 {
108         return block_read_full_page(page, befs_get_block);
109 }
110
111 static int
112 befs_bmap(struct address_space *mapping, long block)
113 {
114         return generic_block_bmap(mapping, block, befs_get_block);
115 }
116
117 /* 
118  * Generic function to map a file position (block) to a 
119  * disk offset (passed back in bh_result).
120  *
121  * Used by many higher level functions.
122  *
123  * Calls befs_fblock2brun() in datastream.c to do the real work.
124  *
125  * -WD 10-26-01
126  */
127
128 static int
129 befs_get_block(struct inode *inode, long block,
130                struct buffer_head *bh_result, int create)
131 {
132         struct super_block *sb = inode->i_sb;
133         befs_data_stream *ds = &BEFS_I(inode)->i_data.ds;
134         befs_block_run run = BAD_IADDR;
135         int res = 0;
136         ulong disk_off;
137
138         befs_debug(sb, "---> befs_get_block() for inode %lu, block %ld",
139                    inode->i_ino, block);
140
141         if (block < 0) {
142                 befs_error(sb, "befs_get_block() was asked for a block "
143                            "number less than zero: block %ld in inode %lu",
144                            block, inode->i_ino);
145                 return -EIO;
146         }
147
148         if (create) {
149                 befs_error(sb, "befs_get_block() was asked to write to "
150                            "block %ld in inode %lu", block, inode->i_ino);
151                 return -EPERM;
152         }
153
154         res = befs_fblock2brun(sb, ds, block, &run);
155         if (res != BEFS_OK) {
156                 befs_error(sb,
157                            "<--- befs_get_block() for inode %lu, block "
158                            "%ld ERROR", inode->i_ino, block);
159                 return -EFBIG;
160         }
161
162         disk_off = (ulong) iaddr2blockno(sb, &run);
163
164         bh_result->b_dev = inode->i_dev;
165         bh_result->b_blocknr = disk_off;
166         bh_result->b_state |= (1UL << BH_Mapped);
167
168         befs_debug(sb, "<--- befs_get_block() for inode %lu, block %ld, "
169                    "disk address %lu", inode->i_ino, block, disk_off);
170
171         return 0;
172 }
173
174 static struct dentry *
175 befs_lookup(struct inode *dir, struct dentry *dentry)
176 {
177         struct inode *inode = NULL;
178         struct super_block *sb = dir->i_sb;
179         befs_data_stream *ds = &BEFS_I(dir)->i_data.ds;
180         befs_off_t offset;
181         int ret;
182         int utfnamelen;
183         char *utfname;
184         const char *name = dentry->d_name.name;
185
186         befs_debug(sb, "---> befs_lookup() "
187                    "name %s inode %ld", dentry->d_name.name, dir->i_ino);
188
189         /* Convert to UTF-8 */
190         if (BEFS_SB(sb)->nls) {
191                 ret =
192                     befs_nls2utf(sb, name, strlen(name), &utfname, &utfnamelen);
193                 if (ret < 0) {
194                         befs_debug(sb, "<--- befs_lookup() ERROR");
195                         return ERR_PTR(ret);
196                 }
197                 ret = befs_btree_find(sb, ds, utfname, &offset);
198                 kfree(utfname);
199
200         } else {
201                 ret = befs_btree_find(sb, ds, dentry->d_name.name, &offset);
202         }
203
204         if (ret == BEFS_BT_NOT_FOUND) {
205                 befs_debug(sb, "<--- befs_lookup() %s not found",
206                            dentry->d_name.name);
207                 return ERR_PTR(-ENOENT);
208
209         } else if (ret != BEFS_OK || offset == 0) {
210                 befs_warning(sb, "<--- befs_lookup() Error");
211                 return ERR_PTR(-ENODATA);
212         }
213
214         inode = iget(dir->i_sb, (ino_t) offset);
215         if (!inode)
216                 return ERR_PTR(-EACCES);
217
218         d_add(dentry, inode);
219
220         befs_debug(sb, "<--- befs_lookup()");
221
222         return NULL;
223 }
224
225 static int
226 befs_readdir(struct file *filp, void *dirent, filldir_t filldir)
227 {
228         struct inode *inode = filp->f_dentry->d_inode;
229         struct super_block *sb = inode->i_sb;
230         befs_data_stream *ds = &BEFS_I(inode)->i_data.ds;
231         befs_off_t value;
232         int result;
233         size_t keysize;
234         unsigned char d_type;
235         char keybuf[BEFS_NAME_LEN + 1];
236         char *nlsname;
237         int nlsnamelen;
238         const char *dirname = filp->f_dentry->d_name.name;
239
240         befs_debug(sb, "---> befs_readdir() "
241                    "name %s, inode %ld, filp->f_pos %Ld",
242                    dirname, inode->i_ino, filp->f_pos);
243
244         result = befs_btree_read(sb, ds, filp->f_pos, BEFS_NAME_LEN + 1,
245                                  keybuf, &keysize, &value);
246
247         if (result == BEFS_ERR) {
248                 befs_debug(sb, "<--- befs_readdir() ERROR");
249                 befs_error(sb, "IO error reading %s (inode %lu)",
250                            dirname, inode->i_ino);
251                 return -EIO;
252
253         } else if (result == BEFS_BT_END) {
254                 befs_debug(sb, "<--- befs_readdir() END");
255                 return 0;
256
257         } else if (result == BEFS_BT_EMPTY) {
258                 befs_debug(sb, "<--- befs_readdir() Empty directory");
259                 return 0;
260         }
261
262         d_type = DT_UNKNOWN;
263
264         /* Convert to NLS */
265         if (BEFS_SB(sb)->nls) {
266                 result =
267                     befs_utf2nls(sb, keybuf, keysize, &nlsname, &nlsnamelen);
268                 if (result < 0) {
269                         befs_debug(sb, "<--- befs_readdir() ERROR");
270                         return result;
271                 }
272                 result = filldir(dirent, nlsname, nlsnamelen, filp->f_pos,
273                                  (ino_t) value, d_type);
274                 kfree(nlsname);
275
276         } else {
277                 result = filldir(dirent, keybuf, keysize, filp->f_pos,
278                                  (ino_t) value, d_type);
279         }
280
281         filp->f_pos++;
282
283         befs_debug(sb, "<--- befs_readdir() filp->f_pos %Ld", filp->f_pos);
284
285         return 0;
286 }
287
288 static void
289 befs_clear_inode(struct inode *inode)
290 {
291         befs_inode_info *b_ino = BEFS_I(inode);
292         inode->u.generic_ip = NULL;
293
294         if (b_ino) {
295                 kmem_cache_free(befs_inode_cachep, b_ino);
296         }
297         return;
298 }
299
300 static void
301 befs_read_inode(struct inode *inode)
302 {
303         struct buffer_head *bh = NULL;
304         befs_inode *raw_inode = NULL;
305
306         struct super_block *sb = inode->i_sb;
307         befs_sb_info *befs_sb = BEFS_SB(sb);
308         befs_inode_info *befs_ino = NULL;
309
310         befs_debug(sb, "---> befs_read_inode() " "inode = %lu", inode->i_ino);
311
312         inode->u.generic_ip = kmem_cache_alloc(befs_inode_cachep, GFP_NOFS);
313         if (inode->u.generic_ip == NULL) {
314                 befs_error(sb, "Unable to allocate memory for private "
315                            "portion of inode %lu.", inode->i_ino);
316                 goto unaquire_none;
317         }
318         befs_ino = BEFS_I(inode);
319
320         /* convert from vfs's inode number to befs's inode number */
321         befs_ino->i_inode_num = blockno2iaddr(sb, inode->i_ino);
322
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);
326
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_ino_info;
332         }
333
334         raw_inode = (befs_inode *) bh->b_data;
335
336         befs_dump_inode(sb, raw_inode);
337
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;
341         }
342
343         inode->i_mode = (umode_t) fs32_to_cpu(sb, raw_inode->mode);
344
345         /*
346          * set uid and gid.  But since current BeOS is single user OS, so
347          * you can change by "uid" or "gid" options.
348          */
349
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);
354
355         inode->i_nlink = 1;
356
357         /*
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          */
362
363         inode->i_mtime =
364             (time_t) (fs64_to_cpu(sb, raw_inode->last_modified_time) >> 16);
365         inode->i_ctime = inode->i_mtime;
366         inode->i_atime = inode->i_mtime;
367         inode->i_blkbits = befs_sb->block_shift;
368         inode->i_blksize = befs_sb->block_size;
369
370         befs_ino->i_inode_num = fsrun_to_cpu(sb, raw_inode->inode_num);
371         befs_ino->i_parent = fsrun_to_cpu(sb, raw_inode->parent);
372         befs_ino->i_attribute = fsrun_to_cpu(sb, raw_inode->attributes);
373         befs_ino->i_flags = fs32_to_cpu(sb, raw_inode->flags);
374
375         if (S_ISLNK(inode->i_mode) && !(inode->i_flags & BEFS_LONG_SYMLINK)) {
376                 inode->i_size = 0;
377                 inode->i_blocks = befs_sb->block_size / VFS_BLOCK_SIZE;
378                 strncpy(befs_ino->i_data.symlink, raw_inode->data.symlink,
379                         BEFS_SYMLINK_LEN);
380         } else {
381                 int num_blks;
382
383                 befs_ino->i_data.ds =
384                     fsds_to_cpu(sb, raw_inode->data.datastream);
385
386                 num_blks = befs_count_blocks(sb, &befs_ino->i_data.ds);
387                 inode->i_blocks =
388                     num_blks * (befs_sb->block_size / VFS_BLOCK_SIZE);
389                 inode->i_size = befs_ino->i_data.ds.size;
390         }
391
392         inode->i_mapping->a_ops = &befs_aops;
393
394         if (S_ISREG(inode->i_mode)) {
395                 inode->i_fop = &befs_file_operations;
396                 inode->i_op = &befs_file_inode_operations;
397         } else if (S_ISDIR(inode->i_mode)) {
398                 inode->i_op = &befs_dir_inode_operations;
399                 inode->i_fop = &befs_dir_operations;
400         } else if (S_ISLNK(inode->i_mode)) {
401                 inode->i_op = &befs_symlink_inode_operations;
402         } else {
403                 befs_error(sb, "Inode %lu is not a regular file, "
404                            "directory or symlink. THAT IS WRONG! BeFS has no "
405                            "on disk special files", inode->i_ino);
406                 goto unaquire_bh;
407         }
408
409         brelse(bh);
410         befs_debug(sb, "<--- befs_read_inode()");
411         return;
412
413       unaquire_bh:
414         brelse(bh);
415
416       unaquire_ino_info:
417         kmem_cache_free(befs_inode_cachep, inode->u.generic_ip);
418
419       unaquire_none:
420         make_bad_inode(inode);
421         inode->u.generic_ip = NULL;
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,
436                                               NULL, 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         struct super_block *sb = dentry->d_sb;
467         befs_inode_info *befs_ino = BEFS_I(dentry->d_inode);
468         char *link;
469         int res;
470
471         if (befs_ino->i_flags & BEFS_LONG_SYMLINK) {
472                 befs_data_stream *data = &befs_ino->i_data.ds;
473                 befs_off_t linklen = data->size;
474
475                 befs_debug(sb, "Follow long symlink");
476
477                 link = kmalloc(linklen, GFP_NOFS);
478                 if (link == NULL)
479                         return -ENOMEM;
480
481                 if (befs_read_lsymlink(sb, data, link, linklen) != linklen) {
482                         kfree(link);
483                         befs_error(sb, "Failed to read entire long symlink");
484                         return -EIO;
485                 }
486
487                 res = vfs_follow_link(nd, link);
488
489                 kfree(link);
490         } else {
491                 link = befs_ino->i_data.symlink;
492                 res = vfs_follow_link(nd, link);
493         }
494
495         return res;
496 }
497
498 static int
499 befs_readlink(struct dentry *dentry, char *buffer, int buflen)
500 {
501         struct super_block *sb = dentry->d_sb;
502         befs_inode_info *befs_ino = BEFS_I(dentry->d_inode);
503         char *link;
504         int res;
505
506         if (befs_ino->i_flags & BEFS_LONG_SYMLINK) {
507                 befs_data_stream *data = &befs_ino->i_data.ds;
508                 befs_off_t linklen = data->size;
509
510                 befs_debug(sb, "Read long symlink");
511
512                 link = kmalloc(linklen, GFP_NOFS);
513                 if (link == NULL)
514                         return -ENOMEM;
515
516                 if (befs_read_lsymlink(sb, data, link, linklen) != linklen) {
517                         kfree(link);
518                         befs_error(sb, "Failed to read entire long symlink");
519                         return -EIO;
520                 }
521
522                 res = vfs_readlink(dentry, buffer, buflen, link);
523
524                 kfree(link);
525         } else {
526                 link = befs_ino->i_data.symlink;
527                 res = vfs_readlink(dentry, buffer, buflen, link);
528         }
529
530         return res;
531 }
532
533 /*
534  * UTF-8 to NLS charset  convert routine
535  *
536  * Changed 8/10/01 by Will Dyson. Now use uni2char() / char2uni() rather than
537  * the nls tables directly
538  */
539
540 static int
541 befs_utf2nls(struct super_block *sb, const char *in,
542              int in_len, char **out, int *out_len)
543 {
544         struct nls_table *nls = BEFS_SB(sb)->nls;
545         int i, o;
546         wchar_t uni;
547         int unilen, utflen;
548         char *result;
549         int maxlen = in_len;    /* The utf8->nls conversion cant make more chars */
550
551         befs_debug(sb, "---> utf2nls()");
552
553         if (!nls) {
554                 befs_error(sb, "befs_utf2nls called with no NLS table loaded");
555                 return -EINVAL;
556         }
557
558         *out = result = kmalloc(maxlen, GFP_NOFS);
559         if (!*out) {
560                 befs_error(sb, "befs_utf2nls() cannot allocate memory");
561                 *out_len = 0;
562                 return -ENOMEM;
563         }
564
565         for (i = o = 0; i < in_len; i += utflen, o += unilen) {
566
567                 /* convert from UTF-8 to Unicode */
568                 utflen = utf8_mbtowc(&uni, &in[i], in_len - i);
569                 if (utflen < 0) {
570                         goto conv_err;
571                 }
572
573                 /* convert from Unicode to nls */
574                 unilen = nls->uni2char(uni, &result[o], 1);
575                 if (unilen < 0) {
576                         goto conv_err;
577                 }
578         }
579         result[o] = '\0';
580         *out_len = o;
581
582         befs_debug(sb, "<--- utf2nls()");
583
584         return o;
585
586       conv_err:
587         befs_error(sb, "Name using charecter set %s contains a charecter that "
588                    "cannot be converted to unicode.", nls->charset);
589         befs_debug(sb, "<--- utf2nls()");
590         kfree(result);
591         return -EILSEQ;
592 }
593
594 /**
595  * befs_nls2utf - Convert NLS string to utf8 encodeing
596  * @sb: Superblock
597  * @src: Input string buffer in NLS format
598  * @srclen: Length of input string in bytes
599  * @dest: The output string in UTF8 format
600  * @destlen: Length of the output buffer
601  * 
602  * Converts input string @src, which is in the format of the loaded NLS map,
603  * into a utf8 string.
604  * 
605  * The destination string @dest is allocated by this function and the caller is
606  * responsible for freeing it with kfree()
607  * 
608  * On return, *@destlen is the length of @dest in bytes.
609  *
610  * On success, the return value is the number of utf8 charecters written to
611  * the ouput buffer @dest.
612  *  
613  * On Failure, a negative number coresponding to the error code is returned.
614  */
615
616 static int
617 befs_nls2utf(struct super_block *sb, const char *in,
618              int in_len, char **out, int *out_len)
619 {
620         struct nls_table *nls = BEFS_SB(sb)->nls;
621         int i, o;
622         wchar_t uni;
623         int unilen, utflen;
624         char *result;
625         int maxlen = 3 * in_len;
626
627         befs_debug(sb, "---> nls2utf()\n");
628
629         if (!nls) {
630                 befs_error(sb, "befs_nls2utf called with no NLS table loaded.");
631                 return -EINVAL;
632         }
633
634         *out = result = kmalloc(maxlen, GFP_NOFS);
635         if (!*out) {
636                 befs_error(sb, "befs_nls2utf() cannot allocate memory");
637                 *out_len = 0;
638                 return -ENOMEM;
639         }
640
641         for (i = o = 0; i < in_len; i += unilen, o += utflen) {
642
643                 /* convert from nls to unicode */
644                 unilen = nls->char2uni(&in[i], in_len - i, &uni);
645                 if (unilen < 0) {
646                         goto conv_err;
647                 }
648
649                 /* convert from unicode to UTF-8 */
650                 utflen = utf8_wctomb(&result[o], uni, 3);
651                 if (utflen <= 0) {
652                         goto conv_err;
653                 }
654         }
655
656         result[o] = '\0';
657         *out_len = o;
658
659         befs_debug(sb, "<--- nls2utf()");
660
661         return i;
662
663       conv_err:
664         befs_error(sb, "Name using charecter set %s contains a charecter that "
665                    "cannot be converted to unicode.", nls->charset);
666         befs_debug(sb, "<--- nls2utf()");
667         kfree(result);
668         return -EILSEQ;
669 }
670
671 /****Superblock****/
672
673 static int
674 parse_options(char *options, befs_mount_options * opts)
675 {
676         char *this_char;
677         char *value;
678         int ret = 1;
679
680         /* Initialize options */
681         opts->uid = 0;
682         opts->gid = 0;
683         opts->use_uid = 0;
684         opts->use_gid = 0;
685         opts->iocharset = NULL;
686         opts->debug = 0;
687
688         if (!options)
689                 return ret;
690
691         for (this_char = strtok(options, ","); this_char != NULL;
692              this_char = strtok(NULL, ",")) {
693
694                 if ((value = strchr(this_char, '=')) != NULL)
695                         *value++ = 0;
696
697                 if (!strcmp(this_char, "uid")) {
698                         if (!value || !*value) {
699                                 ret = 0;
700                         } else {
701                                 opts->uid = simple_strtoul(value, &value, 0);
702                                 opts->use_uid = 1;
703                                 if (*value) {
704                                         printk(KERN_ERR "BEFS: Invalid uid "
705                                                "option: %s\n", value);
706                                         ret = 0;
707                                 }
708                         }
709                 } else if (!strcmp(this_char, "gid")) {
710                         if (!value || !*value)
711                                 ret = 0;
712                         else {
713                                 opts->gid = simple_strtoul(value, &value, 0);
714                                 opts->use_gid = 1;
715                                 if (*value) {
716                                         printk(KERN_ERR
717                                                "BEFS: Invalid gid option: "
718                                                "%s\n", value);
719                                         ret = 0;
720                                 }
721                         }
722                 } else if (!strcmp(this_char, "iocharset") && value) {
723                         char *p = value;
724                         int len;
725
726                         while (*value && *value != ',')
727                                 value++;
728                         len = value - p;
729                         if (len) {
730                                 char *buffer = kmalloc(len + 1, GFP_NOFS);
731                                 if (buffer) {
732                                         opts->iocharset = buffer;
733                                         memcpy(buffer, p, len);
734                                         buffer[len] = 0;
735
736                                 } else {
737                                         printk(KERN_ERR "BEFS: "
738                                                "cannot allocate memory\n");
739                                         ret = 0;
740                                 }
741                         }
742                 } else if (!strcmp(this_char, "debug")) {
743                         opts->debug = 1;
744                 }
745         }
746
747         return ret;
748 }
749
750 /* This function has the responsibiltiy of getting the
751  * filesystem ready for unmounting. 
752  * Basicly, we free everything that we allocated in
753  * befs_read_inode
754  */
755 static void
756 befs_put_super(struct super_block *sb)
757 {
758         if (BEFS_SB(sb)->mount_opts.iocharset) {
759                 kfree(BEFS_SB(sb)->mount_opts.iocharset);
760                 BEFS_SB(sb)->mount_opts.iocharset = NULL;
761         }
762
763         if (BEFS_SB(sb)->nls) {
764                 unload_nls(BEFS_SB(sb)->nls);
765                 BEFS_SB(sb)->nls = NULL;
766         }
767
768         if (sb->u.generic_sbp) {
769                 kfree(sb->u.generic_sbp);
770                 sb->u.generic_sbp = NULL;
771         }
772         return;
773 }
774
775 /* Allocate private field of the superblock, fill it.
776  *
777  * Finish filling the public superblock fields
778  * Make the root directory
779  * Load a set of NLS translations if needed.
780  */
781 static struct super_block *
782 befs_read_super(struct super_block *sb, void *data, int silent)
783 {
784         struct buffer_head *bh;
785         befs_sb_info *befs_sb;
786         befs_super_block *disk_sb;
787         int blocksize;
788
789         const unsigned long sb_block = 0;
790         const off_t x86_sb_off = 512;
791
792         sb->u.generic_sbp = kmalloc(sizeof (struct befs_sb_info), GFP_NOFS);
793         if (sb->u.generic_sbp == NULL) {
794                 printk(KERN_ERR
795                        "BeFS(%s): Unable to allocate memory for private "
796                        "portion of superblock. Bailing.\n",
797                        kdevname(sb->s_dev));
798                 goto unaquire_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 unaquire_priv_sbp;
805         }
806
807         befs_debug(sb, "---> befs_read_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         blocksize = max_t(int, get_hardsect_size(sb->s_dev), 1024);
827         set_blocksize(sb->s_dev, blocksize);
828
829         if (!(bh = bread(sb->s_dev, sb_block, blocksize))) {
830                 befs_error(sb, "unable to read superblock");
831                 goto unaquire_priv_sbp;
832         }
833
834         /* account for offset of super block on x86 */
835         disk_sb = (befs_super_block *) bh->b_data;
836         if ((le32_to_cpu(disk_sb->magic1) == BEFS_SUPER_MAGIC1) ||
837             (be32_to_cpu(disk_sb->magic1) == BEFS_SUPER_MAGIC1)) {
838                 befs_debug(sb, "Using PPC superblock location");
839         } else {
840                 befs_debug(sb, "Using x86 superblock location");
841                 disk_sb =
842                     (befs_super_block *) ((void *) bh->b_data + x86_sb_off);
843         }
844
845         if (befs_load_sb(sb, disk_sb) != BEFS_OK)
846                 goto unaquire_bh;
847
848         befs_dump_super_block(sb, disk_sb);
849
850         brelse(bh);
851
852         if (befs_check_sb(sb) != BEFS_OK)
853                 goto unaquire_priv_sbp;
854
855         /*
856          * set up enough so that it can read an inode
857          * Fill in kernel superblock fields from private sb
858          */
859         sb->s_magic = BEFS_SUPER_MAGIC;
860         sb->s_blocksize = (ulong) befs_sb->block_size;
861         sb->s_blocksize_bits = (unsigned char) befs_sb->block_shift;
862         sb->s_op = (struct super_operations *) &befs_sops;
863         sb->s_root =
864             d_alloc_root(iget(sb, iaddr2blockno(sb, &(befs_sb->root_dir))));
865         if (!sb->s_root) {
866                 befs_error(sb, "get root inode failed");
867                 goto unaquire_priv_sbp;
868         }
869
870         /* load nls library */
871         if (befs_sb->mount_opts.iocharset) {
872                 befs_debug(sb, "Loading nls: %s",
873                            befs_sb->mount_opts.iocharset);
874                 befs_sb->nls = load_nls(befs_sb->mount_opts.iocharset);
875                 if (!befs_sb->nls) {
876                         befs_warning(sb, "Cannot load nls %s"
877                                      "loding default nls",
878                                      befs_sb->mount_opts.iocharset);
879                         befs_sb->nls = load_nls_default();
880                 }
881         }
882
883         /* Set real blocksize of fs */
884         set_blocksize(sb->s_dev, (int) befs_sb->block_size);
885
886         return sb;
887 /*****************/
888       unaquire_bh:
889         brelse(bh);
890
891       unaquire_priv_sbp:
892         kfree(sb->u.generic_sbp);
893
894       unaquire_none:
895         sb->s_dev = 0;
896         sb->u.generic_sbp = NULL;
897         return NULL;
898 }
899
900 static int
901 befs_remount(struct super_block *sb, int *flags, char *data)
902 {
903         if (!(*flags & MS_RDONLY))
904                 return -EINVAL;
905         return 0;
906 }
907
908 static int
909 befs_statfs(struct super_block *sb, struct statfs *buf)
910 {
911
912         befs_debug(sb, "---> befs_statfs()");
913
914         buf->f_type = BEFS_SUPER_MAGIC;
915         buf->f_bsize = sb->s_blocksize;
916         buf->f_blocks = BEFS_SB(sb)->num_blocks;
917         buf->f_bfree = BEFS_SB(sb)->num_blocks - BEFS_SB(sb)->used_blocks;
918         buf->f_bavail = buf->f_bfree;
919         buf->f_files = 0;       /* UNKNOWN */
920         buf->f_ffree = 0;       /* UNKNOWN */
921         buf->f_namelen = BEFS_NAME_LEN;
922
923         befs_debug(sb, "<--- befs_statfs()");
924
925         return 0;
926 }
927
928 /*
929         Makes a variable of type file_system_type, 
930         named befs_fs_tipe, identified by the "befs" string,
931         and containing a reference to the befs_read_super function
932         
933         Macro declared in <linux/fs.h>
934 */
935 static DECLARE_FSTYPE_DEV(befs_fs_type, "befs", befs_read_super);
936
937 static int __init
938 init_befs_fs(void)
939 {
940         int err;
941
942         printk(KERN_INFO "BeFS version: %s\n", BEFS_VERSION);
943
944         err = befs_init_inodecache();
945         if (err)
946                 return err;
947
948         return register_filesystem(&befs_fs_type);
949 }
950
951 static void __exit
952 exit_befs_fs(void)
953 {
954         befs_destroy_inodecache();
955
956         unregister_filesystem(&befs_fs_type);
957 }
958
959 /*
960 Macros that typecheck the init and exit functions,
961 ensures that they are called at init and cleanup,
962 and eliminates warnings about unused functions.
963 */
964 module_init(init_befs_fs)
965     module_exit(exit_befs_fs)