18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* cnode related routines for the coda kernel code
38c2ecf20Sopenharmony_ci   (C) 1996 Peter Braam
48c2ecf20Sopenharmony_ci   */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/types.h>
78c2ecf20Sopenharmony_ci#include <linux/string.h>
88c2ecf20Sopenharmony_ci#include <linux/time.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/coda.h>
118c2ecf20Sopenharmony_ci#include <linux/pagemap.h>
128c2ecf20Sopenharmony_ci#include "coda_psdev.h"
138c2ecf20Sopenharmony_ci#include "coda_linux.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cistatic inline int coda_fideq(struct CodaFid *fid1, struct CodaFid *fid2)
168c2ecf20Sopenharmony_ci{
178c2ecf20Sopenharmony_ci	return memcmp(fid1, fid2, sizeof(*fid1)) == 0;
188c2ecf20Sopenharmony_ci}
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic const struct inode_operations coda_symlink_inode_operations = {
218c2ecf20Sopenharmony_ci	.get_link	= page_get_link,
228c2ecf20Sopenharmony_ci	.setattr	= coda_setattr,
238c2ecf20Sopenharmony_ci};
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/* cnode.c */
268c2ecf20Sopenharmony_cistatic void coda_fill_inode(struct inode *inode, struct coda_vattr *attr)
278c2ecf20Sopenharmony_ci{
288c2ecf20Sopenharmony_ci        coda_vattr_to_iattr(inode, attr);
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci        if (S_ISREG(inode->i_mode)) {
318c2ecf20Sopenharmony_ci                inode->i_op = &coda_file_inode_operations;
328c2ecf20Sopenharmony_ci                inode->i_fop = &coda_file_operations;
338c2ecf20Sopenharmony_ci        } else if (S_ISDIR(inode->i_mode)) {
348c2ecf20Sopenharmony_ci                inode->i_op = &coda_dir_inode_operations;
358c2ecf20Sopenharmony_ci                inode->i_fop = &coda_dir_operations;
368c2ecf20Sopenharmony_ci        } else if (S_ISLNK(inode->i_mode)) {
378c2ecf20Sopenharmony_ci		inode->i_op = &coda_symlink_inode_operations;
388c2ecf20Sopenharmony_ci		inode_nohighmem(inode);
398c2ecf20Sopenharmony_ci		inode->i_data.a_ops = &coda_symlink_aops;
408c2ecf20Sopenharmony_ci		inode->i_mapping = &inode->i_data;
418c2ecf20Sopenharmony_ci	} else
428c2ecf20Sopenharmony_ci                init_special_inode(inode, inode->i_mode, huge_decode_dev(attr->va_rdev));
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic int coda_test_inode(struct inode *inode, void *data)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	struct CodaFid *fid = (struct CodaFid *)data;
488c2ecf20Sopenharmony_ci	struct coda_inode_info *cii = ITOC(inode);
498c2ecf20Sopenharmony_ci	return coda_fideq(&cii->c_fid, fid);
508c2ecf20Sopenharmony_ci}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic int coda_set_inode(struct inode *inode, void *data)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	struct CodaFid *fid = (struct CodaFid *)data;
558c2ecf20Sopenharmony_ci	struct coda_inode_info *cii = ITOC(inode);
568c2ecf20Sopenharmony_ci	cii->c_fid = *fid;
578c2ecf20Sopenharmony_ci	return 0;
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistruct inode * coda_iget(struct super_block * sb, struct CodaFid * fid,
618c2ecf20Sopenharmony_ci			 struct coda_vattr * attr)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	struct inode *inode;
648c2ecf20Sopenharmony_ci	struct coda_inode_info *cii;
658c2ecf20Sopenharmony_ci	unsigned long hash = coda_f2i(fid);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	inode = iget5_locked(sb, hash, coda_test_inode, coda_set_inode, fid);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	if (!inode)
708c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	if (inode->i_state & I_NEW) {
738c2ecf20Sopenharmony_ci		cii = ITOC(inode);
748c2ecf20Sopenharmony_ci		/* we still need to set i_ino for things like stat(2) */
758c2ecf20Sopenharmony_ci		inode->i_ino = hash;
768c2ecf20Sopenharmony_ci		/* inode is locked and unique, no need to grab cii->c_lock */
778c2ecf20Sopenharmony_ci		cii->c_mapcount = 0;
788c2ecf20Sopenharmony_ci		unlock_new_inode(inode);
798c2ecf20Sopenharmony_ci	}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	/* always replace the attributes, type might have changed */
828c2ecf20Sopenharmony_ci	coda_fill_inode(inode, attr);
838c2ecf20Sopenharmony_ci	return inode;
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci/* this is effectively coda_iget:
878c2ecf20Sopenharmony_ci   - get attributes (might be cached)
888c2ecf20Sopenharmony_ci   - get the inode for the fid using vfs iget
898c2ecf20Sopenharmony_ci   - link the two up if this is needed
908c2ecf20Sopenharmony_ci   - fill in the attributes
918c2ecf20Sopenharmony_ci*/
928c2ecf20Sopenharmony_cistruct inode *coda_cnode_make(struct CodaFid *fid, struct super_block *sb)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci        struct coda_vattr attr;
958c2ecf20Sopenharmony_ci	struct inode *inode;
968c2ecf20Sopenharmony_ci        int error;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	/* We get inode numbers from Venus -- see venus source */
998c2ecf20Sopenharmony_ci	error = venus_getattr(sb, fid, &attr);
1008c2ecf20Sopenharmony_ci	if (error)
1018c2ecf20Sopenharmony_ci		return ERR_PTR(error);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	inode = coda_iget(sb, fid, &attr);
1048c2ecf20Sopenharmony_ci	if (IS_ERR(inode))
1058c2ecf20Sopenharmony_ci		pr_warn("%s: coda_iget failed\n", __func__);
1068c2ecf20Sopenharmony_ci	return inode;
1078c2ecf20Sopenharmony_ci}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci/* Although we treat Coda file identifiers as immutable, there is one
1118c2ecf20Sopenharmony_ci * special case for files created during a disconnection where they may
1128c2ecf20Sopenharmony_ci * not be globally unique. When an identifier collision is detected we
1138c2ecf20Sopenharmony_ci * first try to flush the cached inode from the kernel and finally
1148c2ecf20Sopenharmony_ci * resort to renaming/rehashing in-place. Userspace remembers both old
1158c2ecf20Sopenharmony_ci * and new values of the identifier to handle any in-flight upcalls.
1168c2ecf20Sopenharmony_ci * The real solution is to use globally unique UUIDs as identifiers, but
1178c2ecf20Sopenharmony_ci * retrofitting the existing userspace code for this is non-trivial. */
1188c2ecf20Sopenharmony_civoid coda_replace_fid(struct inode *inode, struct CodaFid *oldfid,
1198c2ecf20Sopenharmony_ci		      struct CodaFid *newfid)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	struct coda_inode_info *cii = ITOC(inode);
1228c2ecf20Sopenharmony_ci	unsigned long hash = coda_f2i(newfid);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	BUG_ON(!coda_fideq(&cii->c_fid, oldfid));
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	/* replace fid and rehash inode */
1278c2ecf20Sopenharmony_ci	/* XXX we probably need to hold some lock here! */
1288c2ecf20Sopenharmony_ci	remove_inode_hash(inode);
1298c2ecf20Sopenharmony_ci	cii->c_fid = *newfid;
1308c2ecf20Sopenharmony_ci	inode->i_ino = hash;
1318c2ecf20Sopenharmony_ci	__insert_inode_hash(inode, hash);
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci/* convert a fid to an inode. */
1358c2ecf20Sopenharmony_cistruct inode *coda_fid_to_inode(struct CodaFid *fid, struct super_block *sb)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	struct inode *inode;
1388c2ecf20Sopenharmony_ci	unsigned long hash = coda_f2i(fid);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	inode = ilookup5(sb, hash, coda_test_inode, fid);
1418c2ecf20Sopenharmony_ci	if ( !inode )
1428c2ecf20Sopenharmony_ci		return NULL;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	/* we should never see newly created inodes because we intentionally
1458c2ecf20Sopenharmony_ci	 * fail in the initialization callback */
1468c2ecf20Sopenharmony_ci	BUG_ON(inode->i_state & I_NEW);
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	return inode;
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cistruct coda_file_info *coda_ftoc(struct file *file)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	struct coda_file_info *cfi = file->private_data;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	return cfi;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci/* the CONTROL inode is made without asking attributes from Venus */
1628c2ecf20Sopenharmony_cistruct inode *coda_cnode_makectl(struct super_block *sb)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	struct inode *inode = new_inode(sb);
1658c2ecf20Sopenharmony_ci	if (inode) {
1668c2ecf20Sopenharmony_ci		inode->i_ino = CTL_INO;
1678c2ecf20Sopenharmony_ci		inode->i_op = &coda_ioctl_inode_operations;
1688c2ecf20Sopenharmony_ci		inode->i_fop = &coda_ioctl_operations;
1698c2ecf20Sopenharmony_ci		inode->i_mode = 0444;
1708c2ecf20Sopenharmony_ci		return inode;
1718c2ecf20Sopenharmony_ci	}
1728c2ecf20Sopenharmony_ci	return ERR_PTR(-ENOMEM);
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ci
175