Coverage Report

Created: 2026-12-01 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libnfs/nfs4/nfs4.c
Line
Count
Source
1
/* -*-  mode:c; tab-width:8; c-basic-offset:8; indent-tabs-mode:nil;  -*- */
2
/*
3
   Copyright (C) 2016 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
4
5
   This program is free software; you can redistribute it and/or modify
6
   it under the terms of the GNU Lesser General Public License as published by
7
   the Free Software Foundation; either version 2.1 of the License, or
8
   (at your option) any later version.
9
10
   This program is distributed in the hope that it will be useful,
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
   GNU Lesser General Public License for more details.
14
15
   You should have received a copy of the GNU Lesser General Public License
16
   along with this program; if not, see <http://www.gnu.org/licenses/>.
17
*/
18
19
#ifdef HAVE_CONFIG_H
20
#include "config.h"
21
#endif
22
23
#ifdef PS2_EE
24
#include "ps2_compat.h"
25
#endif
26
27
#ifdef WIN32
28
#include <win32/win32_compat.h>
29
#else
30
#include <sys/stat.h>
31
#endif/*WIN32*/
32
33
#include <stdio.h>
34
#include <assert.h>
35
#include <errno.h>
36
#include <string.h>
37
#include <stdlib.h>
38
#include "libnfs-zdr.h"
39
#include "libnfs.h"
40
#include "libnfs-raw.h"
41
#include "libnfs-private.h"
42
#include "libnfs-raw-nfs4.h"
43
44
char *
45
nfsstat4_to_str(int error)
46
0
{
47
0
  switch (error) {
48
0
        case NFS4_OK: return "NFS4_OK"; break;
49
0
        case NFS4ERR_PERM: return "NFS4ERR_PERM"; break;
50
0
        case NFS4ERR_NOENT: return "NFS4ERR_NOENT"; break;
51
0
        case NFS4ERR_IO: return "NFS4ERR_IO"; break;
52
0
        case NFS4ERR_NXIO: return "NFS4ERR_NXIO"; break;
53
0
        case NFS4ERR_ACCESS: return "NFS4ERR_ACCESS"; break;
54
0
        case NFS4ERR_EXIST: return "NFS4ERR_EXIST"; break;
55
0
        case NFS4ERR_XDEV: return "NFS4ERR_XDEV"; break;
56
0
        case NFS4ERR_NOTDIR: return "NFS4ERR_NOTDIR"; break;
57
0
        case NFS4ERR_ISDIR: return "NFS4ERR_ISDIR"; break;
58
0
        case NFS4ERR_INVAL: return "NFS4ERR_INVAL"; break;
59
0
        case NFS4ERR_FBIG: return "NFS4ERR_FBIG"; break;
60
0
        case NFS4ERR_NOSPC: return "NFS4ERR_NOSPC"; break;
61
0
        case NFS4ERR_ROFS: return"NFS4ERR_ROFS"; break;
62
0
        case NFS4ERR_MLINK: return "NFS4ERR_MLINK"; break;
63
0
        case NFS4ERR_NAMETOOLONG: return "NFS4ERR_NAMETOOLONG"; break;
64
0
        case NFS4ERR_NOTEMPTY: return "NFS4ERR_NOTEMPTY"; break;
65
0
        case NFS4ERR_DQUOT: return "NFS4ERR_DQUOT"; break;
66
0
        case NFS4ERR_STALE: return "NFS4ERR_STALE"; break;
67
0
        case NFS4ERR_BADHANDLE: return "NFS4ERR_BADHANDLE"; break;
68
0
        case NFS4ERR_BAD_COOKIE: return "NFS4ERR_BAD_COOKIE"; break;
69
0
        case NFS4ERR_NOTSUPP: return "NFS4ERR_NOTSUPP"; break;
70
0
        case NFS4ERR_TOOSMALL: return "NFS4ERR_TOOSMALL"; break;
71
0
        case NFS4ERR_SERVERFAULT: return "NFS4ERR_SERVERFAULT"; break;
72
0
        case NFS4ERR_BADTYPE: return "NFS4ERR_BADTYPE"; break;
73
0
        case NFS4ERR_DELAY: return "NFS4ERR_DELAY"; break;
74
0
        case NFS4ERR_SAME: return "NFS4ERR_SAME"; break;
75
0
        case NFS4ERR_DENIED: return "NFS4ERR_DENIED"; break;
76
0
        case NFS4ERR_EXPIRED: return "NFS4ERR_EXPIRED"; break;
77
0
        case NFS4ERR_LOCKED: return "NFS4ERR_LOCKED"; break;
78
0
        case NFS4ERR_GRACE: return "NFS4ERR_GRACE"; break;
79
0
        case NFS4ERR_FHEXPIRED: return "NFS4ERR_FHEXPIRED"; break;
80
0
        case NFS4ERR_SHARE_DENIED: return "NFS4ERR_SHARE_DENIED"; break;
81
0
        case NFS4ERR_WRONGSEC: return "NFS4ERR_WRONGSEC"; break;
82
0
        case NFS4ERR_CLID_INUSE: return "NFS4ERR_CLID_INUSE"; break;
83
0
        case NFS4ERR_RESOURCE: return "NFS4ERR_RESOURCE"; break;
84
0
        case NFS4ERR_MOVED: return "NFS4ERR_MOVED"; break;
85
0
        case NFS4ERR_NOFILEHANDLE: return "NFS4ERR_NOFILEHANDLE"; break;
86
0
        case NFS4ERR_MINOR_VERS_MISMATCH:
87
0
                return "NFS4ERR_MINOR_VERS_MISMATCH"; break;
88
0
        case NFS4ERR_STALE_CLIENTID: return "NFS4ERR_STALE_CLIENTID"; break;
89
0
        case NFS4ERR_STALE_STATEID: return "NFS4ERR_STALE_STATEID"; break;
90
0
        case NFS4ERR_OLD_STATEID: return "NFS4ERR_OLD_STATEID"; break;
91
0
        case NFS4ERR_BAD_STATEID: return "NFS4ERR_BAD_STATEID"; break;
92
0
        case NFS4ERR_BAD_SEQID: return "NFS4ERR_BAD_SEQID"; break;
93
0
        case NFS4ERR_NOT_SAME: return "NFS4ERR_NOT_SAME"; break;
94
0
        case NFS4ERR_LOCK_RANGE: return "NFS4ERR_LOCK_RANGE"; break;
95
0
        case NFS4ERR_SYMLINK: return "NFS4ERR_SYMLINK"; break;
96
0
        case NFS4ERR_RESTOREFH: return "NFS4ERR_RESTOREFH"; break;
97
0
        case NFS4ERR_LEASE_MOVED: return "NFS4ERR_LEASE_MOVED"; break;
98
0
        case NFS4ERR_ATTRNOTSUPP: return "NFS4ERR_ATTRNOTSUPP"; break;
99
0
        case NFS4ERR_NO_GRACE: return "NFS4ERR_NO_GRACE"; break;
100
0
        case NFS4ERR_RECLAIM_BAD: return "NFS4ERR_RECLAIM_BAD"; break;
101
0
        case NFS4ERR_RECLAIM_CONFLICT: return "NFS4ERR_RECLAIM_CONFLICT"; break;
102
0
        case NFS4ERR_BADZDR: return "NFS4ERR_BADZDR"; break;
103
0
        case NFS4ERR_LOCKS_HELD: return "NFS4ERR_LOCKS_HELD"; break;
104
0
        case NFS4ERR_OPENMODE: return "NFS4ERR_OPENMODE"; break;
105
0
        case NFS4ERR_BADOWNER: return "NFS4ERR_BADOWNER"; break;
106
0
        case NFS4ERR_BADCHAR: return "NFS4ERR_BADCHAR"; break;
107
0
        case NFS4ERR_BADNAME: return "NFS4ERR_BADNAME"; break;
108
0
        case NFS4ERR_BAD_RANGE: return "NFS4ERR_BAD_RANGE"; break;
109
0
        case NFS4ERR_LOCK_NOTSUPP: return "NFS4ERR_LOCK_NOTSUPP"; break;
110
0
        case NFS4ERR_OP_ILLEGAL: return "NFS4ERR_OP_ILLEGAL"; break;
111
0
        case NFS4ERR_DEADLOCK: return "NFS4ERR_DEADLOCK"; break;
112
0
        case NFS4ERR_FILE_OPEN: return "NFS4ERR_FILE_OPEN"; break;
113
0
        case NFS4ERR_ADMIN_REVOKED: return "NFS4ERR_ADMIN_REVOKED"; break;
114
0
        case NFS4ERR_CB_PATH_DOWN: return "NFS4ERR_CB_PATH_DOWN"; break;
115
        /* NFSv4.1 (RFC 8881) and NFSv4.2 (RFC 7862) */
116
0
        case NFS4ERR_BADIOMODE: return "NFS4ERR_BADIOMODE"; break;
117
0
        case NFS4ERR_BADLAYOUT: return "NFS4ERR_BADLAYOUT"; break;
118
0
        case NFS4ERR_BAD_SESSION_DIGEST:
119
0
                return "NFS4ERR_BAD_SESSION_DIGEST"; break;
120
0
        case NFS4ERR_BADSESSION: return "NFS4ERR_BADSESSION"; break;
121
0
        case NFS4ERR_BADSLOT: return "NFS4ERR_BADSLOT"; break;
122
0
        case NFS4ERR_COMPLETE_ALREADY:
123
0
                return "NFS4ERR_COMPLETE_ALREADY"; break;
124
0
        case NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
125
0
                return "NFS4ERR_CONN_NOT_BOUND_TO_SESSION"; break;
126
0
        case NFS4ERR_DELEG_ALREADY_WANTED:
127
0
                return "NFS4ERR_DELEG_ALREADY_WANTED"; break;
128
0
        case NFS4ERR_BACK_CHAN_BUSY: return "NFS4ERR_BACK_CHAN_BUSY"; break;
129
0
        case NFS4ERR_LAYOUTTRYLATER: return "NFS4ERR_LAYOUTTRYLATER"; break;
130
0
        case NFS4ERR_LAYOUTUNAVAILABLE:
131
0
                return "NFS4ERR_LAYOUTUNAVAILABLE"; break;
132
0
        case NFS4ERR_NOMATCHING_LAYOUT:
133
0
                return "NFS4ERR_NOMATCHING_LAYOUT"; break;
134
0
        case NFS4ERR_RECALLCONFLICT: return "NFS4ERR_RECALLCONFLICT"; break;
135
0
        case NFS4ERR_UNKNOWN_LAYOUTTYPE:
136
0
                return "NFS4ERR_UNKNOWN_LAYOUTTYPE"; break;
137
0
        case NFS4ERR_SEQ_MISORDERED: return "NFS4ERR_SEQ_MISORDERED"; break;
138
0
        case NFS4ERR_SEQUENCE_POS: return "NFS4ERR_SEQUENCE_POS"; break;
139
0
        case NFS4ERR_REQ_TOO_BIG: return "NFS4ERR_REQ_TOO_BIG"; break;
140
0
        case NFS4ERR_REP_TOO_BIG: return "NFS4ERR_REP_TOO_BIG"; break;
141
0
        case NFS4ERR_REP_TOO_BIG_TO_CACHE:
142
0
                return "NFS4ERR_REP_TOO_BIG_TO_CACHE"; break;
143
0
        case NFS4ERR_RETRY_UNCACHED_REP:
144
0
                return "NFS4ERR_RETRY_UNCACHED_REP"; break;
145
0
        case NFS4ERR_UNSAFE_COMPOUND: return "NFS4ERR_UNSAFE_COMPOUND"; break;
146
0
        case NFS4ERR_TOO_MANY_OPS: return "NFS4ERR_TOO_MANY_OPS"; break;
147
0
        case NFS4ERR_OP_NOT_IN_SESSION:
148
0
                return "NFS4ERR_OP_NOT_IN_SESSION"; break;
149
0
        case NFS4ERR_HASH_ALG_UNSUPP: return "NFS4ERR_HASH_ALG_UNSUPP"; break;
150
0
        case NFS4ERR_CLIENTID_BUSY: return "NFS4ERR_CLIENTID_BUSY"; break;
151
0
        case NFS4ERR_PNFS_IO_HOLE: return "NFS4ERR_PNFS_IO_HOLE"; break;
152
0
        case NFS4ERR_SEQ_FALSE_RETRY: return "NFS4ERR_SEQ_FALSE_RETRY"; break;
153
0
        case NFS4ERR_BAD_HIGH_SLOT: return "NFS4ERR_BAD_HIGH_SLOT"; break;
154
0
        case NFS4ERR_DEADSESSION: return "NFS4ERR_DEADSESSION"; break;
155
0
        case NFS4ERR_ENCR_ALG_UNSUPP: return "NFS4ERR_ENCR_ALG_UNSUPP"; break;
156
0
        case NFS4ERR_PNFS_NO_LAYOUT: return "NFS4ERR_PNFS_NO_LAYOUT"; break;
157
0
        case NFS4ERR_NOT_ONLY_OP: return "NFS4ERR_NOT_ONLY_OP"; break;
158
0
        case NFS4ERR_WRONG_CRED: return "NFS4ERR_WRONG_CRED"; break;
159
0
        case NFS4ERR_WRONG_TYPE: return "NFS4ERR_WRONG_TYPE"; break;
160
0
        case NFS4ERR_DIRDELEG_UNAVAIL:
161
0
                return "NFS4ERR_DIRDELEG_UNAVAIL"; break;
162
0
        case NFS4ERR_REJECT_DELEG: return "NFS4ERR_REJECT_DELEG"; break;
163
0
        case NFS4ERR_RETURNCONFLICT: return "NFS4ERR_RETURNCONFLICT"; break;
164
0
        case NFS4ERR_DELEG_REVOKED: return "NFS4ERR_DELEG_REVOKED"; break;
165
0
        case NFS4ERR_PARTNER_NOTSUPP: return "NFS4ERR_PARTNER_NOTSUPP"; break;
166
0
        case NFS4ERR_PARTNER_NO_AUTH: return "NFS4ERR_PARTNER_NO_AUTH"; break;
167
0
        case NFS4ERR_UNION_NOTSUPP: return "NFS4ERR_UNION_NOTSUPP"; break;
168
0
        case NFS4ERR_OFFLOAD_DENIED: return "NFS4ERR_OFFLOAD_DENIED"; break;
169
0
        case NFS4ERR_WRONG_LFS: return "NFS4ERR_WRONG_LFS"; break;
170
0
        case NFS4ERR_BADLABEL: return "NFS4ERR_BADLABEL"; break;
171
0
        case NFS4ERR_OFFLOAD_NO_REQS: return "NFS4ERR_OFFLOAD_NO_REQS"; break;
172
0
  };
173
0
  return "unknown nfsv4 error";
174
0
}
175
176
int
177
nfsstat4_to_errno(int error)
178
0
{
179
0
  switch (error) {
180
0
        case NFS4_OK: return 0;
181
0
        case NFS4ERR_PERM: return -EPERM;
182
0
        case NFS4ERR_NOENT: return -ENOENT ;
183
0
        case NFS4ERR_IO: return -EIO;
184
0
        case NFS4ERR_NXIO: return -ENXIO;
185
0
        case NFS4ERR_ACCESS: return -EACCES ;
186
0
        case NFS4ERR_EXIST: return -EEXIST;
187
0
        case NFS4ERR_XDEV: return -EXDEV;
188
0
        case NFS4ERR_NOTDIR: return -ENOTDIR ;
189
0
        case NFS4ERR_ISDIR: return -EISDIR ;
190
0
        case NFS4ERR_INVAL: return -EINVAL;
191
0
        case NFS4ERR_FBIG: return -EFBIG;
192
0
        case NFS4ERR_NOSPC: return -ENOSPC;
193
0
        case NFS4ERR_ROFS: return -EROFS;
194
0
        case NFS4ERR_MLINK: return -EMLINK;
195
0
        case NFS4ERR_NAMETOOLONG: return -ENAMETOOLONG;
196
0
        case NFS4ERR_NOTEMPTY: return -ENOTEMPTY;
197
0
        case NFS4ERR_DQUOT: return -ERANGE;
198
0
        case NFS4ERR_STALE: return -ESTALE;
199
0
        case NFS4ERR_BADHANDLE: return -EINVAL;
200
0
        case NFS4ERR_BAD_COOKIE: return -EINVAL;
201
        /*
202
         * The server understood the operation and does not implement
203
         * it, which is exactly ENOTSUP. EINVAL said the arguments were
204
         * wrong instead, and contradicted what the NFSv4.2 and named
205
         * attribute calls document they return for an unsupporting
206
         * server.
207
         */
208
0
        case NFS4ERR_NOTSUPP: return -ENOTSUP;
209
0
        case NFS4ERR_TOOSMALL: return -EIO;
210
0
        case NFS4ERR_SERVERFAULT: return -EIO;
211
0
        case NFS4ERR_BADTYPE: return -EINVAL;
212
0
        case NFS4ERR_DELAY: return -EIO;
213
0
        case NFS4ERR_SAME: return -EIO;
214
0
        case NFS4ERR_DENIED: return -EIO;
215
0
        case NFS4ERR_EXPIRED: return -EAGAIN;
216
0
        case NFS4ERR_LOCKED: return -EIO;
217
0
        case NFS4ERR_GRACE: return -EIO;
218
0
        case NFS4ERR_FHEXPIRED: return -EAGAIN;
219
0
        case NFS4ERR_SHARE_DENIED: return -EIO;
220
0
        case NFS4ERR_WRONGSEC: return -EIO;
221
0
        case NFS4ERR_CLID_INUSE: return -EIO;
222
0
        case NFS4ERR_RESOURCE: return -EIO;
223
0
        case NFS4ERR_MOVED: return -EIO;
224
0
        case NFS4ERR_NOFILEHANDLE: return -EIO;
225
0
        case NFS4ERR_MINOR_VERS_MISMATCH: return -EIO;
226
0
        case NFS4ERR_STALE_CLIENTID: return -EIO;
227
0
        case NFS4ERR_STALE_STATEID: return -EIO;
228
0
        case NFS4ERR_OLD_STATEID: return -EIO;
229
0
        case NFS4ERR_BAD_STATEID: return -EINVAL;
230
0
        case NFS4ERR_BAD_SEQID: return -EINVAL;
231
0
        case NFS4ERR_NOT_SAME: return -EIO;
232
0
        case NFS4ERR_LOCK_RANGE: return -EIO;
233
0
        case NFS4ERR_SYMLINK: return -EIO;
234
0
        case NFS4ERR_RESTOREFH: return -EIO;
235
0
        case NFS4ERR_ATTRNOTSUPP: return -EINVAL;
236
0
        case NFS4ERR_NO_GRACE: return -EIO;
237
0
        case NFS4ERR_RECLAIM_BAD: return -EIO;
238
0
        case NFS4ERR_RECLAIM_CONFLICT: return -EIO;
239
0
        case NFS4ERR_BADZDR: return -EINVAL;
240
0
        case NFS4ERR_LOCKS_HELD: return -EIO;
241
0
        case NFS4ERR_OPENMODE: return -EIO;
242
0
        case NFS4ERR_BADOWNER: return -EINVAL;
243
0
        case NFS4ERR_BADCHAR: return -EINVAL;
244
0
        case NFS4ERR_BADNAME: return -EINVAL;
245
0
        case NFS4ERR_BAD_RANGE: return -EINVAL;
246
0
        case NFS4ERR_LOCK_NOTSUPP: return -EINVAL;
247
0
        case NFS4ERR_OP_ILLEGAL: return -EIO;
248
0
        case NFS4ERR_DEADLOCK: return -EIO;
249
0
        case NFS4ERR_FILE_OPEN: return -EIO;
250
0
        case NFS4ERR_ADMIN_REVOKED: return -EIO;
251
0
        case NFS4ERR_CB_PATH_DOWN: return -EIO;
252
        /* NFSv4.1 (RFC 8881) and NFSv4.2 (RFC 7862) */
253
0
        case NFS4ERR_BADIOMODE: return -EINVAL;
254
0
        case NFS4ERR_BADLAYOUT: return -EINVAL;
255
0
        case NFS4ERR_BAD_SESSION_DIGEST: return -ENOTCONN;
256
0
        case NFS4ERR_BADSESSION: return -ENOTCONN;
257
0
        case NFS4ERR_BADSLOT: return -EIO;
258
0
        case NFS4ERR_COMPLETE_ALREADY: return -EIO;
259
0
        case NFS4ERR_CONN_NOT_BOUND_TO_SESSION: return -ENOTCONN;
260
0
        case NFS4ERR_DELEG_ALREADY_WANTED: return -EALREADY;
261
0
        case NFS4ERR_BACK_CHAN_BUSY: return -EBUSY;
262
0
        case NFS4ERR_LAYOUTTRYLATER: return -EAGAIN;
263
0
        case NFS4ERR_LAYOUTUNAVAILABLE: return -ENOTSUP;
264
0
        case NFS4ERR_NOMATCHING_LAYOUT: return -ENOENT;
265
0
        case NFS4ERR_RECALLCONFLICT: return -EAGAIN;
266
0
        case NFS4ERR_UNKNOWN_LAYOUTTYPE: return -ENOTSUP;
267
0
        case NFS4ERR_SEQ_MISORDERED: return -EIO;
268
0
        case NFS4ERR_SEQUENCE_POS: return -EIO;
269
0
        case NFS4ERR_REQ_TOO_BIG: return -EFBIG;
270
0
        case NFS4ERR_REP_TOO_BIG: return -EFBIG;
271
0
        case NFS4ERR_REP_TOO_BIG_TO_CACHE: return -EFBIG;
272
0
        case NFS4ERR_RETRY_UNCACHED_REP: return -EIO;
273
0
        case NFS4ERR_UNSAFE_COMPOUND: return -EIO;
274
0
        case NFS4ERR_TOO_MANY_OPS: return -E2BIG;
275
0
        case NFS4ERR_OP_NOT_IN_SESSION: return -EIO;
276
0
        case NFS4ERR_HASH_ALG_UNSUPP: return -ENOTSUP;
277
0
        case NFS4ERR_CLIENTID_BUSY: return -EBUSY;
278
0
        case NFS4ERR_PNFS_IO_HOLE: return -EIO;
279
0
        case NFS4ERR_SEQ_FALSE_RETRY: return -EIO;
280
0
        case NFS4ERR_BAD_HIGH_SLOT: return -EIO;
281
0
        case NFS4ERR_DEADSESSION: return -ENOTCONN;
282
0
        case NFS4ERR_ENCR_ALG_UNSUPP: return -ENOTSUP;
283
0
        case NFS4ERR_PNFS_NO_LAYOUT: return -EIO;
284
0
        case NFS4ERR_NOT_ONLY_OP: return -EIO;
285
0
        case NFS4ERR_WRONG_CRED: return -EPERM;
286
0
        case NFS4ERR_WRONG_TYPE: return -EINVAL;
287
0
        case NFS4ERR_DIRDELEG_UNAVAIL: return -ENOTSUP;
288
0
        case NFS4ERR_REJECT_DELEG: return -EIO;
289
0
        case NFS4ERR_RETURNCONFLICT: return -EAGAIN;
290
0
        case NFS4ERR_DELEG_REVOKED: return -EIO;
291
0
        case NFS4ERR_PARTNER_NOTSUPP: return -ENOTSUP;
292
0
        case NFS4ERR_PARTNER_NO_AUTH: return -EPERM;
293
0
        case NFS4ERR_UNION_NOTSUPP: return -ENOTSUP;
294
0
        case NFS4ERR_OFFLOAD_DENIED: return -EPERM;
295
0
        case NFS4ERR_WRONG_LFS: return -EINVAL;
296
0
        case NFS4ERR_BADLABEL: return -EINVAL;
297
0
        case NFS4ERR_OFFLOAD_NO_REQS: return -ENOTSUP;
298
0
  };
299
0
  return -ERANGE;
300
0
}
301
302
#ifdef HAVE_NFS4_2
303
/*
304
 * NFSv4.1+ session slots.
305
 *
306
 * Every COMPOUND past the bootstrap leads with SEQUENCE, which names a slot
307
 * and a sequence id within that slot. RFC 8881 2.10.6.1 allows only one
308
 * request at a time per slot: the server keys its reply cache on the pair, so
309
 * a second request sent on a busy slot is either taken for a retransmit of
310
 * the first or rejected with NFS4ERR_SEQ_MISORDERED. The number of slots the
311
 * session was granted is therefore also the number of COMPOUNDs that may be
312
 * in flight at once.
313
 *
314
 * A slot is taken as the COMPOUND is written to the socket, not when it was
315
 * built, so that an application can queue up as many requests as it likes and
316
 * only the number on the wire is bounded. It is given back by rpc_free_pdu()
317
 * when that COMPOUND is finished with, however it finished, that being the
318
 * one place every pdu passes through.
319
 */
