Merge master.kernel.org:/pub/scm/linux/kernel/git/lethal/sh-2.6
[pandora-kernel.git] / fs / afs / inode.c
1 /*
2  * Copyright (c) 2002 Red Hat, Inc. All rights reserved.
3  *
4  * This software may be freely redistributed under the terms of the
5  * GNU General Public License.
6  *
7  * You should have received a copy of the GNU General Public License
8  * along with this program; if not, write to the Free Software
9  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
10  *
11  * Authors: David Woodhouse <dwmw2@cambridge.redhat.com>
12  *          David Howells <dhowells@redhat.com>
13  *
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/fs.h>
22 #include <linux/pagemap.h>
23 #include "volume.h"
24 #include "vnode.h"
25 #include "super.h"
26 #include "internal.h"
27
28 struct afs_iget_data {
29         struct afs_fid          fid;
30         struct afs_volume       *volume;        /* volume on which resides */
31 };
32
33 /*****************************************************************************/
34 /*
35  * map the AFS file status to the inode member variables
36  */
37 static int afs_inode_map_status(struct afs_vnode *vnode)
38 {
39         struct inode *inode = AFS_VNODE_TO_I(vnode);
40
41         _debug("FS: ft=%d lk=%d sz=%Zu ver=%Lu mod=%hu",
42                vnode->status.type,
43                vnode->status.nlink,
44                vnode->status.size,
45                vnode->status.version,
46                vnode->status.mode);
47
48         switch (vnode->status.type) {
49         case AFS_FTYPE_FILE:
50                 inode->i_mode   = S_IFREG | vnode->status.mode;
51                 inode->i_op     = &afs_file_inode_operations;
52                 inode->i_fop    = &generic_ro_fops;
53                 break;
54         case AFS_FTYPE_DIR:
55                 inode->i_mode   = S_IFDIR | vnode->status.mode;
56                 inode->i_op     = &afs_dir_inode_operations;
57                 inode->i_fop    = &afs_dir_file_operations;
58                 break;
59         case AFS_FTYPE_SYMLINK:
60                 inode->i_mode   = S_IFLNK | vnode->status.mode;
61                 inode->i_op     = &page_symlink_inode_operations;
62                 break;
63         default:
64                 printk("kAFS: AFS vnode with undefined type\n");
65                 return -EBADMSG;
66         }
67
68         inode->i_nlink          = vnode->status.nlink;
69         inode->i_uid            = vnode->status.owner;
70         inode->i_gid            = 0;
71         inode->i_size           = vnode->status.size;
72         inode->i_ctime.tv_sec   = vnode->status.mtime_server;
73         inode->i_ctime.tv_nsec  = 0;
74         inode->i_atime          = inode->i_mtime = inode->i_ctime;
75         inode->i_blocks         = 0;
76         inode->i_version        = vnode->fid.unique;
77         inode->i_mapping->a_ops = &afs_fs_aops;
78
79         /* check to see whether a symbolic link is really a mountpoint */
80         if (vnode->status.type == AFS_FTYPE_SYMLINK) {
81                 afs_mntpt_check_symlink(vnode);
82
83                 if (vnode->flags & AFS_VNODE_MOUNTPOINT) {
84                         inode->i_mode   = S_IFDIR | vnode->status.mode;
85                         inode->i_op     = &afs_mntpt_inode_operations;
86                         inode->i_fop    = &afs_mntpt_file_operations;
87                 }
88         }
89
90         return 0;
91 } /* end afs_inode_map_status() */
92
93 /*****************************************************************************/
94 /*
95  * attempt to fetch the status of an inode, coelescing multiple simultaneous
96  * fetches
97  */
98 static int afs_inode_fetch_status(struct inode *inode)
99 {
100         struct afs_vnode *vnode;
101         int ret;
102
103         vnode = AFS_FS_I(inode);
104
105         ret = afs_vnode_fetch_status(vnode);
106
107         if (ret == 0)
108                 ret = afs_inode_map_status(vnode);
109
110         return ret;
111
112 } /* end afs_inode_fetch_status() */
113
114 /*****************************************************************************/
115 /*
116  * iget5() comparator
117  */
118 static int afs_iget5_test(struct inode *inode, void *opaque)
119 {
120         struct afs_iget_data *data = opaque;
121
122         return inode->i_ino == data->fid.vnode &&
123                 inode->i_version == data->fid.unique;
124 } /* end afs_iget5_test() */
125
126 /*****************************************************************************/
127 /*
128  * iget5() inode initialiser
129  */
130 static int afs_iget5_set(struct inode *inode, void *opaque)
131 {
132         struct afs_iget_data *data = opaque;
133         struct afs_vnode *vnode = AFS_FS_I(inode);
134
135         inode->i_ino = data->fid.vnode;
136         inode->i_version = data->fid.unique;
137         vnode->fid = data->fid;
138         vnode->volume = data->volume;
139
140         return 0;
141 } /* end afs_iget5_set() */
142
143 /*****************************************************************************/
144 /*
145  * inode retrieval
146  */
147 inline int afs_iget(struct super_block *sb, struct afs_fid *fid,
148                     struct inode **_inode)
149 {
150         struct afs_iget_data data = { .fid = *fid };
151         struct afs_super_info *as;
152         struct afs_vnode *vnode;
153         struct inode *inode;
154         int ret;
155
156         _enter(",{%u,%u,%u},,", fid->vid, fid->vnode, fid->unique);
157
158         as = sb->s_fs_info;
159         data.volume = as->volume;
160
161         inode = iget5_locked(sb, fid->vnode, afs_iget5_test, afs_iget5_set,
162                              &data);
163         if (!inode) {
164                 _leave(" = -ENOMEM");
165                 return -ENOMEM;
166         }
167
168         vnode = AFS_FS_I(inode);
169
170         /* deal with an existing inode */
171         if (!(inode->i_state & I_NEW)) {
172                 ret = afs_vnode_fetch_status(vnode);
173                 if (ret==0)
174                         *_inode = inode;
175                 else
176                         iput(inode);
177                 _leave(" = %d", ret);
178                 return ret;
179         }
180
181 #ifdef AFS_CACHING_SUPPORT
182         /* set up caching before reading the status, as fetch-status reads the
183          * first page of symlinks to see if they're really mntpts */
184         cachefs_acquire_cookie(vnode->volume->cache,
185                                NULL,
186                                vnode,
187                                &vnode->cache);
188 #endif
189
190         /* okay... it's a new inode */
191         inode->i_flags |= S_NOATIME;
192         vnode->flags |= AFS_VNODE_CHANGED;
193         ret = afs_inode_fetch_status(inode);
194         if (ret<0)
195                 goto bad_inode;
196
197         /* success */
198         unlock_new_inode(inode);
199
200         *_inode = inode;
201         _leave(" = 0 [CB { v=%u x=%lu t=%u }]",
202                vnode->cb_version,
203                vnode->cb_timeout.timo_jif,
204                vnode->cb_type);
205         return 0;
206
207         /* failure */
208  bad_inode:
209         make_bad_inode(inode);
210         unlock_new_inode(inode);
211         iput(inode);
212
213         _leave(" = %d [bad]", ret);
214         return ret;
215 } /* end afs_iget() */
216
217 /*****************************************************************************/
218 /*
219  * read the attributes of an inode
220  */
221 int afs_inode_getattr(struct vfsmount *mnt, struct dentry *dentry,
222                       struct kstat *stat)
223 {
224         struct afs_vnode *vnode;
225         struct inode *inode;
226         int ret;
227
228         inode = dentry->d_inode;
229
230         _enter("{ ino=%lu v=%lu }", inode->i_ino, inode->i_version);
231
232         vnode = AFS_FS_I(inode);
233
234         ret = afs_inode_fetch_status(inode);
235         if (ret == -ENOENT) {
236                 _leave(" = %d [%d %p]",
237                        ret, atomic_read(&dentry->d_count), dentry->d_inode);
238                 return ret;
239         }
240         else if (ret < 0) {
241                 make_bad_inode(inode);
242                 _leave(" = %d", ret);
243                 return ret;
244         }
245
246         /* transfer attributes from the inode structure to the stat
247          * structure */
248         generic_fillattr(inode, stat);
249
250         _leave(" = 0 CB { v=%u x=%u t=%u }",
251                vnode->cb_version,
252                vnode->cb_expiry,
253                vnode->cb_type);
254
255         return 0;
256 } /* end afs_inode_getattr() */
257
258 /*****************************************************************************/
259 /*
260  * clear an AFS inode
261  */
262 void afs_clear_inode(struct inode *inode)
263 {
264         struct afs_vnode *vnode;
265
266         vnode = AFS_FS_I(inode);
267
268         _enter("ino=%lu { vn=%08x v=%u x=%u t=%u }",
269                inode->i_ino,
270                vnode->fid.vnode,
271                vnode->cb_version,
272                vnode->cb_expiry,
273                vnode->cb_type
274                );
275
276         BUG_ON(inode->i_ino != vnode->fid.vnode);
277
278         afs_vnode_give_up_callback(vnode);
279
280 #ifdef AFS_CACHING_SUPPORT
281         cachefs_relinquish_cookie(vnode->cache, 0);
282         vnode->cache = NULL;
283 #endif
284
285         _leave("");
286 } /* end afs_clear_inode() */