afs: Get rid of afs_call::reply[]
[linux-2.6-microblaze.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->xvnode;
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->out_volsync);
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->xvnode = vnode;
508         call->out_volsync = 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->xvnode;
532         struct afs_read *req = call->read_request;
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->out_volsync);
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         afs_put_read(call->read_request);
656         afs_flat_call_destructor(call);
657 }
658
659 /*
660  * YFS.FetchData64 operation type
661  */
662 static const struct afs_call_type yfs_RXYFSFetchData64 = {
663         .name           = "YFS.FetchData64",
664         .op             = yfs_FS_FetchData64,
665         .deliver        = yfs_deliver_fs_fetch_data64,
666         .destructor     = yfs_fetch_data_destructor,
667 };
668
669 /*
670  * Fetch data from a file.
671  */
672 int yfs_fs_fetch_data(struct afs_fs_cursor *fc, struct afs_read *req)
673 {
674         struct afs_vnode *vnode = fc->vnode;
675         struct afs_call *call;
676         struct afs_net *net = afs_v2net(vnode);
677         __be32 *bp;
678
679         _enter(",%x,{%llx:%llu},%llx,%llx",
680                key_serial(fc->key), vnode->fid.vid, vnode->fid.vnode,
681                req->pos, req->len);
682
683         call = afs_alloc_flat_call(net, &yfs_RXYFSFetchData64,
684                                    sizeof(__be32) * 2 +
685                                    sizeof(struct yfs_xdr_YFSFid) +
686                                    sizeof(struct yfs_xdr_u64) * 2,
687                                    sizeof(struct yfs_xdr_YFSFetchStatus) +
688                                    sizeof(struct yfs_xdr_YFSCallBack) +
689                                    sizeof(struct yfs_xdr_YFSVolSync));
690         if (!call)
691                 return -ENOMEM;
692
693         call->key = fc->key;
694         call->xvnode = vnode;
695         call->out_volsync = NULL;
696         call->read_request = req;
697         call->expected_version = vnode->status.data_version;
698         call->want_reply_time = true;
699
700         /* marshall the parameters */
701         bp = call->request;
702         bp = xdr_encode_u32(bp, YFSFETCHDATA64);
703         bp = xdr_encode_u32(bp, 0); /* RPC flags */
704         bp = xdr_encode_YFSFid(bp, &vnode->fid);
705         bp = xdr_encode_u64(bp, req->pos);
706         bp = xdr_encode_u64(bp, req->len);
707         yfs_check_req(call, bp);
708
709         refcount_inc(&req->usage);
710         call->cb_break = fc->cb_break;
711         afs_use_fs_server(call, fc->cbi);
712         trace_afs_make_fs_call(call, &vnode->fid);
713         afs_set_fc_call(call, fc);
714         afs_make_call(&fc->ac, call, GFP_NOFS);
715         return afs_wait_for_call_to_complete(call, &fc->ac);
716 }
717
718 /*
719  * Deliver reply data for YFS.CreateFile or YFS.MakeDir.
720  */
721 static int yfs_deliver_fs_create_vnode(struct afs_call *call)
722 {
723         struct afs_vnode *dvnode = call->dvnode;
724         const __be32 *bp;
725         int ret;
726
727         _enter("{%u}", call->unmarshall);
728
729         ret = afs_transfer_reply(call);
730         if (ret < 0)
731                 return ret;
732
733         /* unmarshall the reply once we've received all of it */
734         bp = call->buffer;
735         xdr_decode_YFSFid(&bp, call->out_fid);
736         ret = yfs_decode_status(call, &bp, call->out_extra_status, NULL, NULL, NULL);
737         if (ret < 0)
738                 return ret;
739         ret = yfs_decode_status(call, &bp, &dvnode->status, dvnode,
740                                 &call->expected_version, NULL);
741         if (ret < 0)
742                 return ret;
743         xdr_decode_YFSCallBack_raw(call, call->out_cb, &bp);
744         xdr_decode_YFSVolSync(&bp, NULL);
745
746         _leave(" = 0 [done]");
747         return 0;
748 }
749
750 /*
751  * FS.CreateFile and FS.MakeDir operation type
752  */
753 static const struct afs_call_type afs_RXFSCreateFile = {
754         .name           = "YFS.CreateFile",
755         .op             = yfs_FS_CreateFile,
756         .deliver        = yfs_deliver_fs_create_vnode,
757         .destructor     = afs_flat_call_destructor,
758 };
759
760 /*
761  * Create a file.
762  */
763 int yfs_fs_create_file(struct afs_fs_cursor *fc,
764                        const char *name,
765                        umode_t mode,
766                        u64 current_data_version,
767                        struct afs_fid *newfid,
768                        struct afs_file_status *newstatus,
769                        struct afs_callback *newcb)
770 {
771         struct afs_vnode *dvnode = fc->vnode;
772         struct afs_call *call;
773         struct afs_net *net = afs_v2net(dvnode);
774         size_t namesz, reqsz, rplsz;
775         __be32 *bp;
776
777         _enter("");
778
779         namesz = strlen(name);
780         reqsz = (sizeof(__be32) +
781                  sizeof(__be32) +
782                  sizeof(struct yfs_xdr_YFSFid) +
783                  xdr_strlen(namesz) +
784                  sizeof(struct yfs_xdr_YFSStoreStatus) +
785                  sizeof(__be32));
786         rplsz = (sizeof(struct yfs_xdr_YFSFid) +
787                  sizeof(struct yfs_xdr_YFSFetchStatus) +
788                  sizeof(struct yfs_xdr_YFSFetchStatus) +
789                  sizeof(struct yfs_xdr_YFSCallBack) +
790                  sizeof(struct yfs_xdr_YFSVolSync));
791
792         call = afs_alloc_flat_call(net, &afs_RXFSCreateFile, reqsz, rplsz);
793         if (!call)
794                 return -ENOMEM;
795
796         call->key = fc->key;
797         call->dvnode = dvnode;
798         call->out_fid = newfid;
799         call->out_extra_status = newstatus;
800         call->out_cb = newcb;
801         call->expected_version = current_data_version + 1;
802
803         /* marshall the parameters */
804         bp = call->request;
805         bp = xdr_encode_u32(bp, YFSCREATEFILE);
806         bp = xdr_encode_u32(bp, 0); /* RPC flags */
807         bp = xdr_encode_YFSFid(bp, &dvnode->fid);
808         bp = xdr_encode_string(bp, name, namesz);
809         bp = xdr_encode_YFSStoreStatus_mode(bp, mode);
810         bp = xdr_encode_u32(bp, yfs_LockNone); /* ViceLockType */
811         yfs_check_req(call, bp);
812
813         afs_use_fs_server(call, fc->cbi);
814         trace_afs_make_fs_call1(call, &dvnode->fid, name);
815         afs_set_fc_call(call, fc);
816         afs_make_call(&fc->ac, call, GFP_NOFS);
817         return afs_wait_for_call_to_complete(call, &fc->ac);
818 }
819
820 static const struct afs_call_type yfs_RXFSMakeDir = {
821         .name           = "YFS.MakeDir",
822         .op             = yfs_FS_MakeDir,
823         .deliver        = yfs_deliver_fs_create_vnode,
824         .destructor     = afs_flat_call_destructor,
825 };
826
827 /*
828  * Make a directory.
829  */
830 int yfs_fs_make_dir(struct afs_fs_cursor *fc,
831                     const char *name,
832                     umode_t mode,
833                     u64 current_data_version,
834                     struct afs_fid *newfid,
835                     struct afs_file_status *newstatus,
836                     struct afs_callback *newcb)
837 {
838         struct afs_vnode *dvnode = fc->vnode;
839         struct afs_call *call;
840         struct afs_net *net = afs_v2net(dvnode);
841         size_t namesz, reqsz, rplsz;
842         __be32 *bp;
843
844         _enter("");
845
846         namesz = strlen(name);
847         reqsz = (sizeof(__be32) +
848                  sizeof(struct yfs_xdr_RPCFlags) +
849                  sizeof(struct yfs_xdr_YFSFid) +
850                  xdr_strlen(namesz) +
851                  sizeof(struct yfs_xdr_YFSStoreStatus));
852         rplsz = (sizeof(struct yfs_xdr_YFSFid) +
853                  sizeof(struct yfs_xdr_YFSFetchStatus) +
854                  sizeof(struct yfs_xdr_YFSFetchStatus) +
855                  sizeof(struct yfs_xdr_YFSCallBack) +
856                  sizeof(struct yfs_xdr_YFSVolSync));
857
858         call = afs_alloc_flat_call(net, &yfs_RXFSMakeDir, reqsz, rplsz);
859         if (!call)
860                 return -ENOMEM;
861
862         call->key = fc->key;
863         call->dvnode = dvnode;
864         call->out_fid = newfid;
865         call->out_extra_status = newstatus;
866         call->out_cb = newcb;
867         call->expected_version = current_data_version + 1;
868
869         /* marshall the parameters */
870         bp = call->request;
871         bp = xdr_encode_u32(bp, YFSMAKEDIR);
872         bp = xdr_encode_u32(bp, 0); /* RPC flags */
873         bp = xdr_encode_YFSFid(bp, &dvnode->fid);
874         bp = xdr_encode_string(bp, name, namesz);
875         bp = xdr_encode_YFSStoreStatus_mode(bp, mode);
876         yfs_check_req(call, bp);
877
878         afs_use_fs_server(call, fc->cbi);
879         trace_afs_make_fs_call1(call, &dvnode->fid, name);
880         afs_set_fc_call(call, fc);
881         afs_make_call(&fc->ac, call, GFP_NOFS);
882         return afs_wait_for_call_to_complete(call, &fc->ac);
883 }
884
885 /*
886  * Deliver reply data to a YFS.RemoveFile2 operation.
887  */
888 static int yfs_deliver_fs_remove_file2(struct afs_call *call)
889 {
890         struct afs_vnode *dvnode = call->dvnode;
891         struct afs_vnode *vnode = call->xvnode;
892         struct afs_fid fid;
893         const __be32 *bp;
894         int ret;
895
896         _enter("{%u}", call->unmarshall);
897
898         ret = afs_transfer_reply(call);
899         if (ret < 0)
900                 return ret;
901
902         /* unmarshall the reply once we've received all of it */
903         bp = call->buffer;
904         ret = yfs_decode_status(call, &bp, &dvnode->status, dvnode,
905                                 &call->expected_version, NULL);
906         if (ret < 0)
907                 return ret;
908
909         xdr_decode_YFSFid(&bp, &fid);
910         ret = yfs_decode_status(call, &bp, &vnode->status, vnode, NULL, NULL);
911         if (ret < 0)
912                 return ret;
913         /* Was deleted if vnode->status.abort_code == VNOVNODE. */
914
915         xdr_decode_YFSVolSync(&bp, NULL);
916         return 0;
917 }
918
919 /*
920  * YFS.RemoveFile2 operation type.
921  */
922 static const struct afs_call_type yfs_RXYFSRemoveFile2 = {
923         .name           = "YFS.RemoveFile2",
924         .op             = yfs_FS_RemoveFile2,
925         .deliver        = yfs_deliver_fs_remove_file2,
926         .destructor     = afs_flat_call_destructor,
927 };
928
929 /*
930  * Remove a file and retrieve new file status.
931  */
932 int yfs_fs_remove_file2(struct afs_fs_cursor *fc, struct afs_vnode *vnode,
933                         const char *name, u64 current_data_version)
934 {
935         struct afs_vnode *dvnode = fc->vnode;
936         struct afs_call *call;
937         struct afs_net *net = afs_v2net(dvnode);
938         size_t namesz;
939         __be32 *bp;
940
941         _enter("");
942
943         namesz = strlen(name);
944
945         call = afs_alloc_flat_call(net, &yfs_RXYFSRemoveFile2,
946                                    sizeof(__be32) +
947                                    sizeof(struct yfs_xdr_RPCFlags) +
948                                    sizeof(struct yfs_xdr_YFSFid) +
949                                    xdr_strlen(namesz),
950                                    sizeof(struct yfs_xdr_YFSFetchStatus) +
951                                    sizeof(struct yfs_xdr_YFSFid) +
952                                    sizeof(struct yfs_xdr_YFSFetchStatus) +
953                                    sizeof(struct yfs_xdr_YFSVolSync));
954         if (!call)
955                 return -ENOMEM;
956
957         call->key = fc->key;
958         call->dvnode = dvnode;
959         call->xvnode = vnode;
960         call->expected_version = current_data_version + 1;
961
962         /* marshall the parameters */
963         bp = call->request;
964         bp = xdr_encode_u32(bp, YFSREMOVEFILE2);
965         bp = xdr_encode_u32(bp, 0); /* RPC flags */
966         bp = xdr_encode_YFSFid(bp, &dvnode->fid);
967         bp = xdr_encode_string(bp, name, namesz);
968         yfs_check_req(call, bp);
969
970         afs_use_fs_server(call, fc->cbi);
971         trace_afs_make_fs_call1(call, &dvnode->fid, name);
972         afs_set_fc_call(call, fc);
973         afs_make_call(&fc->ac, call, GFP_NOFS);
974         return afs_wait_for_call_to_complete(call, &fc->ac);
975 }
976
977 /*
978  * Deliver reply data to a YFS.RemoveFile or YFS.RemoveDir operation.
979  */
980 static int yfs_deliver_fs_remove(struct afs_call *call)
981 {
982         struct afs_vnode *dvnode = call->dvnode;
983         const __be32 *bp;
984         int ret;
985
986         _enter("{%u}", call->unmarshall);
987
988         ret = afs_transfer_reply(call);
989         if (ret < 0)
990                 return ret;
991
992         /* unmarshall the reply once we've received all of it */
993         bp = call->buffer;
994         ret = yfs_decode_status(call, &bp, &dvnode->status, dvnode,
995                                 &call->expected_version, NULL);
996         if (ret < 0)
997                 return ret;
998
999         xdr_decode_YFSVolSync(&bp, NULL);
1000         return 0;
1001 }
1002
1003 /*
1004  * FS.RemoveDir and FS.RemoveFile operation types.
1005  */
1006 static const struct afs_call_type yfs_RXYFSRemoveFile = {
1007         .name           = "YFS.RemoveFile",
1008         .op             = yfs_FS_RemoveFile,
1009         .deliver        = yfs_deliver_fs_remove,
1010         .destructor     = afs_flat_call_destructor,
1011 };
1012
1013 static const struct afs_call_type yfs_RXYFSRemoveDir = {
1014         .name           = "YFS.RemoveDir",
1015         .op             = yfs_FS_RemoveDir,
1016         .deliver        = yfs_deliver_fs_remove,
1017         .destructor     = afs_flat_call_destructor,
1018 };
1019
1020 /*
1021  * remove a file or directory
1022  */
1023 int yfs_fs_remove(struct afs_fs_cursor *fc, struct afs_vnode *vnode,
1024                   const char *name, bool isdir, u64 current_data_version)
1025 {
1026         struct afs_vnode *dvnode = fc->vnode;
1027         struct afs_call *call;
1028         struct afs_net *net = afs_v2net(dvnode);
1029         size_t namesz;
1030         __be32 *bp;
1031
1032         _enter("");
1033
1034         namesz = strlen(name);
1035         call = afs_alloc_flat_call(
1036                 net, isdir ? &yfs_RXYFSRemoveDir : &yfs_RXYFSRemoveFile,
1037                 sizeof(__be32) +
1038                 sizeof(struct yfs_xdr_RPCFlags) +
1039                 sizeof(struct yfs_xdr_YFSFid) +
1040                 xdr_strlen(namesz),
1041                 sizeof(struct yfs_xdr_YFSFetchStatus) +
1042                 sizeof(struct yfs_xdr_YFSVolSync));
1043         if (!call)
1044                 return -ENOMEM;
1045
1046         call->key = fc->key;
1047         call->dvnode = dvnode;
1048         call->xvnode = vnode;
1049         call->expected_version = current_data_version + 1;
1050
1051         /* marshall the parameters */
1052         bp = call->request;
1053         bp = xdr_encode_u32(bp, isdir ? YFSREMOVEDIR : YFSREMOVEFILE);
1054         bp = xdr_encode_u32(bp, 0); /* RPC flags */
1055         bp = xdr_encode_YFSFid(bp, &dvnode->fid);
1056         bp = xdr_encode_string(bp, name, namesz);
1057         yfs_check_req(call, bp);
1058
1059         afs_use_fs_server(call, fc->cbi);
1060         trace_afs_make_fs_call1(call, &dvnode->fid, name);
1061         afs_set_fc_call(call, fc);
1062         afs_make_call(&fc->ac, call, GFP_NOFS);
1063         return afs_wait_for_call_to_complete(call, &fc->ac);
1064 }
1065
1066 /*
1067  * Deliver reply data to a YFS.Link operation.
1068  */
1069 static int yfs_deliver_fs_link(struct afs_call *call)
1070 {
1071         struct afs_vnode *dvnode = call->dvnode, *vnode = call->xvnode;
1072         const __be32 *bp;
1073         int ret;
1074
1075         _enter("{%u}", call->unmarshall);
1076
1077         ret = afs_transfer_reply(call);
1078         if (ret < 0)
1079                 return ret;
1080
1081         /* unmarshall the reply once we've received all of it */
1082         bp = call->buffer;
1083         ret = yfs_decode_status(call, &bp, &vnode->status, vnode, NULL, NULL);
1084         if (ret < 0)
1085                 return ret;
1086         ret = yfs_decode_status(call, &bp, &dvnode->status, dvnode,
1087                                 &call->expected_version, NULL);
1088         if (ret < 0)
1089                 return ret;
1090         xdr_decode_YFSVolSync(&bp, NULL);
1091         _leave(" = 0 [done]");
1092         return 0;
1093 }
1094
1095 /*
1096  * YFS.Link operation type.
1097  */
1098 static const struct afs_call_type yfs_RXYFSLink = {
1099         .name           = "YFS.Link",
1100         .op             = yfs_FS_Link,
1101         .deliver        = yfs_deliver_fs_link,
1102         .destructor     = afs_flat_call_destructor,
1103 };
1104
1105 /*
1106  * Make a hard link.
1107  */
1108 int yfs_fs_link(struct afs_fs_cursor *fc, struct afs_vnode *vnode,
1109                 const char *name, u64 current_data_version)
1110 {
1111         struct afs_vnode *dvnode = fc->vnode;
1112         struct afs_call *call;
1113         struct afs_net *net = afs_v2net(vnode);
1114         size_t namesz;
1115         __be32 *bp;
1116
1117         _enter("");
1118
1119         namesz = strlen(name);
1120         call = afs_alloc_flat_call(net, &yfs_RXYFSLink,
1121                                    sizeof(__be32) +
1122                                    sizeof(struct yfs_xdr_RPCFlags) +
1123                                    sizeof(struct yfs_xdr_YFSFid) +
1124                                    xdr_strlen(namesz) +
1125                                    sizeof(struct yfs_xdr_YFSFid),
1126                                    sizeof(struct yfs_xdr_YFSFetchStatus) +
1127                                    sizeof(struct yfs_xdr_YFSFetchStatus) +
1128                                    sizeof(struct yfs_xdr_YFSVolSync));
1129         if (!call)
1130                 return -ENOMEM;
1131
1132         call->key = fc->key;
1133         call->dvnode = dvnode;
1134         call->xvnode = vnode;
1135         call->expected_version = current_data_version + 1;
1136
1137         /* marshall the parameters */
1138         bp = call->request;
1139         bp = xdr_encode_u32(bp, YFSLINK);
1140         bp = xdr_encode_u32(bp, 0); /* RPC flags */
1141         bp = xdr_encode_YFSFid(bp, &dvnode->fid);
1142         bp = xdr_encode_string(bp, name, namesz);
1143         bp = xdr_encode_YFSFid(bp, &vnode->fid);
1144         yfs_check_req(call, bp);
1145
1146         afs_use_fs_server(call, fc->cbi);
1147         trace_afs_make_fs_call1(call, &vnode->fid, name);
1148         afs_set_fc_call(call, fc);
1149         afs_make_call(&fc->ac, call, GFP_NOFS);
1150         return afs_wait_for_call_to_complete(call, &fc->ac);
1151 }
1152
1153 /*
1154  * Deliver reply data to a YFS.Symlink operation.
1155  */
1156 static int yfs_deliver_fs_symlink(struct afs_call *call)
1157 {
1158         struct afs_vnode *dvnode = call->dvnode;
1159         const __be32 *bp;
1160         int ret;
1161
1162         _enter("{%u}", call->unmarshall);
1163
1164         ret = afs_transfer_reply(call);
1165         if (ret < 0)
1166                 return ret;
1167
1168         /* unmarshall the reply once we've received all of it */
1169         bp = call->buffer;
1170         xdr_decode_YFSFid(&bp, call->out_fid);
1171         ret = yfs_decode_status(call, &bp, call->out_extra_status, NULL, NULL, NULL);
1172         if (ret < 0)
1173                 return ret;
1174         ret = yfs_decode_status(call, &bp, &dvnode->status, dvnode,
1175                                 &call->expected_version, NULL);
1176         if (ret < 0)
1177                 return ret;
1178         xdr_decode_YFSVolSync(&bp, NULL);
1179
1180         _leave(" = 0 [done]");
1181         return 0;
1182 }
1183
1184 /*
1185  * YFS.Symlink operation type
1186  */
1187 static const struct afs_call_type yfs_RXYFSSymlink = {
1188         .name           = "YFS.Symlink",
1189         .op             = yfs_FS_Symlink,
1190         .deliver        = yfs_deliver_fs_symlink,
1191         .destructor     = afs_flat_call_destructor,
1192 };
1193
1194 /*
1195  * Create a symbolic link.
1196  */
1197 int yfs_fs_symlink(struct afs_fs_cursor *fc,
1198                    const char *name,
1199                    const char *contents,
1200                    u64 current_data_version,
1201                    struct afs_fid *newfid,
1202                    struct afs_file_status *newstatus)
1203 {
1204         struct afs_vnode *dvnode = fc->vnode;
1205         struct afs_call *call;
1206         struct afs_net *net = afs_v2net(dvnode);
1207         size_t namesz, contents_sz;
1208         __be32 *bp;
1209
1210         _enter("");
1211
1212         namesz = strlen(name);
1213         contents_sz = strlen(contents);
1214         call = afs_alloc_flat_call(net, &yfs_RXYFSSymlink,
1215                                    sizeof(__be32) +
1216                                    sizeof(struct yfs_xdr_RPCFlags) +
1217                                    sizeof(struct yfs_xdr_YFSFid) +
1218                                    xdr_strlen(namesz) +
1219                                    xdr_strlen(contents_sz) +
1220                                    sizeof(struct yfs_xdr_YFSStoreStatus),
1221                                    sizeof(struct yfs_xdr_YFSFid) +
1222                                    sizeof(struct yfs_xdr_YFSFetchStatus) +
1223                                    sizeof(struct yfs_xdr_YFSFetchStatus) +
1224                                    sizeof(struct yfs_xdr_YFSVolSync));
1225         if (!call)
1226                 return -ENOMEM;
1227
1228         call->key = fc->key;
1229         call->dvnode = dvnode;
1230         call->out_fid = newfid;
1231         call->out_extra_status = newstatus;
1232         call->expected_version = current_data_version + 1;
1233
1234         /* marshall the parameters */
1235         bp = call->request;
1236         bp = xdr_encode_u32(bp, YFSSYMLINK);
1237         bp = xdr_encode_u32(bp, 0); /* RPC flags */
1238         bp = xdr_encode_YFSFid(bp, &dvnode->fid);
1239         bp = xdr_encode_string(bp, name, namesz);
1240         bp = xdr_encode_string(bp, contents, contents_sz);
1241         bp = xdr_encode_YFSStoreStatus_mode(bp, S_IRWXUGO);
1242         yfs_check_req(call, bp);
1243
1244         afs_use_fs_server(call, fc->cbi);
1245         trace_afs_make_fs_call1(call, &dvnode->fid, name);
1246         afs_set_fc_call(call, fc);
1247         afs_make_call(&fc->ac, call, GFP_NOFS);
1248         return afs_wait_for_call_to_complete(call, &fc->ac);
1249 }
1250
1251 /*
1252  * Deliver reply data to a YFS.Rename operation.
1253  */
1254 static int yfs_deliver_fs_rename(struct afs_call *call)
1255 {
1256         struct afs_vnode *orig_dvnode = call->dvnode;
1257         struct afs_vnode *new_dvnode = call->xvnode;
1258         const __be32 *bp;
1259         int ret;
1260
1261         _enter("{%u}", call->unmarshall);
1262
1263         ret = afs_transfer_reply(call);
1264         if (ret < 0)
1265                 return ret;
1266
1267         /* unmarshall the reply once we've received all of it */
1268         bp = call->buffer;
1269         ret = yfs_decode_status(call, &bp, &orig_dvnode->status, orig_dvnode,
1270                                 &call->expected_version, NULL);
1271         if (ret < 0)
1272                 return ret;
1273         if (new_dvnode != orig_dvnode) {
1274                 ret = yfs_decode_status(call, &bp, &new_dvnode->status, new_dvnode,
1275                                         &call->expected_version_2, NULL);
1276                 if (ret < 0)
1277                         return ret;
1278         }
1279
1280         xdr_decode_YFSVolSync(&bp, NULL);
1281         _leave(" = 0 [done]");
1282         return 0;
1283 }
1284
1285 /*
1286  * YFS.Rename operation type
1287  */
1288 static const struct afs_call_type yfs_RXYFSRename = {
1289         .name           = "FS.Rename",
1290         .op             = yfs_FS_Rename,
1291         .deliver        = yfs_deliver_fs_rename,
1292         .destructor     = afs_flat_call_destructor,
1293 };
1294
1295 /*
1296  * Rename a file or directory.
1297  */
1298 int yfs_fs_rename(struct afs_fs_cursor *fc,
1299                   const char *orig_name,
1300                   struct afs_vnode *new_dvnode,
1301                   const char *new_name,
1302                   u64 current_orig_data_version,
1303                   u64 current_new_data_version)
1304 {
1305         struct afs_vnode *orig_dvnode = fc->vnode;
1306         struct afs_call *call;
1307         struct afs_net *net = afs_v2net(orig_dvnode);
1308         size_t o_namesz, n_namesz;
1309         __be32 *bp;
1310
1311         _enter("");
1312
1313         o_namesz = strlen(orig_name);
1314         n_namesz = strlen(new_name);
1315         call = afs_alloc_flat_call(net, &yfs_RXYFSRename,
1316                                    sizeof(__be32) +
1317                                    sizeof(struct yfs_xdr_RPCFlags) +
1318                                    sizeof(struct yfs_xdr_YFSFid) +
1319                                    xdr_strlen(o_namesz) +
1320                                    sizeof(struct yfs_xdr_YFSFid) +
1321                                    xdr_strlen(n_namesz),
1322                                    sizeof(struct yfs_xdr_YFSFetchStatus) +
1323                                    sizeof(struct yfs_xdr_YFSFetchStatus) +
1324                                    sizeof(struct yfs_xdr_YFSVolSync));
1325         if (!call)
1326                 return -ENOMEM;
1327
1328         call->key = fc->key;
1329         call->dvnode = orig_dvnode;
1330         call->xvnode = new_dvnode;
1331         call->expected_version = current_orig_data_version + 1;
1332         call->expected_version_2 = current_new_data_version + 1;
1333
1334         /* marshall the parameters */
1335         bp = call->request;
1336         bp = xdr_encode_u32(bp, YFSRENAME);
1337         bp = xdr_encode_u32(bp, 0); /* RPC flags */
1338         bp = xdr_encode_YFSFid(bp, &orig_dvnode->fid);
1339         bp = xdr_encode_string(bp, orig_name, o_namesz);
1340         bp = xdr_encode_YFSFid(bp, &new_dvnode->fid);
1341         bp = xdr_encode_string(bp, new_name, n_namesz);
1342         yfs_check_req(call, bp);
1343
1344         afs_use_fs_server(call, fc->cbi);
1345         trace_afs_make_fs_call2(call, &orig_dvnode->fid, orig_name, new_name);
1346         afs_set_fc_call(call, fc);
1347         afs_make_call(&fc->ac, call, GFP_NOFS);
1348         return afs_wait_for_call_to_complete(call, &fc->ac);
1349 }
1350
1351 /*
1352  * Deliver reply data to a YFS.StoreData64 operation.
1353  */
1354 static int yfs_deliver_fs_store_data(struct afs_call *call)
1355 {
1356         struct afs_vnode *vnode = call->xvnode;
1357         const __be32 *bp;
1358         int ret;
1359
1360         _enter("");
1361
1362         ret = afs_transfer_reply(call);
1363         if (ret < 0)
1364                 return ret;
1365
1366         /* unmarshall the reply once we've received all of it */
1367         bp = call->buffer;
1368         ret = yfs_decode_status(call, &bp, &vnode->status, vnode,
1369                                 &call->expected_version, NULL);
1370         if (ret < 0)
1371                 return ret;
1372         xdr_decode_YFSVolSync(&bp, NULL);
1373
1374         afs_pages_written_back(vnode, call);
1375
1376         _leave(" = 0 [done]");
1377         return 0;
1378 }
1379
1380 /*
1381  * YFS.StoreData64 operation type.
1382  */
1383 static const struct afs_call_type yfs_RXYFSStoreData64 = {
1384         .name           = "YFS.StoreData64",
1385         .op             = yfs_FS_StoreData64,
1386         .deliver        = yfs_deliver_fs_store_data,
1387         .destructor     = afs_flat_call_destructor,
1388 };
1389
1390 /*
1391  * Store a set of pages to a large file.
1392  */
1393 int yfs_fs_store_data(struct afs_fs_cursor *fc, struct address_space *mapping,
1394                       pgoff_t first, pgoff_t last,
1395                       unsigned offset, unsigned to)
1396 {
1397         struct afs_vnode *vnode = fc->vnode;
1398         struct afs_call *call;
1399         struct afs_net *net = afs_v2net(vnode);
1400         loff_t size, pos, i_size;
1401         __be32 *bp;
1402
1403         _enter(",%x,{%llx:%llu},,",
1404                key_serial(fc->key), vnode->fid.vid, vnode->fid.vnode);
1405
1406         size = (loff_t)to - (loff_t)offset;
1407         if (first != last)
1408                 size += (loff_t)(last - first) << PAGE_SHIFT;
1409         pos = (loff_t)first << PAGE_SHIFT;
1410         pos += offset;
1411
1412         i_size = i_size_read(&vnode->vfs_inode);
1413         if (pos + size > i_size)
1414                 i_size = size + pos;
1415
1416         _debug("size %llx, at %llx, i_size %llx",
1417                (unsigned long long)size, (unsigned long long)pos,
1418                (unsigned long long)i_size);
1419
1420         call = afs_alloc_flat_call(net, &yfs_RXYFSStoreData64,
1421                                    sizeof(__be32) +
1422                                    sizeof(__be32) +
1423                                    sizeof(struct yfs_xdr_YFSFid) +
1424                                    sizeof(struct yfs_xdr_YFSStoreStatus) +
1425                                    sizeof(struct yfs_xdr_u64) * 3,
1426                                    sizeof(struct yfs_xdr_YFSFetchStatus) +
1427                                    sizeof(struct yfs_xdr_YFSVolSync));
1428         if (!call)
1429                 return -ENOMEM;
1430
1431         call->key = fc->key;
1432         call->mapping = mapping;
1433         call->xvnode = vnode;
1434         call->first = first;
1435         call->last = last;
1436         call->first_offset = offset;
1437         call->last_to = to;
1438         call->send_pages = true;
1439         call->expected_version = vnode->status.data_version + 1;
1440
1441         /* marshall the parameters */
1442         bp = call->request;
1443         bp = xdr_encode_u32(bp, YFSSTOREDATA64);
1444         bp = xdr_encode_u32(bp, 0); /* RPC flags */
1445         bp = xdr_encode_YFSFid(bp, &vnode->fid);
1446         bp = xdr_encode_YFSStoreStatus_mtime(bp, &vnode->vfs_inode.i_mtime);
1447         bp = xdr_encode_u64(bp, pos);
1448         bp = xdr_encode_u64(bp, size);
1449         bp = xdr_encode_u64(bp, i_size);
1450         yfs_check_req(call, bp);
1451
1452         afs_use_fs_server(call, fc->cbi);
1453         trace_afs_make_fs_call(call, &vnode->fid);
1454         afs_set_fc_call(call, fc);
1455         afs_make_call(&fc->ac, call, GFP_NOFS);
1456         return afs_wait_for_call_to_complete(call, &fc->ac);
1457 }
1458
1459 /*
1460  * deliver reply data to an FS.StoreStatus
1461  */
1462 static int yfs_deliver_fs_store_status(struct afs_call *call)
1463 {
1464         struct afs_vnode *vnode = call->xvnode;
1465         const __be32 *bp;
1466         int ret;
1467
1468         _enter("");
1469
1470         ret = afs_transfer_reply(call);
1471         if (ret < 0)
1472                 return ret;
1473
1474         /* unmarshall the reply once we've received all of it */
1475         bp = call->buffer;
1476         ret = yfs_decode_status(call, &bp, &vnode->status, vnode,
1477                                 &call->expected_version, NULL);
1478         if (ret < 0)
1479                 return ret;
1480         xdr_decode_YFSVolSync(&bp, NULL);
1481
1482         _leave(" = 0 [done]");
1483         return 0;
1484 }
1485
1486 /*
1487  * YFS.StoreStatus operation type
1488  */
1489 static const struct afs_call_type yfs_RXYFSStoreStatus = {
1490         .name           = "YFS.StoreStatus",
1491         .op             = yfs_FS_StoreStatus,
1492         .deliver        = yfs_deliver_fs_store_status,
1493         .destructor     = afs_flat_call_destructor,
1494 };
1495
1496 static const struct afs_call_type yfs_RXYFSStoreData64_as_Status = {
1497         .name           = "YFS.StoreData64",
1498         .op             = yfs_FS_StoreData64,
1499         .deliver        = yfs_deliver_fs_store_status,
1500         .destructor     = afs_flat_call_destructor,
1501 };
1502
1503 /*
1504  * Set the attributes on a file, using YFS.StoreData64 rather than
1505  * YFS.StoreStatus so as to alter the file size also.
1506  */
1507 static int yfs_fs_setattr_size(struct afs_fs_cursor *fc, struct iattr *attr)
1508 {
1509         struct afs_vnode *vnode = fc->vnode;
1510         struct afs_call *call;
1511         struct afs_net *net = afs_v2net(vnode);
1512         __be32 *bp;
1513
1514         _enter(",%x,{%llx:%llu},,",
1515                key_serial(fc->key), vnode->fid.vid, vnode->fid.vnode);
1516
1517         call = afs_alloc_flat_call(net, &yfs_RXYFSStoreData64_as_Status,
1518                                    sizeof(__be32) * 2 +
1519                                    sizeof(struct yfs_xdr_YFSFid) +
1520                                    sizeof(struct yfs_xdr_YFSStoreStatus) +
1521                                    sizeof(struct yfs_xdr_u64) * 3,
1522                                    sizeof(struct yfs_xdr_YFSFetchStatus) +
1523                                    sizeof(struct yfs_xdr_YFSVolSync));
1524         if (!call)
1525                 return -ENOMEM;
1526
1527         call->key = fc->key;
1528         call->xvnode = vnode;
1529         call->expected_version = vnode->status.data_version + 1;
1530
1531         /* marshall the parameters */
1532         bp = call->request;
1533         bp = xdr_encode_u32(bp, YFSSTOREDATA64);
1534         bp = xdr_encode_u32(bp, 0); /* RPC flags */
1535         bp = xdr_encode_YFSFid(bp, &vnode->fid);
1536         bp = xdr_encode_YFS_StoreStatus(bp, attr);
1537         bp = xdr_encode_u64(bp, attr->ia_size); /* position of start of write */
1538         bp = xdr_encode_u64(bp, 0);             /* size of write */
1539         bp = xdr_encode_u64(bp, attr->ia_size); /* new file length */
1540         yfs_check_req(call, bp);
1541
1542         afs_use_fs_server(call, fc->cbi);
1543         trace_afs_make_fs_call(call, &vnode->fid);
1544         afs_set_fc_call(call, fc);
1545         afs_make_call(&fc->ac, call, GFP_NOFS);
1546         return afs_wait_for_call_to_complete(call, &fc->ac);
1547 }
1548
1549 /*
1550  * Set the attributes on a file, using YFS.StoreData64 if there's a change in
1551  * file size, and YFS.StoreStatus otherwise.
1552  */
1553 int yfs_fs_setattr(struct afs_fs_cursor *fc, struct iattr *attr)
1554 {
1555         struct afs_vnode *vnode = fc->vnode;
1556         struct afs_call *call;
1557         struct afs_net *net = afs_v2net(vnode);
1558         __be32 *bp;
1559
1560         if (attr->ia_valid & ATTR_SIZE)
1561                 return yfs_fs_setattr_size(fc, attr);
1562
1563         _enter(",%x,{%llx:%llu},,",
1564                key_serial(fc->key), vnode->fid.vid, vnode->fid.vnode);
1565
1566         call = afs_alloc_flat_call(net, &yfs_RXYFSStoreStatus,
1567                                    sizeof(__be32) * 2 +
1568                                    sizeof(struct yfs_xdr_YFSFid) +
1569                                    sizeof(struct yfs_xdr_YFSStoreStatus),
1570                                    sizeof(struct yfs_xdr_YFSFetchStatus) +
1571                                    sizeof(struct yfs_xdr_YFSVolSync));
1572         if (!call)
1573                 return -ENOMEM;
1574
1575         call->key = fc->key;
1576         call->xvnode = vnode;
1577         call->expected_version = vnode->status.data_version;
1578
1579         /* marshall the parameters */
1580         bp = call->request;
1581         bp = xdr_encode_u32(bp, YFSSTORESTATUS);
1582         bp = xdr_encode_u32(bp, 0); /* RPC flags */
1583         bp = xdr_encode_YFSFid(bp, &vnode->fid);
1584         bp = xdr_encode_YFS_StoreStatus(bp, attr);
1585         yfs_check_req(call, bp);
1586
1587         afs_use_fs_server(call, fc->cbi);
1588         trace_afs_make_fs_call(call, &vnode->fid);
1589         afs_set_fc_call(call, fc);
1590         afs_make_call(&fc->ac, call, GFP_NOFS);
1591         return afs_wait_for_call_to_complete(call, &fc->ac);
1592 }
1593
1594 /*
1595  * Deliver reply data to a YFS.GetVolumeStatus operation.
1596  */
1597 static int yfs_deliver_fs_get_volume_status(struct afs_call *call)
1598 {
1599         const __be32 *bp;
1600         char *p;
1601         u32 size;
1602         int ret;
1603
1604         _enter("{%u}", call->unmarshall);
1605
1606         switch (call->unmarshall) {
1607         case 0:
1608                 call->unmarshall++;
1609                 afs_extract_to_buf(call, sizeof(struct yfs_xdr_YFSFetchVolumeStatus));
1610
1611                 /* Fall through - and extract the returned status record */
1612         case 1:
1613                 _debug("extract status");
1614                 ret = afs_extract_data(call, true);
1615                 if (ret < 0)
1616                         return ret;
1617
1618                 bp = call->buffer;
1619                 xdr_decode_YFSFetchVolumeStatus(&bp, call->out_volstatus);
1620                 call->unmarshall++;
1621                 afs_extract_to_tmp(call);
1622
1623                 /* Fall through - and extract the volume name length */
1624         case 2:
1625                 ret = afs_extract_data(call, true);
1626                 if (ret < 0)
1627                         return ret;
1628
1629                 call->count = ntohl(call->tmp);
1630                 _debug("volname length: %u", call->count);
1631                 if (call->count >= AFSNAMEMAX)
1632                         return afs_protocol_error(call, -EBADMSG,
1633                                                   afs_eproto_volname_len);
1634                 size = (call->count + 3) & ~3; /* It's padded */
1635                 afs_extract_to_buf(call, size);
1636                 call->unmarshall++;
1637
1638                 /* Fall through - and extract the volume name */
1639         case 3:
1640                 _debug("extract volname");
1641                 ret = afs_extract_data(call, true);
1642                 if (ret < 0)
1643                         return ret;
1644
1645                 p = call->buffer;
1646                 p[call->count] = 0;
1647                 _debug("volname '%s'", p);
1648                 afs_extract_to_tmp(call);
1649                 call->unmarshall++;
1650
1651                 /* Fall through - and extract the offline message length */
1652         case 4:
1653                 ret = afs_extract_data(call, true);
1654                 if (ret < 0)
1655                         return ret;
1656
1657                 call->count = ntohl(call->tmp);
1658                 _debug("offline msg length: %u", call->count);
1659                 if (call->count >= AFSNAMEMAX)
1660                         return afs_protocol_error(call, -EBADMSG,
1661                                                   afs_eproto_offline_msg_len);
1662                 size = (call->count + 3) & ~3; /* It's padded */
1663                 afs_extract_to_buf(call, size);
1664                 call->unmarshall++;
1665
1666                 /* Fall through - and extract the offline message */
1667         case 5:
1668                 _debug("extract offline");
1669                 ret = afs_extract_data(call, true);
1670                 if (ret < 0)
1671                         return ret;
1672
1673                 p = call->buffer;
1674                 p[call->count] = 0;
1675                 _debug("offline '%s'", p);
1676
1677                 afs_extract_to_tmp(call);
1678                 call->unmarshall++;
1679
1680                 /* Fall through - and extract the message of the day length */
1681         case 6:
1682                 ret = afs_extract_data(call, true);
1683                 if (ret < 0)
1684                         return ret;
1685
1686                 call->count = ntohl(call->tmp);
1687                 _debug("motd length: %u", call->count);
1688                 if (call->count >= AFSNAMEMAX)
1689                         return afs_protocol_error(call, -EBADMSG,
1690                                                   afs_eproto_motd_len);
1691                 size = (call->count + 3) & ~3; /* It's padded */
1692                 afs_extract_to_buf(call, size);
1693                 call->unmarshall++;
1694
1695                 /* Fall through - and extract the message of the day */
1696         case 7:
1697                 _debug("extract motd");
1698                 ret = afs_extract_data(call, false);
1699                 if (ret < 0)
1700                         return ret;
1701
1702                 p = call->buffer;
1703                 p[call->count] = 0;
1704                 _debug("motd '%s'", p);
1705
1706                 call->unmarshall++;
1707
1708                 /* Fall through */
1709         case 8:
1710                 break;
1711         }
1712
1713         _leave(" = 0 [done]");
1714         return 0;
1715 }
1716
1717 /*
1718  * YFS.GetVolumeStatus operation type
1719  */
1720 static const struct afs_call_type yfs_RXYFSGetVolumeStatus = {
1721         .name           = "YFS.GetVolumeStatus",
1722         .op             = yfs_FS_GetVolumeStatus,
1723         .deliver        = yfs_deliver_fs_get_volume_status,
1724         .destructor     = afs_flat_call_destructor,
1725 };
1726
1727 /*
1728  * fetch the status of a volume
1729  */
1730 int yfs_fs_get_volume_status(struct afs_fs_cursor *fc,
1731                              struct afs_volume_status *vs)
1732 {
1733         struct afs_vnode *vnode = fc->vnode;
1734         struct afs_call *call;
1735         struct afs_net *net = afs_v2net(vnode);
1736         __be32 *bp;
1737
1738         _enter("");
1739
1740         call = afs_alloc_flat_call(net, &yfs_RXYFSGetVolumeStatus,
1741                                    sizeof(__be32) * 2 +
1742                                    sizeof(struct yfs_xdr_u64),
1743                                    max_t(size_t,
1744                                          sizeof(struct yfs_xdr_YFSFetchVolumeStatus) +
1745                                          sizeof(__be32),
1746                                          AFSOPAQUEMAX + 1));
1747         if (!call)
1748                 return -ENOMEM;
1749
1750         call->key = fc->key;
1751         call->out_volstatus = vs;
1752
1753         /* marshall the parameters */
1754         bp = call->request;
1755         bp = xdr_encode_u32(bp, YFSGETVOLUMESTATUS);
1756         bp = xdr_encode_u32(bp, 0); /* RPC flags */
1757         bp = xdr_encode_u64(bp, vnode->fid.vid);
1758         yfs_check_req(call, bp);
1759
1760         afs_use_fs_server(call, fc->cbi);
1761         trace_afs_make_fs_call(call, &vnode->fid);
1762         afs_set_fc_call(call, fc);
1763         afs_make_call(&fc->ac, call, GFP_NOFS);
1764         return afs_wait_for_call_to_complete(call, &fc->ac);
1765 }
1766
1767 /*
1768  * Deliver reply data to operations that just return a file status and a volume
1769  * sync record.
1770  */
1771 static int yfs_deliver_status_and_volsync(struct afs_call *call)
1772 {
1773         struct afs_vnode *vnode = call->xvnode;
1774         const __be32 *bp;
1775         int ret;
1776
1777         _enter("{%u}", call->unmarshall);
1778
1779         ret = afs_transfer_reply(call);
1780         if (ret < 0)
1781                 return ret;
1782
1783         /* unmarshall the reply once we've received all of it */
1784         bp = call->buffer;
1785         ret = yfs_decode_status(call, &bp, &vnode->status, vnode,
1786                                 &call->expected_version, NULL);
1787         if (ret < 0)
1788                 return ret;
1789         xdr_decode_YFSVolSync(&bp, NULL);
1790
1791         _leave(" = 0 [done]");
1792         return 0;
1793 }
1794
1795 /*
1796  * YFS.SetLock operation type
1797  */
1798 static const struct afs_call_type yfs_RXYFSSetLock = {
1799         .name           = "YFS.SetLock",
1800         .op             = yfs_FS_SetLock,
1801         .deliver        = yfs_deliver_status_and_volsync,
1802         .done           = afs_lock_op_done,
1803         .destructor     = afs_flat_call_destructor,
1804 };
1805
1806 /*
1807  * YFS.ExtendLock operation type
1808  */
1809 static const struct afs_call_type yfs_RXYFSExtendLock = {
1810         .name           = "YFS.ExtendLock",
1811         .op             = yfs_FS_ExtendLock,
1812         .deliver        = yfs_deliver_status_and_volsync,
1813         .done           = afs_lock_op_done,
1814         .destructor     = afs_flat_call_destructor,
1815 };
1816
1817 /*
1818  * YFS.ReleaseLock operation type
1819  */
1820 static const struct afs_call_type yfs_RXYFSReleaseLock = {
1821         .name           = "YFS.ReleaseLock",
1822         .op             = yfs_FS_ReleaseLock,
1823         .deliver        = yfs_deliver_status_and_volsync,
1824         .destructor     = afs_flat_call_destructor,
1825 };
1826
1827 /*
1828  * Set a lock on a file
1829  */
1830 int yfs_fs_set_lock(struct afs_fs_cursor *fc, afs_lock_type_t type)
1831 {
1832         struct afs_vnode *vnode = fc->vnode;
1833         struct afs_call *call;
1834         struct afs_net *net = afs_v2net(vnode);
1835         __be32 *bp;
1836
1837         _enter("");
1838
1839         call = afs_alloc_flat_call(net, &yfs_RXYFSSetLock,
1840                                    sizeof(__be32) * 2 +
1841                                    sizeof(struct yfs_xdr_YFSFid) +
1842                                    sizeof(__be32),
1843                                    sizeof(struct yfs_xdr_YFSFetchStatus) +
1844                                    sizeof(struct yfs_xdr_YFSVolSync));
1845         if (!call)
1846                 return -ENOMEM;
1847
1848         call->key = fc->key;
1849         call->xvnode = vnode;
1850         call->want_reply_time = true;
1851
1852         /* marshall the parameters */
1853         bp = call->request;
1854         bp = xdr_encode_u32(bp, YFSSETLOCK);
1855         bp = xdr_encode_u32(bp, 0); /* RPC flags */
1856         bp = xdr_encode_YFSFid(bp, &vnode->fid);
1857         bp = xdr_encode_u32(bp, type);
1858         yfs_check_req(call, bp);
1859
1860         afs_use_fs_server(call, fc->cbi);
1861         trace_afs_make_fs_calli(call, &vnode->fid, type);
1862         afs_set_fc_call(call, fc);
1863         afs_make_call(&fc->ac, call, GFP_NOFS);
1864         return afs_wait_for_call_to_complete(call, &fc->ac);
1865 }
1866
1867 /*
1868  * extend a lock on a file
1869  */
1870 int yfs_fs_extend_lock(struct afs_fs_cursor *fc)
1871 {
1872         struct afs_vnode *vnode = fc->vnode;
1873         struct afs_call *call;
1874         struct afs_net *net = afs_v2net(vnode);
1875         __be32 *bp;
1876
1877         _enter("");
1878
1879         call = afs_alloc_flat_call(net, &yfs_RXYFSExtendLock,
1880                                    sizeof(__be32) * 2 +
1881                                    sizeof(struct yfs_xdr_YFSFid),
1882                                    sizeof(struct yfs_xdr_YFSFetchStatus) +
1883                                    sizeof(struct yfs_xdr_YFSVolSync));
1884         if (!call)
1885                 return -ENOMEM;
1886
1887         call->key = fc->key;
1888         call->xvnode = vnode;
1889         call->want_reply_time = true;
1890
1891         /* marshall the parameters */
1892         bp = call->request;
1893         bp = xdr_encode_u32(bp, YFSEXTENDLOCK);
1894         bp = xdr_encode_u32(bp, 0); /* RPC flags */
1895         bp = xdr_encode_YFSFid(bp, &vnode->fid);
1896         yfs_check_req(call, bp);
1897
1898         afs_use_fs_server(call, fc->cbi);
1899         trace_afs_make_fs_call(call, &vnode->fid);
1900         afs_set_fc_call(call, fc);
1901         afs_make_call(&fc->ac, call, GFP_NOFS);
1902         return afs_wait_for_call_to_complete(call, &fc->ac);
1903 }
1904
1905 /*
1906  * release a lock on a file
1907  */
1908 int yfs_fs_release_lock(struct afs_fs_cursor *fc)
1909 {
1910         struct afs_vnode *vnode = fc->vnode;
1911         struct afs_call *call;
1912         struct afs_net *net = afs_v2net(vnode);
1913         __be32 *bp;
1914
1915         _enter("");
1916
1917         call = afs_alloc_flat_call(net, &yfs_RXYFSReleaseLock,
1918                                    sizeof(__be32) * 2 +
1919                                    sizeof(struct yfs_xdr_YFSFid),
1920                                    sizeof(struct yfs_xdr_YFSFetchStatus) +
1921                                    sizeof(struct yfs_xdr_YFSVolSync));
1922         if (!call)
1923                 return -ENOMEM;
1924
1925         call->key = fc->key;
1926         call->xvnode = vnode;
1927
1928         /* marshall the parameters */
1929         bp = call->request;
1930         bp = xdr_encode_u32(bp, YFSRELEASELOCK);
1931         bp = xdr_encode_u32(bp, 0); /* RPC flags */
1932         bp = xdr_encode_YFSFid(bp, &vnode->fid);
1933         yfs_check_req(call, bp);
1934
1935         afs_use_fs_server(call, fc->cbi);
1936         trace_afs_make_fs_call(call, &vnode->fid);
1937         afs_set_fc_call(call, fc);
1938         afs_make_call(&fc->ac, call, GFP_NOFS);
1939         return afs_wait_for_call_to_complete(call, &fc->ac);
1940 }
1941
1942 /*
1943  * Deliver reply data to an FS.FetchStatus with no vnode.
1944  */
1945 static int yfs_deliver_fs_fetch_status(struct afs_call *call)
1946 {
1947         struct afs_file_status *status = call->out_extra_status;
1948         struct afs_callback *callback = call->out_cb;
1949         struct afs_volsync *volsync = call->out_volsync;
1950         const __be32 *bp;
1951         int ret;
1952
1953         ret = afs_transfer_reply(call);
1954         if (ret < 0)
1955                 return ret;
1956
1957         _enter("");
1958
1959         /* unmarshall the reply once we've received all of it */
1960         bp = call->buffer;
1961         ret = yfs_decode_status(call, &bp, status, NULL,
1962                                 &call->expected_version, NULL);
1963         if (ret < 0)
1964                 return ret;
1965         xdr_decode_YFSCallBack_raw(call, callback, &bp);
1966         xdr_decode_YFSVolSync(&bp, volsync);
1967
1968         _leave(" = 0 [done]");
1969         return 0;
1970 }
1971
1972 /*
1973  * YFS.FetchStatus operation type
1974  */
1975 static const struct afs_call_type yfs_RXYFSFetchStatus = {
1976         .name           = "YFS.FetchStatus",
1977         .op             = yfs_FS_FetchStatus,
1978         .deliver        = yfs_deliver_fs_fetch_status,
1979         .destructor     = afs_flat_call_destructor,
1980 };
1981
1982 /*
1983  * Fetch the status information for a fid without needing a vnode handle.
1984  */
1985 int yfs_fs_fetch_status(struct afs_fs_cursor *fc,
1986                         struct afs_net *net,
1987                         struct afs_fid *fid,
1988                         struct afs_file_status *status,
1989                         struct afs_callback *callback,
1990                         struct afs_volsync *volsync)
1991 {
1992         struct afs_call *call;
1993         __be32 *bp;
1994
1995         _enter(",%x,{%llx:%llu},,",
1996                key_serial(fc->key), fid->vid, fid->vnode);
1997
1998         call = afs_alloc_flat_call(net, &yfs_RXYFSFetchStatus,
1999                                    sizeof(__be32) * 2 +
2000                                    sizeof(struct yfs_xdr_YFSFid),
2001                                    sizeof(struct yfs_xdr_YFSFetchStatus) +
2002                                    sizeof(struct yfs_xdr_YFSCallBack) +
2003                                    sizeof(struct yfs_xdr_YFSVolSync));
2004         if (!call) {
2005                 fc->ac.error = -ENOMEM;
2006                 return -ENOMEM;
2007         }
2008
2009         call->key = fc->key;
2010         call->out_extra_status = status;
2011         call->out_cb = callback;
2012         call->out_volsync = volsync;
2013         call->expected_version = 1; /* vnode->status.data_version */
2014
2015         /* marshall the parameters */
2016         bp = call->request;
2017         bp = xdr_encode_u32(bp, YFSFETCHSTATUS);
2018         bp = xdr_encode_u32(bp, 0); /* RPC flags */
2019         bp = xdr_encode_YFSFid(bp, fid);
2020         yfs_check_req(call, bp);
2021
2022         call->cb_break = fc->cb_break;
2023         afs_use_fs_server(call, fc->cbi);
2024         trace_afs_make_fs_call(call, fid);
2025         afs_set_fc_call(call, fc);
2026         afs_make_call(&fc->ac, call, GFP_NOFS);
2027         return afs_wait_for_call_to_complete(call, &fc->ac);
2028 }
2029
2030 /*
2031  * Deliver reply data to an YFS.InlineBulkStatus call
2032  */
2033 static int yfs_deliver_fs_inline_bulk_status(struct afs_call *call)
2034 {
2035         struct afs_file_status *statuses;
2036         struct afs_callback *callbacks;
2037         const __be32 *bp;
2038         u32 tmp;
2039         int ret;
2040
2041         _enter("{%u}", call->unmarshall);
2042
2043         switch (call->unmarshall) {
2044         case 0:
2045                 afs_extract_to_tmp(call);
2046                 call->unmarshall++;
2047
2048                 /* Extract the file status count and array in two steps */
2049                 /* Fall through */
2050         case 1:
2051                 _debug("extract status count");
2052                 ret = afs_extract_data(call, true);
2053                 if (ret < 0)
2054                         return ret;
2055
2056                 tmp = ntohl(call->tmp);
2057                 _debug("status count: %u/%u", tmp, call->count2);
2058                 if (tmp != call->count2)
2059                         return afs_protocol_error(call, -EBADMSG,
2060                                                   afs_eproto_ibulkst_count);
2061
2062                 call->count = 0;
2063                 call->unmarshall++;
2064         more_counts:
2065                 afs_extract_to_buf(call, sizeof(struct yfs_xdr_YFSFetchStatus));
2066
2067                 /* Fall through */
2068         case 2:
2069                 _debug("extract status array %u", call->count);
2070                 ret = afs_extract_data(call, true);
2071                 if (ret < 0)
2072                         return ret;
2073
2074                 bp = call->buffer;
2075                 statuses = call->out_extra_status;
2076                 ret = yfs_decode_status(call, &bp, &statuses[call->count],
2077                                         NULL, NULL, NULL);
2078                 if (ret < 0)
2079                         return ret;
2080
2081                 call->count++;
2082                 if (call->count < call->count2)
2083                         goto more_counts;
2084
2085                 call->count = 0;
2086                 call->unmarshall++;
2087                 afs_extract_to_tmp(call);
2088
2089                 /* Extract the callback count and array in two steps */
2090                 /* Fall through */
2091         case 3:
2092                 _debug("extract CB count");
2093                 ret = afs_extract_data(call, true);
2094                 if (ret < 0)
2095                         return ret;
2096
2097                 tmp = ntohl(call->tmp);
2098                 _debug("CB count: %u", tmp);
2099                 if (tmp != call->count2)
2100                         return afs_protocol_error(call, -EBADMSG,
2101                                                   afs_eproto_ibulkst_cb_count);
2102                 call->count = 0;
2103                 call->unmarshall++;
2104         more_cbs:
2105                 afs_extract_to_buf(call, sizeof(struct yfs_xdr_YFSCallBack));
2106
2107                 /* Fall through */
2108         case 4:
2109                 _debug("extract CB array");
2110                 ret = afs_extract_data(call, true);
2111                 if (ret < 0)
2112                         return ret;
2113
2114                 _debug("unmarshall CB array");
2115                 bp = call->buffer;
2116                 callbacks = call->out_cb;
2117                 xdr_decode_YFSCallBack_raw(call, &callbacks[call->count], &bp);
2118                 call->count++;
2119                 if (call->count < call->count2)
2120                         goto more_cbs;
2121
2122                 afs_extract_to_buf(call, sizeof(struct yfs_xdr_YFSVolSync));
2123                 call->unmarshall++;
2124
2125                 /* Fall through */
2126         case 5:
2127                 ret = afs_extract_data(call, false);
2128                 if (ret < 0)
2129                         return ret;
2130
2131                 bp = call->buffer;
2132                 xdr_decode_YFSVolSync(&bp, call->out_volsync);
2133
2134                 call->unmarshall++;
2135
2136                 /* Fall through */
2137         case 6:
2138                 break;
2139         }
2140
2141         _leave(" = 0 [done]");
2142         return 0;
2143 }
2144
2145 /*
2146  * FS.InlineBulkStatus operation type
2147  */
2148 static const struct afs_call_type yfs_RXYFSInlineBulkStatus = {
2149         .name           = "YFS.InlineBulkStatus",
2150         .op             = yfs_FS_InlineBulkStatus,
2151         .deliver        = yfs_deliver_fs_inline_bulk_status,
2152         .destructor     = afs_flat_call_destructor,
2153 };
2154
2155 /*
2156  * Fetch the status information for up to 1024 files
2157  */
2158 int yfs_fs_inline_bulk_status(struct afs_fs_cursor *fc,
2159                               struct afs_net *net,
2160                               struct afs_fid *fids,
2161                               struct afs_file_status *statuses,
2162                               struct afs_callback *callbacks,
2163                               unsigned int nr_fids,
2164                               struct afs_volsync *volsync)
2165 {
2166         struct afs_call *call;
2167         __be32 *bp;
2168         int i;
2169
2170         _enter(",%x,{%llx:%llu},%u",
2171                key_serial(fc->key), fids[0].vid, fids[1].vnode, nr_fids);
2172
2173         call = afs_alloc_flat_call(net, &yfs_RXYFSInlineBulkStatus,
2174                                    sizeof(__be32) +
2175                                    sizeof(__be32) +
2176                                    sizeof(__be32) +
2177                                    sizeof(struct yfs_xdr_YFSFid) * nr_fids,
2178                                    sizeof(struct yfs_xdr_YFSFetchStatus));
2179         if (!call) {
2180                 fc->ac.error = -ENOMEM;
2181                 return -ENOMEM;
2182         }
2183
2184         call->key = fc->key;
2185         call->out_extra_status = statuses;
2186         call->out_cb = callbacks;
2187         call->out_volsync = volsync;
2188         call->count2 = nr_fids;
2189
2190         /* marshall the parameters */
2191         bp = call->request;
2192         bp = xdr_encode_u32(bp, YFSINLINEBULKSTATUS);
2193         bp = xdr_encode_u32(bp, 0); /* RPCFlags */
2194         bp = xdr_encode_u32(bp, nr_fids);
2195         for (i = 0; i < nr_fids; i++)
2196                 bp = xdr_encode_YFSFid(bp, &fids[i]);
2197         yfs_check_req(call, bp);
2198
2199         call->cb_break = fc->cb_break;
2200         afs_use_fs_server(call, fc->cbi);
2201         trace_afs_make_fs_call(call, &fids[0]);
2202         afs_set_fc_call(call, fc);
2203         afs_make_call(&fc->ac, call, GFP_NOFS);
2204         return afs_wait_for_call_to_complete(call, &fc->ac);
2205 }
2206
2207 /*
2208  * Deliver reply data to an YFS.FetchOpaqueACL.
2209  */
2210 static int yfs_deliver_fs_fetch_opaque_acl(struct afs_call *call)
2211 {
2212         struct afs_volsync *volsync = call->out_volsync;
2213         struct afs_vnode *vnode = call->xvnode;
2214         struct yfs_acl *yacl = call->out_yacl;
2215         struct afs_acl *acl;
2216         const __be32 *bp;
2217         unsigned int size;
2218         int ret;
2219
2220         _enter("{%u}", call->unmarshall);
2221
2222         switch (call->unmarshall) {
2223         case 0:
2224                 afs_extract_to_tmp(call);
2225                 call->unmarshall++;
2226
2227                 /* Extract the file ACL length */
2228         case 1:
2229                 ret = afs_extract_data(call, true);
2230                 if (ret < 0)
2231                         return ret;
2232
2233                 size = call->count2 = ntohl(call->tmp);
2234                 size = round_up(size, 4);
2235
2236                 if (yacl->flags & YFS_ACL_WANT_ACL) {
2237                         acl = kmalloc(struct_size(acl, data, size), GFP_KERNEL);
2238                         if (!acl)
2239                                 return -ENOMEM;
2240                         yacl->acl = acl;
2241                         acl->size = call->count2;
2242                         afs_extract_begin(call, acl->data, size);
2243                 } else {
2244                         iov_iter_discard(&call->iter, READ, size);
2245                 }
2246                 call->unmarshall++;
2247
2248                 /* Extract the file ACL */
2249         case 2:
2250                 ret = afs_extract_data(call, true);
2251                 if (ret < 0)
2252                         return ret;
2253
2254                 afs_extract_to_tmp(call);
2255                 call->unmarshall++;
2256
2257                 /* Extract the volume ACL length */
2258         case 3:
2259                 ret = afs_extract_data(call, true);
2260                 if (ret < 0)
2261                         return ret;
2262
2263                 size = call->count2 = ntohl(call->tmp);
2264                 size = round_up(size, 4);
2265
2266                 if (yacl->flags & YFS_ACL_WANT_VOL_ACL) {
2267                         acl = kmalloc(struct_size(acl, data, size), GFP_KERNEL);
2268                         if (!acl)
2269                                 return -ENOMEM;
2270                         yacl->vol_acl = acl;
2271                         acl->size = call->count2;
2272                         afs_extract_begin(call, acl->data, size);
2273                 } else {
2274                         iov_iter_discard(&call->iter, READ, size);
2275                 }
2276                 call->unmarshall++;
2277
2278                 /* Extract the volume ACL */
2279         case 4:
2280                 ret = afs_extract_data(call, true);
2281                 if (ret < 0)
2282                         return ret;
2283
2284                 afs_extract_to_buf(call,
2285                                    sizeof(__be32) * 2 +
2286                                    sizeof(struct yfs_xdr_YFSFetchStatus) +
2287                                    sizeof(struct yfs_xdr_YFSVolSync));
2288                 call->unmarshall++;
2289
2290                 /* extract the metadata */
2291         case 5:
2292                 ret = afs_extract_data(call, false);
2293                 if (ret < 0)
2294                         return ret;
2295
2296                 bp = call->buffer;
2297                 yacl->inherit_flag = ntohl(*bp++);
2298                 yacl->num_cleaned = ntohl(*bp++);
2299                 ret = yfs_decode_status(call, &bp, &vnode->status, vnode,
2300                                         &call->expected_version, NULL);
2301                 if (ret < 0)
2302                         return ret;
2303                 xdr_decode_YFSVolSync(&bp, volsync);
2304
2305                 call->unmarshall++;
2306
2307         case 6:
2308                 break;
2309         }
2310
2311         _leave(" = 0 [done]");
2312         return 0;
2313 }
2314
2315 void yfs_free_opaque_acl(struct yfs_acl *yacl)
2316 {
2317         if (yacl) {
2318                 kfree(yacl->acl);
2319                 kfree(yacl->vol_acl);
2320                 kfree(yacl);
2321         }
2322 }
2323
2324 /*
2325  * YFS.FetchOpaqueACL operation type
2326  */
2327 static const struct afs_call_type yfs_RXYFSFetchOpaqueACL = {
2328         .name           = "YFS.FetchOpaqueACL",
2329         .op             = yfs_FS_FetchOpaqueACL,
2330         .deliver        = yfs_deliver_fs_fetch_opaque_acl,
2331         .destructor     = afs_flat_call_destructor,
2332 };
2333
2334 /*
2335  * Fetch the YFS advanced ACLs for a file.
2336  */
2337 struct yfs_acl *yfs_fs_fetch_opaque_acl(struct afs_fs_cursor *fc,
2338                                         struct yfs_acl *yacl)
2339 {
2340         struct afs_vnode *vnode = fc->vnode;
2341         struct afs_call *call;
2342         struct afs_net *net = afs_v2net(vnode);
2343         __be32 *bp;
2344
2345         _enter(",%x,{%llx:%llu},,",
2346                key_serial(fc->key), vnode->fid.vid, vnode->fid.vnode);
2347
2348         call = afs_alloc_flat_call(net, &yfs_RXYFSFetchOpaqueACL,
2349                                    sizeof(__be32) * 2 +
2350                                    sizeof(struct yfs_xdr_YFSFid),
2351                                    sizeof(__be32) * 2 +
2352                                    sizeof(struct yfs_xdr_YFSFetchStatus) +
2353                                    sizeof(struct yfs_xdr_YFSVolSync));
2354         if (!call) {
2355                 fc->ac.error = -ENOMEM;
2356                 return ERR_PTR(-ENOMEM);
2357         }
2358
2359         call->key = fc->key;
2360         call->out_yacl = yacl;
2361         call->xvnode = vnode;
2362         call->out_volsync = NULL; /* volsync */
2363
2364         /* marshall the parameters */
2365         bp = call->request;
2366         bp = xdr_encode_u32(bp, YFSFETCHOPAQUEACL);
2367         bp = xdr_encode_u32(bp, 0); /* RPC flags */
2368         bp = xdr_encode_YFSFid(bp, &vnode->fid);
2369         yfs_check_req(call, bp);
2370
2371         call->cb_break = fc->cb_break;
2372         afs_use_fs_server(call, fc->cbi);
2373         trace_afs_make_fs_call(call, &vnode->fid);
2374         afs_make_call(&fc->ac, call, GFP_KERNEL);
2375         return (struct yfs_acl *)afs_wait_for_call_to_complete(call, &fc->ac);
2376 }
2377
2378 /*
2379  * YFS.StoreOpaqueACL2 operation type
2380  */
2381 static const struct afs_call_type yfs_RXYFSStoreOpaqueACL2 = {
2382         .name           = "YFS.StoreOpaqueACL2",
2383         .op             = yfs_FS_StoreOpaqueACL2,
2384         .deliver        = yfs_deliver_status_and_volsync,
2385         .destructor     = afs_flat_call_destructor,
2386 };
2387
2388 /*
2389  * Fetch the YFS ACL for a file.
2390  */
2391 int yfs_fs_store_opaque_acl2(struct afs_fs_cursor *fc, const struct afs_acl *acl)
2392 {
2393         struct afs_vnode *vnode = fc->vnode;
2394         struct afs_call *call;
2395         struct afs_net *net = afs_v2net(vnode);
2396         size_t size;
2397         __be32 *bp;
2398
2399         _enter(",%x,{%llx:%llu},,",
2400                key_serial(fc->key), vnode->fid.vid, vnode->fid.vnode);
2401
2402         size = round_up(acl->size, 4);
2403         call = afs_alloc_flat_call(net, &yfs_RXYFSStoreStatus,
2404                                    sizeof(__be32) * 2 +
2405                                    sizeof(struct yfs_xdr_YFSFid) +
2406                                    sizeof(__be32) + size,
2407                                    sizeof(struct yfs_xdr_YFSFetchStatus) +
2408                                    sizeof(struct yfs_xdr_YFSVolSync));
2409         if (!call) {
2410                 fc->ac.error = -ENOMEM;
2411                 return -ENOMEM;
2412         }
2413
2414         call->key = fc->key;
2415         call->xvnode = vnode;
2416         call->out_volsync = NULL;
2417
2418         /* marshall the parameters */
2419         bp = call->request;
2420         bp = xdr_encode_u32(bp, YFSSTOREOPAQUEACL2);
2421         bp = xdr_encode_u32(bp, 0); /* RPC flags */
2422         bp = xdr_encode_YFSFid(bp, &vnode->fid);
2423         bp = xdr_encode_u32(bp, acl->size);
2424         memcpy(bp, acl->data, acl->size);
2425         if (acl->size != size)
2426                 memset((void *)bp + acl->size, 0, size - acl->size);
2427         yfs_check_req(call, bp);
2428
2429         trace_afs_make_fs_call(call, &vnode->fid);
2430         afs_make_call(&fc->ac, call, GFP_KERNEL);
2431         return afs_wait_for_call_to_complete(call, &fc->ac);
2432 }