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