state.h 29 KB

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