[PATCH] hrtimers: prevent possible itimer DoS
[linux-2.6.22.y-op.git] / fs / afs / inode.c
blob9d9bca6c28b5be5542c421d47df747595dcedeea
1 /*
2 * Copyright (c) 2002 Red Hat, Inc. All rights reserved.
4 * This software may be freely redistributed under the terms of the
5 * GNU General Public License.
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.
11 * Authors: David Woodhouse <dwmw2@cambridge.redhat.com>
12 * David Howells <dhowells@redhat.com>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/fs.h>
21 #include <linux/pagemap.h>
22 #include "volume.h"
23 #include "vnode.h"
24 #include "super.h"
25 #include "internal.h"
27 struct afs_iget_data {
28 struct afs_fid fid;
29 struct afs_volume *volume; /* volume on which resides */
32 /*****************************************************************************/
34 * map the AFS file status to the inode member variables
36 static int afs_inode_map_status(struct afs_vnode *vnode)
38 struct inode *inode = AFS_VNODE_TO_I(vnode);
40 _debug("FS: ft=%d lk=%d sz=%Zu ver=%Lu mod=%hu",
41 vnode->status.type,
42 vnode->status.nlink,
43 vnode->status.size,
44 vnode->status.version,
45 vnode->status.mode);
47 switch (vnode->status.type) {
48 case AFS_FTYPE_FILE:
49 inode->i_mode = S_IFREG | vnode->status.mode;
50 inode->i_op = &afs_file_inode_operations;
51 inode->i_fop = &generic_ro_fops;
52 break;
53 case AFS_FTYPE_DIR:
54 inode->i_mode = S_IFDIR | vnode->status.mode;
55 inode->i_op = &afs_dir_inode_operations;
56 inode->i_fop = &afs_dir_file_operations;
57 break;
58 case AFS_FTYPE_SYMLINK:
59 inode->i_mode = S_IFLNK | vnode->status.mode;
60 inode->i_op = &page_symlink_inode_operations;
61 break;
62 default:
63 printk("kAFS: AFS vnode with undefined type\n");
64 return -EBADMSG;
67 inode->i_nlink = vnode->status.nlink;
68 inode->i_uid = vnode->status.owner;
69 inode->i_gid = 0;
70 inode->i_size = vnode->status.size;
71 inode->i_ctime.tv_sec = vnode->status.mtime_server;
72 inode->i_ctime.tv_nsec = 0;
73 inode->i_atime = inode->i_mtime = inode->i_ctime;
74 inode->i_blocks = 0;
75 inode->i_version = vnode->fid.unique;
76 inode->i_mapping->a_ops = &afs_fs_aops;
78 /* check to see whether a symbolic link is really a mountpoint */
79 if (vnode->status.type == AFS_FTYPE_SYMLINK) {
80 afs_mntpt_check_symlink(vnode);
82 if (vnode->flags & AFS_VNODE_MOUNTPOINT) {
83 inode->i_mode = S_IFDIR | vnode->status.mode;
84 inode->i_op = &afs_mntpt_inode_operations;
85 inode->i_fop = &afs_mntpt_file_operations;
89 return 0;
90 } /* end afs_inode_map_status() */
92 /*****************************************************************************/
94 * attempt to fetch the status of an inode, coelescing multiple simultaneous
95 * fetches
97 static int afs_inode_fetch_status(struct inode *inode)
99 struct afs_vnode *vnode;
100 int ret;
102 vnode = AFS_FS_I(inode);
104 ret = afs_vnode_fetch_status(vnode);
106 if (ret == 0)
107 ret = afs_inode_map_status(vnode);
109 return ret;
111 } /* end afs_inode_fetch_status() */
113 /*****************************************************************************/
115 * iget5() comparator
117 static int afs_iget5_test(struct inode *inode, void *opaque)
119 struct afs_iget_data *data = opaque;
121 return inode->i_ino == data->fid.vnode &&
122 inode->i_version == data->fid.unique;
123 } /* end afs_iget5_test() */
125 /*****************************************************************************/
127 * iget5() inode initialiser
129 static int afs_iget5_set(struct inode *inode, void *opaque)
131 struct afs_iget_data *data = opaque;
132 struct afs_vnode *vnode = AFS_FS_I(inode);
134 inode->i_ino = data->fid.vnode;
135 inode->i_version = data->fid.unique;
136 vnode->fid = data->fid;
137 vnode->volume = data->volume;
139 return 0;
140 } /* end afs_iget5_set() */
142 /*****************************************************************************/
144 * inode retrieval
146 inline int afs_iget(struct super_block *sb, struct afs_fid *fid,
147 struct inode **_inode)
149 struct afs_iget_data data = { .fid = *fid };
150 struct afs_super_info *as;
151 struct afs_vnode *vnode;
152 struct inode *inode;
153 int ret;
155 _enter(",{%u,%u,%u},,", fid->vid, fid->vnode, fid->unique);
157 as = sb->s_fs_info;
158 data.volume = as->volume;
160 inode = iget5_locked(sb, fid->vnode, afs_iget5_test, afs_iget5_set,
161 &data);
162 if (!inode) {
163 _leave(" = -ENOMEM");
164 return -ENOMEM;
167 vnode = AFS_FS_I(inode);
169 /* deal with an existing inode */
170 if (!(inode->i_state & I_NEW)) {
171 ret = afs_vnode_fetch_status(vnode);
172 if (ret==0)
173 *_inode = inode;
174 else
175 iput(inode);
176 _leave(" = %d", ret);
177 return ret;
180 #ifdef AFS_CACHING_SUPPORT
181 /* set up caching before reading the status, as fetch-status reads the
182 * first page of symlinks to see if they're really mntpts */
183 cachefs_acquire_cookie(vnode->volume->cache,
184 NULL,
185 vnode,
186 &vnode->cache);
187 #endif
189 /* okay... it's a new inode */
190 inode->i_flags |= S_NOATIME;
191 vnode->flags |= AFS_VNODE_CHANGED;
192 ret = afs_inode_fetch_status(inode);
193 if (ret<0)
194 goto bad_inode;
196 /* success */
197 unlock_new_inode(inode);
199 *_inode = inode;
200 _leave(" = 0 [CB { v=%u x=%lu t=%u }]",
201 vnode->cb_version,
202 vnode->cb_timeout.timo_jif,
203 vnode->cb_type);
204 return 0;
206 /* failure */
207 bad_inode:
208 make_bad_inode(inode);
209 unlock_new_inode(inode);
210 iput(inode);
212 _leave(" = %d [bad]", ret);
213 return ret;
214 } /* end afs_iget() */
216 /*****************************************************************************/
218 * read the attributes of an inode
220 int afs_inode_getattr(struct vfsmount *mnt, struct dentry *dentry,
221 struct kstat *stat)
223 struct afs_vnode *vnode;
224 struct inode *inode;
225 int ret;
227 inode = dentry->d_inode;
229 _enter("{ ino=%lu v=%lu }", inode->i_ino, inode->i_version);
231 vnode = AFS_FS_I(inode);
233 ret = afs_inode_fetch_status(inode);
234 if (ret == -ENOENT) {
235 _leave(" = %d [%d %p]",
236 ret, atomic_read(&dentry->d_count), dentry->d_inode);
237 return ret;
239 else if (ret < 0) {
240 make_bad_inode(inode);
241 _leave(" = %d", ret);
242 return ret;
245 /* transfer attributes from the inode structure to the stat
246 * structure */
247 generic_fillattr(inode, stat);
249 _leave(" = 0 CB { v=%u x=%u t=%u }",
250 vnode->cb_version,
251 vnode->cb_expiry,
252 vnode->cb_type);
254 return 0;
255 } /* end afs_inode_getattr() */
257 /*****************************************************************************/
259 * clear an AFS inode
261 void afs_clear_inode(struct inode *inode)
263 struct afs_vnode *vnode;
265 vnode = AFS_FS_I(inode);
267 _enter("ino=%lu { vn=%08x v=%u x=%u t=%u }",
268 inode->i_ino,
269 vnode->fid.vnode,
270 vnode->cb_version,
271 vnode->cb_expiry,
272 vnode->cb_type
275 BUG_ON(inode->i_ino != vnode->fid.vnode);
277 afs_vnode_give_up_callback(vnode);
279 #ifdef AFS_CACHING_SUPPORT
280 cachefs_relinquish_cookie(vnode->cache, 0);
281 vnode->cache = NULL;
282 #endif
284 _leave("");
285 } /* end afs_clear_inode() */