xref: /kernel/linux/linux-5.10/fs/fat/nfs.c (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/* fs/fat/nfs.c
38c2ecf20Sopenharmony_ci */
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <linux/exportfs.h>
68c2ecf20Sopenharmony_ci#include "fat.h"
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_cistruct fat_fid {
98c2ecf20Sopenharmony_ci	u32 i_gen;
108c2ecf20Sopenharmony_ci	u32 i_pos_low;
118c2ecf20Sopenharmony_ci	u16 i_pos_hi;
128c2ecf20Sopenharmony_ci	u16 parent_i_pos_hi;
138c2ecf20Sopenharmony_ci	u32 parent_i_pos_low;
148c2ecf20Sopenharmony_ci	u32 parent_i_gen;
158c2ecf20Sopenharmony_ci};
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define FAT_FID_SIZE_WITHOUT_PARENT 3
188c2ecf20Sopenharmony_ci#define FAT_FID_SIZE_WITH_PARENT (sizeof(struct fat_fid)/sizeof(u32))
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/**
218c2ecf20Sopenharmony_ci * Look up a directory inode given its starting cluster.
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_cistatic struct inode *fat_dget(struct super_block *sb, int i_logstart)
248c2ecf20Sopenharmony_ci{
258c2ecf20Sopenharmony_ci	struct msdos_sb_info *sbi = MSDOS_SB(sb);
268c2ecf20Sopenharmony_ci	struct hlist_head *head;
278c2ecf20Sopenharmony_ci	struct msdos_inode_info *i;
288c2ecf20Sopenharmony_ci	struct inode *inode = NULL;
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci	head = sbi->dir_hashtable + fat_dir_hash(i_logstart);
318c2ecf20Sopenharmony_ci	spin_lock(&sbi->dir_hash_lock);
328c2ecf20Sopenharmony_ci	hlist_for_each_entry(i, head, i_dir_hash) {
338c2ecf20Sopenharmony_ci		BUG_ON(i->vfs_inode.i_sb != sb);
348c2ecf20Sopenharmony_ci		if (i->i_logstart != i_logstart)
358c2ecf20Sopenharmony_ci			continue;
368c2ecf20Sopenharmony_ci		inode = igrab(&i->vfs_inode);
378c2ecf20Sopenharmony_ci		if (inode)
388c2ecf20Sopenharmony_ci			break;
398c2ecf20Sopenharmony_ci	}
408c2ecf20Sopenharmony_ci	spin_unlock(&sbi->dir_hash_lock);
418c2ecf20Sopenharmony_ci	return inode;
428c2ecf20Sopenharmony_ci}
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic struct inode *fat_ilookup(struct super_block *sb, u64 ino, loff_t i_pos)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	if (MSDOS_SB(sb)->options.nfs == FAT_NFS_NOSTALE_RO)
478c2ecf20Sopenharmony_ci		return fat_iget(sb, i_pos);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	else {
508c2ecf20Sopenharmony_ci		if ((ino < MSDOS_ROOT_INO) || (ino == MSDOS_FSINFO_INO))
518c2ecf20Sopenharmony_ci			return NULL;
528c2ecf20Sopenharmony_ci		return ilookup(sb, ino);
538c2ecf20Sopenharmony_ci	}
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic struct inode *__fat_nfs_get_inode(struct super_block *sb,
578c2ecf20Sopenharmony_ci				       u64 ino, u32 generation, loff_t i_pos)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	struct inode *inode = fat_ilookup(sb, ino, i_pos);
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	if (inode && generation && (inode->i_generation != generation)) {
628c2ecf20Sopenharmony_ci		iput(inode);
638c2ecf20Sopenharmony_ci		inode = NULL;
648c2ecf20Sopenharmony_ci	}
658c2ecf20Sopenharmony_ci	if (inode == NULL && MSDOS_SB(sb)->options.nfs == FAT_NFS_NOSTALE_RO) {
668c2ecf20Sopenharmony_ci		struct buffer_head *bh = NULL;
678c2ecf20Sopenharmony_ci		struct msdos_dir_entry *de ;
688c2ecf20Sopenharmony_ci		sector_t blocknr;
698c2ecf20Sopenharmony_ci		int offset;
708c2ecf20Sopenharmony_ci		fat_get_blknr_offset(MSDOS_SB(sb), i_pos, &blocknr, &offset);
718c2ecf20Sopenharmony_ci		bh = sb_bread(sb, blocknr);
728c2ecf20Sopenharmony_ci		if (!bh) {
738c2ecf20Sopenharmony_ci			fat_msg(sb, KERN_ERR,
748c2ecf20Sopenharmony_ci				"unable to read block(%llu) for building NFS inode",
758c2ecf20Sopenharmony_ci				(llu)blocknr);
768c2ecf20Sopenharmony_ci			return inode;
778c2ecf20Sopenharmony_ci		}
788c2ecf20Sopenharmony_ci		de = (struct msdos_dir_entry *)bh->b_data;
798c2ecf20Sopenharmony_ci		/* If a file is deleted on server and client is not updated
808c2ecf20Sopenharmony_ci		 * yet, we must not build the inode upon a lookup call.
818c2ecf20Sopenharmony_ci		 */
828c2ecf20Sopenharmony_ci		if (IS_FREE(de[offset].name))
838c2ecf20Sopenharmony_ci			inode = NULL;
848c2ecf20Sopenharmony_ci		else
858c2ecf20Sopenharmony_ci			inode = fat_build_inode(sb, &de[offset], i_pos);
868c2ecf20Sopenharmony_ci		brelse(bh);
878c2ecf20Sopenharmony_ci	}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	return inode;
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistatic struct inode *fat_nfs_get_inode(struct super_block *sb,
938c2ecf20Sopenharmony_ci				       u64 ino, u32 generation)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	return __fat_nfs_get_inode(sb, ino, generation, 0);
978c2ecf20Sopenharmony_ci}
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistatic int
1008c2ecf20Sopenharmony_cifat_encode_fh_nostale(struct inode *inode, __u32 *fh, int *lenp,
1018c2ecf20Sopenharmony_ci		      struct inode *parent)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	int len = *lenp;
1048c2ecf20Sopenharmony_ci	struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
1058c2ecf20Sopenharmony_ci	struct fat_fid *fid = (struct fat_fid *) fh;
1068c2ecf20Sopenharmony_ci	loff_t i_pos;
1078c2ecf20Sopenharmony_ci	int type = FILEID_FAT_WITHOUT_PARENT;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	if (parent) {
1108c2ecf20Sopenharmony_ci		if (len < FAT_FID_SIZE_WITH_PARENT) {
1118c2ecf20Sopenharmony_ci			*lenp = FAT_FID_SIZE_WITH_PARENT;
1128c2ecf20Sopenharmony_ci			return FILEID_INVALID;
1138c2ecf20Sopenharmony_ci		}
1148c2ecf20Sopenharmony_ci	} else {
1158c2ecf20Sopenharmony_ci		if (len < FAT_FID_SIZE_WITHOUT_PARENT) {
1168c2ecf20Sopenharmony_ci			*lenp = FAT_FID_SIZE_WITHOUT_PARENT;
1178c2ecf20Sopenharmony_ci			return FILEID_INVALID;
1188c2ecf20Sopenharmony_ci		}
1198c2ecf20Sopenharmony_ci	}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	i_pos = fat_i_pos_read(sbi, inode);
1228c2ecf20Sopenharmony_ci	*lenp = FAT_FID_SIZE_WITHOUT_PARENT;
1238c2ecf20Sopenharmony_ci	fid->i_gen = inode->i_generation;
1248c2ecf20Sopenharmony_ci	fid->i_pos_low = i_pos & 0xFFFFFFFF;
1258c2ecf20Sopenharmony_ci	fid->i_pos_hi = (i_pos >> 32) & 0xFFFF;
1268c2ecf20Sopenharmony_ci	if (parent) {
1278c2ecf20Sopenharmony_ci		i_pos = fat_i_pos_read(sbi, parent);
1288c2ecf20Sopenharmony_ci		fid->parent_i_pos_hi = (i_pos >> 32) & 0xFFFF;
1298c2ecf20Sopenharmony_ci		fid->parent_i_pos_low = i_pos & 0xFFFFFFFF;
1308c2ecf20Sopenharmony_ci		fid->parent_i_gen = parent->i_generation;
1318c2ecf20Sopenharmony_ci		type = FILEID_FAT_WITH_PARENT;
1328c2ecf20Sopenharmony_ci		*lenp = FAT_FID_SIZE_WITH_PARENT;
1338c2ecf20Sopenharmony_ci	} else {
1348c2ecf20Sopenharmony_ci		/*
1358c2ecf20Sopenharmony_ci		 * We need to initialize this field because the fh is actually
1368c2ecf20Sopenharmony_ci		 * 12 bytes long
1378c2ecf20Sopenharmony_ci		 */
1388c2ecf20Sopenharmony_ci		fid->parent_i_pos_hi = 0;
1398c2ecf20Sopenharmony_ci	}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	return type;
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci/**
1458c2ecf20Sopenharmony_ci * Map a NFS file handle to a corresponding dentry.
1468c2ecf20Sopenharmony_ci * The dentry may or may not be connected to the filesystem root.
1478c2ecf20Sopenharmony_ci */
1488c2ecf20Sopenharmony_cistatic struct dentry *fat_fh_to_dentry(struct super_block *sb, struct fid *fid,
1498c2ecf20Sopenharmony_ci				int fh_len, int fh_type)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1528c2ecf20Sopenharmony_ci				    fat_nfs_get_inode);
1538c2ecf20Sopenharmony_ci}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistatic struct dentry *fat_fh_to_dentry_nostale(struct super_block *sb,
1568c2ecf20Sopenharmony_ci					       struct fid *fh, int fh_len,
1578c2ecf20Sopenharmony_ci					       int fh_type)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci	struct inode *inode = NULL;
1608c2ecf20Sopenharmony_ci	struct fat_fid *fid = (struct fat_fid *)fh;
1618c2ecf20Sopenharmony_ci	loff_t i_pos;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	switch (fh_type) {
1648c2ecf20Sopenharmony_ci	case FILEID_FAT_WITHOUT_PARENT:
1658c2ecf20Sopenharmony_ci		if (fh_len < FAT_FID_SIZE_WITHOUT_PARENT)
1668c2ecf20Sopenharmony_ci			return NULL;
1678c2ecf20Sopenharmony_ci		break;
1688c2ecf20Sopenharmony_ci	case FILEID_FAT_WITH_PARENT:
1698c2ecf20Sopenharmony_ci		if (fh_len < FAT_FID_SIZE_WITH_PARENT)
1708c2ecf20Sopenharmony_ci			return NULL;
1718c2ecf20Sopenharmony_ci		break;
1728c2ecf20Sopenharmony_ci	default:
1738c2ecf20Sopenharmony_ci		return NULL;
1748c2ecf20Sopenharmony_ci	}
1758c2ecf20Sopenharmony_ci	i_pos = fid->i_pos_hi;
1768c2ecf20Sopenharmony_ci	i_pos = (i_pos << 32) | (fid->i_pos_low);
1778c2ecf20Sopenharmony_ci	inode = __fat_nfs_get_inode(sb, 0, fid->i_gen, i_pos);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	return d_obtain_alias(inode);
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci/*
1838c2ecf20Sopenharmony_ci * Find the parent for a file specified by NFS handle.
1848c2ecf20Sopenharmony_ci * This requires that the handle contain the i_ino of the parent.
1858c2ecf20Sopenharmony_ci */
1868c2ecf20Sopenharmony_cistatic struct dentry *fat_fh_to_parent(struct super_block *sb, struct fid *fid,
1878c2ecf20Sopenharmony_ci				int fh_len, int fh_type)
1888c2ecf20Sopenharmony_ci{
1898c2ecf20Sopenharmony_ci	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1908c2ecf20Sopenharmony_ci				    fat_nfs_get_inode);
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_cistatic struct dentry *fat_fh_to_parent_nostale(struct super_block *sb,
1948c2ecf20Sopenharmony_ci					       struct fid *fh, int fh_len,
1958c2ecf20Sopenharmony_ci					       int fh_type)
1968c2ecf20Sopenharmony_ci{
1978c2ecf20Sopenharmony_ci	struct inode *inode = NULL;
1988c2ecf20Sopenharmony_ci	struct fat_fid *fid = (struct fat_fid *)fh;
1998c2ecf20Sopenharmony_ci	loff_t i_pos;
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	if (fh_len < FAT_FID_SIZE_WITH_PARENT)
2028c2ecf20Sopenharmony_ci		return NULL;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	switch (fh_type) {
2058c2ecf20Sopenharmony_ci	case FILEID_FAT_WITH_PARENT:
2068c2ecf20Sopenharmony_ci		i_pos = fid->parent_i_pos_hi;
2078c2ecf20Sopenharmony_ci		i_pos = (i_pos << 32) | (fid->parent_i_pos_low);
2088c2ecf20Sopenharmony_ci		inode = __fat_nfs_get_inode(sb, 0, fid->parent_i_gen, i_pos);
2098c2ecf20Sopenharmony_ci		break;
2108c2ecf20Sopenharmony_ci	}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	return d_obtain_alias(inode);
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci/*
2168c2ecf20Sopenharmony_ci * Rebuild the parent for a directory that is not connected
2178c2ecf20Sopenharmony_ci *  to the filesystem root
2188c2ecf20Sopenharmony_ci */
2198c2ecf20Sopenharmony_cistatic
2208c2ecf20Sopenharmony_cistruct inode *fat_rebuild_parent(struct super_block *sb, int parent_logstart)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	int search_clus, clus_to_match;
2238c2ecf20Sopenharmony_ci	struct msdos_dir_entry *de;
2248c2ecf20Sopenharmony_ci	struct inode *parent = NULL;
2258c2ecf20Sopenharmony_ci	struct inode *dummy_grand_parent = NULL;
2268c2ecf20Sopenharmony_ci	struct fat_slot_info sinfo;
2278c2ecf20Sopenharmony_ci	struct msdos_sb_info *sbi = MSDOS_SB(sb);
2288c2ecf20Sopenharmony_ci	sector_t blknr = fat_clus_to_blknr(sbi, parent_logstart);
2298c2ecf20Sopenharmony_ci	struct buffer_head *parent_bh = sb_bread(sb, blknr);
2308c2ecf20Sopenharmony_ci	if (!parent_bh) {
2318c2ecf20Sopenharmony_ci		fat_msg(sb, KERN_ERR,
2328c2ecf20Sopenharmony_ci			"unable to read cluster of parent directory");
2338c2ecf20Sopenharmony_ci		return NULL;
2348c2ecf20Sopenharmony_ci	}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	de = (struct msdos_dir_entry *) parent_bh->b_data;
2378c2ecf20Sopenharmony_ci	clus_to_match = fat_get_start(sbi, &de[0]);
2388c2ecf20Sopenharmony_ci	search_clus = fat_get_start(sbi, &de[1]);
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	dummy_grand_parent = fat_dget(sb, search_clus);
2418c2ecf20Sopenharmony_ci	if (!dummy_grand_parent) {
2428c2ecf20Sopenharmony_ci		dummy_grand_parent = new_inode(sb);
2438c2ecf20Sopenharmony_ci		if (!dummy_grand_parent) {
2448c2ecf20Sopenharmony_ci			brelse(parent_bh);
2458c2ecf20Sopenharmony_ci			return parent;
2468c2ecf20Sopenharmony_ci		}
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci		dummy_grand_parent->i_ino = iunique(sb, MSDOS_ROOT_INO);
2498c2ecf20Sopenharmony_ci		fat_fill_inode(dummy_grand_parent, &de[1]);
2508c2ecf20Sopenharmony_ci		MSDOS_I(dummy_grand_parent)->i_pos = -1;
2518c2ecf20Sopenharmony_ci	}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	if (!fat_scan_logstart(dummy_grand_parent, clus_to_match, &sinfo))
2548c2ecf20Sopenharmony_ci		parent = fat_build_inode(sb, sinfo.de, sinfo.i_pos);
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	brelse(parent_bh);
2578c2ecf20Sopenharmony_ci	iput(dummy_grand_parent);
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	return parent;
2608c2ecf20Sopenharmony_ci}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci/*
2638c2ecf20Sopenharmony_ci * Find the parent for a directory that is not currently connected to
2648c2ecf20Sopenharmony_ci * the filesystem root.
2658c2ecf20Sopenharmony_ci *
2668c2ecf20Sopenharmony_ci * On entry, the caller holds d_inode(child_dir)->i_mutex.
2678c2ecf20Sopenharmony_ci */
2688c2ecf20Sopenharmony_cistatic struct dentry *fat_get_parent(struct dentry *child_dir)
2698c2ecf20Sopenharmony_ci{
2708c2ecf20Sopenharmony_ci	struct super_block *sb = child_dir->d_sb;
2718c2ecf20Sopenharmony_ci	struct buffer_head *bh = NULL;
2728c2ecf20Sopenharmony_ci	struct msdos_dir_entry *de;
2738c2ecf20Sopenharmony_ci	struct inode *parent_inode = NULL;
2748c2ecf20Sopenharmony_ci	struct msdos_sb_info *sbi = MSDOS_SB(sb);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	if (!fat_get_dotdot_entry(d_inode(child_dir), &bh, &de)) {
2778c2ecf20Sopenharmony_ci		int parent_logstart = fat_get_start(sbi, de);
2788c2ecf20Sopenharmony_ci		parent_inode = fat_dget(sb, parent_logstart);
2798c2ecf20Sopenharmony_ci		if (!parent_inode && sbi->options.nfs == FAT_NFS_NOSTALE_RO)
2808c2ecf20Sopenharmony_ci			parent_inode = fat_rebuild_parent(sb, parent_logstart);
2818c2ecf20Sopenharmony_ci	}
2828c2ecf20Sopenharmony_ci	brelse(bh);
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	return d_obtain_alias(parent_inode);
2858c2ecf20Sopenharmony_ci}
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ciconst struct export_operations fat_export_ops = {
2888c2ecf20Sopenharmony_ci	.fh_to_dentry   = fat_fh_to_dentry,
2898c2ecf20Sopenharmony_ci	.fh_to_parent   = fat_fh_to_parent,
2908c2ecf20Sopenharmony_ci	.get_parent     = fat_get_parent,
2918c2ecf20Sopenharmony_ci};
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ciconst struct export_operations fat_export_ops_nostale = {
2948c2ecf20Sopenharmony_ci	.encode_fh      = fat_encode_fh_nostale,
2958c2ecf20Sopenharmony_ci	.fh_to_dentry   = fat_fh_to_dentry_nostale,
2968c2ecf20Sopenharmony_ci	.fh_to_parent   = fat_fh_to_parent_nostale,
2978c2ecf20Sopenharmony_ci	.get_parent     = fat_get_parent,
2988c2ecf20Sopenharmony_ci};
299