Add fake-validate option
[Samba/vfs_proxy.git] / source4 / ntvfs / proxy / lib / cache / cache.h
blob823e08127c9721bab4aaff799ece581dd8527092
1 /*
2 Unix SMB/PROXY simplistic cache engine
4 Copyright (C) 2007 Sam Liddicott <sam@liddicott.com>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #ifndef CACHE_H
22 #include <sys/types.h>
23 #include "ntvfs/ntvfs.h"
25 #define CACHE_DEBUG_LEVEL 1
27 enum cache_actions {
28 #define CACHE_NONE 0/* can't cache, won't cache */
29 CACHE_FILL_bit, /* populate the cache but don't read from it */
30 #define CACHE_FILL (1 << CACHE_FILL_bit)
31 CACHE_READ_bit, /* read from the cache as well as write to it */
32 #define CACHE_READ (1 << CACHE_READ_bit)
33 CACHE_READ_AHEAD_bit, /* fill by read-ahead */
34 #define CACHE_READ_AHEAD (1 << CACHE_READ_AHEAD_bit)
35 CACHE_VALIDATE_bit, /* bulk re-validate existing cache */
36 #define CACHE_VALIDATE (1 << CACHE_VALIDATE_bit)
39 typedef uint8_t cache_state;
41 #define CACHE_FILLING(cache) !!(cache && cache->status & CACHE_FILL)
43 /* cache file entry, pointed to by the backend file struct, e.g. proxy_file
44 * Contains information pertinent to the cachability of the file */
45 struct proxy_file;
47 struct cache_context {
48 char* root;
49 char* server;
50 char* share;
51 char* prefix;
54 struct cache_file_entry {
55 struct cache_context* context;
56 struct proxy_file* f;
57 int fd;
58 cache_state status; /* current state of this cache file */
59 ssize_t readahead_window; /* how much to read-ahead by */
60 ssize_t readahead_extent; /* the extent of read-aheads requested */
61 ssize_t validated_extent; /* the extent we can read from in the cache */
62 uint16_t smbpid; /* supposed to be some kind of session number, unique per tree-connect at any instant */
63 char *cache_name;
64 char *pathname;
67 struct cache_context *new_cache_context(TALLOC_CTX * memctx, const char* root, const char *server, const char *share);
68 void cache_file_stale(struct cache_file_entry *cache);
69 #define cache_handle_novalidate(handle) do { if (handle->cache) handle->cache->status&=~CACHE_VALIDATE; } while(0)
70 #define cache_handle_stale(handle) do { if (handle->cache) cache_file_stale(handle->cache); } while(0)
71 void cache_new_file(struct cache_file_entry * cache, struct proxy_file *f, const char* filename, int readahead, bool oplock);
72 void cache_file_state(struct cache_file_entry * cache, cache_state state);
73 #define cache_handle_validated(handle, offset) do \
74 { if (handle && handle->cache) { \
75 cache_validated(handle->cache, offset); \
76 } } while (0);
77 void cache_validated(struct cache_file_entry *cache, off_t offset);
78 #define cache_handle_save(handle, data, length, offset) do \
79 { if (handle && handle->cache) { \
80 cache_save(handle->cache, data, length, offset); \
81 } } while (0);
82 void cache_save(struct cache_file_entry *cache, const uint8_t* data, ssize_t size, off_t offset);
83 /* read from cache. If valided is not null then *validated is set to the extent
84 of the data from the cache that is valid.
85 If *validated is 0 then the read data needs validating
86 If it is readx.out.nread then it is all valid.
87 If it is somewhere in between, the caller may choose to truncate then
88 read to the validated section, or re-validate the whole lot */
89 NTSTATUS cache_smb_raw_read(struct cache_file_entry *cache,
90 struct ntvfs_module_context *ntvfs,
91 struct ntvfs_request *req,
92 union smb_read *io,
93 ssize_t* validated);
94 /* read from the cache, returning the checksum of what was read */
95 NTSTATUS cache_smb_raw_checksum(struct cache_file_entry *cache,
96 offset_t offset, ssize_t* length, uint8_t digest[16]);
97 ssize_t cache_raw_read(struct cache_file_entry *cache, uint8_t* data, off_t offset, ssize_t size);
98 #define cache_handle_len(handle) ((handle && handle->cache)?(cache_len(handle->cache)):(-1))
99 ssize_t cache_len(struct cache_file_entry *cache);
100 struct cache_file_entry * cache_filename_open(struct cache_context *cache_context,
101 struct proxy_file *f,
102 const char* filename,
103 bool oplock,
104 int readahead_window);
105 struct cache_file_entry * cache_fileid_open(struct cache_context *cache_context,
106 struct proxy_file *f,
107 const uint64_t* id,
108 bool oplock,
109 int readahead_window);
110 void cache_close(struct cache_file_entry *cache);
111 void cache_beopen(struct cache_file_entry *cache);
112 void cache_reopen(struct cache_file_entry *cache);
113 void cache_md5sum(uint8_t digest[16], uint8_t *data, ssize_t size);
115 #endif /* CACHE_H */