162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#include <linux/fs.h>
362306a36Sopenharmony_ci#include <linux/buffer_head.h>
462306a36Sopenharmony_ci#include <linux/exportfs.h>
562306a36Sopenharmony_ci#include <linux/iso_fs.h>
662306a36Sopenharmony_ci#include <asm/unaligned.h>
762306a36Sopenharmony_ci
862306a36Sopenharmony_cienum isofs_file_format {
962306a36Sopenharmony_ci	isofs_file_normal = 0,
1062306a36Sopenharmony_ci	isofs_file_sparse = 1,
1162306a36Sopenharmony_ci	isofs_file_compressed = 2,
1262306a36Sopenharmony_ci};
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci/*
1562306a36Sopenharmony_ci * iso fs inode data in memory
1662306a36Sopenharmony_ci */
1762306a36Sopenharmony_cistruct iso_inode_info {
1862306a36Sopenharmony_ci	unsigned long i_iget5_block;
1962306a36Sopenharmony_ci	unsigned long i_iget5_offset;
2062306a36Sopenharmony_ci	unsigned int i_first_extent;
2162306a36Sopenharmony_ci	unsigned char i_file_format;
2262306a36Sopenharmony_ci	unsigned char i_format_parm[3];
2362306a36Sopenharmony_ci	unsigned long i_next_section_block;
2462306a36Sopenharmony_ci	unsigned long i_next_section_offset;
2562306a36Sopenharmony_ci	off_t i_section_size;
2662306a36Sopenharmony_ci	struct inode vfs_inode;
2762306a36Sopenharmony_ci};
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci/*
3062306a36Sopenharmony_ci * iso9660 super-block data in memory
3162306a36Sopenharmony_ci */
3262306a36Sopenharmony_cistruct isofs_sb_info {
3362306a36Sopenharmony_ci	unsigned long s_ninodes;
3462306a36Sopenharmony_ci	unsigned long s_nzones;
3562306a36Sopenharmony_ci	unsigned long s_firstdatazone;
3662306a36Sopenharmony_ci	unsigned long s_log_zone_size;
3762306a36Sopenharmony_ci	unsigned long s_max_size;
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci	int           s_rock_offset; /* offset of SUSP fields within SU area */
4062306a36Sopenharmony_ci	s32           s_sbsector;
4162306a36Sopenharmony_ci	unsigned char s_joliet_level;
4262306a36Sopenharmony_ci	unsigned char s_mapping;
4362306a36Sopenharmony_ci	unsigned char s_check;
4462306a36Sopenharmony_ci	unsigned char s_session;
4562306a36Sopenharmony_ci	unsigned int  s_high_sierra:1;
4662306a36Sopenharmony_ci	unsigned int  s_rock:2;
4762306a36Sopenharmony_ci	unsigned int  s_cruft:1; /* Broken disks with high byte of length
4862306a36Sopenharmony_ci				  * containing junk */
4962306a36Sopenharmony_ci	unsigned int  s_nocompress:1;
5062306a36Sopenharmony_ci	unsigned int  s_hide:1;
5162306a36Sopenharmony_ci	unsigned int  s_showassoc:1;
5262306a36Sopenharmony_ci	unsigned int  s_overriderockperm:1;
5362306a36Sopenharmony_ci	unsigned int  s_uid_set:1;
5462306a36Sopenharmony_ci	unsigned int  s_gid_set:1;
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci	umode_t s_fmode;
5762306a36Sopenharmony_ci	umode_t s_dmode;
5862306a36Sopenharmony_ci	kgid_t s_gid;
5962306a36Sopenharmony_ci	kuid_t s_uid;
6062306a36Sopenharmony_ci	struct nls_table *s_nls_iocharset; /* Native language support table */
6162306a36Sopenharmony_ci};
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci#define ISOFS_INVALID_MODE ((umode_t) -1)
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_cistatic inline struct isofs_sb_info *ISOFS_SB(struct super_block *sb)
6662306a36Sopenharmony_ci{
6762306a36Sopenharmony_ci	return sb->s_fs_info;
6862306a36Sopenharmony_ci}
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_cistatic inline struct iso_inode_info *ISOFS_I(struct inode *inode)
7162306a36Sopenharmony_ci{
7262306a36Sopenharmony_ci	return container_of(inode, struct iso_inode_info, vfs_inode);
7362306a36Sopenharmony_ci}
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_cistatic inline int isonum_711(u8 *p)
7662306a36Sopenharmony_ci{
7762306a36Sopenharmony_ci	return *p;
7862306a36Sopenharmony_ci}
7962306a36Sopenharmony_cistatic inline int isonum_712(s8 *p)
8062306a36Sopenharmony_ci{
8162306a36Sopenharmony_ci	return *p;
8262306a36Sopenharmony_ci}
8362306a36Sopenharmony_cistatic inline unsigned int isonum_721(u8 *p)
8462306a36Sopenharmony_ci{
8562306a36Sopenharmony_ci	return get_unaligned_le16(p);
8662306a36Sopenharmony_ci}
8762306a36Sopenharmony_cistatic inline unsigned int isonum_722(u8 *p)
8862306a36Sopenharmony_ci{
8962306a36Sopenharmony_ci	return get_unaligned_be16(p);
9062306a36Sopenharmony_ci}
9162306a36Sopenharmony_cistatic inline unsigned int isonum_723(u8 *p)
9262306a36Sopenharmony_ci{
9362306a36Sopenharmony_ci	/* Ignore bigendian datum due to broken mastering programs */
9462306a36Sopenharmony_ci	return get_unaligned_le16(p);
9562306a36Sopenharmony_ci}
9662306a36Sopenharmony_cistatic inline unsigned int isonum_731(u8 *p)
9762306a36Sopenharmony_ci{
9862306a36Sopenharmony_ci	return get_unaligned_le32(p);
9962306a36Sopenharmony_ci}
10062306a36Sopenharmony_cistatic inline unsigned int isonum_732(u8 *p)
10162306a36Sopenharmony_ci{
10262306a36Sopenharmony_ci	return get_unaligned_be32(p);
10362306a36Sopenharmony_ci}
10462306a36Sopenharmony_cistatic inline unsigned int isonum_733(u8 *p)
10562306a36Sopenharmony_ci{
10662306a36Sopenharmony_ci	/* Ignore bigendian datum due to broken mastering programs */
10762306a36Sopenharmony_ci	return get_unaligned_le32(p);
10862306a36Sopenharmony_ci}
10962306a36Sopenharmony_ciextern int iso_date(u8 *, int);
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_cistruct inode;		/* To make gcc happy */
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ciextern int parse_rock_ridge_inode(struct iso_directory_record *, struct inode *, int relocated);
11462306a36Sopenharmony_ciextern int get_rock_ridge_filename(struct iso_directory_record *, char *, struct inode *);
11562306a36Sopenharmony_ciextern int isofs_name_translate(struct iso_directory_record *, char *, struct inode *);
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ciint get_joliet_filename(struct iso_directory_record *, unsigned char *, struct inode *);
11862306a36Sopenharmony_ciint get_acorn_filename(struct iso_directory_record *, char *, struct inode *);
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ciextern struct dentry *isofs_lookup(struct inode *, struct dentry *, unsigned int flags);
12162306a36Sopenharmony_ciextern struct buffer_head *isofs_bread(struct inode *, sector_t);
12262306a36Sopenharmony_ciextern int isofs_get_blocks(struct inode *, sector_t, struct buffer_head **, unsigned long);
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_cistruct inode *__isofs_iget(struct super_block *sb,
12562306a36Sopenharmony_ci			   unsigned long block,
12662306a36Sopenharmony_ci			   unsigned long offset,
12762306a36Sopenharmony_ci			   int relocated);
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_cistatic inline struct inode *isofs_iget(struct super_block *sb,
13062306a36Sopenharmony_ci				       unsigned long block,
13162306a36Sopenharmony_ci				       unsigned long offset)
13262306a36Sopenharmony_ci{
13362306a36Sopenharmony_ci	return __isofs_iget(sb, block, offset, 0);
13462306a36Sopenharmony_ci}
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_cistatic inline struct inode *isofs_iget_reloc(struct super_block *sb,
13762306a36Sopenharmony_ci					     unsigned long block,
13862306a36Sopenharmony_ci					     unsigned long offset)
13962306a36Sopenharmony_ci{
14062306a36Sopenharmony_ci	return __isofs_iget(sb, block, offset, 1);
14162306a36Sopenharmony_ci}
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci/* Because the inode number is no longer relevant to finding the
14462306a36Sopenharmony_ci * underlying meta-data for an inode, we are free to choose a more
14562306a36Sopenharmony_ci * convenient 32-bit number as the inode number.  The inode numbering
14662306a36Sopenharmony_ci * scheme was recommended by Sergey Vlasov and Eric Lammerts. */
14762306a36Sopenharmony_cistatic inline unsigned long isofs_get_ino(unsigned long block,
14862306a36Sopenharmony_ci					  unsigned long offset,
14962306a36Sopenharmony_ci					  unsigned long bufbits)
15062306a36Sopenharmony_ci{
15162306a36Sopenharmony_ci	return (block << (bufbits - 5)) | (offset >> 5);
15262306a36Sopenharmony_ci}
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci/* Every directory can have many redundant directory entries scattered
15562306a36Sopenharmony_ci * throughout the directory tree.  First there is the directory entry
15662306a36Sopenharmony_ci * with the name of the directory stored in the parent directory.
15762306a36Sopenharmony_ci * Then, there is the "." directory entry stored in the directory
15862306a36Sopenharmony_ci * itself.  Finally, there are possibly many ".." directory entries
15962306a36Sopenharmony_ci * stored in all the subdirectories.
16062306a36Sopenharmony_ci *
16162306a36Sopenharmony_ci * In order for the NFS get_parent() method to work and for the
16262306a36Sopenharmony_ci * general consistency of the dcache, we need to make sure the
16362306a36Sopenharmony_ci * "i_iget5_block" and "i_iget5_offset" all point to exactly one of
16462306a36Sopenharmony_ci * the many redundant entries for each directory.  We normalize the
16562306a36Sopenharmony_ci * block and offset by always making them point to the "."  directory.
16662306a36Sopenharmony_ci *
16762306a36Sopenharmony_ci * Notice that we do not use the entry for the directory with the name
16862306a36Sopenharmony_ci * that is located in the parent directory.  Even though choosing this
16962306a36Sopenharmony_ci * first directory is more natural, it is much easier to find the "."
17062306a36Sopenharmony_ci * entry in the NFS get_parent() method because it is implicitly
17162306a36Sopenharmony_ci * encoded in the "extent + ext_attr_length" fields of _all_ the
17262306a36Sopenharmony_ci * redundant entries for the directory.  Thus, it can always be
17362306a36Sopenharmony_ci * reached regardless of which directory entry you have in hand.
17462306a36Sopenharmony_ci *
17562306a36Sopenharmony_ci * This works because the "." entry is simply the first directory
17662306a36Sopenharmony_ci * record when you start reading the file that holds all the directory
17762306a36Sopenharmony_ci * records, and this file starts at "extent + ext_attr_length" blocks.
17862306a36Sopenharmony_ci * Because the "." entry is always the first entry listed in the
17962306a36Sopenharmony_ci * directories file, the normalized "offset" value is always 0.
18062306a36Sopenharmony_ci *
18162306a36Sopenharmony_ci * You should pass the directory entry in "de".  On return, "block"
18262306a36Sopenharmony_ci * and "offset" will hold normalized values.  Only directories are
18362306a36Sopenharmony_ci * affected making it safe to call even for non-directory file
18462306a36Sopenharmony_ci * types. */
18562306a36Sopenharmony_cistatic inline void
18662306a36Sopenharmony_ciisofs_normalize_block_and_offset(struct iso_directory_record* de,
18762306a36Sopenharmony_ci				 unsigned long *block,
18862306a36Sopenharmony_ci				 unsigned long *offset)
18962306a36Sopenharmony_ci{
19062306a36Sopenharmony_ci	/* Only directories are normalized. */
19162306a36Sopenharmony_ci	if (de->flags[0] & 2) {
19262306a36Sopenharmony_ci		*offset = 0;
19362306a36Sopenharmony_ci		*block = (unsigned long)isonum_733(de->extent)
19462306a36Sopenharmony_ci			+ (unsigned long)isonum_711(de->ext_attr_length);
19562306a36Sopenharmony_ci	}
19662306a36Sopenharmony_ci}
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ciextern const struct inode_operations isofs_dir_inode_operations;
19962306a36Sopenharmony_ciextern const struct file_operations isofs_dir_operations;
20062306a36Sopenharmony_ciextern const struct address_space_operations isofs_symlink_aops;
20162306a36Sopenharmony_ciextern const struct export_operations isofs_export_ops;
202