^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* cnode related routines for the coda kernel code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) (C) 1996 Peter Braam
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/coda.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "coda_psdev.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "coda_linux.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) static inline int coda_fideq(struct CodaFid *fid1, struct CodaFid *fid2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) return memcmp(fid1, fid2, sizeof(*fid1)) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static const struct inode_operations coda_symlink_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) .get_link = page_get_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) .setattr = coda_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /* cnode.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static void coda_fill_inode(struct inode *inode, struct coda_vattr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) coda_vattr_to_iattr(inode, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (S_ISREG(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) inode->i_op = &coda_file_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) inode->i_fop = &coda_file_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) } else if (S_ISDIR(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) inode->i_op = &coda_dir_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) inode->i_fop = &coda_dir_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) } else if (S_ISLNK(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) inode->i_op = &coda_symlink_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) inode_nohighmem(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) inode->i_data.a_ops = &coda_symlink_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) inode->i_mapping = &inode->i_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) init_special_inode(inode, inode->i_mode, huge_decode_dev(attr->va_rdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static int coda_test_inode(struct inode *inode, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct CodaFid *fid = (struct CodaFid *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct coda_inode_info *cii = ITOC(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return coda_fideq(&cii->c_fid, fid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static int coda_set_inode(struct inode *inode, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct CodaFid *fid = (struct CodaFid *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct coda_inode_info *cii = ITOC(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) cii->c_fid = *fid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct inode * coda_iget(struct super_block * sb, struct CodaFid * fid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct coda_vattr * attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct coda_inode_info *cii;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) unsigned long hash = coda_f2i(fid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) inode = iget5_locked(sb, hash, coda_test_inode, coda_set_inode, fid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (inode->i_state & I_NEW) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) cii = ITOC(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /* we still need to set i_ino for things like stat(2) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) inode->i_ino = hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /* inode is locked and unique, no need to grab cii->c_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) cii->c_mapcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) unlock_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /* always replace the attributes, type might have changed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) coda_fill_inode(inode, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return inode;
^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) /* this is effectively coda_iget:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) - get attributes (might be cached)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) - get the inode for the fid using vfs iget
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) - link the two up if this is needed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) - fill in the attributes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct inode *coda_cnode_make(struct CodaFid *fid, struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) struct coda_vattr attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /* We get inode numbers from Venus -- see venus source */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) error = venus_getattr(sb, fid, &attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return ERR_PTR(error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) inode = coda_iget(sb, fid, &attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (IS_ERR(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) pr_warn("%s: coda_iget failed\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) /* Although we treat Coda file identifiers as immutable, there is one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * special case for files created during a disconnection where they may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * not be globally unique. When an identifier collision is detected we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * first try to flush the cached inode from the kernel and finally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * resort to renaming/rehashing in-place. Userspace remembers both old
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * and new values of the identifier to handle any in-flight upcalls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * The real solution is to use globally unique UUIDs as identifiers, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * retrofitting the existing userspace code for this is non-trivial. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) void coda_replace_fid(struct inode *inode, struct CodaFid *oldfid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct CodaFid *newfid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct coda_inode_info *cii = ITOC(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) unsigned long hash = coda_f2i(newfid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) BUG_ON(!coda_fideq(&cii->c_fid, oldfid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /* replace fid and rehash inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) /* XXX we probably need to hold some lock here! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) remove_inode_hash(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) cii->c_fid = *newfid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) inode->i_ino = hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) __insert_inode_hash(inode, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) /* convert a fid to an inode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct inode *coda_fid_to_inode(struct CodaFid *fid, struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) unsigned long hash = coda_f2i(fid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) inode = ilookup5(sb, hash, coda_test_inode, fid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if ( !inode )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /* we should never see newly created inodes because we intentionally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * fail in the initialization callback */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) BUG_ON(inode->i_state & I_NEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct coda_file_info *coda_ftoc(struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct coda_file_info *cfi = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return cfi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^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) /* the CONTROL inode is made without asking attributes from Venus */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct inode *coda_cnode_makectl(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) struct inode *inode = new_inode(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) inode->i_ino = CTL_INO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) inode->i_op = &coda_ioctl_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) inode->i_fop = &coda_ioctl_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) inode->i_mode = 0444;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)