OSDN Git Service

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