Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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