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.
23 #include "system/filesys.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/libcli.h"
26 #include "lib/cmdline/popt_common.h"
30 /* ------------------------------------------------------------------------- */
31 /* UNIX file descriptor IO. */
32 /* ------------------------------------------------------------------------- */
40 #define IO_HANDLE_TO_FD(h) (((struct fd_handle *)(h))->fd)
42 static BOOL fd_seek_func(void * handle, uint64_t offset)
46 ret = lseek(IO_HANDLE_TO_FD(handle), offset, SEEK_SET);
48 fprintf(stderr, "%s: seek failed: %s\n",
49 PROGNAME, strerror(errno));
56 static BOOL fd_read_func(void * handle,
63 ret = read(IO_HANDLE_TO_FD(handle), buf, wanted);
65 fprintf(stderr, "%s: %llu byte read failed: %s\n",
66 PROGNAME, (unsigned long long)wanted,
71 *actual = (uint64_t)ret;
75 static BOOL fd_write_func(void * handle,
82 ret = write(IO_HANDLE_TO_FD(handle), buf, wanted);
84 fprintf(stderr, "%s: %llu byte write failed: %s\n",
85 PROGNAME, (unsigned long long)wanted,
90 *actual = (uint64_t)ret;
94 static struct dd_iohandle * open_fd_handle(const char * path,
98 struct fd_handle * fdh;
101 DEBUG(4, ("opening fd stream for %s\n", path));
102 if ((fdh = talloc_zero(NULL, struct fd_handle)) == 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
114 DEBUG(1, ("no support for direct IO on this platform\n"));
118 if (options & DD_SYNC_IO)
121 oflags |= (options & DD_WRITE) ? (O_WRONLY | O_CREAT) : (O_RDONLY);
123 fdh->fd = open(path, oflags, 0644);
125 fprintf(stderr, "%s: %s: %s\n",
126 PROGNAME, path, strerror(errno));
131 if (options & DD_OPLOCK) {
132 DEBUG(2, ("FIXME: take local oplock on %s\n", path));
135 SMB_ASSERT((void *)fdh == (void *)&fdh->h);
139 /* ------------------------------------------------------------------------- */
140 /* CIFS client IO. */
141 /* ------------------------------------------------------------------------- */
145 struct dd_iohandle h;
146 struct smbcli_state * cli;
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;
159 BOOL smb_read_func(void * handle,
166 struct cifs_handle * smbh;
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
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,
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;
197 BOOL smb_write_func(void * handle,
204 struct cifs_handle * smbh;
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,
223 *actual = w.writex.out.nwritten;
224 smbh->offset += w.writex.out.nwritten;
228 static struct smbcli_state * init_smb_session(const char * host,
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));
249 static int open_smb_file(struct smbcli_state * cli,
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));
293 return(o.ntcreatex.out.file.fnum);
296 static struct dd_iohandle * open_cifs_handle(const char * host,
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",
312 if ((smbh = talloc_zero(NULL, struct cifs_handle)) == 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) {
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);
331 /* ------------------------------------------------------------------------- */
332 /* Abstract IO interface. */
333 /* ------------------------------------------------------------------------- */
335 struct dd_iohandle * dd_open_path(const char * path,
339 if (file_exist(path)) {
340 return(open_fd_handle(path, io_size, options));
345 if (smbcli_parse_unc(path, NULL, &host, &share)) {
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,
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,
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)) {
384 if (read_size == 0) {
385 h->io_flags |= DD_END_OF_FILE;
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++;
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++;
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,
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)) {
430 if (write_size == 0) {
431 fprintf(stderr, "%s: unexpectedly wrote 0 bytes\n",
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)) {
448 if (write_size == 0) {
449 fprintf(stderr, "%s: unexpectedly wrote 0 bytes\n",
454 if (write_size == block_size) {
455 dd_stats.out.fblocks++;
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) {
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)));
496 /* vim: set sw=8 sts=8 ts=8 tw=79 : */