xref: /openbmc/linux/fs/afs/internal.h (revision 78e3dbc1)
1 /* internal AFS stuff
2  *
3  * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11 
12 #include <linux/compiler.h>
13 #include <linux/kernel.h>
14 #include <linux/ktime.h>
15 #include <linux/fs.h>
16 #include <linux/pagemap.h>
17 #include <linux/rxrpc.h>
18 #include <linux/key.h>
19 #include <linux/workqueue.h>
20 #include <linux/sched.h>
21 #include <linux/fscache.h>
22 #include <linux/backing-dev.h>
23 #include <linux/uuid.h>
24 #include <linux/mm_types.h>
25 #include <linux/dns_resolver.h>
26 #include <net/net_namespace.h>
27 #include <net/netns/generic.h>
28 #include <net/sock.h>
29 #include <net/af_rxrpc.h>
30 
31 #include "afs.h"
32 #include "afs_vl.h"
33 
34 #define AFS_CELL_MAX_ADDRS 15
35 
36 struct pagevec;
37 struct afs_call;
38 
39 struct afs_mount_params {
40 	bool			rwpath;		/* T if the parent should be considered R/W */
41 	bool			force;		/* T to force cell type */
42 	bool			autocell;	/* T if set auto mount operation */
43 	bool			dyn_root;	/* T if dynamic root */
44 	afs_voltype_t		type;		/* type of volume requested */
45 	int			volnamesz;	/* size of volume name */
46 	const char		*volname;	/* name of volume to mount */
47 	struct net		*net_ns;	/* Network namespace in effect */
48 	struct afs_net		*net;		/* the AFS net namespace stuff */
49 	struct afs_cell		*cell;		/* cell in which to find volume */
50 	struct afs_volume	*volume;	/* volume record */
51 	struct key		*key;		/* key to use for secure mounting */
52 };
53 
54 struct afs_iget_data {
55 	struct afs_fid		fid;
56 	struct afs_volume	*volume;	/* volume on which resides */
57 };
58 
59 enum afs_call_state {
60 	AFS_CALL_CL_REQUESTING,		/* Client: Request is being sent */
61 	AFS_CALL_CL_AWAIT_REPLY,	/* Client: Awaiting reply */
62 	AFS_CALL_CL_PROC_REPLY,		/* Client: rxrpc call complete; processing reply */
63 	AFS_CALL_SV_AWAIT_OP_ID,	/* Server: Awaiting op ID */
64 	AFS_CALL_SV_AWAIT_REQUEST,	/* Server: Awaiting request data */
65 	AFS_CALL_SV_REPLYING,		/* Server: Replying */
66 	AFS_CALL_SV_AWAIT_ACK,		/* Server: Awaiting final ACK */
67 	AFS_CALL_COMPLETE,		/* Completed or failed */
68 };
69 
70 /*
71  * List of server addresses.
72  */
73 struct afs_addr_list {
74 	struct rcu_head		rcu;		/* Must be first */
75 	refcount_t		usage;
76 	u32			version;	/* Version */
77 	unsigned char		max_addrs;
78 	unsigned char		nr_addrs;
79 	unsigned char		preferred;	/* Preferred address */
80 	unsigned char		nr_ipv4;	/* Number of IPv4 addresses */
81 	enum dns_record_source	source:8;
82 	enum dns_lookup_status	status:8;
83 	unsigned long		probed;		/* Mask of servers that have been probed */
84 	unsigned long		failed;		/* Mask of addrs that failed locally/ICMP */
85 	unsigned long		responded;	/* Mask of addrs that responded */
86 	struct sockaddr_rxrpc	addrs[];
87 #define AFS_MAX_ADDRESSES ((unsigned int)(sizeof(unsigned long) * 8))
88 };
89 
90 /*
91  * a record of an in-progress RxRPC call
92  */
93 struct afs_call {
94 	const struct afs_call_type *type;	/* type of call */
95 	struct afs_addr_list	*alist;		/* Address is alist[addr_ix] */
96 	wait_queue_head_t	waitq;		/* processes awaiting completion */
97 	struct work_struct	async_work;	/* async I/O processor */
98 	struct work_struct	work;		/* actual work processor */
99 	struct rxrpc_call	*rxcall;	/* RxRPC call handle */
100 	struct key		*key;		/* security for this call */
101 	struct afs_net		*net;		/* The network namespace */
102 	struct afs_server	*cm_server;	/* Server affected by incoming CM call */
103 	struct afs_cb_interest	*cbi;		/* Callback interest for server used */
104 	void			*request;	/* request data (first part) */
105 	struct address_space	*mapping;	/* Pages being written from */
106 	struct iov_iter		iter;		/* Buffer iterator */
107 	struct iov_iter		*_iter;		/* Iterator currently in use */
108 	union {	/* Convenience for ->iter */
109 		struct kvec	kvec[1];
110 		struct bio_vec	bvec[1];
111 	};
112 	void			*buffer;	/* reply receive buffer */
113 	void			*reply[4];	/* Where to put the reply */
114 	pgoff_t			first;		/* first page in mapping to deal with */
115 	pgoff_t			last;		/* last page in mapping to deal with */
116 	atomic_t		usage;
117 	enum afs_call_state	state;
118 	spinlock_t		state_lock;
119 	int			error;		/* error code */
120 	u32			abort_code;	/* Remote abort ID or 0 */
121 	u32			epoch;
122 	unsigned		request_size;	/* size of request data */
123 	unsigned		reply_max;	/* maximum size of reply */
124 	unsigned		first_offset;	/* offset into mapping[first] */
125 	unsigned int		cb_break;	/* cb_break + cb_s_break before the call */
126 	union {
127 		unsigned	last_to;	/* amount of mapping[last] */
128 		unsigned	count2;		/* count used in unmarshalling */
129 	};
130 	unsigned char		unmarshall;	/* unmarshalling phase */
131 	unsigned char		addr_ix;	/* Address in ->alist */
132 	bool			incoming;	/* T if incoming call */
133 	bool			send_pages;	/* T if data from mapping should be sent */
134 	bool			need_attention;	/* T if RxRPC poked us */
135 	bool			async;		/* T if asynchronous */
136 	bool			ret_reply0;	/* T if should return reply[0] on success */
137 	bool			upgrade;	/* T to request service upgrade */
138 	bool			want_reply_time; /* T if want reply_time */
139 	u16			service_id;	/* Actual service ID (after upgrade) */
140 	unsigned int		debug_id;	/* Trace ID */
141 	u32			operation_ID;	/* operation ID for an incoming call */
142 	u32			count;		/* count for use in unmarshalling */
143 	union {					/* place to extract temporary data */
144 		struct {
145 			__be32	tmp_u;
146 			__be32	tmp;
147 		} __attribute__((packed));
148 		__be64		tmp64;
149 	};
150 	afs_dataversion_t	expected_version; /* Updated version expected from store */
151 	afs_dataversion_t	expected_version_2; /* 2nd updated version expected from store */
152 	ktime_t			reply_time;	/* Time of first reply packet */
153 };
154 
155 struct afs_call_type {
156 	const char *name;
157 	unsigned int op; /* Really enum afs_fs_operation */
158 
159 	/* deliver request or reply data to an call
160 	 * - returning an error will cause the call to be aborted
161 	 */
162 	int (*deliver)(struct afs_call *call);
163 
164 	/* clean up a call */
165 	void (*destructor)(struct afs_call *call);
166 
167 	/* Work function */
168 	void (*work)(struct work_struct *work);
169 
170 	/* Call done function (gets called immediately on success or failure) */
171 	void (*done)(struct afs_call *call);
172 };
173 
174 /*
175  * Key available for writeback on a file.
176  */
177 struct afs_wb_key {
178 	refcount_t		usage;
179 	struct key		*key;
180 	struct list_head	vnode_link;	/* Link in vnode->wb_keys */
181 };
182 
183 /*
184  * AFS open file information record.  Pointed to by file->private_data.
185  */
186 struct afs_file {
187 	struct key		*key;		/* The key this file was opened with */
188 	struct afs_wb_key	*wb;		/* Writeback key record for this file */
189 };
190 
191 static inline struct key *afs_file_key(struct file *file)
192 {
193 	struct afs_file *af = file->private_data;
194 
195 	return af->key;
196 }
197 
198 /*
199  * Record of an outstanding read operation on a vnode.
200  */
201 struct afs_read {
202 	loff_t			pos;		/* Where to start reading */
203 	loff_t			len;		/* How much we're asking for */
204 	loff_t			actual_len;	/* How much we're actually getting */
205 	loff_t			remain;		/* Amount remaining */
206 	loff_t			file_size;	/* File size returned by server */
207 	afs_dataversion_t	data_version;	/* Version number returned by server */
208 	refcount_t		usage;
209 	unsigned int		index;		/* Which page we're reading into */
210 	unsigned int		nr_pages;
211 	unsigned int		offset;		/* offset into current page */
212 	void (*page_done)(struct afs_call *, struct afs_read *);
213 	struct page		**pages;
214 	struct page		*array[];
215 };
216 
217 /*
218  * AFS superblock private data
219  * - there's one superblock per volume
220  */
221 struct afs_super_info {
222 	struct net		*net_ns;	/* Network namespace */
223 	struct afs_cell		*cell;		/* The cell in which the volume resides */
224 	struct afs_volume	*volume;	/* volume record */
225 	bool			dyn_root;	/* True if dynamic root */
226 };
227 
228 static inline struct afs_super_info *AFS_FS_S(struct super_block *sb)
229 {
230 	return sb->s_fs_info;
231 }
232 
233 extern struct file_system_type afs_fs_type;
234 
235 /*
236  * Set of substitutes for @sys.
237  */
238 struct afs_sysnames {
239 #define AFS_NR_SYSNAME 16
240 	char			*subs[AFS_NR_SYSNAME];
241 	refcount_t		usage;
242 	unsigned short		nr;
243 	char			blank[1];
244 };
245 
246 /*
247  * AFS network namespace record.
248  */
249 struct afs_net {
250 	struct net		*net;		/* Backpointer to the owning net namespace */
251 	struct afs_uuid		uuid;
252 	bool			live;		/* F if this namespace is being removed */
253 
254 	/* AF_RXRPC I/O stuff */
255 	struct socket		*socket;
256 	struct afs_call		*spare_incoming_call;
257 	struct work_struct	charge_preallocation_work;
258 	struct mutex		socket_mutex;
259 	atomic_t		nr_outstanding_calls;
260 	atomic_t		nr_superblocks;
261 
262 	/* Cell database */
263 	struct rb_root		cells;
264 	struct afs_cell __rcu	*ws_cell;
265 	struct work_struct	cells_manager;
266 	struct timer_list	cells_timer;
267 	atomic_t		cells_outstanding;
268 	seqlock_t		cells_lock;
269 
270 	struct mutex		proc_cells_lock;
271 	struct hlist_head	proc_cells;
272 
273 	/* Known servers.  Theoretically each fileserver can only be in one
274 	 * cell, but in practice, people create aliases and subsets and there's
275 	 * no easy way to distinguish them.
276 	 */
277 	seqlock_t		fs_lock;	/* For fs_servers */
278 	struct rb_root		fs_servers;	/* afs_server (by server UUID or address) */
279 	struct list_head	fs_updates;	/* afs_server (by update_at) */
280 	struct hlist_head	fs_proc;	/* procfs servers list */
281 
282 	struct hlist_head	fs_addresses4;	/* afs_server (by lowest IPv4 addr) */
283 	struct hlist_head	fs_addresses6;	/* afs_server (by lowest IPv6 addr) */
284 	seqlock_t		fs_addr_lock;	/* For fs_addresses[46] */
285 
286 	struct work_struct	fs_manager;
287 	struct timer_list	fs_timer;
288 	atomic_t		servers_outstanding;
289 
290 	/* File locking renewal management */
291 	struct mutex		lock_manager_mutex;
292 
293 	/* Misc */
294 	struct super_block	*dynroot_sb;	/* Dynamic root mount superblock */
295 	struct proc_dir_entry	*proc_afs;	/* /proc/net/afs directory */
296 	struct afs_sysnames	*sysnames;
297 	rwlock_t		sysnames_lock;
298 
299 	/* Statistics counters */
300 	atomic_t		n_lookup;	/* Number of lookups done */
301 	atomic_t		n_reval;	/* Number of dentries needing revalidation */
302 	atomic_t		n_inval;	/* Number of invalidations by the server */
303 	atomic_t		n_relpg;	/* Number of invalidations by releasepage */
304 	atomic_t		n_read_dir;	/* Number of directory pages read */
305 	atomic_t		n_dir_cr;	/* Number of directory entry creation edits */
306 	atomic_t		n_dir_rm;	/* Number of directory entry removal edits */
307 	atomic_t		n_stores;	/* Number of store ops */
308 	atomic_long_t		n_store_bytes;	/* Number of bytes stored */
309 	atomic_long_t		n_fetch_bytes;	/* Number of bytes fetched */
310 	atomic_t		n_fetches;	/* Number of data fetch ops */
311 };
312 
313 extern const char afs_init_sysname[];
314 
315 enum afs_cell_state {
316 	AFS_CELL_UNSET,
317 	AFS_CELL_ACTIVATING,
318 	AFS_CELL_ACTIVE,
319 	AFS_CELL_DEACTIVATING,
320 	AFS_CELL_INACTIVE,
321 	AFS_CELL_FAILED,
322 };
323 
324 /*
325  * AFS cell record.
326  *
327  * This is a tricky concept to get right as it is possible to create aliases
328  * simply by pointing AFSDB/SRV records for two names at the same set of VL
329  * servers; it is also possible to do things like setting up two sets of VL
330  * servers, one of which provides a superset of the volumes provided by the
331  * other (for internal/external division, for example).
332  *
333  * Cells only exist in the sense that (a) a cell's name maps to a set of VL
334  * servers and (b) a cell's name is used by the client to select the key to use
335  * for authentication and encryption.  The cell name is not typically used in
336  * the protocol.
337  *
338  * There is no easy way to determine if two cells are aliases or one is a
339  * subset of another.
340  */
341 struct afs_cell {
342 	union {
343 		struct rcu_head	rcu;
344 		struct rb_node	net_node;	/* Node in net->cells */
345 	};
346 	struct afs_net		*net;
347 	struct key		*anonymous_key;	/* anonymous user key for this cell */
348 	struct work_struct	manager;	/* Manager for init/deinit/dns */
349 	struct hlist_node	proc_link;	/* /proc cell list link */
350 #ifdef CONFIG_AFS_FSCACHE
351 	struct fscache_cookie	*cache;		/* caching cookie */
352 #endif
353 	time64_t		dns_expiry;	/* Time AFSDB/SRV record expires */
354 	time64_t		last_inactive;	/* Time of last drop of usage count */
355 	atomic_t		usage;
356 	unsigned long		flags;
357 #define AFS_CELL_FL_NOT_READY	0		/* The cell record is not ready for use */
358 #define AFS_CELL_FL_NO_GC	1		/* The cell was added manually, don't auto-gc */
359 #define AFS_CELL_FL_NOT_FOUND	2		/* Permanent DNS error */
360 #define AFS_CELL_FL_DNS_FAIL	3		/* Failed to access DNS */
361 #define AFS_CELL_FL_NO_LOOKUP_YET 4		/* Not completed first DNS lookup yet */
362 	enum afs_cell_state	state;
363 	short			error;
364 
365 	/* Active fileserver interaction state. */
366 	struct list_head	proc_volumes;	/* procfs volume list */
367 	rwlock_t		proc_lock;
368 
369 	/* VL server list. */
370 	rwlock_t		vl_servers_lock; /* Lock on vl_servers */
371 	struct afs_vlserver_list __rcu *vl_servers;
372 
373 	u8			name_len;	/* Length of name */
374 	char			name[64 + 1];	/* Cell name, case-flattened and NUL-padded */
375 };
376 
377 /*
378  * Volume Location server record.
379  */
380 struct afs_vlserver {
381 	struct rcu_head		rcu;
382 	struct afs_addr_list	__rcu *addresses; /* List of addresses for this VL server */
383 	unsigned long		flags;
384 #define AFS_VLSERVER_FL_PROBED	0		/* The VL server has been probed */
385 #define AFS_VLSERVER_FL_PROBING	1		/* VL server is being probed */
386 #define AFS_VLSERVER_FL_IS_YFS	2		/* Server is YFS not AFS */
387 	rwlock_t		lock;		/* Lock on addresses */
388 	atomic_t		usage;
389 
390 	/* Probe state */
391 	wait_queue_head_t	probe_wq;
392 	atomic_t		probe_outstanding;
393 	spinlock_t		probe_lock;
394 	struct {
395 		unsigned int	rtt;		/* RTT as ktime/64 */
396 		u32		abort_code;
397 		short		error;
398 		bool		have_result;
399 		bool		responded:1;
400 		bool		is_yfs:1;
401 		bool		not_yfs:1;
402 		bool		local_failure:1;
403 	} probe;
404 
405 	u16			port;
406 	u16			name_len;	/* Length of name */
407 	char			name[];		/* Server name, case-flattened */
408 };
409 
410 /*
411  * Weighted list of Volume Location servers.
412  */
413 struct afs_vlserver_entry {
414 	u16			priority;	/* Preference (as SRV) */
415 	u16			weight;		/* Weight (as SRV) */
416 	enum dns_record_source	source:8;
417 	enum dns_lookup_status	status:8;
418 	struct afs_vlserver	*server;
419 };
420 
421 struct afs_vlserver_list {
422 	struct rcu_head		rcu;
423 	atomic_t		usage;
424 	u8			nr_servers;
425 	u8			index;		/* Server currently in use */
426 	u8			preferred;	/* Preferred server */
427 	enum dns_record_source	source:8;
428 	enum dns_lookup_status	status:8;
429 	rwlock_t		lock;
430 	struct afs_vlserver_entry servers[];
431 };
432 
433 /*
434  * Cached VLDB entry.
435  *
436  * This is pointed to by cell->vldb_entries, indexed by name.
437  */
438 struct afs_vldb_entry {
439 	afs_volid_t		vid[3];		/* Volume IDs for R/W, R/O and Bak volumes */
440 
441 	unsigned long		flags;
442 #define AFS_VLDB_HAS_RW		0		/* - R/W volume exists */
443 #define AFS_VLDB_HAS_RO		1		/* - R/O volume exists */
444 #define AFS_VLDB_HAS_BAK	2		/* - Backup volume exists */
445 #define AFS_VLDB_QUERY_VALID	3		/* - Record is valid */
446 #define AFS_VLDB_QUERY_ERROR	4		/* - VL server returned error */
447 
448 	uuid_t			fs_server[AFS_NMAXNSERVERS];
449 	u8			fs_mask[AFS_NMAXNSERVERS];
450 #define AFS_VOL_VTM_RW	0x01 /* R/W version of the volume is available (on this server) */
451 #define AFS_VOL_VTM_RO	0x02 /* R/O version of the volume is available (on this server) */
452 #define AFS_VOL_VTM_BAK	0x04 /* backup version of the volume is available (on this server) */
453 	short			error;
454 	u8			nr_servers;	/* Number of server records */
455 	u8			name_len;
456 	u8			name[AFS_MAXVOLNAME + 1]; /* NUL-padded volume name */
457 };
458 
459 /*
460  * Record of fileserver with which we're actively communicating.
461  */
462 struct afs_server {
463 	struct rcu_head		rcu;
464 	union {
465 		uuid_t		uuid;		/* Server ID */
466 		struct afs_uuid	_uuid;
467 	};
468 
469 	struct afs_addr_list	__rcu *addresses;
470 	struct rb_node		uuid_rb;	/* Link in net->servers */
471 	struct hlist_node	addr4_link;	/* Link in net->fs_addresses4 */
472 	struct hlist_node	addr6_link;	/* Link in net->fs_addresses6 */
473 	struct hlist_node	proc_link;	/* Link in net->fs_proc */
474 	struct afs_server	*gc_next;	/* Next server in manager's list */
475 	time64_t		put_time;	/* Time at which last put */
476 	time64_t		update_at;	/* Time at which to next update the record */
477 	unsigned long		flags;
478 #define AFS_SERVER_FL_NEW	0		/* New server, don't inc cb_s_break */
479 #define AFS_SERVER_FL_NOT_READY	1		/* The record is not ready for use */
480 #define AFS_SERVER_FL_NOT_FOUND	2		/* VL server says no such server */
481 #define AFS_SERVER_FL_VL_FAIL	3		/* Failed to access VL server */
482 #define AFS_SERVER_FL_UPDATING	4
483 #define AFS_SERVER_FL_PROBED	5		/* The fileserver has been probed */
484 #define AFS_SERVER_FL_PROBING	6		/* Fileserver is being probed */
485 #define AFS_SERVER_FL_NO_IBULK	7		/* Fileserver doesn't support FS.InlineBulkStatus */
486 #define AFS_SERVER_FL_MAY_HAVE_CB 8		/* May have callbacks on this fileserver */
487 #define AFS_SERVER_FL_IS_YFS	9		/* Server is YFS not AFS */
488 #define AFS_SERVER_FL_NO_RM2	10		/* Fileserver doesn't support YFS.RemoveFile2 */
489 #define AFS_SERVER_FL_HAVE_EPOCH 11		/* ->epoch is valid */
490 	atomic_t		usage;
491 	u32			addr_version;	/* Address list version */
492 	u32			cm_epoch;	/* Server RxRPC epoch */
493 
494 	/* file service access */
495 	rwlock_t		fs_lock;	/* access lock */
496 
497 	/* callback promise management */
498 	struct hlist_head	cb_volumes;	/* List of volume interests on this server */
499 	unsigned		cb_s_break;	/* Break-everything counter. */
500 	rwlock_t		cb_break_lock;	/* Volume finding lock */
501 
502 	/* Probe state */
503 	wait_queue_head_t	probe_wq;
504 	atomic_t		probe_outstanding;
505 	spinlock_t		probe_lock;
506 	struct {
507 		unsigned int	rtt;		/* RTT as ktime/64 */
508 		u32		abort_code;
509 		u32		cm_epoch;
510 		short		error;
511 		bool		have_result;
512 		bool		responded:1;
513 		bool		is_yfs:1;
514 		bool		not_yfs:1;
515 		bool		local_failure:1;
516 		bool		no_epoch:1;
517 		bool		cm_probed:1;
518 		bool		said_rebooted:1;
519 		bool		said_inconsistent:1;
520 	} probe;
521 };
522 
523 /*
524  * Volume collation in the server's callback interest list.
525  */
526 struct afs_vol_interest {
527 	struct hlist_node	srv_link;	/* Link in server->cb_volumes */
528 	struct hlist_head	cb_interests;	/* List of callback interests on the server */
529 	afs_volid_t		vid;		/* Volume ID to match */
530 	unsigned int		usage;
531 };
532 
533 /*
534  * Interest by a superblock on a server.
535  */
536 struct afs_cb_interest {
537 	struct hlist_node	cb_vlink;	/* Link in vol_interest->cb_interests */
538 	struct afs_vol_interest	*vol_interest;
539 	struct afs_server	*server;	/* Server on which this interest resides */
540 	struct super_block	*sb;		/* Superblock on which inodes reside */
541 	afs_volid_t		vid;		/* Volume ID to match */
542 	refcount_t		usage;
543 };
544 
545 /*
546  * Replaceable server list.
547  */
548 struct afs_server_entry {
549 	struct afs_server	*server;
550 	struct afs_cb_interest	*cb_interest;
551 };
552 
553 struct afs_server_list {
554 	refcount_t		usage;
555 	unsigned char		nr_servers;
556 	unsigned char		preferred;	/* Preferred server */
557 	unsigned short		vnovol_mask;	/* Servers to be skipped due to VNOVOL */
558 	unsigned int		seq;		/* Set to ->servers_seq when installed */
559 	rwlock_t		lock;
560 	struct afs_server_entry	servers[];
561 };
562 
563 /*
564  * Live AFS volume management.
565  */
566 struct afs_volume {
567 	afs_volid_t		vid;		/* volume ID */
568 	atomic_t		usage;
569 	time64_t		update_at;	/* Time at which to next update */
570 	struct afs_cell		*cell;		/* Cell to which belongs (pins ref) */
571 	struct list_head	proc_link;	/* Link in cell->vl_proc */
572 	unsigned long		flags;
573 #define AFS_VOLUME_NEEDS_UPDATE	0	/* - T if an update needs performing */
574 #define AFS_VOLUME_UPDATING	1	/* - T if an update is in progress */
575 #define AFS_VOLUME_WAIT		2	/* - T if users must wait for update */
576 #define AFS_VOLUME_DELETED	3	/* - T if volume appears deleted */
577 #define AFS_VOLUME_OFFLINE	4	/* - T if volume offline notice given */
578 #define AFS_VOLUME_BUSY		5	/* - T if volume busy notice given */
579 #ifdef CONFIG_AFS_FSCACHE
580 	struct fscache_cookie	*cache;		/* caching cookie */
581 #endif
582 	struct afs_server_list	*servers;	/* List of servers on which volume resides */
583 	rwlock_t		servers_lock;	/* Lock for ->servers */
584 	unsigned int		servers_seq;	/* Incremented each time ->servers changes */
585 
586 	unsigned		cb_v_break;	/* Break-everything counter. */
587 	rwlock_t		cb_break_lock;
588 
589 	afs_voltype_t		type;		/* type of volume */
590 	short			error;
591 	char			type_force;	/* force volume type (suppress R/O -> R/W) */
592 	u8			name_len;
593 	u8			name[AFS_MAXVOLNAME + 1]; /* NUL-padded volume name */
594 };
595 
596 enum afs_lock_state {
597 	AFS_VNODE_LOCK_NONE,		/* The vnode has no lock on the server */
598 	AFS_VNODE_LOCK_WAITING_FOR_CB,	/* We're waiting for the server to break the callback */
599 	AFS_VNODE_LOCK_SETTING,		/* We're asking the server for a lock */
600 	AFS_VNODE_LOCK_GRANTED,		/* We have a lock on the server */
601 	AFS_VNODE_LOCK_EXTENDING,	/* We're extending a lock on the server */
602 	AFS_VNODE_LOCK_NEED_UNLOCK,	/* We need to unlock on the server */
603 	AFS_VNODE_LOCK_UNLOCKING,	/* We're telling the server to unlock */
604 };
605 
606 /*
607  * AFS inode private data.
608  *
609  * Note that afs_alloc_inode() *must* reset anything that could incorrectly
610  * leak from one inode to another.
611  */
612 struct afs_vnode {
613 	struct inode		vfs_inode;	/* the VFS's inode record */
614 
615 	struct afs_volume	*volume;	/* volume on which vnode resides */
616 	struct afs_fid		fid;		/* the file identifier for this inode */
617 	struct afs_file_status	status;		/* AFS status info for this file */
618 	afs_dataversion_t	invalid_before;	/* Child dentries are invalid before this */
619 #ifdef CONFIG_AFS_FSCACHE
620 	struct fscache_cookie	*cache;		/* caching cookie */
621 #endif
622 	struct afs_permits __rcu *permit_cache;	/* cache of permits so far obtained */
623 	struct mutex		io_lock;	/* Lock for serialising I/O on this mutex */
624 	struct rw_semaphore	validate_lock;	/* lock for validating this vnode */
625 	spinlock_t		wb_lock;	/* lock for wb_keys */
626 	spinlock_t		lock;		/* waitqueue/flags lock */
627 	unsigned long		flags;
628 #define AFS_VNODE_CB_PROMISED	0		/* Set if vnode has a callback promise */
629 #define AFS_VNODE_UNSET		1		/* set if vnode attributes not yet set */
630 #define AFS_VNODE_DIR_VALID	2		/* Set if dir contents are valid */
631 #define AFS_VNODE_ZAP_DATA	3		/* set if vnode's data should be invalidated */
632 #define AFS_VNODE_DELETED	4		/* set if vnode deleted on server */
633 #define AFS_VNODE_MOUNTPOINT	5		/* set if vnode is a mountpoint symlink */
634 #define AFS_VNODE_AUTOCELL	6		/* set if Vnode is an auto mount point */
635 #define AFS_VNODE_PSEUDODIR	7 		/* set if Vnode is a pseudo directory */
636 #define AFS_VNODE_NEW_CONTENT	8		/* Set if file has new content (create/trunc-0) */
637 
638 	struct list_head	wb_keys;	/* List of keys available for writeback */
639 	struct list_head	pending_locks;	/* locks waiting to be granted */
640 	struct list_head	granted_locks;	/* locks granted on this file */
641 	struct delayed_work	lock_work;	/* work to be done in locking */
642 	struct key		*lock_key;	/* Key to be used in lock ops */
643 	enum afs_lock_state	lock_state : 8;
644 	afs_lock_type_t		lock_type : 8;
645 
646 	/* outstanding callback notification on this file */
647 	struct afs_cb_interest	*cb_interest;	/* Server on which this resides */
648 	unsigned int		cb_s_break;	/* Mass break counter on ->server */
649 	unsigned int		cb_v_break;	/* Mass break counter on ->volume */
650 	unsigned int		cb_break;	/* Break counter on vnode */
651 	seqlock_t		cb_lock;	/* Lock for ->cb_interest, ->status, ->cb_*break */
652 
653 	time64_t		cb_expires_at;	/* time at which callback expires */
654 	unsigned		cb_version;	/* callback version */
655 	afs_callback_type_t	cb_type;	/* type of callback */
656 };
657 
658 static inline struct fscache_cookie *afs_vnode_cache(struct afs_vnode *vnode)
659 {
660 #ifdef CONFIG_AFS_FSCACHE
661 	return vnode->cache;
662 #else
663 	return NULL;
664 #endif
665 }
666 
667 /*
668  * cached security record for one user's attempt to access a vnode
669  */
670 struct afs_permit {
671 	struct key		*key;		/* RxRPC ticket holding a security context */
672 	afs_access_t		access;		/* CallerAccess value for this key */
673 };
674 
675 /*
676  * Immutable cache of CallerAccess records from attempts to access vnodes.
677  * These may be shared between multiple vnodes.
678  */
679 struct afs_permits {
680 	struct rcu_head		rcu;
681 	struct hlist_node	hash_node;	/* Link in hash */
682 	unsigned long		h;		/* Hash value for this permit list */
683 	refcount_t		usage;
684 	unsigned short		nr_permits;	/* Number of records */
685 	bool			invalidated;	/* Invalidated due to key change */
686 	struct afs_permit	permits[];	/* List of permits sorted by key pointer */
687 };
688 
689 /*
690  * record of one of a system's set of network interfaces
691  */
692 struct afs_interface {
693 	struct in_addr	address;	/* IPv4 address bound to interface */
694 	struct in_addr	netmask;	/* netmask applied to address */
695 	unsigned	mtu;		/* MTU of interface */
696 };
697 
698 /*
699  * Error prioritisation and accumulation.
700  */
701 struct afs_error {
702 	short	error;			/* Accumulated error */
703 	bool	responded;		/* T if server responded */
704 };
705 
706 /*
707  * Cursor for iterating over a server's address list.
708  */
709 struct afs_addr_cursor {
710 	struct afs_addr_list	*alist;		/* Current address list (pins ref) */
711 	unsigned long		tried;		/* Tried addresses */
712 	signed char		index;		/* Current address */
713 	bool			responded;	/* T if the current address responded */
714 	unsigned short		nr_iterations;	/* Number of address iterations */
715 	short			error;
716 	u32			abort_code;
717 };
718 
719 /*
720  * Cursor for iterating over a set of volume location servers.
721  */
722 struct afs_vl_cursor {
723 	struct afs_addr_cursor	ac;
724 	struct afs_cell		*cell;		/* The cell we're querying */
725 	struct afs_vlserver_list *server_list;	/* Current server list (pins ref) */
726 	struct afs_vlserver	*server;	/* Server on which this resides */
727 	struct key		*key;		/* Key for the server */
728 	unsigned long		untried;	/* Bitmask of untried servers */
729 	short			index;		/* Current server */
730 	short			error;
731 	unsigned short		flags;
732 #define AFS_VL_CURSOR_STOP	0x0001		/* Set to cease iteration */
733 #define AFS_VL_CURSOR_RETRY	0x0002		/* Set to do a retry */
734 #define AFS_VL_CURSOR_RETRIED	0x0004		/* Set if started a retry */
735 	unsigned short		nr_iterations;	/* Number of server iterations */
736 };
737 
738 /*
739  * Cursor for iterating over a set of fileservers.
740  */
741 struct afs_fs_cursor {
742 	struct afs_addr_cursor	ac;
743 	struct afs_vnode	*vnode;
744 	struct afs_server_list	*server_list;	/* Current server list (pins ref) */
745 	struct afs_cb_interest	*cbi;		/* Server on which this resides (pins ref) */
746 	struct key		*key;		/* Key for the server */
747 	unsigned long		untried;	/* Bitmask of untried servers */
748 	unsigned int		cb_break;	/* cb_break + cb_s_break before the call */
749 	unsigned int		cb_break_2;	/* cb_break + cb_s_break (2nd vnode) */
750 	short			index;		/* Current server */
751 	short			error;
752 	unsigned short		flags;
753 #define AFS_FS_CURSOR_STOP	0x0001		/* Set to cease iteration */
754 #define AFS_FS_CURSOR_VBUSY	0x0002		/* Set if seen VBUSY */
755 #define AFS_FS_CURSOR_VMOVED	0x0004		/* Set if seen VMOVED */
756 #define AFS_FS_CURSOR_VNOVOL	0x0008		/* Set if seen VNOVOL */
757 #define AFS_FS_CURSOR_CUR_ONLY	0x0010		/* Set if current server only (file lock held) */
758 #define AFS_FS_CURSOR_NO_VSLEEP	0x0020		/* Set to prevent sleep on VBUSY, VOFFLINE, ... */
759 	unsigned short		nr_iterations;	/* Number of server iterations */
760 };
761 
762 /*
763  * Cache auxiliary data.
764  */
765 struct afs_vnode_cache_aux {
766 	u64			data_version;
767 } __packed;
768 
769 #include <trace/events/afs.h>
770 
771 /*****************************************************************************/
772 /*
773  * addr_list.c
774  */
775 static inline struct afs_addr_list *afs_get_addrlist(struct afs_addr_list *alist)
776 {
777 	if (alist)
778 		refcount_inc(&alist->usage);
779 	return alist;
780 }
781 extern struct afs_addr_list *afs_alloc_addrlist(unsigned int,
782 						unsigned short,
783 						unsigned short);
784 extern void afs_put_addrlist(struct afs_addr_list *);
785 extern struct afs_vlserver_list *afs_parse_text_addrs(struct afs_net *,
786 						      const char *, size_t, char,
787 						      unsigned short, unsigned short);
788 extern struct afs_vlserver_list *afs_dns_query(struct afs_cell *, time64_t *);
789 extern bool afs_iterate_addresses(struct afs_addr_cursor *);
790 extern int afs_end_cursor(struct afs_addr_cursor *);
791 
792 extern void afs_merge_fs_addr4(struct afs_addr_list *, __be32, u16);
793 extern void afs_merge_fs_addr6(struct afs_addr_list *, __be32 *, u16);
794 
795 /*
796  * cache.c
797  */
798 #ifdef CONFIG_AFS_FSCACHE
799 extern struct fscache_netfs afs_cache_netfs;
800 extern struct fscache_cookie_def afs_cell_cache_index_def;
801 extern struct fscache_cookie_def afs_volume_cache_index_def;
802 extern struct fscache_cookie_def afs_vnode_cache_index_def;
803 #else
804 #define afs_cell_cache_index_def	(*(struct fscache_cookie_def *) NULL)
805 #define afs_volume_cache_index_def	(*(struct fscache_cookie_def *) NULL)
806 #define afs_vnode_cache_index_def	(*(struct fscache_cookie_def *) NULL)
807 #endif
808 
809 /*
810  * callback.c
811  */
812 extern void afs_init_callback_state(struct afs_server *);
813 extern void __afs_break_callback(struct afs_vnode *);
814 extern void afs_break_callback(struct afs_vnode *);
815 extern void afs_break_callbacks(struct afs_server *, size_t, struct afs_callback_break*);
816 
817 extern int afs_register_server_cb_interest(struct afs_vnode *,
818 					   struct afs_server_list *, unsigned int);
819 extern void afs_put_cb_interest(struct afs_net *, struct afs_cb_interest *);
820 extern void afs_clear_callback_interests(struct afs_net *, struct afs_server_list *);
821 
822 static inline struct afs_cb_interest *afs_get_cb_interest(struct afs_cb_interest *cbi)
823 {
824 	if (cbi)
825 		refcount_inc(&cbi->usage);
826 	return cbi;
827 }
828 
829 static inline unsigned int afs_calc_vnode_cb_break(struct afs_vnode *vnode)
830 {
831 	return vnode->cb_break + vnode->cb_s_break + vnode->cb_v_break;
832 }
833 
834 static inline bool afs_cb_is_broken(unsigned int cb_break,
835 				    const struct afs_vnode *vnode,
836 				    const struct afs_cb_interest *cbi)
837 {
838 	return !cbi || cb_break != (vnode->cb_break +
839 				    cbi->server->cb_s_break +
840 				    vnode->volume->cb_v_break);
841 }
842 
843 /*
844  * cell.c
845  */
846 extern int afs_cell_init(struct afs_net *, const char *);
847 extern struct afs_cell *afs_lookup_cell_rcu(struct afs_net *, const char *, unsigned);
848 extern struct afs_cell *afs_lookup_cell(struct afs_net *, const char *, unsigned,
849 					const char *, bool);
850 extern struct afs_cell *afs_get_cell(struct afs_cell *);
851 extern void afs_put_cell(struct afs_net *, struct afs_cell *);
852 extern void afs_manage_cells(struct work_struct *);
853 extern void afs_cells_timer(struct timer_list *);
854 extern void __net_exit afs_cell_purge(struct afs_net *);
855 
856 /*
857  * cmservice.c
858  */
859 extern bool afs_cm_incoming_call(struct afs_call *);
860 
861 /*
862  * dir.c
863  */
864 extern const struct file_operations afs_dir_file_operations;
865 extern const struct inode_operations afs_dir_inode_operations;
866 extern const struct address_space_operations afs_dir_aops;
867 extern const struct dentry_operations afs_fs_dentry_operations;
868 
869 extern void afs_d_release(struct dentry *);
870 
871 /*
872  * dir_edit.c
873  */
874 extern void afs_edit_dir_add(struct afs_vnode *, struct qstr *, struct afs_fid *,
875 			     enum afs_edit_dir_reason);
876 extern void afs_edit_dir_remove(struct afs_vnode *, struct qstr *, enum afs_edit_dir_reason);
877 
878 /*
879  * dynroot.c
880  */
881 extern const struct file_operations afs_dynroot_file_operations;
882 extern const struct inode_operations afs_dynroot_inode_operations;
883 extern const struct dentry_operations afs_dynroot_dentry_operations;
884 
885 extern struct inode *afs_try_auto_mntpt(struct dentry *, struct inode *);
886 extern int afs_dynroot_mkdir(struct afs_net *, struct afs_cell *);
887 extern void afs_dynroot_rmdir(struct afs_net *, struct afs_cell *);
888 extern int afs_dynroot_populate(struct super_block *);
889 extern void afs_dynroot_depopulate(struct super_block *);
890 
891 /*
892  * file.c
893  */
894 extern const struct address_space_operations afs_fs_aops;
895 extern const struct inode_operations afs_file_inode_operations;
896 extern const struct file_operations afs_file_operations;
897 
898 extern int afs_cache_wb_key(struct afs_vnode *, struct afs_file *);
899 extern void afs_put_wb_key(struct afs_wb_key *);
900 extern int afs_open(struct inode *, struct file *);
901 extern int afs_release(struct inode *, struct file *);
902 extern int afs_fetch_data(struct afs_vnode *, struct key *, struct afs_read *);
903 extern int afs_page_filler(void *, struct page *);
904 extern void afs_put_read(struct afs_read *);
905 
906 /*
907  * flock.c
908  */
909 extern struct workqueue_struct *afs_lock_manager;
910 
911 extern void afs_lock_work(struct work_struct *);
912 extern void afs_lock_may_be_available(struct afs_vnode *);
913 extern int afs_lock(struct file *, int, struct file_lock *);
914 extern int afs_flock(struct file *, int, struct file_lock *);
915 
916 /*
917  * fsclient.c
918  */
919 #define AFS_VNODE_NOT_YET_SET	0x01
920 #define AFS_VNODE_META_CHANGED	0x02
921 #define AFS_VNODE_DATA_CHANGED	0x04
922 extern void afs_update_inode_from_status(struct afs_vnode *, struct afs_file_status *,
923 					 const afs_dataversion_t *, u8);
924 
925 extern int afs_fs_fetch_file_status(struct afs_fs_cursor *, struct afs_volsync *, bool);
926 extern int afs_fs_give_up_callbacks(struct afs_net *, struct afs_server *);
927 extern int afs_fs_fetch_data(struct afs_fs_cursor *, struct afs_read *);
928 extern int afs_fs_create(struct afs_fs_cursor *, const char *, umode_t, u64,
929 			 struct afs_fid *, struct afs_file_status *, struct afs_callback *);
930 extern int afs_fs_remove(struct afs_fs_cursor *, struct afs_vnode *, const char *, bool, u64);
931 extern int afs_fs_link(struct afs_fs_cursor *, struct afs_vnode *, const char *, u64);
932 extern int afs_fs_symlink(struct afs_fs_cursor *, const char *, const char *, u64,
933 			  struct afs_fid *, struct afs_file_status *);
934 extern int afs_fs_rename(struct afs_fs_cursor *, const char *,
935 			 struct afs_vnode *, const char *, u64, u64);
936 extern int afs_fs_store_data(struct afs_fs_cursor *, struct address_space *,
937 			     pgoff_t, pgoff_t, unsigned, unsigned);
938 extern int afs_fs_setattr(struct afs_fs_cursor *, struct iattr *);
939 extern int afs_fs_get_volume_status(struct afs_fs_cursor *, struct afs_volume_status *);
940 extern int afs_fs_set_lock(struct afs_fs_cursor *, afs_lock_type_t);
941 extern int afs_fs_extend_lock(struct afs_fs_cursor *);
942 extern int afs_fs_release_lock(struct afs_fs_cursor *);
943 extern int afs_fs_give_up_all_callbacks(struct afs_net *, struct afs_server *,
944 					struct afs_addr_cursor *, struct key *);
945 extern int afs_fs_get_capabilities(struct afs_net *, struct afs_server *,
946 				   struct afs_addr_cursor *, struct key *, unsigned int, bool);
947 extern int afs_fs_inline_bulk_status(struct afs_fs_cursor *, struct afs_net *,
948 				     struct afs_fid *, struct afs_file_status *,
949 				     struct afs_callback *, unsigned int,
950 				     struct afs_volsync *);
951 extern int afs_fs_fetch_status(struct afs_fs_cursor *, struct afs_net *,
952 			       struct afs_fid *, struct afs_file_status *,
953 			       struct afs_callback *, struct afs_volsync *);
954 
955 /*
956  * fs_probe.c
957  */
958 extern void afs_fileserver_probe_result(struct afs_call *);
959 extern int afs_probe_fileservers(struct afs_net *, struct key *, struct afs_server_list *);
960 extern int afs_wait_for_fs_probes(struct afs_server_list *, unsigned long);
961 
962 /*
963  * inode.c
964  */
965 extern int afs_fetch_status(struct afs_vnode *, struct key *, bool);
966 extern int afs_iget5_test(struct inode *, void *);
967 extern struct inode *afs_iget_pseudo_dir(struct super_block *, bool);
968 extern struct inode *afs_iget(struct super_block *, struct key *,
969 			      struct afs_fid *, struct afs_file_status *,
970 			      struct afs_callback *,
971 			      struct afs_cb_interest *);
972 extern void afs_zap_data(struct afs_vnode *);
973 extern int afs_validate(struct afs_vnode *, struct key *);
974 extern int afs_getattr(const struct path *, struct kstat *, u32, unsigned int);
975 extern int afs_setattr(struct dentry *, struct iattr *);
976 extern void afs_evict_inode(struct inode *);
977 extern int afs_drop_inode(struct inode *);
978 
979 /*
980  * main.c
981  */
982 extern struct workqueue_struct *afs_wq;
983 extern int afs_net_id;
984 
985 static inline struct afs_net *afs_net(struct net *net)
986 {
987 	return net_generic(net, afs_net_id);
988 }
989 
990 static inline struct afs_net *afs_sb2net(struct super_block *sb)
991 {
992 	return afs_net(AFS_FS_S(sb)->net_ns);
993 }
994 
995 static inline struct afs_net *afs_d2net(struct dentry *dentry)
996 {
997 	return afs_sb2net(dentry->d_sb);
998 }
999 
1000 static inline struct afs_net *afs_i2net(struct inode *inode)
1001 {
1002 	return afs_sb2net(inode->i_sb);
1003 }
1004 
1005 static inline struct afs_net *afs_v2net(struct afs_vnode *vnode)
1006 {
1007 	return afs_i2net(&vnode->vfs_inode);
1008 }
1009 
1010 static inline struct afs_net *afs_sock2net(struct sock *sk)
1011 {
1012 	return net_generic(sock_net(sk), afs_net_id);
1013 }
1014 
1015 static inline void __afs_stat(atomic_t *s)
1016 {
1017 	atomic_inc(s);
1018 }
1019 
1020 #define afs_stat_v(vnode, n) __afs_stat(&afs_v2net(vnode)->n)
1021 
1022 /*
1023  * misc.c
1024  */
1025 extern int afs_abort_to_error(u32);
1026 extern void afs_prioritise_error(struct afs_error *, int, u32);
1027 
1028 /*
1029  * mntpt.c
1030  */
1031 extern const struct inode_operations afs_mntpt_inode_operations;
1032 extern const struct inode_operations afs_autocell_inode_operations;
1033 extern const struct file_operations afs_mntpt_file_operations;
1034 
1035 extern struct vfsmount *afs_d_automount(struct path *);
1036 extern void afs_mntpt_kill_timer(void);
1037 
1038 /*
1039  * netdevices.c
1040  */
1041 extern int afs_get_ipv4_interfaces(struct afs_net *, struct afs_interface *,
1042 				   size_t, bool);
1043 
1044 /*
1045  * proc.c
1046  */
1047 #ifdef CONFIG_PROC_FS
1048 extern int __net_init afs_proc_init(struct afs_net *);
1049 extern void __net_exit afs_proc_cleanup(struct afs_net *);
1050 extern int afs_proc_cell_setup(struct afs_cell *);
1051 extern void afs_proc_cell_remove(struct afs_cell *);
1052 extern void afs_put_sysnames(struct afs_sysnames *);
1053 #else
1054 static inline int afs_proc_init(struct afs_net *net) { return 0; }
1055 static inline void afs_proc_cleanup(struct afs_net *net) {}
1056 static inline int afs_proc_cell_setup(struct afs_cell *cell) { return 0; }
1057 static inline void afs_proc_cell_remove(struct afs_cell *cell) {}
1058 static inline void afs_put_sysnames(struct afs_sysnames *sysnames) {}
1059 #endif
1060 
1061 /*
1062  * rotate.c
1063  */
1064 extern bool afs_begin_vnode_operation(struct afs_fs_cursor *, struct afs_vnode *,
1065 				      struct key *);
1066 extern bool afs_select_fileserver(struct afs_fs_cursor *);
1067 extern bool afs_select_current_fileserver(struct afs_fs_cursor *);
1068 extern int afs_end_vnode_operation(struct afs_fs_cursor *);
1069 
1070 /*
1071  * rxrpc.c
1072  */
1073 extern struct workqueue_struct *afs_async_calls;
1074 
1075 extern int __net_init afs_open_socket(struct afs_net *);
1076 extern void __net_exit afs_close_socket(struct afs_net *);
1077 extern void afs_charge_preallocation(struct work_struct *);
1078 extern void afs_put_call(struct afs_call *);
1079 extern long afs_make_call(struct afs_addr_cursor *, struct afs_call *, gfp_t, bool);
1080 extern struct afs_call *afs_alloc_flat_call(struct afs_net *,
1081 					    const struct afs_call_type *,
1082 					    size_t, size_t);
1083 extern void afs_flat_call_destructor(struct afs_call *);
1084 extern void afs_send_empty_reply(struct afs_call *);
1085 extern void afs_send_simple_reply(struct afs_call *, const void *, size_t);
1086 extern int afs_extract_data(struct afs_call *, bool);
1087 extern int afs_protocol_error(struct afs_call *, int, enum afs_eproto_cause);
1088 
1089 static inline void afs_extract_begin(struct afs_call *call, void *buf, size_t size)
1090 {
1091 	call->kvec[0].iov_base = buf;
1092 	call->kvec[0].iov_len = size;
1093 	iov_iter_kvec(&call->iter, READ, call->kvec, 1, size);
1094 }
1095 
1096 static inline void afs_extract_to_tmp(struct afs_call *call)
1097 {
1098 	afs_extract_begin(call, &call->tmp, sizeof(call->tmp));
1099 }
1100 
1101 static inline void afs_extract_to_tmp64(struct afs_call *call)
1102 {
1103 	afs_extract_begin(call, &call->tmp64, sizeof(call->tmp64));
1104 }
1105 
1106 static inline void afs_extract_discard(struct afs_call *call, size_t size)
1107 {
1108 	iov_iter_discard(&call->iter, READ, size);
1109 }
1110 
1111 static inline void afs_extract_to_buf(struct afs_call *call, size_t size)
1112 {
1113 	afs_extract_begin(call, call->buffer, size);
1114 }
1115 
1116 static inline int afs_transfer_reply(struct afs_call *call)
1117 {
1118 	return afs_extract_data(call, false);
1119 }
1120 
1121 static inline bool afs_check_call_state(struct afs_call *call,
1122 					enum afs_call_state state)
1123 {
1124 	return READ_ONCE(call->state) == state;
1125 }
1126 
1127 static inline bool afs_set_call_state(struct afs_call *call,
1128 				      enum afs_call_state from,
1129 				      enum afs_call_state to)
1130 {
1131 	bool ok = false;
1132 
1133 	spin_lock_bh(&call->state_lock);
1134 	if (call->state == from) {
1135 		call->state = to;
1136 		trace_afs_call_state(call, from, to, 0, 0);
1137 		ok = true;
1138 	}
1139 	spin_unlock_bh(&call->state_lock);
1140 	return ok;
1141 }
1142 
1143 static inline void afs_set_call_complete(struct afs_call *call,
1144 					 int error, u32 remote_abort)
1145 {
1146 	enum afs_call_state state;
1147 	bool ok = false;
1148 
1149 	spin_lock_bh(&call->state_lock);
1150 	state = call->state;
1151 	if (state != AFS_CALL_COMPLETE) {
1152 		call->abort_code = remote_abort;
1153 		call->error = error;
1154 		call->state = AFS_CALL_COMPLETE;
1155 		trace_afs_call_state(call, state, AFS_CALL_COMPLETE,
1156 				     error, remote_abort);
1157 		ok = true;
1158 	}
1159 	spin_unlock_bh(&call->state_lock);
1160 	if (ok)
1161 		trace_afs_call_done(call);
1162 }
1163 
1164 /*
1165  * security.c
1166  */
1167 extern void afs_put_permits(struct afs_permits *);
1168 extern void afs_clear_permits(struct afs_vnode *);
1169 extern void afs_cache_permit(struct afs_vnode *, struct key *, unsigned int);
1170 extern void afs_zap_permits(struct rcu_head *);
1171 extern struct key *afs_request_key(struct afs_cell *);
1172 extern int afs_check_permit(struct afs_vnode *, struct key *, afs_access_t *);
1173 extern int afs_permission(struct inode *, int);
1174 extern void __exit afs_clean_up_permit_cache(void);
1175 
1176 /*
1177  * server.c
1178  */
1179 extern spinlock_t afs_server_peer_lock;
1180 
1181 static inline struct afs_server *afs_get_server(struct afs_server *server)
1182 {
1183 	atomic_inc(&server->usage);
1184 	return server;
1185 }
1186 
1187 extern struct afs_server *afs_find_server(struct afs_net *,
1188 					  const struct sockaddr_rxrpc *);
1189 extern struct afs_server *afs_find_server_by_uuid(struct afs_net *, const uuid_t *);
1190 extern struct afs_server *afs_lookup_server(struct afs_cell *, struct key *, const uuid_t *);
1191 extern void afs_put_server(struct afs_net *, struct afs_server *);
1192 extern void afs_manage_servers(struct work_struct *);
1193 extern void afs_servers_timer(struct timer_list *);
1194 extern void __net_exit afs_purge_servers(struct afs_net *);
1195 extern bool afs_check_server_record(struct afs_fs_cursor *, struct afs_server *);
1196 
1197 /*
1198  * server_list.c
1199  */
1200 static inline struct afs_server_list *afs_get_serverlist(struct afs_server_list *slist)
1201 {
1202 	refcount_inc(&slist->usage);
1203 	return slist;
1204 }
1205 
1206 extern void afs_put_serverlist(struct afs_net *, struct afs_server_list *);
1207 extern struct afs_server_list *afs_alloc_server_list(struct afs_cell *, struct key *,
1208 						     struct afs_vldb_entry *,
1209 						     u8);
1210 extern bool afs_annotate_server_list(struct afs_server_list *, struct afs_server_list *);
1211 
1212 /*
1213  * super.c
1214  */
1215 extern int __init afs_fs_init(void);
1216 extern void afs_fs_exit(void);
1217 
1218 /*
1219  * vlclient.c
1220  */
1221 extern struct afs_vldb_entry *afs_vl_get_entry_by_name_u(struct afs_vl_cursor *,
1222 							 const char *, int);
1223 extern struct afs_addr_list *afs_vl_get_addrs_u(struct afs_vl_cursor *, const uuid_t *);
1224 extern int afs_vl_get_capabilities(struct afs_net *, struct afs_addr_cursor *, struct key *,
1225 				   struct afs_vlserver *, unsigned int, bool);
1226 extern struct afs_addr_list *afs_yfsvl_get_endpoints(struct afs_vl_cursor *, const uuid_t *);
1227 
1228 /*
1229  * vl_probe.c
1230  */
1231 extern void afs_vlserver_probe_result(struct afs_call *);
1232 extern int afs_send_vl_probes(struct afs_net *, struct key *, struct afs_vlserver_list *);
1233 extern int afs_wait_for_vl_probes(struct afs_vlserver_list *, unsigned long);
1234 
1235 /*
1236  * vl_rotate.c
1237  */
1238 extern bool afs_begin_vlserver_operation(struct afs_vl_cursor *,
1239 					 struct afs_cell *, struct key *);
1240 extern bool afs_select_vlserver(struct afs_vl_cursor *);
1241 extern bool afs_select_current_vlserver(struct afs_vl_cursor *);
1242 extern int afs_end_vlserver_operation(struct afs_vl_cursor *);
1243 
1244 /*
1245  * vlserver_list.c
1246  */
1247 static inline struct afs_vlserver *afs_get_vlserver(struct afs_vlserver *vlserver)
1248 {
1249 	atomic_inc(&vlserver->usage);
1250 	return vlserver;
1251 }
1252 
1253 static inline struct afs_vlserver_list *afs_get_vlserverlist(struct afs_vlserver_list *vllist)
1254 {
1255 	if (vllist)
1256 		atomic_inc(&vllist->usage);
1257 	return vllist;
1258 }
1259 
1260 extern struct afs_vlserver *afs_alloc_vlserver(const char *, size_t, unsigned short);
1261 extern void afs_put_vlserver(struct afs_net *, struct afs_vlserver *);
1262 extern struct afs_vlserver_list *afs_alloc_vlserver_list(unsigned int);
1263 extern void afs_put_vlserverlist(struct afs_net *, struct afs_vlserver_list *);
1264 extern struct afs_vlserver_list *afs_extract_vlserver_list(struct afs_cell *,
1265 							   const void *, size_t);
1266 
1267 /*
1268  * volume.c
1269  */
1270 static inline struct afs_volume *__afs_get_volume(struct afs_volume *volume)
1271 {
1272 	if (volume)
1273 		atomic_inc(&volume->usage);
1274 	return volume;
1275 }
1276 
1277 extern struct afs_volume *afs_create_volume(struct afs_mount_params *);
1278 extern void afs_activate_volume(struct afs_volume *);
1279 extern void afs_deactivate_volume(struct afs_volume *);
1280 extern void afs_put_volume(struct afs_cell *, struct afs_volume *);
1281 extern int afs_check_volume_status(struct afs_volume *, struct key *);
1282 
1283 /*
1284  * write.c
1285  */
1286 extern int afs_set_page_dirty(struct page *);
1287 extern int afs_write_begin(struct file *file, struct address_space *mapping,
1288 			loff_t pos, unsigned len, unsigned flags,
1289 			struct page **pagep, void **fsdata);
1290 extern int afs_write_end(struct file *file, struct address_space *mapping,
1291 			loff_t pos, unsigned len, unsigned copied,
1292 			struct page *page, void *fsdata);
1293 extern int afs_writepage(struct page *, struct writeback_control *);
1294 extern int afs_writepages(struct address_space *, struct writeback_control *);
1295 extern void afs_pages_written_back(struct afs_vnode *, struct afs_call *);
1296 extern ssize_t afs_file_write(struct kiocb *, struct iov_iter *);
1297 extern int afs_fsync(struct file *, loff_t, loff_t, int);
1298 extern vm_fault_t afs_page_mkwrite(struct vm_fault *vmf);
1299 extern void afs_prune_wb_keys(struct afs_vnode *);
1300 extern int afs_launder_page(struct page *);
1301 
1302 /*
1303  * xattr.c
1304  */
1305 extern const struct xattr_handler *afs_xattr_handlers[];
1306 extern ssize_t afs_listxattr(struct dentry *, char *, size_t);
1307 
1308 /*
1309  * yfsclient.c
1310  */
1311 extern int yfs_fs_fetch_file_status(struct afs_fs_cursor *, struct afs_volsync *, bool);
1312 extern int yfs_fs_fetch_data(struct afs_fs_cursor *, struct afs_read *);
1313 extern int yfs_fs_create_file(struct afs_fs_cursor *, const char *, umode_t, u64,
1314 			      struct afs_fid *, struct afs_file_status *, struct afs_callback *);
1315 extern int yfs_fs_make_dir(struct afs_fs_cursor *, const char *, umode_t, u64,
1316 			 struct afs_fid *, struct afs_file_status *, struct afs_callback *);
1317 extern int yfs_fs_remove_file2(struct afs_fs_cursor *, struct afs_vnode *, const char *, u64);
1318 extern int yfs_fs_remove(struct afs_fs_cursor *, struct afs_vnode *, const char *, bool, u64);
1319 extern int yfs_fs_link(struct afs_fs_cursor *, struct afs_vnode *, const char *, u64);
1320 extern int yfs_fs_symlink(struct afs_fs_cursor *, const char *, const char *, u64,
1321 			  struct afs_fid *, struct afs_file_status *);
1322 extern int yfs_fs_rename(struct afs_fs_cursor *, const char *,
1323 			 struct afs_vnode *, const char *, u64, u64);
1324 extern int yfs_fs_store_data(struct afs_fs_cursor *, struct address_space *,
1325 			     pgoff_t, pgoff_t, unsigned, unsigned);
1326 extern int yfs_fs_setattr(struct afs_fs_cursor *, struct iattr *);
1327 extern int yfs_fs_get_volume_status(struct afs_fs_cursor *, struct afs_volume_status *);
1328 extern int yfs_fs_set_lock(struct afs_fs_cursor *, afs_lock_type_t);
1329 extern int yfs_fs_extend_lock(struct afs_fs_cursor *);
1330 extern int yfs_fs_release_lock(struct afs_fs_cursor *);
1331 extern int yfs_fs_fetch_status(struct afs_fs_cursor *, struct afs_net *,
1332 			       struct afs_fid *, struct afs_file_status *,
1333 			       struct afs_callback *, struct afs_volsync *);
1334 extern int yfs_fs_inline_bulk_status(struct afs_fs_cursor *, struct afs_net *,
1335 				     struct afs_fid *, struct afs_file_status *,
1336 				     struct afs_callback *, unsigned int,
1337 				     struct afs_volsync *);
1338 
1339 /*
1340  * Miscellaneous inline functions.
1341  */
1342 static inline struct afs_vnode *AFS_FS_I(struct inode *inode)
1343 {
1344 	return container_of(inode, struct afs_vnode, vfs_inode);
1345 }
1346 
1347 static inline struct inode *AFS_VNODE_TO_I(struct afs_vnode *vnode)
1348 {
1349 	return &vnode->vfs_inode;
1350 }
1351 
1352 static inline void afs_vnode_commit_status(struct afs_fs_cursor *fc,
1353 					   struct afs_vnode *vnode,
1354 					   unsigned int cb_break)
1355 {
1356 	if (fc->ac.error == 0)
1357 		afs_cache_permit(vnode, fc->key, cb_break);
1358 }
1359 
1360 static inline void afs_check_for_remote_deletion(struct afs_fs_cursor *fc,
1361 						 struct afs_vnode *vnode)
1362 {
1363 	if (fc->ac.error == -ENOENT) {
1364 		set_bit(AFS_VNODE_DELETED, &vnode->flags);
1365 		afs_break_callback(vnode);
1366 	}
1367 }
1368 
1369 static inline int afs_io_error(struct afs_call *call, enum afs_io_error where)
1370 {
1371 	trace_afs_io_error(call->debug_id, -EIO, where);
1372 	return -EIO;
1373 }
1374 
1375 static inline int afs_bad(struct afs_vnode *vnode, enum afs_file_error where)
1376 {
1377 	trace_afs_file_error(vnode, -EIO, where);
1378 	return -EIO;
1379 }
1380 
1381 /*****************************************************************************/
1382 /*
1383  * debug tracing
1384  */
1385 extern unsigned afs_debug;
1386 
1387 #define dbgprintk(FMT,...) \
1388 	printk("[%-6.6s] "FMT"\n", current->comm ,##__VA_ARGS__)
1389 
1390 #define kenter(FMT,...)	dbgprintk("==> %s("FMT")",__func__ ,##__VA_ARGS__)
1391 #define kleave(FMT,...)	dbgprintk("<== %s()"FMT"",__func__ ,##__VA_ARGS__)
1392 #define kdebug(FMT,...)	dbgprintk("    "FMT ,##__VA_ARGS__)
1393 
1394 
1395 #if defined(__KDEBUG)
1396 #define _enter(FMT,...)	kenter(FMT,##__VA_ARGS__)
1397 #define _leave(FMT,...)	kleave(FMT,##__VA_ARGS__)
1398 #define _debug(FMT,...)	kdebug(FMT,##__VA_ARGS__)
1399 
1400 #elif defined(CONFIG_AFS_DEBUG)
1401 #define AFS_DEBUG_KENTER	0x01
1402 #define AFS_DEBUG_KLEAVE	0x02
1403 #define AFS_DEBUG_KDEBUG	0x04
1404 
1405 #define _enter(FMT,...)					\
1406 do {							\
1407 	if (unlikely(afs_debug & AFS_DEBUG_KENTER))	\
1408 		kenter(FMT,##__VA_ARGS__);		\
1409 } while (0)
1410 
1411 #define _leave(FMT,...)					\
1412 do {							\
1413 	if (unlikely(afs_debug & AFS_DEBUG_KLEAVE))	\
1414 		kleave(FMT,##__VA_ARGS__);		\
1415 } while (0)
1416 
1417 #define _debug(FMT,...)					\
1418 do {							\
1419 	if (unlikely(afs_debug & AFS_DEBUG_KDEBUG))	\
1420 		kdebug(FMT,##__VA_ARGS__);		\
1421 } while (0)
1422 
1423 #else
1424 #define _enter(FMT,...)	no_printk("==> %s("FMT")",__func__ ,##__VA_ARGS__)
1425 #define _leave(FMT,...)	no_printk("<== %s()"FMT"",__func__ ,##__VA_ARGS__)
1426 #define _debug(FMT,...)	no_printk("    "FMT ,##__VA_ARGS__)
1427 #endif
1428 
1429 /*
1430  * debug assertion checking
1431  */
1432 #if 1 // defined(__KDEBUGALL)
1433 
1434 #define ASSERT(X)						\
1435 do {								\
1436 	if (unlikely(!(X))) {					\
1437 		printk(KERN_ERR "\n");				\
1438 		printk(KERN_ERR "AFS: Assertion failed\n");	\
1439 		BUG();						\
1440 	}							\
1441 } while(0)
1442 
1443 #define ASSERTCMP(X, OP, Y)						\
1444 do {									\
1445 	if (unlikely(!((X) OP (Y)))) {					\
1446 		printk(KERN_ERR "\n");					\
1447 		printk(KERN_ERR "AFS: Assertion failed\n");		\
1448 		printk(KERN_ERR "%lu " #OP " %lu is false\n",		\
1449 		       (unsigned long)(X), (unsigned long)(Y));		\
1450 		printk(KERN_ERR "0x%lx " #OP " 0x%lx is false\n",	\
1451 		       (unsigned long)(X), (unsigned long)(Y));		\
1452 		BUG();							\
1453 	}								\
1454 } while(0)
1455 
1456 #define ASSERTRANGE(L, OP1, N, OP2, H)					\
1457 do {									\
1458 	if (unlikely(!((L) OP1 (N)) || !((N) OP2 (H)))) {		\
1459 		printk(KERN_ERR "\n");					\
1460 		printk(KERN_ERR "AFS: Assertion failed\n");		\
1461 		printk(KERN_ERR "%lu "#OP1" %lu "#OP2" %lu is false\n",	\
1462 		       (unsigned long)(L), (unsigned long)(N),		\
1463 		       (unsigned long)(H));				\
1464 		printk(KERN_ERR "0x%lx "#OP1" 0x%lx "#OP2" 0x%lx is false\n", \
1465 		       (unsigned long)(L), (unsigned long)(N),		\
1466 		       (unsigned long)(H));				\
1467 		BUG();							\
1468 	}								\
1469 } while(0)
1470 
1471 #define ASSERTIF(C, X)						\
1472 do {								\
1473 	if (unlikely((C) && !(X))) {				\
1474 		printk(KERN_ERR "\n");				\
1475 		printk(KERN_ERR "AFS: Assertion failed\n");	\
1476 		BUG();						\
1477 	}							\
1478 } while(0)
1479 
1480 #define ASSERTIFCMP(C, X, OP, Y)					\
1481 do {									\
1482 	if (unlikely((C) && !((X) OP (Y)))) {				\
1483 		printk(KERN_ERR "\n");					\
1484 		printk(KERN_ERR "AFS: Assertion failed\n");		\
1485 		printk(KERN_ERR "%lu " #OP " %lu is false\n",		\
1486 		       (unsigned long)(X), (unsigned long)(Y));		\
1487 		printk(KERN_ERR "0x%lx " #OP " 0x%lx is false\n",	\
1488 		       (unsigned long)(X), (unsigned long)(Y));		\
1489 		BUG();							\
1490 	}								\
1491 } while(0)
1492 
1493 #else
1494 
1495 #define ASSERT(X)				\
1496 do {						\
1497 } while(0)
1498 
1499 #define ASSERTCMP(X, OP, Y)			\
1500 do {						\
1501 } while(0)
1502 
1503 #define ASSERTRANGE(L, OP1, N, OP2, H)		\
1504 do {						\
1505 } while(0)
1506 
1507 #define ASSERTIF(C, X)				\
1508 do {						\
1509 } while(0)
1510 
1511 #define ASSERTIFCMP(C, X, OP, Y)		\
1512 do {						\
1513 } while(0)
1514 
1515 #endif /* __KDEBUGALL */
1516