320
void
321
nfs4_session_mutex_init(struct rpc_context *rpc)
322
0
{
323
#ifdef HAVE_MULTITHREADING
324
        nfs_mt_mutex_init(&rpc->nfs4_slot_mutex);
325
#endif /* HAVE_MULTITHREADING */
326
0
        (void)rpc;
327
0
}
328
329
void
330
nfs4_session_mutex_destroy(struct rpc_context *rpc)
331
2.61k
{
332
#ifdef HAVE_MULTITHREADING
333
        nfs_mt_mutex_destroy(&rpc->nfs4_slot_mutex);
334
#endif /* HAVE_MULTITHREADING */
335
2.61k
        (void)rpc;
336
2.61k
}
337
338
/*
339
 * The slot table has a lock of its own rather than sharing rpc_mutex, because
340
 * slots are taken and released from inside regions that already hold
341
 * rpc_mutex and that mutex is not recursive. Nothing under this lock reaches
342
 * for rpc_mutex, so the two cannot deadlock against each other.
343
 */
344
static void
345
nfs4_session_lock(struct rpc_context *rpc)
346
2.61k
{
347
#ifdef HAVE_MULTITHREADING
348
        if (rpc->multithreading_enabled) {
349
                nfs_mt_mutex_lock(&rpc->nfs4_slot_mutex);
350
        }
351
#endif /* HAVE_MULTITHREADING */
352
2.61k
        (void)rpc;
353
2.61k
}
354
355
static void
356
nfs4_session_unlock(struct rpc_context *rpc)
357
2.61k
{
358
#ifdef HAVE_MULTITHREADING
359
        if (rpc->multithreading_enabled) {
360
                nfs_mt_mutex_unlock(&rpc->nfs4_slot_mutex);
361
        }
362
#endif /* HAVE_MULTITHREADING */
363
2.61k
        (void)rpc;
364
2.61k
}
365
366
int
367
nfs4_session_init(struct rpc_context *rpc, const char *sessionid,
368
                  uint32_t slot_count)
