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