blob: 232c55dc245df2dd25a273b24644961394f0fe7b [file] [log] [blame]
David Howellsec268152007-04-26 15:49:28 -07001/* AFS vnode management
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
David Howells08e0e7c2007-04-26 15:55:03 -07003 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/slab.h>
16#include <linux/fs.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040017#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "internal.h"
19
David Howells08e0e7c2007-04-26 15:55:03 -070020#if 0
21static noinline bool dump_tree_aux(struct rb_node *node, struct rb_node *parent,
22 int depth, char lr)
23{
24 struct afs_vnode *vnode;
25 bool bad = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
David Howells08e0e7c2007-04-26 15:55:03 -070027 if (!node)
28 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
David Howells08e0e7c2007-04-26 15:55:03 -070030 if (node->rb_left)
31 bad = dump_tree_aux(node->rb_left, node, depth + 2, '/');
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
David Howells08e0e7c2007-04-26 15:55:03 -070033 vnode = rb_entry(node, struct afs_vnode, cb_promise);
David Howells260a9802007-04-26 15:59:35 -070034 _debug("%c %*.*s%c%p {%d}",
David Howells08e0e7c2007-04-26 15:55:03 -070035 rb_is_red(node) ? 'R' : 'B',
36 depth, depth, "", lr,
37 vnode, vnode->cb_expires_at);
38 if (rb_parent(node) != parent) {
39 printk("BAD: %p != %p\n", rb_parent(node), parent);
40 bad = true;
41 }
42
43 if (node->rb_right)
44 bad |= dump_tree_aux(node->rb_right, node, depth + 2, '\\');
45
46 return bad;
47}
48
49static noinline void dump_tree(const char *name, struct afs_server *server)
50{
David Howells260a9802007-04-26 15:59:35 -070051 _enter("%s", name);
David Howells08e0e7c2007-04-26 15:55:03 -070052 if (dump_tree_aux(server->cb_promises.rb_node, NULL, 0, '-'))
53 BUG();
54}
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#endif
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
David Howells08e0e7c2007-04-26 15:55:03 -070058 * insert a vnode into the backing server's vnode tree
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 */
David Howells08e0e7c2007-04-26 15:55:03 -070060static void afs_install_vnode(struct afs_vnode *vnode,
61 struct afs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
David Howells08e0e7c2007-04-26 15:55:03 -070063 struct afs_server *old_server = vnode->server;
64 struct afs_vnode *xvnode;
65 struct rb_node *parent, **p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
David Howells08e0e7c2007-04-26 15:55:03 -070067 _enter("%p,%p", vnode, server);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
David Howells08e0e7c2007-04-26 15:55:03 -070069 if (old_server) {
70 spin_lock(&old_server->fs_lock);
71 rb_erase(&vnode->server_rb, &old_server->fs_vnodes);
72 spin_unlock(&old_server->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
74
David Howells08e0e7c2007-04-26 15:55:03 -070075 afs_get_server(server);
76 vnode->server = server;
77 afs_put_server(old_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
David Howells08e0e7c2007-04-26 15:55:03 -070079 /* insert into the server's vnode tree in FID order */
80 spin_lock(&server->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
David Howells08e0e7c2007-04-26 15:55:03 -070082 parent = NULL;
83 p = &server->fs_vnodes.rb_node;
84 while (*p) {
85 parent = *p;
86 xvnode = rb_entry(parent, struct afs_vnode, server_rb);
87 if (vnode->fid.vid < xvnode->fid.vid)
88 p = &(*p)->rb_left;
89 else if (vnode->fid.vid > xvnode->fid.vid)
90 p = &(*p)->rb_right;
91 else if (vnode->fid.vnode < xvnode->fid.vnode)
92 p = &(*p)->rb_left;
93 else if (vnode->fid.vnode > xvnode->fid.vnode)
94 p = &(*p)->rb_right;
95 else if (vnode->fid.unique < xvnode->fid.unique)
96 p = &(*p)->rb_left;
97 else if (vnode->fid.unique > xvnode->fid.unique)
98 p = &(*p)->rb_right;
99 else
100 BUG(); /* can't happen unless afs_iget() malfunctions */
101 }
102
103 rb_link_node(&vnode->server_rb, parent, p);
104 rb_insert_color(&vnode->server_rb, &server->fs_vnodes);
105
106 spin_unlock(&server->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700108}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/*
David Howells08e0e7c2007-04-26 15:55:03 -0700111 * insert a vnode into the promising server's update/expiration tree
112 * - caller must hold vnode->lock
113 */
114static void afs_vnode_note_promise(struct afs_vnode *vnode,
115 struct afs_server *server)
116{
117 struct afs_server *old_server;
118 struct afs_vnode *xvnode;
119 struct rb_node *parent, **p;
120
121 _enter("%p,%p", vnode, server);
122
123 ASSERT(server != NULL);
124
125 old_server = vnode->server;
126 if (vnode->cb_promised) {
127 if (server == old_server &&
128 vnode->cb_expires == vnode->cb_expires_at) {
129 _leave(" [no change]");
130 return;
131 }
132
133 spin_lock(&old_server->cb_lock);
134 if (vnode->cb_promised) {
135 _debug("delete");
136 rb_erase(&vnode->cb_promise, &old_server->cb_promises);
137 vnode->cb_promised = false;
138 }
139 spin_unlock(&old_server->cb_lock);
140 }
141
142 if (vnode->server != server)
143 afs_install_vnode(vnode, server);
144
145 vnode->cb_expires_at = vnode->cb_expires;
146 _debug("PROMISE on %p {%lu}",
147 vnode, (unsigned long) vnode->cb_expires_at);
148
149 /* abuse an RB-tree to hold the expiration order (we may have multiple
150 * items with the same expiration time) */
151 spin_lock(&server->cb_lock);
152
153 parent = NULL;
154 p = &server->cb_promises.rb_node;
155 while (*p) {
156 parent = *p;
157 xvnode = rb_entry(parent, struct afs_vnode, cb_promise);
158 if (vnode->cb_expires_at < xvnode->cb_expires_at)
159 p = &(*p)->rb_left;
160 else
161 p = &(*p)->rb_right;
162 }
163
164 rb_link_node(&vnode->cb_promise, parent, p);
165 rb_insert_color(&vnode->cb_promise, &server->cb_promises);
166 vnode->cb_promised = true;
167
168 spin_unlock(&server->cb_lock);
169 _leave("");
170}
171
172/*
173 * handle remote file deletion by discarding the callback promise
174 */
175static void afs_vnode_deleted_remotely(struct afs_vnode *vnode)
176{
177 struct afs_server *server;
178
David Howells0f300ca2007-05-10 22:22:20 -0700179 _enter("{%p}", vnode->server);
180
David Howells08e0e7c2007-04-26 15:55:03 -0700181 set_bit(AFS_VNODE_DELETED, &vnode->flags);
182
183 server = vnode->server;
David Howells0f300ca2007-05-10 22:22:20 -0700184 if (server) {
David Howells08e0e7c2007-04-26 15:55:03 -0700185 if (vnode->cb_promised) {
David Howells0f300ca2007-05-10 22:22:20 -0700186 spin_lock(&server->cb_lock);
187 if (vnode->cb_promised) {
188 rb_erase(&vnode->cb_promise,
189 &server->cb_promises);
190 vnode->cb_promised = false;
191 }
192 spin_unlock(&server->cb_lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700193 }
David Howells0f300ca2007-05-10 22:22:20 -0700194
195 spin_lock(&server->fs_lock);
196 rb_erase(&vnode->server_rb, &server->fs_vnodes);
197 spin_unlock(&server->fs_lock);
198
199 vnode->server = NULL;
200 afs_put_server(server);
201 } else {
202 ASSERT(!vnode->cb_promised);
David Howells08e0e7c2007-04-26 15:55:03 -0700203 }
204
David Howells0f300ca2007-05-10 22:22:20 -0700205 _leave("");
David Howells08e0e7c2007-04-26 15:55:03 -0700206}
207
208/*
David Howells260a9802007-04-26 15:59:35 -0700209 * finish off updating the recorded status of a file after a successful
210 * operation completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * - starts callback expiry timer
212 * - adds to server's callback list
213 */
David Howells260a9802007-04-26 15:59:35 -0700214void afs_vnode_finalise_status_update(struct afs_vnode *vnode,
215 struct afs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 struct afs_server *oldserver = NULL;
218
David Howells260a9802007-04-26 15:59:35 -0700219 _enter("%p,%p", vnode, server);
220
221 spin_lock(&vnode->lock);
222 clear_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
223 afs_vnode_note_promise(vnode, server);
224 vnode->update_cnt--;
225 ASSERTCMP(vnode->update_cnt, >=, 0);
226 spin_unlock(&vnode->lock);
227
228 wake_up_all(&vnode->update_waitq);
229 afs_put_server(oldserver);
230 _leave("");
231}
232
233/*
234 * finish off updating the recorded status of a file after an operation failed
235 */
236static void afs_vnode_status_update_failed(struct afs_vnode *vnode, int ret)
237{
David Howells0f300ca2007-05-10 22:22:20 -0700238 _enter("{%x:%u},%d", vnode->fid.vid, vnode->fid.vnode, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 spin_lock(&vnode->lock);
241
David Howells08e0e7c2007-04-26 15:55:03 -0700242 clear_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
David Howells260a9802007-04-26 15:59:35 -0700244 if (ret == -ENOENT) {
David Howells08e0e7c2007-04-26 15:55:03 -0700245 /* the file was deleted on the server */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 _debug("got NOENT from server - marking file deleted");
David Howells08e0e7c2007-04-26 15:55:03 -0700247 afs_vnode_deleted_remotely(vnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249
250 vnode->update_cnt--;
David Howells260a9802007-04-26 15:59:35 -0700251 ASSERTCMP(vnode->update_cnt, >=, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 spin_unlock(&vnode->lock);
253
254 wake_up_all(&vnode->update_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700256}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258/*
259 * fetch file status from the volume
260 * - don't issue a fetch if:
261 * - the changed bit is not set and there's a valid callback
262 * - there are any outstanding ops that will fetch the status
263 * - TODO implement local caching
264 */
David Howells00d3b7a2007-04-26 15:57:07 -0700265int afs_vnode_fetch_status(struct afs_vnode *vnode,
266 struct afs_vnode *auth_vnode, struct key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 struct afs_server *server;
David Howells00d3b7a2007-04-26 15:57:07 -0700269 unsigned long acl_order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 int ret;
271
272 DECLARE_WAITQUEUE(myself, current);
273
David Howells416351f2007-05-09 02:33:45 -0700274 _enter("%s,{%x:%u.%u}",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 vnode->volume->vlocation->vldb.name,
276 vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
277
David Howells08e0e7c2007-04-26 15:55:03 -0700278 if (!test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags) &&
279 vnode->cb_promised) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 _leave(" [unchanged]");
281 return 0;
282 }
283
David Howells08e0e7c2007-04-26 15:55:03 -0700284 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 _leave(" [deleted]");
286 return -ENOENT;
287 }
288
David Howells00d3b7a2007-04-26 15:57:07 -0700289 acl_order = 0;
290 if (auth_vnode)
291 acl_order = auth_vnode->acl_order;
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 spin_lock(&vnode->lock);
294
David Howells08e0e7c2007-04-26 15:55:03 -0700295 if (!test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags) &&
296 vnode->cb_promised) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 spin_unlock(&vnode->lock);
298 _leave(" [unchanged]");
299 return 0;
300 }
301
David Howells260a9802007-04-26 15:59:35 -0700302 ASSERTCMP(vnode->update_cnt, >=, 0);
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (vnode->update_cnt > 0) {
305 /* someone else started a fetch */
David Howells260a9802007-04-26 15:59:35 -0700306 _debug("wait on fetch %d", vnode->update_cnt);
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 set_current_state(TASK_UNINTERRUPTIBLE);
David Howells08e0e7c2007-04-26 15:55:03 -0700309 ASSERT(myself.func != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 add_wait_queue(&vnode->update_waitq, &myself);
311
312 /* wait for the status to be updated */
313 for (;;) {
David Howells08e0e7c2007-04-26 15:55:03 -0700314 if (!test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 break;
David Howells08e0e7c2007-04-26 15:55:03 -0700316 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 break;
318
David Howells08e0e7c2007-04-26 15:55:03 -0700319 /* check to see if it got updated and invalidated all
320 * before we saw it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (vnode->update_cnt == 0) {
322 remove_wait_queue(&vnode->update_waitq,
323 &myself);
324 set_current_state(TASK_RUNNING);
325 goto get_anyway;
326 }
327
328 spin_unlock(&vnode->lock);
329
330 schedule();
331 set_current_state(TASK_UNINTERRUPTIBLE);
332
333 spin_lock(&vnode->lock);
334 }
335
336 remove_wait_queue(&vnode->update_waitq, &myself);
337 spin_unlock(&vnode->lock);
338 set_current_state(TASK_RUNNING);
339
David Howells08e0e7c2007-04-26 15:55:03 -0700340 return test_bit(AFS_VNODE_DELETED, &vnode->flags) ?
341 -ENOENT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343
David Howellsec268152007-04-26 15:49:28 -0700344get_anyway:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 /* okay... we're going to have to initiate the op */
346 vnode->update_cnt++;
347
348 spin_unlock(&vnode->lock);
349
350 /* merge AFS status fetches and clear outstanding callback on this
351 * vnode */
352 do {
353 /* pick a server to query */
David Howells08e0e7c2007-04-26 15:55:03 -0700354 server = afs_volume_pick_fileserver(vnode);
355 if (IS_ERR(server))
David Howells260a9802007-04-26 15:59:35 -0700356 goto no_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
David Howells08e0e7c2007-04-26 15:55:03 -0700358 _debug("USING SERVER: %p{%08x}",
359 server, ntohl(server->addr.s_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
David Howells00d3b7a2007-04-26 15:57:07 -0700361 ret = afs_fs_fetch_file_status(server, key, vnode, NULL,
David Howells08e0e7c2007-04-26 15:55:03 -0700362 &afs_sync_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
David Howells08e0e7c2007-04-26 15:55:03 -0700364 } while (!afs_volume_release_fileserver(vnode, server, ret));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 /* adjust the flags */
David Howells260a9802007-04-26 15:59:35 -0700367 if (ret == 0) {
368 _debug("adjust");
369 if (auth_vnode)
370 afs_cache_permit(vnode, key, acl_order);
371 afs_vnode_finalise_status_update(vnode, server);
372 afs_put_server(server);
373 } else {
374 _debug("failed [%d]", ret);
375 afs_vnode_status_update_failed(vnode, ret);
376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
David Howells260a9802007-04-26 15:59:35 -0700378 ASSERTCMP(vnode->update_cnt, >=, 0);
379
380 _leave(" = %d [cnt %d]", ret, vnode->update_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return ret;
David Howells260a9802007-04-26 15:59:35 -0700382
383no_server:
384 spin_lock(&vnode->lock);
385 vnode->update_cnt--;
386 ASSERTCMP(vnode->update_cnt, >=, 0);
387 spin_unlock(&vnode->lock);
388 _leave(" = %ld [cnt %d]", PTR_ERR(server), vnode->update_cnt);
389 return PTR_ERR(server);
David Howellsec268152007-04-26 15:49:28 -0700390}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392/*
393 * fetch file data from the volume
David Howells260a9802007-04-26 15:59:35 -0700394 * - TODO implement caching
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 */
David Howells00d3b7a2007-04-26 15:57:07 -0700396int afs_vnode_fetch_data(struct afs_vnode *vnode, struct key *key,
397 off_t offset, size_t length, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 struct afs_server *server;
400 int ret;
401
David Howells416351f2007-05-09 02:33:45 -0700402 _enter("%s{%x:%u.%u},%x,,,",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 vnode->volume->vlocation->vldb.name,
404 vnode->fid.vid,
405 vnode->fid.vnode,
David Howells00d3b7a2007-04-26 15:57:07 -0700406 vnode->fid.unique,
407 key_serial(key));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /* this op will fetch the status */
410 spin_lock(&vnode->lock);
411 vnode->update_cnt++;
412 spin_unlock(&vnode->lock);
413
414 /* merge in AFS status fetches and clear outstanding callback on this
415 * vnode */
416 do {
417 /* pick a server to query */
David Howells08e0e7c2007-04-26 15:55:03 -0700418 server = afs_volume_pick_fileserver(vnode);
419 if (IS_ERR(server))
David Howells260a9802007-04-26 15:59:35 -0700420 goto no_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 _debug("USING SERVER: %08x\n", ntohl(server->addr.s_addr));
423
David Howells00d3b7a2007-04-26 15:57:07 -0700424 ret = afs_fs_fetch_data(server, key, vnode, offset, length,
David Howells260a9802007-04-26 15:59:35 -0700425 page, &afs_sync_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
David Howells08e0e7c2007-04-26 15:55:03 -0700427 } while (!afs_volume_release_fileserver(vnode, server, ret));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 /* adjust the flags */
David Howells260a9802007-04-26 15:59:35 -0700430 if (ret == 0) {
431 afs_vnode_finalise_status_update(vnode, server);
432 afs_put_server(server);
433 } else {
434 afs_vnode_status_update_failed(vnode, ret);
435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 _leave(" = %d", ret);
438 return ret;
David Howells260a9802007-04-26 15:59:35 -0700439
440no_server:
441 spin_lock(&vnode->lock);
442 vnode->update_cnt--;
443 ASSERTCMP(vnode->update_cnt, >=, 0);
444 spin_unlock(&vnode->lock);
445 return PTR_ERR(server);
446}
447
448/*
449 * make a file or a directory
450 */
451int afs_vnode_create(struct afs_vnode *vnode, struct key *key,
452 const char *name, umode_t mode, struct afs_fid *newfid,
453 struct afs_file_status *newstatus,
454 struct afs_callback *newcb, struct afs_server **_server)
455{
456 struct afs_server *server;
457 int ret;
458
David Howells416351f2007-05-09 02:33:45 -0700459 _enter("%s{%x:%u.%u},%x,%s,,",
David Howells260a9802007-04-26 15:59:35 -0700460 vnode->volume->vlocation->vldb.name,
461 vnode->fid.vid,
462 vnode->fid.vnode,
463 vnode->fid.unique,
464 key_serial(key),
465 name);
466
467 /* this op will fetch the status on the directory we're creating in */
468 spin_lock(&vnode->lock);
469 vnode->update_cnt++;
470 spin_unlock(&vnode->lock);
471
472 do {
473 /* pick a server to query */
474 server = afs_volume_pick_fileserver(vnode);
475 if (IS_ERR(server))
476 goto no_server;
477
478 _debug("USING SERVER: %08x\n", ntohl(server->addr.s_addr));
479
480 ret = afs_fs_create(server, key, vnode, name, mode, newfid,
481 newstatus, newcb, &afs_sync_call);
482
483 } while (!afs_volume_release_fileserver(vnode, server, ret));
484
485 /* adjust the flags */
486 if (ret == 0) {
487 afs_vnode_finalise_status_update(vnode, server);
488 *_server = server;
489 } else {
490 afs_vnode_status_update_failed(vnode, ret);
491 *_server = NULL;
492 }
493
494 _leave(" = %d [cnt %d]", ret, vnode->update_cnt);
495 return ret;
496
497no_server:
498 spin_lock(&vnode->lock);
499 vnode->update_cnt--;
500 ASSERTCMP(vnode->update_cnt, >=, 0);
501 spin_unlock(&vnode->lock);
502 _leave(" = %ld [cnt %d]", PTR_ERR(server), vnode->update_cnt);
503 return PTR_ERR(server);
504}
505
506/*
507 * remove a file or directory
508 */
509int afs_vnode_remove(struct afs_vnode *vnode, struct key *key, const char *name,
510 bool isdir)
511{
512 struct afs_server *server;
513 int ret;
514
David Howells416351f2007-05-09 02:33:45 -0700515 _enter("%s{%x:%u.%u},%x,%s",
David Howells260a9802007-04-26 15:59:35 -0700516 vnode->volume->vlocation->vldb.name,
517 vnode->fid.vid,
518 vnode->fid.vnode,
519 vnode->fid.unique,
520 key_serial(key),
521 name);
522
523 /* this op will fetch the status on the directory we're removing from */
524 spin_lock(&vnode->lock);
525 vnode->update_cnt++;
526 spin_unlock(&vnode->lock);
527
528 do {
529 /* pick a server to query */
530 server = afs_volume_pick_fileserver(vnode);
531 if (IS_ERR(server))
532 goto no_server;
533
534 _debug("USING SERVER: %08x\n", ntohl(server->addr.s_addr));
535
536 ret = afs_fs_remove(server, key, vnode, name, isdir,
537 &afs_sync_call);
538
539 } while (!afs_volume_release_fileserver(vnode, server, ret));
540
541 /* adjust the flags */
542 if (ret == 0) {
543 afs_vnode_finalise_status_update(vnode, server);
544 afs_put_server(server);
545 } else {
546 afs_vnode_status_update_failed(vnode, ret);
547 }
548
549 _leave(" = %d [cnt %d]", ret, vnode->update_cnt);
550 return ret;
551
552no_server:
553 spin_lock(&vnode->lock);
554 vnode->update_cnt--;
555 ASSERTCMP(vnode->update_cnt, >=, 0);
556 spin_unlock(&vnode->lock);
557 _leave(" = %ld [cnt %d]", PTR_ERR(server), vnode->update_cnt);
558 return PTR_ERR(server);
559}
560
561/*
562 * create a hard link
563 */
564extern int afs_vnode_link(struct afs_vnode *dvnode, struct afs_vnode *vnode,
565 struct key *key, const char *name)
566{
567 struct afs_server *server;
568 int ret;
569
David Howells416351f2007-05-09 02:33:45 -0700570 _enter("%s{%x:%u.%u},%s{%x:%u.%u},%x,%s",
David Howells260a9802007-04-26 15:59:35 -0700571 dvnode->volume->vlocation->vldb.name,
572 dvnode->fid.vid,
573 dvnode->fid.vnode,
574 dvnode->fid.unique,
575 vnode->volume->vlocation->vldb.name,
576 vnode->fid.vid,
577 vnode->fid.vnode,
578 vnode->fid.unique,
579 key_serial(key),
580 name);
581
582 /* this op will fetch the status on the directory we're removing from */
583 spin_lock(&vnode->lock);
584 vnode->update_cnt++;
585 spin_unlock(&vnode->lock);
586 spin_lock(&dvnode->lock);
587 dvnode->update_cnt++;
588 spin_unlock(&dvnode->lock);
589
590 do {
591 /* pick a server to query */
592 server = afs_volume_pick_fileserver(dvnode);
593 if (IS_ERR(server))
594 goto no_server;
595
596 _debug("USING SERVER: %08x\n", ntohl(server->addr.s_addr));
597
598 ret = afs_fs_link(server, key, dvnode, vnode, name,
599 &afs_sync_call);
600
601 } while (!afs_volume_release_fileserver(dvnode, server, ret));
602
603 /* adjust the flags */
604 if (ret == 0) {
605 afs_vnode_finalise_status_update(vnode, server);
606 afs_vnode_finalise_status_update(dvnode, server);
607 afs_put_server(server);
608 } else {
609 afs_vnode_status_update_failed(vnode, ret);
610 afs_vnode_status_update_failed(dvnode, ret);
611 }
612
613 _leave(" = %d [cnt %d]", ret, vnode->update_cnt);
614 return ret;
615
616no_server:
617 spin_lock(&vnode->lock);
618 vnode->update_cnt--;
619 ASSERTCMP(vnode->update_cnt, >=, 0);
620 spin_unlock(&vnode->lock);
621 spin_lock(&dvnode->lock);
622 dvnode->update_cnt--;
623 ASSERTCMP(dvnode->update_cnt, >=, 0);
624 spin_unlock(&dvnode->lock);
625 _leave(" = %ld [cnt %d]", PTR_ERR(server), vnode->update_cnt);
626 return PTR_ERR(server);
627}
628
629/*
630 * create a symbolic link
631 */
632int afs_vnode_symlink(struct afs_vnode *vnode, struct key *key,
633 const char *name, const char *content,
634 struct afs_fid *newfid,
635 struct afs_file_status *newstatus,
636 struct afs_server **_server)
637{
638 struct afs_server *server;
639 int ret;
640
David Howells416351f2007-05-09 02:33:45 -0700641 _enter("%s{%x:%u.%u},%x,%s,%s,,,",
David Howells260a9802007-04-26 15:59:35 -0700642 vnode->volume->vlocation->vldb.name,
643 vnode->fid.vid,
644 vnode->fid.vnode,
645 vnode->fid.unique,
646 key_serial(key),
647 name, content);
648
649 /* this op will fetch the status on the directory we're creating in */
650 spin_lock(&vnode->lock);
651 vnode->update_cnt++;
652 spin_unlock(&vnode->lock);
653
654 do {
655 /* pick a server to query */
656 server = afs_volume_pick_fileserver(vnode);
657 if (IS_ERR(server))
658 goto no_server;
659
660 _debug("USING SERVER: %08x\n", ntohl(server->addr.s_addr));
661
662 ret = afs_fs_symlink(server, key, vnode, name, content,
663 newfid, newstatus, &afs_sync_call);
664
665 } while (!afs_volume_release_fileserver(vnode, server, ret));
666
667 /* adjust the flags */
668 if (ret == 0) {
669 afs_vnode_finalise_status_update(vnode, server);
670 *_server = server;
671 } else {
672 afs_vnode_status_update_failed(vnode, ret);
673 *_server = NULL;
674 }
675
676 _leave(" = %d [cnt %d]", ret, vnode->update_cnt);
677 return ret;
678
679no_server:
680 spin_lock(&vnode->lock);
681 vnode->update_cnt--;
682 ASSERTCMP(vnode->update_cnt, >=, 0);
683 spin_unlock(&vnode->lock);
684 _leave(" = %ld [cnt %d]", PTR_ERR(server), vnode->update_cnt);
685 return PTR_ERR(server);
686}
687
688/*
689 * rename a file
690 */
691int afs_vnode_rename(struct afs_vnode *orig_dvnode,
692 struct afs_vnode *new_dvnode,
693 struct key *key,
694 const char *orig_name,
695 const char *new_name)
696{
697 struct afs_server *server;
698 int ret;
699
David Howells416351f2007-05-09 02:33:45 -0700700 _enter("%s{%x:%u.%u},%s{%u,%u,%u},%x,%s,%s",
David Howells260a9802007-04-26 15:59:35 -0700701 orig_dvnode->volume->vlocation->vldb.name,
702 orig_dvnode->fid.vid,
703 orig_dvnode->fid.vnode,
704 orig_dvnode->fid.unique,
705 new_dvnode->volume->vlocation->vldb.name,
706 new_dvnode->fid.vid,
707 new_dvnode->fid.vnode,
708 new_dvnode->fid.unique,
709 key_serial(key),
710 orig_name,
711 new_name);
712
713 /* this op will fetch the status on both the directories we're dealing
714 * with */
715 spin_lock(&orig_dvnode->lock);
716 orig_dvnode->update_cnt++;
717 spin_unlock(&orig_dvnode->lock);
718 if (new_dvnode != orig_dvnode) {
719 spin_lock(&new_dvnode->lock);
720 new_dvnode->update_cnt++;
721 spin_unlock(&new_dvnode->lock);
722 }
723
724 do {
725 /* pick a server to query */
726 server = afs_volume_pick_fileserver(orig_dvnode);
727 if (IS_ERR(server))
728 goto no_server;
729
730 _debug("USING SERVER: %08x\n", ntohl(server->addr.s_addr));
731
732 ret = afs_fs_rename(server, key, orig_dvnode, orig_name,
733 new_dvnode, new_name, &afs_sync_call);
734
735 } while (!afs_volume_release_fileserver(orig_dvnode, server, ret));
736
737 /* adjust the flags */
738 if (ret == 0) {
739 afs_vnode_finalise_status_update(orig_dvnode, server);
740 if (new_dvnode != orig_dvnode)
741 afs_vnode_finalise_status_update(new_dvnode, server);
742 afs_put_server(server);
743 } else {
744 afs_vnode_status_update_failed(orig_dvnode, ret);
745 if (new_dvnode != orig_dvnode)
746 afs_vnode_status_update_failed(new_dvnode, ret);
747 }
748
749 _leave(" = %d [cnt %d]", ret, orig_dvnode->update_cnt);
750 return ret;
751
752no_server:
753 spin_lock(&orig_dvnode->lock);
754 orig_dvnode->update_cnt--;
755 ASSERTCMP(orig_dvnode->update_cnt, >=, 0);
756 spin_unlock(&orig_dvnode->lock);
757 if (new_dvnode != orig_dvnode) {
758 spin_lock(&new_dvnode->lock);
759 new_dvnode->update_cnt--;
760 ASSERTCMP(new_dvnode->update_cnt, >=, 0);
761 spin_unlock(&new_dvnode->lock);
762 }
763 _leave(" = %ld [cnt %d]", PTR_ERR(server), orig_dvnode->update_cnt);
764 return PTR_ERR(server);
David Howellsec268152007-04-26 15:49:28 -0700765}
David Howells31143d52007-05-09 02:33:46 -0700766
767/*
768 * write to a file
769 */
770int afs_vnode_store_data(struct afs_writeback *wb, pgoff_t first, pgoff_t last,
771 unsigned offset, unsigned to)
772{
773 struct afs_server *server;
774 struct afs_vnode *vnode = wb->vnode;
775 int ret;
776
777 _enter("%s{%x:%u.%u},%x,%lx,%lx,%x,%x",
778 vnode->volume->vlocation->vldb.name,
779 vnode->fid.vid,
780 vnode->fid.vnode,
781 vnode->fid.unique,
782 key_serial(wb->key),
783 first, last, offset, to);
784
785 /* this op will fetch the status */
786 spin_lock(&vnode->lock);
787 vnode->update_cnt++;
788 spin_unlock(&vnode->lock);
789
790 do {
791 /* pick a server to query */
792 server = afs_volume_pick_fileserver(vnode);
793 if (IS_ERR(server))
794 goto no_server;
795
796 _debug("USING SERVER: %08x\n", ntohl(server->addr.s_addr));
797
798 ret = afs_fs_store_data(server, wb, first, last, offset, to,
799 &afs_sync_call);
800
801 } while (!afs_volume_release_fileserver(vnode, server, ret));
802
803 /* adjust the flags */
804 if (ret == 0) {
805 afs_vnode_finalise_status_update(vnode, server);
806 afs_put_server(server);
807 } else {
808 afs_vnode_status_update_failed(vnode, ret);
809 }
810
811 _leave(" = %d", ret);
812 return ret;
813
814no_server:
815 spin_lock(&vnode->lock);
816 vnode->update_cnt--;
817 ASSERTCMP(vnode->update_cnt, >=, 0);
818 spin_unlock(&vnode->lock);
819 return PTR_ERR(server);
820}
821
822/*
823 * set the attributes on a file
824 */
825int afs_vnode_setattr(struct afs_vnode *vnode, struct key *key,
826 struct iattr *attr)
827{
828 struct afs_server *server;
829 int ret;
830
831 _enter("%s{%x:%u.%u},%x",
832 vnode->volume->vlocation->vldb.name,
833 vnode->fid.vid,
834 vnode->fid.vnode,
835 vnode->fid.unique,
836 key_serial(key));
837
838 /* this op will fetch the status */
839 spin_lock(&vnode->lock);
840 vnode->update_cnt++;
841 spin_unlock(&vnode->lock);
842
843 do {
844 /* pick a server to query */
845 server = afs_volume_pick_fileserver(vnode);
846 if (IS_ERR(server))
847 goto no_server;
848
849 _debug("USING SERVER: %08x\n", ntohl(server->addr.s_addr));
850
851 ret = afs_fs_setattr(server, key, vnode, attr, &afs_sync_call);
852
853 } while (!afs_volume_release_fileserver(vnode, server, ret));
854
855 /* adjust the flags */
856 if (ret == 0) {
857 afs_vnode_finalise_status_update(vnode, server);
858 afs_put_server(server);
859 } else {
860 afs_vnode_status_update_failed(vnode, ret);
861 }
862
863 _leave(" = %d", ret);
864 return ret;
865
866no_server:
867 spin_lock(&vnode->lock);
868 vnode->update_cnt--;
869 ASSERTCMP(vnode->update_cnt, >=, 0);
870 spin_unlock(&vnode->lock);
871 return PTR_ERR(server);
872}
David Howells45222b92007-05-10 22:22:20 -0700873
874/*
875 * get the status of a volume
876 */
877int afs_vnode_get_volume_status(struct afs_vnode *vnode, struct key *key,
878 struct afs_volume_status *vs)
879{
880 struct afs_server *server;
881 int ret;
882
883 _enter("%s{%x:%u.%u},%x,",
884 vnode->volume->vlocation->vldb.name,
885 vnode->fid.vid,
886 vnode->fid.vnode,
887 vnode->fid.unique,
888 key_serial(key));
889
890 /* this op will fetch the status */
891 spin_lock(&vnode->lock);
892 vnode->update_cnt++;
893 spin_unlock(&vnode->lock);
894
895 do {
896 /* pick a server to query */
897 server = afs_volume_pick_fileserver(vnode);
898 if (IS_ERR(server))
899 goto no_server;
900
901 _debug("USING SERVER: %08x\n", ntohl(server->addr.s_addr));
902
903 ret = afs_fs_get_volume_status(server, key, vnode, vs, &afs_sync_call);
904
905 } while (!afs_volume_release_fileserver(vnode, server, ret));
906
907 /* adjust the flags */
908 if (ret == 0) {
909 afs_vnode_finalise_status_update(vnode, server);
910 afs_put_server(server);
911 } else {
912 afs_vnode_status_update_failed(vnode, ret);
913 }
914
915 _leave(" = %d", ret);
916 return ret;
917
918no_server:
919 spin_lock(&vnode->lock);
920 vnode->update_cnt--;
921 ASSERTCMP(vnode->update_cnt, >=, 0);
922 spin_unlock(&vnode->lock);
923 return PTR_ERR(server);
924}