]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/afs/fsclient.c
afs: implement acl setting
[linux.git] / fs / afs / fsclient.c
1 /* AFS File Server client stubs
2  *
3  * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
4  * 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/slab.h>
14 #include <linux/sched.h>
15 #include <linux/circ_buf.h>
16 #include <linux/iversion.h>
17 #include "internal.h"
18 #include "afs_fs.h"
19 #include "xdr_fs.h"
20 #include "protocol_yfs.h"
21
22 static const struct afs_fid afs_zero_fid;
23
24 static inline void afs_use_fs_server(struct afs_call *call, struct afs_cb_interest *cbi)
25 {
26         call->cbi = afs_get_cb_interest(cbi);
27 }
28
29 /*
30  * decode an AFSFid block
31  */
32 static void xdr_decode_AFSFid(const __be32 **_bp, struct afs_fid *fid)
33 {
34         const __be32 *bp = *_bp;
35
36         fid->vid                = ntohl(*bp++);
37         fid->vnode              = ntohl(*bp++);
38         fid->unique             = ntohl(*bp++);
39         *_bp = bp;
40 }
41
42 /*
43  * Dump a bad file status record.
44  */
45 static void xdr_dump_bad(const __be32 *bp)
46 {
47         __be32 x[4];
48         int i;
49
50         pr_notice("AFS XDR: Bad status record\n");
51         for (i = 0; i < 5 * 4 * 4; i += 16) {
52                 memcpy(x, bp, 16);
53                 bp += 4;
54                 pr_notice("%03x: %08x %08x %08x %08x\n",
55                           i, ntohl(x[0]), ntohl(x[1]), ntohl(x[2]), ntohl(x[3]));
56         }
57
58         memcpy(x, bp, 4);
59         pr_notice("0x50: %08x\n", ntohl(x[0]));
60 }
61
62 /*
63  * Update the core inode struct from a returned status record.
64  */
65 void afs_update_inode_from_status(struct afs_vnode *vnode,
66                                   struct afs_file_status *status,
67                                   const afs_dataversion_t *expected_version,
68                                   u8 flags)
69 {
70         struct timespec64 t;
71         umode_t mode;
72
73         t = status->mtime_client;
74         vnode->vfs_inode.i_ctime = t;
75         vnode->vfs_inode.i_mtime = t;
76         vnode->vfs_inode.i_atime = t;
77
78         if (flags & (AFS_VNODE_META_CHANGED | AFS_VNODE_NOT_YET_SET)) {
79                 vnode->vfs_inode.i_uid = make_kuid(&init_user_ns, status->owner);
80                 vnode->vfs_inode.i_gid = make_kgid(&init_user_ns, status->group);
81                 set_nlink(&vnode->vfs_inode, status->nlink);
82
83                 mode = vnode->vfs_inode.i_mode;
84                 mode &= ~S_IALLUGO;
85                 mode |= status->mode;
86                 barrier();
87                 vnode->vfs_inode.i_mode = mode;
88         }
89
90         if (!(flags & AFS_VNODE_NOT_YET_SET)) {
91                 if (expected_version &&
92                     *expected_version != status->data_version) {
93                         _debug("vnode modified %llx on {%llx:%llu} [exp %llx]",
94                                (unsigned long long) status->data_version,
95                                vnode->fid.vid, vnode->fid.vnode,
96                                (unsigned long long) *expected_version);
97                         vnode->invalid_before = status->data_version;
98                         if (vnode->status.type == AFS_FTYPE_DIR) {
99                                 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags))
100                                         afs_stat_v(vnode, n_inval);
101                         } else {
102                                 set_bit(AFS_VNODE_ZAP_DATA, &vnode->flags);
103                         }
104                 } else if (vnode->status.type == AFS_FTYPE_DIR) {
105                         /* Expected directory change is handled elsewhere so
106                          * that we can locally edit the directory and save on a
107                          * download.
108                          */
109                         if (test_bit(AFS_VNODE_DIR_VALID, &vnode->flags))
110                                 flags &= ~AFS_VNODE_DATA_CHANGED;
111                 }
112         }
113
114         if (flags & (AFS_VNODE_DATA_CHANGED | AFS_VNODE_NOT_YET_SET)) {
115                 inode_set_iversion_raw(&vnode->vfs_inode, status->data_version);
116                 i_size_write(&vnode->vfs_inode, status->size);
117         }
118 }
119
120 /*
121  * decode an AFSFetchStatus block
122  */
123 static int xdr_decode_AFSFetchStatus(struct afs_call *call,
124                                      const __be32 **_bp,
125                                      struct afs_file_status *status,
126                                      struct afs_vnode *vnode,
127                                      const afs_dataversion_t *expected_version,
128                                      struct afs_read *read_req)
129 {
130         const struct afs_xdr_AFSFetchStatus *xdr = (const void *)*_bp;
131         bool inline_error = (call->operation_ID == afs_FS_InlineBulkStatus);
132         u64 data_version, size;
133         u32 type, abort_code;
134         u8 flags = 0;
135
136         abort_code = ntohl(xdr->abort_code);
137
138         if (xdr->if_version != htonl(AFS_FSTATUS_VERSION)) {
139                 if (xdr->if_version == htonl(0) &&
140                     abort_code != 0 &&
141                     inline_error) {
142                         /* The OpenAFS fileserver has a bug in FS.InlineBulkStatus
143                          * whereby it doesn't set the interface version in the error
144                          * case.
145                          */
146                         status->abort_code = abort_code;
147                         return 0;
148                 }
149
150                 pr_warn("Unknown AFSFetchStatus version %u\n", ntohl(xdr->if_version));
151                 goto bad;
152         }
153
154         if (abort_code != 0 && inline_error) {
155                 status->abort_code = abort_code;
156                 return 0;
157         }
158
159         type = ntohl(xdr->type);
160         switch (type) {
161         case AFS_FTYPE_FILE:
162         case AFS_FTYPE_DIR:
163         case AFS_FTYPE_SYMLINK:
164                 if (type != status->type &&
165                     vnode &&
166                     !test_bit(AFS_VNODE_UNSET, &vnode->flags)) {
167                         pr_warning("Vnode %llx:%llx:%x changed type %u to %u\n",
168                                    vnode->fid.vid,
169                                    vnode->fid.vnode,
170                                    vnode->fid.unique,
171                                    status->type, type);
172                         goto bad;
173                 }
174                 status->type = type;
175                 break;
176         default:
177                 goto bad;
178         }
179
180 #define EXTRACT_M(FIELD)                                        \
181         do {                                                    \
182                 u32 x = ntohl(xdr->FIELD);                      \
183                 if (status->FIELD != x) {                       \
184                         flags |= AFS_VNODE_META_CHANGED;        \
185                         status->FIELD = x;                      \
186                 }                                               \
187         } while (0)
188
189         EXTRACT_M(nlink);
190         EXTRACT_M(author);
191         EXTRACT_M(owner);
192         EXTRACT_M(caller_access); /* call ticket dependent */
193         EXTRACT_M(anon_access);
194         EXTRACT_M(mode);
195         EXTRACT_M(group);
196
197         status->mtime_client.tv_sec = ntohl(xdr->mtime_client);
198         status->mtime_client.tv_nsec = 0;
199         status->mtime_server.tv_sec = ntohl(xdr->mtime_server);
200         status->mtime_server.tv_nsec = 0;
201         status->lock_count   = ntohl(xdr->lock_count);
202
203         size  = (u64)ntohl(xdr->size_lo);
204         size |= (u64)ntohl(xdr->size_hi) << 32;
205         status->size = size;
206
207         data_version  = (u64)ntohl(xdr->data_version_lo);
208         data_version |= (u64)ntohl(xdr->data_version_hi) << 32;
209         if (data_version != status->data_version) {
210                 status->data_version = data_version;
211                 flags |= AFS_VNODE_DATA_CHANGED;
212         }
213
214         if (read_req) {
215                 read_req->data_version = data_version;
216                 read_req->file_size = size;
217         }
218
219         *_bp = (const void *)*_bp + sizeof(*xdr);
220
221         if (vnode) {
222                 if (test_bit(AFS_VNODE_UNSET, &vnode->flags))
223                         flags |= AFS_VNODE_NOT_YET_SET;
224                 afs_update_inode_from_status(vnode, status, expected_version,
225                                              flags);
226         }
227
228         return 0;
229
230 bad:
231         xdr_dump_bad(*_bp);
232         return afs_protocol_error(call, -EBADMSG, afs_eproto_bad_status);
233 }
234
235 /*
236  * Decode the file status.  We need to lock the target vnode if we're going to
237  * update its status so that stat() sees the attributes update atomically.
238  */
239 static int afs_decode_status(struct afs_call *call,
240                              const __be32 **_bp,
241                              struct afs_file_status *status,
242                              struct afs_vnode *vnode,
243                              const afs_dataversion_t *expected_version,
244                              struct afs_read *read_req)
245 {
246         int ret;
247
248         if (!vnode)
249                 return xdr_decode_AFSFetchStatus(call, _bp, status, vnode,
250                                                  expected_version, read_req);
251
252         write_seqlock(&vnode->cb_lock);
253         ret = xdr_decode_AFSFetchStatus(call, _bp, status, vnode,
254                                         expected_version, read_req);
255         write_sequnlock(&vnode->cb_lock);
256         return ret;
257 }
258
259 /*
260  * decode an AFSCallBack block
261  */
262 static void xdr_decode_AFSCallBack(struct afs_call *call,
263                                    struct afs_vnode *vnode,
264                                    const __be32 **_bp)
265 {
266         struct afs_cb_interest *old, *cbi = call->cbi;
267         const __be32 *bp = *_bp;
268         u32 cb_expiry;
269
270         write_seqlock(&vnode->cb_lock);
271
272         if (!afs_cb_is_broken(call->cb_break, vnode, cbi)) {
273                 vnode->cb_version       = ntohl(*bp++);
274                 cb_expiry               = ntohl(*bp++);
275                 vnode->cb_type          = ntohl(*bp++);
276                 vnode->cb_expires_at    = cb_expiry + ktime_get_real_seconds();
277                 old = vnode->cb_interest;
278                 if (old != call->cbi) {
279                         vnode->cb_interest = cbi;
280                         cbi = old;
281                 }
282                 set_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
283         } else {
284                 bp += 3;
285         }
286
287         write_sequnlock(&vnode->cb_lock);
288         call->cbi = cbi;
289         *_bp = bp;
290 }
291
292 static ktime_t xdr_decode_expiry(struct afs_call *call, u32 expiry)
293 {
294         return ktime_add_ns(call->reply_time, expiry * NSEC_PER_SEC);
295 }
296
297 static void xdr_decode_AFSCallBack_raw(struct afs_call *call,
298                                        const __be32 **_bp,
299                                        struct afs_callback *cb)
300 {
301         const __be32 *bp = *_bp;
302
303         cb->version     = ntohl(*bp++);
304         cb->expires_at  = xdr_decode_expiry(call, ntohl(*bp++));
305         cb->type        = ntohl(*bp++);
306         *_bp = bp;
307 }
308
309 /*
310  * decode an AFSVolSync block
311  */
312 static void xdr_decode_AFSVolSync(const __be32 **_bp,
313                                   struct afs_volsync *volsync)
314 {
315         const __be32 *bp = *_bp;
316         u32 creation;
317
318         creation = ntohl(*bp++);
319         bp++; /* spare2 */
320         bp++; /* spare3 */
321         bp++; /* spare4 */
322         bp++; /* spare5 */
323         bp++; /* spare6 */
324         *_bp = bp;
325
326         if (volsync)
327                 volsync->creation = creation;
328 }
329
330 /*
331  * encode the requested attributes into an AFSStoreStatus block
332  */
333 static void xdr_encode_AFS_StoreStatus(__be32 **_bp, struct iattr *attr)
334 {
335         __be32 *bp = *_bp;
336         u32 mask = 0, mtime = 0, owner = 0, group = 0, mode = 0;
337
338         mask = 0;
339         if (attr->ia_valid & ATTR_MTIME) {
340                 mask |= AFS_SET_MTIME;
341                 mtime = attr->ia_mtime.tv_sec;
342         }
343
344         if (attr->ia_valid & ATTR_UID) {
345                 mask |= AFS_SET_OWNER;
346                 owner = from_kuid(&init_user_ns, attr->ia_uid);
347         }
348
349         if (attr->ia_valid & ATTR_GID) {
350                 mask |= AFS_SET_GROUP;
351                 group = from_kgid(&init_user_ns, attr->ia_gid);
352         }
353
354         if (attr->ia_valid & ATTR_MODE) {
355                 mask |= AFS_SET_MODE;
356                 mode = attr->ia_mode & S_IALLUGO;
357         }
358
359         *bp++ = htonl(mask);
360         *bp++ = htonl(mtime);
361         *bp++ = htonl(owner);
362         *bp++ = htonl(group);
363         *bp++ = htonl(mode);
364         *bp++ = 0;              /* segment size */
365         *_bp = bp;
366 }
367
368 /*
369  * decode an AFSFetchVolumeStatus block
370  */
371 static void xdr_decode_AFSFetchVolumeStatus(const __be32 **_bp,
372                                             struct afs_volume_status *vs)
373 {
374         const __be32 *bp = *_bp;
375
376         vs->vid                 = ntohl(*bp++);
377         vs->parent_id           = ntohl(*bp++);
378         vs->online              = ntohl(*bp++);
379         vs->in_service          = ntohl(*bp++);
380         vs->blessed             = ntohl(*bp++);
381         vs->needs_salvage       = ntohl(*bp++);
382         vs->type                = ntohl(*bp++);
383         vs->min_quota           = ntohl(*bp++);
384         vs->max_quota           = ntohl(*bp++);
385         vs->blocks_in_use       = ntohl(*bp++);
386         vs->part_blocks_avail   = ntohl(*bp++);
387         vs->part_max_blocks     = ntohl(*bp++);
388         vs->vol_copy_date       = 0;
389         vs->vol_backup_date     = 0;
390         *_bp = bp;
391 }
392
393 /*
394  * deliver reply data to an FS.FetchStatus
395  */
396 static int afs_deliver_fs_fetch_status_vnode(struct afs_call *call)
397 {
398         struct afs_vnode *vnode = call->reply[0];
399         const __be32 *bp;
400         int ret;
401
402         ret = afs_transfer_reply(call);
403         if (ret < 0)
404                 return ret;
405
406         _enter("{%llx:%llu}", vnode->fid.vid, vnode->fid.vnode);
407
408         /* unmarshall the reply once we've received all of it */
409         bp = call->buffer;
410         ret = afs_decode_status(call, &bp, &vnode->status, vnode,
411                                 &call->expected_version, NULL);
412         if (ret < 0)
413                 return ret;
414         xdr_decode_AFSCallBack(call, vnode, &bp);
415         xdr_decode_AFSVolSync(&bp, call->reply[1]);
416
417         _leave(" = 0 [done]");
418         return 0;
419 }
420
421 /*
422  * FS.FetchStatus operation type
423  */
424 static const struct afs_call_type afs_RXFSFetchStatus_vnode = {
425         .name           = "FS.FetchStatus(vnode)",
426         .op             = afs_FS_FetchStatus,
427         .deliver        = afs_deliver_fs_fetch_status_vnode,
428         .destructor     = afs_flat_call_destructor,
429 };
430
431 /*
432  * fetch the status information for a file
433  */
434 int afs_fs_fetch_file_status(struct afs_fs_cursor *fc, struct afs_volsync *volsync,
435                              bool new_inode)
436 {
437         struct afs_vnode *vnode = fc->vnode;
438         struct afs_call *call;
439         struct afs_net *net = afs_v2net(vnode);
440         __be32 *bp;
441
442         if (test_bit(AFS_SERVER_FL_IS_YFS, &fc->cbi->server->flags))
443                 return yfs_fs_fetch_file_status(fc, volsync, new_inode);
444
445         _enter(",%x,{%llx:%llu},,",
446                key_serial(fc->key), vnode->fid.vid, vnode->fid.vnode);
447
448         call = afs_alloc_flat_call(net, &afs_RXFSFetchStatus_vnode,
449                                    16, (21 + 3 + 6) * 4);
450         if (!call) {
451                 fc->ac.error = -ENOMEM;
452                 return -ENOMEM;
453         }
454
455         call->key = fc->key;
456         call->reply[0] = vnode;
457         call->reply[1] = volsync;
458         call->expected_version = new_inode ? 1 : vnode->status.data_version;
459         call->want_reply_time = true;
460
461         /* marshall the parameters */
462         bp = call->request;
463         bp[0] = htonl(FSFETCHSTATUS);
464         bp[1] = htonl(vnode->fid.vid);
465         bp[2] = htonl(vnode->fid.vnode);
466         bp[3] = htonl(vnode->fid.unique);
467
468         call->cb_break = fc->cb_break;
469         afs_use_fs_server(call, fc->cbi);
470         trace_afs_make_fs_call(call, &vnode->fid);
471
472         afs_make_call(&fc->ac, call, GFP_NOFS);
473         return afs_wait_for_call_to_complete(call, &fc->ac);
474 }
475
476 /*
477  * deliver reply data to an FS.FetchData
478  */
479 static int afs_deliver_fs_fetch_data(struct afs_call *call)
480 {
481         struct afs_vnode *vnode = call->reply[0];
482         struct afs_read *req = call->reply[2];
483         const __be32 *bp;
484         unsigned int size;
485         int ret;
486
487         _enter("{%u,%zu/%llu}",
488                call->unmarshall, iov_iter_count(&call->iter), req->actual_len);
489
490         switch (call->unmarshall) {
491         case 0:
492                 req->actual_len = 0;
493                 req->index = 0;
494                 req->offset = req->pos & (PAGE_SIZE - 1);
495                 call->unmarshall++;
496                 if (call->operation_ID == FSFETCHDATA64) {
497                         afs_extract_to_tmp64(call);
498                 } else {
499                         call->tmp_u = htonl(0);
500                         afs_extract_to_tmp(call);
501                 }
502
503                 /* extract the returned data length */
504         case 1:
505                 _debug("extract data length");
506                 ret = afs_extract_data(call, true);
507                 if (ret < 0)
508                         return ret;
509
510                 req->actual_len = be64_to_cpu(call->tmp64);
511                 _debug("DATA length: %llu", req->actual_len);
512                 req->remain = min(req->len, req->actual_len);
513                 if (req->remain == 0)
514                         goto no_more_data;
515
516                 call->unmarshall++;
517
518         begin_page:
519                 ASSERTCMP(req->index, <, req->nr_pages);
520                 if (req->remain > PAGE_SIZE - req->offset)
521                         size = PAGE_SIZE - req->offset;
522                 else
523                         size = req->remain;
524                 call->bvec[0].bv_len = size;
525                 call->bvec[0].bv_offset = req->offset;
526                 call->bvec[0].bv_page = req->pages[req->index];
527                 iov_iter_bvec(&call->iter, READ, call->bvec, 1, size);
528                 ASSERTCMP(size, <=, PAGE_SIZE);
529
530                 /* extract the returned data */
531         case 2:
532                 _debug("extract data %zu/%llu",
533                        iov_iter_count(&call->iter), req->remain);
534
535                 ret = afs_extract_data(call, true);
536                 if (ret < 0)
537                         return ret;
538                 req->remain -= call->bvec[0].bv_len;
539                 req->offset += call->bvec[0].bv_len;
540                 ASSERTCMP(req->offset, <=, PAGE_SIZE);
541                 if (req->offset == PAGE_SIZE) {
542                         req->offset = 0;
543                         if (req->page_done)
544                                 req->page_done(call, req);
545                         req->index++;
546                         if (req->remain > 0)
547                                 goto begin_page;
548                 }
549
550                 ASSERTCMP(req->remain, ==, 0);
551                 if (req->actual_len <= req->len)
552                         goto no_more_data;
553
554                 /* Discard any excess data the server gave us */
555                 iov_iter_discard(&call->iter, READ, req->actual_len - req->len);
556                 call->unmarshall = 3;
557         case 3:
558                 _debug("extract discard %zu/%llu",
559                        iov_iter_count(&call->iter), req->actual_len - req->len);
560
561                 ret = afs_extract_data(call, true);
562                 if (ret < 0)
563                         return ret;
564
565         no_more_data:
566                 call->unmarshall = 4;
567                 afs_extract_to_buf(call, (21 + 3 + 6) * 4);
568
569                 /* extract the metadata */
570         case 4:
571                 ret = afs_extract_data(call, false);
572                 if (ret < 0)
573                         return ret;
574
575                 bp = call->buffer;
576                 ret = afs_decode_status(call, &bp, &vnode->status, vnode,
577                                         &vnode->status.data_version, req);
578                 if (ret < 0)
579                         return ret;
580                 xdr_decode_AFSCallBack(call, vnode, &bp);
581                 xdr_decode_AFSVolSync(&bp, call->reply[1]);
582
583                 call->unmarshall++;
584
585         case 5:
586                 break;
587         }
588
589         for (; req->index < req->nr_pages; req->index++) {
590                 if (req->offset < PAGE_SIZE)
591                         zero_user_segment(req->pages[req->index],
592                                           req->offset, PAGE_SIZE);
593                 if (req->page_done)
594                         req->page_done(call, req);
595                 req->offset = 0;
596         }
597
598         _leave(" = 0 [done]");
599         return 0;
600 }
601
602 static void afs_fetch_data_destructor(struct afs_call *call)
603 {
604         struct afs_read *req = call->reply[2];
605
606         afs_put_read(req);
607         afs_flat_call_destructor(call);
608 }
609
610 /*
611  * FS.FetchData operation type
612  */
613 static const struct afs_call_type afs_RXFSFetchData = {
614         .name           = "FS.FetchData",
615         .op             = afs_FS_FetchData,
616         .deliver        = afs_deliver_fs_fetch_data,
617         .destructor     = afs_fetch_data_destructor,
618 };
619
620 static const struct afs_call_type afs_RXFSFetchData64 = {
621         .name           = "FS.FetchData64",
622         .op             = afs_FS_FetchData64,
623         .deliver        = afs_deliver_fs_fetch_data,
624         .destructor     = afs_fetch_data_destructor,
625 };
626
627 /*
628  * fetch data from a very large file
629  */
630 static int afs_fs_fetch_data64(struct afs_fs_cursor *fc, struct afs_read *req)
631 {
632         struct afs_vnode *vnode = fc->vnode;
633         struct afs_call *call;
634         struct afs_net *net = afs_v2net(vnode);
635         __be32 *bp;
636
637         _enter("");
638
639         call = afs_alloc_flat_call(net, &afs_RXFSFetchData64, 32, (21 + 3 + 6) * 4);
640         if (!call)
641                 return -ENOMEM;
642
643         call->key = fc->key;
644         call->reply[0] = vnode;
645         call->reply[1] = NULL; /* volsync */
646         call->reply[2] = req;
647         call->expected_version = vnode->status.data_version;
648         call->want_reply_time = true;
649
650         /* marshall the parameters */
651         bp = call->request;
652         bp[0] = htonl(FSFETCHDATA64);
653         bp[1] = htonl(vnode->fid.vid);
654         bp[2] = htonl(vnode->fid.vnode);
655         bp[3] = htonl(vnode->fid.unique);
656         bp[4] = htonl(upper_32_bits(req->pos));
657         bp[5] = htonl(lower_32_bits(req->pos));
658         bp[6] = 0;
659         bp[7] = htonl(lower_32_bits(req->len));
660
661         refcount_inc(&req->usage);
662         call->cb_break = fc->cb_break;
663         afs_use_fs_server(call, fc->cbi);
664         trace_afs_make_fs_call(call, &vnode->fid);
665         afs_make_call(&fc->ac, call, GFP_NOFS);
666         return afs_wait_for_call_to_complete(call, &fc->ac);
667 }
668
669 /*
670  * fetch data from a file
671  */
672 int afs_fs_fetch_data(struct afs_fs_cursor *fc, struct afs_read *req)
673 {
674         struct afs_vnode *vnode = fc->vnode;
675         struct afs_call *call;
676         struct afs_net *net = afs_v2net(vnode);
677         __be32 *bp;
678
679         if (test_bit(AFS_SERVER_FL_IS_YFS, &fc->cbi->server->flags))
680                 return yfs_fs_fetch_data(fc, req);
681
682         if (upper_32_bits(req->pos) ||
683             upper_32_bits(req->len) ||
684             upper_32_bits(req->pos + req->len))
685                 return afs_fs_fetch_data64(fc, req);
686
687         _enter("");
688
689         call = afs_alloc_flat_call(net, &afs_RXFSFetchData, 24, (21 + 3 + 6) * 4);
690         if (!call)
691                 return -ENOMEM;
692
693         call->key = fc->key;
694         call->reply[0] = vnode;
695         call->reply[1] = NULL; /* volsync */
696         call->reply[2] = req;
697         call->expected_version = vnode->status.data_version;
698         call->want_reply_time = true;
699
700         /* marshall the parameters */
701         bp = call->request;
702         bp[0] = htonl(FSFETCHDATA);
703         bp[1] = htonl(vnode->fid.vid);
704         bp[2] = htonl(vnode->fid.vnode);
705         bp[3] = htonl(vnode->fid.unique);
706         bp[4] = htonl(lower_32_bits(req->pos));
707         bp[5] = htonl(lower_32_bits(req->len));
708
709         refcount_inc(&req->usage);
710         call->cb_break = fc->cb_break;
711         afs_use_fs_server(call, fc->cbi);
712         trace_afs_make_fs_call(call, &vnode->fid);
713         afs_make_call(&fc->ac, call, GFP_NOFS);
714         return afs_wait_for_call_to_complete(call, &fc->ac);
715 }
716
717 /*
718  * deliver reply data to an FS.CreateFile or an FS.MakeDir
719  */
720 static int afs_deliver_fs_create_vnode(struct afs_call *call)
721 {
722         struct afs_vnode *vnode = call->reply[0];
723         const __be32 *bp;
724         int ret;
725
726         _enter("{%u}", call->unmarshall);
727
728         ret = afs_transfer_reply(call);
729         if (ret < 0)
730                 return ret;
731
732         /* unmarshall the reply once we've received all of it */
733         bp = call->buffer;
734         xdr_decode_AFSFid(&bp, call->reply[1]);
735         ret = afs_decode_status(call, &bp, call->reply[2], NULL, NULL, NULL);
736         if (ret < 0)
737                 return ret;
738         ret = afs_decode_status(call, &bp, &vnode->status, vnode,
739                                 &call->expected_version, NULL);
740         if (ret < 0)
741                 return ret;
742         xdr_decode_AFSCallBack_raw(call, &bp, call->reply[3]);
743         /* xdr_decode_AFSVolSync(&bp, call->reply[X]); */
744
745         _leave(" = 0 [done]");
746         return 0;
747 }
748
749 /*
750  * FS.CreateFile and FS.MakeDir operation type
751  */
752 static const struct afs_call_type afs_RXFSCreateFile = {
753         .name           = "FS.CreateFile",
754         .op             = afs_FS_CreateFile,
755         .deliver        = afs_deliver_fs_create_vnode,
756         .destructor     = afs_flat_call_destructor,
757 };
758
759 static const struct afs_call_type afs_RXFSMakeDir = {
760         .name           = "FS.MakeDir",
761         .op             = afs_FS_MakeDir,
762         .deliver        = afs_deliver_fs_create_vnode,
763         .destructor     = afs_flat_call_destructor,
764 };
765
766 /*
767  * create a file or make a directory
768  */
769 int afs_fs_create(struct afs_fs_cursor *fc,
770                   const char *name,
771                   umode_t mode,
772                   u64 current_data_version,
773                   struct afs_fid *newfid,
774                   struct afs_file_status *newstatus,
775                   struct afs_callback *newcb)
776 {
777         struct afs_vnode *vnode = fc->vnode;
778         struct afs_call *call;
779         struct afs_net *net = afs_v2net(vnode);
780         size_t namesz, reqsz, padsz;
781         __be32 *bp;
782
783         if (test_bit(AFS_SERVER_FL_IS_YFS, &fc->cbi->server->flags)){
784                 if (S_ISDIR(mode))
785                         return yfs_fs_make_dir(fc, name, mode, current_data_version,
786                                                newfid, newstatus, newcb);
787                 else
788                         return yfs_fs_create_file(fc, name, mode, current_data_version,
789                                                   newfid, newstatus, newcb);
790         }
791
792         _enter("");
793
794         namesz = strlen(name);
795         padsz = (4 - (namesz & 3)) & 3;
796         reqsz = (5 * 4) + namesz + padsz + (6 * 4);
797
798         call = afs_alloc_flat_call(
799                 net, S_ISDIR(mode) ? &afs_RXFSMakeDir : &afs_RXFSCreateFile,
800                 reqsz, (3 + 21 + 21 + 3 + 6) * 4);
801         if (!call)
802                 return -ENOMEM;
803
804         call->key = fc->key;
805         call->reply[0] = vnode;
806         call->reply[1] = newfid;
807         call->reply[2] = newstatus;
808         call->reply[3] = newcb;
809         call->expected_version = current_data_version + 1;
810         call->want_reply_time = true;
811
812         /* marshall the parameters */
813         bp = call->request;
814         *bp++ = htonl(S_ISDIR(mode) ? FSMAKEDIR : FSCREATEFILE);
815         *bp++ = htonl(vnode->fid.vid);
816         *bp++ = htonl(vnode->fid.vnode);
817         *bp++ = htonl(vnode->fid.unique);
818         *bp++ = htonl(namesz);
819         memcpy(bp, name, namesz);
820         bp = (void *) bp + namesz;
821         if (padsz > 0) {
822                 memset(bp, 0, padsz);
823                 bp = (void *) bp + padsz;
824         }
825         *bp++ = htonl(AFS_SET_MODE | AFS_SET_MTIME);
826         *bp++ = htonl(vnode->vfs_inode.i_mtime.tv_sec); /* mtime */
827         *bp++ = 0; /* owner */
828         *bp++ = 0; /* group */
829         *bp++ = htonl(mode & S_IALLUGO); /* unix mode */
830         *bp++ = 0; /* segment size */
831
832         afs_use_fs_server(call, fc->cbi);
833         trace_afs_make_fs_call1(call, &vnode->fid, name);
834         afs_make_call(&fc->ac, call, GFP_NOFS);
835         return afs_wait_for_call_to_complete(call, &fc->ac);
836 }
837
838 /*
839  * Deliver reply data to any operation that returns file status and volume
840  * sync.
841  */
842 static int afs_deliver_fs_status_and_vol(struct afs_call *call)
843 {
844         struct afs_vnode *vnode = call->reply[0];
845         const __be32 *bp;
846         int ret;
847
848         _enter("{%u}", call->unmarshall);
849
850         ret = afs_transfer_reply(call);
851         if (ret < 0)
852                 return ret;
853
854         /* unmarshall the reply once we've received all of it */
855         bp = call->buffer;
856         ret = afs_decode_status(call, &bp, &vnode->status, vnode,
857                                 &call->expected_version, NULL);
858         if (ret < 0)
859                 return ret;
860         /* xdr_decode_AFSVolSync(&bp, call->reply[X]); */
861
862         _leave(" = 0 [done]");
863         return 0;
864 }
865
866 /*
867  * FS.RemoveDir/FS.RemoveFile operation type
868  */
869 static const struct afs_call_type afs_RXFSRemoveFile = {
870         .name           = "FS.RemoveFile",
871         .op             = afs_FS_RemoveFile,
872         .deliver        = afs_deliver_fs_status_and_vol,
873         .destructor     = afs_flat_call_destructor,
874 };
875
876 static const struct afs_call_type afs_RXFSRemoveDir = {
877         .name           = "FS.RemoveDir",
878         .op             = afs_FS_RemoveDir,
879         .deliver        = afs_deliver_fs_status_and_vol,
880         .destructor     = afs_flat_call_destructor,
881 };
882
883 /*
884  * remove a file or directory
885  */
886 int afs_fs_remove(struct afs_fs_cursor *fc, struct afs_vnode *vnode,
887                   const char *name, bool isdir, u64 current_data_version)
888 {
889         struct afs_vnode *dvnode = fc->vnode;
890         struct afs_call *call;
891         struct afs_net *net = afs_v2net(dvnode);
892         size_t namesz, reqsz, padsz;
893         __be32 *bp;
894
895         if (test_bit(AFS_SERVER_FL_IS_YFS, &fc->cbi->server->flags))
896                 return yfs_fs_remove(fc, vnode, name, isdir, current_data_version);
897
898         _enter("");
899
900         namesz = strlen(name);
901         padsz = (4 - (namesz & 3)) & 3;
902         reqsz = (5 * 4) + namesz + padsz;
903
904         call = afs_alloc_flat_call(
905                 net, isdir ? &afs_RXFSRemoveDir : &afs_RXFSRemoveFile,
906                 reqsz, (21 + 6) * 4);
907         if (!call)
908                 return -ENOMEM;
909
910         call->key = fc->key;
911         call->reply[0] = dvnode;
912         call->reply[1] = vnode;
913         call->expected_version = current_data_version + 1;
914
915         /* marshall the parameters */
916         bp = call->request;
917         *bp++ = htonl(isdir ? FSREMOVEDIR : FSREMOVEFILE);
918         *bp++ = htonl(dvnode->fid.vid);
919         *bp++ = htonl(dvnode->fid.vnode);
920         *bp++ = htonl(dvnode->fid.unique);
921         *bp++ = htonl(namesz);
922         memcpy(bp, name, namesz);
923         bp = (void *) bp + namesz;
924         if (padsz > 0) {
925                 memset(bp, 0, padsz);
926                 bp = (void *) bp + padsz;
927         }
928
929         afs_use_fs_server(call, fc->cbi);
930         trace_afs_make_fs_call1(call, &dvnode->fid, name);
931         afs_make_call(&fc->ac, call, GFP_NOFS);
932         return afs_wait_for_call_to_complete(call, &fc->ac);
933 }
934
935 /*
936  * deliver reply data to an FS.Link
937  */
938 static int afs_deliver_fs_link(struct afs_call *call)
939 {
940         struct afs_vnode *dvnode = call->reply[0], *vnode = call->reply[1];
941         const __be32 *bp;
942         int ret;
943
944         _enter("{%u}", call->unmarshall);
945
946         ret = afs_transfer_reply(call);
947         if (ret < 0)
948                 return ret;
949
950         /* unmarshall the reply once we've received all of it */
951         bp = call->buffer;
952         ret = afs_decode_status(call, &bp, &vnode->status, vnode, NULL, NULL);
953         if (ret < 0)
954                 return ret;
955         ret = afs_decode_status(call, &bp, &dvnode->status, dvnode,
956                                 &call->expected_version, NULL);
957         if (ret < 0)
958                 return ret;
959         /* xdr_decode_AFSVolSync(&bp, call->reply[X]); */
960
961         _leave(" = 0 [done]");
962         return 0;
963 }
964
965 /*
966  * FS.Link operation type
967  */
968 static const struct afs_call_type afs_RXFSLink = {
969         .name           = "FS.Link",
970         .op             = afs_FS_Link,
971         .deliver        = afs_deliver_fs_link,
972         .destructor     = afs_flat_call_destructor,
973 };
974
975 /*
976  * make a hard link
977  */
978 int afs_fs_link(struct afs_fs_cursor *fc, struct afs_vnode *vnode,
979                 const char *name, u64 current_data_version)
980 {
981         struct afs_vnode *dvnode = fc->vnode;
982         struct afs_call *call;
983         struct afs_net *net = afs_v2net(vnode);
984         size_t namesz, reqsz, padsz;
985         __be32 *bp;
986
987         if (test_bit(AFS_SERVER_FL_IS_YFS, &fc->cbi->server->flags))
988                 return yfs_fs_link(fc, vnode, name, current_data_version);
989
990         _enter("");
991
992         namesz = strlen(name);
993         padsz = (4 - (namesz & 3)) & 3;
994         reqsz = (5 * 4) + namesz + padsz + (3 * 4);
995
996         call = afs_alloc_flat_call(net, &afs_RXFSLink, reqsz, (21 + 21 + 6) * 4);
997         if (!call)
998                 return -ENOMEM;
999
1000         call->key = fc->key;
1001         call->reply[0] = dvnode;
1002         call->reply[1] = vnode;
1003         call->expected_version = current_data_version + 1;
1004
1005         /* marshall the parameters */
1006         bp = call->request;
1007         *bp++ = htonl(FSLINK);
1008         *bp++ = htonl(dvnode->fid.vid);
1009         *bp++ = htonl(dvnode->fid.vnode);
1010         *bp++ = htonl(dvnode->fid.unique);
1011         *bp++ = htonl(namesz);
1012         memcpy(bp, name, namesz);
1013         bp = (void *) bp + namesz;
1014         if (padsz > 0) {
1015                 memset(bp, 0, padsz);
1016                 bp = (void *) bp + padsz;
1017         }
1018         *bp++ = htonl(vnode->fid.vid);
1019         *bp++ = htonl(vnode->fid.vnode);
1020         *bp++ = htonl(vnode->fid.unique);
1021
1022         afs_use_fs_server(call, fc->cbi);
1023         trace_afs_make_fs_call1(call, &vnode->fid, name);
1024         afs_make_call(&fc->ac, call, GFP_NOFS);
1025         return afs_wait_for_call_to_complete(call, &fc->ac);
1026 }
1027
1028 /*
1029  * deliver reply data to an FS.Symlink
1030  */
1031 static int afs_deliver_fs_symlink(struct afs_call *call)
1032 {
1033         struct afs_vnode *vnode = call->reply[0];
1034         const __be32 *bp;
1035         int ret;
1036
1037         _enter("{%u}", call->unmarshall);
1038
1039         ret = afs_transfer_reply(call);
1040         if (ret < 0)
1041                 return ret;
1042
1043         /* unmarshall the reply once we've received all of it */
1044         bp = call->buffer;
1045         xdr_decode_AFSFid(&bp, call->reply[1]);
1046         ret = afs_decode_status(call, &bp, call->reply[2], NULL, NULL, NULL);
1047         if (ret < 0)
1048                 return ret;
1049         ret = afs_decode_status(call, &bp, &vnode->status, vnode,
1050                                 &call->expected_version, NULL);
1051         if (ret < 0)
1052                 return ret;
1053         /* xdr_decode_AFSVolSync(&bp, call->reply[X]); */
1054
1055         _leave(" = 0 [done]");
1056         return 0;
1057 }
1058
1059 /*
1060  * FS.Symlink operation type
1061  */
1062 static const struct afs_call_type afs_RXFSSymlink = {
1063         .name           = "FS.Symlink",
1064         .op             = afs_FS_Symlink,
1065         .deliver        = afs_deliver_fs_symlink,
1066         .destructor     = afs_flat_call_destructor,
1067 };
1068
1069 /*
1070  * create a symbolic link
1071  */
1072 int afs_fs_symlink(struct afs_fs_cursor *fc,
1073                    const char *name,
1074                    const char *contents,
1075                    u64 current_data_version,
1076                    struct afs_fid *newfid,
1077                    struct afs_file_status *newstatus)
1078 {
1079         struct afs_vnode *vnode = fc->vnode;
1080         struct afs_call *call;
1081         struct afs_net *net = afs_v2net(vnode);
1082         size_t namesz, reqsz, padsz, c_namesz, c_padsz;
1083         __be32 *bp;
1084
1085         if (test_bit(AFS_SERVER_FL_IS_YFS, &fc->cbi->server->flags))
1086                 return yfs_fs_symlink(fc, name, contents, current_data_version,
1087                                       newfid, newstatus);
1088
1089         _enter("");
1090
1091         namesz = strlen(name);
1092         padsz = (4 - (namesz & 3)) & 3;
1093
1094         c_namesz = strlen(contents);
1095         c_padsz = (4 - (c_namesz & 3)) & 3;
1096
1097         reqsz = (6 * 4) + namesz + padsz + c_namesz + c_padsz + (6 * 4);
1098
1099         call = afs_alloc_flat_call(net, &afs_RXFSSymlink, reqsz,
1100                                    (3 + 21 + 21 + 6) * 4);
1101         if (!call)
1102                 return -ENOMEM;
1103
1104         call->key = fc->key;
1105         call->reply[0] = vnode;
1106         call->reply[1] = newfid;
1107         call->reply[2] = newstatus;
1108         call->expected_version = current_data_version + 1;
1109
1110         /* marshall the parameters */
1111         bp = call->request;
1112         *bp++ = htonl(FSSYMLINK);
1113         *bp++ = htonl(vnode->fid.vid);
1114         *bp++ = htonl(vnode->fid.vnode);
1115         *bp++ = htonl(vnode->fid.unique);
1116         *bp++ = htonl(namesz);
1117         memcpy(bp, name, namesz);
1118         bp = (void *) bp + namesz;
1119         if (padsz > 0) {
1120                 memset(bp, 0, padsz);
1121                 bp = (void *) bp + padsz;
1122         }
1123         *bp++ = htonl(c_namesz);
1124         memcpy(bp, contents, c_namesz);
1125         bp = (void *) bp + c_namesz;
1126         if (c_padsz > 0) {
1127                 memset(bp, 0, c_padsz);
1128                 bp = (void *) bp + c_padsz;
1129         }
1130         *bp++ = htonl(AFS_SET_MODE | AFS_SET_MTIME);
1131         *bp++ = htonl(vnode->vfs_inode.i_mtime.tv_sec); /* mtime */
1132         *bp++ = 0; /* owner */
1133         *bp++ = 0; /* group */
1134         *bp++ = htonl(S_IRWXUGO); /* unix mode */
1135         *bp++ = 0; /* segment size */
1136
1137         afs_use_fs_server(call, fc->cbi);
1138         trace_afs_make_fs_call1(call, &vnode->fid, name);
1139         afs_make_call(&fc->ac, call, GFP_NOFS);
1140         return afs_wait_for_call_to_complete(call, &fc->ac);
1141 }
1142
1143 /*
1144  * deliver reply data to an FS.Rename
1145  */
1146 static int afs_deliver_fs_rename(struct afs_call *call)
1147 {
1148         struct afs_vnode *orig_dvnode = call->reply[0], *new_dvnode = call->reply[1];
1149         const __be32 *bp;
1150         int ret;
1151
1152         _enter("{%u}", call->unmarshall);
1153
1154         ret = afs_transfer_reply(call);
1155         if (ret < 0)
1156                 return ret;
1157
1158         /* unmarshall the reply once we've received all of it */
1159         bp = call->buffer;
1160         ret = afs_decode_status(call, &bp, &orig_dvnode->status, orig_dvnode,
1161                                 &call->expected_version, NULL);
1162         if (ret < 0)
1163                 return ret;
1164         if (new_dvnode != orig_dvnode) {
1165                 ret = afs_decode_status(call, &bp, &new_dvnode->status, new_dvnode,
1166                                         &call->expected_version_2, NULL);
1167                 if (ret < 0)
1168                         return ret;
1169         }
1170         /* xdr_decode_AFSVolSync(&bp, call->reply[X]); */
1171
1172         _leave(" = 0 [done]");
1173         return 0;
1174 }
1175
1176 /*
1177  * FS.Rename operation type
1178  */
1179 static const struct afs_call_type afs_RXFSRename = {
1180         .name           = "FS.Rename",
1181         .op             = afs_FS_Rename,
1182         .deliver        = afs_deliver_fs_rename,
1183         .destructor     = afs_flat_call_destructor,
1184 };
1185
1186 /*
1187  * create a symbolic link
1188  */
1189 int afs_fs_rename(struct afs_fs_cursor *fc,
1190                   const char *orig_name,
1191                   struct afs_vnode *new_dvnode,
1192                   const char *new_name,
1193                   u64 current_orig_data_version,
1194                   u64 current_new_data_version)
1195 {
1196         struct afs_vnode *orig_dvnode = fc->vnode;
1197         struct afs_call *call;
1198         struct afs_net *net = afs_v2net(orig_dvnode);
1199         size_t reqsz, o_namesz, o_padsz, n_namesz, n_padsz;
1200         __be32 *bp;
1201
1202         if (test_bit(AFS_SERVER_FL_IS_YFS, &fc->cbi->server->flags))
1203                 return yfs_fs_rename(fc, orig_name,
1204                                      new_dvnode, new_name,
1205                                      current_orig_data_version,
1206                                      current_new_data_version);
1207
1208         _enter("");
1209
1210         o_namesz = strlen(orig_name);
1211         o_padsz = (4 - (o_namesz & 3)) & 3;
1212
1213         n_namesz = strlen(new_name);
1214         n_padsz = (4 - (n_namesz & 3)) & 3;
1215
1216         reqsz = (4 * 4) +
1217                 4 + o_namesz + o_padsz +
1218                 (3 * 4) +
1219                 4 + n_namesz + n_padsz;
1220
1221         call = afs_alloc_flat_call(net, &afs_RXFSRename, reqsz, (21 + 21 + 6) * 4);
1222         if (!call)
1223                 return -ENOMEM;
1224
1225         call->key = fc->key;
1226         call->reply[0] = orig_dvnode;
1227         call->reply[1] = new_dvnode;
1228         call->expected_version = current_orig_data_version + 1;
1229         call->expected_version_2 = current_new_data_version + 1;
1230
1231         /* marshall the parameters */
1232         bp = call->request;
1233         *bp++ = htonl(FSRENAME);
1234         *bp++ = htonl(orig_dvnode->fid.vid);
1235         *bp++ = htonl(orig_dvnode->fid.vnode);
1236         *bp++ = htonl(orig_dvnode->fid.unique);
1237         *bp++ = htonl(o_namesz);
1238         memcpy(bp, orig_name, o_namesz);
1239         bp = (void *) bp + o_namesz;
1240         if (o_padsz > 0) {
1241                 memset(bp, 0, o_padsz);
1242                 bp = (void *) bp + o_padsz;
1243         }
1244
1245         *bp++ = htonl(new_dvnode->fid.vid);
1246         *bp++ = htonl(new_dvnode->fid.vnode);
1247         *bp++ = htonl(new_dvnode->fid.unique);
1248         *bp++ = htonl(n_namesz);
1249         memcpy(bp, new_name, n_namesz);
1250         bp = (void *) bp + n_namesz;
1251         if (n_padsz > 0) {
1252                 memset(bp, 0, n_padsz);
1253                 bp = (void *) bp + n_padsz;
1254         }
1255
1256         afs_use_fs_server(call, fc->cbi);
1257         trace_afs_make_fs_call2(call, &orig_dvnode->fid, orig_name, new_name);
1258         afs_make_call(&fc->ac, call, GFP_NOFS);
1259         return afs_wait_for_call_to_complete(call, &fc->ac);
1260 }
1261
1262 /*
1263  * deliver reply data to an FS.StoreData
1264  */
1265 static int afs_deliver_fs_store_data(struct afs_call *call)
1266 {
1267         struct afs_vnode *vnode = call->reply[0];
1268         const __be32 *bp;
1269         int ret;
1270
1271         _enter("");
1272
1273         ret = afs_transfer_reply(call);
1274         if (ret < 0)
1275                 return ret;
1276
1277         /* unmarshall the reply once we've received all of it */
1278         bp = call->buffer;
1279         ret = afs_decode_status(call, &bp, &vnode->status, vnode,
1280                                 &call->expected_version, NULL);
1281         if (ret < 0)
1282                 return ret;
1283         /* xdr_decode_AFSVolSync(&bp, call->reply[X]); */
1284
1285         afs_pages_written_back(vnode, call);
1286
1287         _leave(" = 0 [done]");
1288         return 0;
1289 }
1290
1291 /*
1292  * FS.StoreData operation type
1293  */
1294 static const struct afs_call_type afs_RXFSStoreData = {
1295         .name           = "FS.StoreData",
1296         .op             = afs_FS_StoreData,
1297         .deliver        = afs_deliver_fs_store_data,
1298         .destructor     = afs_flat_call_destructor,
1299 };
1300
1301 static const struct afs_call_type afs_RXFSStoreData64 = {
1302         .name           = "FS.StoreData64",
1303         .op             = afs_FS_StoreData64,
1304         .deliver        = afs_deliver_fs_store_data,
1305         .destructor     = afs_flat_call_destructor,
1306 };
1307
1308 /*
1309  * store a set of pages to a very large file
1310  */
1311 static int afs_fs_store_data64(struct afs_fs_cursor *fc,
1312                                struct address_space *mapping,
1313                                pgoff_t first, pgoff_t last,
1314                                unsigned offset, unsigned to,
1315                                loff_t size, loff_t pos, loff_t i_size)
1316 {
1317         struct afs_vnode *vnode = fc->vnode;
1318         struct afs_call *call;
1319         struct afs_net *net = afs_v2net(vnode);
1320         __be32 *bp;
1321
1322         _enter(",%x,{%llx:%llu},,",
1323                key_serial(fc->key), vnode->fid.vid, vnode->fid.vnode);
1324
1325         call = afs_alloc_flat_call(net, &afs_RXFSStoreData64,
1326                                    (4 + 6 + 3 * 2) * 4,
1327                                    (21 + 6) * 4);
1328         if (!call)
1329                 return -ENOMEM;
1330
1331         call->key = fc->key;
1332         call->mapping = mapping;
1333         call->reply[0] = vnode;
1334         call->first = first;
1335         call->last = last;
1336         call->first_offset = offset;
1337         call->last_to = to;
1338         call->send_pages = true;
1339         call->expected_version = vnode->status.data_version + 1;
1340
1341         /* marshall the parameters */
1342         bp = call->request;
1343         *bp++ = htonl(FSSTOREDATA64);
1344         *bp++ = htonl(vnode->fid.vid);
1345         *bp++ = htonl(vnode->fid.vnode);
1346         *bp++ = htonl(vnode->fid.unique);
1347
1348         *bp++ = htonl(AFS_SET_MTIME); /* mask */
1349         *bp++ = htonl(vnode->vfs_inode.i_mtime.tv_sec); /* mtime */
1350         *bp++ = 0; /* owner */
1351         *bp++ = 0; /* group */
1352         *bp++ = 0; /* unix mode */
1353         *bp++ = 0; /* segment size */
1354
1355         *bp++ = htonl(pos >> 32);
1356         *bp++ = htonl((u32) pos);
1357         *bp++ = htonl(size >> 32);
1358         *bp++ = htonl((u32) size);
1359         *bp++ = htonl(i_size >> 32);
1360         *bp++ = htonl((u32) i_size);
1361
1362         trace_afs_make_fs_call(call, &vnode->fid);
1363         afs_make_call(&fc->ac, call, GFP_NOFS);
1364         return afs_wait_for_call_to_complete(call, &fc->ac);
1365 }
1366
1367 /*
1368  * store a set of pages
1369  */
1370 int afs_fs_store_data(struct afs_fs_cursor *fc, struct address_space *mapping,
1371                       pgoff_t first, pgoff_t last,
1372                       unsigned offset, unsigned to)
1373 {
1374         struct afs_vnode *vnode = fc->vnode;
1375         struct afs_call *call;
1376         struct afs_net *net = afs_v2net(vnode);
1377         loff_t size, pos, i_size;
1378         __be32 *bp;
1379
1380         if (test_bit(AFS_SERVER_FL_IS_YFS, &fc->cbi->server->flags))
1381                 return yfs_fs_store_data(fc, mapping, first, last, offset, to);
1382
1383         _enter(",%x,{%llx:%llu},,",
1384                key_serial(fc->key), vnode->fid.vid, vnode->fid.vnode);
1385
1386         size = (loff_t)to - (loff_t)offset;
1387         if (first != last)
1388                 size += (loff_t)(last - first) << PAGE_SHIFT;
1389         pos = (loff_t)first << PAGE_SHIFT;
1390         pos += offset;
1391
1392         i_size = i_size_read(&vnode->vfs_inode);
1393         if (pos + size > i_size)
1394                 i_size = size + pos;
1395
1396         _debug("size %llx, at %llx, i_size %llx",
1397                (unsigned long long) size, (unsigned long long) pos,
1398                (unsigned long long) i_size);
1399
1400         if (pos >> 32 || i_size >> 32 || size >> 32 || (pos + size) >> 32)
1401                 return afs_fs_store_data64(fc, mapping, first, last, offset, to,
1402                                            size, pos, i_size);
1403
1404         call = afs_alloc_flat_call(net, &afs_RXFSStoreData,
1405                                    (4 + 6 + 3) * 4,
1406                                    (21 + 6) * 4);
1407         if (!call)
1408                 return -ENOMEM;
1409
1410         call->key = fc->key;
1411         call->mapping = mapping;
1412         call->reply[0] = vnode;
1413         call->first = first;
1414         call->last = last;
1415         call->first_offset = offset;
1416         call->last_to = to;
1417         call->send_pages = true;
1418         call->expected_version = vnode->status.data_version + 1;
1419
1420         /* marshall the parameters */
1421         bp = call->request;
1422         *bp++ = htonl(FSSTOREDATA);
1423         *bp++ = htonl(vnode->fid.vid);
1424         *bp++ = htonl(vnode->fid.vnode);
1425         *bp++ = htonl(vnode->fid.unique);
1426
1427         *bp++ = htonl(AFS_SET_MTIME); /* mask */
1428         *bp++ = htonl(vnode->vfs_inode.i_mtime.tv_sec); /* mtime */
1429         *bp++ = 0; /* owner */
1430         *bp++ = 0; /* group */
1431         *bp++ = 0; /* unix mode */
1432         *bp++ = 0; /* segment size */
1433
1434         *bp++ = htonl(pos);
1435         *bp++ = htonl(size);
1436         *bp++ = htonl(i_size);
1437
1438         afs_use_fs_server(call, fc->cbi);
1439         trace_afs_make_fs_call(call, &vnode->fid);
1440         afs_make_call(&fc->ac, call, GFP_NOFS);
1441         return afs_wait_for_call_to_complete(call, &fc->ac);
1442 }
1443
1444 /*
1445  * deliver reply data to an FS.StoreStatus
1446  */
1447 static int afs_deliver_fs_store_status(struct afs_call *call)
1448 {
1449         struct afs_vnode *vnode = call->reply[0];
1450         const __be32 *bp;
1451         int ret;
1452
1453         _enter("");
1454
1455         ret = afs_transfer_reply(call);
1456         if (ret < 0)
1457                 return ret;
1458
1459         /* unmarshall the reply once we've received all of it */
1460         bp = call->buffer;
1461         ret = afs_decode_status(call, &bp, &vnode->status, vnode,
1462                                 &call->expected_version, NULL);
1463         if (ret < 0)
1464                 return ret;
1465         /* xdr_decode_AFSVolSync(&bp, call->reply[X]); */
1466
1467         _leave(" = 0 [done]");
1468         return 0;
1469 }
1470
1471 /*
1472  * FS.StoreStatus operation type
1473  */
1474 static const struct afs_call_type afs_RXFSStoreStatus = {
1475         .name           = "FS.StoreStatus",
1476         .op             = afs_FS_StoreStatus,
1477         .deliver        = afs_deliver_fs_store_status,
1478         .destructor     = afs_flat_call_destructor,
1479 };
1480
1481 static const struct afs_call_type afs_RXFSStoreData_as_Status = {
1482         .name           = "FS.StoreData",
1483         .op             = afs_FS_StoreData,
1484         .deliver        = afs_deliver_fs_store_status,
1485         .destructor     = afs_flat_call_destructor,
1486 };
1487
1488 static const struct afs_call_type afs_RXFSStoreData64_as_Status = {
1489         .name           = "FS.StoreData64",
1490         .op             = afs_FS_StoreData64,
1491         .deliver        = afs_deliver_fs_store_status,
1492         .destructor     = afs_flat_call_destructor,
1493 };
1494
1495 /*
1496  * set the attributes on a very large file, using FS.StoreData rather than
1497  * FS.StoreStatus so as to alter the file size also
1498  */
1499 static int afs_fs_setattr_size64(struct afs_fs_cursor *fc, struct iattr *attr)
1500 {
1501         struct afs_vnode *vnode = fc->vnode;
1502         struct afs_call *call;
1503         struct afs_net *net = afs_v2net(vnode);
1504         __be32 *bp;
1505
1506         _enter(",%x,{%llx:%llu},,",
1507                key_serial(fc->key), vnode->fid.vid, vnode->fid.vnode);
1508
1509         ASSERT(attr->ia_valid & ATTR_SIZE);
1510
1511         call = afs_alloc_flat_call(net, &afs_RXFSStoreData64_as_Status,
1512                                    (4 + 6 + 3 * 2) * 4,
1513                                    (21 + 6) * 4);
1514         if (!call)
1515                 return -ENOMEM;
1516
1517         call->key = fc->key;
1518         call->reply[0] = vnode;
1519         call->expected_version = vnode->status.data_version + 1;
1520
1521         /* marshall the parameters */
1522         bp = call->request;
1523         *bp++ = htonl(FSSTOREDATA64);
1524         *bp++ = htonl(vnode->fid.vid);
1525         *bp++ = htonl(vnode->fid.vnode);
1526         *bp++ = htonl(vnode->fid.unique);
1527
1528         xdr_encode_AFS_StoreStatus(&bp, attr);
1529
1530         *bp++ = htonl(attr->ia_size >> 32);     /* position of start of write */
1531         *bp++ = htonl((u32) attr->ia_size);
1532         *bp++ = 0;                              /* size of write */
1533         *bp++ = 0;
1534         *bp++ = htonl(attr->ia_size >> 32);     /* new file length */
1535         *bp++ = htonl((u32) attr->ia_size);
1536
1537         afs_use_fs_server(call, fc->cbi);
1538         trace_afs_make_fs_call(call, &vnode->fid);
1539         afs_make_call(&fc->ac, call, GFP_NOFS);
1540         return afs_wait_for_call_to_complete(call, &fc->ac);
1541 }
1542
1543 /*
1544  * set the attributes on a file, using FS.StoreData rather than FS.StoreStatus
1545  * so as to alter the file size also
1546  */
1547 static int afs_fs_setattr_size(struct afs_fs_cursor *fc, struct iattr *attr)
1548 {
1549         struct afs_vnode *vnode = fc->vnode;
1550         struct afs_call *call;
1551         struct afs_net *net = afs_v2net(vnode);
1552         __be32 *bp;
1553
1554         _enter(",%x,{%llx:%llu},,",
1555                key_serial(fc->key), vnode->fid.vid, vnode->fid.vnode);
1556
1557         ASSERT(attr->ia_valid & ATTR_SIZE);
1558         if (attr->ia_size >> 32)
1559                 return afs_fs_setattr_size64(fc, attr);
1560
1561         call = afs_alloc_flat_call(net, &afs_RXFSStoreData_as_Status,
1562                                    (4 + 6 + 3) * 4,
1563                                    (21 + 6) * 4);
1564         if (!call)
1565                 return -ENOMEM;
1566
1567         call->key = fc->key;
1568         call->reply[0] = vnode;
1569         call->expected_version = vnode->status.data_version + 1;
1570
1571         /* marshall the parameters */
1572         bp = call->request;
1573         *bp++ = htonl(FSSTOREDATA);
1574         *bp++ = htonl(vnode->fid.vid);
1575         *bp++ = htonl(vnode->fid.vnode);
1576         *bp++ = htonl(vnode->fid.unique);
1577
1578         xdr_encode_AFS_StoreStatus(&bp, attr);
1579
1580         *bp++ = htonl(attr->ia_size);           /* position of start of write */
1581         *bp++ = 0;                              /* size of write */
1582         *bp++ = htonl(attr->ia_size);           /* new file length */
1583
1584         afs_use_fs_server(call, fc->cbi);
1585         trace_afs_make_fs_call(call, &vnode->fid);
1586         afs_make_call(&fc->ac, call, GFP_NOFS);
1587         return afs_wait_for_call_to_complete(call, &fc->ac);
1588 }
1589
1590 /*
1591  * set the attributes on a file, using FS.StoreData if there's a change in file
1592  * size, and FS.StoreStatus otherwise
1593  */
1594 int afs_fs_setattr(struct afs_fs_cursor *fc, struct iattr *attr)
1595 {
1596         struct afs_vnode *vnode = fc->vnode;
1597         struct afs_call *call;
1598         struct afs_net *net = afs_v2net(vnode);
1599         __be32 *bp;
1600
1601         if (test_bit(AFS_SERVER_FL_IS_YFS, &fc->cbi->server->flags))
1602                 return yfs_fs_setattr(fc, attr);
1603
1604         if (attr->ia_valid & ATTR_SIZE)
1605                 return afs_fs_setattr_size(fc, attr);
1606
1607         _enter(",%x,{%llx:%llu},,",
1608                key_serial(fc->key), vnode->fid.vid, vnode->fid.vnode);
1609
1610         call = afs_alloc_flat_call(net, &afs_RXFSStoreStatus,
1611                                    (4 + 6) * 4,
1612                                    (21 + 6) * 4);
1613         if (!call)
1614                 return -ENOMEM;
1615
1616         call->key = fc->key;
1617         call->reply[0] = vnode;
1618         call->expected_version = vnode->status.data_version;
1619
1620         /* marshall the parameters */
1621         bp = call->request;
1622         *bp++ = htonl(FSSTORESTATUS);
1623         *bp++ = htonl(vnode->fid.vid);
1624         *bp++ = htonl(vnode->fid.vnode);
1625         *bp++ = htonl(vnode->fid.unique);
1626
1627         xdr_encode_AFS_StoreStatus(&bp, attr);
1628
1629         afs_use_fs_server(call, fc->cbi);
1630         trace_afs_make_fs_call(call, &vnode->fid);
1631         afs_make_call(&fc->ac, call, GFP_NOFS);
1632         return afs_wait_for_call_to_complete(call, &fc->ac);
1633 }
1634
1635 /*
1636  * deliver reply data to an FS.GetVolumeStatus
1637  */
1638 static int afs_deliver_fs_get_volume_status(struct afs_call *call)
1639 {
1640         const __be32 *bp;
1641         char *p;
1642         u32 size;
1643         int ret;
1644
1645         _enter("{%u}", call->unmarshall);
1646
1647         switch (call->unmarshall) {
1648         case 0:
1649                 call->unmarshall++;
1650                 afs_extract_to_buf(call, 12 * 4);
1651
1652                 /* extract the returned status record */
1653         case 1:
1654                 _debug("extract status");
1655                 ret = afs_extract_data(call, true);
1656                 if (ret < 0)
1657                         return ret;
1658
1659                 bp = call->buffer;
1660                 xdr_decode_AFSFetchVolumeStatus(&bp, call->reply[1]);
1661                 call->unmarshall++;
1662                 afs_extract_to_tmp(call);
1663
1664                 /* extract the volume name length */
1665         case 2:
1666                 ret = afs_extract_data(call, true);
1667                 if (ret < 0)
1668                         return ret;
1669
1670                 call->count = ntohl(call->tmp);
1671                 _debug("volname length: %u", call->count);
1672                 if (call->count >= AFSNAMEMAX)
1673                         return afs_protocol_error(call, -EBADMSG,
1674                                                   afs_eproto_volname_len);
1675                 size = (call->count + 3) & ~3; /* It's padded */
1676                 afs_extract_begin(call, call->reply[2], size);
1677                 call->unmarshall++;
1678
1679                 /* extract the volume name */
1680         case 3:
1681                 _debug("extract volname");
1682                 ret = afs_extract_data(call, true);
1683                 if (ret < 0)
1684                         return ret;
1685
1686                 p = call->reply[2];
1687                 p[call->count] = 0;
1688                 _debug("volname '%s'", p);
1689                 afs_extract_to_tmp(call);
1690                 call->unmarshall++;
1691
1692                 /* extract the offline message length */
1693         case 4:
1694                 ret = afs_extract_data(call, true);
1695                 if (ret < 0)
1696                         return ret;
1697
1698                 call->count = ntohl(call->tmp);
1699                 _debug("offline msg length: %u", call->count);
1700                 if (call->count >= AFSNAMEMAX)
1701                         return afs_protocol_error(call, -EBADMSG,
1702                                                   afs_eproto_offline_msg_len);
1703                 size = (call->count + 3) & ~3; /* It's padded */
1704                 afs_extract_begin(call, call->reply[2], size);
1705                 call->unmarshall++;
1706
1707                 /* extract the offline message */
1708         case 5:
1709                 _debug("extract offline");
1710                 ret = afs_extract_data(call, true);
1711                 if (ret < 0)
1712                         return ret;
1713
1714                 p = call->reply[2];
1715                 p[call->count] = 0;
1716                 _debug("offline '%s'", p);
1717
1718                 afs_extract_to_tmp(call);
1719                 call->unmarshall++;
1720
1721                 /* extract the message of the day length */
1722         case 6:
1723                 ret = afs_extract_data(call, true);
1724                 if (ret < 0)
1725                         return ret;
1726
1727                 call->count = ntohl(call->tmp);
1728                 _debug("motd length: %u", call->count);
1729                 if (call->count >= AFSNAMEMAX)
1730                         return afs_protocol_error(call, -EBADMSG,
1731                                                   afs_eproto_motd_len);
1732                 size = (call->count + 3) & ~3; /* It's padded */
1733                 afs_extract_begin(call, call->reply[2], size);
1734                 call->unmarshall++;
1735
1736                 /* extract the message of the day */
1737         case 7:
1738                 _debug("extract motd");
1739                 ret = afs_extract_data(call, false);
1740                 if (ret < 0)
1741                         return ret;
1742
1743                 p = call->reply[2];
1744                 p[call->count] = 0;
1745                 _debug("motd '%s'", p);
1746
1747                 call->unmarshall++;
1748
1749         case 8:
1750                 break;
1751         }
1752
1753         _leave(" = 0 [done]");
1754         return 0;
1755 }
1756
1757 /*
1758  * destroy an FS.GetVolumeStatus call
1759  */
1760 static void afs_get_volume_status_call_destructor(struct afs_call *call)
1761 {
1762         kfree(call->reply[2]);
1763         call->reply[2] = NULL;
1764         afs_flat_call_destructor(call);
1765 }
1766
1767 /*
1768  * FS.GetVolumeStatus operation type
1769  */
1770 static const struct afs_call_type afs_RXFSGetVolumeStatus = {
1771         .name           = "FS.GetVolumeStatus",
1772         .op             = afs_FS_GetVolumeStatus,
1773         .deliver        = afs_deliver_fs_get_volume_status,
1774         .destructor     = afs_get_volume_status_call_destructor,
1775 };
1776
1777 /*
1778  * fetch the status of a volume
1779  */
1780 int afs_fs_get_volume_status(struct afs_fs_cursor *fc,
1781                              struct afs_volume_status *vs)
1782 {
1783         struct afs_vnode *vnode = fc->vnode;
1784         struct afs_call *call;
1785         struct afs_net *net = afs_v2net(vnode);
1786         __be32 *bp;
1787         void *tmpbuf;
1788
1789         if (test_bit(AFS_SERVER_FL_IS_YFS, &fc->cbi->server->flags))
1790                 return yfs_fs_get_volume_status(fc, vs);
1791
1792         _enter("");
1793
1794         tmpbuf = kmalloc(AFSOPAQUEMAX, GFP_KERNEL);
1795         if (!tmpbuf)
1796                 return -ENOMEM;
1797
1798         call = afs_alloc_flat_call(net, &afs_RXFSGetVolumeStatus, 2 * 4, 12 * 4);
1799         if (!call) {
1800                 kfree(tmpbuf);
1801                 return -ENOMEM;
1802         }
1803
1804         call->key = fc->key;
1805         call->reply[0] = vnode;
1806         call->reply[1] = vs;
1807         call->reply[2] = tmpbuf;
1808
1809         /* marshall the parameters */
1810         bp = call->request;
1811         bp[0] = htonl(FSGETVOLUMESTATUS);
1812         bp[1] = htonl(vnode->fid.vid);
1813
1814         afs_use_fs_server(call, fc->cbi);
1815         trace_afs_make_fs_call(call, &vnode->fid);
1816         afs_make_call(&fc->ac, call, GFP_NOFS);
1817         return afs_wait_for_call_to_complete(call, &fc->ac);
1818 }
1819
1820 /*
1821  * deliver reply data to an FS.SetLock, FS.ExtendLock or FS.ReleaseLock
1822  */
1823 static int afs_deliver_fs_xxxx_lock(struct afs_call *call)
1824 {
1825         const __be32 *bp;
1826         int ret;
1827
1828         _enter("{%u}", call->unmarshall);
1829
1830         ret = afs_transfer_reply(call);
1831         if (ret < 0)
1832                 return ret;
1833
1834         /* unmarshall the reply once we've received all of it */
1835         bp = call->buffer;
1836         /* xdr_decode_AFSVolSync(&bp, call->reply[X]); */
1837
1838         _leave(" = 0 [done]");
1839         return 0;
1840 }
1841
1842 /*
1843  * FS.SetLock operation type
1844  */
1845 static const struct afs_call_type afs_RXFSSetLock = {
1846         .name           = "FS.SetLock",
1847         .op             = afs_FS_SetLock,
1848         .deliver        = afs_deliver_fs_xxxx_lock,
1849         .done           = afs_lock_op_done,
1850         .destructor     = afs_flat_call_destructor,
1851 };
1852
1853 /*
1854  * FS.ExtendLock operation type
1855  */
1856 static const struct afs_call_type afs_RXFSExtendLock = {
1857         .name           = "FS.ExtendLock",
1858         .op             = afs_FS_ExtendLock,
1859         .deliver        = afs_deliver_fs_xxxx_lock,
1860         .done           = afs_lock_op_done,
1861         .destructor     = afs_flat_call_destructor,
1862 };
1863
1864 /*
1865  * FS.ReleaseLock operation type
1866  */
1867 static const struct afs_call_type afs_RXFSReleaseLock = {
1868         .name           = "FS.ReleaseLock",
1869         .op             = afs_FS_ReleaseLock,
1870         .deliver        = afs_deliver_fs_xxxx_lock,
1871         .destructor     = afs_flat_call_destructor,
1872 };
1873
1874 /*
1875  * Set a lock on a file
1876  */
1877 int afs_fs_set_lock(struct afs_fs_cursor *fc, afs_lock_type_t type)
1878 {
1879         struct afs_vnode *vnode = fc->vnode;
1880         struct afs_call *call;
1881         struct afs_net *net = afs_v2net(vnode);
1882         __be32 *bp;
1883
1884         if (test_bit(AFS_SERVER_FL_IS_YFS, &fc->cbi->server->flags))
1885                 return yfs_fs_set_lock(fc, type);
1886
1887         _enter("");
1888
1889         call = afs_alloc_flat_call(net, &afs_RXFSSetLock, 5 * 4, 6 * 4);
1890         if (!call)
1891                 return -ENOMEM;
1892
1893         call->key = fc->key;
1894         call->reply[0] = vnode;
1895         call->want_reply_time = true;
1896
1897         /* marshall the parameters */
1898         bp = call->request;
1899         *bp++ = htonl(FSSETLOCK);
1900         *bp++ = htonl(vnode->fid.vid);
1901         *bp++ = htonl(vnode->fid.vnode);
1902         *bp++ = htonl(vnode->fid.unique);
1903         *bp++ = htonl(type);
1904
1905         afs_use_fs_server(call, fc->cbi);
1906         trace_afs_make_fs_calli(call, &vnode->fid, type);
1907         afs_make_call(&fc->ac, call, GFP_NOFS);
1908         return afs_wait_for_call_to_complete(call, &fc->ac);
1909 }
1910
1911 /*
1912  * extend a lock on a file
1913  */
1914 int afs_fs_extend_lock(struct afs_fs_cursor *fc)
1915 {
1916         struct afs_vnode *vnode = fc->vnode;
1917         struct afs_call *call;
1918         struct afs_net *net = afs_v2net(vnode);
1919         __be32 *bp;
1920
1921         if (test_bit(AFS_SERVER_FL_IS_YFS, &fc->cbi->server->flags))
1922                 return yfs_fs_extend_lock(fc);
1923
1924         _enter("");
1925
1926         call = afs_alloc_flat_call(net, &afs_RXFSExtendLock, 4 * 4, 6 * 4);
1927         if (!call)
1928                 return -ENOMEM;
1929
1930         call->key = fc->key;
1931         call->reply[0] = vnode;
1932         call->want_reply_time = true;
1933
1934         /* marshall the parameters */
1935         bp = call->request;
1936         *bp++ = htonl(FSEXTENDLOCK);
1937         *bp++ = htonl(vnode->fid.vid);
1938         *bp++ = htonl(vnode->fid.vnode);
1939         *bp++ = htonl(vnode->fid.unique);
1940
1941         afs_use_fs_server(call, fc->cbi);
1942         trace_afs_make_fs_call(call, &vnode->fid);
1943         afs_make_call(&fc->ac, call, GFP_NOFS);
1944         return afs_wait_for_call_to_complete(call, &fc->ac);
1945 }
1946
1947 /*
1948  * release a lock on a file
1949  */
1950 int afs_fs_release_lock(struct afs_fs_cursor *fc)
1951 {
1952         struct afs_vnode *vnode = fc->vnode;
1953         struct afs_call *call;
1954         struct afs_net *net = afs_v2net(vnode);
1955         __be32 *bp;
1956
1957         if (test_bit(AFS_SERVER_FL_IS_YFS, &fc->cbi->server->flags))
1958                 return yfs_fs_release_lock(fc);
1959
1960         _enter("");
1961
1962         call = afs_alloc_flat_call(net, &afs_RXFSReleaseLock, 4 * 4, 6 * 4);
1963         if (!call)
1964                 return -ENOMEM;
1965
1966         call->key = fc->key;
1967         call->reply[0] = vnode;
1968
1969         /* marshall the parameters */
1970         bp = call->request;
1971         *bp++ = htonl(FSRELEASELOCK);
1972         *bp++ = htonl(vnode->fid.vid);
1973         *bp++ = htonl(vnode->fid.vnode);
1974         *bp++ = htonl(vnode->fid.unique);
1975
1976         afs_use_fs_server(call, fc->cbi);
1977         trace_afs_make_fs_call(call, &vnode->fid);
1978         afs_make_call(&fc->ac, call, GFP_NOFS);
1979         return afs_wait_for_call_to_complete(call, &fc->ac);
1980 }
1981
1982 /*
1983  * Deliver reply data to an FS.GiveUpAllCallBacks operation.
1984  */
1985 static int afs_deliver_fs_give_up_all_callbacks(struct afs_call *call)
1986 {
1987         return afs_transfer_reply(call);
1988 }
1989
1990 /*
1991  * FS.GiveUpAllCallBacks operation type
1992  */
1993 static const struct afs_call_type afs_RXFSGiveUpAllCallBacks = {
1994         .name           = "FS.GiveUpAllCallBacks",
1995         .op             = afs_FS_GiveUpAllCallBacks,
1996         .deliver        = afs_deliver_fs_give_up_all_callbacks,
1997         .destructor     = afs_flat_call_destructor,
1998 };
1999
2000 /*
2001  * Flush all the callbacks we have on a server.
2002  */
2003 int afs_fs_give_up_all_callbacks(struct afs_net *net,
2004                                  struct afs_server *server,
2005                                  struct afs_addr_cursor *ac,
2006                                  struct key *key)
2007 {
2008         struct afs_call *call;
2009         __be32 *bp;
2010
2011         _enter("");
2012
2013         call = afs_alloc_flat_call(net, &afs_RXFSGiveUpAllCallBacks, 1 * 4, 0);
2014         if (!call)
2015                 return -ENOMEM;
2016
2017         call->key = key;
2018
2019         /* marshall the parameters */
2020         bp = call->request;
2021         *bp++ = htonl(FSGIVEUPALLCALLBACKS);
2022
2023         /* Can't take a ref on server */
2024         afs_make_call(ac, call, GFP_NOFS);
2025         return afs_wait_for_call_to_complete(call, ac);
2026 }
2027
2028 /*
2029  * Deliver reply data to an FS.GetCapabilities operation.
2030  */
2031 static int afs_deliver_fs_get_capabilities(struct afs_call *call)
2032 {
2033         u32 count;
2034         int ret;
2035
2036         _enter("{%u,%zu}", call->unmarshall, iov_iter_count(&call->iter));
2037
2038         switch (call->unmarshall) {
2039         case 0:
2040                 afs_extract_to_tmp(call);
2041                 call->unmarshall++;
2042
2043                 /* Extract the capabilities word count */
2044         case 1:
2045                 ret = afs_extract_data(call, true);
2046                 if (ret < 0)
2047                         return ret;
2048
2049                 count = ntohl(call->tmp);
2050
2051                 call->count = count;
2052                 call->count2 = count;
2053                 iov_iter_discard(&call->iter, READ, count * sizeof(__be32));
2054                 call->unmarshall++;
2055
2056                 /* Extract capabilities words */
2057         case 2:
2058                 ret = afs_extract_data(call, false);
2059                 if (ret < 0)
2060                         return ret;
2061
2062                 /* TODO: Examine capabilities */
2063
2064                 call->unmarshall++;
2065                 break;
2066         }
2067
2068         _leave(" = 0 [done]");
2069         return 0;
2070 }
2071
2072 static void afs_destroy_fs_get_capabilities(struct afs_call *call)
2073 {
2074         struct afs_server *server = call->reply[0];
2075
2076         afs_put_server(call->net, server);
2077         afs_flat_call_destructor(call);
2078 }
2079
2080 /*
2081  * FS.GetCapabilities operation type
2082  */
2083 static const struct afs_call_type afs_RXFSGetCapabilities = {
2084         .name           = "FS.GetCapabilities",
2085         .op             = afs_FS_GetCapabilities,
2086         .deliver        = afs_deliver_fs_get_capabilities,
2087         .done           = afs_fileserver_probe_result,
2088         .destructor     = afs_destroy_fs_get_capabilities,
2089 };
2090
2091 /*
2092  * Probe a fileserver for the capabilities that it supports.  This can
2093  * return up to 196 words.
2094  */
2095 struct afs_call *afs_fs_get_capabilities(struct afs_net *net,
2096                                          struct afs_server *server,
2097                                          struct afs_addr_cursor *ac,
2098                                          struct key *key,
2099                                          unsigned int server_index)
2100 {
2101         struct afs_call *call;
2102         __be32 *bp;
2103
2104         _enter("");
2105
2106         call = afs_alloc_flat_call(net, &afs_RXFSGetCapabilities, 1 * 4, 16 * 4);
2107         if (!call)
2108                 return ERR_PTR(-ENOMEM);
2109
2110         call->key = key;
2111         call->reply[0] = afs_get_server(server);
2112         call->reply[1] = (void *)(long)server_index;
2113         call->upgrade = true;
2114         call->want_reply_time = true;
2115         call->async = true;
2116
2117         /* marshall the parameters */
2118         bp = call->request;
2119         *bp++ = htonl(FSGETCAPABILITIES);
2120
2121         /* Can't take a ref on server */
2122         trace_afs_make_fs_call(call, NULL);
2123         afs_make_call(ac, call, GFP_NOFS);
2124         return call;
2125 }
2126
2127 /*
2128  * Deliver reply data to an FS.FetchStatus with no vnode.
2129  */
2130 static int afs_deliver_fs_fetch_status(struct afs_call *call)
2131 {
2132         struct afs_file_status *status = call->reply[1];
2133         struct afs_callback *callback = call->reply[2];
2134         struct afs_volsync *volsync = call->reply[3];
2135         struct afs_fid *fid = call->reply[0];
2136         const __be32 *bp;
2137         int ret;
2138
2139         ret = afs_transfer_reply(call);
2140         if (ret < 0)
2141                 return ret;
2142
2143         _enter("{%llx:%llu}", fid->vid, fid->vnode);
2144
2145         /* unmarshall the reply once we've received all of it */
2146         bp = call->buffer;
2147         ret = afs_decode_status(call, &bp, status, NULL,
2148                                 &call->expected_version, NULL);
2149         if (ret < 0)
2150                 return ret;
2151         xdr_decode_AFSCallBack_raw(call, &bp, callback);
2152         xdr_decode_AFSVolSync(&bp, volsync);
2153
2154         _leave(" = 0 [done]");
2155         return 0;
2156 }
2157
2158 /*
2159  * FS.FetchStatus operation type
2160  */
2161 static const struct afs_call_type afs_RXFSFetchStatus = {
2162         .name           = "FS.FetchStatus",
2163         .op             = afs_FS_FetchStatus,
2164         .deliver        = afs_deliver_fs_fetch_status,
2165         .destructor     = afs_flat_call_destructor,
2166 };
2167
2168 /*
2169  * Fetch the status information for a fid without needing a vnode handle.
2170  */
2171 int afs_fs_fetch_status(struct afs_fs_cursor *fc,
2172                         struct afs_net *net,
2173                         struct afs_fid *fid,
2174                         struct afs_file_status *status,
2175                         struct afs_callback *callback,
2176                         struct afs_volsync *volsync)
2177 {
2178         struct afs_call *call;
2179         __be32 *bp;
2180
2181         if (test_bit(AFS_SERVER_FL_IS_YFS, &fc->cbi->server->flags))
2182                 return yfs_fs_fetch_status(fc, net, fid, status, callback, volsync);
2183
2184         _enter(",%x,{%llx:%llu},,",
2185                key_serial(fc->key), fid->vid, fid->vnode);
2186
2187         call = afs_alloc_flat_call(net, &afs_RXFSFetchStatus, 16, (21 + 3 + 6) * 4);
2188         if (!call) {
2189                 fc->ac.error = -ENOMEM;
2190                 return -ENOMEM;
2191         }
2192
2193         call->key = fc->key;
2194         call->reply[0] = fid;
2195         call->reply[1] = status;
2196         call->reply[2] = callback;
2197         call->reply[3] = volsync;
2198         call->expected_version = 1; /* vnode->status.data_version */
2199         call->want_reply_time = true;
2200
2201         /* marshall the parameters */
2202         bp = call->request;
2203         bp[0] = htonl(FSFETCHSTATUS);
2204         bp[1] = htonl(fid->vid);
2205         bp[2] = htonl(fid->vnode);
2206         bp[3] = htonl(fid->unique);
2207
2208         call->cb_break = fc->cb_break;
2209         afs_use_fs_server(call, fc->cbi);
2210         trace_afs_make_fs_call(call, fid);
2211         afs_make_call(&fc->ac, call, GFP_NOFS);
2212         return afs_wait_for_call_to_complete(call, &fc->ac);
2213 }
2214
2215 /*
2216  * Deliver reply data to an FS.InlineBulkStatus call
2217  */
2218 static int afs_deliver_fs_inline_bulk_status(struct afs_call *call)
2219 {
2220         struct afs_file_status *statuses;
2221         struct afs_callback *callbacks;
2222         struct afs_vnode *vnode = call->reply[0];
2223         const __be32 *bp;
2224         u32 tmp;
2225         int ret;
2226
2227         _enter("{%u}", call->unmarshall);
2228
2229         switch (call->unmarshall) {
2230         case 0:
2231                 afs_extract_to_tmp(call);
2232                 call->unmarshall++;
2233
2234                 /* Extract the file status count and array in two steps */
2235         case 1:
2236                 _debug("extract status count");
2237                 ret = afs_extract_data(call, true);
2238                 if (ret < 0)
2239                         return ret;
2240
2241                 tmp = ntohl(call->tmp);
2242                 _debug("status count: %u/%u", tmp, call->count2);
2243                 if (tmp != call->count2)
2244                         return afs_protocol_error(call, -EBADMSG,
2245                                                   afs_eproto_ibulkst_count);
2246
2247                 call->count = 0;
2248                 call->unmarshall++;
2249         more_counts:
2250                 afs_extract_to_buf(call, 21 * sizeof(__be32));
2251
2252         case 2:
2253                 _debug("extract status array %u", call->count);
2254                 ret = afs_extract_data(call, true);
2255                 if (ret < 0)
2256                         return ret;
2257
2258                 bp = call->buffer;
2259                 statuses = call->reply[1];
2260                 ret = afs_decode_status(call, &bp, &statuses[call->count],
2261                                         call->count == 0 ? vnode : NULL,
2262                                         NULL, NULL);
2263                 if (ret < 0)
2264                         return ret;
2265
2266                 call->count++;
2267                 if (call->count < call->count2)
2268                         goto more_counts;
2269
2270                 call->count = 0;
2271                 call->unmarshall++;
2272                 afs_extract_to_tmp(call);
2273
2274                 /* Extract the callback count and array in two steps */
2275         case 3:
2276                 _debug("extract CB count");
2277                 ret = afs_extract_data(call, true);
2278                 if (ret < 0)
2279                         return ret;
2280
2281                 tmp = ntohl(call->tmp);
2282                 _debug("CB count: %u", tmp);
2283                 if (tmp != call->count2)
2284                         return afs_protocol_error(call, -EBADMSG,
2285                                                   afs_eproto_ibulkst_cb_count);
2286                 call->count = 0;
2287                 call->unmarshall++;
2288         more_cbs:
2289                 afs_extract_to_buf(call, 3 * sizeof(__be32));
2290
2291         case 4:
2292                 _debug("extract CB array");
2293                 ret = afs_extract_data(call, true);
2294                 if (ret < 0)
2295                         return ret;
2296
2297                 _debug("unmarshall CB array");
2298                 bp = call->buffer;
2299                 callbacks = call->reply[2];
2300                 callbacks[call->count].version  = ntohl(bp[0]);
2301                 callbacks[call->count].expires_at = xdr_decode_expiry(call, ntohl(bp[1]));
2302                 callbacks[call->count].type     = ntohl(bp[2]);
2303                 statuses = call->reply[1];
2304                 if (call->count == 0 && vnode && statuses[0].abort_code == 0)
2305                         xdr_decode_AFSCallBack(call, vnode, &bp);
2306                 call->count++;
2307                 if (call->count < call->count2)
2308                         goto more_cbs;
2309
2310                 afs_extract_to_buf(call, 6 * sizeof(__be32));
2311                 call->unmarshall++;
2312
2313         case 5:
2314                 ret = afs_extract_data(call, false);
2315                 if (ret < 0)
2316                         return ret;
2317
2318                 bp = call->buffer;
2319                 xdr_decode_AFSVolSync(&bp, call->reply[3]);
2320
2321                 call->unmarshall++;
2322
2323         case 6:
2324                 break;
2325         }
2326
2327         _leave(" = 0 [done]");
2328         return 0;
2329 }
2330
2331 /*
2332  * FS.InlineBulkStatus operation type
2333  */
2334 static const struct afs_call_type afs_RXFSInlineBulkStatus = {
2335         .name           = "FS.InlineBulkStatus",
2336         .op             = afs_FS_InlineBulkStatus,
2337         .deliver        = afs_deliver_fs_inline_bulk_status,
2338         .destructor     = afs_flat_call_destructor,
2339 };
2340
2341 /*
2342  * Fetch the status information for up to 50 files
2343  */
2344 int afs_fs_inline_bulk_status(struct afs_fs_cursor *fc,
2345                               struct afs_net *net,
2346                               struct afs_fid *fids,
2347                               struct afs_file_status *statuses,
2348                               struct afs_callback *callbacks,
2349                               unsigned int nr_fids,
2350                               struct afs_volsync *volsync)
2351 {
2352         struct afs_call *call;
2353         __be32 *bp;
2354         int i;
2355
2356         if (test_bit(AFS_SERVER_FL_IS_YFS, &fc->cbi->server->flags))
2357                 return yfs_fs_inline_bulk_status(fc, net, fids, statuses, callbacks,
2358                                                  nr_fids, volsync);
2359
2360         _enter(",%x,{%llx:%llu},%u",
2361                key_serial(fc->key), fids[0].vid, fids[1].vnode, nr_fids);
2362
2363         call = afs_alloc_flat_call(net, &afs_RXFSInlineBulkStatus,
2364                                    (2 + nr_fids * 3) * 4,
2365                                    21 * 4);
2366         if (!call) {
2367                 fc->ac.error = -ENOMEM;
2368                 return -ENOMEM;
2369         }
2370
2371         call->key = fc->key;
2372         call->reply[0] = NULL; /* vnode for fid[0] */
2373         call->reply[1] = statuses;
2374         call->reply[2] = callbacks;
2375         call->reply[3] = volsync;
2376         call->count2 = nr_fids;
2377         call->want_reply_time = true;
2378
2379         /* marshall the parameters */
2380         bp = call->request;
2381         *bp++ = htonl(FSINLINEBULKSTATUS);
2382         *bp++ = htonl(nr_fids);
2383         for (i = 0; i < nr_fids; i++) {
2384                 *bp++ = htonl(fids[i].vid);
2385                 *bp++ = htonl(fids[i].vnode);
2386                 *bp++ = htonl(fids[i].unique);
2387         }
2388
2389         call->cb_break = fc->cb_break;
2390         afs_use_fs_server(call, fc->cbi);
2391         trace_afs_make_fs_call(call, &fids[0]);
2392         afs_make_call(&fc->ac, call, GFP_NOFS);
2393         return afs_wait_for_call_to_complete(call, &fc->ac);
2394 }
2395
2396 /*
2397  * deliver reply data to an FS.FetchACL
2398  */
2399 static int afs_deliver_fs_fetch_acl(struct afs_call *call)
2400 {
2401         struct afs_vnode *vnode = call->reply[1];
2402         struct afs_acl *acl;
2403         const __be32 *bp;
2404         unsigned int size;
2405         int ret;
2406
2407         _enter("{%u}", call->unmarshall);
2408
2409         switch (call->unmarshall) {
2410         case 0:
2411                 afs_extract_to_tmp(call);
2412                 call->unmarshall++;
2413
2414                 /* extract the returned data length */
2415         case 1:
2416                 ret = afs_extract_data(call, true);
2417                 if (ret < 0)
2418                         return ret;
2419
2420                 size = call->count2 = ntohl(call->tmp);
2421                 size = round_up(size, 4);
2422
2423                 acl = kmalloc(struct_size(acl, data, size), GFP_KERNEL);
2424                 if (!acl)
2425                         return -ENOMEM;
2426                 call->reply[0] = acl;
2427                 acl->size = call->count2;
2428                 afs_extract_begin(call, acl->data, size);
2429                 call->unmarshall++;
2430
2431                 /* extract the returned data */
2432         case 2:
2433                 ret = afs_extract_data(call, true);
2434                 if (ret < 0)
2435                         return ret;
2436
2437                 afs_extract_to_buf(call, (21 + 6) * 4);
2438                 call->unmarshall++;
2439
2440                 /* extract the metadata */
2441         case 3:
2442                 ret = afs_extract_data(call, false);
2443                 if (ret < 0)
2444                         return ret;
2445
2446                 bp = call->buffer;
2447                 ret = afs_decode_status(call, &bp, &vnode->status, vnode,
2448                                         &vnode->status.data_version, NULL);
2449                 if (ret < 0)
2450                         return ret;
2451                 xdr_decode_AFSVolSync(&bp, call->reply[2]);
2452
2453                 call->unmarshall++;
2454
2455         case 4:
2456                 break;
2457         }
2458
2459         _leave(" = 0 [done]");
2460         return 0;
2461 }
2462
2463 static void afs_destroy_fs_fetch_acl(struct afs_call *call)
2464 {
2465         kfree(call->reply[0]);
2466         afs_flat_call_destructor(call);
2467 }
2468
2469 /*
2470  * FS.FetchACL operation type
2471  */
2472 static const struct afs_call_type afs_RXFSFetchACL = {
2473         .name           = "FS.FetchACL",
2474         .op             = afs_FS_FetchACL,
2475         .deliver        = afs_deliver_fs_fetch_acl,
2476         .destructor     = afs_destroy_fs_fetch_acl,
2477 };
2478
2479 /*
2480  * Fetch the ACL for a file.
2481  */
2482 struct afs_acl *afs_fs_fetch_acl(struct afs_fs_cursor *fc)
2483 {
2484         struct afs_vnode *vnode = fc->vnode;
2485         struct afs_call *call;
2486         struct afs_net *net = afs_v2net(vnode);
2487         __be32 *bp;
2488
2489         _enter(",%x,{%llx:%llu},,",
2490                key_serial(fc->key), vnode->fid.vid, vnode->fid.vnode);
2491
2492         call = afs_alloc_flat_call(net, &afs_RXFSFetchACL, 16, (21 + 6) * 4);
2493         if (!call) {
2494                 fc->ac.error = -ENOMEM;
2495                 return ERR_PTR(-ENOMEM);
2496         }
2497
2498         call->key = fc->key;
2499         call->reply[0] = NULL;
2500         call->reply[1] = vnode;
2501         call->reply[2] = NULL; /* volsync */
2502         call->ret_reply0 = true;
2503
2504         /* marshall the parameters */
2505         bp = call->request;
2506         bp[0] = htonl(FSFETCHACL);
2507         bp[1] = htonl(vnode->fid.vid);
2508         bp[2] = htonl(vnode->fid.vnode);
2509         bp[3] = htonl(vnode->fid.unique);
2510
2511         call->cb_break = fc->cb_break;
2512         afs_use_fs_server(call, fc->cbi);
2513         trace_afs_make_fs_call(call, &vnode->fid);
2514         afs_make_call(&fc->ac, call, GFP_KERNEL);
2515         return (struct afs_acl *)afs_wait_for_call_to_complete(call, &fc->ac);
2516 }
2517
2518 /*
2519  * FS.StoreACL operation type
2520  */
2521 static const struct afs_call_type afs_RXFSStoreACL = {
2522         .name           = "FS.StoreACL",
2523         .op             = afs_FS_StoreACL,
2524         .deliver        = afs_deliver_fs_status_and_vol,
2525         .destructor     = afs_flat_call_destructor,
2526 };
2527
2528 /*
2529  * Fetch the ACL for a file.
2530  */
2531 int afs_fs_store_acl(struct afs_fs_cursor *fc, const struct afs_acl *acl)
2532 {
2533         struct afs_vnode *vnode = fc->vnode;
2534         struct afs_call *call;
2535         struct afs_net *net = afs_v2net(vnode);
2536         size_t size;
2537         __be32 *bp;
2538
2539         _enter(",%x,{%llx:%llu},,",
2540                key_serial(fc->key), vnode->fid.vid, vnode->fid.vnode);
2541
2542         size = round_up(acl->size, 4);
2543         call = afs_alloc_flat_call(net, &afs_RXFSStoreACL,
2544                                    5 * 4 + size, (21 + 6) * 4);
2545         if (!call) {
2546                 fc->ac.error = -ENOMEM;
2547                 return -ENOMEM;
2548         }
2549
2550         call->key = fc->key;
2551         call->reply[0] = vnode;
2552         call->reply[2] = NULL; /* volsync */
2553
2554         /* marshall the parameters */
2555         bp = call->request;
2556         bp[0] = htonl(FSSTOREACL);
2557         bp[1] = htonl(vnode->fid.vid);
2558         bp[2] = htonl(vnode->fid.vnode);
2559         bp[3] = htonl(vnode->fid.unique);
2560         bp[4] = htonl(acl->size);
2561         memcpy(&bp[5], acl->data, acl->size);
2562         if (acl->size != size)
2563                 memset((void *)&bp[5] + acl->size, 0, size - acl->size);
2564
2565         trace_afs_make_fs_call(call, &vnode->fid);
2566         afs_make_call(&fc->ac, call, GFP_KERNEL);
2567         return afs_wait_for_call_to_complete(call, &fc->ac);
2568 }