/* SPDX-License-Identifier: GPL-2.0-or-later */


#ifndef _LINUX_NETFS_H
#define _LINUX_NETFS_H

#include <linux/workqueue.h>
#include <linux/fs.h>
#include <linux/pagemap.h>


#define PageFsCache(page)		PagePrivate2((page))
#define SetPageFsCache(page)		SetPagePrivate2((page))
#define ClearPageFsCache(page)		ClearPagePrivate2((page))
#define TestSetPageFsCache(page)	TestSetPagePrivate2((page))
#define TestClearPageFsCache(page)	TestClearPagePrivate2((page))


static inline void set_page_fscache(struct page *page)
{
	set_page_private_2(page);
}


static inline void end_page_fscache(struct page *page)
{
	end_page_private_2(page);
}


static inline void wait_on_page_fscache(struct page *page)
{
	wait_on_page_private_2(page);
}


static inline int wait_on_page_fscache_killable(struct page *page)
{
	return wait_on_page_private_2_killable(page);
}

enum netfs_read_source {
	NETFS_FILL_WITH_ZEROES,
	NETFS_DOWNLOAD_FROM_SERVER,
	NETFS_READ_FROM_CACHE,
	NETFS_INVALID_READ,
} __mode(byte);

typedef void (*netfs_io_terminated_t)(void *priv, ssize_t transferred_or_error,
				      bool was_async);


struct netfs_cache_resources {
	const struct netfs_cache_ops	*ops;
	void				*cache_priv;
	void				*cache_priv2;
	unsigned int			debug_id;	
};


struct netfs_read_subrequest {
	struct netfs_read_request *rreq;	
	struct list_head	rreq_link;	
	loff_t			start;		
	size_t			len;		
	size_t			transferred;	
	refcount_t		usage;
	short			error;		
	unsigned short		debug_index;	
	enum netfs_read_source	source;		
	unsigned long		flags;
#define NETFS_SREQ_WRITE_TO_CACHE	0	
#define NETFS_SREQ_CLEAR_TAIL		1	
#define NETFS_SREQ_SHORT_READ		2	
#define NETFS_SREQ_SEEK_DATA_READ	3	
#define NETFS_SREQ_NO_PROGRESS		4	
};


struct netfs_read_request {
	struct work_struct	work;
	struct inode		*inode;		
	struct address_space	*mapping;	
	struct netfs_cache_resources cache_resources;
	struct list_head	subrequests;	
	void			*netfs_priv;	
	unsigned int		debug_id;
	atomic_t		nr_rd_ops;	
	atomic_t		nr_wr_ops;	
	size_t			submitted;	
	size_t			len;		
	short			error;		
	loff_t			i_size;		
	loff_t			start;		
	pgoff_t			no_unlock_page;	
	refcount_t		usage;
	unsigned long		flags;
#define NETFS_RREQ_INCOMPLETE_IO	0	
#define NETFS_RREQ_WRITE_TO_CACHE	1	
#define NETFS_RREQ_NO_UNLOCK_PAGE	2	
#define NETFS_RREQ_DONT_UNLOCK_PAGES	3	
#define NETFS_RREQ_FAILED		4	
#define NETFS_RREQ_IN_PROGRESS		5	
	const struct netfs_read_request_ops *netfs_ops;
};


struct netfs_read_request_ops {
	bool (*is_cache_enabled)(struct inode *inode);
	void (*init_rreq)(struct netfs_read_request *rreq, struct file *file);
	int (*begin_cache_operation)(struct netfs_read_request *rreq);
	void (*expand_readahead)(struct netfs_read_request *rreq);
	bool (*clamp_length)(struct netfs_read_subrequest *subreq);
	void (*issue_op)(struct netfs_read_subrequest *subreq);
	bool (*is_still_valid)(struct netfs_read_request *rreq);
	int (*check_write_begin)(struct file *file, loff_t pos, unsigned len,
				 struct page *page, void **_fsdata);
	void (*done)(struct netfs_read_request *rreq);
	void (*cleanup)(struct address_space *mapping, void *netfs_priv);
};


struct netfs_cache_ops {
	
	void (*end_operation)(struct netfs_cache_resources *cres);

	
	int (*read)(struct netfs_cache_resources *cres,
		    loff_t start_pos,
		    struct iov_iter *iter,
		    bool seek_data,
		    netfs_io_terminated_t term_func,
		    void *term_func_priv);

	
	int (*write)(struct netfs_cache_resources *cres,
		     loff_t start_pos,
		     struct iov_iter *iter,
		     netfs_io_terminated_t term_func,
		     void *term_func_priv);

	
	void (*expand_readahead)(struct netfs_cache_resources *cres,
				 loff_t *_start, size_t *_len, loff_t i_size);

	
	enum netfs_read_source (*prepare_read)(struct netfs_read_subrequest *subreq,
					       loff_t i_size);

	
	int (*prepare_write)(struct netfs_cache_resources *cres,
			     loff_t *_start, size_t *_len, loff_t i_size);
};

struct readahead_control;
extern void netfs_readahead(struct readahead_control *,
			    const struct netfs_read_request_ops *,
			    void *);
extern int netfs_readpage(struct file *,
			  struct page *,
			  const struct netfs_read_request_ops *,
			  void *);
extern int netfs_write_begin(struct file *, struct address_space *,
			     loff_t, unsigned int, unsigned int, struct page **,
			     void **,
			     const struct netfs_read_request_ops *,
			     void *);

extern void netfs_subreq_terminated(struct netfs_read_subrequest *, ssize_t, bool);
extern void netfs_stats_show(struct seq_file *);

#endif 
