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