1 /*
2 * Copyright (c) 2001 The Regents of the University of Michigan.
3 * All rights reserved.
4 *
5 * Kendrick Smith <kmsmith@umich.edu>
6 * Andy Adamson <andros@umich.edu>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35 #ifndef _NFSD4_STATE_H
36 #define _NFSD4_STATE_H
37
38 #include <crypto/md5.h>
39
40 #include <linux/filelock.h>
41 #include <linux/idr.h>
42 #include <linux/refcount.h>
43 #include <linux/sunrpc/svc_xprt.h>
44
45 #include "nfsfh.h"
46 #include "nfsd.h"
47
48 typedef struct {
49 u32 cl_boot;
50 u32 cl_id;
51 } clientid_t;
52
53 typedef struct {
54 clientid_t so_clid;
55 u32 so_id;
56 } stateid_opaque_t;
57
58 typedef struct {
59 u32 si_generation;
60 stateid_opaque_t si_opaque;
61 } stateid_t;
62
63 typedef struct {
64 stateid_t cs_stid;
65 #define NFS4_COPYNOTIFY_STID 2
66 unsigned char cs_type;
67 refcount_t cs_count;
68 } copy_stateid_t;
69
70 struct nfsd4_referring_call {
71 struct list_head __list;
72
73 u32 rc_sequenceid;
74 u32 rc_slotid;
75 };
76
77 struct nfsd4_referring_call_list {
78 struct list_head __list;
79
80 struct nfs4_sessionid rcl_sessionid;
81 int __nr_referring_calls;
82 struct list_head rcl_referring_calls;
83 };
84
85 struct nfsd4_callback {
86 struct nfs4_client *cb_clp;
87 struct rpc_message cb_msg;
88 #define NFSD4_CALLBACK_RUNNING (0)
89 #define NFSD4_CALLBACK_WAKE (1)
90 #define NFSD4_CALLBACK_REQUEUE (2)
91 unsigned long cb_flags;
92 const struct nfsd4_callback_ops *cb_ops;
93 struct work_struct cb_work;
94 int cb_seq_status;
95 int cb_status;
96 int cb_held_slot;
97
98 int cb_nr_referring_call_list;
99 struct list_head cb_referring_call_list;
100 };
101
102 struct nfsd4_callback_ops {
103 bool (*prepare)(struct nfsd4_callback *cb);
104 int (*done)(struct nfsd4_callback *cb, struct rpc_task *task);
105 void (*release)(struct nfsd4_callback *cb);
106 uint32_t opcode;
107 };
108
109 /*
110 * A core object that represents a "common" stateid. These are generally
111 * embedded within the different (more specific) stateid objects and contain
112 * fields that are of general use to any stateid.
113 */
114 struct nfs4_stid {
115 refcount_t sc_count;
116
117 /* A new stateid is added to the cl_stateids idr early before it
118 * is fully initialised. Its sc_type is then zero. After
119 * initialisation the sc_type it set under cl_lock, and then
120 * never changes.
121 */
122 #define SC_TYPE_OPEN BIT(0)
123 #define SC_TYPE_LOCK BIT(1)
124 #define SC_TYPE_DELEG BIT(2)
125 #define SC_TYPE_LAYOUT BIT(3)
126 #define SC_TYPE_COPY BIT(4)
127 unsigned short sc_type;
128
129 /* nn->deleg_lock protects sc_status for delegation stateids.
130 * ->cl_lock protects sc_status for open and lock stateids.
131 * ->st_mutex also protect sc_status for open stateids.
132 * ->ls_lock protects sc_status for layout stateids.
133 */
134 /*
135 * For an open stateid kept around *only* to process close replays.
136 * For deleg stateid, kept in idr until last reference is dropped.
137 */
138 #define SC_STATUS_CLOSED BIT(0)
139 /* For a deleg stateid kept around only to process free_stateid's: */
140 #define SC_STATUS_REVOKED BIT(1)
141 #define SC_STATUS_ADMIN_REVOKED BIT(2)
142 #define SC_STATUS_FREEABLE BIT(3)
143 #define SC_STATUS_FREED BIT(4)
144 unsigned short sc_status;
145
146 struct list_head sc_cp_list;
147 stateid_t sc_stateid;
148 spinlock_t sc_lock;
149 struct nfs4_client *sc_client;
150 struct nfs4_file *sc_file;
151 struct svc_export *sc_export;
152 void (*sc_free)(struct nfs4_stid *);
153 };
154
155 /* Keep a list of stateids issued by the COPY_NOTIFY, associate it with the
156 * parent OPEN/LOCK/DELEG stateid.
157 */
158 struct nfs4_cpntf_state {
159 copy_stateid_t cp_stateid;
160 struct list_head cp_list; /* per parent nfs4_stid */
161 stateid_t cp_p_stateid; /* copy of parent's stateid */
162 clientid_t cp_p_clid; /* copy of parent's clid */
163 time64_t cpntf_time; /* last time stateid used */
164 };
165
166 /*
167 * RFC 7862 Section 4.8 states:
168 *
169 * | A copy offload stateid will be valid until either (A) the client
170 * | or server restarts or (B) the client returns the resource by
171 * | issuing an OFFLOAD_CANCEL operation or the client replies to a
172 * | CB_OFFLOAD operation.
173 *
174 * Because a client might not reply to a CB_OFFLOAD, or a reply
175 * might get lost due to connection loss, NFSD purges async copy
176 * state after a short period to prevent it from accumulating
177 * over time.
178 */
179 #define NFSD_COPY_INITIAL_TTL 10
180
181 struct nfs4_cb_fattr {
182 struct nfsd4_callback ncf_getattr;
183 u32 ncf_cb_status;
184
185 /* from CB_GETATTR reply */
186 u64 ncf_cb_change;
187 u64 ncf_cb_fsize;
188 struct timespec64 ncf_cb_mtime;
189 struct timespec64 ncf_cb_atime;
190
191 bool ncf_file_modified;
192 u64 ncf_initial_cinfo;
193 u64 ncf_cur_fsize;
194 };
195
196 /*
197 * FIXME: the current backchannel encoder can't handle a send buffer longer
198 * than a single page (see bc_malloc/bc_free).
199 */
200 #define NOTIFY4_EVENT_QUEUE_SIZE 3
201 #define NOTIFY4_PAGE_ARRAY_SIZE 1
202
203 struct nfsd_notify_event {
204 refcount_t ne_ref; // refcount
205 u32 ne_mask; // FS_* mask from fsnotify callback
206 struct dentry *ne_dentry; // dentry reference to target
207 struct inode *ne_target; // inode overwritten by rename, or NULL
208 u32 ne_namelen; // length of ne_name (old name for a rename)
209 u32 ne_newnamelen; // length of new name (rename only), else 0
210 char ne_name[]; // entry name, then new name (rename only)
211 };
212
213 /*
214 * For a rename, the new name is snapshotted at event-alloc time and stored
215 * immediately after the (NUL-terminated) old name in ne_name[]. ne_dentry can
216 * be renamed again before the CB_NOTIFY work runs, so the new name must not be
217 * read from the live dentry at encode time.
218 */
nfsd_notify_event_newname(struct nfsd_notify_event * ne)219 static inline char *nfsd_notify_event_newname(struct nfsd_notify_event *ne)
220 {
221 return ne->ne_name + ne->ne_namelen + 1;
222 }
223
nfsd_notify_event_get(struct nfsd_notify_event * ne)224 static inline struct nfsd_notify_event *nfsd_notify_event_get(struct nfsd_notify_event *ne)
225 {
226 refcount_inc(&ne->ne_ref);
227 return ne;
228 }
229
nfsd_notify_event_put(struct nfsd_notify_event * ne)230 static inline void nfsd_notify_event_put(struct nfsd_notify_event *ne)
231 {
232 if (refcount_dec_and_test(&ne->ne_ref)) {
233 iput(ne->ne_target);
234 dput(ne->ne_dentry);
235 kfree(ne);
236 }
237 }
238
239 /*
240 * Represents a directory delegation. The callback is for handling CB_NOTIFYs.
241 * As notifications from fsnotify come in, allocate a new event, take the ncn_lock,
242 * and add it to the ncn_evt queue. The CB_NOTIFY prepare handler will take the
243 * lock, clean out the list and process it.
244 */
245 struct nfsd4_cb_notify {
246 spinlock_t ncn_lock; // protects the evt queue and count
247 int ncn_evt_cnt; // count of events in ncn_evt
248 int ncn_nf_cnt; // count of valid entries in ncn_nf
249 struct nfsd_notify_event *ncn_evt[NOTIFY4_EVENT_QUEUE_SIZE]; // list of events
250 struct page *ncn_pages[NOTIFY4_PAGE_ARRAY_SIZE]; // for encoding
251 struct notify4 *ncn_nf; // array of notify4's to be sent
252 bool ncn_encode_err; // did encoding fail?
253 struct nfsd4_callback ncn_cb; // notify4 callback
254 };
255
256 /*
257 * Represents a delegation stateid. The nfs4_client holds references to these
258 * and they are put when it is being destroyed or when the delegation is
259 * returned by the client:
260 *
261 * o 1 reference as long as a delegation is still in force (taken when it's
262 * alloc'd, put when it's returned or revoked)
263 *
264 * o 1 reference as long as a recall rpc is in progress (taken when the lease
265 * is broken, put when the rpc exits)
266 *
267 * o 1 more ephemeral reference for each nfsd thread currently doing something
268 * with that delegation without holding the cl_lock
269 *
270 * If the server attempts to recall a delegation and the client doesn't do so
271 * before a timeout, the server may also revoke the delegation. In that case,
272 * the object will either be destroyed (v4.0) or moved to a per-client list of
273 * revoked delegations (v4.1+).
274 *
275 * This object is a superset of the nfs4_stid.
276 */
277 struct nfs4_delegation {
278 struct nfs4_stid dl_stid; /* must be first field */
279 struct list_head dl_perfile;
280 struct list_head dl_perclnt;
281 struct list_head dl_recall_lru; /* delegation recalled */
282 struct nfs4_clnt_odstate *dl_clnt_odstate;
283 time64_t dl_time;
284 u32 dl_type;
285 /* For recall: */
286 int dl_retries;
287 struct nfsd4_callback dl_recall;
288 bool dl_recalled;
289 bool dl_written;
290 bool dl_setattr;
291
292 union {
293 /* for CB_GETATTR */
294 struct nfs4_cb_fattr dl_cb_fattr;
295 /* for CB_NOTIFY */
296 struct nfsd4_cb_notify dl_cb_notify;
297 };
298
299 /* For delegated timestamps */
300 struct timespec64 dl_atime;
301 struct timespec64 dl_mtime;
302 struct timespec64 dl_ctime;
303
304 /* For dir delegations */
305 u32 dl_notify_mask;
306 u32 dl_child_attrs[2];
307 u32 dl_dir_attrs[2];
308 };
309
deleg_is_read(u32 dl_type)310 static inline bool deleg_is_read(u32 dl_type)
311 {
312 return (dl_type == OPEN_DELEGATE_READ || dl_type == OPEN_DELEGATE_READ_ATTRS_DELEG);
313 }
314
deleg_is_write(u32 dl_type)315 static inline bool deleg_is_write(u32 dl_type)
316 {
317 return (dl_type == OPEN_DELEGATE_WRITE || dl_type == OPEN_DELEGATE_WRITE_ATTRS_DELEG);
318 }
319
deleg_attrs_deleg(u32 dl_type)320 static inline bool deleg_attrs_deleg(u32 dl_type)
321 {
322 return dl_type == OPEN_DELEGATE_READ_ATTRS_DELEG ||
323 dl_type == OPEN_DELEGATE_WRITE_ATTRS_DELEG;
324 }
325
326 bool nfsd4_vet_deleg_time(struct timespec64 *cb, const struct timespec64 *orig,
327 const struct timespec64 *now);
328
329 #define cb_to_delegation(cb) \
330 container_of(cb, struct nfs4_delegation, dl_recall)
331
332 /* client delegation callback info */
333 struct nfs4_cb_conn {
334 /* SETCLIENTID info */
335 struct sockaddr_storage cb_addr;
336 struct sockaddr_storage cb_saddr;
337 size_t cb_addrlen;
338 u32 cb_prog; /* used only in 4.0 case;
339 per-session otherwise */
340 u32 cb_ident; /* minorversion 0 only */
341 struct svc_xprt *cb_xprt; /* minorversion 1 only */
342 };
343
delegstateid(struct nfs4_stid * s)344 static inline struct nfs4_delegation *delegstateid(struct nfs4_stid *s)
345 {
346 return container_of(s, struct nfs4_delegation, dl_stid);
347 }
348
349 /* Maximum number of slots per session. This is for sanity-check only.
350 * It could be increased if we had a mechanism to shutdown misbehaving clients.
351 * A large number can be needed to get good throughput on high-latency servers.
352 */
353 #define NFSD_MAX_SLOTS_PER_SESSION 2048
354 /* Maximum session per slot cache size */
355 #define NFSD_SLOT_CACHE_SIZE 2048
356 /* Maximum number of NFSD_SLOT_CACHE_SIZE slots per session */
357 #define NFSD_CACHE_SIZE_SLOTS_PER_SESSION 32
358 #define NFSD_MAX_MEM_PER_SESSION \
359 (NFSD_CACHE_SIZE_SLOTS_PER_SESSION * NFSD_SLOT_CACHE_SIZE)
360
361 struct nfsd4_slot {
362 u32 sl_seqid;
363 __be32 sl_status;
364 struct svc_cred sl_cred;
365 u32 sl_index;
366 u32 sl_datalen;
367 u16 sl_opcnt;
368 u16 sl_generation;
369 #define NFSD4_SLOT_INUSE (1 << 0)
370 #define NFSD4_SLOT_CACHETHIS (1 << 1)
371 #define NFSD4_SLOT_INITIALIZED (1 << 2)
372 #define NFSD4_SLOT_CACHED (1 << 3)
373 #define NFSD4_SLOT_REUSED (1 << 4)
374 u8 sl_flags;
375 char sl_data[];
376 };
377
378 struct nfsd4_channel_attrs {
379 u32 headerpadsz;
380 u32 maxreq_sz;
381 u32 maxresp_sz;
382 u32 maxresp_cached;
383 u32 maxops;
384 u32 maxreqs;
385 u32 nr_rdma_attrs;
386 u32 rdma_attrs;
387 };
388
389 struct nfsd4_cb_sec {
390 u32 flavor; /* (u32)(-1) used to mean "no valid flavor" */
391 kuid_t uid;
392 kgid_t gid;
393 };
394
395 struct nfsd4_create_session {
396 clientid_t clientid;
397 struct nfs4_sessionid sessionid;
398 u32 seqid;
399 u32 flags;
400 struct nfsd4_channel_attrs fore_channel;
401 struct nfsd4_channel_attrs back_channel;
402 u32 callback_prog;
403 struct nfsd4_cb_sec cb_sec;
404 };
405
406 struct nfsd4_backchannel_ctl {
407 u32 bc_cb_program;
408 struct nfsd4_cb_sec bc_cb_sec;
409 };
410
411 struct nfsd4_bind_conn_to_session {
412 struct nfs4_sessionid sessionid;
413 u32 dir;
414 };
415
416 /* The single slot clientid cache structure */
417 struct nfsd4_clid_slot {
418 u32 sl_seqid;
419 __be32 sl_status;
420 struct nfsd4_create_session sl_cr_ses;
421 };
422
423 struct nfsd4_conn {
424 struct list_head cn_persession;
425 struct svc_xprt *cn_xprt;
426 struct svc_xpt_user cn_xpt_user;
427 struct nfsd4_session *cn_session;
428 /* CDFC4_FORE, CDFC4_BACK: */
429 unsigned char cn_flags;
430 };
431
432 /* Maximum number of slots that nfsd will use in the backchannel */
433 #define NFSD_BC_SLOT_TABLE_SIZE (sizeof(u32) * 8)
434
435 /*
436 * Representation of a v4.1+ session. These are refcounted in a similar fashion
437 * to the nfs4_client. References are only taken when the server is actively
438 * working on the object (primarily during the processing of compounds).
439 */
440 struct nfsd4_session {
441 atomic_t se_ref;
442 spinlock_t se_lock;
443 u32 se_cb_slot_avail; /* bitmap of available slots */
444 u32 se_cb_highest_slot; /* highest slot client wants */
445 u32 se_cb_prog;
446 struct list_head se_hash; /* hash by sessionid */
447 struct list_head se_perclnt;
448 struct list_head se_all_sessions;/* global list of sessions */
449 struct nfs4_client *se_client;
450 struct nfs4_sessionid se_sessionid;
451 struct nfsd4_channel_attrs se_fchannel;
452 struct nfsd4_cb_sec se_cb_sec;
453 struct list_head se_conns;
454 u32 se_cb_seq_nr[NFSD_BC_SLOT_TABLE_SIZE];
455 struct xarray se_slots; /* forward channel slots */
456 u16 se_slot_gen;
457 bool se_dead;
458 u32 se_target_maxslots;
459 struct rcu_head rcu_head;
460 };
461
462 /* formatted contents of nfs4_sessionid */
463 struct nfsd4_sessionid {
464 clientid_t clientid;
465 u32 sequence;
466 u32 reserved;
467 };
468
469 /* Length of MD5 digest as hex, plus terminating '\0' */
470 #define HEXDIR_LEN (2 * MD5_DIGEST_SIZE + 1)
471
472 /*
473 * State Meaning Where set
474 * --------------------------------------------------------------------------
475 * | NFSD4_ACTIVE | Confirmed, active | Default |
476 * |------------------- ----------------------------------------------------|
477 * | NFSD4_COURTESY | Courtesy state. | nfs4_get_client_reaplist |
478 * | | Lease/lock/share | |
479 * | | reservation conflict | |
480 * | | can cause Courtesy | |
481 * | | client to be expired | |
482 * |------------------------------------------------------------------------|
483 * | NFSD4_EXPIRABLE | Courtesy client to be| nfs4_laundromat |
484 * | | expired by Laundromat| try_to_expire_client |
485 * | | due to conflict | |
486 * |------------------------------------------------------------------------|
487 */
488 enum {
489 NFSD4_ACTIVE = 0,
490 NFSD4_COURTESY,
491 NFSD4_EXPIRABLE,
492 };
493
494 /*
495 * struct nfs4_client - one per client. Clientids live here.
496 *
497 * The initial object created by an NFS client using SETCLIENTID (for NFSv4.0)
498 * or EXCHANGE_ID (for NFSv4.1+). These objects are refcounted and timestamped.
499 * Each nfsd_net_ns object contains a set of these and they are tracked via
500 * short and long form clientid. They are hashed and searched for under the
501 * per-nfsd_net client_lock spinlock.
502 *
503 * References to it are only held during the processing of compounds, and in
504 * certain other operations. In their "resting state" they have a refcount of
505 * 0. If they are not renewed within a lease period, they become eligible for
506 * destruction by the laundromat.
507 *
508 * These objects can also be destroyed if the client sends certain forms of
509 * SETCLIENTID or EXCHANGE_ID operations.
510 *
511 * Care is taken *not* to do this however when the objects have an elevated
512 * refcount.
513 *
514 * o Each nfs4_client is hashed by clientid
515 *
516 * o Each nfs4_clients is also hashed by name (the opaque quantity initially
517 * sent by the client to identify itself).
518 *
519 * o cl_perclient list is used to ensure no dangling stateowner references
520 * when we expire the nfs4_client
521 */
522 struct nfs4_client {
523 struct list_head cl_idhash; /* hash by cl_clientid.id */
524 struct rb_node cl_namenode; /* link into by-name trees */
525 struct list_head *cl_ownerstr_hashtbl;
526 struct list_head cl_openowners;
527 struct idr cl_stateids; /* stateid lookup */
528 struct list_head cl_delegations;
529 struct list_head cl_revoked; /* unacknowledged, revoked 4.1 state */
530 struct list_head cl_lru; /* tail queue */
531 #ifdef CONFIG_NFSD_PNFS
532 struct list_head cl_lo_states; /* outstanding layout states */
533 bool cl_fence_retry_warn;
534 #endif
535 struct xdr_netobj cl_name; /* id generated by client */
536 nfs4_verifier cl_verifier; /* generated by client */
537 time64_t cl_time; /* time of last lease renewal */
538 struct sockaddr_storage cl_addr; /* client ipaddress */
539 bool cl_mach_cred; /* SP4_MACH_CRED in force */
540 struct svc_cred cl_cred; /* setclientid principal */
541 clientid_t cl_clientid; /* generated by server */
542 nfs4_verifier cl_confirm; /* generated by server */
543 u32 cl_minorversion;
544 atomic_t cl_admin_revoked; /* count of admin-revoked states */
545 /* NFSv4.1 client implementation id: */
546 struct xdr_netobj cl_nii_domain;
547 struct xdr_netobj cl_nii_name;
548 struct timespec64 cl_nii_time;
549
550 /* for v4.0 and v4.1 callbacks: */
551 struct nfs4_cb_conn cl_cb_conn;
552 #define NFSD4_CLIENT_CB_UPDATE (0)
553 #define NFSD4_CLIENT_CB_KILL (1)
554 #define NFSD4_CLIENT_STABLE (2) /* client on stable storage */
555 #define NFSD4_CLIENT_RECLAIM_COMPLETE (3) /* reclaim_complete done */
556 #define NFSD4_CLIENT_CONFIRMED (4) /* client is confirmed */
557 #define NFSD4_CLIENT_UPCALL_LOCK (5) /* upcall serialization */
558 #define NFSD4_CLIENT_CB_FLAG_MASK (1 << NFSD4_CLIENT_CB_UPDATE | \
559 1 << NFSD4_CLIENT_CB_KILL)
560 unsigned long cl_flags;
561
562 struct workqueue_struct *cl_callback_wq;
563 const struct cred *cl_cb_cred;
564 struct rpc_clnt *cl_cb_client;
565 u32 cl_cb_ident;
566 #define NFSD4_CB_UP 0
567 #define NFSD4_CB_UNKNOWN 1
568 #define NFSD4_CB_DOWN 2
569 #define NFSD4_CB_FAULT 3
570 int cl_cb_state;
571 struct nfsd4_callback cl_cb_null;
572 struct nfsd4_session __rcu *cl_cb_session;
573
574 /* for all client information that callback code might need: */
575 spinlock_t cl_lock;
576
577 /* for nfs41 */
578 struct list_head cl_sessions;
579 struct nfsd4_clid_slot cl_cs_slot; /* create_session slot */
580 u32 cl_exchange_flags;
581 /* number of rpc's in progress over an associated session: */
582 atomic_t cl_rpc_users;
583 struct nfsdfs_client cl_nfsdfs;
584 struct nfs4_op_map cl_spo_must_allow;
585
586 /* debugging info directory under nfsd/clients/ : */
587 struct dentry *cl_nfsd_dentry;
588 /* 'info' file within that directory. Ref is not counted,
589 * but will remain valid iff cl_nfsd_dentry != NULL
590 */
591 struct dentry *cl_nfsd_info_dentry;
592
593 struct rpc_wait_queue cl_cb_waitq; /* backchannel callers may */
594 /* wait here for slots */
595 struct net *net;
596 struct list_head async_copies; /* list of async copies */
597 spinlock_t async_lock; /* lock for async copies */
598 atomic_t cl_cb_inflight; /* Outstanding callbacks */
599
600 unsigned int cl_state;
601 atomic_t cl_delegs_in_recall;
602
603 struct nfsd4_cb_recall_any *cl_ra;
604 time64_t cl_ra_time;
605 #ifdef CONFIG_NFSD_SCSILAYOUT
606 struct xarray cl_dev_fences;
607 struct mutex cl_fence_mutex;
608 #endif
609 };
610
611 /* struct nfs4_client_reset
612 * one per old client. Populates reset_str_hashtbl. Filled from conf_id_hashtbl
613 * upon lease reset, or from upcall to state_daemon (to read in state
614 * from non-volitile storage) upon reboot.
615 */
616 struct nfs4_client_reclaim {
617 struct list_head cr_strhash; /* hash by cr_name */
618 struct nfs4_client *cr_clp; /* pointer to associated clp */
619 struct xdr_netobj cr_name; /* recovery dir name */
620 struct xdr_netobj cr_princhash;
621 };
622
623 /*
624 * REPLAY_ISIZE is sized for an OPEN response with delegation:
625 * 4(status) + 8(stateid) + 20(changeinfo) + 4(rflags) +
626 * 8(verifier) + 4(deleg. type) + 8(deleg. stateid) +
627 * 4(deleg. recall flag) + 20(deleg. space limit) +
628 * ~32(deleg. ace) = 112 bytes
629 *
630 * Some responses can exceed this. A LOCK denial includes the conflicting
631 * lock owner, which can be up to 1024 bytes (NFS4_OPAQUE_LIMIT). When a
632 * response exceeds REPLAY_ISIZE, a buffer is dynamically allocated. If
633 * that allocation fails, only rp_status is saved. Enlarging this constant
634 * increases the size of every nfs4_stateowner.
635 */
636
637 #define NFSD4_REPLAY_ISIZE 112
638
639 /*
640 * Replay buffer, where the result of the last seqid-mutating operation
641 * is cached.
642 */
643 struct nfs4_replay {
644 __be32 rp_status;
645 unsigned int rp_buflen;
646 char *rp_buf; /* rp_ibuf or kmalloc'd */
647 struct knfsd_fh rp_openfh;
648 int rp_locked;
649 char rp_ibuf[NFSD4_REPLAY_ISIZE];
650 };
651
652 extern void nfs4_replay_free_cache(struct nfs4_replay *rp);
653
654 struct nfs4_stateowner;
655
656 struct nfs4_stateowner_operations {
657 void (*so_unhash)(struct nfs4_stateowner *);
658 void (*so_free)(struct nfs4_stateowner *);
659 };
660
661 /*
662 * A core object that represents either an open or lock owner. The object and
663 * lock owner objects have one of these embedded within them. Refcounts and
664 * other fields common to both owner types are contained within these
665 * structures.
666 */
667 struct nfs4_stateowner {
668 struct list_head so_strhash;
669 struct list_head so_stateids;
670 struct nfs4_client *so_client;
671 const struct nfs4_stateowner_operations *so_ops;
672 /* after increment in nfsd4_bump_seqid, represents the next
673 * sequence id expected from the client: */
674 atomic_t so_count;
675 u32 so_seqid;
676 struct xdr_netobj so_owner; /* open owner name */
677 struct nfs4_replay so_replay;
678 bool so_is_open_owner;
679 };
680
681 /*
682 * When a file is opened, the client provides an open state owner opaque string
683 * that indicates the "owner" of that open. These objects are refcounted.
684 * References to it are held by each open state associated with it. This object
685 * is a superset of the nfs4_stateowner struct.
686 */
687 struct nfs4_openowner {
688 struct nfs4_stateowner oo_owner; /* must be first field */
689 struct list_head oo_perclient;
690 /*
691 * We keep around openowners a little while after last close,
692 * which saves clients from having to confirm, and allows us to
693 * handle close replays if they come soon enough. The close_lru
694 * is a list of such openowners, to be reaped by the laundromat
695 * thread eventually if they remain unused:
696 */
697 struct list_head oo_close_lru;
698 struct nfs4_ol_stateid *oo_last_closed_stid;
699 time64_t oo_time; /* time of placement on so_close_lru */
700 #define NFS4_OO_CONFIRMED 1
701 unsigned char oo_flags;
702 };
703
704 /*
705 * Represents a generic "lockowner". Similar to an openowner. References to it
706 * are held by the lock stateids that are created on its behalf. This object is
707 * a superset of the nfs4_stateowner struct.
708 */
709 struct nfs4_lockowner {
710 struct nfs4_stateowner lo_owner; /* must be first element */
711 struct list_head lo_blocked; /* blocked file_locks */
712 };
713
openowner(struct nfs4_stateowner * so)714 static inline struct nfs4_openowner * openowner(struct nfs4_stateowner *so)
715 {
716 return container_of(so, struct nfs4_openowner, oo_owner);
717 }
718
lockowner(struct nfs4_stateowner * so)719 static inline struct nfs4_lockowner * lockowner(struct nfs4_stateowner *so)
720 {
721 return container_of(so, struct nfs4_lockowner, lo_owner);
722 }
723
724 /*
725 * Per-client state indicating no. of opens and outstanding delegations
726 * on a file from a particular client.'od' stands for 'open & delegation'
727 */
728 struct nfs4_clnt_odstate {
729 struct nfs4_client *co_client;
730 struct nfs4_file *co_file;
731 struct list_head co_perfile;
732 refcount_t co_odcount;
733 };
734
735 /*
736 * nfs4_file: a file opened by some number of (open) nfs4_stateowners.
737 *
738 * These objects are global. nfsd keeps one instance of a nfs4_file per
739 * filehandle (though it may keep multiple file descriptors for each). Each
740 * inode can have multiple filehandles associated with it, so there is
741 * (potentially) a many to one relationship between this struct and struct
742 * inode.
743 */
744 struct nfs4_file {
745 refcount_t fi_ref;
746 struct inode * fi_inode;
747 bool fi_aliased;
748 spinlock_t fi_lock;
749 struct rhlist_head fi_rlist;
750 struct list_head fi_stateids;
751 union {
752 struct list_head fi_delegations;
753 struct rcu_head fi_rcu;
754 };
755 struct list_head fi_clnt_odstate;
756 /* One each for O_RDONLY, O_WRONLY, O_RDWR: */
757 struct nfsd_file *fi_fds[3];
758 /*
759 * Each open or lock stateid contributes 0-4 to the counts
760 * below depending on which bits are set in st_access_bitmap:
761 * 1 to fi_access[O_RDONLY] if NFS4_SHARE_ACCES_READ is set
762 * + 1 to fi_access[O_WRONLY] if NFS4_SHARE_ACCESS_WRITE is set
763 * + 1 to both of the above if NFS4_SHARE_ACCESS_BOTH is set.
764 */
765 atomic_t fi_access[2];
766 u32 fi_share_deny;
767 struct nfsd_file __rcu *fi_deleg_file;
768 struct nfsd_file *fi_rdeleg_file;
769 int fi_delegees;
770 struct knfsd_fh fi_fhandle;
771 bool fi_had_conflict;
772 #ifdef CONFIG_NFSD_PNFS
773 struct list_head fi_lo_states;
774 atomic_t fi_lo_recalls;
775 #endif
776 };
777
778 /*
779 * A generic struct representing either a open or lock stateid. The nfs4_client
780 * holds a reference to each of these objects, and they in turn hold a
781 * reference to their respective stateowners. The client's reference is
782 * released in response to a close or unlock (depending on whether it's an open
783 * or lock stateid) or when the client is being destroyed.
784 *
785 * In the case of v4.0 open stateids, these objects are preserved for a little
786 * while after close in order to handle CLOSE replays. Those are eventually
787 * reclaimed via a LRU scheme by the laundromat.
788 *
789 * This object is a superset of the nfs4_stid. "ol" stands for "Open or Lock".
790 * Better suggestions welcome.
791 */
792 struct nfs4_ol_stateid {
793 struct nfs4_stid st_stid;
794 struct list_head st_perfile;
795 struct list_head st_perstateowner;
796 struct list_head st_locks;
797 struct nfs4_stateowner *st_stateowner;
798 struct nfs4_clnt_odstate *st_clnt_odstate;
799 /*
800 * These bitmasks use 3 separate bits for READ, ALLOW, and BOTH; see the
801 * comment above bmap_to_share_mode() for explanation:
802 */
803 unsigned char st_access_bmap;
804 unsigned char st_deny_bmap;
805 struct nfs4_ol_stateid *st_openstp;
806 struct mutex st_mutex;
807 };
808
openlockstateid(struct nfs4_stid * s)809 static inline struct nfs4_ol_stateid *openlockstateid(struct nfs4_stid *s)
810 {
811 return container_of(s, struct nfs4_ol_stateid, st_stid);
812 }
813
814 struct nfs4_layout_stateid {
815 struct nfs4_stid ls_stid;
816 struct list_head ls_perclnt;
817 struct list_head ls_perfile;
818 spinlock_t ls_lock;
819 struct list_head ls_layouts;
820 u32 ls_layout_type;
821 struct nfsd_file *ls_file;
822 struct nfsd4_callback ls_recall;
823 stateid_t ls_recall_sid;
824 bool ls_recalled;
825 struct mutex ls_mutex;
826
827 struct delayed_work ls_fence_work;
828 unsigned int ls_fence_delay;
829 bool ls_fenced;
830 bool ls_fence_inflight;
831 };
832
layoutstateid(struct nfs4_stid * s)833 static inline struct nfs4_layout_stateid *layoutstateid(struct nfs4_stid *s)
834 {
835 return container_of(s, struct nfs4_layout_stateid, ls_stid);
836 }
837
838 /* flags for preprocess_seqid_op() */
839 #define RD_STATE 0x00000010
840 #define WR_STATE 0x00000020
841
842 enum nfsd4_cb_op {
843 NFSPROC4_CLNT_CB_NULL = 0,
844 NFSPROC4_CLNT_CB_RECALL,
845 NFSPROC4_CLNT_CB_LAYOUT,
846 NFSPROC4_CLNT_CB_OFFLOAD,
847 NFSPROC4_CLNT_CB_SEQUENCE,
848 NFSPROC4_CLNT_CB_NOTIFY_LOCK,
849 NFSPROC4_CLNT_CB_RECALL_ANY,
850 NFSPROC4_CLNT_CB_GETATTR,
851 NFSPROC4_CLNT_CB_NOTIFY,
852 };
853
854 /* Returns true iff a is later than b: */
nfsd4_stateid_generation_after(stateid_t * a,stateid_t * b)855 static inline bool nfsd4_stateid_generation_after(stateid_t *a, stateid_t *b)
856 {
857 return (s32)(a->si_generation - b->si_generation) > 0;
858 }
859
860 /*
861 * When a client tries to get a lock on a file, we set one of these objects
862 * on the blocking lock. When the lock becomes free, we can then issue a
863 * CB_NOTIFY_LOCK to the server.
864 */
865 struct nfsd4_blocked_lock {
866 struct list_head nbl_list;
867 struct list_head nbl_lru;
868 time64_t nbl_time;
869 struct file_lock nbl_lock;
870 struct knfsd_fh nbl_fh;
871 struct nfsd4_callback nbl_cb;
872 struct kref nbl_kref;
873 };
874
875 struct nfsd4_compound_state;
876 struct nfsd_net;
877 struct nfsd4_copy;
878 struct nfsd4_async_copy;
879
880 extern __be32 nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
881 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
882 stateid_t *stateid, int flags, struct nfsd_file **filp,
883 struct nfs4_stid **cstid);
884 __be32 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
885 stateid_t *stateid, unsigned short typemask,
886 unsigned short statusmask,
887 struct nfs4_stid **s, struct nfsd_net *nn);
888 struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
889 void (*sc_free)(struct nfs4_stid *));
890 struct nfsd4_async_copy *nfs4_alloc_copy_stid(struct nfs4_client *clp);
891 struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
892 struct nfs4_stid *p_stid);
893 void nfs4_put_stid(struct nfs4_stid *s);
894 void nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid);
895 void nfs4_remove_reclaim_record(struct nfs4_client_reclaim *, struct nfsd_net *);
896 extern void nfs4_release_reclaim(struct nfsd_net *);
897 extern struct nfs4_client_reclaim *nfsd4_find_reclaim_client(struct xdr_netobj name,
898 struct nfsd_net *nn);
899 extern __be32 nfs4_check_open_reclaim(struct nfs4_client *);
900 extern void nfsd4_probe_callback(struct nfs4_client *clp);
901 extern void nfsd4_probe_callback_sync(struct nfs4_client *clp);
902 extern void nfsd4_change_callback(struct nfs4_client *clp, struct nfs4_cb_conn *);
903 extern void nfsd41_cb_referring_call(struct nfsd4_callback *cb,
904 struct nfs4_sessionid *sessionid,
905 u32 slotid, u32 seqno);
906 extern void nfsd41_cb_destroy_referring_call_list(struct nfsd4_callback *cb);
907 extern void nfsd4_init_cb(struct nfsd4_callback *cb, struct nfs4_client *clp,
908 const struct nfsd4_callback_ops *ops, enum nfsd4_cb_op op);
909 extern bool nfsd4_run_cb(struct nfsd4_callback *cb);
910
nfsd4_try_run_cb(struct nfsd4_callback * cb)911 static inline void nfsd4_try_run_cb(struct nfsd4_callback *cb)
912 {
913 if (!test_and_set_bit(NFSD4_CALLBACK_RUNNING, &cb->cb_flags))
914 WARN_ON_ONCE(!nfsd4_run_cb(cb));
915 }
916
917 extern void nfsd4_shutdown_callback(struct nfs4_client *);
918 extern void nfsd4_shutdown_copy(struct nfs4_client *clp);
919 void nfsd4_put_client(struct nfs4_client *clp);
920 void nfsd4_async_copy_reaper(struct nfsd_net *nn);
921 bool nfsd4_has_active_async_copies(struct nfs4_client *clp);
922 void nfsd_update_cmtime_attr(struct file *f, unsigned int flags);
923 extern struct nfs4_client_reclaim *nfs4_client_to_reclaim(struct xdr_netobj name,
924 struct xdr_netobj princhash, struct nfsd_net *nn);
925 extern bool nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn);
926 int nfsd_handle_dir_event(u32 mask, const struct inode *dir, const void *data,
927 int data_type, const struct qstr *name);
928
929 void put_nfs4_file(struct nfs4_file *fi);
930 extern void nfs4_put_cpntf_state(struct nfsd_net *nn,
931 struct nfs4_cpntf_state *cps);
932 extern __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
933 struct nfs4_client *clp,
934 struct nfs4_cpntf_state **cps);
get_nfs4_file(struct nfs4_file * fi)935 static inline void get_nfs4_file(struct nfs4_file *fi)
936 {
937 refcount_inc(&fi->fi_ref);
938 }
939 struct nfsd_file *find_any_file(struct nfs4_file *f);
940
941 #ifdef CONFIG_NFSD_V4
942 void nfsd4_revoke_states(struct nfsd_net *nn, struct super_block *sb);
943 void nfsd4_revoke_export_states(struct nfsd_net *nn, const struct path *path);
944 void nfsd4_cancel_copy_by_sb(struct net *net, struct super_block *sb);
945 int nfsd_net_cb_init(struct nfsd_net *nn);
946 void nfsd_net_cb_shutdown(struct nfsd_net *nn);
947 #else
nfsd4_revoke_states(struct nfsd_net * nn,struct super_block * sb)948 static inline void nfsd4_revoke_states(struct nfsd_net *nn, struct super_block *sb)
949 {
950 }
nfsd4_revoke_export_states(struct nfsd_net * nn,const struct path * path)951 static inline void nfsd4_revoke_export_states(struct nfsd_net *nn,
952 const struct path *path)
953 {
954 }
nfsd4_cancel_copy_by_sb(struct net * net,struct super_block * sb)955 static inline void nfsd4_cancel_copy_by_sb(struct net *net, struct super_block *sb)
956 {
957 }
nfsd_net_cb_init(struct nfsd_net * nn)958 static inline int nfsd_net_cb_init(struct nfsd_net *nn)
959 {
960 return 0;
961 }
nfsd_net_cb_shutdown(struct nfsd_net * nn)962 static inline void nfsd_net_cb_shutdown(struct nfsd_net *nn)
963 {
964 }
965 #endif
966
967 /* grace period management */
968 bool nfsd4_force_end_grace(struct nfsd_net *nn);
969
970 /* nfs4recover operations */
971 extern int nfsd4_client_tracking_init(struct net *net);
972 extern void nfsd4_client_tracking_exit(struct net *net);
973 extern void nfsd4_client_record_create(struct nfs4_client *clp);
974 extern void nfsd4_client_record_remove(struct nfs4_client *clp);
975 extern int nfsd4_client_record_check(struct nfs4_client *clp);
976 extern void nfsd4_record_grace_done(struct nfsd_net *nn);
977
try_to_expire_client(struct nfs4_client * clp)978 static inline bool try_to_expire_client(struct nfs4_client *clp)
979 {
980 cmpxchg(&clp->cl_state, NFSD4_COURTESY, NFSD4_EXPIRABLE);
981 return clp->cl_state == NFSD4_EXPIRABLE;
982 }
983
984 extern __be32 nfsd4_deleg_getattr_conflict(struct svc_rqst *rqstp,
985 struct dentry *dentry, struct nfs4_delegation **pdp);
986
987 struct nfsd4_get_dir_delegation;
988 struct nfs4_delegation *nfsd_get_dir_deleg(struct nfsd4_compound_state *cstate,
989 struct nfsd4_get_dir_delegation *gdd,
990 struct nfsd_file *nf);
991 #endif /* NFSD4_STATE_H */
992