state.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 <linux/idr.h>
  37. #include <linux/sunrpc/svc_xprt.h>
  38. #include "nfsfh.h"
  39. typedef struct {
  40. u32 cl_boot;
  41. u32 cl_id;
  42. } clientid_t;
  43. typedef struct {
  44. clientid_t so_clid;
  45. u32 so_id;
  46. } stateid_opaque_t;
  47. typedef struct {
  48. u32 si_generation;
  49. stateid_opaque_t si_opaque;
  50. } stateid_t;
  51. #define STATEID_FMT "(%08x/%08x/%08x/%08x)"
  52. #define STATEID_VAL(s) \
  53. (s)->si_opaque.so_clid.cl_boot, \
  54. (s)->si_opaque.so_clid.cl_id, \
  55. (s)->si_opaque.so_id, \
  56. (s)->si_generation
  57. struct nfsd4_callback {
  58. void *cb_op;
  59. struct nfs4_client *cb_clp;
  60. struct list_head cb_per_client;
  61. u32 cb_minorversion;
  62. struct rpc_message cb_msg;
  63. const struct rpc_call_ops *cb_ops;
  64. struct work_struct cb_work;
  65. bool cb_done;
  66. };
  67. struct nfs4_stid {
  68. #define NFS4_OPEN_STID 1
  69. #define NFS4_LOCK_STID 2
  70. #define NFS4_DELEG_STID 4
  71. /* For an open stateid kept around *only* to process close replays: */
  72. #define NFS4_CLOSED_STID 8
  73. /* For a deleg stateid kept around only to process free_stateid's: */
  74. #define NFS4_REVOKED_DELEG_STID 16
  75. unsigned char sc_type;
  76. stateid_t sc_stateid;
  77. struct nfs4_client *sc_client;
  78. };
  79. struct nfs4_delegation {
  80. struct nfs4_stid dl_stid; /* must be first field */
  81. struct list_head dl_perfile;
  82. struct list_head dl_perclnt;
  83. struct list_head dl_recall_lru; /* delegation recalled */
  84. atomic_t dl_count; /* ref count */
  85. struct nfs4_file *dl_file;
  86. u32 dl_type;
  87. time_t dl_time;
  88. /* For recall: */
  89. struct knfsd_fh dl_fh;
  90. int dl_retries;
  91. struct nfsd4_callback dl_recall;
  92. };
  93. /* client delegation callback info */
  94. struct nfs4_cb_conn {
  95. /* SETCLIENTID info */
  96. struct sockaddr_storage cb_addr;
  97. struct sockaddr_storage cb_saddr;
  98. size_t cb_addrlen;
  99. u32 cb_prog; /* used only in 4.0 case;
  100. per-session otherwise */
  101. u32 cb_ident; /* minorversion 0 only */
  102. struct svc_xprt *cb_xprt; /* minorversion 1 only */
  103. };
  104. static inline struct nfs4_delegation *delegstateid(struct nfs4_stid *s)
  105. {
  106. return container_of(s, struct nfs4_delegation, dl_stid);
  107. }
  108. /* Maximum number of slots per session. 160 is useful for long haul TCP */
  109. #define NFSD_MAX_SLOTS_PER_SESSION 160
  110. /* Maximum number of operations per session compound */
  111. #define NFSD_MAX_OPS_PER_COMPOUND 16
  112. /* Maximum session per slot cache size */
  113. #define NFSD_SLOT_CACHE_SIZE 2048
  114. /* Maximum number of NFSD_SLOT_CACHE_SIZE slots per session */
  115. #define NFSD_CACHE_SIZE_SLOTS_PER_SESSION 32
  116. #define NFSD_MAX_MEM_PER_SESSION \
  117. (NFSD_CACHE_SIZE_SLOTS_PER_SESSION * NFSD_SLOT_CACHE_SIZE)
  118. struct nfsd4_slot {
  119. u32 sl_seqid;
  120. __be32 sl_status;
  121. u32 sl_datalen;
  122. u16 sl_opcnt;
  123. #define NFSD4_SLOT_INUSE (1 << 0)
  124. #define NFSD4_SLOT_CACHETHIS (1 << 1)
  125. #define NFSD4_SLOT_INITIALIZED (1 << 2)
  126. u8 sl_flags;
  127. char sl_data[];
  128. };
  129. struct nfsd4_channel_attrs {
  130. u32 headerpadsz;
  131. u32 maxreq_sz;
  132. u32 maxresp_sz;
  133. u32 maxresp_cached;
  134. u32 maxops;
  135. u32 maxreqs;
  136. u32 nr_rdma_attrs;
  137. u32 rdma_attrs;
  138. };
  139. struct nfsd4_cb_sec {
  140. u32 flavor; /* (u32)(-1) used to mean "no valid flavor" */
  141. kuid_t uid;
  142. kgid_t gid;
  143. };
  144. struct nfsd4_create_session {
  145. clientid_t clientid;
  146. struct nfs4_sessionid sessionid;
  147. u32 seqid;
  148. u32 flags;
  149. struct nfsd4_channel_attrs fore_channel;
  150. struct nfsd4_channel_attrs back_channel;
  151. u32 callback_prog;
  152. struct nfsd4_cb_sec cb_sec;
  153. };
  154. struct nfsd4_backchannel_ctl {
  155. u32 bc_cb_program;
  156. struct nfsd4_cb_sec bc_cb_sec;
  157. };
  158. struct nfsd4_bind_conn_to_session {
  159. struct nfs4_sessionid sessionid;
  160. u32 dir;
  161. };
  162. /* The single slot clientid cache structure */
  163. struct nfsd4_clid_slot {
  164. u32 sl_seqid;
  165. __be32 sl_status;
  166. struct nfsd4_create_session sl_cr_ses;
  167. };
  168. struct nfsd4_conn {
  169. struct list_head cn_persession;
  170. struct svc_xprt *cn_xprt;
  171. struct svc_xpt_user cn_xpt_user;
  172. struct nfsd4_session *cn_session;
  173. /* CDFC4_FORE, CDFC4_BACK: */
  174. unsigned char cn_flags;
  175. };
  176. struct nfsd4_session {
  177. atomic_t se_ref;
  178. struct list_head se_hash; /* hash by sessionid */
  179. struct list_head se_perclnt;
  180. /* See SESSION4_PERSIST, etc. for standard flags; this is internal-only: */
  181. #define NFS4_SESSION_DEAD 0x010
  182. u32 se_flags;
  183. struct nfs4_client *se_client;
  184. struct nfs4_sessionid se_sessionid;
  185. struct nfsd4_channel_attrs se_fchannel;
  186. struct nfsd4_channel_attrs se_bchannel;
  187. struct nfsd4_cb_sec se_cb_sec;
  188. struct list_head se_conns;
  189. u32 se_cb_prog;
  190. u32 se_cb_seq_nr;
  191. struct nfsd4_slot *se_slots[]; /* forward channel slots */
  192. };
  193. /* formatted contents of nfs4_sessionid */
  194. struct nfsd4_sessionid {
  195. clientid_t clientid;
  196. u32 sequence;
  197. u32 reserved;
  198. };
  199. #define HEXDIR_LEN 33 /* hex version of 16 byte md5 of cl_name plus '\0' */
  200. /*
  201. * struct nfs4_client - one per client. Clientids live here.
  202. * o Each nfs4_client is hashed by clientid.
  203. *
  204. * o Each nfs4_clients is also hashed by name
  205. * (the opaque quantity initially sent by the client to identify itself).
  206. *
  207. * o cl_perclient list is used to ensure no dangling stateowner references
  208. * when we expire the nfs4_client
  209. */
  210. struct nfs4_client {
  211. struct list_head cl_idhash; /* hash by cl_clientid.id */
  212. struct rb_node cl_namenode; /* link into by-name trees */
  213. struct list_head cl_openowners;
  214. struct idr cl_stateids; /* stateid lookup */
  215. struct list_head cl_delegations;
  216. struct list_head cl_revoked; /* unacknowledged, revoked 4.1 state */
  217. struct list_head cl_lru; /* tail queue */
  218. struct xdr_netobj cl_name; /* id generated by client */
  219. nfs4_verifier cl_verifier; /* generated by client */
  220. time_t cl_time; /* time of last lease renewal */
  221. struct sockaddr_storage cl_addr; /* client ipaddress */
  222. bool cl_mach_cred; /* SP4_MACH_CRED in force */
  223. struct svc_cred cl_cred; /* setclientid principal */
  224. clientid_t cl_clientid; /* generated by server */
  225. nfs4_verifier cl_confirm; /* generated by server */
  226. u32 cl_minorversion;
  227. /* for v4.0 and v4.1 callbacks: */
  228. struct nfs4_cb_conn cl_cb_conn;
  229. #define NFSD4_CLIENT_CB_UPDATE (0)
  230. #define NFSD4_CLIENT_CB_KILL (1)
  231. #define NFSD4_CLIENT_STABLE (2) /* client on stable storage */
  232. #define NFSD4_CLIENT_RECLAIM_COMPLETE (3) /* reclaim_complete done */
  233. #define NFSD4_CLIENT_CONFIRMED (4) /* client is confirmed */
  234. #define NFSD4_CLIENT_CB_FLAG_MASK (1 << NFSD4_CLIENT_CB_UPDATE | \
  235. 1 << NFSD4_CLIENT_CB_KILL)
  236. unsigned long cl_flags;
  237. struct rpc_cred *cl_cb_cred;
  238. struct rpc_clnt *cl_cb_client;
  239. u32 cl_cb_ident;
  240. #define NFSD4_CB_UP 0
  241. #define NFSD4_CB_UNKNOWN 1
  242. #define NFSD4_CB_DOWN 2
  243. #define NFSD4_CB_FAULT 3
  244. int cl_cb_state;
  245. struct nfsd4_callback cl_cb_null;
  246. struct nfsd4_session *cl_cb_session;
  247. struct list_head cl_callbacks; /* list of in-progress callbacks */
  248. /* for all client information that callback code might need: */
  249. spinlock_t cl_lock;
  250. /* for nfs41 */
  251. struct list_head cl_sessions;
  252. struct nfsd4_clid_slot cl_cs_slot; /* create_session slot */
  253. u32 cl_exchange_flags;
  254. /* number of rpc's in progress over an associated session: */
  255. atomic_t cl_refcount;
  256. /* for nfs41 callbacks */
  257. /* We currently support a single back channel with a single slot */
  258. unsigned long cl_cb_slot_busy;
  259. struct rpc_wait_queue cl_cb_waitq; /* backchannel callers may */
  260. /* wait here for slots */
  261. struct net *net;
  262. };
  263. /* struct nfs4_client_reset
  264. * one per old client. Populates reset_str_hashtbl. Filled from conf_id_hashtbl
  265. * upon lease reset, or from upcall to state_daemon (to read in state
  266. * from non-volitile storage) upon reboot.
  267. */
  268. struct nfs4_client_reclaim {
  269. struct list_head cr_strhash; /* hash by cr_name */
  270. struct nfs4_client *cr_clp; /* pointer to associated clp */
  271. char cr_recdir[HEXDIR_LEN]; /* recover dir */
  272. };
  273. static inline void
  274. update_stateid(stateid_t *stateid)
  275. {
  276. stateid->si_generation++;
  277. /* Wraparound recommendation from 3530bis-13 9.1.3.2: */
  278. if (stateid->si_generation == 0)
  279. stateid->si_generation = 1;
  280. }
  281. /* A reasonable value for REPLAY_ISIZE was estimated as follows:
  282. * The OPEN response, typically the largest, requires
  283. * 4(status) + 8(stateid) + 20(changeinfo) + 4(rflags) + 8(verifier) +
  284. * 4(deleg. type) + 8(deleg. stateid) + 4(deleg. recall flag) +
  285. * 20(deleg. space limit) + ~32(deleg. ace) = 112 bytes
  286. */
  287. #define NFSD4_REPLAY_ISIZE 112
  288. /*
  289. * Replay buffer, where the result of the last seqid-mutating operation
  290. * is cached.
  291. */
  292. struct nfs4_replay {
  293. __be32 rp_status;
  294. unsigned int rp_buflen;
  295. char *rp_buf;
  296. struct knfsd_fh rp_openfh;
  297. char rp_ibuf[NFSD4_REPLAY_ISIZE];
  298. };
  299. struct nfs4_stateowner {
  300. struct list_head so_strhash; /* hash by op_name */
  301. struct list_head so_stateids;
  302. struct nfs4_client * so_client;
  303. /* after increment in ENCODE_SEQID_OP_TAIL, represents the next
  304. * sequence id expected from the client: */
  305. u32 so_seqid;
  306. struct xdr_netobj so_owner; /* open owner name */
  307. struct nfs4_replay so_replay;
  308. bool so_is_open_owner;
  309. };
  310. struct nfs4_openowner {
  311. struct nfs4_stateowner oo_owner; /* must be first field */
  312. struct list_head oo_perclient;
  313. /*
  314. * We keep around openowners a little while after last close,
  315. * which saves clients from having to confirm, and allows us to
  316. * handle close replays if they come soon enough. The close_lru
  317. * is a list of such openowners, to be reaped by the laundromat
  318. * thread eventually if they remain unused:
  319. */
  320. struct list_head oo_close_lru;
  321. struct nfs4_ol_stateid *oo_last_closed_stid;
  322. time_t oo_time; /* time of placement on so_close_lru */
  323. #define NFS4_OO_CONFIRMED 1
  324. #define NFS4_OO_NEW 4
  325. unsigned char oo_flags;
  326. };
  327. struct nfs4_lockowner {
  328. struct nfs4_stateowner lo_owner; /* must be first element */
  329. };
  330. static inline struct nfs4_openowner * openowner(struct nfs4_stateowner *so)
  331. {
  332. return container_of(so, struct nfs4_openowner, oo_owner);
  333. }
  334. static inline struct nfs4_lockowner * lockowner(struct nfs4_stateowner *so)
  335. {
  336. return container_of(so, struct nfs4_lockowner, lo_owner);
  337. }
  338. /* nfs4_file: a file opened by some number of (open) nfs4_stateowners. */
  339. struct nfs4_file {
  340. atomic_t fi_ref;
  341. spinlock_t fi_lock;
  342. struct hlist_node fi_hash; /* hash by "struct inode *" */
  343. struct list_head fi_stateids;
  344. struct list_head fi_delegations;
  345. /* One each for O_RDONLY, O_WRONLY, O_RDWR: */
  346. struct file * fi_fds[3];
  347. /*
  348. * Each open or lock stateid contributes 0-4 to the counts
  349. * below depending on which bits are set in st_access_bitmap:
  350. * 1 to fi_access[O_RDONLY] if NFS4_SHARE_ACCES_READ is set
  351. * + 1 to fi_access[O_WRONLY] if NFS4_SHARE_ACCESS_WRITE is set
  352. * + 1 to both of the above if NFS4_SHARE_ACCESS_BOTH is set.
  353. */
  354. atomic_t fi_access[2];
  355. struct file *fi_deleg_file;
  356. struct file_lock *fi_lease;
  357. atomic_t fi_delegees;
  358. struct inode *fi_inode;
  359. bool fi_had_conflict;
  360. };
  361. /* "ol" stands for "Open or Lock". Better suggestions welcome. */
  362. struct nfs4_ol_stateid {
  363. struct nfs4_stid st_stid; /* must be first field */
  364. struct list_head st_perfile;
  365. struct list_head st_perstateowner;
  366. struct list_head st_locks;
  367. struct nfs4_stateowner * st_stateowner;
  368. struct nfs4_file * st_file;
  369. unsigned long st_access_bmap;
  370. unsigned long st_deny_bmap;
  371. struct nfs4_ol_stateid * st_openstp;
  372. };
  373. static inline struct nfs4_ol_stateid *openlockstateid(struct nfs4_stid *s)
  374. {
  375. return container_of(s, struct nfs4_ol_stateid, st_stid);
  376. }
  377. /* flags for preprocess_seqid_op() */
  378. #define RD_STATE 0x00000010
  379. #define WR_STATE 0x00000020
  380. struct nfsd4_compound_state;
  381. struct nfsd_net;
  382. extern __be32 nfs4_preprocess_stateid_op(struct net *net,
  383. struct nfsd4_compound_state *cstate,
  384. stateid_t *stateid, int flags, struct file **filp);
  385. extern void nfs4_lock_state(void);
  386. extern void nfs4_unlock_state(void);
  387. void nfs4_remove_reclaim_record(struct nfs4_client_reclaim *, struct nfsd_net *);
  388. extern void nfs4_release_reclaim(struct nfsd_net *);
  389. extern struct nfs4_client_reclaim *nfsd4_find_reclaim_client(const char *recdir,
  390. struct nfsd_net *nn);
  391. extern __be32 nfs4_check_open_reclaim(clientid_t *clid,
  392. struct nfsd4_compound_state *cstate, struct nfsd_net *nn);
  393. extern int set_callback_cred(void);
  394. extern void nfsd4_init_callback(struct nfsd4_callback *);
  395. extern void nfsd4_probe_callback(struct nfs4_client *clp);
  396. extern void nfsd4_probe_callback_sync(struct nfs4_client *clp);
  397. extern void nfsd4_change_callback(struct nfs4_client *clp, struct nfs4_cb_conn *);
  398. extern void nfsd4_cb_recall(struct nfs4_delegation *dp);
  399. extern int nfsd4_create_callback_queue(void);
  400. extern void nfsd4_destroy_callback_queue(void);
  401. extern void nfsd4_shutdown_callback(struct nfs4_client *);
  402. extern void nfs4_put_delegation(struct nfs4_delegation *dp);
  403. extern struct nfs4_client_reclaim *nfs4_client_to_reclaim(const char *name,
  404. struct nfsd_net *nn);
  405. extern bool nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn);
  406. /* nfs4recover operations */
  407. extern int nfsd4_client_tracking_init(struct net *net);
  408. extern void nfsd4_client_tracking_exit(struct net *net);
  409. extern void nfsd4_client_record_create(struct nfs4_client *clp);
  410. extern void nfsd4_client_record_remove(struct nfs4_client *clp);
  411. extern int nfsd4_client_record_check(struct nfs4_client *clp);
  412. extern void nfsd4_record_grace_done(struct nfsd_net *nn, time_t boot_time);
  413. /* nfs fault injection functions */
  414. #ifdef CONFIG_NFSD_FAULT_INJECTION
  415. int nfsd_fault_inject_init(void);
  416. void nfsd_fault_inject_cleanup(void);
  417. u64 nfsd_for_n_state(u64, u64 (*)(struct nfs4_client *, u64));
  418. struct nfs4_client *nfsd_find_client(struct sockaddr_storage *, size_t);
  419. u64 nfsd_forget_client(struct nfs4_client *, u64);
  420. u64 nfsd_forget_client_locks(struct nfs4_client*, u64);
  421. u64 nfsd_forget_client_openowners(struct nfs4_client *, u64);
  422. u64 nfsd_forget_client_delegations(struct nfs4_client *, u64);
  423. u64 nfsd_recall_client_delegations(struct nfs4_client *, u64);
  424. u64 nfsd_print_client(struct nfs4_client *, u64);
  425. u64 nfsd_print_client_locks(struct nfs4_client *, u64);
  426. u64 nfsd_print_client_openowners(struct nfs4_client *, u64);
  427. u64 nfsd_print_client_delegations(struct nfs4_client *, u64);
  428. #else /* CONFIG_NFSD_FAULT_INJECTION */
  429. static inline int nfsd_fault_inject_init(void) { return 0; }
  430. static inline void nfsd_fault_inject_cleanup(void) {}
  431. #endif /* CONFIG_NFSD_FAULT_INJECTION */
  432. #endif /* NFSD4_STATE_H */