r13280: Tidy up formatting.
[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,
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 iosz,
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 iosz,
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, uint64_t iosz, int options)
331 {
332         if (file_exist(path)) {
333                 return(open_fd_handle(path, iosz, options));
334         } else {
335                 char * host;
336                 char * share;
337
338                 if (smbcli_parse_unc(path, NULL, &host, &share)) {
339                         const char * remain;
340                         remain = strstr(path, share) + strlen(share);
341
342                         /* Skip over leading directory separators. */
343                         while (*remain == '/' || *remain == '\\') { remain++; }
344
345                         return(open_smb_handle(host, share, remain,
346                                                 iosz, options));
347                 }
348
349                 return(open_fd_handle(path, iosz, options));
350         }
351 }
352
353 /* Fill the buffer till it has at least needsz bytes. Use read operations of
354  * blocksz bytes. Return the number of bytes read and fill bufsz with the new
355  * buffer size.
356  *
357  * NOTE: The IO buffer is guaranteed to be big enough to fit needsz + blocksz
358  * bytes into it.
359  */
360 BOOL dd_fill_block(struct dd_iohandle * h,
361                 uint8_t * buf,
362                 uint64_t * bufsz,
363                 uint64_t needsz,
364                 uint64_t blocksz)
365 {
366         uint64_t readsz;
367
368         SMB_ASSERT(blocksz > 0);
369         SMB_ASSERT(needsz > 0);
370
371         while (*bufsz < needsz) {
372
373                 if (!h->io_read(h, buf + (*bufsz), blocksz, &readsz)) {
374                         return(False);
375                 }
376
377                 if (readsz == 0) {
378                         h->io_flags |= DD_END_OF_FILE;
379                         break;
380                 }
381
382                 DEBUG(6, ("added %llu bytes to IO buffer (need %llu bytes)\n",
383                         (unsigned long long)readsz, (unsigned long long)needsz));
384
385                 *bufsz += readsz;
386                 dd_stats.in.bytes += readsz;
387
388                 if (readsz == blocksz) {
389                         dd_stats.in.fblocks++;
390                 } else {
391                         DEBUG(3, ("partial read of %llu bytes (expected %llu)\n",
392                                 (unsigned long long)readsz, (unsigned long long)blocksz));
393                         dd_stats.in.pblocks++;
394                 }
395         }
396
397         return(True);
398 }
399
400 /* Flush a buffer that contains bufsz bytes. Use writes of blocksz to do it,
401  * and shift any remaining bytes back to the head of the buffer when there are
402  * no more blocksz sized IOs left.
403  */
404 BOOL dd_flush_block(struct dd_iohandle * h,
405                 uint8_t * buf,
406                 uint64_t * bufsz,
407                 uint64_t blocksz)
408 {
409         uint64_t writesz;
410         uint64_t totalsz = 0;
411
412         SMB_ASSERT(blocksz > 0);
413
414         /* We have explicitly been asked to write a partial block. */
415         if ((*bufsz) < blocksz) {
416
417                 if (!h->io_write(h, buf, *bufsz, &writesz)) {
418                         return(False);
419                 }
420
421                 if (writesz == 0) {
422                         fprintf(stderr, "%s: unexpectedly wrote 0 bytes\n",
423                                         PROGNAME);
424                         return(False);
425                 }
426
427                 totalsz += writesz;
428                 dd_stats.out.bytes += writesz;
429                 dd_stats.out.pblocks++;
430         }
431
432         /* Write as many full blocks as there are in the buffer. */
433         while (((*bufsz) - totalsz) >= blocksz) {
434
435                 if (!h->io_write(h, buf + totalsz, blocksz, &writesz)) {
436                         return(False);
437                 }
438
439                 if (writesz == 0) {
440                         fprintf(stderr, "%s: unexpectedly wrote 0 bytes\n",
441                                         PROGNAME);
442                         return(False);
443                 }
444
445                 if (writesz == blocksz) {
446                         dd_stats.out.fblocks++;
447                 } else {
448                         dd_stats.out.pblocks++;
449                 }
450
451                 totalsz += writesz;
452                 dd_stats.out.bytes += writesz;
453
454                 DEBUG(6, ("flushed %llu bytes from IO buffer of %llu bytes (%llu remain)\n",
455                         (unsigned long long)blocksz, (unsigned long long)blocksz,
456                         (unsigned long long)(blocksz - totalsz)));
457         }
458
459         SMB_ASSERT(totalsz > 0);
460
461         /* We have flushed as much of the IO buffer as we can while
462          * still doing blocksz-sized operations. Shift any remaining data
463          * to the front of the IO buffer.
464          */
465         if ((*bufsz) > totalsz) {
466                 uint64_t remain = (*bufsz) - totalsz;
467
468                 DEBUG(3, ("shifting %llu remainder bytes to IO buffer head\n",
469                         (unsigned long long)remain));
470
471                 memmove(buf, buf + totalsz, remain);
472                 (*bufsz) = remain;
473         } else if ((*bufsz) == totalsz) {
474                 (*bufsz) = 0;
475         } else {
476                 /* Else buffer contains bufsz bytes that we will append
477                  * to next time round.
478                  */
479                 DEBUG(3, ("%llu unflushed bytes left in IO buffer\n",
480                         (unsigned long long)(*bufsz)));
481         }
482
483         return(True);
484 }
485
486 /* vim: set sw=8 sts=8 ts=8 tw=79 : */