1 // SPDX-License-Identifier: GPL-2.0
2 /* cnode related routines for the coda kernel code
6 #include <linux/types.h>
7 #include <linux/string.h>
8 #include <linux/time.h>
10 #include <linux/coda.h>
11 #include <linux/pagemap.h>
12 #include "coda_psdev.h"
13 #include "coda_linux.h"
15 static inline int coda_fideq(struct CodaFid
*fid1
, struct CodaFid
*fid2
)
17 return memcmp(fid1
, fid2
, sizeof(*fid1
)) == 0;
20 static const struct inode_operations coda_symlink_inode_operations
= {
21 .get_link
= page_get_link
,
22 .setattr
= coda_setattr
,
26 static void coda_fill_inode(struct inode
*inode
, struct coda_vattr
*attr
)
28 coda_vattr_to_iattr(inode
, attr
);
30 if (S_ISREG(inode
->i_mode
)) {
31 inode
->i_op
= &coda_file_inode_operations
;
32 inode
->i_fop
= &coda_file_operations
;
33 } else if (S_ISDIR(inode
->i_mode
)) {
34 inode
->i_op
= &coda_dir_inode_operations
;
35 inode
->i_fop
= &coda_dir_operations
;
36 } else if (S_ISLNK(inode
->i_mode
)) {
37 inode
->i_op
= &coda_symlink_inode_operations
;
38 inode_nohighmem(inode
);
39 inode
->i_data
.a_ops
= &coda_symlink_aops
;
40 inode
->i_mapping
= &inode
->i_data
;
42 init_special_inode(inode
, inode
->i_mode
, huge_decode_dev(attr
->va_rdev
));
45 static int coda_test_inode(struct inode
*inode
, void *data
)
47 struct CodaFid
*fid
= (struct CodaFid
*)data
;
48 struct coda_inode_info
*cii
= ITOC(inode
);
49 return coda_fideq(&cii
->c_fid
, fid
);
52 static int coda_set_inode(struct inode
*inode
, void *data
)
54 struct CodaFid
*fid
= (struct CodaFid
*)data
;
55 struct coda_inode_info
*cii
= ITOC(inode
);
60 struct inode
* coda_iget(struct super_block
* sb
, struct CodaFid
* fid
,
61 struct coda_vattr
* attr
)
64 struct coda_inode_info
*cii
;
65 unsigned long hash
= coda_f2i(fid
);
66 umode_t inode_type
= coda_inode_type(attr
);
69 inode
= iget5_locked(sb
, hash
, coda_test_inode
, coda_set_inode
, fid
);
71 return ERR_PTR(-ENOMEM
);
73 if (inode
->i_state
& I_NEW
) {
75 /* we still need to set i_ino for things like stat(2) */
77 /* inode is locked and unique, no need to grab cii->c_lock */
79 coda_fill_inode(inode
, attr
);
80 unlock_new_inode(inode
);
81 } else if ((inode
->i_mode
& S_IFMT
) != inode_type
) {
82 /* Inode has changed type, mark bad and grab a new one */
83 remove_inode_hash(inode
);
84 coda_flag_inode(inode
, C_PURGE
);
91 /* this is effectively coda_iget:
92 - get attributes (might be cached)
93 - get the inode for the fid using vfs iget
94 - link the two up if this is needed
95 - fill in the attributes
97 struct inode
*coda_cnode_make(struct CodaFid
*fid
, struct super_block
*sb
)
99 struct coda_vattr attr
;
103 /* We get inode numbers from Venus -- see venus source */
104 error
= venus_getattr(sb
, fid
, &attr
);
106 return ERR_PTR(error
);
108 inode
= coda_iget(sb
, fid
, &attr
);
110 pr_warn("%s: coda_iget failed\n", __func__
);
115 /* Although we treat Coda file identifiers as immutable, there is one
116 * special case for files created during a disconnection where they may
117 * not be globally unique. When an identifier collision is detected we
118 * first try to flush the cached inode from the kernel and finally
119 * resort to renaming/rehashing in-place. Userspace remembers both old
120 * and new values of the identifier to handle any in-flight upcalls.
121 * The real solution is to use globally unique UUIDs as identifiers, but
122 * retrofitting the existing userspace code for this is non-trivial. */
123 void coda_replace_fid(struct inode
*inode
, struct CodaFid
*oldfid
,
124 struct CodaFid
*newfid
)
126 struct coda_inode_info
*cii
= ITOC(inode
);
127 unsigned long hash
= coda_f2i(newfid
);
129 BUG_ON(!coda_fideq(&cii
->c_fid
, oldfid
));
131 /* replace fid and rehash inode */
132 /* XXX we probably need to hold some lock here! */
133 remove_inode_hash(inode
);
134 cii
->c_fid
= *newfid
;
136 __insert_inode_hash(inode
, hash
);
139 /* convert a fid to an inode. */
140 struct inode
*coda_fid_to_inode(struct CodaFid
*fid
, struct super_block
*sb
)
143 unsigned long hash
= coda_f2i(fid
);
145 inode
= ilookup5(sb
, hash
, coda_test_inode
, fid
);
149 /* we should never see newly created inodes because we intentionally
150 * fail in the initialization callback */
151 BUG_ON(inode
->i_state
& I_NEW
);
156 struct coda_file_info
*coda_ftoc(struct file
*file
)
158 struct coda_file_info
*cfi
= file
->private_data
;
160 BUG_ON(!cfi
|| cfi
->cfi_magic
!= CODA_MAGIC
);
166 /* the CONTROL inode is made without asking attributes from Venus */
167 struct inode
*coda_cnode_makectl(struct super_block
*sb
)
169 struct inode
*inode
= new_inode(sb
);
171 inode
->i_ino
= CTL_INO
;
172 inode
->i_op
= &coda_ioctl_inode_operations
;
173 inode
->i_fop
= &coda_ioctl_operations
;
174 inode
->i_mode
= 0444;
177 return ERR_PTR(-ENOMEM
);