s3:mdssvc: mds_dalloc_dump() -> dalloc_dump()
[garming/samba-autobuild/.git] / source3 / rpc_server / mdssvc / mdssvc.h
1 /*
2    Unix SMB/CIFS implementation.
3    Main metadata server / Spotlight routines
4
5    Copyright (C) Ralph Boehme                   2012-2014
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #ifndef _MDSSVC_H
22 #define _MDSSVC_H
23
24 #include "dalloc.h"
25 #include "marshalling.h"
26 #include "lib/util/dlinklist.h"
27 #include "librpc/gen_ndr/mdssvc.h"
28
29 /*
30  * glib uses TRUE and FALSE which was redefined by "includes.h" to be
31  * unusable, undefine so glib can establish its own working
32  * replacement.
33  */
34 #undef TRUE
35 #undef FALSE
36
37 #define MAX_SL_FRAGMENT_SIZE 0xFFFFF
38 #define MAX_SL_RESULTS 100
39 #define MAX_SL_RUNTIME 30
40 #define MDS_TRACKER_ASYNC_TIMEOUT_MS 250
41
42 #define SLQ_DEBUG(lvl, _slq, state) do { if (CHECK_DEBUGLVL(lvl)) {     \
43         const struct sl_query *__slq = _slq;                            \
44         struct timeval_buf start_buf;                                   \
45         const char *start;                                              \
46         struct timeval_buf last_used_buf;                               \
47         const char *last_used;                                          \
48         struct timeval_buf expire_buf;                                  \
49         const char *expire;                                             \
50         start = timeval_str_buf(&__slq->start_time, false,              \
51                                 true, &start_buf);                      \
52         last_used = timeval_str_buf(&__slq->last_used, false,           \
53                                     true, &last_used_buf);              \
54         expire = timeval_str_buf(&__slq->expire_time, false,            \
55                                  true, &expire_buf);                    \
56         DEBUG(lvl,("%s slq[0x%jx,0x%jx], start: %s, last_used: %s, "    \
57                    "expires: %s, query: '%s'\n", state,                 \
58                    (uintmax_t)__slq->ctx1, (uintmax_t)__slq->ctx2,      \
59                    start, last_used, expire, __slq->query_string));     \
60 }} while(0)
61
62 /******************************************************************************
63  * Some helper stuff dealing with queries
64  ******************************************************************************/
65
66 /* query state */
67 typedef enum {
68         SLQ_STATE_NEW,       /* Query received from client         */
69         SLQ_STATE_RUNNING,   /* Query dispatched to Tracker        */
70         SLQ_STATE_RESULTS,   /* Async Tracker query read           */
71         SLQ_STATE_FULL,      /* the max amount of result has beed queued */
72         SLQ_STATE_DONE,      /* Got all results from Tracker       */
73         SLQ_STATE_END,       /* Query results returned to client   */
74         SLQ_STATE_ERROR      /* an error happended somewhere       */
75 } slq_state_t;
76
77 /* query structure */
78 struct sl_query {
79         struct sl_query *prev, *next;    /* list pointers */
80         struct mds_ctx  *mds_ctx;        /* context handle */
81         void            *backend_private; /* search backend private data */
82         slq_state_t      state;          /* query state */
83         struct timeval   start_time;     /* Query start time */
84         struct timeval   last_used;      /* Time of last result fetch */
85         struct timeval   expire_time;    /* Query expiration time */
86         struct tevent_timer *te;         /* query timeout */
87         int              snum;           /* share snum  */
88         uint64_t         ctx1;           /* client context 1 */
89         uint64_t         ctx2;           /* client context 2 */
90         sl_array_t      *reqinfo;        /* array with requested metadata */
91         char            *query_string;   /* the Spotlight query string */
92         uint64_t        *cnids;          /* restrict query to these CNIDs */
93         size_t           cnids_num;      /* Size of slq_cnids array */
94         const char      *path_scope;     /* path to directory to search */
95         struct sl_rslts *query_results;  /* query results */
96         TALLOC_CTX      *entries_ctx;    /* talloc parent of the search results */
97 };
98
99 struct sl_rslts {
100         int                num_results;
101         sl_cnids_t        *cnids;
102         sl_array_t        *fm_array;
103 };
104
105 struct sl_inode_path_map {
106         struct mds_ctx    *mds_ctx;
107         uint64_t           ino;
108         char              *path;
109 };
110
111 /* Per process state */
112 struct mdssvc_ctx {
113         struct tevent_context *ev_ctx;
114         void *backend_private;
115 };
116
117 /* Per tree connect state */
118 struct mds_ctx {
119         struct mdssvc_backend *backend;
120         struct mdssvc_ctx *mdssvc_ctx;
121         void *backend_private;
122         struct auth_session_info *pipe_session_info;
123         struct dom_sid sid;
124         uid_t uid;
125         smb_iconv_t ic_nfc_to_nfd;
126         smb_iconv_t ic_nfd_to_nfc;
127         int snum;
128         const char *sharename;
129         const char *spath;
130         struct sl_query *query_list;     /* list of active queries */
131         struct db_context *ino_path_map; /* dbwrap rbt for storing inode->path mappings */
132 };
133
134 struct mdssvc_backend {
135         bool (*init)(struct mdssvc_ctx *mdssvc_ctx);
136         bool (*connect)(struct mds_ctx *mds_ctx);
137         bool (*search_map)(struct sl_query *slq);
138         bool (*search_start)(struct sl_query *slq);
139         bool (*search_cont)(struct sl_query *slq);
140         bool (*shutdown)(struct mdssvc_ctx *mdssvc_ctx);
141 };
142
143 /******************************************************************************
144  * Function declarations
145  ******************************************************************************/
146
147 /*
148  * mdssvc.c
149  */
150 extern bool mds_init(struct messaging_context *msg_ctx);
151 extern bool mds_shutdown(void);
152 struct mds_ctx *mds_init_ctx(TALLOC_CTX *mem_ctx,
153                              struct tevent_context *ev,
154                              struct auth_session_info *session_info,
155                              int snum,
156                              const char *sharename,
157                              const char *path);
158 extern bool mds_dispatch(struct mds_ctx *query_ctx,
159                          struct mdssvc_blob *request_blob,
160                          struct mdssvc_blob *response_blob);
161 bool mds_add_result(struct sl_query *slq, const char *path);
162
163 #endif /* _MDSSVC_H */