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 21 enum netfs_sreq_ref_trace; 22 23 /* 24 * Overload PG_private_2 to give us PG_fscache - this is used to indicate that 25 * a page is currently backed by a local disk cache 26 */ 27 #define folio_test_fscache(folio) folio_test_private_2(folio) 28 #define PageFsCache(page) PagePrivate2((page)) 29 #define SetPageFsCache(page) SetPagePrivate2((page)) 30 #define ClearPageFsCache(page) ClearPagePrivate2((page)) 31 #define TestSetPageFsCache(page) TestSetPagePrivate2((page)) 32 #define TestClearPageFsCache(page) TestClearPagePrivate2((page)) 33 34 /** 35 * folio_start_fscache - Start an fscache write on a folio. 36 * @folio: The folio. 37 * 38 * Call this function before writing a folio to a local cache. Starting a 39 * second write before the first one finishes is not allowed. 40 */ 41 static inline void folio_start_fscache(struct folio *folio) 42 { 43 VM_BUG_ON_FOLIO(folio_test_private_2(folio), folio); 44 folio_get(folio); 45 folio_set_private_2(folio); 46 } 47 48 /** 49 * folio_end_fscache - End an fscache write on a folio. 50 * @folio: The folio. 51 * 52 * Call this function after the folio has been written to the local cache. 53 * This will wake any sleepers waiting on this folio. 54 */ 55 static inline void folio_end_fscache(struct folio *folio) 56 { 57 folio_end_private_2(folio); 58 } 59 60 /** 61 * folio_wait_fscache - Wait for an fscache write on this folio to end. 62 * @folio: The folio. 63 * 64 * If this folio is currently being written to a local cache, wait for 65 * the write to finish. Another write may start after this one finishes, 66 * unless the caller holds the folio lock. 67 */ 68 static inline void folio_wait_fscache(struct folio *folio) 69 { 70 folio_wait_private_2(folio); 71 } 72 73 /** 74 * folio_wait_fscache_killable - Wait for an fscache write on this folio to end. 75 * @folio: The folio. 76 * 77 * If this folio is currently being written to a local cache, wait 78 * for the write to finish or for a fatal signal to be received. 79 * Another write may start after this one finishes, unless the caller 80 * holds the folio lock. 81 * 82 * Return: 83 * - 0 if successful. 84 * - -EINTR if a fatal signal was encountered. 85 */ 86 static inline int folio_wait_fscache_killable(struct folio *folio) 87 { 88 return folio_wait_private_2_killable(folio); 89 } 90 91 static inline void set_page_fscache(struct page *page) 92 { 93 folio_start_fscache(page_folio(page)); 94 } 95 96 static inline void end_page_fscache(struct page *page) 97 { 98 folio_end_private_2(page_folio(page)); 99 } 100 101 static inline void wait_on_page_fscache(struct page *page) 102 { 103 folio_wait_private_2(page_folio(page)); 104 } 105 106 static inline int wait_on_page_fscache_killable(struct page *page) 107 { 108 return folio_wait_private_2_killable(page_folio(page)); 109 } 110 111 enum netfs_io_source { 112 NETFS_FILL_WITH_ZEROES, 113 NETFS_DOWNLOAD_FROM_SERVER, 114 NETFS_READ_FROM_CACHE, 115 NETFS_INVALID_READ, 116 } __mode(byte); 117 118 typedef void (*netfs_io_terminated_t)(void *priv, ssize_t transferred_or_error, 119 bool was_async); 120 121 /* 122 * Per-inode description. This must be directly after the inode struct. 123 */ 124 struct netfs_i_context { 125 const struct netfs_request_ops *ops; 126 #if IS_ENABLED(CONFIG_FSCACHE) 127 struct fscache_cookie *cache; 128 #endif 129 loff_t remote_i_size; /* Size of the remote file */ 130 }; 131 132 /* 133 * Resources required to do operations on a cache. 134 */ 135 struct netfs_cache_resources { 136 const struct netfs_cache_ops *ops; 137 void *cache_priv; 138 void *cache_priv2; 139 unsigned int debug_id; /* Cookie debug ID */ 140 unsigned int inval_counter; /* object->inval_counter at begin_op */ 141 }; 142 143 /* 144 * Descriptor for a single component subrequest. 145 */ 146 struct netfs_io_subrequest { 147 struct netfs_io_request *rreq; /* Supervising I/O request */ 148 struct list_head rreq_link; /* Link in rreq->subrequests */ 149 loff_t start; /* Where to start the I/O */ 150 size_t len; /* Size of the I/O */ 151 size_t transferred; /* Amount of data transferred */ 152 refcount_t ref; 153 short error; /* 0 or error that occurred */ 154 unsigned short debug_index; /* Index in list (for debugging output) */ 155 enum netfs_io_source source; /* Where to read from/write to */ 156 unsigned long flags; 157 #define NETFS_SREQ_COPY_TO_CACHE 0 /* Set if should copy the data to the cache */ 158 #define NETFS_SREQ_CLEAR_TAIL 1 /* Set if the rest of the read should be cleared */ 159 #define NETFS_SREQ_SHORT_IO 2 /* Set if the I/O was short */ 160 #define NETFS_SREQ_SEEK_DATA_READ 3 /* Set if ->read() should SEEK_DATA first */ 161 #define NETFS_SREQ_NO_PROGRESS 4 /* Set if we didn't manage to read any data */ 162 #define NETFS_SREQ_ONDEMAND 5 /* Set if it's from on-demand read mode */ 163 }; 164 165 enum netfs_io_origin { 166 NETFS_READAHEAD, /* This read was triggered by readahead */ 167 NETFS_READPAGE, /* This read is a synchronous read */ 168 NETFS_READ_FOR_WRITE, /* This read is to prepare a write */ 169 } __mode(byte); 170 171 /* 172 * Descriptor for an I/O helper request. This is used to make multiple I/O 173 * operations to a variety of data stores and then stitch the result together. 174 */ 175 struct netfs_io_request { 176 struct work_struct work; 177 struct inode *inode; /* The file being accessed */ 178 struct address_space *mapping; /* The mapping being accessed */ 179 struct netfs_cache_resources cache_resources; 180 struct list_head subrequests; /* Contributory I/O operations */ 181 void *netfs_priv; /* Private data for the netfs */ 182 unsigned int debug_id; 183 atomic_t nr_outstanding; /* Number of ops in progress */ 184 atomic_t nr_copy_ops; /* Number of copy-to-cache ops in progress */ 185 size_t submitted; /* Amount submitted for I/O so far */ 186 size_t len; /* Length of the request */ 187 short error; /* 0 or error that occurred */ 188 enum netfs_io_origin origin; /* Origin of the request */ 189 loff_t i_size; /* Size of the file */ 190 loff_t start; /* Start position */ 191 pgoff_t no_unlock_folio; /* Don't unlock this folio after read */ 192 refcount_t ref; 193 unsigned long flags; 194 #define NETFS_RREQ_INCOMPLETE_IO 0 /* Some ioreqs terminated short or with error */ 195 #define NETFS_RREQ_COPY_TO_CACHE 1 /* Need to write to the cache */ 196 #define NETFS_RREQ_NO_UNLOCK_FOLIO 2 /* Don't unlock no_unlock_folio on completion */ 197 #define NETFS_RREQ_DONT_UNLOCK_FOLIOS 3 /* Don't unlock the folios on completion */ 198 #define NETFS_RREQ_FAILED 4 /* The request failed */ 199 #define NETFS_RREQ_IN_PROGRESS 5 /* Unlocked when the request completes */ 200 const struct netfs_request_ops *netfs_ops; 201 }; 202 203 /* 204 * Operations the network filesystem can/must provide to the helpers. 205 */ 206 struct netfs_request_ops { 207 int (*init_request)(struct netfs_io_request *rreq, struct file *file); 208 int (*begin_cache_operation)(struct netfs_io_request *rreq); 209 void (*expand_readahead)(struct netfs_io_request *rreq); 210 bool (*clamp_length)(struct netfs_io_subrequest *subreq); 211 void (*issue_read)(struct netfs_io_subrequest *subreq); 212 bool (*is_still_valid)(struct netfs_io_request *rreq); 213 int (*check_write_begin)(struct file *file, loff_t pos, unsigned len, 214 struct folio *folio, void **_fsdata); 215 void (*done)(struct netfs_io_request *rreq); 216 void (*cleanup)(struct address_space *mapping, void *netfs_priv); 217 }; 218 219 /* 220 * How to handle reading from a hole. 221 */ 222 enum netfs_read_from_hole { 223 NETFS_READ_HOLE_IGNORE, 224 NETFS_READ_HOLE_CLEAR, 225 NETFS_READ_HOLE_FAIL, 226 }; 227 228 /* 229 * Table of operations for access to a cache. This is obtained by 230 * rreq->ops->begin_cache_operation(). 231 */ 232 struct netfs_cache_ops { 233 /* End an operation */ 234 void (*end_operation)(struct netfs_cache_resources *cres); 235 236 /* Read data from the cache */ 237 int (*read)(struct netfs_cache_resources *cres, 238 loff_t start_pos, 239 struct iov_iter *iter, 240 enum netfs_read_from_hole read_hole, 241 netfs_io_terminated_t term_func, 242 void *term_func_priv); 243 244 /* Write data to the cache */ 245 int (*write)(struct netfs_cache_resources *cres, 246 loff_t start_pos, 247 struct iov_iter *iter, 248 netfs_io_terminated_t term_func, 249 void *term_func_priv); 250 251 /* Expand readahead request */ 252 void (*expand_readahead)(struct netfs_cache_resources *cres, 253 loff_t *_start, size_t *_len, loff_t i_size); 254 255 /* Prepare a read operation, shortening it to a cached/uncached 256 * boundary as appropriate. 257 */ 258 enum netfs_io_source (*prepare_read)(struct netfs_io_subrequest *subreq, 259 loff_t i_size); 260 261 /* Prepare a write operation, working out what part of the write we can 262 * actually do. 263 */ 264 int (*prepare_write)(struct netfs_cache_resources *cres, 265 loff_t *_start, size_t *_len, loff_t i_size, 266 bool no_space_allocated_yet); 267 268 /* Query the occupancy of the cache in a region, returning where the 269 * next chunk of data starts and how long it is. 270 */ 271 int (*query_occupancy)(struct netfs_cache_resources *cres, 272 loff_t start, size_t len, size_t granularity, 273 loff_t *_data_start, size_t *_data_len); 274 }; 275 276 struct readahead_control; 277 extern void netfs_readahead(struct readahead_control *); 278 int netfs_read_folio(struct file *, struct folio *); 279 extern int netfs_write_begin(struct file *, struct address_space *, 280 loff_t, unsigned int, struct folio **, 281 void **); 282 283 extern void netfs_subreq_terminated(struct netfs_io_subrequest *, ssize_t, bool); 284 extern void netfs_get_subrequest(struct netfs_io_subrequest *subreq, 285 enum netfs_sreq_ref_trace what); 286 extern void netfs_put_subrequest(struct netfs_io_subrequest *subreq, 287 bool was_async, enum netfs_sreq_ref_trace what); 288 extern void netfs_stats_show(struct seq_file *); 289 290 /** 291 * netfs_i_context - Get the netfs inode context from the inode 292 * @inode: The inode to query 293 * 294 * Get the netfs lib inode context from the network filesystem's inode. The 295 * context struct is expected to directly follow on from the VFS inode struct. 296 */ 297 static inline struct netfs_i_context *netfs_i_context(struct inode *inode) 298 { 299 return (void *)inode + sizeof(*inode); 300 } 301 302 /** 303 * netfs_inode - Get the netfs inode from the inode context 304 * @ctx: The context to query 305 * 306 * Get the netfs inode from the netfs library's inode context. The VFS inode 307 * is expected to directly precede the context struct. 308 */ 309 static inline struct inode *netfs_inode(struct netfs_i_context *ctx) 310 { 311 return (void *)ctx - sizeof(struct inode); 312 } 313 314 /** 315 * netfs_i_context_init - Initialise a netfs lib context 316 * @inode: The inode with which the context is associated 317 * @ops: The netfs's operations list 318 * 319 * Initialise the netfs library context struct. This is expected to follow on 320 * directly from the VFS inode struct. 321 */ 322 static inline void netfs_i_context_init(struct inode *inode, 323 const struct netfs_request_ops *ops) 324 { 325 struct netfs_i_context *ctx = netfs_i_context(inode); 326 327 memset(ctx, 0, sizeof(*ctx)); 328 ctx->ops = ops; 329 ctx->remote_i_size = i_size_read(inode); 330 } 331 332 /** 333 * netfs_resize_file - Note that a file got resized 334 * @inode: The inode being resized 335 * @new_i_size: The new file size 336 * 337 * Inform the netfs lib that a file got resized so that it can adjust its state. 338 */ 339 static inline void netfs_resize_file(struct inode *inode, loff_t new_i_size) 340 { 341 struct netfs_i_context *ctx = netfs_i_context(inode); 342 343 ctx->remote_i_size = new_i_size; 344 } 345 346 /** 347 * netfs_i_cookie - Get the cache cookie from the inode 348 * @inode: The inode to query 349 * 350 * Get the caching cookie (if enabled) from the network filesystem's inode. 351 */ 352 static inline struct fscache_cookie *netfs_i_cookie(struct inode *inode) 353 { 354 #if IS_ENABLED(CONFIG_FSCACHE) 355 struct netfs_i_context *ctx = netfs_i_context(inode); 356 return ctx->cache; 357 #else 358 return NULL; 359 #endif 360 } 361 362 #endif /* _LINUX_NETFS_H */ 363