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