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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "system/filesys.h"
23 #include "libcli/raw/libcliraw.h"
24 #include "libcli/libcli.h"
25 #include "lib/cmdline/popt_common.h"
29 /* ------------------------------------------------------------------------- */
30 /* UNIX file descriptor IO. */
31 /* ------------------------------------------------------------------------- */
39 #define IO_HANDLE_TO_FD(h) (((struct fd_handle *)(h))->fd)
41 static bool fd_seek_func(void * handle, uint64_t offset)
45 ret = lseek(IO_HANDLE_TO_FD(handle), offset, SEEK_SET);
47 fprintf(stderr, "%s: seek failed: %s\n",
48 PROGNAME, strerror(errno));
55 static bool fd_read_func(void * handle,
62 ret = read(IO_HANDLE_TO_FD(handle), buf, wanted);
64 fprintf(stderr, "%s: %llu byte read failed: %s\n",
65 PROGNAME, (unsigned long long)wanted,
70 *actual = (uint64_t)ret;
74 static bool fd_write_func(void * handle,
81 ret = write(IO_HANDLE_TO_FD(handle), buf, wanted);
83 fprintf(stderr, "%s: %llu byte write failed: %s\n",
84 PROGNAME, (unsigned long long)wanted,
89 *actual = (uint64_t)ret;
93 static struct dd_iohandle * open_fd_handle(const char * path,
97 struct fd_handle * fdh;
100 DEBUG(4, ("opening fd stream for %s\n", path));
101 if ((fdh = talloc_zero(NULL, struct fd_handle)) == NULL) {
105 fdh->h.io_read = fd_read_func;
106 fdh->h.io_write = fd_write_func;
107 fdh->h.io_seek = fd_seek_func;
109 if (options & DD_DIRECT_IO) {
110 #ifdef HAVE_OPEN_O_DIRECT
113 DEBUG(1, ("no support for direct IO on this platform\n"));
117 if (options & DD_SYNC_IO)
120 oflags |= (options & DD_WRITE) ? (O_WRONLY | O_CREAT) : (O_RDONLY);
122 fdh->fd = open(path, oflags, 0644);
124 fprintf(stderr, "%s: %s: %s\n",
125 PROGNAME, path, strerror(errno));
130 if (options & DD_OPLOCK) {
131 DEBUG(2, ("FIXME: take local oplock on %s\n", path));
134 SMB_ASSERT((void *)fdh == (void *)&fdh->h);
138 /* ------------------------------------------------------------------------- */
139 /* CIFS client IO. */
140 /* ------------------------------------------------------------------------- */
144 struct dd_iohandle h;
145 struct smbcli_state * cli;
150 #define IO_HANDLE_TO_SMB(h) ((struct cifs_handle *)(h))
152 static bool smb_seek_func(void * handle, uint64_t offset)
154 IO_HANDLE_TO_SMB(handle)->offset = offset;
158 static bool smb_read_func(void * handle, uint8_t * buf, uint64_t wanted,
163 struct cifs_handle * smbh;
166 smbh = IO_HANDLE_TO_SMB(handle);
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;
175 /* FIXME: Should I really set readx.in.remaining? That just seems
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,
186 /* Trap integer wrap. */
187 SMB_ASSERT((smbh->offset + r.readx.out.nread) >= smbh->offset);
189 *actual = r.readx.out.nread;
190 smbh->offset += r.readx.out.nread;
194 static bool smb_write_func(void * handle, uint8_t * buf, uint64_t wanted,
199 struct cifs_handle * smbh;
202 smbh = IO_HANDLE_TO_SMB(handle);
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;
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,
218 *actual = w.writex.out.nwritten;
219 smbh->offset += w.writex.out.nwritten;
223 static struct smbcli_state * init_smb_session(const char * host,
227 struct smbcli_state * cli = NULL;
229 /* When we support SMB URLs, we can get different user credentials for
230 * each connection, but for now, we just use the same one for both.
232 ret = smbcli_full_connection(NULL, &cli, host, share,
233 NULL /* devtype */, cmdline_credentials, NULL /* events */);
235 if (!NT_STATUS_IS_OK(ret)) {
236 fprintf(stderr, "%s: connecting to //%s/%s: %s\n",
237 PROGNAME, host, share, nt_errstr(ret));
244 static int open_smb_file(struct smbcli_state * cli,
253 o.ntcreatex.level = RAW_OPEN_NTCREATEX;
254 o.ntcreatex.in.fname = path;
256 /* TODO: It's not clear whether to use these flags or to use the
257 * similarly named NTCREATEX flags in the create_options field.
259 if (options & DD_DIRECT_IO)
260 o.ntcreatex.in.flags |= FILE_FLAG_NO_BUFFERING;
262 if (options & DD_SYNC_IO)
263 o.ntcreatex.in.flags |= FILE_FLAG_WRITE_THROUGH;
265 o.ntcreatex.in.access_mask |=
266 (options & DD_WRITE) ? SEC_FILE_WRITE_DATA
267 : SEC_FILE_READ_DATA;
269 /* Try to create the file only if we will be writing to it. */
270 o.ntcreatex.in.open_disposition =
271 (options & DD_WRITE) ? NTCREATEX_DISP_OPEN_IF
272 : NTCREATEX_DISP_OPEN;
274 o.ntcreatex.in.share_access =
275 NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
277 if (options & DD_OPLOCK) {
278 o.ntcreatex.in.flags |= NTCREATEX_FLAGS_REQUEST_OPLOCK;
281 ret = smb_raw_open(cli->tree, NULL, &o);
282 if (!NT_STATUS_IS_OK(ret)) {
283 fprintf(stderr, "%s: opening %s: %s\n",
284 PROGNAME, path, nt_errstr(ret));
288 return(o.ntcreatex.out.file.fnum);
291 static struct dd_iohandle * open_cifs_handle(const char * host,
297 struct cifs_handle * smbh;
299 if (path == NULL || *path == '\0') {
300 fprintf(stderr, "%s: missing path name within share //%s/%s\n",
301 PROGNAME, host, share);
304 DEBUG(4, ("opening SMB stream to //%s/%s for %s\n",
307 if ((smbh = talloc_zero(NULL, struct cifs_handle)) == NULL) {
311 smbh->h.io_read = smb_read_func;
312 smbh->h.io_write = smb_write_func;
313 smbh->h.io_seek = smb_seek_func;
315 if ((smbh->cli = init_smb_session(host, share)) == NULL) {
319 DEBUG(4, ("connected to //%s/%s with xmit size of %u bytes\n",
320 host, share, smbh->cli->transport->negotiate.max_xmit));
322 smbh->fnum = open_smb_file(smbh->cli, path, options);
326 /* ------------------------------------------------------------------------- */
327 /* Abstract IO interface. */
328 /* ------------------------------------------------------------------------- */
330 struct dd_iohandle * dd_open_path(const char * path,
334 if (file_exist(path)) {
335 return(open_fd_handle(path, io_size, options));
340 if (smbcli_parse_unc(path, NULL, &host, &share)) {
342 remain = strstr(path, share) + strlen(share);
344 /* Skip over leading directory separators. */
345 while (*remain == '/' || *remain == '\\') { remain++; }
347 return(open_cifs_handle(host, share, remain,
351 return(open_fd_handle(path, io_size, options));
355 /* Fill the buffer till it has at least need_size bytes. Use read operations of
356 * block_size bytes. Return the number of bytes read and fill buf_size with
357 * the new buffer size.
359 * NOTE: The IO buffer is guaranteed to be big enough to fit
360 * need_size + block_size bytes into it.
362 bool dd_fill_block(struct dd_iohandle * h,
370 SMB_ASSERT(block_size > 0);
371 SMB_ASSERT(need_size > 0);
373 while (*buf_size < need_size) {
375 if (!h->io_read(h, buf + (*buf_size), block_size, &read_size)) {
379 if (read_size == 0) {
380 h->io_flags |= DD_END_OF_FILE;
384 DEBUG(6, ("added %llu bytes to IO buffer (need %llu bytes)\n",
385 (unsigned long long)read_size,
386 (unsigned long long)need_size));
388 *buf_size += read_size;
389 dd_stats.in.bytes += read_size;
391 if (read_size == block_size) {
392 dd_stats.in.fblocks++;
394 DEBUG(3, ("partial read of %llu bytes (expected %llu)\n",
395 (unsigned long long)read_size,
396 (unsigned long long)block_size));
397 dd_stats.in.pblocks++;
404 /* Flush a buffer that contains buf_size bytes. Use writes of block_size to do it,
405 * and shift any remaining bytes back to the head of the buffer when there are
406 * no more block_size sized IOs left.
408 bool dd_flush_block(struct dd_iohandle * h,
414 uint64_t total_size = 0;
416 SMB_ASSERT(block_size > 0);
418 /* We have explicitly been asked to write a partial block. */
419 if ((*buf_size) < block_size) {
421 if (!h->io_write(h, buf, *buf_size, &write_size)) {
425 if (write_size == 0) {
426 fprintf(stderr, "%s: unexpectedly wrote 0 bytes\n",
431 total_size += write_size;
432 dd_stats.out.bytes += write_size;
433 dd_stats.out.pblocks++;
436 /* Write as many full blocks as there are in the buffer. */
437 while (((*buf_size) - total_size) >= block_size) {
439 if (!h->io_write(h, buf + total_size, block_size, &write_size)) {
443 if (write_size == 0) {
444 fprintf(stderr, "%s: unexpectedly wrote 0 bytes\n",
449 if (write_size == block_size) {
450 dd_stats.out.fblocks++;
452 dd_stats.out.pblocks++;
455 total_size += write_size;
456 dd_stats.out.bytes += write_size;
458 DEBUG(6, ("flushed %llu bytes from IO buffer of %llu bytes (%llu remain)\n",
459 (unsigned long long)block_size,
460 (unsigned long long)block_size,
461 (unsigned long long)(block_size - total_size)));
464 SMB_ASSERT(total_size > 0);
466 /* We have flushed as much of the IO buffer as we can while
467 * still doing block_size'd operations. Shift any remaining data
468 * to the front of the IO buffer.
470 if ((*buf_size) > total_size) {
471 uint64_t remain = (*buf_size) - total_size;
473 DEBUG(3, ("shifting %llu remainder bytes to IO buffer head\n",
474 (unsigned long long)remain));
476 memmove(buf, buf + total_size, remain);
477 (*buf_size) = remain;
478 } else if ((*buf_size) == total_size) {
481 /* Else buffer contains buf_size bytes that we will append
482 * to next time round.
484 DEBUG(3, ("%llu unflushed bytes left in IO buffer\n",
485 (unsigned long long)(*buf_size)));
491 /* vim: set sw=8 sts=8 ts=8 tw=79 : */