r18121: Simplify m4 code, hopefully fix Samba4 build problems.
[jelmer/samba4-debian.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 cifs_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 cifs_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 cifs_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.file.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 cifs_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.file.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.file.fnum);
294 }
295
296 static struct dd_iohandle * open_cifs_handle(const char * host,
297                                         const char * share,
298                                         const char * path,
299                                         uint64_t io_size,
300                                         int options)
301 {
302         struct cifs_handle * smbh;
303
304         if (path == NULL  || *path == '\0') {
305                 fprintf(stderr, "%s: missing path name within share //%s/%s\n",
306                         PROGNAME, host, share);
307         }
308
309         DEBUG(4, ("opening SMB stream to //%s/%s for %s\n",
310                 host, share, path));
311
312         if ((smbh = talloc_zero(NULL, struct cifs_handle)) == NULL) {
313                 return(NULL);
314         }
315
316         smbh->h.io_read = smb_read_func;
317         smbh->h.io_write = smb_write_func;
318         smbh->h.io_seek = smb_seek_func;
319
320         if ((smbh->cli = init_smb_session(host, share)) == NULL) {
321                 return(NULL);
322         }
323
324         DEBUG(4, ("connected to //%s/%s with xmit size of %u bytes\n",
325                 host, share, smbh->cli->transport->negotiate.max_xmit));
326
327         smbh->fnum = open_smb_file(smbh->cli, path, options);
328         return(&smbh->h);
329 }
330
331 /* ------------------------------------------------------------------------- */
332 /* Abstract IO interface.                                                    */
333 /* ------------------------------------------------------------------------- */
334
335 struct dd_iohandle * dd_open_path(const char * path,
336                                 uint64_t io_size,
337                                 int options)
338 {
339         if (file_exist(path)) {
340                 return(open_fd_handle(path, io_size, options));
341         } else {
342                 char * host;
343                 char * share;
344
345                 if (smbcli_parse_unc(path, NULL, &host, &share)) {
346                         const char * remain;
347                         remain = strstr(path, share) + strlen(share);
348
349                         /* Skip over leading directory separators. */
350                         while (*remain == '/' || *remain == '\\') { remain++; }
351
352                         return(open_cifs_handle(host, share, remain,
353                                                 io_size, options));
354                 }
355
356                 return(open_fd_handle(path, io_size, options));
357         }
358 }
359
360 /* Fill the buffer till it has at least need_size bytes. Use read operations of
361  * block_size bytes. Return the number of bytes read and fill buf_size with
362  * the new buffer size.
363  *
364  * NOTE: The IO buffer is guaranteed to be big enough to fit
365  * need_size + block_size bytes into it.
366  */
367 BOOL dd_fill_block(struct dd_iohandle * h,
368                 uint8_t * buf,
369                 uint64_t * buf_size,
370                 uint64_t need_size,
371                 uint64_t block_size)
372 {
373         uint64_t read_size;
374
375         SMB_ASSERT(block_size > 0);
376         SMB_ASSERT(need_size > 0);
377
378         while (*buf_size < need_size) {
379
380                 if (!h->io_read(h, buf + (*buf_size), block_size, &read_size)) {
381                         return(False);
382                 }
383
384                 if (read_size == 0) {
385                         h->io_flags |= DD_END_OF_FILE;
386                         break;
387                 }
388
389                 DEBUG(6, ("added %llu bytes to IO buffer (need %llu bytes)\n",
390                         (unsigned long long)read_size,
391                         (unsigned long long)need_size));
392
393                 *buf_size += read_size;
394                 dd_stats.in.bytes += read_size;
395
396                 if (read_size == block_size) {
397                         dd_stats.in.fblocks++;
398                 } else {
399                         DEBUG(3, ("partial read of %llu bytes (expected %llu)\n",
400                                 (unsigned long long)read_size,
401                                 (unsigned long long)block_size));
402                         dd_stats.in.pblocks++;
403                 }
404         }
405
406         return(True);
407 }
408
409 /* Flush a buffer that contains buf_size bytes. Use writes of block_size to do it,
410  * and shift any remaining bytes back to the head of the buffer when there are
411  * no more block_size sized IOs left.
412  */
413 BOOL dd_flush_block(struct dd_iohandle * h,
414                 uint8_t * buf,
415                 uint64_t * buf_size,
416                 uint64_t block_size)
417 {
418         uint64_t write_size;
419         uint64_t total_size = 0;
420
421         SMB_ASSERT(block_size > 0);
422
423         /* We have explicitly been asked to write a partial block. */
424         if ((*buf_size) < block_size) {
425
426                 if (!h->io_write(h, buf, *buf_size, &write_size)) {
427                         return(False);
428                 }
429
430                 if (write_size == 0) {
431                         fprintf(stderr, "%s: unexpectedly wrote 0 bytes\n",
432                                         PROGNAME);
433                         return(False);
434                 }
435
436                 total_size += write_size;
437                 dd_stats.out.bytes += write_size;
438                 dd_stats.out.pblocks++;
439         }
440
441         /* Write as many full blocks as there are in the buffer. */
442         while (((*buf_size) - total_size) >= block_size) {
443
444                 if (!h->io_write(h, buf + total_size, block_size, &write_size)) {
445                         return(False);
446                 }
447
448                 if (write_size == 0) {
449                         fprintf(stderr, "%s: unexpectedly wrote 0 bytes\n",
450                                         PROGNAME);
451                         return(False);
452                 }
453
454                 if (write_size == block_size) {
455                         dd_stats.out.fblocks++;
456                 } else {
457                         dd_stats.out.pblocks++;
458                 }
459
460                 total_size += write_size;
461                 dd_stats.out.bytes += write_size;
462
463                 DEBUG(6, ("flushed %llu bytes from IO buffer of %llu bytes (%llu remain)\n",
464                         (unsigned long long)block_size,
465                         (unsigned long long)block_size,
466                         (unsigned long long)(block_size - total_size)));
467         }
468
469         SMB_ASSERT(total_size > 0);
470
471         /* We have flushed as much of the IO buffer as we can while
472          * still doing block_size'd operations. Shift any remaining data
473          * to the front of the IO buffer.
474          */
475         if ((*buf_size) > total_size) {
476                 uint64_t remain = (*buf_size) - total_size;
477
478                 DEBUG(3, ("shifting %llu remainder bytes to IO buffer head\n",
479                         (unsigned long long)remain));
480
481                 memmove(buf, buf + total_size, remain);
482                 (*buf_size) = remain;
483         } else if ((*buf_size) == total_size) {
484                 (*buf_size) = 0;
485         } else {
486                 /* Else buffer contains buf_size bytes that we will append
487                  * to next time round.
488                  */
489                 DEBUG(3, ("%llu unflushed bytes left in IO buffer\n",
490                         (unsigned long long)(*buf_size)));
491         }
492
493         return(True);
494 }
495
496 /* vim: set sw=8 sts=8 ts=8 tw=79 : */