1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /* Network filesystem support services.
4 * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
9 * Documentation/filesystems/netfs_library.rst
11 * for a description of the network filesystem interface declared here.
14 #ifndef _LINUX_NETFS_H
15 #define _LINUX_NETFS_H
17 #include <linux/workqueue.h>
19 #include <linux/pagemap.h>
22 * Overload PG_private_2 to give us PG_fscache - this is used to indicate that
23 * a page is currently backed by a local disk cache
25 #define PageFsCache(page) PagePrivate2((page))
26 #define SetPageFsCache(page) SetPagePrivate2((page))
27 #define ClearPageFsCache(page) ClearPagePrivate2((page))
28 #define TestSetPageFsCache(page) TestSetPagePrivate2((page))
29 #define TestClearPageFsCache(page) TestClearPagePrivate2((page))
32 * set_page_fscache - Set PG_fscache on a page and take a ref
35 * Set the PG_fscache (PG_private_2) flag on a page and take the reference
36 * needed for the VM to handle its lifetime correctly. This sets the flag and
37 * takes the reference unconditionally, so care must be taken not to set the
38 * flag again if it's already set.
40 static inline void set_page_fscache(struct page *page)
42 set_page_private_2(page);
46 * end_page_fscache - Clear PG_fscache and release any waiters
49 * Clear the PG_fscache (PG_private_2) bit on a page and wake up any sleepers
50 * waiting for this. The page ref held for PG_private_2 being set is released.
52 * This is, for example, used when a netfs page is being written to a local
53 * disk cache, thereby allowing writes to the cache for the same page to be
56 static inline void end_page_fscache(struct page *page)
58 end_page_private_2(page);
62 * wait_on_page_fscache - Wait for PG_fscache to be cleared on a page
63 * @page: The page to wait on
65 * Wait for PG_fscache (aka PG_private_2) to be cleared on a page.
67 static inline void wait_on_page_fscache(struct page *page)
69 wait_on_page_private_2(page);
73 * wait_on_page_fscache_killable - Wait for PG_fscache to be cleared on a page
74 * @page: The page to wait on
76 * Wait for PG_fscache (aka PG_private_2) to be cleared on a page or until a
77 * fatal signal is received by the calling task.
81 * - -EINTR if a fatal signal was encountered.
83 static inline int wait_on_page_fscache_killable(struct page *page)
85 return wait_on_page_private_2_killable(page);
88 enum netfs_read_source {
89 NETFS_FILL_WITH_ZEROES,
90 NETFS_DOWNLOAD_FROM_SERVER,
91 NETFS_READ_FROM_CACHE,
95 typedef void (*netfs_io_terminated_t)(void *priv, ssize_t transferred_or_error,
99 * Resources required to do operations on a cache.
101 struct netfs_cache_resources {
102 const struct netfs_cache_ops *ops;
108 * Descriptor for a single component subrequest.
110 struct netfs_read_subrequest {
111 struct netfs_read_request *rreq; /* Supervising read request */
112 struct list_head rreq_link; /* Link in rreq->subrequests */
113 loff_t start; /* Where to start the I/O */
114 size_t len; /* Size of the I/O */
115 size_t transferred; /* Amount of data transferred */
117 short error; /* 0 or error that occurred */
118 unsigned short debug_index; /* Index in list (for debugging output) */
119 enum netfs_read_source source; /* Where to read from */
121 #define NETFS_SREQ_WRITE_TO_CACHE 0 /* Set if should write to cache */
122 #define NETFS_SREQ_CLEAR_TAIL 1 /* Set if the rest of the read should be cleared */
123 #define NETFS_SREQ_SHORT_READ 2 /* Set if there was a short read from the cache */
124 #define NETFS_SREQ_SEEK_DATA_READ 3 /* Set if ->read() should SEEK_DATA first */
125 #define NETFS_SREQ_NO_PROGRESS 4 /* Set if we didn't manage to read any data */
129 * Descriptor for a read helper request. This is used to make multiple I/O
130 * requests on a variety of sources and then stitch the result together.
132 struct netfs_read_request {
133 struct work_struct work;
134 struct inode *inode; /* The file being accessed */
135 struct address_space *mapping; /* The mapping being accessed */
136 struct netfs_cache_resources cache_resources;
137 struct list_head subrequests; /* Requests to fetch I/O from disk or net */
138 void *netfs_priv; /* Private data for the netfs */
139 unsigned int debug_id;
140 unsigned int cookie_debug_id;
141 atomic_t nr_rd_ops; /* Number of read ops in progress */
142 atomic_t nr_wr_ops; /* Number of write ops in progress */
143 size_t submitted; /* Amount submitted for I/O so far */
144 size_t len; /* Length of the request */
145 short error; /* 0 or error that occurred */
146 loff_t i_size; /* Size of the file */
147 loff_t start; /* Start position */
148 pgoff_t no_unlock_page; /* Don't unlock this page after read */
151 #define NETFS_RREQ_INCOMPLETE_IO 0 /* Some ioreqs terminated short or with error */
152 #define NETFS_RREQ_WRITE_TO_CACHE 1 /* Need to write to the cache */
153 #define NETFS_RREQ_NO_UNLOCK_PAGE 2 /* Don't unlock no_unlock_page on completion */
154 #define NETFS_RREQ_DONT_UNLOCK_PAGES 3 /* Don't unlock the pages on completion */
155 #define NETFS_RREQ_FAILED 4 /* The request failed */
156 #define NETFS_RREQ_IN_PROGRESS 5 /* Unlocked when the request completes */
157 const struct netfs_read_request_ops *netfs_ops;
161 * Operations the network filesystem can/must provide to the helpers.
163 struct netfs_read_request_ops {
164 bool (*is_cache_enabled)(struct inode *inode);
165 void (*init_rreq)(struct netfs_read_request *rreq, struct file *file);
166 int (*begin_cache_operation)(struct netfs_read_request *rreq);
167 void (*expand_readahead)(struct netfs_read_request *rreq);
168 bool (*clamp_length)(struct netfs_read_subrequest *subreq);
169 void (*issue_op)(struct netfs_read_subrequest *subreq);
170 bool (*is_still_valid)(struct netfs_read_request *rreq);
171 int (*check_write_begin)(struct file *file, loff_t pos, unsigned len,
172 struct page *page, void **_fsdata);
173 void (*done)(struct netfs_read_request *rreq);
174 void (*cleanup)(struct address_space *mapping, void *netfs_priv);
178 * Table of operations for access to a cache. This is obtained by
179 * rreq->ops->begin_cache_operation().
181 struct netfs_cache_ops {
182 /* End an operation */
183 void (*end_operation)(struct netfs_cache_resources *cres);
185 /* Read data from the cache */
186 int (*read)(struct netfs_cache_resources *cres,
188 struct iov_iter *iter,
190 netfs_io_terminated_t term_func,
191 void *term_func_priv);
193 /* Write data to the cache */
194 int (*write)(struct netfs_cache_resources *cres,
196 struct iov_iter *iter,
197 netfs_io_terminated_t term_func,
198 void *term_func_priv);
200 /* Expand readahead request */
201 void (*expand_readahead)(struct netfs_cache_resources *cres,
202 loff_t *_start, size_t *_len, loff_t i_size);
204 /* Prepare a read operation, shortening it to a cached/uncached
205 * boundary as appropriate.
207 enum netfs_read_source (*prepare_read)(struct netfs_read_subrequest *subreq,
210 /* Prepare a write operation, working out what part of the write we can
213 int (*prepare_write)(struct netfs_cache_resources *cres,
214 loff_t *_start, size_t *_len, loff_t i_size);
217 struct readahead_control;
218 extern void netfs_readahead(struct readahead_control *,
219 const struct netfs_read_request_ops *,
221 extern int netfs_readpage(struct file *,
223 const struct netfs_read_request_ops *,
225 extern int netfs_write_begin(struct file *, struct address_space *,
226 loff_t, unsigned int, unsigned int, struct page **,
228 const struct netfs_read_request_ops *,
231 extern void netfs_subreq_terminated(struct netfs_read_subrequest *, ssize_t, bool);
232 extern void netfs_stats_show(struct seq_file *);
234 #endif /* _LINUX_NETFS_H */