r22882: It seems entirly reasonable to follow metze's suggestion and check for
[Samba.git] / source / client / cifsddio.c
blob05e98f60bb4c13b89b7d2169e16651eeeddc8252
1 /*
2 CIFSDD - dd for SMB.
3 IO routines, generic and specific.
5 Copyright (C) James Peach 2005-2006
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.
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.
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.
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"
28 #include "cifsdd.h"
30 /* ------------------------------------------------------------------------- */
31 /* UNIX file descriptor IO. */
32 /* ------------------------------------------------------------------------- */
34 struct fd_handle
36 struct dd_iohandle h;
37 int fd;
40 #define IO_HANDLE_TO_FD(h) (((struct fd_handle *)(h))->fd)
42 static BOOL fd_seek_func(void * handle, uint64_t offset)
44 ssize_t ret;
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);
53 return(True);
56 static BOOL fd_read_func(void * handle,
57 uint8_t * buf,
58 uint64_t wanted,
59 uint64_t * actual)
61 ssize_t ret;
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);
71 *actual = (uint64_t)ret;
72 return(True);
75 static BOOL fd_write_func(void * handle,
76 uint8_t * buf,
77 uint64_t wanted,
78 uint64_t * actual)
80 ssize_t ret;
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);
90 *actual = (uint64_t)ret;
91 return(True);
94 static struct dd_iohandle * open_fd_handle(const char * path,
95 uint64_t io_size,
96 int options)
98 struct fd_handle * fdh;
99 int oflags = 0;
101 DEBUG(4, ("opening fd stream for %s\n", path));
102 if ((fdh = talloc_zero(NULL, struct fd_handle)) == NULL) {
103 return(NULL);
106 fdh->h.io_read = fd_read_func;
107 fdh->h.io_write = fd_write_func;
108 fdh->h.io_seek = fd_seek_func;
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
118 if (options & DD_SYNC_IO)
119 oflags |= O_SYNC;
121 oflags |= (options & DD_WRITE) ? (O_WRONLY | O_CREAT) : (O_RDONLY);
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);
131 if (options & DD_OPLOCK) {
132 DEBUG(2, ("FIXME: take local oplock on %s\n", path));
135 SMB_ASSERT((void *)fdh == (void *)&fdh->h);
136 return(&fdh->h);
139 /* ------------------------------------------------------------------------- */
140 /* CIFS client IO. */
141 /* ------------------------------------------------------------------------- */
143 struct cifs_handle
145 struct dd_iohandle h;
146 struct smbcli_state * cli;
147 int fnum;
148 uint64_t offset;
151 #define IO_HANDLE_TO_SMB(h) ((struct cifs_handle *)(h))
153 BOOL smb_seek_func(void * handle, uint64_t offset)
155 IO_HANDLE_TO_SMB(handle)->offset = offset;
156 return(True);
159 BOOL smb_read_func(void * handle,
160 uint8_t * buf,
161 uint64_t wanted,
162 uint64_t * actual)
164 NTSTATUS ret;
165 union smb_read r;
166 struct cifs_handle * smbh;
168 ZERO_STRUCT(r);
169 smbh = IO_HANDLE_TO_SMB(handle);
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;
178 /* FIXME: Should I really set readx.in.remaining? That just seems
179 * redundant.
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);
189 /* Trap integer wrap. */
190 SMB_ASSERT((smbh->offset + r.readx.out.nread) >= smbh->offset);
192 *actual = r.readx.out.nread;
193 smbh->offset += r.readx.out.nread;
194 return(True);
197 BOOL smb_write_func(void * handle,
198 uint8_t * buf,
199 uint64_t wanted,
200 uint64_t * actual)
202 NTSTATUS ret;
203 union smb_write w;
204 struct cifs_handle * smbh;
206 ZERO_STRUCT(w);
207 smbh = IO_HANDLE_TO_SMB(handle);
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;
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);
223 *actual = w.writex.out.nwritten;
224 smbh->offset += w.writex.out.nwritten;
225 return(True);
228 static struct smbcli_state * init_smb_session(const char * host,
229 const char * share)
231 NTSTATUS ret;
232 struct smbcli_state * cli = NULL;
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.
237 ret = smbcli_full_connection(NULL, &cli, host, share,
238 NULL /* devtype */, cmdline_credentials, NULL /* events */);
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);
246 return(cli);
249 static int open_smb_file(struct smbcli_state * cli,
250 const char * path,
251 int options)
253 NTSTATUS ret;
254 union smb_open o;
256 ZERO_STRUCT(o);
258 o.ntcreatex.level = RAW_OPEN_NTCREATEX;
259 o.ntcreatex.in.fname = path;
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.
264 if (options & DD_DIRECT_IO)
265 o.ntcreatex.in.flags |= FILE_FLAG_NO_BUFFERING;
267 if (options & DD_SYNC_IO)
268 o.ntcreatex.in.flags |= FILE_FLAG_WRITE_THROUGH;
270 o.ntcreatex.in.access_mask |=
271 (options & DD_WRITE) ? SEC_FILE_WRITE_DATA
272 : SEC_FILE_READ_DATA;
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;
279 o.ntcreatex.in.share_access =
280 NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
282 if (options & DD_OPLOCK) {
283 o.ntcreatex.in.flags |= NTCREATEX_FLAGS_REQUEST_OPLOCK;
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);
293 return(o.ntcreatex.out.file.fnum);
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)
302 struct cifs_handle * smbh;
304 if (path == NULL || *path == '\0') {
305 fprintf(stderr, "%s: missing path name within share //%s/%s\n",
306 PROGNAME, host, share);
309 DEBUG(4, ("opening SMB stream to //%s/%s for %s\n",
310 host, share, path));
312 if ((smbh = talloc_zero(NULL, struct cifs_handle)) == NULL) {
313 return(NULL);
316 smbh->h.io_read = smb_read_func;
317 smbh->h.io_write = smb_write_func;
318 smbh->h.io_seek = smb_seek_func;
320 if ((smbh->cli = init_smb_session(host, share)) == NULL) {
321 return(NULL);
324 DEBUG(4, ("connected to //%s/%s with xmit size of %u bytes\n",
325 host, share, smbh->cli->transport->negotiate.max_xmit));
327 smbh->fnum = open_smb_file(smbh->cli, path, options);
328 return(&smbh->h);
331 /* ------------------------------------------------------------------------- */
332 /* Abstract IO interface. */
333 /* ------------------------------------------------------------------------- */
335 struct dd_iohandle * dd_open_path(const char * path,
336 uint64_t io_size,
337 int options)
339 if (file_exist(path)) {
340 return(open_fd_handle(path, io_size, options));
341 } else {
342 char * host;
343 char * share;
345 if (smbcli_parse_unc(path, NULL, &host, &share)) {
346 const char * remain;
347 remain = strstr(path, share) + strlen(share);
349 /* Skip over leading directory separators. */
350 while (*remain == '/' || *remain == '\\') { remain++; }
352 return(open_cifs_handle(host, share, remain,
353 io_size, options));
356 return(open_fd_handle(path, io_size, options));
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.
364 * NOTE: The IO buffer is guaranteed to be big enough to fit
365 * need_size + block_size bytes into it.
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)
373 uint64_t read_size;
375 SMB_ASSERT(block_size > 0);
376 SMB_ASSERT(need_size > 0);
378 while (*buf_size < need_size) {
380 if (!h->io_read(h, buf + (*buf_size), block_size, &read_size)) {
381 return(False);
384 if (read_size == 0) {
385 h->io_flags |= DD_END_OF_FILE;
386 break;
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));
393 *buf_size += read_size;
394 dd_stats.in.bytes += read_size;
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++;
406 return(True);
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.
413 BOOL dd_flush_block(struct dd_iohandle * h,
414 uint8_t * buf,
415 uint64_t * buf_size,
416 uint64_t block_size)
418 uint64_t write_size;
419 uint64_t total_size = 0;
421 SMB_ASSERT(block_size > 0);
423 /* We have explicitly been asked to write a partial block. */
424 if ((*buf_size) < block_size) {
426 if (!h->io_write(h, buf, *buf_size, &write_size)) {
427 return(False);
430 if (write_size == 0) {
431 fprintf(stderr, "%s: unexpectedly wrote 0 bytes\n",
432 PROGNAME);
433 return(False);
436 total_size += write_size;
437 dd_stats.out.bytes += write_size;
438 dd_stats.out.pblocks++;
441 /* Write as many full blocks as there are in the buffer. */
442 while (((*buf_size) - total_size) >= block_size) {
444 if (!h->io_write(h, buf + total_size, block_size, &write_size)) {
445 return(False);
448 if (write_size == 0) {
449 fprintf(stderr, "%s: unexpectedly wrote 0 bytes\n",
450 PROGNAME);
451 return(False);
454 if (write_size == block_size) {
455 dd_stats.out.fblocks++;
456 } else {
457 dd_stats.out.pblocks++;
460 total_size += write_size;
461 dd_stats.out.bytes += write_size;
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)));
469 SMB_ASSERT(total_size > 0);
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.
475 if ((*buf_size) > total_size) {
476 uint64_t remain = (*buf_size) - total_size;
478 DEBUG(3, ("shifting %llu remainder bytes to IO buffer head\n",
479 (unsigned long long)remain));
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.
489 DEBUG(3, ("%llu unflushed bytes left in IO buffer\n",
490 (unsigned long long)(*buf_size)));
493 return(True);
496 /* vim: set sw=8 sts=8 ts=8 tw=79 : */