blob: 602240569c89ace556721e0f4d9c954bfd27c491 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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
Arjan van de Ven754661f2007-02-12 00:55:38 -080019static const struct inode_operations coda_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 .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;
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);
66
67 inode = iget5_locked(sb, hash, coda_test_inode, coda_set_inode, fid);
68
69 if (!inode)
70 return ERR_PTR(-ENOMEM);
71
72 if (inode->i_state & I_NEW) {
73 cii = ITOC(inode);
74 /* we still need to set i_ino for things like stat(2) */
75 inode->i_ino = hash;
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -040076 /* inode is locked and unique, no need to grab cii->c_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 cii->c_mapcount = 0;
78 unlock_new_inode(inode);
79 }
80
81 /* always replace the attributes, type might have changed */
82 coda_fill_inode(inode, attr);
83 return inode;
84}
85
86/* this is effectively coda_iget:
87 - get attributes (might be cached)
88 - get the inode for the fid using vfs iget
89 - link the two up if this is needed
90 - fill in the attributes
91*/
92int coda_cnode_make(struct inode **inode, struct CodaFid *fid, struct super_block *sb)
93{
94 struct coda_vattr attr;
95 int error;
96
97 /* We get inode numbers from Venus -- see venus source */
98 error = venus_getattr(sb, fid, &attr);
99 if ( error ) {
100 *inode = NULL;
101 return error;
102 }
103
104 *inode = coda_iget(sb, fid, &attr);
105 if ( IS_ERR(*inode) ) {
106 printk("coda_cnode_make: coda_iget failed\n");
107 return PTR_ERR(*inode);
108 }
109 return 0;
110}
111
112
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -0400113/* Although we treat Coda file identifiers as immutable, there is one
114 * special case for files created during a disconnection where they may
115 * not be globally unique. When an identifier collision is detected we
116 * first try to flush the cached inode from the kernel and finally
117 * resort to renaming/rehashing in-place. Userspace remembers both old
118 * and new values of the identifier to handle any in-flight upcalls.
119 * The real solution is to use globally unique UUIDs as identifiers, but
120 * retrofitting the existing userspace code for this is non-trivial. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121void coda_replace_fid(struct inode *inode, struct CodaFid *oldfid,
122 struct CodaFid *newfid)
123{
Yoshihisa Abeb5ce1d82010-10-25 02:03:44 -0400124 struct coda_inode_info *cii = ITOC(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 unsigned long hash = coda_f2i(newfid);
126
Eric Sesterhennc5d32372006-03-24 18:42:13 +0100127 BUG_ON(!coda_fideq(&cii->c_fid, oldfid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 /* replace fid and rehash inode */
130 /* XXX we probably need to hold some lock here! */
131 remove_inode_hash(inode);
132 cii->c_fid = *newfid;
133 inode->i_ino = hash;
134 __insert_inode_hash(inode, hash);
135}
136
137/* convert a fid to an inode. */
138struct inode *coda_fid_to_inode(struct CodaFid *fid, struct super_block *sb)
139{
140 struct inode *inode;
141 unsigned long hash = coda_f2i(fid);
142
143 if ( !sb ) {
144 printk("coda_fid_to_inode: no sb!\n");
145 return NULL;
146 }
147
Jan Harkesed31a7d2007-07-19 01:48:44 -0700148 inode = ilookup5(sb, hash, coda_test_inode, fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if ( !inode )
150 return NULL;
151
152 /* we should never see newly created inodes because we intentionally
153 * fail in the initialization callback */
154 BUG_ON(inode->i_state & I_NEW);
155
156 return inode;
157}
158
159/* the CONTROL inode is made without asking attributes from Venus */
160int coda_cnode_makectl(struct inode **inode, struct super_block *sb)
161{
162 int error = -ENOMEM;
163
164 *inode = new_inode(sb);
165 if (*inode) {
166 (*inode)->i_ino = CTL_INO;
167 (*inode)->i_op = &coda_ioctl_inode_operations;
168 (*inode)->i_fop = &coda_ioctl_operations;
169 (*inode)->i_mode = 0444;
170 error = 0;
171 }
172
173 return error;
174}
175