blob: 62a3d2565c26165bae988676f105391060ea3b2f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* cnode related routines for the coda kernel code
3 (C) 1996 Peter Braam
4 */
5
6#include <linux/types.h>
7#include <linux/string.h>
8#include <linux/time.h>
9
10#include <linux/coda.h>
Al Viro21fc61c2015-11-17 01:07:57 -050011#include <linux/pagemap.h>
David Howells8fc8b9d2019-07-16 16:28:47 -070012#include "coda_psdev.h"
Al Viro31a203d2011-01-12 16:36:09 -050013#include "coda_linux.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15static inline int coda_fideq(struct CodaFid *fid1, struct CodaFid *fid2)
16{
17 return memcmp(fid1, fid2, sizeof(*fid1)) == 0;
18}
19
Arjan van de Ven754661f2007-02-12 00:55:38 -080020static const struct inode_operations coda_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050021 .get_link = page_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 .setattr = coda_setattr,
23};
24
25/* cnode.c */
26static void coda_fill_inode(struct inode *inode, struct coda_vattr *attr)
27{
28 coda_vattr_to_iattr(inode, attr);
29
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;
Al Viro21fc61c2015-11-17 01:07:57 -050038 inode_nohighmem(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 inode->i_data.a_ops = &coda_symlink_aops;
40 inode->i_mapping = &inode->i_data;
41 } else
42 init_special_inode(inode, inode->i_mode, huge_decode_dev(attr->va_rdev));
43}
44
45static int coda_test_inode(struct inode *inode, void *data)
46{
47 struct CodaFid *fid = (struct CodaFid *)data;
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -040048 struct coda_inode_info *cii = ITOC(inode);
49 return coda_fideq(&cii->c_fid, fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
52static int coda_set_inode(struct inode *inode, void *data)
53{
54 struct CodaFid *fid = (struct CodaFid *)data;
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -040055 struct coda_inode_info *cii = ITOC(inode);
56 cii->c_fid = *fid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 return 0;
58}
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060struct inode * coda_iget(struct super_block * sb, struct CodaFid * fid,
61 struct coda_vattr * attr)
62{
63 struct inode *inode;
64 struct coda_inode_info *cii;
65 unsigned long hash = coda_f2i(fid);
Jan Harkes5a646fb2021-11-08 18:34:45 -080066 umode_t inode_type = coda_inode_type(attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Jan Harkes5a646fb2021-11-08 18:34:45 -080068retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 inode = iget5_locked(sb, hash, coda_test_inode, coda_set_inode, fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 if (!inode)
71 return ERR_PTR(-ENOMEM);
72
73 if (inode->i_state & I_NEW) {
74 cii = ITOC(inode);
75 /* we still need to set i_ino for things like stat(2) */
76 inode->i_ino = hash;
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -040077 /* inode is locked and unique, no need to grab cii->c_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 cii->c_mapcount = 0;
Jan Harkes5a646fb2021-11-08 18:34:45 -080079 coda_fill_inode(inode, attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 unlock_new_inode(inode);
Jan Harkes5a646fb2021-11-08 18:34:45 -080081 } 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);
85 iput(inode);
86 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return inode;
89}
90
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
96*/
Al Virof4947fb2012-01-10 11:11:49 -050097struct inode *coda_cnode_make(struct CodaFid *fid, struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 struct coda_vattr attr;
Al Virof4947fb2012-01-10 11:11:49 -0500100 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 int error;
102
103 /* We get inode numbers from Venus -- see venus source */
104 error = venus_getattr(sb, fid, &attr);
Al Virof4947fb2012-01-10 11:11:49 -0500105 if (error)
106 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Al Virof4947fb2012-01-10 11:11:49 -0500108 inode = coda_iget(sb, fid, &attr);
109 if (IS_ERR(inode))
Fabian Frederick6d6bd942014-06-06 14:36:20 -0700110 pr_warn("%s: coda_iget failed\n", __func__);
Al Virof4947fb2012-01-10 11:11:49 -0500111 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
114
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -0400115/* 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. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123void coda_replace_fid(struct inode *inode, struct CodaFid *oldfid,
124 struct CodaFid *newfid)
125{
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -0400126 struct coda_inode_info *cii = ITOC(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 unsigned long hash = coda_f2i(newfid);
128
Eric Sesterhennc5d32372006-03-24 18:42:13 +0100129 BUG_ON(!coda_fideq(&cii->c_fid, oldfid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
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;
135 inode->i_ino = hash;
136 __insert_inode_hash(inode, hash);
137}
138
139/* convert a fid to an inode. */
140struct inode *coda_fid_to_inode(struct CodaFid *fid, struct super_block *sb)
141{
142 struct inode *inode;
143 unsigned long hash = coda_f2i(fid);
144
Jan Harkesed31a7d2007-07-19 01:48:44 -0700145 inode = ilookup5(sb, hash, coda_test_inode, fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if ( !inode )
147 return NULL;
148
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);
152
153 return inode;
154}
155
Fabian Frederick5bb44812019-07-16 16:29:09 -0700156struct coda_file_info *coda_ftoc(struct file *file)
157{
158 struct coda_file_info *cfi = file->private_data;
159
160 BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
161
162 return cfi;
163
164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/* the CONTROL inode is made without asking attributes from Venus */
Al Viro0b2c4e32012-01-10 10:46:03 -0500167struct inode *coda_cnode_makectl(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Al Viro0b2c4e32012-01-10 10:46:03 -0500169 struct inode *inode = new_inode(sb);
170 if (inode) {
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;
175 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
Al Viro0b2c4e32012-01-10 10:46:03 -0500177 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179