ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / coda / cnode.c
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
14 inline int coda_fideq(struct CodaFid *fid1, struct CodaFid *fid2)
15 {
16         return memcmp(fid1, fid2, sizeof(*fid1)) == 0;
17 }
18
19 static struct inode_operations coda_symlink_inode_operations = {
20         .readlink       = page_readlink,
21         .follow_link    = page_follow_link,
22         .setattr        = coda_setattr,
23 };
24
25 /* cnode.c */
26 static 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;
38                 inode->i_data.a_ops = &coda_symlink_aops;
39                 inode->i_mapping = &inode->i_data;
40         } else
41                 init_special_inode(inode, inode->i_mode, huge_decode_dev(attr->va_rdev));
42 }
43
44 static int coda_test_inode(struct inode *inode, void *data)
45 {
46         struct CodaFid *fid = (struct CodaFid *)data;
47         return coda_fideq(&(ITOC(inode)->c_fid), fid);
48 }
49
50 static int coda_set_inode(struct inode *inode, void *data)
51 {
52         struct CodaFid *fid = (struct CodaFid *)data;
53         ITOC(inode)->c_fid = *fid;
54         return 0;
55 }
56
57 static int coda_fail_inode(struct inode *inode, void *data)
58 {
59         return -1;
60 }
61
62 struct inode * coda_iget(struct super_block * sb, struct CodaFid * fid,
63                          struct coda_vattr * attr)
64 {
65         struct inode *inode;
66         struct coda_inode_info *cii;
67         unsigned long hash = coda_f2i(fid);
68
69         inode = iget5_locked(sb, hash, coda_test_inode, coda_set_inode, fid);
70
71         if (!inode)
72                 return ERR_PTR(-ENOMEM);
73
74         if (inode->i_state & I_NEW) {
75                 cii = ITOC(inode);
76                 /* we still need to set i_ino for things like stat(2) */
77                 inode->i_ino = hash;
78                 cii->c_mapcount = 0;
79                 unlock_new_inode(inode);
80         }
81
82         /* always replace the attributes, type might have changed */
83         coda_fill_inode(inode, attr);
84         return inode;
85 }
86
87 /* this is effectively coda_iget:
88    - get attributes (might be cached)
89    - get the inode for the fid using vfs iget
90    - link the two up if this is needed
91    - fill in the attributes
92 */
93 int coda_cnode_make(struct inode **inode, struct CodaFid *fid, struct super_block *sb)
94 {
95         struct coda_vattr attr;
96         int error;
97         
98         /* We get inode numbers from Venus -- see venus source */
99         error = venus_getattr(sb, fid, &attr);
100         if ( error ) {
101             *inode = NULL;
102             return error;
103         } 
104
105         *inode = coda_iget(sb, fid, &attr);
106         if ( IS_ERR(*inode) ) {
107                 printk("coda_cnode_make: coda_iget failed\n");
108                 return PTR_ERR(*inode);
109         }
110         return 0;
111 }
112
113
114 void coda_replace_fid(struct inode *inode, struct CodaFid *oldfid, 
115                       struct CodaFid *newfid)
116 {
117         struct coda_inode_info *cii;
118         unsigned long hash = coda_f2i(newfid);
119         
120         cii = ITOC(inode);
121
122         if (!coda_fideq(&cii->c_fid, oldfid))
123                 BUG();
124
125         /* replace fid and rehash inode */
126         /* XXX we probably need to hold some lock here! */
127         remove_inode_hash(inode);
128         cii->c_fid = *newfid;
129         inode->i_ino = hash;
130         __insert_inode_hash(inode, hash);
131 }
132
133 /* convert a fid to an inode. */
134 struct inode *coda_fid_to_inode(struct CodaFid *fid, struct super_block *sb) 
135 {
136         struct inode *inode;
137         unsigned long hash = coda_f2i(fid);
138
139         if ( !sb ) {
140                 printk("coda_fid_to_inode: no sb!\n");
141                 return NULL;
142         }
143
144         inode = iget5_locked(sb, hash, coda_test_inode, coda_fail_inode, fid);
145         if ( !inode )
146                 return NULL;
147
148         /* we should never see newly created inodes because we intentionally
149          * fail in the initialization callback */
150         BUG_ON(inode->i_state & I_NEW);
151
152         return inode;
153 }
154
155 /* the CONTROL inode is made without asking attributes from Venus */
156 int coda_cnode_makectl(struct inode **inode, struct super_block *sb)
157 {
158         int error = -ENOMEM;
159
160         *inode = new_inode(sb);
161         if (*inode) {
162                 (*inode)->i_ino = CTL_INO;
163                 (*inode)->i_op = &coda_ioctl_inode_operations;
164                 (*inode)->i_fop = &coda_ioctl_operations;
165                 (*inode)->i_mode = 0444;
166                 error = 0;
167         }
168
169         return error;
170 }
171