xref: /linux/include/linux/netfs.h (revision 69bf14300b50631e7c2271289d347bbd1b19a06c)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* Network filesystem support services.
3  *
4  * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  *
7  * See:
8  *
9  *	Documentation/filesystems/netfs_library.rst
10  *
11  * for a description of the network filesystem interface declared here.
12  */
13 
14 #ifndef _LINUX_NETFS_H
15 #define _LINUX_NETFS_H
16 
17 #include <linux/workqueue.h>
18 #include <linux/fs.h>
19 #include <linux/pagemap.h>
20 #include <linux/uio.h>
21 #include <linux/rolling_buffer.h>
22 
23 enum netfs_sreq_ref_trace;
24 typedef struct mempool mempool_t;
25 struct folio_queue;
26 
27 /**
28  * folio_start_private_2 - Start an fscache write on a folio.  [DEPRECATED]
29  * @folio: The folio.
30  *
31  * Call this function before writing a folio to a local cache.  Starting a
32  * second write before the first one finishes is not allowed.
33  *
34  * Note that this should no longer be used.
35  */
36 static inline void folio_start_private_2(struct folio *folio)
37 {
38 	VM_BUG_ON_FOLIO(folio_test_private_2(folio), folio);
39 	folio_get(folio);
40 	folio_set_private_2(folio);
41 }
42 
43 enum netfs_io_source {
44 	NETFS_SOURCE_UNKNOWN,
45 	NETFS_FILL_WITH_ZEROES,
46 	NETFS_DOWNLOAD_FROM_SERVER,
47 	NETFS_READ_FROM_CACHE,
48 	NETFS_INVALID_READ,
49 	NETFS_UPLOAD_TO_SERVER,
50 	NETFS_WRITE_TO_CACHE,
51 } __mode(byte);
52 
53 typedef void (*netfs_io_terminated_t)(void *priv, ssize_t transferred_or_error);
54 
55 /*
56  * Per-inode context.  This wraps the VFS inode.
57  */
58 struct netfs_inode {
59 	struct inode		inode;		/* The VFS inode */
60 	const struct netfs_request_ops *ops;
61 #if IS_ENABLED(CONFIG_FSCACHE)
62 	struct fscache_cookie	*cache;
63 #endif
64 	struct list_head	wb_queue;	/* Queue of processes wanting to do writeback */
65 	loff_t			_remote_i_size;	/* Size of the remote file */
66 	loff_t			_zero_point;	/* Size after which we assume there's no data
67 						 * on the server */
68 	spinlock_t		lock;		/* Lock covering wb_queue */
69 	atomic_t		io_count;	/* Number of outstanding reqs */
70 	unsigned long		flags;
71 #define NETFS_ICTX_ODIRECT	0		/* The file has DIO in progress */
72 #define NETFS_ICTX_UNBUFFERED	1		/* I/O should not use the pagecache */
73 #define NETFS_ICTX_WB_LOCK	2		/* Writeback serialisation lock */
74 #define NETFS_ICTX_MODIFIED_ATTR 3		/* Indicate change in mtime/ctime */
75 #define NETFS_ICTX_SINGLE_NO_UPLOAD 4		/* Monolithic payload, cache but no upload */
76 };
77 
78 /*
79  * A netfs group - for instance a ceph snap.  This is marked on dirty pages and
80  * pages marked with a group must be flushed before they can be written under
81  * the domain of another group.
82  */
83 struct netfs_group {
84 	refcount_t		ref;
85 	void (*free)(struct netfs_group *netfs_group);
86 };
87 
88 /*
89  * Information about a dirty page (attached only if necessary).
90  * folio->private
91  */
92 struct netfs_folio {
93 	struct netfs_group	*netfs_group;	/* Filesystem's grouping marker (or NULL). */
94 	unsigned int		dirty_offset;	/* Write-streaming dirty data offset */
95 	unsigned int		dirty_len;	/* Write-streaming dirty data length */
96 };
97 #define NETFS_FOLIO_INFO	0x1UL	/* OR'd with folio->private. */
98 #define NETFS_FOLIO_COPY_TO_CACHE ((struct netfs_group *)0x356UL) /* Write to the cache only */
99 
100 static inline bool netfs_is_folio_info(const void *priv)
101 {
102 	return (unsigned long)priv & NETFS_FOLIO_INFO;
103 }
104 
105 static inline struct netfs_folio *__netfs_folio_info(const void *priv)
106 {
107 	if (netfs_is_folio_info(priv))
108 		return (struct netfs_folio *)((unsigned long)priv & ~NETFS_FOLIO_INFO);
109 	return NULL;
110 }
111 
112 static inline struct netfs_folio *netfs_folio_info(struct folio *folio)
113 {
114 	return __netfs_folio_info(folio_get_private(folio));
115 }
116 
117 static inline struct netfs_group *netfs_folio_group(struct folio *folio)
118 {
119 	struct netfs_folio *finfo;
120 	void *priv = folio_get_private(folio);
121 
122 	finfo = netfs_folio_info(folio);
123 	if (finfo)
124 		return finfo->netfs_group;
125 	return priv;
126 }
127 
128 /*
129  * Stream of I/O subrequests going to a particular destination, such as the
130  * server or the local cache.  This is mainly intended for writing where we may
131  * have to write to multiple destinations concurrently.
132  */
133 struct netfs_io_stream {
134 	/* Submission tracking */
135 	struct netfs_io_subrequest *construct;	/* Op being constructed */
136 	size_t			sreq_max_len;	/* Maximum size of a subrequest */
137 	unsigned int		sreq_max_segs;	/* 0 or max number of segments in an iterator */
138 	unsigned int		submit_off;	/* Folio offset we're submitting from */
139 	unsigned int		submit_len;	/* Amount of data left to submit */
140 	unsigned int		submit_extendable_to; /* Amount I/O can be rounded up to */
141 	void (*prepare_write)(struct netfs_io_subrequest *subreq);
142 	void (*issue_write)(struct netfs_io_subrequest *subreq);
143 	/* Collection tracking */
144 	struct list_head	subrequests;	/* Contributory I/O operations */
145 	unsigned long long	collected_to;	/* Position we've collected results to */
146 	size_t			transferred;	/* The amount transferred from this stream */
147 	unsigned short		error;		/* Aggregate error for the stream */
148 	enum netfs_io_source	source;		/* Where to read from/write to */
149 	unsigned char		stream_nr;	/* Index of stream in parent table */
150 	bool			avail;		/* T if stream is available */
151 	bool			active;		/* T if stream is active */
152 	bool			need_retry;	/* T if this stream needs retrying */
153 	bool			failed;		/* T if this stream failed */
154 	bool			transferred_valid; /* T is ->transferred is valid */
155 };
156 
157 /*
158  * Resources required to do operations on a cache.
159  */
160 struct netfs_cache_resources {
161 	const struct netfs_cache_ops	*ops;
162 	void				*cache_priv;
163 	void				*cache_priv2;
164 	unsigned int			debug_id;	/* Cookie debug ID */
165 	unsigned int			inval_counter;	/* object->inval_counter at begin_op */
166 };
167 
168 /*
169  * Descriptor for a single component subrequest.  Each operation represents an
170  * individual read/write from/to a server, a cache, a journal, etc..
171  *
172  * The buffer iterator is persistent for the life of the subrequest struct and
173  * the pages it points to can be relied on to exist for the duration.
174  */
175 struct netfs_io_subrequest {
176 	struct netfs_io_request *rreq;		/* Supervising I/O request */
177 	struct work_struct	work;
178 	struct list_head	rreq_link;	/* Link in rreq->subrequests */
179 	struct iov_iter		io_iter;	/* Iterator for this subrequest */
180 	unsigned long long	start;		/* Where to start the I/O */
181 	size_t			len;		/* Size of the I/O */
182 	size_t			transferred;	/* Amount of data transferred */
183 	refcount_t		ref;
184 	short			error;		/* 0 or error that occurred */
185 	unsigned short		debug_index;	/* Index in list (for debugging output) */
186 	unsigned int		nr_segs;	/* Number of segs in io_iter */
187 	u8			retry_count;	/* The number of retries (0 on initial pass) */
188 	enum netfs_io_source	source;		/* Where to read from/write to */
189 	unsigned char		stream_nr;	/* I/O stream this belongs to */
190 	unsigned long		flags;
191 #define NETFS_SREQ_COPY_TO_CACHE	0	/* Set if should copy the data to the cache */
192 #define NETFS_SREQ_CLEAR_TAIL		1	/* Set if the rest of the read should be cleared */
193 #define NETFS_SREQ_MADE_PROGRESS	4	/* Set if we transferred at least some data */
194 #define NETFS_SREQ_BOUNDARY		6	/* Set if ends on hard boundary (eg. ceph object) */
195 #define NETFS_SREQ_HIT_EOF		7	/* Set if short due to EOF */
196 #define NETFS_SREQ_IN_PROGRESS		8	/* Unlocked when the subrequest completes */
197 #define NETFS_SREQ_NEED_RETRY		9	/* Set if the filesystem requests a retry */
198 #define NETFS_SREQ_FAILED		10	/* Set if the subreq failed unretryably */
199 };
200 
201 enum netfs_io_origin {
202 	NETFS_READAHEAD,		/* This read was triggered by readahead */
203 	NETFS_READPAGE,			/* This read is a synchronous read */
204 	NETFS_READ_GAPS,		/* This read is a synchronous read to fill gaps */
205 	NETFS_READ_SINGLE,		/* This read should be treated as a single object */
206 	NETFS_READ_FOR_WRITE,		/* This read is to prepare a write */
207 	NETFS_UNBUFFERED_READ,		/* This is an unbuffered read */
208 	NETFS_DIO_READ,			/* This is a direct I/O read */
209 	NETFS_WRITEBACK,		/* This write was triggered by writepages */
210 	NETFS_WRITEBACK_SINGLE,		/* This monolithic write was triggered by writepages */
211 	NETFS_WRITETHROUGH,		/* This write was made by netfs_perform_write() */
212 	NETFS_UNBUFFERED_WRITE,		/* This is an unbuffered write */
213 	NETFS_DIO_WRITE,		/* This is a direct I/O write */
214 	NETFS_PGPRIV2_COPY_TO_CACHE,	/* [DEPRECATED] This is writing read data to the cache */
215 	nr__netfs_io_origin
216 } __mode(byte);
217 
218 /*
219  * Descriptor for an I/O helper request.  This is used to make multiple I/O
220  * operations to a variety of data stores and then stitch the result together.
221  */
222 struct netfs_io_request {
223 	union {
224 		struct work_struct cleanup_work; /* Deferred cleanup work */
225 		struct rcu_head rcu;
226 	};
227 	struct work_struct	work;		/* Result collector work */
228 	struct inode		*inode;		/* The file being accessed */
229 	struct address_space	*mapping;	/* The mapping being accessed */
230 	struct kiocb		*iocb;		/* AIO completion vector */
231 	struct netfs_cache_resources cache_resources;
232 	struct netfs_io_request	*copy_to_cache;	/* Request to write just-read data to the cache */
233 #ifdef CONFIG_PROC_FS
234 	struct list_head	proc_link;	/* Link in netfs_iorequests */
235 #endif
236 	struct netfs_io_stream	io_streams[2];	/* Streams of parallel I/O operations */
237 #define NR_IO_STREAMS 2 //wreq->nr_io_streams
238 	struct netfs_group	*group;		/* Writeback group being written back */
239 	struct rolling_buffer	buffer;		/* Unencrypted buffer */
240 #define NETFS_ROLLBUF_PUT_MARK		ROLLBUF_MARK_1
241 #define NETFS_ROLLBUF_PAGECACHE_MARK	ROLLBUF_MARK_2
242 	wait_queue_head_t	waitq;		/* Processor waiter */
243 	void			*netfs_priv;	/* Private data for the netfs */
244 	void			*netfs_priv2;	/* Private data for the netfs */
245 	struct bio_vec		*direct_bv;	/* DIO buffer list (when handling iovec-iter) */
246 	unsigned long long	submitted;	/* Amount submitted for I/O so far */
247 	unsigned long long	len;		/* Length of the request */
248 	size_t			transferred;	/* Amount to be indicated as transferred */
249 	size_t			progress_at;	/* Report read progress when hit this much read */
250 	long			error;		/* 0 or error that occurred */
251 	unsigned long long	i_size;		/* Size of the file */
252 	unsigned long long	start;		/* Start position */
253 	atomic64_t		issued_to;	/* Write issuer folio cursor */
254 	unsigned long long	collected_to;	/* Point we've collected to */
255 	unsigned long long	cleaned_to;	/* Position we've cleaned folios to */
256 	unsigned long long	abandon_to;	/* Position to abandon folios to */
257 	const struct folio	*no_unlock_folio; /* Don't unlock this folio after read */
258 	gfp_t			gfp;		/* GFP flags to use */
259 	unsigned int		direct_bv_count; /* Number of elements in direct_bv[] */
260 	unsigned int		debug_id;
261 	unsigned int		rsize;		/* Maximum read size (0 for none) */
262 	unsigned int		wsize;		/* Maximum write size (0 for none) */
263 	atomic_t		subreq_counter;	/* Next subreq->debug_index */
264 	unsigned int		nr_group_rel;	/* Number of refs to release on ->group */
265 	spinlock_t		lock;		/* Lock for queuing subreqs */
266 	enum netfs_io_origin	origin;		/* Origin of the request */
267 	bool			direct_bv_unpin; /* T if direct_bv[] must be unpinned */
268 	refcount_t		ref;
269 	unsigned long		flags;
270 #define NETFS_RREQ_IN_PROGRESS		0	/* Unlocked when the request completes (has ref) */
271 #define NETFS_RREQ_ALL_QUEUED		1	/* All subreqs are now queued */
272 #define NETFS_RREQ_PAUSE		2	/* Pause subrequest generation */
273 #define NETFS_RREQ_FAILED		3	/* The request failed */
274 #define NETFS_RREQ_RETRYING		4	/* Set if we're in the retry path */
275 #define NETFS_RREQ_SHORT_TRANSFER	5	/* Set if we have a short transfer */
276 #define NETFS_RREQ_OFFLOAD_COLLECTION	8	/* Offload collection to workqueue */
277 #define NETFS_RREQ_NO_UNLOCK_FOLIO	9	/* Don't unlock no_unlock_folio on completion */
278 #define NETFS_RREQ_CANCEL_CACHING	10	/* Set to cancel caching */
279 #define NETFS_RREQ_UPLOAD_TO_SERVER	11	/* Need to write to the server */
280 #define NETFS_RREQ_USE_IO_ITER		12	/* Use ->io_iter rather than ->i_pages */
281 #define NETFS_RREQ_NEED_PUT_RA_REFS	17	/* Need to put the folio refs RA gave us */
282 #define NETFS_RREQ_USE_PGPRIV2		31	/* [DEPRECATED] Use PG_private_2 to mark
283 						 * write to cache on read */
284 	const struct netfs_request_ops *netfs_ops;
285 };
286 
287 /*
288  * Operations the network filesystem can/must provide to the helpers.
289  */
290 struct netfs_request_ops {
291 	mempool_t *request_pool;
292 	mempool_t *subrequest_pool;
293 	int (*init_request)(struct netfs_io_request *rreq, struct file *file);
294 	void (*free_request)(struct netfs_io_request *rreq);
295 	void (*free_subrequest)(struct netfs_io_subrequest *rreq);
296 
297 	/* Read request handling */
298 	void (*expand_readahead)(struct netfs_io_request *rreq);
299 	int (*prepare_read)(struct netfs_io_subrequest *subreq);
300 	void (*issue_read)(struct netfs_io_subrequest *subreq);
301 	bool (*is_still_valid)(struct netfs_io_request *rreq);
302 	int (*check_write_begin)(struct file *file, loff_t pos, unsigned len,
303 				 struct folio **foliop, void **_fsdata);
304 	void (*done)(struct netfs_io_request *rreq);
305 
306 	/* Modification handling */
307 	void (*update_i_size)(struct inode *inode, loff_t i_size);
308 	void (*post_modify)(struct inode *inode);
309 
310 	/* Write request handling */
311 	void (*begin_writeback)(struct netfs_io_request *wreq);
312 	void (*prepare_write)(struct netfs_io_subrequest *subreq);
313 	void (*issue_write)(struct netfs_io_subrequest *subreq);
314 	void (*retry_request)(struct netfs_io_request *wreq, struct netfs_io_stream *stream);
315 	void (*invalidate_cache)(struct netfs_io_request *wreq);
316 };
317 
318 /*
319  * How to handle reading from a hole.
320  */
321 enum netfs_read_from_hole {
322 	NETFS_READ_HOLE_IGNORE,
323 	NETFS_READ_HOLE_FAIL,
324 };
325 
326 /*
327  * Table of operations for access to a cache.
328  */
329 struct netfs_cache_ops {
330 	/* End an operation */
331 	void (*end_operation)(struct netfs_cache_resources *cres);
332 
333 	/* Read data from the cache */
334 	int (*read)(struct netfs_cache_resources *cres,
335 		    loff_t start_pos,
336 		    struct iov_iter *iter,
337 		    enum netfs_read_from_hole read_hole,
338 		    netfs_io_terminated_t term_func,
339 		    void *term_func_priv);
340 
341 	/* Write data to the cache */
342 	int (*write)(struct netfs_cache_resources *cres,
343 		     loff_t start_pos,
344 		     struct iov_iter *iter,
345 		     netfs_io_terminated_t term_func,
346 		     void *term_func_priv);
347 
348 	/* Write data to the cache from a netfs subrequest. */
349 	void (*issue_write)(struct netfs_io_subrequest *subreq);
350 
351 	/* Expand readahead request */
352 	void (*expand_readahead)(struct netfs_cache_resources *cres,
353 				 unsigned long long *_start,
354 				 unsigned long long *_len,
355 				 unsigned long long i_size);
356 
357 	/* Prepare a read operation, shortening it to a cached/uncached
358 	 * boundary as appropriate.
359 	 */
360 	enum netfs_io_source (*prepare_read)(struct netfs_io_subrequest *subreq,
361 					     unsigned long long i_size);
362 
363 	/* Prepare a write subrequest, working out if we're allowed to do it
364 	 * and finding out the maximum amount of data to gather before
365 	 * attempting to submit.  If we're not permitted to do it, the
366 	 * subrequest should be marked failed.
367 	 */
368 	void (*prepare_write_subreq)(struct netfs_io_subrequest *subreq);
369 
370 	/* Prepare a write operation, working out what part of the write we can
371 	 * actually do.
372 	 */
373 	int (*prepare_write)(struct netfs_cache_resources *cres,
374 			     loff_t *_start, size_t *_len, size_t upper_len,
375 			     loff_t i_size, bool no_space_allocated_yet);
376 
377 	/* Query the occupancy of the cache in a region, returning where the
378 	 * next chunk of data starts and how long it is.
379 	 */
380 	int (*query_occupancy)(struct netfs_cache_resources *cres,
381 			       loff_t start, size_t len, size_t granularity,
382 			       loff_t *_data_start, size_t *_data_len);
383 };
384 
385 /* High-level read API. */
386 ssize_t netfs_unbuffered_read_iter_locked(struct kiocb *iocb, struct iov_iter *iter);
387 ssize_t netfs_unbuffered_read_iter(struct kiocb *iocb, struct iov_iter *iter);
388 ssize_t netfs_buffered_read_iter(struct kiocb *iocb, struct iov_iter *iter);
389 ssize_t netfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter);
390 
391 /* High-level write API */
392 ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter,
393 			    struct netfs_group *netfs_group);
394 ssize_t netfs_buffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *from,
395 					 struct netfs_group *netfs_group);
396 ssize_t netfs_unbuffered_write_iter(struct kiocb *iocb, struct iov_iter *from);
397 ssize_t netfs_unbuffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *iter,
398 					   struct netfs_group *netfs_group);
399 ssize_t netfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from);
400 void netfs_clear_stale_post_isize(struct inode *inode, uoff_t from,
401 				  uoff_t to);
402 
403 /* Single, monolithic object read/write API. */
404 void netfs_single_mark_inode_dirty(struct inode *inode);
405 ssize_t netfs_read_single(struct inode *inode, struct file *file, struct iov_iter *iter);
406 int netfs_writeback_single(struct address_space *mapping,
407 			   struct writeback_control *wbc,
408 			   struct iov_iter *iter);
409 
410 /* Address operations API */
411 struct readahead_control;
412 void netfs_readahead(struct readahead_control *);
413 int netfs_read_folio(struct file *, struct folio *);
414 int netfs_write_begin(struct netfs_inode *, struct file *,
415 		      struct address_space *, loff_t pos, unsigned int len,
416 		      struct folio **, void **fsdata);
417 int netfs_writepages(struct address_space *mapping,
418 		     struct writeback_control *wbc);
419 bool netfs_dirty_folio(struct address_space *mapping, struct folio *folio);
420 int netfs_unpin_writeback(struct inode *inode, struct writeback_control *wbc);
421 void netfs_clear_inode_writeback(struct inode *inode, const void *aux);
422 void netfs_invalidate_folio(struct folio *folio, size_t offset, size_t length);
423 bool netfs_release_folio(struct folio *folio, gfp_t gfp);
424 
425 /* VMA operations API. */
426 vm_fault_t netfs_page_mkwrite(struct vm_fault *vmf, struct netfs_group *netfs_group);
427 
428 /* (Sub)request management API. */
429 void netfs_read_subreq_progress(struct netfs_io_subrequest *subreq);
430 void netfs_read_subreq_terminated(struct netfs_io_subrequest *subreq);
431 void netfs_get_subrequest(struct netfs_io_subrequest *subreq,
432 			  enum netfs_sreq_ref_trace what);
433 void netfs_put_subrequest(struct netfs_io_subrequest *subreq,
434 			  enum netfs_sreq_ref_trace what);
435 ssize_t netfs_extract_user_iter(struct iov_iter *orig, size_t orig_len,
436 				struct iov_iter *new,
437 				iov_iter_extraction_t extraction_flags);
438 size_t netfs_limit_iter(const struct iov_iter *iter, size_t start_offset,
439 			size_t max_size, size_t max_segs);
440 void netfs_prepare_write_failed(struct netfs_io_subrequest *subreq);
441 void netfs_write_subrequest_terminated(void *_op, ssize_t transferred_or_error);
442 
443 int netfs_start_io_read(struct inode *inode);
444 void netfs_end_io_read(struct inode *inode);
445 int netfs_start_io_write(struct inode *inode);
446 void netfs_end_io_write(struct inode *inode);
447 int netfs_start_io_direct(struct inode *inode);
448 void netfs_end_io_direct(struct inode *inode);
449 
450 /* Miscellaneous APIs. */
451 struct folio_queue *netfs_folioq_alloc(unsigned int rreq_id, gfp_t gfp,
452 				       unsigned int trace /*enum netfs_folioq_trace*/);
453 void netfs_folioq_free(struct folio_queue *folioq,
454 		       unsigned int trace /*enum netfs_trace_folioq*/);
455 
456 /* Buffer wrangling helpers API. */
457 int netfs_alloc_folioq_buffer(struct address_space *mapping,
458 			      struct folio_queue **_buffer,
459 			      size_t *_cur_size, ssize_t size, gfp_t gfp);
460 void netfs_free_folioq_buffer(struct folio_queue *fq);
461 
462 /* Writeback exclusion API. */
463 bool netfs_wb_begin(struct netfs_inode *ictx, bool nowait);
464 void netfs_wb_end(struct netfs_inode *ictx);
465 
466 /**
467  * netfs_inode - Get the netfs inode context from the inode
468  * @inode: The inode to query
469  *
470  * Get the netfs lib inode context from the network filesystem's inode.  The
471  * context struct is expected to directly follow on from the VFS inode struct.
472  */
473 static inline struct netfs_inode *netfs_inode(struct inode *inode)
474 {
475 	return container_of(inode, struct netfs_inode, inode);
476 }
477 
478 /**
479  * netfs_read_remote_i_size - Read remote_i_size safely
480  * @inode: The inode to access
481  *
482  * Read remote_i_size safely without the potential for tearing on 32-bit
483  * arches.
484  *
485  * NOTE: in a 32bit arch with a preemptable kernel and an UP compile the
486  * i_size_read/write must be atomic with respect to the local cpu (unlike with
487  * preempt disabled), but they don't need to be atomic with respect to other
488  * cpus like in true SMP (so they need either to either locally disable irq
489  * around the read or for example on x86 they can be still implemented as a
490  * cmpxchg8b without the need of the lock prefix).  For SMP compiles and 64bit
491  * archs it makes no difference if preempt is enabled or not.
492  */
493 static inline unsigned long long netfs_read_remote_i_size(const struct inode *inode)
494 {
495 	const struct netfs_inode *ictx = container_of(inode, struct netfs_inode, inode);
496 	unsigned long long remote_i_size;
497 
498 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
499 	unsigned int seq;
500 
501 	do {
502 		seq = read_seqcount_begin(&inode->i_size_seqcount);
503 		remote_i_size = ictx->_remote_i_size;
504 	} while (read_seqcount_retry(&inode->i_size_seqcount, seq));
505 #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
506 	preempt_disable();
507 	remote_i_size = ictx->_remote_i_size;
508 	preempt_enable();
509 #else
510 	/* Pairs with smp_store_release() in netfs_write_remote_i_size() */
511 	remote_i_size = smp_load_acquire(&ictx->_remote_i_size);
512 #endif
513 	return remote_i_size;
514 }
515 
516 /*
517  * netfs_write_remote_i_size - Set remote_i_size safely
518  * @inode: The inode to access
519  * @remote_i_size: The new value for the size of the file on the server
520  *
521  * Set remote_i_size safely without the potential for tearing on 32-bit arches.
522  *
523  * Context: The caller must hold inode->i_lock.
524  *
525  * NOTE: unlike netfs_read_remote_i_size(), netfs_write_remote_i_size() does
526  * need locking around it (normally i_rwsem), otherwise on 32bit/SMP an update
527  * of i_size_seqcount can be lost, resulting in subsequent i_size_read() calls
528  * spinning forever.
529  */
530 static inline void netfs_write_remote_i_size(struct inode *inode,
531 					     unsigned long long remote_i_size)
532 {
533 	struct netfs_inode *ictx = netfs_inode(inode);
534 
535 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
536 	write_seqcount_begin(&inode->i_size_seqcount);
537 	ictx->_remote_i_size = remote_i_size;
538 	write_seqcount_end(&inode->i_size_seqcount);
539 #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
540 	preempt_disable();
541 	ictx->_remote_i_size = remote_i_size;
542 	preempt_enable();
543 #else
544 	/*
545 	 * Pairs with smp_load_acquire() in netfs_read_remote_i_size() to
546 	 * ensure changes related to inode size (such as page contents) are
547 	 * visible before we see the changed inode size.
548 	 */
549 	smp_store_release(&ictx->_remote_i_size, remote_i_size);
550 #endif
551 }
552 
553 /**
554  * netfs_read_zero_point - Read zero_point safely
555  * @inode: The inode to access
556  *
557  * Read zero_point safely without the potential for tearing on 32-bit
558  * arches.
559  *
560  * NOTE: in a 32bit arch with a preemptable kernel and an UP compile the
561  * i_size_read/write must be atomic with respect to the local cpu (unlike with
562  * preempt disabled), but they don't need to be atomic with respect to other
563  * cpus like in true SMP (so they need either to either locally disable irq
564  * around the read or for example on x86 they can be still implemented as a
565  * cmpxchg8b without the need of the lock prefix).  For SMP compiles and 64bit
566  * archs it makes no difference if preempt is enabled or not.
567  */
568 static inline unsigned long long netfs_read_zero_point(const struct inode *inode)
569 {
570 	struct netfs_inode *ictx = container_of(inode, struct netfs_inode, inode);
571 	unsigned long long zero_point;
572 
573 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
574 	unsigned int seq;
575 
576 	do {
577 		seq = read_seqcount_begin(&inode->i_size_seqcount);
578 		zero_point = ictx->_zero_point;
579 	} while (read_seqcount_retry(&inode->i_size_seqcount, seq));
580 #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
581 	preempt_disable();
582 	zero_point = ictx->_zero_point;
583 	preempt_enable();
584 #else
585 	/* Pairs with smp_store_release() in netfs_write_zero_point() */
586 	zero_point = smp_load_acquire(&ictx->_zero_point);
587 #endif
588 	return zero_point;
589 }
590 
591 /*
592  * netfs_write_zero_point - Set zero_point safely
593  * @inode: The inode to access
594  * @zero_point: The new value for the point beyond which the server has no data
595  *
596  * Set zero_point safely without the potential for tearing on 32-bit arches.
597  *
598  * Context: The caller must hold inode->i_lock.
599  *
600  * NOTE: unlike netfs_read_zero_point(), netfs_write_zero_point() does need
601  * locking around it (normally i_rwsem), otherwise on 32bit/SMP an update of
602  * i_size_seqcount can be lost, resulting in subsequent read calls spinning
603  * forever.
604  */
605 static inline void netfs_write_zero_point(struct inode *inode,
606 					  unsigned long long zero_point)
607 {
608 	struct netfs_inode *ictx = netfs_inode(inode);
609 
610 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
611 	write_seqcount_begin(&inode->i_size_seqcount);
612 	ictx->_zero_point = zero_point;
613 	write_seqcount_end(&inode->i_size_seqcount);
614 #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
615 	preempt_disable();
616 	ictx->_zero_point = zero_point;
617 	preempt_enable();
618 #else
619 	/*
620 	 * Pairs with smp_load_acquire() in netfs_read_zero_point() to
621 	 * ensure changes related to inode size (such as page contents) are
622 	 * visible before we see the changed inode size.
623 	 */
624 	smp_store_release(&ictx->_zero_point, zero_point);
625 #endif
626 }
627 
628 /**
629  * netfs_read_sizes - Read remote_i_size and zero_point safely
630  * @inode: The inode to access
631  * @i_size: Where to return the local file size.
632  * @remote_i_size: Where to return the size of the file on the server
633  * @zero_point: Where to return the the point beyond which the server has no data
634  *
635  * Read remote_i_size and zero_point safely without the potential for tearing
636  * on 32-bit arches.
637  *
638  * NOTE: in a 32bit arch with a preemptable kernel and an UP compile the
639  * i_size_read/write must be atomic with respect to the local cpu (unlike with
640  * preempt disabled), but they don't need to be atomic with respect to other
641  * cpus like in true SMP (so they need either to either locally disable irq
642  * around the read or for example on x86 they can be still implemented as a
643  * cmpxchg8b without the need of the lock prefix).  For SMP compiles and 64bit
644  * archs it makes no difference if preempt is enabled or not.
645  */
646 static inline void netfs_read_sizes(const struct inode *inode,
647 				    unsigned long long *i_size,
648 				    unsigned long long *remote_i_size,
649 				    unsigned long long *zero_point)
650 {
651 	const struct netfs_inode *ictx = container_of(inode, struct netfs_inode, inode);
652 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
653 	unsigned int seq;
654 
655 	do {
656 		seq = read_seqcount_begin(&inode->i_size_seqcount);
657 		*i_size = inode->i_size;
658 		*remote_i_size = ictx->_remote_i_size;
659 		*zero_point = ictx->_zero_point;
660 	} while (read_seqcount_retry(&inode->i_size_seqcount, seq));
661 #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
662 	preempt_disable();
663 	*i_size = inode->i_size;
664 	*remote_i_size = ictx->_remote_i_size;
665 	*zero_point = ictx->_zero_point;
666 	preempt_enable();
667 #else
668 	/* Pairs with smp_store_release() in i_size_write() */
669 	*i_size = smp_load_acquire(&inode->i_size);
670 	/* Pairs with smp_store_release() in netfs_write_remote_i_size() */
671 	*remote_i_size = smp_load_acquire(&ictx->_remote_i_size);
672 	/* Pairs with smp_store_release() in netfs_write_zero_point() */
673 	*zero_point = smp_load_acquire(&ictx->_zero_point);
674 #endif
675 }
676 
677 /*
678  * netfs_write_sizes - Set i_size, remote_i_size and zero_point safely
679  * @inode: The inode to access
680  * @i_size: The new value for the local size of the file
681  * @remote_i_size: The new value for the size of the file on the server
682  * @zero_point: The new value for the point beyond which the server has no data
683  *
684  * Set both remote_i_size and zero_point safely without the potential for
685  * tearing on 32-bit arches.
686  *
687  * Context: The caller must hold inode->i_lock.
688  *
689  * NOTE: unlike netfs_read_zero_point(), netfs_write_zero_point() does need
690  * locking around it (normally i_rwsem), otherwise on 32bit/SMP an update of
691  * i_size_seqcount can be lost, resulting in subsequent read calls spinning
692  * forever.
693  */
694 static inline void netfs_write_sizes(struct inode *inode,
695 				     unsigned long long i_size,
696 				     unsigned long long remote_i_size,
697 				     unsigned long long zero_point)
698 {
699 	struct netfs_inode *ictx = netfs_inode(inode);
700 
701 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
702 	write_seqcount_begin(&inode->i_size_seqcount);
703 	inode->i_size = i_size;
704 	ictx->_remote_i_size = remote_i_size;
705 	ictx->_zero_point = zero_point;
706 	write_seqcount_end(&inode->i_size_seqcount);
707 #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
708 	preempt_disable();
709 	inode->i_size = i_size;
710 	ictx->_remote_i_size = remote_i_size;
711 	ictx->_zero_point = zero_point;
712 	preempt_enable();
713 #else
714 	/*
715 	 * Pairs with smp_load_acquire() in i_size_read(),
716 	 * netfs_read_remote_i_size() and netfs_read_zero_point() to ensure
717 	 * changes related to inode size (such as page contents) are visible
718 	 * before we see the changed inode size.
719 	 */
720 	smp_store_release(&inode->i_size, i_size);
721 	smp_store_release(&ictx->_remote_i_size, remote_i_size);
722 	smp_store_release(&ictx->_zero_point, zero_point);
723 #endif
724 }
725 
726 /**
727  * netfs_inode_init - Initialise a netfslib inode context
728  * @ctx: The netfs inode to initialise
729  * @ops: The netfs's operations list
730  * @use_zero_point: True to use the zero_point read optimisation
731  *
732  * Initialise the netfs library context struct.  This is expected to follow on
733  * directly from the VFS inode struct.
734  */
735 static inline void netfs_inode_init(struct netfs_inode *ctx,
736 				    const struct netfs_request_ops *ops,
737 				    bool use_zero_point)
738 {
739 	ctx->ops = ops;
740 	ctx->_remote_i_size = i_size_read(&ctx->inode);
741 	ctx->_zero_point = LLONG_MAX;
742 	ctx->flags = 0;
743 	atomic_set(&ctx->io_count, 0);
744 #if IS_ENABLED(CONFIG_FSCACHE)
745 	ctx->cache = NULL;
746 #endif
747 	INIT_LIST_HEAD(&ctx->wb_queue);
748 	spin_lock_init(&ctx->lock);
749 	/* ->releasepage() drives zero_point */
750 	if (use_zero_point) {
751 		ctx->_zero_point = ctx->_remote_i_size;
752 		mapping_set_release_always(ctx->inode.i_mapping);
753 	}
754 }
755 
756 /**
757  * netfs_resize_file - Note that a file got resized
758  * @ictx: The netfs inode being resized
759  * @new_i_size: The new file size
760  * @changed_on_server: The change was applied to the server
761  *
762  * Inform the netfs lib that a file got resized so that it can adjust its state.
763  */
764 static inline void netfs_resize_file(struct netfs_inode *ictx,
765 				     unsigned long long new_i_size,
766 				     bool changed_on_server)
767 {
768 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
769 	struct inode *inode = &ictx->inode;
770 
771 	preempt_disable();
772 	write_seqcount_begin(&inode->i_size_seqcount);
773 	if (changed_on_server)
774 		ictx->_remote_i_size = new_i_size;
775 	if (new_i_size < ictx->_zero_point)
776 		ictx->_zero_point = new_i_size;
777 	write_seqcount_end(&inode->i_size_seqcount);
778 	preempt_enable();
779 #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
780 	preempt_disable();
781 	if (changed_on_server)
782 		ictx->_remote_i_size = new_i_size;
783 	if (new_i_size < ictx->_zero_point)
784 		ictx->_zero_point = new_i_size;
785 	preempt_enable();
786 #else
787 	/*
788 	 * Pairs with smp_load_acquire() in netfs_read_remote_i_size and
789 	 * netfs_read_zero_point() to ensure changes related to inode size
790 	 * (such as page contents) are visible before we see the changed inode
791 	 * size.
792 	 */
793 	if (changed_on_server)
794 		smp_store_release(&ictx->_remote_i_size, new_i_size);
795 	if (new_i_size < ictx->_zero_point)
796 		smp_store_release(&ictx->_zero_point, new_i_size);
797 #endif
798 }
799 
800 /**
801  * netfs_i_cookie - Get the cache cookie from the inode
802  * @ctx: The netfs inode to query
803  *
804  * Get the caching cookie (if enabled) from the network filesystem's inode.
805  */
806 static inline struct fscache_cookie *netfs_i_cookie(struct netfs_inode *ctx)
807 {
808 #if IS_ENABLED(CONFIG_FSCACHE)
809 	return ctx->cache;
810 #else
811 	return NULL;
812 #endif
813 }
814 
815 /**
816  * netfs_wait_for_outstanding_io - Wait for outstanding I/O to complete
817  * @inode: The netfs inode to wait on
818  *
819  * Wait for outstanding I/O requests of any type to complete.  This is intended
820  * to be called from inode eviction routines.  This makes sure that any
821  * resources held by those requests are cleaned up before we let the inode get
822  * cleaned up.
823  */
824 static inline void netfs_wait_for_outstanding_io(struct inode *inode)
825 {
826 	struct netfs_inode *ictx = netfs_inode(inode);
827 
828 	wait_var_event(&ictx->io_count, atomic_read(&ictx->io_count) == 0);
829 }
830 
831 #endif /* _LINUX_NETFS_H */
832