xref: /linux/fs/afs/inode.c (revision eb2bce7f5e7ac1ca6da434461217fadf3c688d2c)
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 "internal.h"
23 
24 struct afs_iget_data {
25 	struct afs_fid		fid;
26 	struct afs_volume	*volume;	/* volume on which resides */
27 };
28 
29 /*
30  * map the AFS file status to the inode member variables
31  */
32 static int afs_inode_map_status(struct afs_vnode *vnode, struct key *key)
33 {
34 	struct inode *inode = AFS_VNODE_TO_I(vnode);
35 
36 	_debug("FS: ft=%d lk=%d sz=%llu ver=%Lu mod=%hu",
37 	       vnode->status.type,
38 	       vnode->status.nlink,
39 	       (unsigned long long) vnode->status.size,
40 	       vnode->status.data_version,
41 	       vnode->status.mode);
42 
43 	switch (vnode->status.type) {
44 	case AFS_FTYPE_FILE:
45 		inode->i_mode	= S_IFREG | vnode->status.mode;
46 		inode->i_op	= &afs_file_inode_operations;
47 		inode->i_fop	= &afs_file_operations;
48 		break;
49 	case AFS_FTYPE_DIR:
50 		inode->i_mode	= S_IFDIR | vnode->status.mode;
51 		inode->i_op	= &afs_dir_inode_operations;
52 		inode->i_fop	= &afs_dir_file_operations;
53 		break;
54 	case AFS_FTYPE_SYMLINK:
55 		inode->i_mode	= S_IFLNK | vnode->status.mode;
56 		inode->i_op	= &page_symlink_inode_operations;
57 		break;
58 	default:
59 		printk("kAFS: AFS vnode with undefined type\n");
60 		return -EBADMSG;
61 	}
62 
63 	inode->i_nlink		= vnode->status.nlink;
64 	inode->i_uid		= vnode->status.owner;
65 	inode->i_gid		= 0;
66 	inode->i_size		= vnode->status.size;
67 	inode->i_ctime.tv_sec	= vnode->status.mtime_server;
68 	inode->i_ctime.tv_nsec	= 0;
69 	inode->i_atime		= inode->i_mtime = inode->i_ctime;
70 	inode->i_blocks		= 0;
71 	inode->i_version	= vnode->fid.unique;
72 	inode->i_mapping->a_ops	= &afs_fs_aops;
73 
74 	/* check to see whether a symbolic link is really a mountpoint */
75 	if (vnode->status.type == AFS_FTYPE_SYMLINK) {
76 		afs_mntpt_check_symlink(vnode, key);
77 
78 		if (test_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags)) {
79 			inode->i_mode	= S_IFDIR | vnode->status.mode;
80 			inode->i_op	= &afs_mntpt_inode_operations;
81 			inode->i_fop	= &afs_mntpt_file_operations;
82 		}
83 	}
84 
85 	return 0;
86 }
87 
88 /*
89  * iget5() comparator
90  */
91 static int afs_iget5_test(struct inode *inode, void *opaque)
92 {
93 	struct afs_iget_data *data = opaque;
94 
95 	return inode->i_ino == data->fid.vnode &&
96 		inode->i_version == data->fid.unique;
97 }
98 
99 /*
100  * iget5() inode initialiser
101  */
102 static int afs_iget5_set(struct inode *inode, void *opaque)
103 {
104 	struct afs_iget_data *data = opaque;
105 	struct afs_vnode *vnode = AFS_FS_I(inode);
106 
107 	inode->i_ino = data->fid.vnode;
108 	inode->i_version = data->fid.unique;
109 	vnode->fid = data->fid;
110 	vnode->volume = data->volume;
111 
112 	return 0;
113 }
114 
115 /*
116  * inode retrieval
117  */
118 struct inode *afs_iget(struct super_block *sb, struct key *key,
119 		       struct afs_fid *fid, struct afs_file_status *status,
120 		       struct afs_callback *cb)
121 {
122 	struct afs_iget_data data = { .fid = *fid };
123 	struct afs_super_info *as;
124 	struct afs_vnode *vnode;
125 	struct inode *inode;
126 	int ret;
127 
128 	_enter(",{%u,%u,%u},,", fid->vid, fid->vnode, fid->unique);
129 
130 	as = sb->s_fs_info;
131 	data.volume = as->volume;
132 
133 	inode = iget5_locked(sb, fid->vnode, afs_iget5_test, afs_iget5_set,
134 			     &data);
135 	if (!inode) {
136 		_leave(" = -ENOMEM");
137 		return ERR_PTR(-ENOMEM);
138 	}
139 
140 	_debug("GOT INODE %p { vl=%x vn=%x, u=%x }",
141 	       inode, fid->vid, fid->vnode, fid->unique);
142 
143 	vnode = AFS_FS_I(inode);
144 
145 	/* deal with an existing inode */
146 	if (!(inode->i_state & I_NEW)) {
147 		_leave(" = %p", inode);
148 		return inode;
149 	}
150 
151 #ifdef AFS_CACHING_SUPPORT
152 	/* set up caching before reading the status, as fetch-status reads the
153 	 * first page of symlinks to see if they're really mntpts */
154 	cachefs_acquire_cookie(vnode->volume->cache,
155 			       NULL,
156 			       vnode,
157 			       &vnode->cache);
158 #endif
159 
160 	if (!status) {
161 		/* it's a remotely extant inode */
162 		set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
163 		ret = afs_vnode_fetch_status(vnode, NULL, key);
164 		if (ret < 0)
165 			goto bad_inode;
166 	} else {
167 		/* it's an inode we just created */
168 		memcpy(&vnode->status, status, sizeof(vnode->status));
169 
170 		if (!cb) {
171 			/* it's a symlink we just created (the fileserver
172 			 * didn't give us a callback) */
173 			vnode->cb_version = 0;
174 			vnode->cb_expiry = 0;
175 			vnode->cb_type = 0;
176 			vnode->cb_expires = get_seconds();
177 		} else {
178 			vnode->cb_version = cb->version;
179 			vnode->cb_expiry = cb->expiry;
180 			vnode->cb_type = cb->type;
181 			vnode->cb_expires = vnode->cb_expiry + get_seconds();
182 		}
183 	}
184 
185 	ret = afs_inode_map_status(vnode, key);
186 	if (ret < 0)
187 		goto bad_inode;
188 
189 	/* success */
190 	clear_bit(AFS_VNODE_UNSET, &vnode->flags);
191 	inode->i_flags |= S_NOATIME;
192 	unlock_new_inode(inode);
193 	_leave(" = %p [CB { v=%u t=%u }]", inode, vnode->cb_version, vnode->cb_type);
194 	return inode;
195 
196 	/* failure */
197 bad_inode:
198 	make_bad_inode(inode);
199 	unlock_new_inode(inode);
200 	iput(inode);
201 
202 	_leave(" = %d [bad]", ret);
203 	return ERR_PTR(ret);
204 }
205 
206 /*
207  * validate a vnode/inode
208  * - there are several things we need to check
209  *   - parent dir data changes (rm, rmdir, rename, mkdir, create, link,
210  *     symlink)
211  *   - parent dir metadata changed (security changes)
212  *   - dentry data changed (write, truncate)
213  *   - dentry metadata changed (security changes)
214  */
215 int afs_validate(struct afs_vnode *vnode, struct key *key)
216 {
217 	int ret;
218 
219 	_enter("{v={%x:%u} fl=%lx},%x",
220 	       vnode->fid.vid, vnode->fid.vnode, vnode->flags,
221 	       key_serial(key));
222 
223 	if (vnode->cb_promised &&
224 	    !test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags) &&
225 	    !test_bit(AFS_VNODE_MODIFIED, &vnode->flags) &&
226 	    !test_bit(AFS_VNODE_ZAP_DATA, &vnode->flags)) {
227 		if (vnode->cb_expires < get_seconds() + 10) {
228 			_debug("callback expired");
229 			set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
230 		} else {
231 			goto valid;
232 		}
233 	}
234 
235 	if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
236 		goto valid;
237 
238 	mutex_lock(&vnode->validate_lock);
239 
240 	/* if the promise has expired, we need to check the server again to get
241 	 * a new promise - note that if the (parent) directory's metadata was
242 	 * changed then the security may be different and we may no longer have
243 	 * access */
244 	if (!vnode->cb_promised ||
245 	    test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags)) {
246 		_debug("not promised");
247 		ret = afs_vnode_fetch_status(vnode, NULL, key);
248 		if (ret < 0)
249 			goto error_unlock;
250 		_debug("new promise [fl=%lx]", vnode->flags);
251 	}
252 
253 	if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
254 		_debug("file already deleted");
255 		ret = -ESTALE;
256 		goto error_unlock;
257 	}
258 
259 	/* if the vnode's data version number changed then its contents are
260 	 * different */
261 	if (test_and_clear_bit(AFS_VNODE_ZAP_DATA, &vnode->flags)) {
262 		_debug("zap data {%x:%d}", vnode->fid.vid, vnode->fid.vnode);
263 		invalidate_remote_inode(&vnode->vfs_inode);
264 	}
265 
266 	clear_bit(AFS_VNODE_MODIFIED, &vnode->flags);
267 	mutex_unlock(&vnode->validate_lock);
268 valid:
269 	_leave(" = 0");
270 	return 0;
271 
272 error_unlock:
273 	mutex_unlock(&vnode->validate_lock);
274 	_leave(" = %d", ret);
275 	return ret;
276 }
277 
278 /*
279  * read the attributes of an inode
280  */
281 int afs_inode_getattr(struct vfsmount *mnt, struct dentry *dentry,
282 		      struct kstat *stat)
283 {
284 	struct inode *inode;
285 
286 	inode = dentry->d_inode;
287 
288 	_enter("{ ino=%lu v=%lu }", inode->i_ino, inode->i_version);
289 
290 	generic_fillattr(inode, stat);
291 	return 0;
292 }
293 
294 /*
295  * clear an AFS inode
296  */
297 void afs_clear_inode(struct inode *inode)
298 {
299 	struct afs_permits *permits;
300 	struct afs_vnode *vnode;
301 
302 	vnode = AFS_FS_I(inode);
303 
304 	_enter("{%x:%d.%d} v=%u x=%u t=%u }",
305 	       vnode->fid.vid,
306 	       vnode->fid.vnode,
307 	       vnode->fid.unique,
308 	       vnode->cb_version,
309 	       vnode->cb_expiry,
310 	       vnode->cb_type);
311 
312 	_debug("CLEAR INODE %p", inode);
313 
314 	ASSERTCMP(inode->i_ino, ==, vnode->fid.vnode);
315 
316 	afs_give_up_callback(vnode);
317 
318 	if (vnode->server) {
319 		spin_lock(&vnode->server->fs_lock);
320 		rb_erase(&vnode->server_rb, &vnode->server->fs_vnodes);
321 		spin_unlock(&vnode->server->fs_lock);
322 		afs_put_server(vnode->server);
323 		vnode->server = NULL;
324 	}
325 
326 	ASSERT(!vnode->cb_promised);
327 
328 #ifdef AFS_CACHING_SUPPORT
329 	cachefs_relinquish_cookie(vnode->cache, 0);
330 	vnode->cache = NULL;
331 #endif
332 
333 	mutex_lock(&vnode->permits_lock);
334 	permits = vnode->permits;
335 	rcu_assign_pointer(vnode->permits, NULL);
336 	mutex_unlock(&vnode->permits_lock);
337 	if (permits)
338 		call_rcu(&permits->rcu, afs_zap_permits);
339 
340 	_leave("");
341 }
342