1/* cnode related routines for the coda kernel code
2   (C) 1996 Peter Braam
3   */
4
5#include <linux/types.h>
6#include <linux/string.h>
7#include <linux/time.h>
8
9#include <linux/coda.h>
10#include <linux/coda_linux.h>
11#include <linux/coda_fs_i.h>
12#include <linux/coda_psdev.h>
13
14static inline int coda_fideq(struct CodaFid *fid1, struct CodaFid *fid2)
15{
16	return memcmp(fid1, fid2, sizeof(*fid1)) == 0;
17}
18
19static const struct inode_operations coda_symlink_inode_operations = {
20	.readlink	= generic_readlink,
21	.follow_link	= page_follow_link_light,
22	.put_link	= page_put_link,
23	.setattr	= coda_setattr,
24};
25
26/* cnode.c */
27static void coda_fill_inode(struct inode *inode, struct coda_vattr *attr)
28{
29        coda_vattr_to_iattr(inode, attr);
30
31        if (S_ISREG(inode->i_mode)) {
32                inode->i_op = &coda_file_inode_operations;
33                inode->i_fop = &coda_file_operations;
34        } else if (S_ISDIR(inode->i_mode)) {
35                inode->i_op = &coda_dir_inode_operations;
36                inode->i_fop = &coda_dir_operations;
37        } else if (S_ISLNK(inode->i_mode)) {
38		inode->i_op = &coda_symlink_inode_operations;
39		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;
48	return coda_fideq(&(ITOC(inode)->c_fid), fid);
49}
50
51static int coda_set_inode(struct inode *inode, void *data)
52{
53	struct CodaFid *fid = (struct CodaFid *)data;
54	ITOC(inode)->c_fid = *fid;
55	return 0;
56}
57
58struct inode * coda_iget(struct super_block * sb, struct CodaFid * fid,
59			 struct coda_vattr * attr)
60{
61	struct inode *inode;
62	struct coda_inode_info *cii;
63	unsigned long hash = coda_f2i(fid);
64
65	inode = iget5_locked(sb, hash, coda_test_inode, coda_set_inode, fid);
66
67	if (!inode)
68		return ERR_PTR(-ENOMEM);
69
70	if (inode->i_state & I_NEW) {
71		cii = ITOC(inode);
72		/* we still need to set i_ino for things like stat(2) */
73		inode->i_ino = hash;
74		cii->c_mapcount = 0;
75		unlock_new_inode(inode);
76	}
77
78	/* always replace the attributes, type might have changed */
79	coda_fill_inode(inode, attr);
80	return inode;
81}
82
83/* this is effectively coda_iget:
84   - get attributes (might be cached)
85   - get the inode for the fid using vfs iget
86   - link the two up if this is needed
87   - fill in the attributes
88*/
89int coda_cnode_make(struct inode **inode, struct CodaFid *fid, struct super_block *sb)
90{
91        struct coda_vattr attr;
92        int error;
93
94	/* We get inode numbers from Venus -- see venus source */
95	error = venus_getattr(sb, fid, &attr);
96	if ( error ) {
97	    *inode = NULL;
98	    return error;
99	}
100
101	*inode = coda_iget(sb, fid, &attr);
102	if ( IS_ERR(*inode) ) {
103		printk("coda_cnode_make: coda_iget failed\n");
104                return PTR_ERR(*inode);
105        }
106	return 0;
107}
108
109
110void coda_replace_fid(struct inode *inode, struct CodaFid *oldfid,
111		      struct CodaFid *newfid)
112{
113	struct coda_inode_info *cii;
114	unsigned long hash = coda_f2i(newfid);
115
116	cii = ITOC(inode);
117
118	BUG_ON(!coda_fideq(&cii->c_fid, oldfid));
119
120	/* replace fid and rehash inode */
121	remove_inode_hash(inode);
122	cii->c_fid = *newfid;
123	inode->i_ino = hash;
124	__insert_inode_hash(inode, hash);
125}
126
127/* convert a fid to an inode. */
128struct inode *coda_fid_to_inode(struct CodaFid *fid, struct super_block *sb)
129{
130	struct inode *inode;
131	unsigned long hash = coda_f2i(fid);
132
133	if ( !sb ) {
134		printk("coda_fid_to_inode: no sb!\n");
135		return NULL;
136	}
137
138	inode = ilookup5(sb, hash, coda_test_inode, fid);
139	if ( !inode )
140		return NULL;
141
142	/* we should never see newly created inodes because we intentionally
143	 * fail in the initialization callback */
144	BUG_ON(inode->i_state & I_NEW);
145
146	return inode;
147}
148
149/* the CONTROL inode is made without asking attributes from Venus */
150int coda_cnode_makectl(struct inode **inode, struct super_block *sb)
151{
152	int error = -ENOMEM;
153
154	*inode = new_inode(sb);
155	if (*inode) {
156		(*inode)->i_ino = CTL_INO;
157		(*inode)->i_op = &coda_ioctl_inode_operations;
158		(*inode)->i_fop = &coda_ioctl_operations;
159		(*inode)->i_mode = 0444;
160		error = 0;
161	}
162
163	return error;
164}
165