r26644: Janitorial: Pass resolve_context explicitly to various SMB functions, should...
[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 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 static bool smb_seek_func(void * handle, uint64_t offset)
153 {
154         IO_HANDLE_TO_SMB(handle)->offset = offset;
155         return(true);
156 }
157
158 static bool smb_read_func(void * handle, uint8_t * buf, uint64_t wanted,
159                           uint64_t * actual)
160 {
161         NTSTATUS                ret;
162         union smb_read          r;
163         struct cifs_handle *    smbh;
164
165         ZERO_STRUCT(r);
166         smbh = IO_HANDLE_TO_SMB(handle);
167
168         r.generic.level         = RAW_READ_READX;
169         r.readx.in.file.fnum    = smbh->fnum;
170         r.readx.in.offset       = smbh->offset;
171         r.readx.in.mincnt       = wanted;
172         r.readx.in.maxcnt       = wanted;
173         r.readx.out.data        = buf;
174
175         /* FIXME: Should I really set readx.in.remaining? That just seems
176          * redundant.
177          */
178         ret = smb_raw_read(smbh->cli->tree, &r);
179         if (!NT_STATUS_IS_OK(ret)) {
180                 fprintf(stderr, "%s: %llu byte read failed: %s\n",
181                                 PROGNAME, (unsigned long long)wanted,
182                                 nt_errstr(ret));
183                 return(false);
184         }
185
186         /* Trap integer wrap. */
187         SMB_ASSERT((smbh->offset + r.readx.out.nread) >= smbh->offset);
188
189         *actual = r.readx.out.nread;
190         smbh->offset += r.readx.out.nread;
191         return(true);
192 }
193
194 static bool smb_write_func(void * handle, uint8_t * buf, uint64_t wanted,
195                            uint64_t * actual)
196 {
197         NTSTATUS                ret;
198         union smb_write         w;
199         struct cifs_handle *    smbh;
200
201         ZERO_STRUCT(w);
202         smbh = IO_HANDLE_TO_SMB(handle);
203
204         w.generic.level         = RAW_WRITE_WRITEX;
205         w.writex.in.file.fnum   = smbh->fnum;
206         w.writex.in.offset      = smbh->offset;
207         w.writex.in.count       = wanted;
208         w.writex.in.data        = buf;
209
210         ret = smb_raw_write(smbh->cli->tree, &w);
211         if (!NT_STATUS_IS_OK(ret)) {
212                 fprintf(stderr, "%s: %llu byte write failed: %s\n",
213                                 PROGNAME, (unsigned long long)wanted,
214                                 nt_errstr(ret));
215                 return(false);
216         }
217
218         *actual = w.writex.out.nwritten;
219         smbh->offset += w.writex.out.nwritten;
220         return(true);
221 }
222
223 static struct smbcli_state * init_smb_session(struct resolve_context *resolve_ctx,
224                                               const char * host,
225                                               const char **ports,
226                                               const char * share)
227 {
228         NTSTATUS                ret;
229         struct smbcli_state *   cli = NULL;
230
231         /* When we support SMB URLs, we can get different user credentials for
232          * each connection, but for now, we just use the same one for both.
233          */
234         ret = smbcli_full_connection(NULL, &cli, host, ports, share,
235                          NULL /* devtype */, cmdline_credentials, resolve_ctx, 
236                          NULL /* events */);
237
238         if (!NT_STATUS_IS_OK(ret)) {
239                 fprintf(stderr, "%s: connecting to //%s/%s: %s\n",
240                         PROGNAME, host, share, nt_errstr(ret));
241                 return(NULL);
242         }
243
244         return(cli);
245 }
246
247 static int open_smb_file(struct smbcli_state * cli,
248                         const char * path,
249                         int options)
250 {
251         NTSTATUS        ret;
252         union smb_open  o;
253
254         ZERO_STRUCT(o);
255
256         o.ntcreatex.level = RAW_OPEN_NTCREATEX;
257         o.ntcreatex.in.fname = path;
258
259         /* TODO: It's not clear whether to use these flags or to use the
260          * similarly named NTCREATEX flags in the create_options field.
261          */
262         if (options & DD_DIRECT_IO)
263                 o.ntcreatex.in.flags |= FILE_FLAG_NO_BUFFERING;
264
265         if (options & DD_SYNC_IO)
266                 o.ntcreatex.in.flags |= FILE_FLAG_WRITE_THROUGH;
267
268         o.ntcreatex.in.access_mask |=
269                 (options & DD_WRITE) ? SEC_FILE_WRITE_DATA
270                                         : SEC_FILE_READ_DATA;
271
272         /* Try to create the file only if we will be writing to it. */
273         o.ntcreatex.in.open_disposition =
274                 (options & DD_WRITE) ? NTCREATEX_DISP_OPEN_IF
275                                         : NTCREATEX_DISP_OPEN;
276
277         o.ntcreatex.in.share_access =
278                 NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
279
280         if (options & DD_OPLOCK) {
281                 o.ntcreatex.in.flags |= NTCREATEX_FLAGS_REQUEST_OPLOCK;
282         }
283
284         ret = smb_raw_open(cli->tree, NULL, &o);
285         if (!NT_STATUS_IS_OK(ret)) {
286                 fprintf(stderr, "%s: opening %s: %s\n",
287                         PROGNAME, path, nt_errstr(ret));
288                 return(-1);
289         }
290
291         return(o.ntcreatex.out.file.fnum);
292 }
293
294 static struct dd_iohandle * open_cifs_handle(struct resolve_context *resolve_ctx,
295                                              const char * host,
296                                         const char **ports,
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(resolve_ctx, host, ports, 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(struct resolve_context *resolve_ctx, 
336                                   const char * path,
337                                   const char **ports,
338                                 uint64_t io_size,
339                                 int options)
340 {
341         if (file_exist(path)) {
342                 return(open_fd_handle(path, io_size, options));
343         } else {
344                 char * host;
345                 char * share;
346
347                 if (smbcli_parse_unc(path, NULL, &host, &share)) {
348                         const char * remain;
349                         remain = strstr(path, share) + strlen(share);
350
351                         /* Skip over leading directory separators. */
352                         while (*remain == '/' || *remain == '\\') { remain++; }
353
354                         return(open_cifs_handle(resolve_ctx, host, ports, 
355                                                 share, remain,
356                                                 io_size, options));
357                 }
358
359                 return(open_fd_handle(path, io_size, options));
360         }
361 }
362
363 /* Fill the buffer till it has at least need_size bytes. Use read operations of
364  * block_size bytes. Return the number of bytes read and fill buf_size with
365  * the new buffer size.
366  *
367  * NOTE: The IO buffer is guaranteed to be big enough to fit
368  * need_size + block_size bytes into it.
369  */
370 bool dd_fill_block(struct dd_iohandle * h,
371                 uint8_t * buf,
372                 uint64_t * buf_size,
373                 uint64_t need_size,
374                 uint64_t block_size)
375 {
376         uint64_t read_size;
377
378         SMB_ASSERT(block_size > 0);
379         SMB_ASSERT(need_size > 0);
380
381         while (*buf_size < need_size) {
382
383                 if (!h->io_read(h, buf + (*buf_size), block_size, &read_size)) {
384                         return(false);
385                 }
386
387                 if (read_size == 0) {
388                         h->io_flags |= DD_END_OF_FILE;
389                         break;
390                 }
391
392                 DEBUG(6, ("added %llu bytes to IO buffer (need %llu bytes)\n",
393                         (unsigned long long)read_size,
394                         (unsigned long long)need_size));
395
396                 *buf_size += read_size;
397                 dd_stats.in.bytes += read_size;
398
399                 if (read_size == block_size) {
400                         dd_stats.in.fblocks++;
401                 } else {
402                         DEBUG(3, ("partial read of %llu bytes (expected %llu)\n",
403                                 (unsigned long long)read_size,
404                                 (unsigned long long)block_size));
405                         dd_stats.in.pblocks++;
406                 }
407         }
408
409         return(true);
410 }
411
412 /* Flush a buffer that contains buf_size bytes. Use writes of block_size to do it,
413  * and shift any remaining bytes back to the head of the buffer when there are
414  * no more block_size sized IOs left.
415  */
416 bool dd_flush_block(struct dd_iohandle * h,
417                 uint8_t * buf,
418                 uint64_t * buf_size,
419                 uint64_t block_size)
420 {
421         uint64_t write_size;
422         uint64_t total_size = 0;
423
424         SMB_ASSERT(block_size > 0);
425
426         /* We have explicitly been asked to write a partial block. */
427         if ((*buf_size) < block_size) {
428
429                 if (!h->io_write(h, buf, *buf_size, &write_size)) {
430                         return(false);
431                 }
432
433                 if (write_size == 0) {
434                         fprintf(stderr, "%s: unexpectedly wrote 0 bytes\n",
435                                         PROGNAME);
436                         return(false);
437                 }
438
439                 total_size += write_size;
440                 dd_stats.out.bytes += write_size;
441                 dd_stats.out.pblocks++;
442         }
443
444         /* Write as many full blocks as there are in the buffer. */
445         while (((*buf_size) - total_size) >= block_size) {
446
447                 if (!h->io_write(h, buf + total_size, block_size, &write_size)) {
448                         return(false);
449                 }
450
451                 if (write_size == 0) {
452                         fprintf(stderr, "%s: unexpectedly wrote 0 bytes\n",
453                                         PROGNAME);
454                         return(false);
455                 }
456
457                 if (write_size == block_size) {
458                         dd_stats.out.fblocks++;
459                 } else {
460                         dd_stats.out.pblocks++;
461                 }
462
463                 total_size += write_size;
464                 dd_stats.out.bytes += write_size;
465
466                 DEBUG(6, ("flushed %llu bytes from IO buffer of %llu bytes (%llu remain)\n",
467                         (unsigned long long)block_size,
468                         (unsigned long long)block_size,
469                         (unsigned long long)(block_size - total_size)));
470         }
471
472         SMB_ASSERT(total_size > 0);
473
474         /* We have flushed as much of the IO buffer as we can while
475          * still doing block_size'd operations. Shift any remaining data
476          * to the front of the IO buffer.
477          */
478         if ((*buf_size) > total_size) {
479                 uint64_t remain = (*buf_size) - total_size;
480
481                 DEBUG(3, ("shifting %llu remainder bytes to IO buffer head\n",
482                         (unsigned long long)remain));
483
484                 memmove(buf, buf + total_size, remain);
485                 (*buf_size) = remain;
486         } else if ((*buf_size) == total_size) {
487                 (*buf_size) = 0;
488         } else {
489                 /* Else buffer contains buf_size bytes that we will append
490                  * to next time round.
491                  */
492                 DEBUG(3, ("%llu unflushed bytes left in IO buffer\n",
493                         (unsigned long long)(*buf_size)));
494         }
495
496         return(true);
497 }
498
499 /* vim: set sw=8 sts=8 ts=8 tw=79 : */