369
0
{
370
0
        struct nfs4_slot *slots;
371
372
0
        if (slot_count < 1) {
373
0
                return -1;
374
0
        }
375
0
        if (slot_count > NFS4_MAX_SLOTS) {
376
0
                slot_count = NFS4_MAX_SLOTS;
377
0
        }
378
379
0
        slots = calloc(slot_count, sizeof(struct nfs4_slot));
380
0
        if (slots == NULL) {
381
0
                return -1;
382
0
        }
383
384
0
        nfs4_session_lock(rpc);
385
0
        free(rpc->nfs4_slots);
386
0
        memcpy(rpc->nfs4_sessionid, sessionid, sizeof(sessionid4));
387
0
        rpc->nfs4_slots = slots;
388
0
        rpc->nfs4_slot_count = slot_count;
389
0
        rpc->nfs4_slots_in_use = 0;
390
0
        rpc->nfs4_slot_hint = 0;
391
0
        rpc->nfs4_renew_due = rpc_current_time() + NFS4_SESSION_RENEW_MSECS;
392
0
        rpc->nfs4_session_valid = 1;
393
0
        nfs4_session_unlock(rpc);
394
0
        nfs4_cb_reset(rpc);
395
396
0
        return 0;
397
0
}
398
399
void
400
nfs4_session_destroy(struct rpc_context *rpc)
401
2.61k
{
402
2.61k
        nfs4_session_lock(rpc);
403
2.61k
        rpc->nfs4_session_valid = 0;
404
2.61k
        rpc->nfs4_slot_count = 0;
405
2.61k
        rpc->nfs4_slots_in_use = 0;
406
2.61k
        free(rpc->nfs4_slots);
407
2.61k
        rpc->nfs4_slots = NULL;
408
2.61k
        nfs4_session_unlock(rpc);
409
2.61k
        nfs4_cb_reset(rpc);
410
2.61k
}
411
412
/*
413
 * Take a free slot and advance its sequence id. Returns -1 when every slot is
414
 * busy, i.e. when the application already has as many COMPOUNDs outstanding
415
 * as the session can carry.
416
 */
417
static int
418
nfs4_session_get_slot(struct rpc_context *rpc, SEQUENCE4args *sargs,
419
                      uint32_t *slotid)
420
0
{
421
0
        uint32_t i = 0, n;
422
423
0
        nfs4_session_lock(rpc);
424
0
        if (!rpc->nfs4_session_valid) {
425
0
                nfs4_session_unlock(rpc);
426
0
                return -1;
427
0
        }
428
0
        if (rpc->nfs4_slots_in_use >= rpc->nfs4_slot_count) {
429
0
                nfs4_session_unlock(rpc);
430
0
                return -1;
431
0
        }
432
        /*
433
         * Start where the last search left off. Slots are taken and released
434
         * in roughly the order requests are issued, so the next free one is
435
         * almost always the next one along and this finds it immediately
436
         * rather than rescanning the busy run from the start every time.
437
         */
438
0
        for (n = 0; n < rpc->nfs4_slot_count; n++) {
439
0
                i = (rpc->nfs4_slot_hint + n) % rpc->nfs4_slot_count;
440
0
                if (!rpc->nfs4_slots[i].in_use) {
441
0
                        break;
442
0
                }
443
0
        }
444
0
        assert(n < rpc->nfs4_slot_count);
445
0
        rpc->nfs4_slot_hint = (i + 1) % rpc->nfs4_slot_count;
446
0
        rpc->nfs4_slots[i].in_use = 1;
447
0
        rpc->nfs4_slots_in_use++;
448
        /*
449
         * The first request on a slot uses sequence id 1 and each new request
450
         * on it advances by one, so that the server can tell a fresh request
451
         * from a retransmit of the previous one.
452
         */
453
0
        rpc->nfs4_slots[i].seqid++;
454
455
0
        memcpy(sargs->sa_sessionid, rpc->nfs4_sessionid, sizeof(sessionid4));
456
0
        sargs->sa_sequenceid = rpc->nfs4_slots[i].seqid;
457
0
        sargs->sa_slotid = i;
458
0
        sargs->sa_highest_slotid = rpc->nfs4_slot_count - 1;
459
0
        sargs->sa_cachethis = 0;
460
0
        nfs4_session_unlock(rpc);
461
462
0
        *slotid = i;
463
0
        return 0;
464
0
}
465
466
/*
467
 * Give a slot back. rollback is for a COMPOUND that was abandoned before it
468
 * could be queued for sending: the server never saw that sequence id, so
469
 * leaving it consumed would put a gap in the slot that the next request there
470
 * is rejected for.
471
 */
