xref: /linux/fs/afs/inode.c (revision c537b994505099b7197e7d3125b942ecbcc51eb6)
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/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"
26 
27 struct afs_iget_data {
28 	struct afs_fid		fid;
29 	struct afs_volume	*volume;	/* volume on which resides */
30 };
31 
32 /*****************************************************************************/
33 /*
34  * map the AFS file status to the inode member variables
35  */
36 static int afs_inode_map_status(struct afs_vnode *vnode)
37 {
38 	struct inode *inode = AFS_VNODE_TO_I(vnode);
39 
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);
46 
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;
65 	}
66 
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;
77 
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);
81 
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;
86 		}
87 	}
88 
89 	return 0;
90 } /* end afs_inode_map_status() */
91 
92 /*****************************************************************************/
93 /*
94  * attempt to fetch the status of an inode, coelescing multiple simultaneous
95  * fetches
96  */
97 static int afs_inode_fetch_status(struct inode *inode)
98 {
99 	struct afs_vnode *vnode;
100 	int ret;
101 
102 	vnode = AFS_FS_I(inode);
103 
104 	ret = afs_vnode_fetch_status(vnode);
105 
106 	if (ret == 0)
107 		ret = afs_inode_map_status(vnode);
108 
109 	return ret;
110 
111 } /* end afs_inode_fetch_status() */
112 
113 /*****************************************************************************/
114 /*
115  * iget5() comparator
116  */
117 static int afs_iget5_test(struct inode *inode, void *opaque)
118 {
119 	struct afs_iget_data *data = opaque;
120 
121 	return inode->i_ino == data->fid.vnode &&
122 		inode->i_version == data->fid.unique;
123 } /* end afs_iget5_test() */
124 
125 /*****************************************************************************/
126 /*
127  * iget5() inode initialiser
128  */
129 static int afs_iget5_set(struct inode *inode, void *opaque)
130 {
131 	struct afs_iget_data *data = opaque;
132 	struct afs_vnode *vnode = AFS_FS_I(inode);
133 
134 	inode->i_ino = data->fid.vnode;
135 	inode->i_version = data->fid.unique;
136 	vnode->fid = data->fid;
137 	vnode->volume = data->volume;
138 
139 	return 0;
140 } /* end afs_iget5_set() */
141 
142 /*****************************************************************************/
143 /*
144  * inode retrieval
145  */
146 inline int afs_iget(struct super_block *sb, struct afs_fid *fid,
147 		    struct inode **_inode)
148 {
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;
154 
155 	_enter(",{%u,%u,%u},,", fid->vid, fid->vnode, fid->unique);
156 
157 	as = sb->s_fs_info;
158 	data.volume = as->volume;
159 
160 	inode = iget5_locked(sb, fid->vnode, afs_iget5_test, afs_iget5_set,
161 			     &data);
162 	if (!inode) {
163 		_leave(" = -ENOMEM");
164 		return -ENOMEM;
165 	}
166 
167 	vnode = AFS_FS_I(inode);
168 
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;
178 	}
179 
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
188 
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;
195 
196 	/* success */
197 	unlock_new_inode(inode);
198 
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;
205 
206 	/* failure */
207  bad_inode:
208 	make_bad_inode(inode);
209 	unlock_new_inode(inode);
210 	iput(inode);
211 
212 	_leave(" = %d [bad]", ret);
213 	return ret;
214 } /* end afs_iget() */
215 
216 /*****************************************************************************/
217 /*
218  * read the attributes of an inode
219  */
220 int afs_inode_getattr(struct vfsmount *mnt, struct dentry *dentry,
221 		      struct kstat *stat)
222 {
223 	struct afs_vnode *vnode;
224 	struct inode *inode;
225 	int ret;
226 
227 	inode = dentry->d_inode;
228 
229 	_enter("{ ino=%lu v=%lu }", inode->i_ino, inode->i_version);
230 
231 	vnode = AFS_FS_I(inode);
232 
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;
238 	}
239 	else if (ret < 0) {
240 		make_bad_inode(inode);
241 		_leave(" = %d", ret);
242 		return ret;
243 	}
244 
245 	/* transfer attributes from the inode structure to the stat
246 	 * structure */
247 	generic_fillattr(inode, stat);
248 
249 	_leave(" = 0 CB { v=%u x=%u t=%u }",
250 	       vnode->cb_version,
251 	       vnode->cb_expiry,
252 	       vnode->cb_type);
253 
254 	return 0;
255 } /* end afs_inode_getattr() */
256 
257 /*****************************************************************************/
258 /*
259  * clear an AFS inode
260  */
261 void afs_clear_inode(struct inode *inode)
262 {
263 	struct afs_vnode *vnode;
264 
265 	vnode = AFS_FS_I(inode);
266 
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
273 	       );
274 
275 	BUG_ON(inode->i_ino != vnode->fid.vnode);
276 
277 	afs_vnode_give_up_callback(vnode);
278 
279 #ifdef AFS_CACHING_SUPPORT
280 	cachefs_relinquish_cookie(vnode->cache, 0);
281 	vnode->cache = NULL;
282 #endif
283 
284 	_leave("");
285 } /* end afs_clear_inode() */
286