xref: /openbmc/linux/fs/nfs/nfs4_fs.h (revision 9c1f8594)
1 /*
2  * linux/fs/nfs/nfs4_fs.h
3  *
4  * Copyright (C) 2005 Trond Myklebust
5  *
6  * NFSv4-specific filesystem definitions and declarations
7  */
8 
9 #ifndef __LINUX_FS_NFS_NFS4_FS_H
10 #define __LINUX_FS_NFS_NFS4_FS_H
11 
12 #ifdef CONFIG_NFS_V4
13 
14 struct idmap;
15 
16 /*
17  * In a seqid-mutating op, this macro controls which error return
18  * values trigger incrementation of the seqid.
19  *
20  * from rfc 3010:
21  * The client MUST monotonically increment the sequence number for the
22  * CLOSE, LOCK, LOCKU, OPEN, OPEN_CONFIRM, and OPEN_DOWNGRADE
23  * operations.  This is true even in the event that the previous
24  * operation that used the sequence number received an error.  The only
25  * exception to this rule is if the previous operation received one of
26  * the following errors: NFSERR_STALE_CLIENTID, NFSERR_STALE_STATEID,
27  * NFSERR_BAD_STATEID, NFSERR_BAD_SEQID, NFSERR_BADXDR,
28  * NFSERR_RESOURCE, NFSERR_NOFILEHANDLE.
29  *
30  */
31 #define seqid_mutating_err(err)       \
32 (((err) != NFSERR_STALE_CLIENTID) &&  \
33  ((err) != NFSERR_STALE_STATEID)  &&  \
34  ((err) != NFSERR_BAD_STATEID)    &&  \
35  ((err) != NFSERR_BAD_SEQID)      &&  \
36  ((err) != NFSERR_BAD_XDR)        &&  \
37  ((err) != NFSERR_RESOURCE)       &&  \
38  ((err) != NFSERR_NOFILEHANDLE))
39 
40 enum nfs4_client_state {
41 	NFS4CLNT_MANAGER_RUNNING  = 0,
42 	NFS4CLNT_CHECK_LEASE,
43 	NFS4CLNT_LEASE_EXPIRED,
44 	NFS4CLNT_RECLAIM_REBOOT,
45 	NFS4CLNT_RECLAIM_NOGRACE,
46 	NFS4CLNT_DELEGRETURN,
47 	NFS4CLNT_LAYOUTRECALL,
48 	NFS4CLNT_SESSION_RESET,
49 	NFS4CLNT_RECALL_SLOT,
50 	NFS4CLNT_LEASE_CONFIRM,
51 	NFS4CLNT_SERVER_SCOPE_MISMATCH,
52 };
53 
54 enum nfs4_session_state {
55 	NFS4_SESSION_INITING,
56 	NFS4_SESSION_DRAINING,
57 };
58 
59 #define NFS4_RENEW_TIMEOUT		0x01
60 #define NFS4_RENEW_DELEGATION_CB	0x02
61 
62 struct nfs4_minor_version_ops {
63 	u32	minor_version;
64 
65 	int	(*call_sync)(struct rpc_clnt *clnt,
66 			struct nfs_server *server,
67 			struct rpc_message *msg,
68 			struct nfs4_sequence_args *args,
69 			struct nfs4_sequence_res *res,
70 			int cache_reply);
71 	int	(*validate_stateid)(struct nfs_delegation *,
72 			const nfs4_stateid *);
73 	int	(*find_root_sec)(struct nfs_server *, struct nfs_fh *,
74 			struct nfs_fsinfo *);
75 	const struct nfs4_state_recovery_ops *reboot_recovery_ops;
76 	const struct nfs4_state_recovery_ops *nograce_recovery_ops;
77 	const struct nfs4_state_maintenance_ops *state_renewal_ops;
78 };
79 
80 /*
81  * struct rpc_sequence ensures that RPC calls are sent in the exact
82  * order that they appear on the list.
83  */
84 struct rpc_sequence {
85 	struct rpc_wait_queue	wait;	/* RPC call delay queue */
86 	spinlock_t lock;		/* Protects the list */
87 	struct list_head list;		/* Defines sequence of RPC calls */
88 };
89 
90 #define NFS_SEQID_CONFIRMED 1
91 struct nfs_seqid_counter {
92 	struct rpc_sequence *sequence;
93 	int flags;
94 	u32 counter;
95 };
96 
97 struct nfs_seqid {
98 	struct nfs_seqid_counter *sequence;
99 	struct list_head list;
100 };
101 
102 static inline void nfs_confirm_seqid(struct nfs_seqid_counter *seqid, int status)
103 {
104 	if (seqid_mutating_err(-status))
105 		seqid->flags |= NFS_SEQID_CONFIRMED;
106 }
107 
108 struct nfs_unique_id {
109 	struct rb_node rb_node;
110 	__u64 id;
111 };
112 
113 /*
114  * NFS4 state_owners and lock_owners are simply labels for ordered
115  * sequences of RPC calls. Their sole purpose is to provide once-only
116  * semantics by allowing the server to identify replayed requests.
117  */
118 struct nfs4_state_owner {
119 	struct nfs_unique_id so_owner_id;
120 	struct nfs_server    *so_server;
121 	struct rb_node	     so_server_node;
122 
123 	struct rpc_cred	     *so_cred;	 /* Associated cred */
124 
125 	spinlock_t	     so_lock;
126 	atomic_t	     so_count;
127 	unsigned long	     so_flags;
128 	struct list_head     so_states;
129 	struct nfs_seqid_counter so_seqid;
130 	struct rpc_sequence  so_sequence;
131 };
132 
133 enum {
134 	NFS_OWNER_RECLAIM_REBOOT,
135 	NFS_OWNER_RECLAIM_NOGRACE
136 };
137 
138 #define NFS_LOCK_NEW		0
139 #define NFS_LOCK_RECLAIM	1
140 #define NFS_LOCK_EXPIRED	2
141 
142 /*
143  * struct nfs4_state maintains the client-side state for a given
144  * (state_owner,inode) tuple (OPEN) or state_owner (LOCK).
145  *
146  * OPEN:
147  * In order to know when to OPEN_DOWNGRADE or CLOSE the state on the server,
148  * we need to know how many files are open for reading or writing on a
149  * given inode. This information too is stored here.
150  *
151  * LOCK: one nfs4_state (LOCK) to hold the lock stateid nfs4_state(OPEN)
152  */
153 
154 struct nfs4_lock_owner {
155 	unsigned int lo_type;
156 #define NFS4_ANY_LOCK_TYPE	(0U)
157 #define NFS4_FLOCK_LOCK_TYPE	(1U << 0)
158 #define NFS4_POSIX_LOCK_TYPE	(1U << 1)
159 	union {
160 		fl_owner_t posix_owner;
161 		pid_t flock_owner;
162 	} lo_u;
163 };
164 
165 struct nfs4_lock_state {
166 	struct list_head	ls_locks;	/* Other lock stateids */
167 	struct nfs4_state *	ls_state;	/* Pointer to open state */
168 #define NFS_LOCK_INITIALIZED 1
169 	int			ls_flags;
170 	struct nfs_seqid_counter	ls_seqid;
171 	struct rpc_sequence	ls_sequence;
172 	struct nfs_unique_id	ls_id;
173 	nfs4_stateid		ls_stateid;
174 	atomic_t		ls_count;
175 	struct nfs4_lock_owner	ls_owner;
176 };
177 
178 /* bits for nfs4_state->flags */
179 enum {
180 	LK_STATE_IN_USE,
181 	NFS_DELEGATED_STATE,		/* Current stateid is delegation */
182 	NFS_O_RDONLY_STATE,		/* OPEN stateid has read-only state */
183 	NFS_O_WRONLY_STATE,		/* OPEN stateid has write-only state */
184 	NFS_O_RDWR_STATE,		/* OPEN stateid has read/write state */
185 	NFS_STATE_RECLAIM_REBOOT,	/* OPEN stateid server rebooted */
186 	NFS_STATE_RECLAIM_NOGRACE,	/* OPEN stateid needs to recover state */
187 	NFS_STATE_POSIX_LOCKS,		/* Posix locks are supported */
188 };
189 
190 struct nfs4_state {
191 	struct list_head open_states;	/* List of states for the same state_owner */
192 	struct list_head inode_states;	/* List of states for the same inode */
193 	struct list_head lock_states;	/* List of subservient lock stateids */
194 
195 	struct nfs4_state_owner *owner;	/* Pointer to the open owner */
196 	struct inode *inode;		/* Pointer to the inode */
197 
198 	unsigned long flags;		/* Do we hold any locks? */
199 	spinlock_t state_lock;		/* Protects the lock_states list */
200 
201 	seqlock_t seqlock;		/* Protects the stateid/open_stateid */
202 	nfs4_stateid stateid;		/* Current stateid: may be delegation */
203 	nfs4_stateid open_stateid;	/* OPEN stateid */
204 
205 	/* The following 3 fields are protected by owner->so_lock */
206 	unsigned int n_rdonly;		/* Number of read-only references */
207 	unsigned int n_wronly;		/* Number of write-only references */
208 	unsigned int n_rdwr;		/* Number of read/write references */
209 	fmode_t state;			/* State on the server (R,W, or RW) */
210 	atomic_t count;
211 };
212 
213 
214 struct nfs4_exception {
215 	long timeout;
216 	int retry;
217 	struct nfs4_state *state;
218 };
219 
220 struct nfs4_state_recovery_ops {
221 	int owner_flag_bit;
222 	int state_flag_bit;
223 	int (*recover_open)(struct nfs4_state_owner *, struct nfs4_state *);
224 	int (*recover_lock)(struct nfs4_state *, struct file_lock *);
225 	int (*establish_clid)(struct nfs_client *, struct rpc_cred *);
226 	struct rpc_cred * (*get_clid_cred)(struct nfs_client *);
227 	int (*reclaim_complete)(struct nfs_client *);
228 };
229 
230 struct nfs4_state_maintenance_ops {
231 	int (*sched_state_renewal)(struct nfs_client *, struct rpc_cred *, unsigned);
232 	struct rpc_cred * (*get_state_renewal_cred_locked)(struct nfs_client *);
233 	int (*renew_lease)(struct nfs_client *, struct rpc_cred *);
234 };
235 
236 extern const struct dentry_operations nfs4_dentry_operations;
237 extern const struct inode_operations nfs4_dir_inode_operations;
238 
239 /* nfs4proc.c */
240 extern int nfs4_proc_setclientid(struct nfs_client *, u32, unsigned short, struct rpc_cred *, struct nfs4_setclientid_res *);
241 extern int nfs4_proc_setclientid_confirm(struct nfs_client *, struct nfs4_setclientid_res *arg, struct rpc_cred *);
242 extern int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred);
243 extern int nfs4_init_clientid(struct nfs_client *, struct rpc_cred *);
244 extern int nfs41_init_clientid(struct nfs_client *, struct rpc_cred *);
245 extern int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc);
246 extern int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle);
247 extern int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
248 		struct nfs4_fs_locations *fs_locations, struct page *page);
249 extern void nfs4_release_lockowner(const struct nfs4_lock_state *);
250 extern const struct xattr_handler *nfs4_xattr_handlers[];
251 
252 #if defined(CONFIG_NFS_V4_1)
253 static inline struct nfs4_session *nfs4_get_session(const struct nfs_server *server)
254 {
255 	return server->nfs_client->cl_session;
256 }
257 
258 extern int nfs4_setup_sequence(const struct nfs_server *server,
259 		struct nfs4_sequence_args *args, struct nfs4_sequence_res *res,
260 		int cache_reply, struct rpc_task *task);
261 extern int nfs41_setup_sequence(struct nfs4_session *session,
262 		struct nfs4_sequence_args *args, struct nfs4_sequence_res *res,
263 		int cache_reply, struct rpc_task *task);
264 extern void nfs4_destroy_session(struct nfs4_session *session);
265 extern struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp);
266 extern int nfs4_proc_create_session(struct nfs_client *);
267 extern int nfs4_proc_destroy_session(struct nfs4_session *);
268 extern int nfs4_init_session(struct nfs_server *server);
269 extern int nfs4_proc_get_lease_time(struct nfs_client *clp,
270 		struct nfs_fsinfo *fsinfo);
271 extern int nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data,
272 				  bool sync);
273 
274 static inline bool
275 is_ds_only_client(struct nfs_client *clp)
276 {
277 	return (clp->cl_exchange_flags & EXCHGID4_FLAG_MASK_PNFS) ==
278 		EXCHGID4_FLAG_USE_PNFS_DS;
279 }
280 
281 static inline bool
282 is_ds_client(struct nfs_client *clp)
283 {
284 	return clp->cl_exchange_flags & EXCHGID4_FLAG_USE_PNFS_DS;
285 }
286 #else /* CONFIG_NFS_v4_1 */
287 static inline struct nfs4_session *nfs4_get_session(const struct nfs_server *server)
288 {
289 	return NULL;
290 }
291 
292 static inline int nfs4_setup_sequence(const struct nfs_server *server,
293 		struct nfs4_sequence_args *args, struct nfs4_sequence_res *res,
294 		int cache_reply, struct rpc_task *task)
295 {
296 	return 0;
297 }
298 
299 static inline int nfs4_init_session(struct nfs_server *server)
300 {
301 	return 0;
302 }
303 
304 static inline bool
305 is_ds_only_client(struct nfs_client *clp)
306 {
307 	return false;
308 }
309 
310 static inline bool
311 is_ds_client(struct nfs_client *clp)
312 {
313 	return false;
314 }
315 #endif /* CONFIG_NFS_V4_1 */
316 
317 extern const struct nfs4_minor_version_ops *nfs_v4_minor_ops[];
318 
319 extern const u32 nfs4_fattr_bitmap[2];
320 extern const u32 nfs4_statfs_bitmap[2];
321 extern const u32 nfs4_pathconf_bitmap[2];
322 extern const u32 nfs4_fsinfo_bitmap[3];
323 extern const u32 nfs4_fs_locations_bitmap[2];
324 
325 /* nfs4renewd.c */
326 extern void nfs4_schedule_state_renewal(struct nfs_client *);
327 extern void nfs4_renewd_prepare_shutdown(struct nfs_server *);
328 extern void nfs4_kill_renewd(struct nfs_client *);
329 extern void nfs4_renew_state(struct work_struct *);
330 
331 /* nfs4state.c */
332 struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp);
333 struct rpc_cred *nfs4_get_renew_cred_locked(struct nfs_client *clp);
334 #if defined(CONFIG_NFS_V4_1)
335 struct rpc_cred *nfs4_get_machine_cred_locked(struct nfs_client *clp);
336 struct rpc_cred *nfs4_get_exchange_id_cred(struct nfs_client *clp);
337 extern void nfs4_schedule_session_recovery(struct nfs4_session *);
338 #else
339 static inline void nfs4_schedule_session_recovery(struct nfs4_session *session)
340 {
341 }
342 #endif /* CONFIG_NFS_V4_1 */
343 
344 extern struct nfs4_state_owner * nfs4_get_state_owner(struct nfs_server *, struct rpc_cred *);
345 extern void nfs4_put_state_owner(struct nfs4_state_owner *);
346 extern struct nfs4_state * nfs4_get_open_state(struct inode *, struct nfs4_state_owner *);
347 extern void nfs4_put_open_state(struct nfs4_state *);
348 extern void nfs4_close_state(struct nfs4_state *, fmode_t);
349 extern void nfs4_close_sync(struct nfs4_state *, fmode_t);
350 extern void nfs4_state_set_mode_locked(struct nfs4_state *, fmode_t);
351 extern void nfs4_schedule_lease_recovery(struct nfs_client *);
352 extern void nfs4_schedule_state_manager(struct nfs_client *);
353 extern void nfs4_schedule_path_down_recovery(struct nfs_client *clp);
354 extern void nfs4_schedule_stateid_recovery(const struct nfs_server *, struct nfs4_state *);
355 extern void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags);
356 extern void nfs41_handle_recall_slot(struct nfs_client *clp);
357 extern void nfs41_handle_server_scope(struct nfs_client *,
358 				      struct server_scope **);
359 extern void nfs4_put_lock_state(struct nfs4_lock_state *lsp);
360 extern int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl);
361 extern void nfs4_copy_stateid(nfs4_stateid *, struct nfs4_state *, fl_owner_t, pid_t);
362 
363 extern struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask);
364 extern int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task);
365 extern void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid);
366 extern void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid);
367 extern void nfs_release_seqid(struct nfs_seqid *seqid);
368 extern void nfs_free_seqid(struct nfs_seqid *seqid);
369 
370 extern const nfs4_stateid zero_stateid;
371 
372 /* nfs4xdr.c */
373 extern struct rpc_procinfo nfs4_procedures[];
374 
375 struct nfs4_mount_data;
376 
377 /* callback_xdr.c */
378 extern struct svc_version nfs4_callback_version1;
379 extern struct svc_version nfs4_callback_version4;
380 
381 #else
382 
383 #define nfs4_close_state(a, b) do { } while (0)
384 #define nfs4_close_sync(a, b) do { } while (0)
385 
386 #endif /* CONFIG_NFS_V4 */
387 #endif /* __LINUX_FS_NFS_NFS4_FS.H */
388