472
void
473
nfs4_session_put_slot(struct rpc_context *rpc, uint32_t slotid, int rollback)
474
0
{
475
0
        nfs4_session_lock(rpc);
476
0
        if (rpc->nfs4_slots && slotid < rpc->nfs4_slot_count) {
477
0
                if (rollback && rpc->nfs4_slots[slotid].seqid > 0) {
478
0
                        rpc->nfs4_slots[slotid].seqid--;
479
0
                }
480
0
                if (rpc->nfs4_slots[slotid].in_use) {
481
0
                        rpc->nfs4_slots[slotid].in_use = 0;
482
0
                        rpc->nfs4_slots_in_use--;
483
0
                }
484
0
        }
485
0
        nfs4_session_unlock(rpc);
486
0
}
487
488
/*
489
 * Note where the leading SEQUENCE of a COMPOUND is going to land in the
490
 * encoded buffer, and mark the pdu as needing a slot. Called before the args
491
 * are encoded. A COMPOUND without a leading SEQUENCE, which is any of the
492
 * bootstrap ones and every NFSv4.0 COMPOUND, is left alone.
493
 *
494
 * The slot itself is not taken here. Taking one at encode time would cap how
495
 * many requests an application may have outstanding at the number of slots
496
 * the session was granted, which for libnfs is the wrong trade: submission
497
 * should stay unbounded and the slot count should only bound how many
498
 * COMPOUNDs are on the wire at once. So the slot is taken later, by
499
 * nfs4_pdu_take_slot() as the pdu is written, and everything behind it simply
500
 * waits in the send queue.
501
 */
502
static int
503
nfs4_pdu_note_sequence(struct rpc_context *rpc, struct rpc_pdu *pdu,
504
                       struct COMPOUND4args *args, uint32_t start)
505
0
{
506
0
        if (args->argarray.argarray_len < 1 ||
507
0
            args->argarray.argarray_val[0].argop != OP_SEQUENCE) {
508
0
                return 0;
509
0
        }
510
511
        /*
512
         * With Kerberos integrity or privacy the payload is signed or wrapped
513
         * once it has been marshalled, so it cannot be touched again
514
         * afterwards. There the slot has to be taken now and stamped into the
515
         * args before they are encoded, which does put a request back under
516
         * the old ceiling of one outstanding COMPOUND per slot. Plain AUTH_SYS
517
         * and AUTH_TLS, which is everything else, take theirs at send time.
518
         */
519
#ifdef HAVE_LIBKRB5
520
        if (rpc->sec == RPC_SEC_KRB5I || rpc->sec == RPC_SEC_KRB5P) {
521
                uint32_t slotid;
522
523
                if (nfs4_session_get_slot(rpc,
524
                        &args->argarray.argarray_val[0].nfs_argop4_u.opsequence,
525
                        &slotid) < 0) {
526
                        rpc_set_error(rpc, "NFSv4.2 session has no free slot. "
527
                                      "All %d are in use by requests that have "
528
                                      "not completed yet.",
529
                                      rpc->nfs4_slot_count);
530
                        return -1;
531
                }
532
                pdu->nfs4_slot = slotid;
533
                pdu->nfs4_slot_held = 1;
534
                rpc->nfs4_renew_due = rpc_current_time() +
535
                        NFS4_SESSION_RENEW_MSECS;
536
                return 0;
537
        }
538
#endif /* HAVE_LIBKRB5 */
539
540
        /*
541
         * COMPOUND4args is a tag, a minor version and the operation array,
542
         * and the first operation here is the SEQUENCE whose args we want to
543
         * come back to. Everything ahead of those args is fixed width, so
544
         * their offset can be counted out: the tag's length word and its
545
         * padded contents, the minor version, the operation count and the
546
         * opcode itself.
547
         */
548
0
        pdu->nfs4_seq_pos = start + 4 +
549
0
                ((args->tag.utf8string_len + 3) & ~3u) + 4 + 4 + 4;
550
0
        pdu->nfs4_needs_slot = 1;
551
552
0
        return 0;
553
0
}
554
555
/*
556
 * Take a slot for a pdu that is about to be written, and stamp the slot id
557
 * and its sequence id into the SEQUENCE that was encoded earlier. Returns -1
558
 * when every slot is busy, which means the caller should leave this pdu
559
 * queued and come back to it once one frees up.
560
 */
561
int
562
nfs4_pdu_take_slot(struct rpc_context *rpc, struct rpc_pdu *pdu)
563
0
{
564
0
        SEQUENCE4args sargs;
565
0
        uint32_t slotid, pos;
566
567
0
        if (!pdu->nfs4_needs_slot || pdu->nfs4_slot_held) {
568
0
                return 0;
569
0
        }
570
571
0
        memset(&sargs, 0, sizeof(sargs));
572
0
        if (nfs4_session_get_slot(rpc, &sargs, &slotid) < 0) {
573
0
                return -1;
574
0
        }
575
576
        /*
577
         * Rewrite SEQUENCE4args where it already sits. The encode buffer is
578
         * what the socket writes from and the record is fixed width up to
579
         * this point, so seeking back and re-encoding just these args
580
         * disturbs nothing else.
581
         */
582
0
        pos = zdr_getpos(&pdu->zdr);
583
0
        assert(pdu->nfs4_seq_pos < pos);
584
0
        zdr_setpos(&pdu->zdr, pdu->nfs4_seq_pos);
585
        /*
586
         * Cannot fail: these are fixed width fields being written back over
587
         * the ones already encoded at this position, so there is nothing to
588
         * grow the buffer for.
589
         */
590
0
        (void)zdr_SEQUENCE4args(&pdu->zdr, &sargs);
591
0
        assert(zdr_getpos(&pdu->zdr) <= pos);
592
0
        zdr_setpos(&pdu->zdr, pos);
593
594
0
        pdu->nfs4_slot = slotid;
595
0
        pdu->nfs4_slot_held = 1;
596
597
        /*
598
         * Any SEQUENCE renews the lease, so a connection that is being used
599
         * never needs a renewal of its own.
600
         */
601
0
        rpc->nfs4_renew_due = rpc_current_time() + NFS4_SESSION_RENEW_MSECS;
602
603
0
        return 0;
604
0
}
605
606
/*
607
 * Whether a COMPOUND that needs a slot could be sent right now. The send path
608
 * asks this before requesting POLLOUT, so that a full slot table parks the
609
 * writer instead of spinning on a socket it must not write to yet.
610
 */
611
int
612
nfs4_session_has_free_slot(struct rpc_context *rpc)
613
0
{
614
0
        int free_slot;
615
616
0
        nfs4_session_lock(rpc);
617
0
        free_slot = !rpc->nfs4_session_valid ||
618
0
                rpc->nfs4_slots_in_use < rpc->nfs4_slot_count;
619
0
        nfs4_session_unlock(rpc);
620
621
0
        return free_slot;
622
0
}
623
624
/* RFC 8881 15.1.1.3: replay only if no state-changing op has succeeded. */
625
static void
626
nfs4_pdu_note_delay(struct rpc_pdu *pdu, const COMPOUND4args *args)
627
0
{
628
0
        uint32_t i;
629
630
0
        if (!pdu->nfs4_needs_slot) {
631
0
                return;
632
0
        }
633
0
        for (i = 0; i < args->argarray.argarray_len; i++) {
634
0
                pdu->nfs4_delay_maxres = i + 1;
635
0
                switch (args->argarray.argarray_val[i].argop) {
636
0
                case OP_SEQUENCE:
637
0
                case OP_PUTFH:
638
0
                case OP_PUTROOTFH:
639
0
                case OP_SAVEFH:
640
0
                case OP_RESTOREFH:
641
0
                case OP_LOOKUP:
642
0
                case OP_LOOKUPP:
643
0
                case OP_GETFH:
644
0
                case OP_GETATTR:
645
0
                case OP_ACCESS:
646
0
                case OP_READLINK:
647
0
                        break;
648
0
                default:
649
0
                        return;
650
0
                }
651
0
        }
652
0
}
653
654
0
#define NFS4_DELAY_MAX_ATTEMPTS 8
655
0
#define NFS4_DELAY_BASE_MSECS   100u
656
0
#define NFS4_DELAY_MAX_MSECS    1000u
657
658
int
659
nfs4_pdu_retry_delay(struct rpc_context *rpc, struct rpc_pdu *pdu,
660
                     const COMPOUND4res *res)
