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