0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef _LINUX_NETFS_H
0015 #define _LINUX_NETFS_H
0016
0017 #include <linux/workqueue.h>
0018 #include <linux/fs.h>
0019 #include <linux/pagemap.h>
0020
0021 enum netfs_sreq_ref_trace;
0022
0023
0024
0025
0026
0027 #define folio_test_fscache(folio) folio_test_private_2(folio)
0028 #define PageFsCache(page) PagePrivate2((page))
0029 #define SetPageFsCache(page) SetPagePrivate2((page))
0030 #define ClearPageFsCache(page) ClearPagePrivate2((page))
0031 #define TestSetPageFsCache(page) TestSetPagePrivate2((page))
0032 #define TestClearPageFsCache(page) TestClearPagePrivate2((page))
0033
0034
0035
0036
0037
0038
0039
0040
0041 static inline void folio_start_fscache(struct folio *folio)
0042 {
0043 VM_BUG_ON_FOLIO(folio_test_private_2(folio), folio);
0044 folio_get(folio);
0045 folio_set_private_2(folio);
0046 }
0047
0048
0049
0050
0051
0052
0053
0054
0055 static inline void folio_end_fscache(struct folio *folio)
0056 {
0057 folio_end_private_2(folio);
0058 }
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068 static inline void folio_wait_fscache(struct folio *folio)
0069 {
0070 folio_wait_private_2(folio);
0071 }
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086 static inline int folio_wait_fscache_killable(struct folio *folio)
0087 {
0088 return folio_wait_private_2_killable(folio);
0089 }
0090
0091 static inline void set_page_fscache(struct page *page)
0092 {
0093 folio_start_fscache(page_folio(page));
0094 }
0095
0096 static inline void end_page_fscache(struct page *page)
0097 {
0098 folio_end_private_2(page_folio(page));
0099 }
0100
0101 static inline void wait_on_page_fscache(struct page *page)
0102 {
0103 folio_wait_private_2(page_folio(page));
0104 }
0105
0106 static inline int wait_on_page_fscache_killable(struct page *page)
0107 {
0108 return folio_wait_private_2_killable(page_folio(page));
0109 }
0110
0111 enum netfs_io_source {
0112 NETFS_FILL_WITH_ZEROES,
0113 NETFS_DOWNLOAD_FROM_SERVER,
0114 NETFS_READ_FROM_CACHE,
0115 NETFS_INVALID_READ,
0116 } __mode(byte);
0117
0118 typedef void (*netfs_io_terminated_t)(void *priv, ssize_t transferred_or_error,
0119 bool was_async);
0120
0121
0122
0123
0124 struct netfs_inode {
0125 struct inode inode;
0126 const struct netfs_request_ops *ops;
0127 #if IS_ENABLED(CONFIG_FSCACHE)
0128 struct fscache_cookie *cache;
0129 #endif
0130 loff_t remote_i_size;
0131 };
0132
0133
0134
0135
0136 struct netfs_cache_resources {
0137 const struct netfs_cache_ops *ops;
0138 void *cache_priv;
0139 void *cache_priv2;
0140 unsigned int debug_id;
0141 unsigned int inval_counter;
0142 };
0143
0144
0145
0146
0147 struct netfs_io_subrequest {
0148 struct netfs_io_request *rreq;
0149 struct list_head rreq_link;
0150 loff_t start;
0151 size_t len;
0152 size_t transferred;
0153 refcount_t ref;
0154 short error;
0155 unsigned short debug_index;
0156 enum netfs_io_source source;
0157 unsigned long flags;
0158 #define NETFS_SREQ_COPY_TO_CACHE 0
0159 #define NETFS_SREQ_CLEAR_TAIL 1
0160 #define NETFS_SREQ_SHORT_IO 2
0161 #define NETFS_SREQ_SEEK_DATA_READ 3
0162 #define NETFS_SREQ_NO_PROGRESS 4
0163 #define NETFS_SREQ_ONDEMAND 5
0164 };
0165
0166 enum netfs_io_origin {
0167 NETFS_READAHEAD,
0168 NETFS_READPAGE,
0169 NETFS_READ_FOR_WRITE,
0170 } __mode(byte);
0171
0172
0173
0174
0175
0176 struct netfs_io_request {
0177 struct work_struct work;
0178 struct inode *inode;
0179 struct address_space *mapping;
0180 struct netfs_cache_resources cache_resources;
0181 struct list_head subrequests;
0182 void *netfs_priv;
0183 unsigned int debug_id;
0184 atomic_t nr_outstanding;
0185 atomic_t nr_copy_ops;
0186 size_t submitted;
0187 size_t len;
0188 short error;
0189 enum netfs_io_origin origin;
0190 loff_t i_size;
0191 loff_t start;
0192 pgoff_t no_unlock_folio;
0193 refcount_t ref;
0194 unsigned long flags;
0195 #define NETFS_RREQ_INCOMPLETE_IO 0
0196 #define NETFS_RREQ_COPY_TO_CACHE 1
0197 #define NETFS_RREQ_NO_UNLOCK_FOLIO 2
0198 #define NETFS_RREQ_DONT_UNLOCK_FOLIOS 3
0199 #define NETFS_RREQ_FAILED 4
0200 #define NETFS_RREQ_IN_PROGRESS 5
0201 const struct netfs_request_ops *netfs_ops;
0202 };
0203
0204
0205
0206
0207 struct netfs_request_ops {
0208 int (*init_request)(struct netfs_io_request *rreq, struct file *file);
0209 void (*free_request)(struct netfs_io_request *rreq);
0210 int (*begin_cache_operation)(struct netfs_io_request *rreq);
0211
0212 void (*expand_readahead)(struct netfs_io_request *rreq);
0213 bool (*clamp_length)(struct netfs_io_subrequest *subreq);
0214 void (*issue_read)(struct netfs_io_subrequest *subreq);
0215 bool (*is_still_valid)(struct netfs_io_request *rreq);
0216 int (*check_write_begin)(struct file *file, loff_t pos, unsigned len,
0217 struct folio **foliop, void **_fsdata);
0218 void (*done)(struct netfs_io_request *rreq);
0219 };
0220
0221
0222
0223
0224 enum netfs_read_from_hole {
0225 NETFS_READ_HOLE_IGNORE,
0226 NETFS_READ_HOLE_CLEAR,
0227 NETFS_READ_HOLE_FAIL,
0228 };
0229
0230
0231
0232
0233
0234 struct netfs_cache_ops {
0235
0236 void (*end_operation)(struct netfs_cache_resources *cres);
0237
0238
0239 int (*read)(struct netfs_cache_resources *cres,
0240 loff_t start_pos,
0241 struct iov_iter *iter,
0242 enum netfs_read_from_hole read_hole,
0243 netfs_io_terminated_t term_func,
0244 void *term_func_priv);
0245
0246
0247 int (*write)(struct netfs_cache_resources *cres,
0248 loff_t start_pos,
0249 struct iov_iter *iter,
0250 netfs_io_terminated_t term_func,
0251 void *term_func_priv);
0252
0253
0254 void (*expand_readahead)(struct netfs_cache_resources *cres,
0255 loff_t *_start, size_t *_len, loff_t i_size);
0256
0257
0258
0259
0260 enum netfs_io_source (*prepare_read)(struct netfs_io_subrequest *subreq,
0261 loff_t i_size);
0262
0263
0264
0265
0266 int (*prepare_write)(struct netfs_cache_resources *cres,
0267 loff_t *_start, size_t *_len, loff_t i_size,
0268 bool no_space_allocated_yet);
0269
0270
0271
0272
0273 int (*query_occupancy)(struct netfs_cache_resources *cres,
0274 loff_t start, size_t len, size_t granularity,
0275 loff_t *_data_start, size_t *_data_len);
0276 };
0277
0278 struct readahead_control;
0279 void netfs_readahead(struct readahead_control *);
0280 int netfs_read_folio(struct file *, struct folio *);
0281 int netfs_write_begin(struct netfs_inode *, struct file *,
0282 struct address_space *, loff_t pos, unsigned int len,
0283 struct folio **, void **fsdata);
0284
0285 void netfs_subreq_terminated(struct netfs_io_subrequest *, ssize_t, bool);
0286 void netfs_get_subrequest(struct netfs_io_subrequest *subreq,
0287 enum netfs_sreq_ref_trace what);
0288 void netfs_put_subrequest(struct netfs_io_subrequest *subreq,
0289 bool was_async, enum netfs_sreq_ref_trace what);
0290 void netfs_stats_show(struct seq_file *);
0291
0292
0293
0294
0295
0296
0297
0298
0299 static inline struct netfs_inode *netfs_inode(struct inode *inode)
0300 {
0301 return container_of(inode, struct netfs_inode, inode);
0302 }
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312 static inline void netfs_inode_init(struct netfs_inode *ctx,
0313 const struct netfs_request_ops *ops)
0314 {
0315 ctx->ops = ops;
0316 ctx->remote_i_size = i_size_read(&ctx->inode);
0317 #if IS_ENABLED(CONFIG_FSCACHE)
0318 ctx->cache = NULL;
0319 #endif
0320 }
0321
0322
0323
0324
0325
0326
0327
0328
0329 static inline void netfs_resize_file(struct netfs_inode *ctx, loff_t new_i_size)
0330 {
0331 ctx->remote_i_size = new_i_size;
0332 }
0333
0334
0335
0336
0337
0338
0339
0340 static inline struct fscache_cookie *netfs_i_cookie(struct netfs_inode *ctx)
0341 {
0342 #if IS_ENABLED(CONFIG_FSCACHE)
0343 return ctx->cache;
0344 #else
0345 return NULL;
0346 #endif
0347 }
0348
0349 #endif