661
0
{
662
0
        uint32_t count, delay, pos;
663
664
        /* Leave v4.0, GSS, zero-copy I/O and SEQUENCE errors unchanged. */
665
0
        if (!pdu->nfs4_delay_maxres || rpc->nfs4_minorversion == 0 ||
666
0
            rpc->is_udp || pdu->msg.body.cbody.cred.oa_flavor != AUTH_SYS || pdu->in.base ||
667
0
            pdu->do_not_retry || !pdu->nfs4_slot_held || !res ||
668
0
            res->status != NFS4ERR_DELAY ||
669
0
            pdu->nfs4_delay_attempts >= NFS4_DELAY_MAX_ATTEMPTS) {
670
0
                return 0;
671
0
        }
672
0
        count = res->resarray.resarray_len;
673
0
        if (count < 2 || count > pdu->nfs4_delay_maxres ||
674
0
            res->resarray.resarray_val[0].resop != OP_SEQUENCE ||
675
0
            res->resarray.resarray_val[0].nfs_resop4_u.opsequence.sr_status != NFS4_OK) {
676
0
                return 0;
677
0
        }
678
679
        /* A new logical request needs a new sequence, not a transport replay. */
680
0
        nfs4_session_put_slot(rpc, pdu->nfs4_slot, 0);
681
0
        pdu->nfs4_slot_held = 0;
682
0
        pdu->nfs4_slot_sent = 0;
683
0
        delay = pdu->nfs4_delay_attempts < 4 ?
684
0
                NFS4_DELAY_BASE_MSECS << pdu->nfs4_delay_attempts :
685
0
                NFS4_DELAY_MAX_MSECS;
686
0
        pdu->nfs4_delay_attempts++;
687
0
        pdu->nfs4_delay_until = rpc_current_time() + delay;
688
689
#ifdef HAVE_MULTITHREADING
690
        if (rpc->multithreading_enabled) {
691
                nfs_mt_mutex_lock(&rpc->rpc_mutex);
692
        }
693
#endif
694
0
        pdu->xid = rpc->xid++;
695
#ifdef HAVE_MULTITHREADING
696
        if (rpc->multithreading_enabled) {
697
                nfs_mt_mutex_unlock(&rpc->rpc_mutex);
698
        }
699
#endif
700
        /* Preserve the encoded arguments and the original caller's credentials. */
701
0
        pos = zdr_getpos(&pdu->zdr);
702
0
        zdr_setpos(&pdu->zdr, 0);
703
0
        (void)zdr_uint32_t(&pdu->zdr, &pdu->xid);
704
0
        zdr_setpos(&pdu->zdr, pos);
705
0
        pdu->pdu_stats.xid = pdu->xid;
706
0
        pdu->msg.xid = pdu->xid;
707
0
        RPC_LOG(rpc, 2, "NFS4ERR_DELAY: retry %u in %u ms",
708
0
                pdu->nfs4_delay_attempts, delay);
709
0
        return 1;
710
0
}
711
712
void
713
nfs4_defer_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu)
714
0
{
715
0
        pdu->zdr_decode_buf = NULL;
716
0
        pdu->out.num_done = 0;
717
0
        rpc_reset_cursor(rpc, &pdu->in);
718
0
        pdu->timeout = 0;
719
#ifdef HAVE_MULTITHREADING
720
        if (rpc->multithreading_enabled) {
721
                nfs_mt_mutex_lock(&rpc->rpc_mutex);
722
        }
723
#endif
724
0
        rpc_enqueue(&rpc->nfs4_delay_queue, pdu);
725
0
        rpc->nfs4_delay_queue_len++;
726
0
        rpc_wakeup_service_thread(rpc);
727
#ifdef HAVE_MULTITHREADING
728
        if (rpc->multithreading_enabled) {
729
                nfs_mt_mutex_unlock(&rpc->rpc_mutex);
730
        }
731
#endif
732
0
}
733
734
/* Milliseconds until the earliest pending DELAY retry, or -1 if none. */
735
int
736
nfs4_next_delay_msecs(struct rpc_context *rpc)
737
0
{
738
0
        struct rpc_pdu *pdu;
739
0
        uint64_t now, due = 0;
740
0
        int msecs;
741
742
#ifdef HAVE_MULTITHREADING
743
        if (rpc->multithreading_enabled) {
744
                nfs_mt_mutex_lock(&rpc->rpc_mutex);
745
        }
746
#endif
747
0
        for (pdu = rpc->nfs4_delay_queue.head; pdu; pdu = pdu->next) {
748
0
                if (due == 0 || pdu->nfs4_delay_until < due) {
749
0
                        due = pdu->nfs4_delay_until;
750
0
                }
751
0
        }
752
#ifdef HAVE_MULTITHREADING
753
        if (rpc->multithreading_enabled) {
754
                nfs_mt_mutex_unlock(&rpc->rpc_mutex);
755
        }
756
#endif
757
0
        if (due == 0) {
758
0
                return -1;
759
0
        }
760
761
0
        now = rpc_current_time();
762
0
        if (now >= due) {
763
0
                return 0;
764
0
        }
765
0
        msecs = (int)(due - now);
766
767
0
        return msecs;
768
0
}
769
770
void
771
nfs4_service_delayed(struct rpc_context *rpc)
772
0
{
773
0
        struct rpc_pdu *pdu, *next;
774
0
        uint64_t now = rpc_current_time();
775
776
#ifdef HAVE_MULTITHREADING
777
        if (rpc->multithreading_enabled) {
778
                nfs_mt_mutex_lock(&rpc->rpc_mutex);
779
        }
780
#endif
781
0
        for (pdu = rpc->nfs4_delay_queue.head; pdu; pdu = next) {
782
0
                next = pdu->next;
783
0
                if (now < pdu->nfs4_delay_until) {
784
0
                        continue;
785
0
                }
786
0
                rpc_remove_pdu_from_queue(&rpc->nfs4_delay_queue, pdu);
787
0
                rpc->nfs4_delay_queue_len--;
788
0
                pdu->nfs4_delay_until = 0;
789
0
                pdu_set_timeout(rpc, pdu, now);
790
0
                rpc_add_to_outqueue_lowp(rpc, pdu);
791
0
        }
792
#ifdef HAVE_MULTITHREADING
793
        if (rpc->multithreading_enabled) {
794
                nfs_mt_mutex_unlock(&rpc->rpc_mutex);
795
        }
796
#endif
797
0
}
798
799
/* Caller holds rpc_mutex. */
800
void
801
nfs4_requeue_delayed(struct rpc_context *rpc)
802
0
{
803
0
        struct rpc_pdu *pdu, *next;
804
805
0
        for (pdu = rpc->nfs4_delay_queue.head; pdu; pdu = next) {
806
0
                next = pdu->next;
807
0
                rpc_remove_pdu_from_queue(&rpc->nfs4_delay_queue, pdu);
808
0
                rpc->nfs4_delay_queue_len--;
809
0
                pdu->nfs4_delay_until = 0;
810
0
                rpc_return_to_outqueue(rpc, pdu);
811
0
        }
812
0
}
813
814
/*
815
 * Send a COMPOUND holding exactly one session management operation.
816
 *
817
 * BIND_CONN_TO_SESSION and DESTROY_SESSION are both operations that RFC 8881
818
 * requires to travel on their own, without a leading SEQUENCE, so they take
819
 * no slot and go out even when every slot is busy. prio lets the caller put
820
 * one at the head of the send queue, which is what rebinding a session to a
821
 * fresh connection needs: nothing else on that connection may be sent first.
822
 */
823
static struct rpc_pdu *
824
nfs4_session_op_task(struct rpc_context *rpc, rpc_cb cb, nfs_argop4 *op,
825
                     int prio, void *private_data, const char *what)
826
0
{
827
0
        struct rpc_pdu *pdu;
828
0
        COMPOUND4args args;
829
830
0
        pdu = rpc_allocate_pdu(rpc, NFS4_PROGRAM, NFS_V4, NFSPROC4_COMPOUND,
831
0
                               cb, private_data, (zdrproc_t)zdr_COMPOUND4res,
832
0
                               sizeof(COMPOUND4res));
833
0
        if (pdu == NULL) {
834
0
                rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for "
835
0
                              "NFS4/%s", what);
836
0
                return NULL;
837
0
        }
838
839
0
        memset(&args, 0, sizeof(args));
840
0
        args.minorversion = rpc->nfs4_minorversion;
841
0
        args.argarray.argarray_len = 1;
842
0
        args.argarray.argarray_val = op;
843
844
0
        if (zdr_COMPOUND4args(&pdu->zdr, &args) == 0) {
845
0
                rpc_set_error(rpc, "ZDR error: Failed to encode COMPOUND4args "
846
0
                              "for %s", what);
847
0
                rpc_free_pdu(rpc, pdu);
848
0
                return NULL;
849
0
        }
850
851
0
        if (rpc_queue_pdu2(rpc, pdu, prio) != 0) {
852
0
                rpc_set_error(rpc, "Failed to queue pdu for NFS4/%s", what);
853
0
                return NULL;
854
0
        }
855
856
0
        return pdu;
857
0
}
858
859
/*
860
 * A session is bound to the connection it was created on. After an automatic
861
 * reconnect the session is still alive on the server, but the new connection
862
 * is not associated with it and every COMPOUND on it would be answered with
863
 * NFS4ERR_CONN_NOT_BOUND_TO_SESSION. This reassociates the two, and must be
864
 * the first thing sent on the new connection.
865
 */
866
struct rpc_pdu *
867
rpc_nfs4_bind_conn_to_session_task(struct rpc_context *rpc, rpc_cb cb,
868
                                   void *private_data)
869
0
{
870
0
        nfs_argop4 op;
871
0
        BIND_CONN_TO_SESSION4args *bargs;
872
873
0
        memset(&op, 0, sizeof(op));
874
0
        op.argop = OP_BIND_CONN_TO_SESSION;
875
0
        bargs = &op.nfs_argop4_u.opbindconntosession;
876
877
0
        memcpy(bargs->bctsa_sessid, rpc->nfs4_sessionid, sizeof(sessionid4));
878
0
        bargs->bctsa_dir = rpc->nfs4_cb_fn && !rpc_is_rdma(rpc) ?
879
0
                CDFC4_FORE_OR_BOTH : CDFC4_FORE;
880
0
        bargs->bctsa_use_conn_in_rdma_mode = 0;
881
882
0
        return nfs4_session_op_task(rpc, cb, &op, PDU_Q_PRIO_HI, private_data,
883
0
                                    "BIND_CONN_TO_SESSION");
884
0
}
885
886
/*
887
 * Release the session. Without this the server holds the session, and the
888
 * state under it, until the lease expires.
889
 */
890
struct rpc_pdu *
891
rpc_nfs4_destroy_session_task(struct rpc_context *rpc, rpc_cb cb,
892
                              void *private_data)
893
0
{
894
0
        nfs_argop4 op;
895
896
0
        memset(&op, 0, sizeof(op));
897
0
        op.argop = OP_DESTROY_SESSION;
898
0
        memcpy(op.nfs_argop4_u.opdestroysession.dsa_sessionid,
899
0
               rpc->nfs4_sessionid, sizeof(sessionid4));
900
901
0
        return nfs4_session_op_task(rpc, cb, &op, PDU_Q_PRIO_LOW, private_data,
902
0
                                    "DESTROY_SESSION");
903
0
}
904
905
/*
906
 * A COMPOUND that is nothing but its own SEQUENCE. It does no work; sending
907
 * one renews the session's lease, which is what keeps an otherwise idle
908
 * connection's session from being reaped by the server.
909
 *
910
 * It goes through the ordinary COMPOUND path so that it takes a slot and is
911
 * sequenced like anything else.
912
 */
913
struct rpc_pdu *
914
rpc_nfs4_renew_session_task(struct rpc_context *rpc, rpc_cb cb,
915
                            void *private_data)
916
0
{
917
0
        COMPOUND4args args;
918
0
        nfs_argop4 op;
919
920
0
        memset(&op, 0, sizeof(op));
921
0
        op.argop = OP_SEQUENCE;
922
923
0
        memset(&args, 0, sizeof(args));
924
0
        args.argarray.argarray_len = 1;
925
0
        args.argarray.argarray_val = &op;
926
927
0
        return rpc_nfs4_compound_task(rpc, cb, &args, private_data);
928
0
}
929
930
int
931
rpc_nfs4_session_is_valid(struct rpc_context *rpc)
932
0
{
933
0
        return rpc->nfs4_session_valid;
934
0
}
935
936
/*
937
 * Back channel. The server sends CB_NULL and CB_COMPOUND calls on our
938
 * connection. There is one back channel slot, so the server has at most one
939
 * CB_COMPOUND outstanding, and the last reply is kept to answer a replay.
940
 */
