blob: d196840127c6bdf7174defa94fc7184c25e2ad92 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/slab.h>
20#include <linux/fs.h>
21#include <linux/pagemap.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040022#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "internal.h"
24
25struct afs_iget_data {
26 struct afs_fid fid;
27 struct afs_volume *volume; /* volume on which resides */
28};
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/*
31 * map the AFS file status to the inode member variables
32 */
David Howells00d3b7a2007-04-26 15:57:07 -070033static int afs_inode_map_status(struct afs_vnode *vnode, struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
35 struct inode *inode = AFS_VNODE_TO_I(vnode);
36
David Howells260a9802007-04-26 15:59:35 -070037 _debug("FS: ft=%d lk=%d sz=%llu ver=%Lu mod=%hu",
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 vnode->status.type,
39 vnode->status.nlink,
David S. Millerba3e0e12007-04-26 16:06:22 -070040 (unsigned long long) vnode->status.size,
David Howells08e0e7c2007-04-26 15:55:03 -070041 vnode->status.data_version,
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 vnode->status.mode);
43
44 switch (vnode->status.type) {
45 case AFS_FTYPE_FILE:
46 inode->i_mode = S_IFREG | vnode->status.mode;
47 inode->i_op = &afs_file_inode_operations;
David Howells00d3b7a2007-04-26 15:57:07 -070048 inode->i_fop = &afs_file_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 break;
50 case AFS_FTYPE_DIR:
51 inode->i_mode = S_IFDIR | vnode->status.mode;
52 inode->i_op = &afs_dir_inode_operations;
53 inode->i_fop = &afs_dir_file_operations;
54 break;
55 case AFS_FTYPE_SYMLINK:
56 inode->i_mode = S_IFLNK | vnode->status.mode;
57 inode->i_op = &page_symlink_inode_operations;
58 break;
59 default:
60 printk("kAFS: AFS vnode with undefined type\n");
61 return -EBADMSG;
62 }
63
64 inode->i_nlink = vnode->status.nlink;
65 inode->i_uid = vnode->status.owner;
66 inode->i_gid = 0;
67 inode->i_size = vnode->status.size;
68 inode->i_ctime.tv_sec = vnode->status.mtime_server;
69 inode->i_ctime.tv_nsec = 0;
70 inode->i_atime = inode->i_mtime = inode->i_ctime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 inode->i_blocks = 0;
72 inode->i_version = vnode->fid.unique;
73 inode->i_mapping->a_ops = &afs_fs_aops;
74
75 /* check to see whether a symbolic link is really a mountpoint */
76 if (vnode->status.type == AFS_FTYPE_SYMLINK) {
David Howells00d3b7a2007-04-26 15:57:07 -070077 afs_mntpt_check_symlink(vnode, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
David Howells08e0e7c2007-04-26 15:55:03 -070079 if (test_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 inode->i_mode = S_IFDIR | vnode->status.mode;
81 inode->i_op = &afs_mntpt_inode_operations;
82 inode->i_fop = &afs_mntpt_file_operations;
83 }
84 }
85
86 return 0;
David Howellsec268152007-04-26 15:49:28 -070087}
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * iget5() comparator
91 */
92static int afs_iget5_test(struct inode *inode, void *opaque)
93{
94 struct afs_iget_data *data = opaque;
95
96 return inode->i_ino == data->fid.vnode &&
97 inode->i_version == data->fid.unique;
David Howellsec268152007-04-26 15:49:28 -070098}
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/*
101 * iget5() inode initialiser
102 */
103static int afs_iget5_set(struct inode *inode, void *opaque)
104{
105 struct afs_iget_data *data = opaque;
106 struct afs_vnode *vnode = AFS_FS_I(inode);
107
108 inode->i_ino = data->fid.vnode;
109 inode->i_version = data->fid.unique;
110 vnode->fid = data->fid;
111 vnode->volume = data->volume;
112
113 return 0;
David Howellsec268152007-04-26 15:49:28 -0700114}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116/*
117 * inode retrieval
118 */
David Howells260a9802007-04-26 15:59:35 -0700119struct inode *afs_iget(struct super_block *sb, struct key *key,
120 struct afs_fid *fid, struct afs_file_status *status,
121 struct afs_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 struct afs_iget_data data = { .fid = *fid };
124 struct afs_super_info *as;
125 struct afs_vnode *vnode;
126 struct inode *inode;
127 int ret;
128
David Howells416351f2007-05-09 02:33:45 -0700129 _enter(",{%x:%u.%u},,", fid->vid, fid->vnode, fid->unique);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 as = sb->s_fs_info;
132 data.volume = as->volume;
133
134 inode = iget5_locked(sb, fid->vnode, afs_iget5_test, afs_iget5_set,
135 &data);
136 if (!inode) {
137 _leave(" = -ENOMEM");
David Howells08e0e7c2007-04-26 15:55:03 -0700138 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
140
David Howells08e0e7c2007-04-26 15:55:03 -0700141 _debug("GOT INODE %p { vl=%x vn=%x, u=%x }",
142 inode, fid->vid, fid->vnode, fid->unique);
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 vnode = AFS_FS_I(inode);
145
146 /* deal with an existing inode */
147 if (!(inode->i_state & I_NEW)) {
David Howells08e0e7c2007-04-26 15:55:03 -0700148 _leave(" = %p", inode);
149 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151
152#ifdef AFS_CACHING_SUPPORT
153 /* set up caching before reading the status, as fetch-status reads the
154 * first page of symlinks to see if they're really mntpts */
155 cachefs_acquire_cookie(vnode->volume->cache,
156 NULL,
157 vnode,
158 &vnode->cache);
159#endif
160
David Howells260a9802007-04-26 15:59:35 -0700161 if (!status) {
162 /* it's a remotely extant inode */
163 set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
164 ret = afs_vnode_fetch_status(vnode, NULL, key);
165 if (ret < 0)
166 goto bad_inode;
167 } else {
168 /* it's an inode we just created */
169 memcpy(&vnode->status, status, sizeof(vnode->status));
170
171 if (!cb) {
172 /* it's a symlink we just created (the fileserver
173 * didn't give us a callback) */
174 vnode->cb_version = 0;
175 vnode->cb_expiry = 0;
176 vnode->cb_type = 0;
177 vnode->cb_expires = get_seconds();
178 } else {
179 vnode->cb_version = cb->version;
180 vnode->cb_expiry = cb->expiry;
181 vnode->cb_type = cb->type;
182 vnode->cb_expires = vnode->cb_expiry + get_seconds();
183 }
184 }
185
David Howells00d3b7a2007-04-26 15:57:07 -0700186 ret = afs_inode_map_status(vnode, key);
David Howells08e0e7c2007-04-26 15:55:03 -0700187 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 goto bad_inode;
189
190 /* success */
David Howells260a9802007-04-26 15:59:35 -0700191 clear_bit(AFS_VNODE_UNSET, &vnode->flags);
David Howells08e0e7c2007-04-26 15:55:03 -0700192 inode->i_flags |= S_NOATIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 unlock_new_inode(inode);
David Howells08e0e7c2007-04-26 15:55:03 -0700194 _leave(" = %p [CB { v=%u t=%u }]", inode, vnode->cb_version, vnode->cb_type);
195 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 /* failure */
David Howellsec268152007-04-26 15:49:28 -0700198bad_inode:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 make_bad_inode(inode);
200 unlock_new_inode(inode);
201 iput(inode);
202
203 _leave(" = %d [bad]", ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700204 return ERR_PTR(ret);
David Howellsec268152007-04-26 15:49:28 -0700205}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207/*
David Howells416351f2007-05-09 02:33:45 -0700208 * mark the data attached to an inode as obsolete due to a write on the server
209 * - might also want to ditch all the outstanding writes and dirty pages
210 */
211void afs_zap_data(struct afs_vnode *vnode)
212{
David Howells0f300ca2007-05-10 22:22:20 -0700213 _enter("{%x:%u}", vnode->fid.vid, vnode->fid.vnode);
David Howells416351f2007-05-09 02:33:45 -0700214
215 /* nuke all the non-dirty pages that aren't locked, mapped or being
David Howells0f300ca2007-05-10 22:22:20 -0700216 * written back in a regular file and completely discard the pages in a
217 * directory or symlink */
218 if (S_ISREG(vnode->vfs_inode.i_mode))
219 invalidate_remote_inode(&vnode->vfs_inode);
220 else
221 invalidate_inode_pages2(vnode->vfs_inode.i_mapping);
David Howells416351f2007-05-09 02:33:45 -0700222}
223
224/*
David Howells260a9802007-04-26 15:59:35 -0700225 * validate a vnode/inode
226 * - there are several things we need to check
227 * - parent dir data changes (rm, rmdir, rename, mkdir, create, link,
228 * symlink)
229 * - parent dir metadata changed (security changes)
230 * - dentry data changed (write, truncate)
231 * - dentry metadata changed (security changes)
232 */
233int afs_validate(struct afs_vnode *vnode, struct key *key)
234{
235 int ret;
236
237 _enter("{v={%x:%u} fl=%lx},%x",
238 vnode->fid.vid, vnode->fid.vnode, vnode->flags,
239 key_serial(key));
240
241 if (vnode->cb_promised &&
242 !test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags) &&
243 !test_bit(AFS_VNODE_MODIFIED, &vnode->flags) &&
244 !test_bit(AFS_VNODE_ZAP_DATA, &vnode->flags)) {
245 if (vnode->cb_expires < get_seconds() + 10) {
246 _debug("callback expired");
247 set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
248 } else {
249 goto valid;
250 }
251 }
252
253 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
254 goto valid;
255
256 mutex_lock(&vnode->validate_lock);
257
258 /* if the promise has expired, we need to check the server again to get
259 * a new promise - note that if the (parent) directory's metadata was
260 * changed then the security may be different and we may no longer have
261 * access */
262 if (!vnode->cb_promised ||
263 test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags)) {
264 _debug("not promised");
265 ret = afs_vnode_fetch_status(vnode, NULL, key);
266 if (ret < 0)
267 goto error_unlock;
268 _debug("new promise [fl=%lx]", vnode->flags);
269 }
270
271 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
272 _debug("file already deleted");
273 ret = -ESTALE;
274 goto error_unlock;
275 }
276
277 /* if the vnode's data version number changed then its contents are
278 * different */
David Howells416351f2007-05-09 02:33:45 -0700279 if (test_and_clear_bit(AFS_VNODE_ZAP_DATA, &vnode->flags))
280 afs_zap_data(vnode);
David Howells260a9802007-04-26 15:59:35 -0700281
282 clear_bit(AFS_VNODE_MODIFIED, &vnode->flags);
283 mutex_unlock(&vnode->validate_lock);
284valid:
285 _leave(" = 0");
286 return 0;
287
288error_unlock:
289 mutex_unlock(&vnode->validate_lock);
290 _leave(" = %d", ret);
291 return ret;
292}
293
294/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 * read the attributes of an inode
296 */
David Howells416351f2007-05-09 02:33:45 -0700297int afs_getattr(struct vfsmount *mnt, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 struct kstat *stat)
299{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 inode = dentry->d_inode;
303
304 _enter("{ ino=%lu v=%lu }", inode->i_ino, inode->i_version);
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 generic_fillattr(inode, stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return 0;
David Howellsec268152007-04-26 15:49:28 -0700308}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310/*
311 * clear an AFS inode
312 */
313void afs_clear_inode(struct inode *inode)
314{
David Howells00d3b7a2007-04-26 15:57:07 -0700315 struct afs_permits *permits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 struct afs_vnode *vnode;
317
318 vnode = AFS_FS_I(inode);
319
David Howells416351f2007-05-09 02:33:45 -0700320 _enter("{%x:%u.%d} v=%u x=%u t=%u }",
David Howells260a9802007-04-26 15:59:35 -0700321 vnode->fid.vid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 vnode->fid.vnode,
David Howells260a9802007-04-26 15:59:35 -0700323 vnode->fid.unique,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 vnode->cb_version,
325 vnode->cb_expiry,
David Howells08e0e7c2007-04-26 15:55:03 -0700326 vnode->cb_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
David Howells08e0e7c2007-04-26 15:55:03 -0700328 _debug("CLEAR INODE %p", inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
David Howells08e0e7c2007-04-26 15:55:03 -0700330 ASSERTCMP(inode->i_ino, ==, vnode->fid.vnode);
331
332 afs_give_up_callback(vnode);
333
334 if (vnode->server) {
335 spin_lock(&vnode->server->fs_lock);
336 rb_erase(&vnode->server_rb, &vnode->server->fs_vnodes);
337 spin_unlock(&vnode->server->fs_lock);
338 afs_put_server(vnode->server);
339 vnode->server = NULL;
340 }
341
David Howells31143d52007-05-09 02:33:46 -0700342 ASSERT(list_empty(&vnode->writebacks));
David Howells08e0e7c2007-04-26 15:55:03 -0700343 ASSERT(!vnode->cb_promised);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345#ifdef AFS_CACHING_SUPPORT
346 cachefs_relinquish_cookie(vnode->cache, 0);
347 vnode->cache = NULL;
348#endif
349
David Howells00d3b7a2007-04-26 15:57:07 -0700350 mutex_lock(&vnode->permits_lock);
351 permits = vnode->permits;
352 rcu_assign_pointer(vnode->permits, NULL);
353 mutex_unlock(&vnode->permits_lock);
354 if (permits)
355 call_rcu(&permits->rcu, afs_zap_permits);
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700358}
David Howells31143d52007-05-09 02:33:46 -0700359
360/*
361 * set the attributes of an inode
362 */
363int afs_setattr(struct dentry *dentry, struct iattr *attr)
364{
365 struct afs_vnode *vnode = AFS_FS_I(dentry->d_inode);
366 struct key *key;
367 int ret;
368
369 _enter("{%x:%u},{n=%s},%x",
370 vnode->fid.vid, vnode->fid.vnode, dentry->d_name.name,
371 attr->ia_valid);
372
373 if (!(attr->ia_valid & (ATTR_SIZE | ATTR_MODE | ATTR_UID | ATTR_GID |
374 ATTR_MTIME))) {
375 _leave(" = 0 [unsupported]");
376 return 0;
377 }
378
379 /* flush any dirty data outstanding on a regular file */
380 if (S_ISREG(vnode->vfs_inode.i_mode)) {
381 filemap_write_and_wait(vnode->vfs_inode.i_mapping);
382 afs_writeback_all(vnode);
383 }
384
385 if (attr->ia_valid & ATTR_FILE) {
386 key = attr->ia_file->private_data;
387 } else {
388 key = afs_request_key(vnode->volume->cell);
389 if (IS_ERR(key)) {
390 ret = PTR_ERR(key);
391 goto error;
392 }
393 }
394
395 ret = afs_vnode_setattr(vnode, key, attr);
396 if (!(attr->ia_valid & ATTR_FILE))
397 key_put(key);
398
399error:
400 _leave(" = %d", ret);
401 return ret;
402}