162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * fs/isofs/export.c
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci *  (C) 2004  Paul Serice - The new inode scheme requires switching
662306a36Sopenharmony_ci *                          from iget() to iget5_locked() which means
762306a36Sopenharmony_ci *                          the NFS export operations have to be hand
862306a36Sopenharmony_ci *                          coded because the default routines rely on
962306a36Sopenharmony_ci *                          iget().
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * The following files are helpful:
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci *     Documentation/filesystems/nfs/exporting.rst
1462306a36Sopenharmony_ci *     fs/exportfs/expfs.c.
1562306a36Sopenharmony_ci */
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#include "isofs.h"
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_cistatic struct dentry *
2062306a36Sopenharmony_ciisofs_export_iget(struct super_block *sb,
2162306a36Sopenharmony_ci		  unsigned long block,
2262306a36Sopenharmony_ci		  unsigned long offset,
2362306a36Sopenharmony_ci		  __u32 generation)
2462306a36Sopenharmony_ci{
2562306a36Sopenharmony_ci	struct inode *inode;
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci	if (block == 0)
2862306a36Sopenharmony_ci		return ERR_PTR(-ESTALE);
2962306a36Sopenharmony_ci	inode = isofs_iget(sb, block, offset);
3062306a36Sopenharmony_ci	if (IS_ERR(inode))
3162306a36Sopenharmony_ci		return ERR_CAST(inode);
3262306a36Sopenharmony_ci	if (generation && inode->i_generation != generation) {
3362306a36Sopenharmony_ci		iput(inode);
3462306a36Sopenharmony_ci		return ERR_PTR(-ESTALE);
3562306a36Sopenharmony_ci	}
3662306a36Sopenharmony_ci	return d_obtain_alias(inode);
3762306a36Sopenharmony_ci}
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci/* This function is surprisingly simple.  The trick is understanding
4062306a36Sopenharmony_ci * that "child" is always a directory. So, to find its parent, you
4162306a36Sopenharmony_ci * simply need to find its ".." entry, normalize its block and offset,
4262306a36Sopenharmony_ci * and return the underlying inode.  See the comments for
4362306a36Sopenharmony_ci * isofs_normalize_block_and_offset(). */
4462306a36Sopenharmony_cistatic struct dentry *isofs_export_get_parent(struct dentry *child)
4562306a36Sopenharmony_ci{
4662306a36Sopenharmony_ci	unsigned long parent_block = 0;
4762306a36Sopenharmony_ci	unsigned long parent_offset = 0;
4862306a36Sopenharmony_ci	struct inode *child_inode = d_inode(child);
4962306a36Sopenharmony_ci	struct iso_inode_info *e_child_inode = ISOFS_I(child_inode);
5062306a36Sopenharmony_ci	struct iso_directory_record *de = NULL;
5162306a36Sopenharmony_ci	struct buffer_head * bh = NULL;
5262306a36Sopenharmony_ci	struct dentry *rv = NULL;
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci	/* "child" must always be a directory. */
5562306a36Sopenharmony_ci	if (!S_ISDIR(child_inode->i_mode)) {
5662306a36Sopenharmony_ci		printk(KERN_ERR "isofs: isofs_export_get_parent(): "
5762306a36Sopenharmony_ci		       "child is not a directory!\n");
5862306a36Sopenharmony_ci		rv = ERR_PTR(-EACCES);
5962306a36Sopenharmony_ci		goto out;
6062306a36Sopenharmony_ci	}
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci	/* It is an invariant that the directory offset is zero.  If
6362306a36Sopenharmony_ci	 * it is not zero, it means the directory failed to be
6462306a36Sopenharmony_ci	 * normalized for some reason. */
6562306a36Sopenharmony_ci	if (e_child_inode->i_iget5_offset != 0) {
6662306a36Sopenharmony_ci		printk(KERN_ERR "isofs: isofs_export_get_parent(): "
6762306a36Sopenharmony_ci		       "child directory not normalized!\n");
6862306a36Sopenharmony_ci		rv = ERR_PTR(-EACCES);
6962306a36Sopenharmony_ci		goto out;
7062306a36Sopenharmony_ci	}
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	/* The child inode has been normalized such that its
7362306a36Sopenharmony_ci	 * i_iget5_block value points to the "." entry.  Fortunately,
7462306a36Sopenharmony_ci	 * the ".." entry is located in the same block. */
7562306a36Sopenharmony_ci	parent_block = e_child_inode->i_iget5_block;
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	/* Get the block in question. */
7862306a36Sopenharmony_ci	bh = sb_bread(child_inode->i_sb, parent_block);
7962306a36Sopenharmony_ci	if (bh == NULL) {
8062306a36Sopenharmony_ci		rv = ERR_PTR(-EACCES);
8162306a36Sopenharmony_ci		goto out;
8262306a36Sopenharmony_ci	}
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	/* This is the "." entry. */
8562306a36Sopenharmony_ci	de = (struct iso_directory_record*)bh->b_data;
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci	/* The ".." entry is always the second entry. */
8862306a36Sopenharmony_ci	parent_offset = (unsigned long)isonum_711(de->length);
8962306a36Sopenharmony_ci	de = (struct iso_directory_record*)(bh->b_data + parent_offset);
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci	/* Verify it is in fact the ".." entry. */
9262306a36Sopenharmony_ci	if ((isonum_711(de->name_len) != 1) || (de->name[0] != 1)) {
9362306a36Sopenharmony_ci		printk(KERN_ERR "isofs: Unable to find the \"..\" "
9462306a36Sopenharmony_ci		       "directory for NFS.\n");
9562306a36Sopenharmony_ci		rv = ERR_PTR(-EACCES);
9662306a36Sopenharmony_ci		goto out;
9762306a36Sopenharmony_ci	}
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci	/* Normalize */
10062306a36Sopenharmony_ci	isofs_normalize_block_and_offset(de, &parent_block, &parent_offset);
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci	rv = d_obtain_alias(isofs_iget(child_inode->i_sb, parent_block,
10362306a36Sopenharmony_ci				     parent_offset));
10462306a36Sopenharmony_ci out:
10562306a36Sopenharmony_ci	if (bh)
10662306a36Sopenharmony_ci		brelse(bh);
10762306a36Sopenharmony_ci	return rv;
10862306a36Sopenharmony_ci}
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_cistatic int
11162306a36Sopenharmony_ciisofs_export_encode_fh(struct inode *inode,
11262306a36Sopenharmony_ci		       __u32 *fh32,
11362306a36Sopenharmony_ci		       int *max_len,
11462306a36Sopenharmony_ci		       struct inode *parent)
11562306a36Sopenharmony_ci{
11662306a36Sopenharmony_ci	struct iso_inode_info * ei = ISOFS_I(inode);
11762306a36Sopenharmony_ci	int len = *max_len;
11862306a36Sopenharmony_ci	int type = 1;
11962306a36Sopenharmony_ci	__u16 *fh16 = (__u16*)fh32;
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci	/*
12262306a36Sopenharmony_ci	 * WARNING: max_len is 5 for NFSv2.  Because of this
12362306a36Sopenharmony_ci	 * limitation, we use the lower 16 bits of fh32[1] to hold the
12462306a36Sopenharmony_ci	 * offset of the inode and the upper 16 bits of fh32[1] to
12562306a36Sopenharmony_ci	 * hold the offset of the parent.
12662306a36Sopenharmony_ci	 */
12762306a36Sopenharmony_ci	if (parent && (len < 5)) {
12862306a36Sopenharmony_ci		*max_len = 5;
12962306a36Sopenharmony_ci		return FILEID_INVALID;
13062306a36Sopenharmony_ci	} else if (len < 3) {
13162306a36Sopenharmony_ci		*max_len = 3;
13262306a36Sopenharmony_ci		return FILEID_INVALID;
13362306a36Sopenharmony_ci	}
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci	len = 3;
13662306a36Sopenharmony_ci	fh32[0] = ei->i_iget5_block;
13762306a36Sopenharmony_ci 	fh16[2] = (__u16)ei->i_iget5_offset;  /* fh16 [sic] */
13862306a36Sopenharmony_ci	fh16[3] = 0;  /* avoid leaking uninitialized data */
13962306a36Sopenharmony_ci	fh32[2] = inode->i_generation;
14062306a36Sopenharmony_ci	if (parent) {
14162306a36Sopenharmony_ci		struct iso_inode_info *eparent;
14262306a36Sopenharmony_ci		eparent = ISOFS_I(parent);
14362306a36Sopenharmony_ci		fh32[3] = eparent->i_iget5_block;
14462306a36Sopenharmony_ci		fh16[3] = (__u16)eparent->i_iget5_offset;  /* fh16 [sic] */
14562306a36Sopenharmony_ci		fh32[4] = parent->i_generation;
14662306a36Sopenharmony_ci		len = 5;
14762306a36Sopenharmony_ci		type = 2;
14862306a36Sopenharmony_ci	}
14962306a36Sopenharmony_ci	*max_len = len;
15062306a36Sopenharmony_ci	return type;
15162306a36Sopenharmony_ci}
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_cistruct isofs_fid {
15462306a36Sopenharmony_ci	u32 block;
15562306a36Sopenharmony_ci	u16 offset;
15662306a36Sopenharmony_ci	u16 parent_offset;
15762306a36Sopenharmony_ci	u32 generation;
15862306a36Sopenharmony_ci	u32 parent_block;
15962306a36Sopenharmony_ci	u32 parent_generation;
16062306a36Sopenharmony_ci};
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_cistatic struct dentry *isofs_fh_to_dentry(struct super_block *sb,
16362306a36Sopenharmony_ci	struct fid *fid, int fh_len, int fh_type)
16462306a36Sopenharmony_ci{
16562306a36Sopenharmony_ci	struct isofs_fid *ifid = (struct isofs_fid *)fid;
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci	if (fh_len < 3 || fh_type > 2)
16862306a36Sopenharmony_ci		return NULL;
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci	return isofs_export_iget(sb, ifid->block, ifid->offset,
17162306a36Sopenharmony_ci			ifid->generation);
17262306a36Sopenharmony_ci}
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_cistatic struct dentry *isofs_fh_to_parent(struct super_block *sb,
17562306a36Sopenharmony_ci		struct fid *fid, int fh_len, int fh_type)
17662306a36Sopenharmony_ci{
17762306a36Sopenharmony_ci	struct isofs_fid *ifid = (struct isofs_fid *)fid;
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci	if (fh_len < 2 || fh_type != 2)
18062306a36Sopenharmony_ci		return NULL;
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci	return isofs_export_iget(sb,
18362306a36Sopenharmony_ci			fh_len > 2 ? ifid->parent_block : 0,
18462306a36Sopenharmony_ci			ifid->parent_offset,
18562306a36Sopenharmony_ci			fh_len > 4 ? ifid->parent_generation : 0);
18662306a36Sopenharmony_ci}
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ciconst struct export_operations isofs_export_ops = {
18962306a36Sopenharmony_ci	.encode_fh	= isofs_export_encode_fh,
19062306a36Sopenharmony_ci	.fh_to_dentry	= isofs_fh_to_dentry,
19162306a36Sopenharmony_ci	.fh_to_parent	= isofs_fh_to_parent,
19262306a36Sopenharmony_ci	.get_parent     = isofs_export_get_parent,
19362306a36Sopenharmony_ci};
194