941
0
#define NFS4_CB_MAX_OPS   8
942
0
#define NFS4_CB_MAX_REPLY 16384
943
944
struct nfs4_cb_reply {
945
        nfsstat4 status;
946
        utf8str_cs tag;
947
        uint32_t nres;
948
        nfs_cb_resop4 res[NFS4_CB_MAX_OPS];
949
};
950
951
/* A reply that is already encoded, the cached one or a fresh one. */
952
struct nfs4_cb_raw {
953
        char *buf;
954
        uint32_t len;
955
};
956
957
void
958
nfs4_cb_reset(struct rpc_context *rpc)
959
2.61k
{
960
2.61k
        rpc->nfs4_cb_up = 0;
961
2.61k
        rpc->nfs4_cb_seqid = 0;
962
2.61k
        free(rpc->nfs4_cb_reply);
963
2.61k
        rpc->nfs4_cb_reply = NULL;
964
2.61k
        rpc->nfs4_cb_reply_len = 0;
965
2.61k
}
966
967
static int
968
nfs4_cb_is_op(uint32_t op, uint32_t minorversion)
969
0
{
970
        /* CB_GETATTR .. CB_NOTIFY_DEVICEID, and CB_OFFLOAD in 4.2. */
971
0
        return op >= OP_CB_GETATTR && op <= (minorversion >= 2 ? 15 : 14);
972
0
}
973
974
/* The ops we decode and hand to the application. */
975
static int
976
nfs4_cb_app_op(uint32_t op)
977
0
{
978
0
        switch (op) {
979
0
        case OP_CB_GETATTR:
980
0
        case OP_CB_RECALL:
981
0
        case OP_CB_PUSH_DELEG:
982
0
        case OP_CB_RECALL_ANY:
983
0
        case OP_CB_RECALLABLE_OBJ_AVAIL:
984
0
        case OP_CB_OFFLOAD:
985
0
                return 1;
986
0
        }
987
0
        return 0;
988
0
}
989
990
static uint32_t
991
zdr_nfs4_cb_reply(ZDR *zdrs, struct nfs4_cb_reply *r)
992
0
{
993
0
        uint32_t i;
994
995
0
        if (!zdr_nfsstat4(zdrs, &r->status) ||
996
0
            !zdr_utf8str_cs(zdrs, &r->tag) ||
997
0
            !zdr_uint32_t(zdrs, &r->nres)) {
998
0
                return FALSE;
999
0
        }
1000
0
        for (i = 0; i < r->nres; i++) {
1001
0
                nfs_cb_resop4 *res = &r->res[i];
1002
1003
0
                if (nfs4_cb_app_op(res->resop) ||
1004
0
                    res->resop == OP_CB_SEQUENCE ||
1005
0
                    res->resop == OP_CB_ILLEGAL) {
1006
0
                        if (!zdr_nfs_cb_resop4(zdrs, res)) {
1007
0
                                return FALSE;
1008
0
                        }
1009
0
                } else if (!zdr_uint32_t(zdrs, &res->resop) ||
1010
0
                           !zdr_nfsstat4(zdrs, &res->nfs_cb_resop4_u.opcbillegal.status)) {
1011
                        /* Only failures for ops we do not decode. */
1012
0
                        return FALSE;
1013
0
                }
1014
0
        }
1015
0
        return TRUE;
1016
0
}
1017
1018
static uint32_t
1019
zdr_nfs4_cb_raw(ZDR *zdrs, struct nfs4_cb_raw *raw)
1020
0
{
1021
0
        return zdr_opaque(zdrs, raw->buf, raw->len);
1022
0
}
1023
1024
static int
1025
nfs4_cb_send_raw(struct rpc_context *rpc, uint32_t xid, char *buf,
1026
                 uint32_t len)
1027
0
{
1028
0
        struct nfs4_cb_raw raw;
1029
1030
0
        raw.buf = buf;
1031
0
        raw.len = len;
1032
0
        return rpc_send_cb_reply(rpc, xid, SUCCESS, &raw,
1033
0
                                 (zdrproc_t)zdr_nfs4_cb_raw, len);
1034
0
}
1035
1036
/*
1037
 * Check CB_SEQUENCE against the slot. Returns 1 for a new request, 0 for
1038
 * a replay of the last one, and -1 with the error in res otherwise.
1039
 */
1040
static int
1041
nfs4_cb_sequence(struct rpc_context *rpc, CB_SEQUENCE4args *args,
1042
                 CB_SEQUENCE4res *res)
1043
0
{
1044
0
        CB_SEQUENCE4resok *ok = &res->CB_SEQUENCE4res_u.csr_resok4;
1045
1046
0
        if (!rpc->nfs4_session_valid ||
1047
0
            memcmp(args->csa_sessionid, rpc->nfs4_sessionid,
1048
0
                   sizeof(sessionid4))) {
1049
0
                res->csr_status = NFS4ERR_BADSESSION;
1050
0
                return -1;
1051
0
        }
1052
0
        if (args->csa_slotid != 0) {
1053
0
                res->csr_status = NFS4ERR_BADSLOT;
1054
0
                return -1;
1055
0
        }
1056
        /* Every new request's reply is cached, so this is a replay. */
1057
0
        if (rpc->nfs4_cb_reply && args->csa_sequenceid == rpc->nfs4_cb_seqid) {
1058
0
                return 0;
1059
0
        }
1060
0
        if (args->csa_sequenceid != rpc->nfs4_cb_seqid + 1) {
1061
0
                res->csr_status = NFS4ERR_SEQ_MISORDERED;
1062
0
                return -1;
1063
0
        }
1064
1065
0
        res->csr_status = NFS4_OK;
1066
0
        memcpy(ok->csr_sessionid, args->csa_sessionid, sizeof(sessionid4));
1067
0
        ok->csr_sequenceid = args->csa_sequenceid;
1068
0
        ok->csr_slotid = 0;
1069
0
        ok->csr_highest_slotid = 0;
1070
0
        ok->csr_target_highest_slotid = 0;
1071
0
        return 1;
1072
0
}
1073
1074
static int
1075
nfs4_cb_compound(struct rpc_context *rpc, ZDR *zdr, uint32_t xid)
1076
0
{
1077
0
        struct nfs4_cb_reply r;
1078
0
        nfs_cb_argop4 arg;
1079
0
        uint32_t minorversion, ident, nops, i, pos;
1080
0
        int new_request = 0, ret;
1081
0
        char *buf;
1082
0
        ZDR out;
1083
1084
0
        memset(&r, 0, sizeof(r));
1085
0
        if (!zdr_utf8str_cs(zdr, &r.tag) ||
1086
0
            !zdr_uint32_t(zdr, &minorversion) ||
1087
0
            !zdr_uint32_t(zdr, &ident) ||
1088
0
            !zdr_uint32_t(zdr, &nops)) {
1089
0
                return rpc_send_cb_reply(rpc, xid, GARBAGE_ARGS, NULL, NULL, 0);
1090
0
        }
1091
1092
0
        if (minorversion != rpc->nfs4_minorversion) {
1093
0
                r.status = NFS4ERR_MINOR_VERS_MISMATCH;
1094
0
                nops = 0;
1095
0
        } else if (nops > NFS4_CB_MAX_OPS) {
1096
0
                r.status = NFS4ERR_TOO_MANY_OPS;
1097
0
                nops = 0;
1098
0
        }
1099
1100
0
        for (i = 0; i < nops; i++) {
1101
0
                nfs_cb_resop4 *res = &r.res[i];
1102
1103
0
                memset(&arg, 0, sizeof(arg));
1104
0
                pos = zdr_getpos(zdr);
1105
0
                if (!zdr_uint32_t(zdr, &arg.argop)) {
1106
0
                        return rpc_send_cb_reply(rpc, xid, GARBAGE_ARGS,
1107
0
                                                 NULL, NULL, 0);
1108
0
                }
1109
0
                r.nres++;
1110
0
                res->resop = arg.argop;
1111
0
                if (!nfs4_cb_is_op(arg.argop, minorversion)) {
1112
0
                        res->resop = OP_CB_ILLEGAL;
1113
0
                        r.status = NFS4ERR_OP_ILLEGAL;
1114
0
                } else if (i == 0 && arg.argop != OP_CB_SEQUENCE) {
1115
0
                        r.status = NFS4ERR_OP_NOT_IN_SESSION;
1116
0
                } else if (i > 0 && arg.argop == OP_CB_SEQUENCE) {
1117
0
                        r.status = NFS4ERR_SEQUENCE_POS;
1118
0
                } else if (arg.argop == OP_CB_SEQUENCE) {
1119
0
                        if (!zdr_CB_SEQUENCE4args(zdr, &arg.nfs_cb_argop4_u.opcbsequence)) {
1120
0
                                return rpc_send_cb_reply(rpc, xid, GARBAGE_ARGS,
1121
0
                                                         NULL, NULL, 0);
1122
0
                        }
1123
0
                        ret = nfs4_cb_sequence(rpc, &arg.nfs_cb_argop4_u.opcbsequence,
1124
0
                                               &res->nfs_cb_resop4_u.opcbsequence);
1125
0
                        if (ret == 0) {
1126
0
                                return nfs4_cb_send_raw(rpc, xid,
1127
0
                                                        rpc->nfs4_cb_reply,
1128
0
                                                        rpc->nfs4_cb_reply_len);
1129
0
                        }
1130
0
                        new_request = ret == 1;
1131
0
                        r.status = res->nfs_cb_resop4_u.opcbsequence.csr_status;
1132
0
                } else if (nfs4_cb_app_op(arg.argop)) {
1133
0
                        zdr_setpos(zdr, pos);
1134
0
                        if (!zdr_nfs_cb_argop4(zdr, &arg)) {
1135
0
                                return rpc_send_cb_reply(rpc, xid, GARBAGE_ARGS,
1136
0
                                                         NULL, NULL, 0);
1137
0
                        }
1138
0
                        res->nfs_cb_resop4_u.opcbillegal.status = NFS4ERR_NOTSUPP;
1139
0
                        if (rpc->nfs4_cb_fn) {
1140
0
                                rpc->nfs4_cb_fn(rpc, &arg, res,
1141
0
                                                rpc->nfs4_cb_private_data);
1142
0
                        }
1143
0
                        res->resop = arg.argop;
1144
0
                        r.status = res->nfs_cb_resop4_u.opcbillegal.status;
1145
0
                } else {
1146
                        /* An op we cannot decode, so nothing after it either. */
1147
0
                        r.status = NFS4ERR_NOTSUPP;
1148
0
                }
1149
0
                if (r.status != NFS4_OK) {
1150
                        /* All CB results lead with their status. */
1151
0
                        res->nfs_cb_resop4_u.opcbillegal.status = r.status;
1152
0
                        break;
1153
0
                }
1154
0
        }
1155
1156
0
        buf = malloc(NFS4_CB_MAX_REPLY);
1157
0
        if (buf == NULL) {
1158
0
                return rpc_send_cb_reply(rpc, xid, SYSTEM_ERR, NULL, NULL, 0);
1159
0
        }
1160
0
        zdrmem_create(&out, buf, NFS4_CB_MAX_REPLY, ZDR_ENCODE);
1161
0
        if (!zdr_nfs4_cb_reply(&out, &r)) {
1162
0
                zdr_destroy(&out);
1163
0
                free(buf);
1164
0
                return rpc_send_cb_reply(rpc, xid, SYSTEM_ERR, NULL, NULL, 0);
1165
0
        }
1166
0
        ret = nfs4_cb_send_raw(rpc, xid, buf, zdr_getpos(&out));
1167
0
        if (new_request) {
1168
0
                free(rpc->nfs4_cb_reply);
1169
0
                rpc->nfs4_cb_reply = buf;
1170
0
                rpc->nfs4_cb_reply_len = zdr_getpos(&out);
1171
0
                rpc->nfs4_cb_seqid++;
1172
0
                buf = NULL;
1173
0
        }
1174
0
        zdr_destroy(&out);
1175
0
        free(buf);
1176
0
        return ret;
1177
0
}
1178
1179
/*
1180
 * A CALL from the server. Returns -1 only if the connection is unusable.
1181
 */
