blob: 167ca615c2e6108435e958a0c4a1f4aa14347b9c [file] [log] [blame]
David Howells08e0e7c2007-04-26 15:55:03 -07001/* AFS File Server client stubs
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/init.h>
13#include <linux/sched.h>
David Howells08e0e7c2007-04-26 15:55:03 -070014#include <linux/circ_buf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "internal.h"
David Howells08e0e7c2007-04-26 15:55:03 -070016#include "afs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Linus Torvalds1da177e2005-04-16 15:20:36 -070018/*
David Howells08e0e7c2007-04-26 15:55:03 -070019 * decode an AFSFetchStatus block
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
David Howells08e0e7c2007-04-26 15:55:03 -070021static void xdr_decode_AFSFetchStatus(const __be32 **_bp,
22 struct afs_vnode *vnode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
David Howells08e0e7c2007-04-26 15:55:03 -070024 const __be32 *bp = *_bp;
25 umode_t mode;
26 u64 data_version;
27 u32 changed = 0; /* becomes non-zero if ctime-type changes seen */
28
29#define EXTRACT(DST) \
30 do { \
31 u32 x = ntohl(*bp++); \
32 changed |= DST - x; \
33 DST = x; \
34 } while (0)
35
36 vnode->status.if_version = ntohl(*bp++);
37 EXTRACT(vnode->status.type);
38 vnode->status.nlink = ntohl(*bp++);
39 EXTRACT(vnode->status.size);
40 data_version = ntohl(*bp++);
41 EXTRACT(vnode->status.author);
42 EXTRACT(vnode->status.owner);
43 EXTRACT(vnode->status.caller_access); /* call ticket dependent */
44 EXTRACT(vnode->status.anon_access);
45 EXTRACT(vnode->status.mode);
46 vnode->status.parent.vid = vnode->fid.vid;
47 EXTRACT(vnode->status.parent.vnode);
48 EXTRACT(vnode->status.parent.unique);
49 bp++; /* seg size */
50 vnode->status.mtime_client = ntohl(*bp++);
51 vnode->status.mtime_server = ntohl(*bp++);
52 bp++; /* group */
53 bp++; /* sync counter */
54 data_version |= (u64) ntohl(*bp++) << 32;
55 bp++; /* spare2 */
56 bp++; /* spare3 */
57 bp++; /* spare4 */
58 *_bp = bp;
59
60 if (changed) {
61 _debug("vnode changed");
62 set_bit(AFS_VNODE_CHANGED, &vnode->flags);
63 vnode->vfs_inode.i_uid = vnode->status.owner;
64 vnode->vfs_inode.i_size = vnode->status.size;
65 vnode->vfs_inode.i_version = vnode->fid.unique;
66
67 vnode->status.mode &= S_IALLUGO;
68 mode = vnode->vfs_inode.i_mode;
69 mode &= ~S_IALLUGO;
70 mode |= vnode->status.mode;
71 vnode->vfs_inode.i_mode = mode;
72 }
73
74 _debug("vnode time %lx, %lx",
75 vnode->status.mtime_client, vnode->status.mtime_server);
76 vnode->vfs_inode.i_ctime.tv_sec = vnode->status.mtime_server;
77 vnode->vfs_inode.i_mtime = vnode->vfs_inode.i_ctime;
78 vnode->vfs_inode.i_atime = vnode->vfs_inode.i_ctime;
79
80 if (vnode->status.data_version != data_version) {
81 _debug("vnode modified %llx", data_version);
82 vnode->status.data_version = data_version;
83 set_bit(AFS_VNODE_MODIFIED, &vnode->flags);
84 set_bit(AFS_VNODE_ZAP_DATA, &vnode->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
David Howellsec268152007-04-26 15:49:28 -070086}
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/*
David Howells08e0e7c2007-04-26 15:55:03 -070089 * decode an AFSCallBack block
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 */
David Howells08e0e7c2007-04-26 15:55:03 -070091static void xdr_decode_AFSCallBack(const __be32 **_bp, struct afs_vnode *vnode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
David Howells08e0e7c2007-04-26 15:55:03 -070093 const __be32 *bp = *_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
David Howells08e0e7c2007-04-26 15:55:03 -070095 vnode->cb_version = ntohl(*bp++);
96 vnode->cb_expiry = ntohl(*bp++);
97 vnode->cb_type = ntohl(*bp++);
98 vnode->cb_expires = vnode->cb_expiry + get_seconds();
99 *_bp = bp;
David Howellsec268152007-04-26 15:49:28 -0700100}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/*
David Howells08e0e7c2007-04-26 15:55:03 -0700103 * decode an AFSVolSync block
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 */
David Howells08e0e7c2007-04-26 15:55:03 -0700105static void xdr_decode_AFSVolSync(const __be32 **_bp,
106 struct afs_volsync *volsync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
David Howells08e0e7c2007-04-26 15:55:03 -0700108 const __be32 *bp = *_bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
David Howells08e0e7c2007-04-26 15:55:03 -0700110 volsync->creation = ntohl(*bp++);
111 bp++; /* spare2 */
112 bp++; /* spare3 */
113 bp++; /* spare4 */
114 bp++; /* spare5 */
115 bp++; /* spare6 */
116 *_bp = bp;
David Howellsec268152007-04-26 15:49:28 -0700117}
David Howells08e0e7c2007-04-26 15:55:03 -0700118
119/*
120 * deliver reply data to an FS.FetchStatus
121 */
122static int afs_deliver_fs_fetch_status(struct afs_call *call,
123 struct sk_buff *skb, bool last)
124{
125 const __be32 *bp;
126
127 _enter(",,%u", last);
128
129 afs_transfer_reply(call, skb);
130 if (!last)
131 return 0;
132
133 if (call->reply_size != call->reply_max)
134 return -EBADMSG;
135
136 /* unmarshall the reply once we've received all of it */
137 bp = call->buffer;
138 xdr_decode_AFSFetchStatus(&bp, call->reply);
139 xdr_decode_AFSCallBack(&bp, call->reply);
140 if (call->reply2)
141 xdr_decode_AFSVolSync(&bp, call->reply2);
142
143 _leave(" = 0 [done]");
144 return 0;
145}
146
147/*
148 * FS.FetchStatus operation type
149 */
150static const struct afs_call_type afs_RXFSFetchStatus = {
151 .deliver = afs_deliver_fs_fetch_status,
152 .abort_to_error = afs_abort_to_error,
153 .destructor = afs_flat_call_destructor,
154};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/*
157 * fetch the status information for a file
158 */
David Howells08e0e7c2007-04-26 15:55:03 -0700159int afs_fs_fetch_file_status(struct afs_server *server,
160 struct afs_vnode *vnode,
161 struct afs_volsync *volsync,
162 const struct afs_wait_mode *wait_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
David Howells08e0e7c2007-04-26 15:55:03 -0700164 struct afs_call *call;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 __be32 *bp;
166
David Howells08e0e7c2007-04-26 15:55:03 -0700167 _enter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
David Howells08e0e7c2007-04-26 15:55:03 -0700169 call = afs_alloc_flat_call(&afs_RXFSFetchStatus, 16, 120);
170 if (!call)
171 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
David Howells08e0e7c2007-04-26 15:55:03 -0700173 call->reply = vnode;
174 call->reply2 = volsync;
175 call->service_id = FS_SERVICE;
176 call->port = htons(AFS_FS_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 /* marshall the parameters */
David Howells08e0e7c2007-04-26 15:55:03 -0700179 bp = call->request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 bp[0] = htonl(FSFETCHSTATUS);
181 bp[1] = htonl(vnode->fid.vid);
182 bp[2] = htonl(vnode->fid.vnode);
183 bp[3] = htonl(vnode->fid.unique);
184
David Howells08e0e7c2007-04-26 15:55:03 -0700185 return afs_make_call(&server->addr, call, GFP_NOFS, wait_mode);
David Howellsec268152007-04-26 15:49:28 -0700186}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/*
David Howells08e0e7c2007-04-26 15:55:03 -0700189 * deliver reply data to an FS.FetchData
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 */
David Howells08e0e7c2007-04-26 15:55:03 -0700191static int afs_deliver_fs_fetch_data(struct afs_call *call,
192 struct sk_buff *skb, bool last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
David Howells08e0e7c2007-04-26 15:55:03 -0700194 const __be32 *bp;
195 struct page *page;
196 void *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 int ret;
David Howells08e0e7c2007-04-26 15:55:03 -0700198
199 _enter("{%u},{%u},%d", call->unmarshall, skb->len, last);
200
201 switch (call->unmarshall) {
202 case 0:
203 call->offset = 0;
204 call->unmarshall++;
205
206 /* extract the returned data length */
207 case 1:
208 _debug("extract data length");
209 ret = afs_extract_data(call, skb, last, &call->tmp, 4);
210 switch (ret) {
211 case 0: break;
212 case -EAGAIN: return 0;
213 default: return ret;
214 }
215
216 call->count = ntohl(call->tmp);
217 _debug("DATA length: %u", call->count);
218 if (call->count > PAGE_SIZE)
219 return -EBADMSG;
220 call->offset = 0;
221 call->unmarshall++;
222
223 if (call->count < PAGE_SIZE) {
224 buffer = kmap_atomic(call->reply3, KM_USER0);
225 memset(buffer + PAGE_SIZE - call->count, 0,
226 call->count);
227 kunmap_atomic(buffer, KM_USER0);
228 }
229
230 /* extract the returned data */
231 case 2:
232 _debug("extract data");
233 page = call->reply3;
234 buffer = kmap_atomic(page, KM_USER0);
235 ret = afs_extract_data(call, skb, last, buffer, call->count);
236 kunmap_atomic(buffer, KM_USER0);
237 switch (ret) {
238 case 0: break;
239 case -EAGAIN: return 0;
240 default: return ret;
241 }
242
243 call->offset = 0;
244 call->unmarshall++;
245
246 /* extract the metadata */
247 case 3:
248 ret = afs_extract_data(call, skb, last, call->buffer, 120);
249 switch (ret) {
250 case 0: break;
251 case -EAGAIN: return 0;
252 default: return ret;
253 }
254
255 bp = call->buffer;
256 xdr_decode_AFSFetchStatus(&bp, call->reply);
257 xdr_decode_AFSCallBack(&bp, call->reply);
258 if (call->reply2)
259 xdr_decode_AFSVolSync(&bp, call->reply2);
260
261 call->offset = 0;
262 call->unmarshall++;
263
264 case 4:
265 _debug("trailer");
266 if (skb->len != 0)
267 return -EBADMSG;
268 break;
269 }
270
271 if (!last)
272 return 0;
273
274 _leave(" = 0 [done]");
275 return 0;
276}
277
278/*
279 * FS.FetchData operation type
280 */
281static const struct afs_call_type afs_RXFSFetchData = {
282 .deliver = afs_deliver_fs_fetch_data,
283 .abort_to_error = afs_abort_to_error,
284 .destructor = afs_flat_call_destructor,
285};
286
287/*
288 * fetch data from a file
289 */
290int afs_fs_fetch_data(struct afs_server *server,
291 struct afs_vnode *vnode,
292 off_t offset, size_t length,
293 struct page *buffer,
294 struct afs_volsync *volsync,
295 const struct afs_wait_mode *wait_mode)
296{
297 struct afs_call *call;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 __be32 *bp;
299
David Howells08e0e7c2007-04-26 15:55:03 -0700300 _enter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
David Howells08e0e7c2007-04-26 15:55:03 -0700302 call = afs_alloc_flat_call(&afs_RXFSFetchData, 24, 120);
303 if (!call)
304 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
David Howells08e0e7c2007-04-26 15:55:03 -0700306 call->reply = vnode;
307 call->reply2 = volsync;
308 call->reply3 = buffer;
309 call->service_id = FS_SERVICE;
310 call->port = htons(AFS_FS_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 /* marshall the parameters */
David Howells08e0e7c2007-04-26 15:55:03 -0700313 bp = call->request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 bp[0] = htonl(FSFETCHDATA);
David Howells08e0e7c2007-04-26 15:55:03 -0700315 bp[1] = htonl(vnode->fid.vid);
316 bp[2] = htonl(vnode->fid.vnode);
317 bp[3] = htonl(vnode->fid.unique);
318 bp[4] = htonl(offset);
319 bp[5] = htonl(length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
David Howells08e0e7c2007-04-26 15:55:03 -0700321 return afs_make_call(&server->addr, call, GFP_NOFS, wait_mode);
David Howellsec268152007-04-26 15:49:28 -0700322}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324/*
David Howells08e0e7c2007-04-26 15:55:03 -0700325 * deliver reply data to an FS.GiveUpCallBacks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 */
David Howells08e0e7c2007-04-26 15:55:03 -0700327static int afs_deliver_fs_give_up_callbacks(struct afs_call *call,
328 struct sk_buff *skb, bool last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
David Howells08e0e7c2007-04-26 15:55:03 -0700330 _enter(",{%u},%d", skb->len, last);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
David Howells08e0e7c2007-04-26 15:55:03 -0700332 if (skb->len > 0)
333 return -EBADMSG; /* shouldn't be any reply data */
334 return 0;
335}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
David Howells08e0e7c2007-04-26 15:55:03 -0700337/*
338 * FS.GiveUpCallBacks operation type
339 */
340static const struct afs_call_type afs_RXFSGiveUpCallBacks = {
341 .deliver = afs_deliver_fs_give_up_callbacks,
342 .abort_to_error = afs_abort_to_error,
343 .destructor = afs_flat_call_destructor,
344};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
David Howells08e0e7c2007-04-26 15:55:03 -0700346/*
347 * give up a set of callbacks
348 * - the callbacks are held in the server->cb_break ring
349 */
350int afs_fs_give_up_callbacks(struct afs_server *server,
351 const struct afs_wait_mode *wait_mode)
352{
353 struct afs_call *call;
354 size_t ncallbacks;
355 __be32 *bp, *tp;
356 int loop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
David Howells08e0e7c2007-04-26 15:55:03 -0700358 ncallbacks = CIRC_CNT(server->cb_break_head, server->cb_break_tail,
359 ARRAY_SIZE(server->cb_break));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
David Howells08e0e7c2007-04-26 15:55:03 -0700361 _enter("{%zu},", ncallbacks);
362
363 if (ncallbacks == 0)
364 return 0;
365 if (ncallbacks > AFSCBMAX)
366 ncallbacks = AFSCBMAX;
367
368 _debug("break %zu callbacks", ncallbacks);
369
370 call = afs_alloc_flat_call(&afs_RXFSGiveUpCallBacks,
371 12 + ncallbacks * 6 * 4, 0);
372 if (!call)
373 return -ENOMEM;
374
375 call->service_id = FS_SERVICE;
376 call->port = htons(AFS_FS_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 /* marshall the parameters */
David Howells08e0e7c2007-04-26 15:55:03 -0700379 bp = call->request;
380 tp = bp + 2 + ncallbacks * 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 *bp++ = htonl(FSGIVEUPCALLBACKS);
David Howells08e0e7c2007-04-26 15:55:03 -0700382 *bp++ = htonl(ncallbacks);
383 *tp++ = htonl(ncallbacks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
David Howells08e0e7c2007-04-26 15:55:03 -0700385 atomic_sub(ncallbacks, &server->cb_break_n);
386 for (loop = ncallbacks; loop > 0; loop--) {
387 struct afs_callback *cb =
388 &server->cb_break[server->cb_break_tail];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
David Howells08e0e7c2007-04-26 15:55:03 -0700390 *bp++ = htonl(cb->fid.vid);
391 *bp++ = htonl(cb->fid.vnode);
392 *bp++ = htonl(cb->fid.unique);
393 *tp++ = htonl(cb->version);
394 *tp++ = htonl(cb->expiry);
395 *tp++ = htonl(cb->type);
396 smp_mb();
397 server->cb_break_tail =
398 (server->cb_break_tail + 1) &
399 (ARRAY_SIZE(server->cb_break) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401
David Howells08e0e7c2007-04-26 15:55:03 -0700402 ASSERT(ncallbacks > 0);
403 wake_up_nr(&server->cb_break_waitq, ncallbacks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
David Howells08e0e7c2007-04-26 15:55:03 -0700405 return afs_make_call(&server->addr, call, GFP_NOFS, wait_mode);
David Howellsec268152007-04-26 15:49:28 -0700406}