c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen#ifndef FS_API_PRIVATE_H
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen#define FS_API_PRIVATE_H
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen#include "fs-api.h"
edfd009103881de283e146609f691281aba150aeTimo Sirainen#include "fs-wrapper.h"
ea19bfdfc2205af178a70915054c809ed2b8709eTimo Sirainen#include "module-context.h"
ea19bfdfc2205af178a70915054c809ed2b8709eTimo Sirainen
48eb19f796671260c0c77d3c61a5aeb33c9a63b4Timo Sirainen#include <sys/time.h>
48eb19f796671260c0c77d3c61a5aeb33c9a63b4Timo Sirainen
ea19bfdfc2205af178a70915054c809ed2b8709eTimo Sirainenstruct fs_api_module_register {
ea19bfdfc2205af178a70915054c809ed2b8709eTimo Sirainen unsigned int id;
ea19bfdfc2205af178a70915054c809ed2b8709eTimo Sirainen};
ea19bfdfc2205af178a70915054c809ed2b8709eTimo Sirainen
ea19bfdfc2205af178a70915054c809ed2b8709eTimo Sirainenunion fs_api_module_context {
ea19bfdfc2205af178a70915054c809ed2b8709eTimo Sirainen struct fs_api_module_register *reg;
ea19bfdfc2205af178a70915054c809ed2b8709eTimo Sirainen};
ea19bfdfc2205af178a70915054c809ed2b8709eTimo Sirainen
ea19bfdfc2205af178a70915054c809ed2b8709eTimo Sirainenextern struct fs_api_module_register fs_api_module_register;
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainenstruct fs_vfuncs {
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen struct fs *(*alloc)(void);
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen int (*init)(struct fs *fs, const char *args,
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen const struct fs_settings *set);
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen void (*deinit)(struct fs *fs);
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen enum fs_properties (*get_properties)(struct fs *fs);
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen
86ad48a2e740bd63a9b7299d7f3e229831d9b303Timo Sirainen struct fs_file *(*file_alloc)(void);
86ad48a2e740bd63a9b7299d7f3e229831d9b303Timo Sirainen void (*file_init)(struct fs_file *file, const char *path,
86ad48a2e740bd63a9b7299d7f3e229831d9b303Timo Sirainen enum fs_open_mode mode, enum fs_open_flags flags);
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen void (*file_deinit)(struct fs_file *file);
0b32a8d139f6a4f2b18a6444fc66d31b4a1b0da6Timo Sirainen void (*file_close)(struct fs_file *file);
3dcb9fd82c1edd40bea1ad572ed39f024686e463Timo Sirainen const char *(*get_path)(struct fs_file *file);
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen void (*set_async_callback)(struct fs_file *file,
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen fs_file_async_callback_t *callback,
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen void *context);
d34b8a75f3b74e26adc85b6436d033b1dcfaf9daTimo Sirainen void (*wait_async)(struct fs *fs);
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen void (*set_metadata)(struct fs_file *file, const char *key,
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen const char *value);
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen int (*get_metadata)(struct fs_file *file,
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen const ARRAY_TYPE(fs_metadata) **metadata_r);
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen bool (*prefetch)(struct fs_file *file, uoff_t length);
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen ssize_t (*read)(struct fs_file *file, void *buf, size_t size);
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen struct istream *(*read_stream)(struct fs_file *file,
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen size_t max_buffer_size);
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen int (*write)(struct fs_file *file, const void *data, size_t size);
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen void (*write_stream)(struct fs_file *file);
a0cf7d3924dec409e6eadaf3237fcecd4194ec75Timo Sirainen /* After write_stream_finish() is called once, all the following
a0cf7d3924dec409e6eadaf3237fcecd4194ec75Timo Sirainen (async) calls will have success==TRUE. */
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen int (*write_stream_finish)(struct fs_file *file, bool success);
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen int (*lock)(struct fs_file *file, unsigned int secs,
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen struct fs_lock **lock_r);
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen void (*unlock)(struct fs_lock *lock);
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen int (*exists)(struct fs_file *file);
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen int (*stat)(struct fs_file *file, struct stat *st_r);
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen int (*copy)(struct fs_file *src, struct fs_file *dest);
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen int (*rename)(struct fs_file *src, struct fs_file *dest);
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen int (*delete_file)(struct fs_file *file);
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen
4d71d712b42285afbd2a997f3408142ef0d5f899Timo Sirainen struct fs_iter *(*iter_alloc)(void);
4d71d712b42285afbd2a997f3408142ef0d5f899Timo Sirainen void (*iter_init)(struct fs_iter *iter, const char *path,
4d71d712b42285afbd2a997f3408142ef0d5f899Timo Sirainen enum fs_iter_flags flags);
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen const char *(*iter_next)(struct fs_iter *iter);
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen int (*iter_deinit)(struct fs_iter *iter);
8296531314913c7f9d4ab1857c6f79ff1308a12fTimo Sirainen
8296531314913c7f9d4ab1857c6f79ff1308a12fTimo Sirainen bool (*switch_ioloop)(struct fs *fs);
a63cd84128875485e40ed804dbf0b0945526989cTimo Sirainen int (*get_nlinks)(struct fs_file *file, nlink_t *nlinks_r);
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen};
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainenstruct fs {
c4e3c997c04eb6f93f782c5fa53354390ed07a02Timo Sirainen struct fs *parent; /* for wrapper filesystems */
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen const char *name;
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen struct fs_vfuncs v;
a06af8e117e14e2ddc5835bcbe0d2f0370cbc0a1Timo Sirainen char *temp_path_prefix;
e853125eceafe8c3e286ce9b4176f6df4b806aceTimo Sirainen int refcount;
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen
02cc28525a97ee60cb512c8eb15908082743bfe9Timo Sirainen char *username, *session_id;
02cc28525a97ee60cb512c8eb15908082743bfe9Timo Sirainen
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen struct fs_settings set;
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen string_t *last_error;
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen
5804ddddad82319839785885fdacbba60e0c9581Timo Sirainen /* may be used by fs_wait_async() to do the waiting */
5804ddddad82319839785885fdacbba60e0c9581Timo Sirainen struct ioloop *wait_ioloop, *prev_ioloop;
5804ddddad82319839785885fdacbba60e0c9581Timo Sirainen
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen unsigned int files_open_count;
92093e6d8bdddeac59e169bcde6d0080a17e66efTimo Sirainen struct fs_file *files;
e13e798694ba1b0d0a5cf471600168eeaa8d2af4Timo Sirainen struct fs_iter *iters;
fc59aba4be5d2f57908e84f0f7477c467a72d567Timo Sirainen struct event *event;
302167a8a77cf3597f0b33e644a1805aa3313a8cTimo Sirainen
302167a8a77cf3597f0b33e644a1805aa3313a8cTimo Sirainen struct fs_stats stats;
ea19bfdfc2205af178a70915054c809ed2b8709eTimo Sirainen
ea19bfdfc2205af178a70915054c809ed2b8709eTimo Sirainen ARRAY(union fs_api_module_context *) module_contexts;
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen};
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainenstruct fs_file {
e13e798694ba1b0d0a5cf471600168eeaa8d2af4Timo Sirainen /* linked list of all files */
92093e6d8bdddeac59e169bcde6d0080a17e66efTimo Sirainen struct fs_file *prev, *next;
92093e6d8bdddeac59e169bcde6d0080a17e66efTimo Sirainen
edfd009103881de283e146609f691281aba150aeTimo Sirainen struct fs_file *parent; /* for wrapper filesystems */
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen struct fs *fs;
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen struct ostream *output;
fc59aba4be5d2f57908e84f0f7477c467a72d567Timo Sirainen struct event *event;
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen char *path;
a06af8e117e14e2ddc5835bcbe0d2f0370cbc0a1Timo Sirainen enum fs_open_flags flags;
251457b81928bab1ad25193e8a09bda829abe9eeTimo Sirainen
4948718872b7dacb239e7db20393cd232bf4568bTimo Sirainen struct istream *seekable_input;
251457b81928bab1ad25193e8a09bda829abe9eeTimo Sirainen struct istream *pending_read_input;
84669c712403b742cc07ae70229725c486ef1235Timo Sirainen
67a163f3a07593446fab1cbbb8f92a89d4c6cb57Timo Sirainen const struct hash_method *write_digest_method;
67a163f3a07593446fab1cbbb8f92a89d4c6cb57Timo Sirainen void *write_digest;
67a163f3a07593446fab1cbbb8f92a89d4c6cb57Timo Sirainen
84669c712403b742cc07ae70229725c486ef1235Timo Sirainen pool_t metadata_pool;
84669c712403b742cc07ae70229725c486ef1235Timo Sirainen ARRAY_TYPE(fs_metadata) metadata;
84669c712403b742cc07ae70229725c486ef1235Timo Sirainen
84669c712403b742cc07ae70229725c486ef1235Timo Sirainen struct fs_file *copy_src;
84669c712403b742cc07ae70229725c486ef1235Timo Sirainen struct istream *copy_input;
84669c712403b742cc07ae70229725c486ef1235Timo Sirainen struct ostream *copy_output;
392538eef147981f6d818cd14cabc94cf8049d8eTimo Sirainen
1c244f6fdbb509cca857982368f5d426e999f2d1Timo Sirainen struct timeval timing_start[FS_OP_COUNT];
1c244f6fdbb509cca857982368f5d426e999f2d1Timo Sirainen
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool write_pending:1;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool writing_stream:1;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool metadata_changed:1;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool read_or_prefetch_counted:1;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool lookup_metadata_counted:1;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool stat_counted:1;
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen bool istream_open:1;
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen};
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainenstruct fs_lock {
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen struct fs_file *file;
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen};
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainenstruct fs_iter {
e13e798694ba1b0d0a5cf471600168eeaa8d2af4Timo Sirainen /* linked list of all iters */
e13e798694ba1b0d0a5cf471600168eeaa8d2af4Timo Sirainen struct fs_iter *prev, *next;
e13e798694ba1b0d0a5cf471600168eeaa8d2af4Timo Sirainen
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen struct fs *fs;
fc59aba4be5d2f57908e84f0f7477c467a72d567Timo Sirainen struct event *event;
63e94e8f57920b510fcdc479c5482ba9f4337a5cTimo Sirainen enum fs_iter_flags flags;
1c244f6fdbb509cca857982368f5d426e999f2d1Timo Sirainen struct timeval start_time;
9c45f33d0e0ca0b8f87f9a3318dd505a78fd198eTimo Sirainen
9c45f33d0e0ca0b8f87f9a3318dd505a78fd198eTimo Sirainen bool async_have_more;
9c45f33d0e0ca0b8f87f9a3318dd505a78fd198eTimo Sirainen fs_file_async_callback_t *async_callback;
9c45f33d0e0ca0b8f87f9a3318dd505a78fd198eTimo Sirainen void *async_context;
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen};
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen
6c0d8dc7cad70a336b01bd1978b3eee84a63b3b7Timo Sirainenextern const struct fs fs_class_dict;
6e8fce0589289d10e6dcd9b71fde763492bb29b8Timo Sirainenextern const struct fs fs_class_posix;
5a441a6d97225cbdc67dad7bdd80de3c7bfedaabTimo Sirainenextern const struct fs fs_class_randomfail;
84669c712403b742cc07ae70229725c486ef1235Timo Sirainenextern const struct fs fs_class_metawrap;
6e8fce0589289d10e6dcd9b71fde763492bb29b8Timo Sirainenextern const struct fs fs_class_sis;
6e8fce0589289d10e6dcd9b71fde763492bb29b8Timo Sirainenextern const struct fs fs_class_sis_queue;
140afad728d78c56fb4c0256f94eb81a213688dbTimo Sirainenextern const struct fs fs_class_test;
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen
60eda49183dbec5f3d5ec18b38433581e28e2ebaTimo Sirainenvoid fs_class_register(const struct fs *fs_class);
60eda49183dbec5f3d5ec18b38433581e28e2ebaTimo Sirainen
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainenvoid fs_set_error(struct fs *fs, const char *fmt, ...) ATTR_FORMAT(2, 3);
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainenvoid fs_set_critical(struct fs *fs, const char *fmt, ...) ATTR_FORMAT(2, 3);
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainenvoid fs_set_error_async(struct fs *fs);
c59b9c273b41f7bcf51f6803110b67813879ff05Timo Sirainen
84669c712403b742cc07ae70229725c486ef1235Timo Sirainenssize_t fs_read_via_stream(struct fs_file *file, void *buf, size_t size);
84669c712403b742cc07ae70229725c486ef1235Timo Sirainenint fs_write_via_stream(struct fs_file *file, const void *data, size_t size);
84669c712403b742cc07ae70229725c486ef1235Timo Sirainenvoid fs_metadata_init(struct fs_file *file);
185f63aeffefe857e823bc4eeaea53898f574d57Timo Sirainenvoid fs_metadata_init_or_clear(struct fs_file *file);
84669c712403b742cc07ae70229725c486ef1235Timo Sirainenvoid fs_default_set_metadata(struct fs_file *file,
84669c712403b742cc07ae70229725c486ef1235Timo Sirainen const char *key, const char *value);
5b302b077a61204beb6852b89927952840458ccbTimo Sirainenconst char *fs_metadata_find(const ARRAY_TYPE(fs_metadata) *metadata,
5b302b077a61204beb6852b89927952840458ccbTimo Sirainen const char *key);
84669c712403b742cc07ae70229725c486ef1235Timo Sirainenint fs_default_copy(struct fs_file *src, struct fs_file *dest);
84669c712403b742cc07ae70229725c486ef1235Timo Sirainen
1c244f6fdbb509cca857982368f5d426e999f2d1Timo Sirainenvoid fs_file_timing_end(struct fs_file *file, enum fs_op op);
1c244f6fdbb509cca857982368f5d426e999f2d1Timo Sirainen
6d1218e68ce883735ffde9d7907e626ab81b9fb5Timo Sirainenstruct fs_file *
6d1218e68ce883735ffde9d7907e626ab81b9fb5Timo Sirainenfs_file_init_parent(struct fs_file *parent, const char *path, int mode_flags);
6d1218e68ce883735ffde9d7907e626ab81b9fb5Timo Sirainenstruct fs_iter *
6d1218e68ce883735ffde9d7907e626ab81b9fb5Timo Sirainenfs_iter_init_parent(struct fs_iter *parent,
6d1218e68ce883735ffde9d7907e626ab81b9fb5Timo Sirainen const char *path, enum fs_iter_flags flags);
6d1218e68ce883735ffde9d7907e626ab81b9fb5Timo Sirainen
bd7ae8952d41fc1e847098439690355b09920618Timo Sirainen/* Same as fs_write_stream_abort_error(), except it closes the *parent* file
bd7ae8952d41fc1e847098439690355b09920618Timo Sirainen and error is left untouched */
bd7ae8952d41fc1e847098439690355b09920618Timo Sirainenvoid fs_write_stream_abort_parent(struct fs_file *file, struct ostream **output);
bd7ae8952d41fc1e847098439690355b09920618Timo Sirainen
c33d3f93abf8392fdc60e12bea41ffd12cc85a8dTimo Sirainen#endif