1182
int
1183
nfs4_cb_process_call(struct rpc_context *rpc, char *buf, uint32_t size)
1184
0
{
1185
0
        struct rpc_msg call;
1186
0
        ZDR zdr;
1187
0
        int ret;
1188
1189
0
        memset(&call, 0, sizeof(call));
1190
0
        zdrmem_create(&zdr, buf, size, ZDR_DECODE);
1191
0
        if (!zdr_callmsg(rpc, &zdr, &call)) {
1192
0
                RPC_LOG(rpc, 1, "ignoring a malformed back channel call");
1193
0
                zdr_destroy(&zdr);
1194
0
                return 0;
1195
0
        }
1196
1197
0
        if (call.body.cbody.prog != NFS4_CALLBACK) {
1198
0
                ret = rpc_send_cb_reply(rpc, call.xid, PROG_UNAVAIL, NULL,
1199
0
                                        NULL, 0);
1200
0
        } else if (call.body.cbody.vers != NFS_CB) {
1201
0
                ret = rpc_send_cb_reply(rpc, call.xid, PROG_MISMATCH, NULL,
1202
0
                                        NULL, 0);
1203
0
        } else if (call.body.cbody.proc == CB_NULL) {
1204
0
                ret = rpc_send_cb_reply(rpc, call.xid, SUCCESS, NULL,
1205
0
                                        (zdrproc_t)zdr_void, 0);
1206
0
        } else if (call.body.cbody.proc == CB_COMPOUND) {
1207
0
                ret = nfs4_cb_compound(rpc, &zdr, call.xid);
1208
0
        } else {
1209
0
                ret = rpc_send_cb_reply(rpc, call.xid, PROC_UNAVAIL, NULL,
1210
0
                                        NULL, 0);
1211
0
        }
1212
0
        zdr_destroy(&zdr);
1213
0
        return ret;
1214
0
}
1215
1216
#endif /* HAVE_NFS4_2 */
1217
1218
int
1219
rpc_nfs4_enable_backchannel(struct rpc_context *rpc, rpc_nfs4_cb_fn fn,
1220
                            void *private_data)
1221
0
{
1222
0
#ifdef HAVE_NFS4_2
1223
0
        rpc->nfs4_cb_fn = fn;
1224
0
        rpc->nfs4_cb_private_data = private_data;
1225
0
        return 0;
1226
#else
1227
        (void)rpc; (void)fn; (void)private_data;
1228
        return -1;
1229
#endif
1230
0
}
1231
1232
int
1233
rpc_nfs4_backchannel_is_up(struct rpc_context *rpc)
1234
0
{
1235
0
#ifdef HAVE_NFS4_2
1236
0
        return rpc->nfs4_cb_up;
1237
#else
1238
        (void)rpc;
1239
        return 0;
1240
#endif
1241
0
}
1242
1243
1244
struct rpc_pdu *rpc_nfs4_null_task(struct rpc_context *rpc, rpc_cb cb,
1245
                                   void *private_data)
1246
0
{
1247
0
  struct rpc_pdu *pdu;
1248
1249
0
  pdu = rpc_allocate_pdu(rpc, NFS4_PROGRAM, NFS_V4, NFSPROC4_NULL, cb,
1250
0
                               private_data, (zdrproc_t)zdr_void, 0);
1251
0
  if (pdu == NULL) {
1252
0
    rpc_set_error(rpc, "Out of memory. Failed to allocate pdu "
1253
0
                              "for NFS4/NULL call");
1254
0
    return NULL;
1255
0
  }
1256
1257
0
  if (rpc_queue_pdu(rpc, pdu) != 0) {
1258
0
    rpc_set_error(rpc, "Out of memory. Failed to queue pdu for "
1259
0
                              "NFS4/NULL call");
1260
0
    return NULL;
1261
0
  }
1262
1263
0
  return pdu;
1264
0
}
1265
1266
static struct rpc_pdu *rpc_nfs4_compound_task_auth(struct rpc_context *rpc,
1267
                                                   rpc_cb cb,
1268
                                                   struct COMPOUND4args *args,
1269
                                                   void *private_data,
1270
                                                   size_t alloc_hint,
1271
                                                   const struct AUTH *auth)
1272
0
{
1273
0
  struct rpc_pdu *pdu;
1274
1275
0
  pdu = rpc_allocate_pdu2_auth(rpc, NFS4_PROGRAM, NFS_V4, NFSPROC4_COMPOUND,
1276
0
                               cb, private_data, (zdrproc_t)zdr_COMPOUND4res,
1277
0
                               sizeof(COMPOUND4res),
1278
0
                               alloc_hint, 0, auth);
1279
0
  if (pdu == NULL) {
1280
0
    return NULL;
1281
0
  }
1282
1283
0
#ifdef HAVE_NFS4_2
1284
  /*
1285
   * The minor version and the session both belong to the connection, not
1286
   * to each caller, so they are stamped here rather than at every one of
1287
   * the ~40 places that build a COMPOUND. The minor version stays 0
1288
   * unless the context selected NFSv4.2, and a COMPOUND that does not
1289
   * lead with SEQUENCE needs no slot. Without NFSv4.2 there is nothing
1290
   * but 0 to stamp, and callers already memset the args, so the whole
1291
   * thing compiles out.
1292
   */
1293
0
  args->minorversion = rpc->nfs4_minorversion;
1294
  /*
1295
   * Only a COMPOUND that leads with SEQUENCE, i.e. only NFSv4.2 once its
1296
   * session exists, is marked as needing a slot. NFSv4.0 builds no
1297
   * SEQUENCE and so is never gated on the slot table.
1298
   */
1299
0
  if (nfs4_pdu_note_sequence(rpc, pdu, args,
1300
0
           zdr_getpos(&pdu->zdr)) < 0) {
1301
0
    rpc_free_pdu(rpc, pdu);
1302
0
    return NULL;
1303
0
  }
1304
0
#endif /* HAVE_NFS4_2 */
1305
1306
0
  if (zdr_COMPOUND4args(&pdu->zdr,  args) == 0) {
1307
0
    rpc_set_error(rpc, "ZDR error: Failed to encode COMPOUND4args");
1308
0
    rpc_free_pdu(rpc, pdu);
1309
0
    return NULL;
1310
0
  }
1311
1312
0
#ifdef HAVE_NFS4_2
1313
0
        nfs4_pdu_note_delay(pdu, args);
1314
0
#endif
1315
1316
0
  if (rpc_queue_pdu(rpc, pdu) != 0) {
1317
0
    rpc_set_error(rpc, "Out of memory. Failed to queue pdu for "
1318
0
                              "NFS4/COMPOUND4 call");
1319
0
    return NULL;
1320
0
  }
1321
1322
0
  return pdu;
1323
0
}
1324
1325
struct rpc_pdu *rpc_nfs4_compound_task2(struct rpc_context *rpc, rpc_cb cb,
1326
                                        struct COMPOUND4args *args,
1327
                                        void *private_data,
1328
                                        size_t alloc_hint)
1329
0
{
1330
0
  return rpc_nfs4_compound_task_auth(rpc, cb, args, private_data,
1331
0
                                      alloc_hint, NULL);
1332
0
}
1333
1334
struct rpc_pdu *rpc_nfs40_renew_task(struct rpc_context *rpc, rpc_cb cb,
1335
                                    void *private_data)
1336
0
{
1337
0
  COMPOUND4args args;
1338
0
  nfs_argop4 op;
1339
1340
0
  memset(&op, 0, sizeof(op));
1341
0
  op.argop = OP_RENEW;
1342
0
  op.nfs_argop4_u.oprenew.clientid = rpc->nfs40_clientid;
1343
0
  memset(&args, 0, sizeof(args));
1344
0
  args.argarray.argarray_len = 1;
1345
0
  args.argarray.argarray_val = &op;
1346
0
  return rpc_nfs4_compound_task_auth(rpc, cb, &args, private_data, 0,
1347
0
                                     rpc->nfs40_renew_auth);
1348
0
}
1349
1350
1351
struct rpc_pdu *rpc_nfs4_compound_task(struct rpc_context *rpc, rpc_cb cb,
1352
                                       struct COMPOUND4args *args,
1353
                                       void *private_data)
1354
0
{
1355
0
        return rpc_nfs4_compound_task2(rpc, cb, args, private_data, 0);
1356
0
}
1357
1358
struct rpc_pdu *rpc_nfs4_readv_task(struct rpc_context *rpc, rpc_cb cb,
1359
                                   const struct iovec *iov, int iovcnt,
1360
                                   struct COMPOUND4args *args,
1361
                                   void *private_data)
1362
0
{
1363
0
  struct rpc_pdu *pdu;
1364
0
  int i;
1365
1366
0
        if (iovcnt == 0 || iov == NULL) {
1367
0
    rpc_set_error(rpc, "Invalid arguments: iov and iovcnt must be specified");
1368
0
    return NULL;
1369
0
        }
1370
1371
        /*
1372
         * It's disallowed since it's not tested. It may work.
1373
         */
1374
0
        if (iovcnt > 1 && rpc->is_udp) {
1375
0
    rpc_set_error(rpc, "Invalid arguments: Vectored read not supported for UDP transport");
1376
0
    return NULL;
1377
0
        }
1378
1379
        /*
1380
         * Don't accept more iovecs than what readv() can handle.
1381
         */
1382
0
        if (iovcnt > RPC_MAX_VECTORS) {
1383
0
    rpc_set_error(rpc, "Invalid arguments: iovcnt must be <= %d", RPC_MAX_VECTORS);
1384
0
    return NULL;
1385
0
        }
1386
1387
0
  pdu = rpc_allocate_pdu2(rpc, NFS4_PROGRAM, NFS_V4, NFSPROC4_COMPOUND,
1388
0
                               cb, private_data, (zdrproc_t)zdr_COMPOUND4res,
1389
0
                                sizeof(COMPOUND4res), 0, 0);
1390
0
  if (pdu == NULL) {
1391
0
    rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for "
1392
0
                              "NFS4/COMPOUND call");
1393
0
    return NULL;
1394
0
  }
