r13255: New CIFS dd client for use in performance testing. The guts of this is
[ira/wip.git] / source4 / client / cifsddio.c
1 /*
2    CIFSDD - dd for SMB.
3    IO routines, generic and specific.
4
5    Copyright (C) James Peach 2005-2006
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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/libcli.h"
26 #include "lib/cmdline/popt_common.h"
27
28 #include "cifsdd.h"
29
30 /* ------------------------------------------------------------------------- */
31 /* UNIX file descriptor IO.                                                  */
32 /* ------------------------------------------------------------------------- */
33
34 struct fd_handle
35 {
36         struct dd_iohandle      h;
37         int                     fd;
38 };
39
40 #define IO_HANDLE_TO_FD(h) (((struct fd_handle *)(h))->fd)
41
42 static BOOL fd_seek_func(void * handle, uint64_t offset)
43 {
44         ssize_t ret;
45
46         ret = lseek(IO_HANDLE_TO_FD(handle), offset, SEEK_SET);
47         if (ret < 0) {
48                 fprintf(stderr, "%s: seek failed: %s\n",
49                                 PROGNAME, strerror(errno));
50                 return(False);
51         }
52
53         return(True);
54 }
55
56 static BOOL fd_read_func(void * handle, uint8_t * buf, uint64_t wanted, uint64_t * actual)
57 {
58         ssize_t ret;
59
60         ret = read(IO_HANDLE_TO_FD(handle), buf, wanted);
61         if (ret < 0) {
62                 fprintf(stderr, "%s: %llu byte read failed: %s\n",
63                                 PROGNAME, (unsigned long long)wanted, strerror(errno));
64                 return(False);
65         }
66
67         *actual = (uint64_t)ret;
68         return(True);
69 }
70
71 static BOOL
72 fd_write_func(void * handle, uint8_t * buf, uint64_t wanted, uint64_t * actual)
73 {
74         ssize_t ret;
75
76         ret = write(IO_HANDLE_TO_FD(handle), buf, wanted);
77         if (ret < 0) {
78                 fprintf(stderr, "%s: %llu byte write failed: %s\n",
79                                 PROGNAME, (unsigned long long)wanted, strerror(errno));
80                 return(False);
81         }
82
83         *actual = (uint64_t)ret;
84         return(True);
85 }
86
87 static struct dd_iohandle *
88 open_fd_handle(const char * path, uint64_t iosz, int options)
89 {
90         struct fd_handle * fdh;
91         int oflags = 0;
92
93         DEBUG(4, ("opening fd stream for %s\n", path));
94         if ((fdh = talloc_zero(NULL, struct fd_handle)) == NULL) {
95                 return(NULL);
96         }
97
98         fdh->h.io_read = fd_read_func;
99         fdh->h.io_write = fd_write_func;
100         fdh->h.io_seek = fd_seek_func;
101
102         if (options & DD_DIRECT_IO)
103                 oflags |= O_DIRECT;
104
105         if (options & DD_SYNC_IO)
106                 oflags |= O_SYNC;
107
108         oflags |= (options & DD_WRITE) ?  (O_WRONLY | O_CREAT) : (O_RDONLY);
109
110         fdh->fd = open(path, oflags, 0644);
111         if (fdh->fd < 0) {
112                 fprintf(stderr, "%s: %s: %s\n",
113                         PROGNAME, path, strerror(errno));
114                 talloc_free(fdh);
115                 return(NULL);
116         }
117
118         if (options & DD_OPLOCK) {
119                 DEBUG(2, ("FIXME: take local oplock on %s\n", path));
120         }
121
122         SMB_ASSERT((void *)fdh == (void *)&fdh->h);
123         return(&fdh->h);
124 }
125
126 /* ------------------------------------------------------------------------- */
127 /* CIFS client IO.                                                           */
128 /* ------------------------------------------------------------------------- */
129
130 struct smb_handle
131 {
132         struct dd_iohandle      h;
133         struct smbcli_state *   cli;
134         int                     fnum;
135         uint64_t                offset;
136 };
137
138 #define IO_HANDLE_TO_SMB(h) ((struct smb_handle *)(h))
139
140 BOOL smb_seek_func(void * handle, uint64_t offset)
141 {
142         IO_HANDLE_TO_SMB(handle)->offset = offset;
143         return(True);
144 }
145
146 BOOL smb_read_func(void * handle, uint8_t * buf,
147                                 uint64_t wanted, uint64_t * actual)
148 {
149         NTSTATUS                ret;
150         union smb_read          r;
151         struct smb_handle *     smbh;
152
153         ZERO_STRUCT(r);
154         smbh = IO_HANDLE_TO_SMB(handle);
155
156         r.generic.level = RAW_READ_READX;
157         r.readx.in.fnum = smbh->fnum;
158         r.readx.in.offset = smbh->offset;
159         r.readx.in.mincnt = wanted;
160         r.readx.in.maxcnt = wanted;
161         r.readx.out.data = buf;
162
163         /* FIXME: Should I really set readx.in.remaining? That just seems
164          * redundant.
165          */
166         ret = smb_raw_read(smbh->cli->tree, &r);
167         if (!NT_STATUS_IS_OK(ret)) {
168                 fprintf(stderr, "%s: %llu byte read failed: %s\n",
169                                 PROGNAME, (unsigned long long)wanted, nt_errstr(ret));
170                 return(False);
171         }
172
173         /* Trap integer wrap. */
174         SMB_ASSERT((smbh->offset + r.readx.out.nread) >= smbh->offset);
175
176         *actual = r.readx.out.nread;
177         smbh->offset += r.readx.out.nread;
178         return(True);
179 }
180
181 BOOL smb_write_func(void * handle, uint8_t * buf,
182                                 uint64_t wanted, uint64_t * actual)
183 {
184         NTSTATUS                ret;
185         union smb_write         w;
186         struct smb_handle *     smbh;
187
188         ZERO_STRUCT(w);
189         smbh = IO_HANDLE_TO_SMB(handle);
190
191         w.generic.level = RAW_WRITE_WRITEX;
192         w.writex.in.fnum = smbh->fnum;
193         w.writex.in.offset = smbh->offset;
194         w.writex.in.count = wanted;
195         w.writex.in.data = buf;
196
197         ret = smb_raw_write(smbh->cli->tree, &w);
198         if (!NT_STATUS_IS_OK(ret)) {
199                 fprintf(stderr, "%s: %llu byte write failed: %s\n",
200                                 PROGNAME, (unsigned long long)wanted, nt_errstr(ret));
201                 return(False);
202         }
203
204         *actual = w.writex.out.nwritten;
205         smbh->offset += w.writex.out.nwritten;
206         return(True);
207 }
208
209 static struct smbcli_state * init_smb_session(const char * host, const char * share)
210 {
211         NTSTATUS                ret;
212         struct smbcli_state *   cli = NULL;
213
214         /* When we support SMB URLs, we can get different user credentials for
215          * each connection, but for now, we just use the same one for both.
216          */
217         ret = smbcli_full_connection(NULL, &cli, host, share,
218                          NULL /* devtype */, cmdline_credentials, NULL /* events */);
219
220         if (!NT_STATUS_IS_OK(ret)) {
221                 fprintf(stderr, "%s: connecting to //%s/%s: %s\n",
222                         PROGNAME, host, share, nt_errstr(ret));
223                 return(NULL);
224         }
225
226         return(cli);
227 }
228
229 static int open_smb_file(struct smbcli_state * cli, const char * path, int options)
230 {
231         NTSTATUS        ret;
232         union smb_open  o;
233
234         ZERO_STRUCT(o);
235
236         o.ntcreatex.level = RAW_OPEN_NTCREATEX;
237         o.ntcreatex.in.fname = path;
238
239         /* TODO: It's not clear whether to use these flags or to use the
240          * similarly named NTCREATEX flags in the create_options field.
241          */
242         if (options & DD_DIRECT_IO)
243                 o.ntcreatex.in.flags |= FILE_FLAG_NO_BUFFERING;
244
245         if (options & DD_SYNC_IO)
246                 o.ntcreatex.in.flags |= FILE_FLAG_WRITE_THROUGH;
247
248         o.ntcreatex.in.access_mask |=
249                 (options & DD_WRITE) ? SEC_FILE_WRITE_DATA
250                                         : SEC_FILE_READ_DATA;
251
252         /* Try to create the file only if we will be writing to it. */
253         o.ntcreatex.in.open_disposition =
254                 (options & DD_WRITE) ? NTCREATEX_DISP_OPEN_IF
255                                         : NTCREATEX_DISP_OPEN;
256
257         o.ntcreatex.in.share_access =
258                 NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
259
260         if (options & DD_OPLOCK) {
261                 o.ntcreatex.in.flags |= NTCREATEX_FLAGS_REQUEST_OPLOCK;
262         }
263
264         ret = smb_raw_open(cli->tree, NULL, &o);
265         if (!NT_STATUS_IS_OK(ret)) {
266                 fprintf(stderr, "%s: opening %s: %s\n",
267                         PROGNAME, path, nt_errstr(ret));
268                 return(-1);
269         }
270
271         return(o.ntcreatex.out.fnum);
272 }
273
274 static struct dd_iohandle * open_smb_handle(const char * host, const char * share,
275                                         const char * path, uint64_t iosz, int options)
276 {
277         struct smb_handle * smbh;
278
279         DEBUG(4, ("opening SMB stream to //%s/%s for %s\n",
280                 host, share, path));
281
282         if ((smbh = talloc_zero(NULL, struct smb_handle)) == NULL) {
283                 return(NULL);
284         }
285
286         smbh->h.io_read = smb_read_func;
287         smbh->h.io_write = smb_write_func;
288         smbh->h.io_seek = smb_seek_func;
289
290         if ((smbh->cli = init_smb_session(host, share)) == NULL) {
291                 return(NULL);
292         }
293
294         DEBUG(4, ("connected to //%s/%s with xmit size of %u bytes\n",
295                 host, share, smbh->cli->transport->negotiate.max_xmit));
296
297         smbh->fnum = open_smb_file(smbh->cli, path, options);
298         return(&smbh->h);
299 }
300
301 /* ------------------------------------------------------------------------- */
302 /* Abstract IO interface.                                                    */
303 /* ------------------------------------------------------------------------- */
304
305 struct dd_iohandle * dd_open_path(const char * path, uint64_t iosz, int options)
306 {
307         if (file_exist(path)) {
308                 return(open_fd_handle(path, iosz, options));
309         } else {
310                 char * host;
311                 char * share;
312
313                 if (smbcli_parse_unc(path, NULL, &host, &share)) {
314                         const char * remain;
315                         remain = strstr(path, share) + strlen(share);
316
317                         /* Skip over leading directory separators. */
318                         while (*remain == '/' || *remain == '\\') { remain++; }
319
320                         return(open_smb_handle(host, share, remain,
321                                                 iosz, options));
322                 }
323
324                 return(open_fd_handle(path, iosz, options));
325         }
326 }
327
328 /* Fill the buffer till it has at least needsz bytes. Use read operations of
329  * blocksz bytes. Return the number of bytes read and fill bufsz with the new
330  * buffer size.
331  *
332  * NOTE: The IO buffer is guaranteed to be big enough to fit needsz + blocksz
333  * bytes into it.
334  */
335 BOOL dd_fill_block(struct dd_iohandle * h, uint8_t * buf,
336                 uint64_t * bufsz, uint64_t needsz, uint64_t blocksz)
337 {
338         uint64_t readsz;
339
340         SMB_ASSERT(blocksz > 0);
341         SMB_ASSERT(needsz > 0);
342
343         while (*bufsz < needsz) {
344
345                 if (!h->io_read(h, buf + (*bufsz), blocksz, &readsz)) {
346                         return(False);
347                 }
348
349                 if (readsz == 0) {
350                         h->io_flags |= DD_END_OF_FILE;
351                         break;
352                 }
353
354                 DEBUG(6, ("added %llu bytes to IO buffer (need %llu bytes)\n",
355                         (unsigned long long)readsz, (unsigned long long)needsz));
356
357                 *bufsz += readsz;
358                 dd_stats.in.bytes += readsz;
359
360                 if (readsz == blocksz) {
361                         dd_stats.in.fblocks++;
362                 } else {
363                         DEBUG(3, ("partial read of %llu bytes (expected %llu)\n",
364                                 (unsigned long long)readsz, (unsigned long long)blocksz));
365                         dd_stats.in.pblocks++;
366                 }
367         }
368
369         return(True);
370 }
371
372 /* Flush a buffer that contains bufsz bytes. Use writes of blocksz to do it,
373  * and shift any remaining bytes back to the head of the buffer when there are
374  * no more blocksz sized IOs left.
375  */
376 BOOL dd_flush_block(struct dd_iohandle * h, uint8_t * buf,
377                 uint64_t * bufsz, uint64_t blocksz)
378 {
379         uint64_t writesz;
380         uint64_t totalsz = 0;
381
382         SMB_ASSERT(blocksz > 0);
383
384         /* We have explicitly been asked to write a partial block. */
385         if ((*bufsz) < blocksz) {
386
387                 if (!h->io_write(h, buf, *bufsz, &writesz)) {
388                         return(False);
389                 }
390
391                 if (writesz == 0) {
392                         fprintf(stderr, "%s: unexpectedly wrote 0 bytes\n",
393                                         PROGNAME);
394                         return(False);
395                 }
396
397                 totalsz += writesz;
398                 dd_stats.out.bytes += writesz;
399                 dd_stats.out.pblocks++;
400         }
401
402         /* Write as many full blocks as there are in the buffer. */
403         while (((*bufsz) - totalsz) >= blocksz) {
404
405                 if (!h->io_write(h, buf + totalsz, blocksz, &writesz)) {
406                         return(False);
407                 }
408
409                 if (writesz == 0) {
410                         fprintf(stderr, "%s: unexpectedly wrote 0 bytes\n",
411                                         PROGNAME);
412                         return(False);
413                 }
414
415                 if (writesz == blocksz) {
416                         dd_stats.out.fblocks++;
417                 } else {
418                         dd_stats.out.pblocks++;
419                 }
420
421                 totalsz += writesz;
422                 dd_stats.out.bytes += writesz;
423
424                 DEBUG(6, ("flushed %llu bytes from IO buffer of %llu bytes (%llu remain)\n",
425                         (unsigned long long)blocksz, (unsigned long long)blocksz,
426                         (unsigned long long)(blocksz - totalsz)));
427         }
428
429         SMB_ASSERT(totalsz > 0);
430
431         /* We have flushed as much of the IO buffer as we can while
432          * still doing blocksz-sized operations. Shift any remaining data
433          * to the front of the IO buffer.
434          */
435         if ((*bufsz) > totalsz) {
436                 uint64_t remain = (*bufsz) - totalsz;
437
438                 DEBUG(3, ("shifting %llu remainder bytes to IO buffer head\n",
439                         (unsigned long long)remain));
440
441                 memmove(buf, buf + totalsz, remain);
442                 (*bufsz) = remain;
443         } else if ((*bufsz) == totalsz) {
444                 (*bufsz) = 0;
445         } else {
446                 /* Else buffer contains bufsz bytes that we will append
447                  * to next time round.
448                  */
449                 DEBUG(3, ("%llu unflushed bytes left in IO buffer\n",
450                         (unsigned long long)(*bufsz)));
451         }
452
453         return(True);
454 }
455
456 /* vim: set sw=8 sts=8 ts=8 tw=79 : */