summaryrefslogtreecommitdiffstats
path: root/glusterfs-guts/src/guts-parse.c
blob: dd17a737ebbae9511b96131027bb4a95a9940653 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
   Copyright (c) 2008 Z RESEARCH, Inc. <http://www.zresearch.com>
   This file is part of GlusterFS.

   GlusterFS is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published
   by the Free Software Foundation; either version 3 of the License,
   or (at your option) any later version.

   GlusterFS is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "guts-parse.h"
#include "guts-tables.h"

/* unavoidable usage of global data.. :'( */
static int32_t tio_fd = 0;

int32_t
guts_tio_init (const char *filename)
{
  tio_fd = open (filename, O_WRONLY | O_CREAT);
  
  if (tio_fd < 0) {
    gf_log ("guts",
	    GF_LOG_ERROR,
	    "failed to open tio file %s", filename);
  }
  
  return tio_fd;
}

void
guts_reply_dump (fuse_req_t req,
		 const void *arg,
		 int32_t len)
{
  uint8_t *buf = NULL;
  uint8_t *ibuf = NULL;
  uint32_t buf_size = REP_HEADER_FULL_LEN + len;

  ibuf = buf = CALLOC (1, buf_size);
  
  /* being paranoid, checking for both ibuf and buf.. ;) */
  if (ibuf && buf) {
    memcpy (ibuf, REP_BEGIN, strlen (REP_BEGIN));
    ibuf += strlen (REP_BEGIN);
    memcpy (ibuf, req, sizeof (struct fuse_req));
    ibuf += sizeof (struct fuse_req);
    memcpy (ibuf, &len, sizeof (len));
    ibuf += sizeof (len);
    memcpy (ibuf, arg, len);
  
    gf_full_write (tio_fd, buf, buf_size);
    
    free (buf);
  } else {
    gf_log ("glusterfs-guts", GF_LOG_DEBUG,
	    "failed to allocate memory while dumping reply");
  }
}

void
guts_req_dump (struct fuse_in_header *in,
	       const void *arg,
	       int32_t len)
{
  /* GUTS_REQUEST_BEGIN:<fuse_in_header>:<arg-len>:<args>:GUTS_REQUEST_END */
  uint8_t *buf = NULL;
  uint8_t *ibuf = NULL;
  uint32_t buf_size = REQ_HEADER_FULL_LEN + len;

  ibuf = buf = CALLOC (1, buf_size);
  
  if (ibuf && buf) {
    memcpy (ibuf, REQ_BEGIN, strlen (REQ_BEGIN));
    ibuf += strlen (REQ_BEGIN);
    memcpy (ibuf, in, sizeof (*in));
    ibuf += sizeof (*in);
    memcpy (ibuf, &len, sizeof (len));
    ibuf += sizeof (len);
    memcpy (ibuf, arg, len);
    
    gf_full_write (tio_fd, buf, buf_size);
    
    free (buf);
  } else {
    gf_log ("glusterfs-guts", GF_LOG_DEBUG,
	    "failed to allocate memory while dumping reply");
  }
}



guts_req_t *
guts_read_entry (guts_replay_ctx_t *ctx)
{
  guts_req_t *req = NULL;
  guts_reply_t *reply = NULL;
  uint8_t begin[256] = {0,};
  int32_t ret = 0;
  int32_t fd = ctx->tio_fd;

  while (!req) {
    req = guts_get_request (ctx);
    
    if (!req) {
      ret = read (fd, begin, strlen (REQ_BEGIN));
      
      if (ret == 0) {
	gf_log ("glusterfs-guts", GF_LOG_DEBUG, 
		"guts replay finished");
	req = NULL;
      }
      
      if (is_request (begin)) {
	req = CALLOC (1, sizeof (*req));
	ERR_ABORT (req);
	gf_full_read (fd, (char *)req, REQ_HEADER_LEN);
	
	req->arg = CALLOC (1, req->arg_len + 1);
	ERR_ABORT (req->arg);
	gf_full_read (fd, req->arg, req->arg_len);
	gf_log ("guts",
		GF_LOG_DEBUG,
		"%s: fop %s (%d)\n", 
		begin, guts_log[req->header.opcode].name, req->header.opcode);
	guts_add_request (ctx, req);
	req = guts_get_request (ctx);
      } else {
	/* whenever a reply is read, we put it to a hash table and we would like to retrieve it whenever
	 * we get a reply for any call
	 */
	reply = CALLOC (1, sizeof (*reply));
	ERR_ABORT (reply);
	gf_full_read (fd, (char *)reply, REP_HEADER_LEN);
	
	reply->arg = CALLOC (1, reply->arg_len + 1);
	ERR_ABORT (reply->arg);
	gf_full_read (fd, reply->arg, reply->arg_len);
	
	/* add a new reply to */
	ret = guts_add_reply (ctx, reply);
	gf_log ("guts",
		GF_LOG_DEBUG,
		"got a reply with unique: %ld", reply->req.unique);
      }
    }
  }
  return req;
}

guts_reply_t *
guts_read_reply (guts_replay_ctx_t *ctx,
		 uint64_t unique)
{
  guts_req_t *req = NULL;
  guts_reply_t *reply = NULL, *rep = NULL;
  uint8_t begin[256] = {0,};
  int32_t ret = 0;
  int32_t fd = ctx->tio_fd;

  while (!rep) {
    
    ret = read (fd, begin, strlen (REQ_BEGIN));
    
    if (ret == 0) {
      printf ("\ndone\n");
      return NULL;
    }
    
    if (is_request (begin)) {
      req = CALLOC (1, sizeof (*req));
      ERR_ABORT (req);
      gf_full_read (fd, (char *)req, REQ_HEADER_LEN);
      
      req->arg = CALLOC (1, req->arg_len + 1);
      ERR_ABORT (req->arg);
      gf_full_read (fd, req->arg, req->arg_len);
      gf_log ("guts",
	      GF_LOG_DEBUG,
	      "%s: fop %s (%d)\n", 
	      begin, guts_log[req->header.opcode].name, req->header.opcode);
      
      ret = guts_add_request (ctx, req);
      
    } else {
      /* whenever a reply is read, we put it to a hash table and we would like to retrieve it whenever
       * we get a reply for any call
       */
      reply = CALLOC (1, sizeof (*reply));
      ERR_ABORT (reply);
      gf_full_read (fd, (char *)reply, REP_HEADER_LEN);
      
      reply->arg = CALLOC (1, reply->arg_len + 1);
      ERR_ABORT (reply->arg);
      gf_full_read (fd, reply->arg, reply->arg_len);
      
      /* add a new reply to */
      if (reply->req.unique == unique) {
	return reply;
      } else {
	ret = guts_add_reply (ctx, reply);
	gf_log ("guts",
		GF_LOG_DEBUG,
		"got a reply with unique: %ld", reply->req.unique);
      }
    }
  }
  return NULL;
}