1395
1396
0
#ifdef HAVE_NFS4_2
1397
  /*
1398
   * The minor version and the session both belong to the connection, not
1399
   * to each caller, so they are stamped here rather than at every one of
1400
   * the ~40 places that build a COMPOUND. The minor version stays 0
1401
   * unless the context selected NFSv4.2, and a COMPOUND that does not
1402
   * lead with SEQUENCE needs no slot. Without NFSv4.2 there is nothing
1403
   * but 0 to stamp, and callers already memset the args, so the whole
1404
   * thing compiles out.
1405
   */
1406
0
  args->minorversion = rpc->nfs4_minorversion;
1407
  /*
1408
   * Only a COMPOUND that leads with SEQUENCE, i.e. only NFSv4.2 once its
1409
   * session exists, is marked as needing a slot. NFSv4.0 builds no
1410
   * SEQUENCE and so is never gated on the slot table.
1411
   */
1412
0
  if (nfs4_pdu_note_sequence(rpc, pdu, args,
1413
0
           zdr_getpos(&pdu->zdr)) < 0) {
1414
0
    rpc_free_pdu(rpc, pdu);
1415
0
    return NULL;
1416
0
  }
1417
0
#endif /* HAVE_NFS4_2 */
1418
1419
0
  if (zdr_COMPOUND4args(&pdu->zdr,  args) == 0) {
1420
0
    rpc_set_error(rpc, "ZDR error: Failed to encode COMPOUND4args");
1421
0
    rpc_free_pdu(rpc, pdu);
1422
0
    return NULL;
1423
0
  }
1424
1425
0
  pdu->in.base = (struct iovec *) malloc(sizeof(struct iovec) * iovcnt * 2);
1426
0
  if (!pdu->in.base) {
1427
0
    rpc_set_error(rpc, "error: Failed to allocate memory");
1428
0
    rpc_free_pdu(rpc, pdu);
1429
0
    return NULL;
1430
0
  }
1431
1432
0
        pdu->in.iov = pdu->in.base;
1433
0
        pdu->in.iov_ref = pdu->in.base + iovcnt;
1434
0
  pdu->in.iovcnt = pdu->in.iovcnt_ref = iovcnt;
1435
1436
0
        for (i = 0; i < iovcnt; i++) {
1437
0
                pdu->in.iov[i] = pdu->in.iov_ref[i] = iov[i];
1438
0
                pdu->in.remaining_size += iov[i].iov_len;
1439
0
        }
1440
1441
0
        pdu->requested_read_count = pdu->in.remaining_size;
1442
0
        pdu->zero_copy_iov = 1;
1443
1444
0
  if (rpc_queue_pdu(rpc, pdu) != 0) {
1445
0
    rpc_set_error(rpc, "Out of memory. Failed to queue pdu for "
1446
0
                              "NFS4/COMPOUND4 call");
1447
0
    return NULL;
1448
0
  }
1449
1450
0
  return pdu;
1451
0
}
1452
1453
struct rpc_pdu *rpc_nfs4_read_task(struct rpc_context *rpc, rpc_cb cb,
1454
                                   void *buf, size_t count,
1455
                                   struct COMPOUND4args *args,
1456
                                   void *private_data)
1457
0
{
1458
0
  struct iovec iov;
1459
1460
0
  iov.iov_base = buf;
1461
0
  iov.iov_len = count;
1462
1463
0
  return rpc_nfs4_readv_task(rpc, cb, &iov, 1, args, private_data);
1464
0
}
1465
1466
struct rpc_pdu *rpc_nfs4_writev_task(struct rpc_context *rpc, rpc_cb cb,
1467
                                     const struct iovec *iov, int iovcnt,
1468
                                     struct COMPOUND4args *args,
1469
                                     void *private_data)
1470
0
{
1471
0
  struct rpc_pdu *pdu;
1472
0
        int start;
1473
0
        uint32_t len;
1474
0
        static uint32_t zero_padding;
1475
0
        int i, copy_payload;
1476
1477
0
        if (iovcnt == 0 || iov == NULL) {
1478
0
    rpc_set_error(rpc, "Invalid arguments: iov and iovcnt must be specified");
1479
0
    return NULL;
1480
0
        }
1481
1482
0
        if (iovcnt > 1 && rpc->is_udp) {
1483
0
    rpc_set_error(rpc, "Invalid arguments: Vectored write not supported for UDP transport");
1484
0
    return NULL;
1485
0
        }
1486
1487
0
        len = 0;
1488
0
        for (i = 0; i < iovcnt; i++) {
1489
0
                len += iov[i].iov_len;
1490
0
        }
1491
0
        copy_payload = rpc_sec_covers_payload(rpc);
1492
1493
0
  pdu = rpc_allocate_pdu2(rpc, NFS4_PROGRAM, NFS_V4, NFSPROC4_COMPOUND,
1494
0
                                cb, private_data, (zdrproc_t)zdr_COMPOUND4res,
1495
0
                                sizeof(COMPOUND4res),
1496
0
                                copy_payload ? len + 8 : 0, iovcnt);
1497
0
  if (pdu == NULL) {
1498
0
    rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for "
1499
0
                              "NFS4/COMPOUND call");
1500
0
    return NULL;
1501
0
  }
1502
1503
0
        start = zdr_getpos(&pdu->zdr);
1504
1505
0
#ifdef HAVE_NFS4_2
1506
  /*
1507
   * The minor version and the session both belong to the connection, not
1508
   * to each caller, so they are stamped here rather than at every one of
1509
   * the ~40 places that build a COMPOUND. The minor version stays 0
1510
   * unless the context selected NFSv4.2, and a COMPOUND that does not
1511
   * lead with SEQUENCE needs no slot. Without NFSv4.2 there is nothing
1512
   * but 0 to stamp, and callers already memset the args, so the whole
1513
   * thing compiles out.
1514
   */
1515
0
  args->minorversion = rpc->nfs4_minorversion;
1516
  /*
1517
   * Only a COMPOUND that leads with SEQUENCE, i.e. only NFSv4.2 once its
1518
   * session exists, is marked as needing a slot. NFSv4.0 builds no
1519
   * SEQUENCE and so is never gated on the slot table.
1520
   */
1521
0
  if (nfs4_pdu_note_sequence(rpc, pdu, args,
1522
0
           zdr_getpos(&pdu->zdr)) < 0) {
1523
0
    rpc_free_pdu(rpc, pdu);
1524
0
    return NULL;
1525
0
  }
1526
0
#endif /* HAVE_NFS4_2 */
1527
1528
0
  if (zdr_COMPOUND4args(&pdu->zdr,  args) == 0) {
1529
0
    rpc_set_error(rpc, "ZDR error: Failed to encode COMPOUND4args");
1530
0
    rpc_free_pdu(rpc, pdu);
1531
0
    return NULL;
1532
0
  }
1533
1534
0
        if (copy_payload) {
1535
0
                if (rpc_zdr_put_payload(&pdu->zdr, iov, iovcnt, len) < 0) {
1536
0
                        rpc_set_error(rpc, "ZDR error: Failed to encode WRITE4 data");
1537
0
                        rpc_free_pdu(rpc, pdu);
1538
0
                        return NULL;
1539
0
                }
1540
0
                goto queue;
1541
0
        }
1542
1543
        /* Add an iovector for the COMPOUND4/.../WRITE4 header */
1544
0
        if (rpc_add_iovector(rpc, &pdu->out, &pdu->outdata.data[start + 4],
1545
0
                             zdr_getpos(&pdu->zdr) - start, NULL) < 0) {
1546
0
    rpc_free_pdu(rpc, pdu);
1547
0
    return NULL;
1548
0
        }
1549
1550
        /* Add an iovector for the length of the byte/array blob */
1551
0
        start = zdr_getpos(&pdu->zdr);
1552
0
        zdr_u_int(&pdu->zdr, &len);
1553
0
        if (rpc_add_iovector(rpc, &pdu->out, &pdu->outdata.data[start + 4],
1554
0
                             4, NULL) < 0) {
1555
0
    rpc_free_pdu(rpc, pdu);
1556
0
    return NULL;
1557
0
        }
1558
1559
        /* Add iovector(s) for the data itself */
1560
0
        for (i = 0; i < iovcnt; i++) {
1561
0
                if (rpc_add_iovector(rpc, &pdu->out,
1562
0
                                     iov[i].iov_base,
1563
0
                                     iov[i].iov_len, NULL) < 0) {
1564
0
                        rpc_free_pdu(rpc, pdu);
1565
0
                        return NULL;
1566
0
                }
1567
0
        }
1568
1569
        /* We may need to pad this to 4 byte boundary */
1570
0
        if (len & 0x03) {
1571
0
                if (rpc_add_iovector(rpc, &pdu->out, (char *)&zero_padding,
1572
0
                                     4 - (len & 0x03),
1573
0
                                     NULL) < 0) {
1574
0
                        rpc_free_pdu(rpc, pdu);
1575
0
                        return NULL;
1576
0
                }
1577
0
        }
1578
1579
0
 queue:
1580
0
  if (rpc_queue_pdu(rpc, pdu) != 0) {
1581
0
    rpc_set_error(rpc, "Out of memory. Failed to queue pdu for "
1582
0
                              "NFS4/COMPOUND4 call");
1583
0
    return NULL;
1584
0
  }
1585
1586
0
  return pdu;
1587
0
}
1588
1589
struct rpc_pdu *rpc_nfs4_write_task(struct rpc_context *rpc, rpc_cb cb,
1590
                                    const void *buf, size_t count,
1591
                                    struct COMPOUND4args *args,
1592
                                    void *private_data)
1593
0
{
1594
        /*
1595
         * We don't have an iovec definition with const iov_base pointer, but
1596
         * rpc_nfs4_writev_task() won't modify the contents of *buf, so the
1597
         * following cast is safe.
1598
         */
1599
0
        struct iovec iov;
1600
1601
0
        iov.iov_base = (void *) buf;
1602
0
        iov.iov_len = count;
1603
1604
0
        return rpc_nfs4_writev_task(rpc, cb, &iov, 1, args, private_data);
1605
0
}