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