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