xref: /linux/fs/nfsd/xdr4.h (revision 5d15ab717d503ff10b585a144870648b9a88c616)
1 /*
2  *  Server-side types for NFSv4.
3  *
4  *  Copyright (c) 2002 The Regents of the University of Michigan.
5  *  All rights reserved.
6  *
7  *  Kendrick Smith <kmsmith@umich.edu>
8  *  Andy Adamson   <andros@umich.edu>
9  *
10  *  Redistribution and use in source and binary forms, with or without
11  *  modification, are permitted provided that the following conditions
12  *  are met:
13  *
14  *  1. Redistributions of source code must retain the above copyright
15  *     notice, this list of conditions and the following disclaimer.
16  *  2. Redistributions in binary form must reproduce the above copyright
17  *     notice, this list of conditions and the following disclaimer in the
18  *     documentation and/or other materials provided with the distribution.
19  *  3. Neither the name of the University nor the names of its
20  *     contributors may be used to endorse or promote products derived
21  *     from this software without specific prior written permission.
22  *
23  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  */
36 
37 #ifndef _LINUX_NFSD_XDR4_H
38 #define _LINUX_NFSD_XDR4_H
39 
40 #include "state.h"
41 #include "nfsd.h"
42 
43 #define NFSD4_MAX_TAGLEN	128
44 #define XDR_LEN(n)                     (((n) + 3) & ~3)
45 
46 #define CURRENT_STATE_ID_FLAG (1<<0)
47 #define SAVED_STATE_ID_FLAG (1<<1)
48 
49 #define SET_CSTATE_FLAG(c, f) ((c)->sid_flags |= (f))
50 #define HAS_CSTATE_FLAG(c, f) ((c)->sid_flags & (f))
51 #define CLEAR_CSTATE_FLAG(c, f) ((c)->sid_flags &= ~(f))
52 
53 /**
54  * nfsd4_encode_bool - Encode an XDR bool type result
55  * @xdr: target XDR stream
56  * @val: boolean value to encode
57  *
58  * Return values:
59  *    %nfs_ok: @val encoded; @xdr advanced to next position
60  *    %nfserr_resource: stream buffer space exhausted
61  */
62 static __always_inline __be32
63 nfsd4_encode_bool(struct xdr_stream *xdr, bool val)
64 {
65 	__be32 *p = xdr_reserve_space(xdr, XDR_UNIT);
66 
67 	if (unlikely(p == NULL))
68 		return nfserr_resource;
69 	*p = val ? xdr_one : xdr_zero;
70 	return nfs_ok;
71 }
72 
73 /**
74  * nfsd4_encode_uint32_t - Encode an XDR uint32_t type result
75  * @xdr: target XDR stream
76  * @val: integer value to encode
77  *
78  * Return values:
79  *    %nfs_ok: @val encoded; @xdr advanced to next position
80  *    %nfserr_resource: stream buffer space exhausted
81  */
82 static __always_inline __be32
83 nfsd4_encode_uint32_t(struct xdr_stream *xdr, u32 val)
84 {
85 	__be32 *p = xdr_reserve_space(xdr, XDR_UNIT);
86 
87 	if (unlikely(p == NULL))
88 		return nfserr_resource;
89 	*p = cpu_to_be32(val);
90 	return nfs_ok;
91 }
92 
93 #define nfsd4_encode_aceflag4(x, v)	nfsd4_encode_uint32_t(x, v)
94 #define nfsd4_encode_acemask4(x, v)	nfsd4_encode_uint32_t(x, v)
95 #define nfsd4_encode_acetype4(x, v)	nfsd4_encode_uint32_t(x, v)
96 #define nfsd4_encode_count4(x, v)	nfsd4_encode_uint32_t(x, v)
97 #define nfsd4_encode_mode4(x, v)	nfsd4_encode_uint32_t(x, v)
98 #define nfsd4_encode_nfs_lease4(x, v)	nfsd4_encode_uint32_t(x, v)
99 #define nfsd4_encode_qop4(x, v)		nfsd4_encode_uint32_t(x, v)
100 #define nfsd4_encode_sequenceid4(x, v)	nfsd4_encode_uint32_t(x, v)
101 #define nfsd4_encode_slotid4(x, v)	nfsd4_encode_uint32_t(x, v)
102 
103 /**
104  * nfsd4_encode_uint64_t - Encode an XDR uint64_t type result
105  * @xdr: target XDR stream
106  * @val: integer value to encode
107  *
108  * Return values:
109  *    %nfs_ok: @val encoded; @xdr advanced to next position
110  *    %nfserr_resource: stream buffer space exhausted
111  */
112 static __always_inline __be32
113 nfsd4_encode_uint64_t(struct xdr_stream *xdr, u64 val)
114 {
115 	__be32 *p = xdr_reserve_space(xdr, XDR_UNIT * 2);
116 
117 	if (unlikely(p == NULL))
118 		return nfserr_resource;
119 	put_unaligned_be64(val, p);
120 	return nfs_ok;
121 }
122 
123 #define nfsd4_encode_changeid4(x, v)	nfsd4_encode_uint64_t(x, v)
124 #define nfsd4_encode_nfs_cookie4(x, v)	nfsd4_encode_uint64_t(x, v)
125 #define nfsd4_encode_length4(x, v)	nfsd4_encode_uint64_t(x, v)
126 #define nfsd4_encode_offset4(x, v)	nfsd4_encode_uint64_t(x, v)
127 
128 /**
129  * nfsd4_encode_opaque_fixed - Encode a fixed-length XDR opaque type result
130  * @xdr: target XDR stream
131  * @data: pointer to data
132  * @size: length of data in bytes
133  *
134  * Return values:
135  *    %nfs_ok: @data encoded; @xdr advanced to next position
136  *    %nfserr_resource: stream buffer space exhausted
137  */
138 static __always_inline __be32
139 nfsd4_encode_opaque_fixed(struct xdr_stream *xdr, const void *data,
140 			  size_t size)
141 {
142 	__be32 *p = xdr_reserve_space(xdr, xdr_align_size(size));
143 	size_t pad = xdr_pad_size(size);
144 
145 	if (unlikely(p == NULL))
146 		return nfserr_resource;
147 	memcpy(p, data, size);
148 	if (pad)
149 		memset((char *)p + size, 0, pad);
150 	return nfs_ok;
151 }
152 
153 /**
154  * nfsd4_encode_opaque - Encode a variable-length XDR opaque type result
155  * @xdr: target XDR stream
156  * @data: pointer to data
157  * @size: length of data in bytes
158  *
159  * Return values:
160  *    %nfs_ok: @data encoded; @xdr advanced to next position
161  *    %nfserr_resource: stream buffer space exhausted
162  */
163 static __always_inline __be32
164 nfsd4_encode_opaque(struct xdr_stream *xdr, const void *data, size_t size)
165 {
166 	size_t pad = xdr_pad_size(size);
167 	__be32 *p;
168 
169 	p = xdr_reserve_space(xdr, XDR_UNIT + xdr_align_size(size));
170 	if (unlikely(p == NULL))
171 		return nfserr_resource;
172 	*p++ = cpu_to_be32(size);
173 	memcpy(p, data, size);
174 	if (pad)
175 		memset((char *)p + size, 0, pad);
176 	return nfs_ok;
177 }
178 
179 #define nfsd4_encode_component4(x, d, s)	nfsd4_encode_opaque(x, d, s)
180 
181 struct nfsd4_compound_state {
182 	struct svc_fh		current_fh;
183 	struct svc_fh		save_fh;
184 	struct nfs4_stateowner	*replay_owner;
185 	struct nfs4_client	*clp;
186 	/* For sessions DRC */
187 	struct nfsd4_session	*session;
188 	struct nfsd4_slot	*slot;
189 	int			data_offset;
190 	bool                    spo_must_allowed;
191 	size_t			iovlen;
192 	u32			minorversion;
193 	__be32			status;
194 	stateid_t	current_stateid;
195 	stateid_t	save_stateid;
196 	/* to indicate current and saved state id presents */
197 	u32		sid_flags;
198 };
199 
200 static inline bool nfsd4_has_session(struct nfsd4_compound_state *cs)
201 {
202 	return cs->slot != NULL;
203 }
204 
205 struct nfsd4_change_info {
206 	u32		atomic;
207 	u64		before_change;
208 	u64		after_change;
209 };
210 
211 struct nfsd4_access {
212 	u32		ac_req_access;      /* request */
213 	u32		ac_supported;       /* response */
214 	u32		ac_resp_access;     /* response */
215 };
216 
217 struct nfsd4_close {
218 	u32		cl_seqid;           /* request */
219 	stateid_t	cl_stateid;         /* request+response */
220 };
221 
222 struct nfsd4_commit {
223 	u64		co_offset;          /* request */
224 	u32		co_count;           /* request */
225 	nfs4_verifier	co_verf;            /* response */
226 };
227 
228 struct nfsd4_create {
229 	u32		cr_namelen;         /* request */
230 	char *		cr_name;            /* request */
231 	u32		cr_type;            /* request */
232 	union {                             /* request */
233 		struct {
234 			u32 datalen;
235 			char *data;
236 			struct kvec first;
237 		} link;   /* NF4LNK */
238 		struct {
239 			u32 specdata1;
240 			u32 specdata2;
241 		} dev;    /* NF4BLK, NF4CHR */
242 	} u;
243 	u32		cr_bmval[3];        /* request */
244 	struct iattr	cr_iattr;           /* request */
245 	int		cr_umask;           /* request */
246 	struct nfsd4_change_info  cr_cinfo; /* response */
247 	struct nfs4_acl *cr_acl;
248 	struct posix_acl *cr_dpacl;
249 	struct posix_acl *cr_pacl;
250 	struct xdr_netobj cr_label;
251 };
252 #define cr_datalen	u.link.datalen
253 #define cr_data		u.link.data
254 #define cr_first	u.link.first
255 #define cr_specdata1	u.dev.specdata1
256 #define cr_specdata2	u.dev.specdata2
257 
258 struct nfsd4_delegreturn {
259 	stateid_t	dr_stateid;
260 };
261 
262 struct nfsd4_getattr {
263 	u32		ga_bmval[3];        /* request */
264 	struct svc_fh	*ga_fhp;            /* response */
265 };
266 
267 struct nfsd4_link {
268 	u32		li_namelen;         /* request */
269 	char *		li_name;            /* request */
270 	struct nfsd4_change_info  li_cinfo; /* response */
271 };
272 
273 struct nfsd4_lock_denied {
274 	clientid_t	ld_clientid;
275 	struct xdr_netobj	ld_owner;
276 	u64             ld_start;
277 	u64             ld_length;
278 	u32             ld_type;
279 };
280 
281 struct nfsd4_lock {
282 	/* request */
283 	u32             lk_type;
284 	u32             lk_reclaim;         /* boolean */
285 	u64             lk_offset;
286 	u64             lk_length;
287 	u32             lk_is_new;
288 	union {
289 		struct {
290 			u32             open_seqid;
291 			stateid_t       open_stateid;
292 			u32             lock_seqid;
293 			clientid_t      clientid;
294 			struct xdr_netobj owner;
295 		} new;
296 		struct {
297 			stateid_t       lock_stateid;
298 			u32             lock_seqid;
299 		} old;
300 	} v;
301 
302 	/* response */
303 	stateid_t			lk_resp_stateid;
304 	struct nfsd4_lock_denied        lk_denied;
305 };
306 #define lk_new_open_seqid       v.new.open_seqid
307 #define lk_new_open_stateid     v.new.open_stateid
308 #define lk_new_lock_seqid       v.new.lock_seqid
309 #define lk_new_clientid         v.new.clientid
310 #define lk_new_owner            v.new.owner
311 #define lk_old_lock_stateid     v.old.lock_stateid
312 #define lk_old_lock_seqid       v.old.lock_seqid
313 
314 struct nfsd4_lockt {
315 	u32				lt_type;
316 	clientid_t			lt_clientid;
317 	struct xdr_netobj		lt_owner;
318 	u64				lt_offset;
319 	u64				lt_length;
320 	struct nfsd4_lock_denied	lt_denied;
321 };
322 
323 struct nfsd4_locku {
324 	u32             lu_type;
325 	u32             lu_seqid;
326 	stateid_t       lu_stateid;
327 	u64             lu_offset;
328 	u64             lu_length;
329 };
330 
331 
332 struct nfsd4_lookup {
333 	u32		lo_len;             /* request */
334 	char *		lo_name;            /* request */
335 };
336 
337 struct nfsd4_putfh {
338 	u32		pf_fhlen;           /* request */
339 	char		*pf_fhval;          /* request */
340 	bool		no_verify;	    /* represents foreigh fh */
341 };
342 
343 struct nfsd4_getxattr {
344 	char		*getxa_name;		/* request */
345 	u32		getxa_len;		/* request */
346 	void		*getxa_buf;
347 };
348 
349 struct nfsd4_setxattr {
350 	u32		setxa_flags;		/* request */
351 	char		*setxa_name;		/* request */
352 	char		*setxa_buf;		/* request */
353 	u32		setxa_len;		/* request */
354 	struct nfsd4_change_info  setxa_cinfo;	/* response */
355 };
356 
357 struct nfsd4_removexattr {
358 	char		*rmxa_name;		/* request */
359 	struct nfsd4_change_info  rmxa_cinfo;	/* response */
360 };
361 
362 struct nfsd4_listxattrs {
363 	u64		lsxa_cookie;		/* request */
364 	u32		lsxa_maxcount;		/* request */
365 	char		*lsxa_buf;		/* unfiltered buffer (reply) */
366 	u32		lsxa_len;		/* unfiltered len (reply) */
367 };
368 
369 struct nfsd4_open {
370 	u32		op_claim_type;      /* request */
371 	u32		op_fnamelen;
372 	char *		op_fname;	    /* request - everything but CLAIM_PREV */
373 	u32		op_delegate_type;   /* request - CLAIM_PREV only */
374 	stateid_t       op_delegate_stateid; /* request - response */
375 	u32		op_why_no_deleg;    /* response - DELEG_NONE_EXT only */
376 	u32		op_create;     	    /* request */
377 	u32		op_createmode;      /* request */
378 	int		op_umask;           /* request */
379 	u32		op_bmval[3];        /* request */
380 	struct iattr	op_iattr;           /* UNCHECKED4, GUARDED4, EXCLUSIVE4_1 */
381 	nfs4_verifier	op_verf __attribute__((aligned(32)));
382 					    /* EXCLUSIVE4 */
383 	clientid_t	op_clientid;        /* request */
384 	struct xdr_netobj op_owner;           /* request */
385 	u32		op_seqid;           /* request */
386 	u32		op_share_access;    /* request */
387 	u32		op_share_deny;      /* request */
388 	u32		op_deleg_want;      /* request */
389 	stateid_t	op_stateid;         /* response */
390 	__be32		op_xdr_error;       /* see nfsd4_open_omfg() */
391 	struct nfsd4_change_info  op_cinfo; /* response */
392 	u32		op_rflags;          /* response */
393 	bool		op_recall;          /* response */
394 	bool		op_truncate;        /* used during processing */
395 	bool		op_created;         /* used during processing */
396 	struct nfs4_openowner *op_openowner; /* used during processing */
397 	struct file	*op_filp;           /* used during processing */
398 	struct nfs4_file *op_file;          /* used during processing */
399 	struct nfs4_ol_stateid *op_stp;	    /* used during processing */
400 	struct nfs4_clnt_odstate *op_odstate; /* used during processing */
401 	struct nfs4_acl *op_acl;
402 	struct posix_acl *op_dpacl;
403 	struct posix_acl *op_pacl;
404 	struct xdr_netobj op_label;
405 	struct svc_rqst *op_rqstp;
406 };
407 
408 struct nfsd4_open_confirm {
409 	stateid_t	oc_req_stateid		/* request */;
410 	u32		oc_seqid    		/* request */;
411 	stateid_t	oc_resp_stateid		/* response */;
412 };
413 
414 struct nfsd4_open_downgrade {
415 	stateid_t       od_stateid;
416 	u32             od_seqid;
417 	u32             od_share_access;	/* request */
418 	u32		od_deleg_want;		/* request */
419 	u32             od_share_deny;		/* request */
420 };
421 
422 
423 struct nfsd4_read {
424 	stateid_t		rd_stateid;         /* request */
425 	u64			rd_offset;          /* request */
426 	u32			rd_length;          /* request */
427 	int			rd_vlen;
428 	struct nfsd_file	*rd_nf;
429 
430 	struct svc_rqst		*rd_rqstp;          /* response */
431 	struct svc_fh		*rd_fhp;            /* response */
432 	u32			rd_eof;             /* response */
433 };
434 
435 /*
436  * Cache the case-folding properties of @dir so a batched encoder
437  * (e.g., READDIR) does not re-probe per child. @dir is the
438  * directory being read, held by the request, so it is stable
439  * against rename for the duration of the cache's lifetime.
440  */
441 struct nfsd_case_attrs_cache {
442 	struct dentry	*dir;
443 	bool		valid;
444 	bool		insensitive;
445 	bool		preserving;
446 };
447 
448 struct nfsd4_readdir {
449 	u64		rd_cookie;          /* request */
450 	nfs4_verifier	rd_verf;            /* request */
451 	u32		rd_dircount;        /* request */
452 	u32		rd_maxcount;        /* request */
453 	u32		rd_bmval[3];        /* request */
454 	struct svc_rqst *rd_rqstp;          /* response */
455 	struct svc_fh * rd_fhp;             /* response */
456 
457 	struct readdir_cd	common;
458 	struct xdr_stream	*xdr;
459 	int			cookie_offset;
460 	struct nfsd_case_attrs_cache rd_case_cache;
461 };
462 
463 struct nfsd4_release_lockowner {
464 	clientid_t        rl_clientid;
465 	struct xdr_netobj rl_owner;
466 };
467 struct nfsd4_readlink {
468 	struct svc_rqst *rl_rqstp;          /* request */
469 	struct svc_fh *	rl_fhp;             /* request */
470 };
471 
472 struct nfsd4_remove {
473 	u32		rm_namelen;         /* request */
474 	char *		rm_name;            /* request */
475 	struct nfsd4_change_info  rm_cinfo; /* response */
476 };
477 
478 struct nfsd4_rename {
479 	u32		rn_snamelen;        /* request */
480 	char *		rn_sname;           /* request */
481 	u32		rn_tnamelen;        /* request */
482 	char *		rn_tname;           /* request */
483 	struct nfsd4_change_info  rn_sinfo; /* response */
484 	struct nfsd4_change_info  rn_tinfo; /* response */
485 };
486 
487 struct nfsd4_secinfo {
488 	u32 si_namelen;					/* request */
489 	char *si_name;					/* request */
490 	struct svc_export *si_exp;			/* response */
491 };
492 
493 struct nfsd4_secinfo_no_name {
494 	u32 sin_style;					/* request */
495 	struct svc_export *sin_exp;			/* response */
496 };
497 
498 struct nfsd4_setattr {
499 	stateid_t	sa_stateid;         /* request */
500 	u32		sa_bmval[3];        /* request */
501 	struct iattr	sa_iattr;           /* request */
502 	struct nfs4_acl *sa_acl;
503 	struct xdr_netobj sa_label;
504 	struct posix_acl *sa_dpacl;
505 	struct posix_acl *sa_pacl;
506 };
507 
508 struct nfsd4_setclientid {
509 	nfs4_verifier	se_verf;            /* request */
510 	struct xdr_netobj se_name;
511 	u32		se_callback_prog;   /* request */
512 	u32		se_callback_netid_len;  /* request */
513 	char *		se_callback_netid_val;  /* request */
514 	u32		se_callback_addr_len;   /* request */
515 	char *		se_callback_addr_val;   /* request */
516 	u32		se_callback_ident;  /* request */
517 	clientid_t	se_clientid;        /* response */
518 	nfs4_verifier	se_confirm;         /* response */
519 };
520 
521 struct nfsd4_setclientid_confirm {
522 	clientid_t	sc_clientid;
523 	nfs4_verifier	sc_confirm;
524 };
525 
526 struct nfsd4_test_stateid_id {
527 	__be32			ts_id_status;
528 	stateid_t		ts_id_stateid;
529 	struct list_head	ts_id_list;
530 };
531 
532 struct nfsd4_test_stateid {
533 	u32		ts_num_ids;
534 	struct list_head ts_stateid_list;
535 };
536 
537 struct nfsd4_free_stateid {
538 	stateid_t	fr_stateid;         /* request */
539 };
540 
541 struct nfsd4_get_dir_delegation {
542 	/* request */
543 	u32			gdda_signal_deleg_avail;
544 	u32			gdda_notification_types[1];
545 	struct timespec64	gdda_child_attr_delay;
546 	struct timespec64	gdda_dir_attr_delay;
547 	u32			gdda_child_attributes[3];
548 	u32			gdda_dir_attributes[3];
549 	/* response */
550 	u32			gddrnf_status;
551 	nfs4_verifier		gddr_cookieverf;
552 	stateid_t		gddr_stateid;
553 	u32			gddr_notification[1];
554 	u32			gddr_child_attributes[3];
555 	u32			gddr_dir_attributes[3];
556 	bool			gddrnf_will_signal_deleg_avail;
557 };
558 
559 /* also used for NVERIFY */
560 struct nfsd4_verify {
561 	u32		ve_bmval[3];        /* request */
562 	u32		ve_attrlen;         /* request */
563 	char *		ve_attrval;         /* request */
564 };
565 
566 struct nfsd4_write {
567 	stateid_t	wr_stateid;         /* request */
568 	u64		wr_offset;          /* request */
569 	u32		wr_stable_how;      /* request */
570 	u32		wr_buflen;          /* request */
571 	struct xdr_buf	wr_payload;         /* request */
572 
573 	u32		wr_bytes_written;   /* response */
574 	u32		wr_how_written;     /* response */
575 	nfs4_verifier	wr_verifier;        /* response */
576 };
577 
578 struct nfsd4_exchange_id {
579 	nfs4_verifier	verifier;
580 	struct xdr_netobj clname;
581 	u32		flags;
582 	clientid_t	clientid;
583 	u32		seqid;
584 	u32		spa_how;
585 	u32             spo_must_enforce[3];
586 	u32             spo_must_allow[3];
587 	struct xdr_netobj nii_domain;
588 	struct xdr_netobj nii_name;
589 	struct timespec64 nii_time;
590 	char		*server_impl_name;
591 };
592 
593 struct nfsd4_sequence {
594 	struct nfs4_sessionid	sessionid;		/* request/response */
595 	u32			seqid;			/* request/response */
596 	u32			slotid;			/* request/response */
597 	u32			maxslots;		/* request */
598 	u32			cachethis;		/* request */
599 	u32			maxslots_response;	/* response */
600 	u32			target_maxslots;	/* response */
601 	u32			status_flags;		/* response */
602 };
603 
604 struct nfsd4_destroy_session {
605 	struct nfs4_sessionid	sessionid;
606 };
607 
608 struct nfsd4_destroy_clientid {
609 	clientid_t clientid;
610 };
611 
612 struct nfsd4_reclaim_complete {
613 	u32 rca_one_fs;
614 };
615 
616 struct nfsd4_deviceid {
617 	u64			fsid_idx;
618 	u32			generation;
619 };
620 
621 static inline __be32 *
622 svcxdr_encode_deviceid4(__be32 *p, const struct nfsd4_deviceid *devid)
623 {
624 	__be64 *q = (__be64 *)p;
625 
626 	*q = (__force __be64)devid->fsid_idx;
627 	p += 2;
628 	*p++ = (__force __be32)devid->generation;
629 	*p++ = xdr_zero;
630 	return p;
631 }
632 
633 static inline __be32 *
634 svcxdr_decode_deviceid4(__be32 *p, struct nfsd4_deviceid *devid)
635 {
636 	__be64 *q = (__be64 *)p;
637 
638 	devid->fsid_idx = (__force u64)(*q);
639 	p += 2;
640 	devid->generation = (__force u32)(*p++);
641 	p++; /* NFSD does not use the remaining octets */
642 	return p;
643 }
644 
645 static inline __be32
646 nfsd4_decode_deviceid4(struct xdr_stream *xdr, struct nfsd4_deviceid *devid)
647 {
648 	__be32 *p = xdr_inline_decode(xdr, NFS4_DEVICEID4_SIZE);
649 
650 	if (unlikely(!p))
651 		return nfserr_bad_xdr;
652 	svcxdr_decode_deviceid4(p, devid);
653 	return nfs_ok;
654 }
655 
656 struct nfsd4_layout_seg {
657 	u32			iomode;
658 	u64			offset;
659 	u64			length;
660 };
661 
662 struct nfsd4_getdeviceinfo {
663 	struct nfsd4_deviceid	gd_devid;	/* request */
664 	u32			gd_layout_type;	/* request */
665 	u32			gd_maxcount;	/* request */
666 	u32			gd_notify_types;/* request - response */
667 	void			*gd_device;	/* response */
668 };
669 
670 struct nfsd4_layoutget {
671 	u64			lg_minlength;	/* request */
672 	u32			lg_signal;	/* request */
673 	u32			lg_layout_type;	/* request */
674 	u32			lg_maxcount;	/* request */
675 	stateid_t		lg_sid;		/* request/response */
676 	struct nfsd4_layout_seg	lg_seg;		/* request/response */
677 	void			*lg_content;	/* response */
678 };
679 
680 struct nfsd4_layoutcommit {
681 	stateid_t		lc_sid;		/* request */
682 	struct nfsd4_layout_seg	lc_seg;		/* request */
683 	u32			lc_reclaim;	/* request */
684 	u32			lc_newoffset;	/* request */
685 	u64			lc_last_wr;	/* request */
686 	struct timespec64	lc_mtime;	/* request */
687 	u32			lc_layout_type;	/* request */
688 	struct xdr_buf		lc_up_layout;	/* decoded by callback */
689 	bool			lc_size_chg;	/* response */
690 	u64			lc_newsize;	/* response */
691 };
692 
693 struct nfsd4_layoutreturn {
694 	u32			lr_return_type;	/* request */
695 	u32			lr_layout_type;	/* request */
696 	struct nfsd4_layout_seg	lr_seg;		/* request */
697 	u32			lr_reclaim;	/* request */
698 	u32			lrf_body_len;	/* request */
699 	void			*lrf_body;	/* request */
700 	stateid_t		lr_sid;		/* request/response */
701 	bool			lrs_present;	/* response */
702 };
703 
704 struct nfsd4_fallocate {
705 	/* request */
706 	stateid_t	falloc_stateid;
707 	loff_t		falloc_offset;
708 	u64		falloc_length;
709 };
710 
711 struct nfsd4_clone {
712 	/* request */
713 	stateid_t	cl_src_stateid;
714 	stateid_t	cl_dst_stateid;
715 	u64		cl_src_pos;
716 	u64		cl_dst_pos;
717 	u64		cl_count;
718 };
719 
720 struct nfsd42_write_res {
721 	u64			wr_bytes_written;
722 	u32			wr_stable_how;
723 	nfs4_verifier		wr_verifier;
724 	stateid_t		cb_stateid;
725 };
726 
727 struct nfsd4_cb_offload {
728 	struct nfsd4_callback	co_cb;
729 	struct nfsd42_write_res	co_res;
730 	__be32			co_nfserr;
731 	unsigned int		co_retries;
732 	struct knfsd_fh		co_fh;
733 
734 	struct nfs4_sessionid	co_referring_sessionid;
735 	u32			co_referring_slotid;
736 	u32			co_referring_seqno;
737 };
738 
739 struct nfsd4_copy {
740 	/* request */
741 	stateid_t		cp_src_stateid;
742 	stateid_t		cp_dst_stateid;
743 	u64			cp_src_pos;
744 	u64			cp_dst_pos;
745 	u64			cp_count;
746 	struct nl4_server	*cp_src;
747 
748 	unsigned long		cp_flags;
749 #define NFSD4_COPY_F_STOPPED		(0)
750 #define NFSD4_COPY_F_INTRA		(1)
751 #define NFSD4_COPY_F_SYNCHRONOUS	(2)
752 #define NFSD4_COPY_F_COMMITTED		(3)
753 #define NFSD4_COPY_F_COMPLETED		(4)
754 #define NFSD4_COPY_F_OFFLOAD_DONE	(5)
755 #define NFSD4_COPY_F_CB_ERROR		(6)
756 
757 	/* response */
758 	__be32			nfserr;
759 	struct nfsd42_write_res	cp_res;
760 	struct knfsd_fh		fh;
761 
762 	/* offload callback */
763 	struct nfsd4_cb_offload	cp_cb_offload;
764 
765 	struct nfs4_client      *cp_clp;
766 
767 	struct nfsd_file        *nf_src;
768 	struct nfsd_file        *nf_dst;
769 	bool			attr_update;
770 
771 	copy_stateid_t		cp_stateid;
772 
773 	struct list_head	copies;
774 	struct task_struct	*copy_task;
775 	refcount_t		refcount;
776 	unsigned int		cp_ttl;
777 
778 	struct nfsd4_ssc_umount_item *ss_nsui;
779 	struct nfs_fh		c_fh;
780 	nfs4_stateid		stateid;
781 	struct nfsd_net		*cp_nn;
782 };
783 
784 static inline void nfsd4_copy_set_sync(struct nfsd4_copy *copy, bool sync)
785 {
786 	if (sync)
787 		set_bit(NFSD4_COPY_F_SYNCHRONOUS, &copy->cp_flags);
788 	else
789 		clear_bit(NFSD4_COPY_F_SYNCHRONOUS, &copy->cp_flags);
790 }
791 
792 static inline bool nfsd4_copy_is_sync(const struct nfsd4_copy *copy)
793 {
794 	return test_bit(NFSD4_COPY_F_SYNCHRONOUS, &copy->cp_flags);
795 }
796 
797 static inline bool nfsd4_copy_is_async(const struct nfsd4_copy *copy)
798 {
799 	return !test_bit(NFSD4_COPY_F_SYNCHRONOUS, &copy->cp_flags);
800 }
801 
802 static inline bool nfsd4_ssc_is_inter(const struct nfsd4_copy *copy)
803 {
804 	return !test_bit(NFSD4_COPY_F_INTRA, &copy->cp_flags);
805 }
806 
807 struct nfsd4_seek {
808 	/* request */
809 	stateid_t	seek_stateid;
810 	loff_t		seek_offset;
811 	u32		seek_whence;
812 
813 	/* response */
814 	u32		seek_eof;
815 	loff_t		seek_pos;
816 };
817 
818 struct nfsd4_offload_status {
819 	/* request */
820 	stateid_t	stateid;
821 
822 	/* response */
823 	u64		count;
824 	__be32		status;
825 	bool		completed;
826 };
827 
828 struct nfsd4_copy_notify {
829 	/* request */
830 	stateid_t		cpn_src_stateid;
831 	struct nl4_server	*cpn_dst;
832 
833 	/* response */
834 	stateid_t		cpn_cnr_stateid;
835 	struct timespec64	cpn_lease_time;
836 	struct nl4_server	*cpn_src;
837 };
838 
839 struct nfsd4_op {
840 	u32					opnum;
841 	__be32					status;
842 	const struct nfsd4_operation		*opdesc;
843 	struct nfs4_replay			*replay;
844 	union nfsd4_op_u {
845 		struct nfsd4_access		access;
846 		struct nfsd4_close		close;
847 		struct nfsd4_commit		commit;
848 		struct nfsd4_create		create;
849 		struct nfsd4_delegreturn	delegreturn;
850 		struct nfsd4_getattr		getattr;
851 		struct svc_fh *			getfh;
852 		struct nfsd4_link		link;
853 		struct nfsd4_lock		lock;
854 		struct nfsd4_lockt		lockt;
855 		struct nfsd4_locku		locku;
856 		struct nfsd4_lookup		lookup;
857 		struct nfsd4_verify		nverify;
858 		struct nfsd4_open		open;
859 		struct nfsd4_open_confirm	open_confirm;
860 		struct nfsd4_open_downgrade	open_downgrade;
861 		struct nfsd4_putfh		putfh;
862 		struct nfsd4_read		read;
863 		struct nfsd4_readdir		readdir;
864 		struct nfsd4_readlink		readlink;
865 		struct nfsd4_remove		remove;
866 		struct nfsd4_rename		rename;
867 		clientid_t			renew;
868 		struct nfsd4_secinfo		secinfo;
869 		struct nfsd4_setattr		setattr;
870 		struct nfsd4_setclientid	setclientid;
871 		struct nfsd4_setclientid_confirm setclientid_confirm;
872 		struct nfsd4_verify		verify;
873 		struct nfsd4_write		write;
874 		struct nfsd4_release_lockowner	release_lockowner;
875 
876 		/* NFSv4.1 */
877 		struct nfsd4_exchange_id	exchange_id;
878 		struct nfsd4_backchannel_ctl	backchannel_ctl;
879 		struct nfsd4_bind_conn_to_session bind_conn_to_session;
880 		struct nfsd4_create_session	create_session;
881 		struct nfsd4_destroy_session	destroy_session;
882 		struct nfsd4_destroy_clientid	destroy_clientid;
883 		struct nfsd4_sequence		sequence;
884 		struct nfsd4_reclaim_complete	reclaim_complete;
885 		struct nfsd4_test_stateid	test_stateid;
886 		struct nfsd4_free_stateid	free_stateid;
887 		struct nfsd4_get_dir_delegation	get_dir_delegation;
888 		struct nfsd4_getdeviceinfo	getdeviceinfo;
889 		struct nfsd4_layoutget		layoutget;
890 		struct nfsd4_layoutcommit	layoutcommit;
891 		struct nfsd4_layoutreturn	layoutreturn;
892 		struct nfsd4_secinfo_no_name	secinfo_no_name;
893 
894 		/* NFSv4.2 */
895 		struct nfsd4_fallocate		allocate;
896 		struct nfsd4_fallocate		deallocate;
897 		struct nfsd4_clone		clone;
898 		struct nfsd4_copy		copy;
899 		struct nfsd4_offload_status	offload_status;
900 		struct nfsd4_copy_notify	copy_notify;
901 		struct nfsd4_seek		seek;
902 
903 		struct nfsd4_getxattr		getxattr;
904 		struct nfsd4_setxattr		setxattr;
905 		struct nfsd4_listxattrs		listxattrs;
906 		struct nfsd4_removexattr	removexattr;
907 	} u;
908 };
909 
910 bool nfsd4_cache_this_op(struct nfsd4_op *);
911 
912 /*
913  * Memory needed just for the duration of processing one compound:
914  */
915 struct svcxdr_tmpbuf {
916 	struct svcxdr_tmpbuf *next;
917 	char buf[];
918 };
919 
920 struct nfsd4_compoundargs {
921 	/* scratch variables for XDR decode */
922 	struct xdr_stream		*xdr;
923 	struct svcxdr_tmpbuf		*to_free;
924 	struct svc_rqst			*rqstp;
925 
926 	char *				tag;
927 	u32				taglen;
928 	u32				minorversion;
929 	u32				client_opcnt;
930 	u32				opcnt;
931 	bool				splice_ok;
932 	struct nfsd4_op			*ops;
933 	struct nfsd4_op			iops[8];
934 };
935 
936 struct nfsd4_compoundres {
937 	/* scratch variables for XDR encode */
938 	struct xdr_stream		*xdr;
939 	struct svc_rqst *		rqstp;
940 
941 	__be32				*statusp;
942 	char *				tag;
943 	u32				taglen;
944 	u32				opcnt;
945 
946 	struct nfsd4_compound_state	cstate;
947 };
948 
949 static inline bool nfsd4_last_compound_op(struct svc_rqst *rqstp)
950 {
951 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
952 	struct nfsd4_compoundargs *argp = rqstp->rq_argp;
953 
954 	return argp->opcnt == resp->opcnt;
955 }
956 
957 const struct nfsd4_operation *OPDESC(struct nfsd4_op *op);
958 int nfsd4_max_reply(struct svc_rqst *rqstp, struct nfsd4_op *op);
959 void warn_on_nonidempotent_op(struct nfsd4_op *op);
960 
961 #define NFS4_SVC_XDRSIZE		sizeof(struct nfsd4_compoundargs)
962 
963 bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp);
964 bool nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, struct xdr_stream *xdr);
965 bool nfs4svc_encode_compoundres(struct svc_rqst *rqstp, struct xdr_stream *xdr);
966 __be32 nfsd4_check_resp_size(struct nfsd4_compoundres *, u32);
967 void nfsd4_encode_operation(struct nfsd4_compoundres *, struct nfsd4_op *);
968 void nfsd4_encode_replay(struct xdr_stream *xdr, struct nfsd4_op *op);
969 __be32 nfsd4_encode_fattr_to_buf(__be32 **p, int words,
970 		struct svc_fh *fhp, struct svc_export *exp,
971 		struct dentry *dentry,
972 		u32 *bmval, struct svc_rqst *, int ignore_crossmnt);
973 extern __be32 nfsd4_setclientid(struct svc_rqst *rqstp,
974 		struct nfsd4_compound_state *, union nfsd4_op_u *u);
975 extern __be32 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
976 		struct nfsd4_compound_state *, union nfsd4_op_u *u);
977 void nfsd4_exchange_id_release(union nfsd4_op_u *u);
978 extern __be32 nfsd4_exchange_id(struct svc_rqst *rqstp,
979 		struct nfsd4_compound_state *, union nfsd4_op_u *u);
980 extern __be32 nfsd4_backchannel_ctl(struct svc_rqst *,
981 		struct nfsd4_compound_state *, union nfsd4_op_u *u);
982 extern __be32 nfsd4_bind_conn_to_session(struct svc_rqst *,
983 		struct nfsd4_compound_state *, union nfsd4_op_u *u);
984 extern __be32 nfsd4_create_session(struct svc_rqst *,
985 		struct nfsd4_compound_state *, union nfsd4_op_u *u);
986 extern __be32 nfsd4_sequence(struct svc_rqst *,
987 		struct nfsd4_compound_state *, union nfsd4_op_u *u);
988 extern void nfsd4_sequence_done(struct nfsd4_compoundres *resp);
989 extern __be32 nfsd4_destroy_session(struct svc_rqst *,
990 		struct nfsd4_compound_state *, union nfsd4_op_u *u);
991 extern __be32 nfsd4_destroy_clientid(struct svc_rqst *, struct nfsd4_compound_state *,
992 		union nfsd4_op_u *u);
993 __be32 nfsd4_reclaim_complete(struct svc_rqst *, struct nfsd4_compound_state *,
994 		union nfsd4_op_u *u);
995 extern __be32 nfsd4_process_open1(struct nfsd4_compound_state *,
996 		struct nfsd4_open *open, struct nfsd_net *nn);
997 extern __be32 nfsd4_process_open2(struct svc_rqst *rqstp,
998 		struct svc_fh *current_fh, struct nfsd4_open *open);
999 extern void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate);
1000 extern void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
1001 		struct nfsd4_open *open);
1002 extern __be32 nfsd4_open_confirm(struct svc_rqst *rqstp,
1003 		struct nfsd4_compound_state *, union nfsd4_op_u *u);
1004 extern __be32 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *,
1005 		union nfsd4_op_u *u);
1006 extern __be32 nfsd4_open_downgrade(struct svc_rqst *rqstp,
1007 		struct nfsd4_compound_state *, union nfsd4_op_u *u);
1008 extern __be32 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *,
1009 		union nfsd4_op_u *u);
1010 extern void nfsd4_lock_release(union nfsd4_op_u *u);
1011 extern __be32 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *,
1012 		union nfsd4_op_u *u);
1013 extern void nfsd4_lockt_release(union nfsd4_op_u *u);
1014 extern __be32 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *,
1015 		union nfsd4_op_u *u);
1016 extern __be32
1017 nfsd4_release_lockowner(struct svc_rqst *rqstp,
1018 		struct nfsd4_compound_state *, union nfsd4_op_u *u);
1019 extern void nfsd4_release_compoundargs(struct svc_rqst *rqstp);
1020 extern __be32 nfsd4_delegreturn(struct svc_rqst *rqstp,
1021 		struct nfsd4_compound_state *, union nfsd4_op_u *u);
1022 extern __be32 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *,
1023 		union nfsd4_op_u *u);
1024 extern __be32 nfsd4_test_stateid(struct svc_rqst *rqstp,
1025 		struct nfsd4_compound_state *, union nfsd4_op_u *);
1026 extern __be32 nfsd4_free_stateid(struct svc_rqst *rqstp,
1027 		struct nfsd4_compound_state *, union nfsd4_op_u *);
1028 extern void nfsd4_bump_seqid(struct nfsd4_compound_state *, __be32 nfserr);
1029 
1030 enum nfsd4_op_flags {
1031 	ALLOWED_WITHOUT_FH = 1 << 0,    /* No current filehandle required */
1032 	ALLOWED_ON_ABSENT_FS = 1 << 1,  /* ops processed on absent fs */
1033 	ALLOWED_AS_FIRST_OP = 1 << 2,   /* ops reqired first in compound */
1034 	/* For rfc 5661 section 2.6.3.1.1: */
1035 	OP_HANDLES_WRONGSEC = 1 << 3,
1036 	OP_IS_PUTFH_LIKE = 1 << 4,
1037 	/*
1038 	 * These are the ops whose result size we estimate before
1039 	 * encoding, to avoid performing an op then not being able to
1040 	 * respond or cache a response.  This includes writes and setattrs
1041 	 * as well as the operations usually called "nonidempotent":
1042 	 */
1043 	OP_MODIFIES_SOMETHING = 1 << 5,
1044 	/*
1045 	 * Cache compounds containing these ops in the xid-based drc:
1046 	 * We use the DRC for compounds containing non-idempotent
1047 	 * operations, *except* those that are 4.1-specific (since
1048 	 * sessions provide their own EOS), and except for stateful
1049 	 * operations other than setclientid and setclientid_confirm
1050 	 * (since sequence numbers provide EOS for open, lock, etc in
1051 	 * the v4.0 case).
1052 	 */
1053 	OP_CACHEME = 1 << 6,
1054 	/*
1055 	 * These are ops which clear current state id.
1056 	 */
1057 	OP_CLEAR_STATEID = 1 << 7,
1058 	/* Most ops return only an error on failure; some may do more: */
1059 	OP_NONTRIVIAL_ERROR_ENCODE = 1 << 8,
1060 };
1061 
1062 struct nfsd4_operation {
1063 	__be32 (*op_func)(struct svc_rqst *, struct nfsd4_compound_state *,
1064 			union nfsd4_op_u *);
1065 	void (*op_release)(union nfsd4_op_u *);
1066 	u32 op_flags;
1067 	char *op_name;
1068 	/* Try to get response size before operation */
1069 	u32 (*op_rsize_bop)(const struct svc_rqst *rqstp,
1070 			const struct nfsd4_op *op);
1071 	void (*op_get_currentstateid)(struct nfsd4_compound_state *,
1072 			union nfsd4_op_u *);
1073 	void (*op_set_currentstateid)(struct nfsd4_compound_state *,
1074 			union nfsd4_op_u *);
1075 };
1076 
1077 struct nfsd4_cb_recall_any {
1078 	struct nfsd4_callback	ra_cb;
1079 	u32			ra_keep;
1080 	u32			ra_bmval[1];
1081 };
1082 
1083 #endif
1084