| 1 |
|
| 2 | |
| 3 | *
|
| 4 | * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
|
| 5 | * Written by David Howells ([email protected])
|
| 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 |
|
| 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;
|
| 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;
|
| 66 | uoff_t _remote_i_size;
|
| 67 | uoff_t _zero_point; |
| 68 | * on the server
|
| 69 | spinlock_t lock;
|
| 70 | atomic_t io_count;
|
| 71 | unsigned long flags;
|
| 72 | #define NETFS_ICTX_ODIRECT 0
|
| 73 | #define NETFS_ICTX_UNBUFFERED 1
|
| 74 | #define NETFS_ICTX_WB_LOCK 2
|
| 75 | #define NETFS_ICTX_MODIFIED_ATTR 3
|
| 76 | #define NETFS_ICTX_SINGLE_NO_UPLOAD 4
|
| 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;
|
| 95 | unsigned int dirty_offset;
|
| 96 | unsigned int dirty_len;
|
| 97 | };
|
| 98 | #define NETFS_FOLIO_INFO 0x1UL
|
| 99 | #define NETFS_FOLIO_COPY_TO_CACHE ((struct netfs_group *)0x356UL)
|
| 100 |
|
| 101 | static inline bool netfs_is_folio_info(const void *priv)
|
| 102 | {
|
| 103 | return (unsigned long)priv & NETFS_FOLIO_INFO;
|
| 104 | }
|
| 105 |
|
| 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 |
|
| 113 | static inline struct netfs_folio *netfs_folio_info(struct folio *folio)
|
| 114 | {
|
| 115 | return __netfs_folio_info(folio_get_private(folio));
|
| 116 | }
|
| 117 |
|
| 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,
|
| 131 | NETFS_CACHE_COLLECT_WRITE_DATA,
|
| 132 | NETFS_CACHE_COLLECT_WRITE_CANCEL,
|
| 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 |
|
| 142 | struct netfs_io_subrequest *construct;
|
| 143 | size_t sreq_max_len;
|
| 144 | unsigned int sreq_max_segs;
|
| 145 | unsigned int submit_off;
|
| 146 | unsigned int submit_len;
|
| 147 | unsigned int submit_extendable_to;
|
| 148 | void (*prepare_write)(struct netfs_io_subrequest *subreq);
|
| 149 | void (*issue_write)(struct netfs_io_subrequest *subreq);
|
| 150 |
|
| 151 | struct list_head subrequests;
|
| 152 | uoff_t collected_to;
|
| 153 | size_t transferred;
|
| 154 | unsigned short error;
|
| 155 | enum netfs_io_source source;
|
| 156 | unsigned char stream_nr;
|
| 157 | bool avail;
|
| 158 | bool active;
|
| 159 | bool need_retry;
|
| 160 | bool failed;
|
| 161 | bool transferred_valid;
|
| 162 | enum netfs_cache_collect cache_collect;
|
| 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;
|
| 173 | unsigned int cookie_id;
|
| 174 | unsigned int object_id;
|
| 175 | unsigned int inval_counter;
|
| 176 | unsigned int dio_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;
|
| 188 | struct work_struct work;
|
| 189 | struct list_head rreq_link;
|
| 190 | struct iov_iter io_iter;
|
| 191 | uoff_t start;
|
| 192 | size_t len;
|
| 193 | size_t transferred;
|
| 194 | refcount_t ref;
|
| 195 | short error;
|
| 196 | unsigned short debug_index;
|
| 197 | unsigned int nr_segs;
|
| 198 | u8 retry_count;
|
| 199 | enum netfs_io_source source;
|
| 200 | unsigned char stream_nr;
|
| 201 | unsigned long flags;
|
| 202 | #define NETFS_SREQ_COPY_TO_CACHE 0
|
| 203 | #define NETFS_SREQ_CLEAR_TAIL 1
|
| 204 | #define NETFS_SREQ_MADE_PROGRESS 4
|
| 205 | #define NETFS_SREQ_BOUNDARY 6
|
| 206 | #define NETFS_SREQ_HIT_EOF 7
|
| 207 | #define NETFS_SREQ_IN_PROGRESS 8
|
| 208 | #define NETFS_SREQ_NEED_RETRY 9
|
| 209 | #define NETFS_SREQ_FAILED 10
|
| 210 | #define NETFS_SREQ_CANCELLED 11
|
| 211 | };
|
| 212 |
|
| 213 | enum netfs_io_origin {
|
| 214 | NETFS_READAHEAD,
|
| 215 | NETFS_READPAGE,
|
| 216 | NETFS_READ_GAPS,
|
| 217 | NETFS_READ_SINGLE,
|
| 218 | NETFS_READ_FOR_WRITE,
|
| 219 | NETFS_UNBUFFERED_READ,
|
| 220 | NETFS_DIO_READ,
|
| 221 | NETFS_WRITEBACK,
|
| 222 | NETFS_WRITEBACK_SINGLE,
|
| 223 | NETFS_UNBUFFERED_WRITE,
|
| 224 | NETFS_DIO_WRITE,
|
| 225 | NETFS_PGPRIV2_COPY_TO_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;
|
| 236 | struct rcu_head rcu;
|
| 237 | };
|
| 238 | struct work_struct work;
|
| 239 | struct inode *inode;
|
| 240 | struct address_space *mapping;
|
| 241 | struct kiocb *iocb;
|
| 242 | struct netfs_cache_resources cache_resources;
|
| 243 | struct netfs_io_request *copy_to_cache;
|
| 244 | #ifdef CONFIG_PROC_FS
|
| 245 | struct list_head proc_link;
|
| 246 | #endif
|
| 247 | struct netfs_io_stream io_streams[2];
|
| 248 | #define NR_IO_STREAMS 2
|
| 249 | struct netfs_group *group;
|
| 250 | struct rolling_buffer 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;
|
| 254 | void *netfs_priv;
|
| 255 | void *netfs_priv2;
|
| 256 | struct bio_vec *direct_bv;
|
| 257 | uoff_t submitted;
|
| 258 | uoff_t len;
|
| 259 | size_t transferred;
|
| 260 | size_t progress_at;
|
| 261 | long error;
|
| 262 | uoff_t i_size;
|
| 263 | uoff_t start;
|
| 264 | atomic64_t issued_to;
|
| 265 | uoff_t collected_to;
|
| 266 | uoff_t cache_coll_to;
|
| 267 | uoff_t cleaned_to;
|
| 268 | uoff_t abandon_to;
|
| 269 | const struct folio *no_unlock_folio;
|
| 270 | gfp_t gfp;
|
| 271 | unsigned int direct_bv_count;
|
| 272 | unsigned int debug_id;
|
| 273 | unsigned int rsize;
|
| 274 | unsigned int wsize;
|
| 275 | atomic_t subreq_counter;
|
| 276 | unsigned int nr_group_rel;
|
| 277 | spinlock_t lock;
|
| 278 | enum netfs_io_origin origin;
|
| 279 | bool direct_bv_unpin;
|
| 280 | refcount_t ref;
|
| 281 | unsigned long flags;
|
| 282 | #define NETFS_RREQ_IN_PROGRESS 0
|
| 283 | #define NETFS_RREQ_ALL_QUEUED 1
|
| 284 | #define NETFS_RREQ_PAUSE 2
|
| 285 | #define NETFS_RREQ_FAILED 3
|
| 286 | #define NETFS_RREQ_RETRYING 4
|
| 287 | #define NETFS_RREQ_SHORT_TRANSFER 5
|
| 288 | #define NETFS_RREQ_CACHE_STOP 8
|
| 289 | #define NETFS_RREQ_CACHE_ERROR 9
|
| 290 | #define NETFS_RREQ_CANCEL_CACHING 10
|
| 291 | #define NETFS_RREQ_OFFLOAD_COLLECTION 12
|
| 292 | #define NETFS_RREQ_NO_UNLOCK_FOLIO 13
|
| 293 | #define NETFS_RREQ_UPLOAD_TO_SERVER 14
|
| 294 | #define NETFS_RREQ_USE_IO_ITER 15
|
| 295 | #define NETFS_RREQ_NEED_PUT_RA_REFS 17
|
| 296 | #ifdef CONFIG_NETFS_PGPRIV2
|
| 297 | #define NETFS_RREQ_USE_PGPRIV2 31 |
| 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 |
|
| 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 |
|
| 323 | void (*update_i_size)(struct inode *inode, uoff_t i_size);
|
| 324 | void (*post_modify)(struct inode *inode);
|
| 325 |
|
| 326 |
|
| 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 |
|
| 347 | void (*end_operation)(struct netfs_cache_resources *cres);
|
| 348 |
|
| 349 |
|
| 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 |
|
| 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 |
|
| 365 | void (*issue_write)(struct netfs_io_subrequest *subreq);
|
| 366 |
|
| 367 |
|
| 368 | void (*expand_readahead)(struct netfs_cache_resources *cres,
|
| 369 | uoff_t *_start,
|
| 370 | uoff_t *_len,
|
| 371 | uoff_t i_size);
|
| 372 |
|
| 373 | |
| 374 | * boundary as appropriate.
|
| 375 |
|
| 376 | int (*prepare_read)(struct netfs_io_subrequest *subreq);
|
| 377 |
|
| 378 | |
| 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 | |
| 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 | |
| 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 | |
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 450 | vm_fault_t netfs_page_mkwrite(struct vm_fault *vmf, struct netfs_group *netfs_group);
|
| 451 |
|
| 452 |
|
| 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 |
|
| 475 | struct folio_queue *netfs_folioq_alloc(unsigned int rreq_id, gfp_t gfp,
|
| 476 | unsigned int trace );
|
| 477 | void netfs_folioq_free(struct folio_queue *folioq,
|
| 478 | unsigned int trace );
|
| 479 |
|
| 480 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 693 | *i_size = smp_load_acquire(&inode->i_size);
|
| 694 |
|
| 695 | *remote_i_size = smp_load_acquire(&ictx->_remote_i_size);
|
| 696 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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
|