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, uint8_t * buf, uint64_t wanted, uint64_t * actual)
60 ret = read(IO_HANDLE_TO_FD(handle), buf, wanted);
62 fprintf(stderr, "%s: %llu byte read failed: %s\n",
63 PROGNAME, (unsigned long long)wanted, strerror(errno));
67 *actual = (uint64_t)ret;
72 fd_write_func(void * handle, uint8_t * buf, uint64_t wanted, uint64_t * actual)
76 ret = write(IO_HANDLE_TO_FD(handle), buf, wanted);
78 fprintf(stderr, "%s: %llu byte write failed: %s\n",
79 PROGNAME, (unsigned long long)wanted, strerror(errno));
83 *actual = (uint64_t)ret;
87 static struct dd_iohandle *
88 open_fd_handle(const char * path, uint64_t iosz, int options)
90 struct fd_handle * fdh;
93 DEBUG(4, ("opening fd stream for %s\n", path));
94 if ((fdh = talloc_zero(NULL, struct fd_handle)) == NULL) {
98 fdh->h.io_read = fd_read_func;
99 fdh->h.io_write = fd_write_func;
100 fdh->h.io_seek = fd_seek_func;
102 if (options & DD_DIRECT_IO) {
103 #ifdef HAVE_OPEN_O_DIRECT
106 DEBUG(1, ("no support for direct IO on this platform\n"));
110 if (options & DD_SYNC_IO)
113 oflags |= (options & DD_WRITE) ? (O_WRONLY | O_CREAT) : (O_RDONLY);
115 fdh->fd = open(path, oflags, 0644);
117 fprintf(stderr, "%s: %s: %s\n",
118 PROGNAME, path, strerror(errno));
123 if (options & DD_OPLOCK) {
124 DEBUG(2, ("FIXME: take local oplock on %s\n", path));
127 SMB_ASSERT((void *)fdh == (void *)&fdh->h);
131 /* ------------------------------------------------------------------------- */
132 /* CIFS client IO. */
133 /* ------------------------------------------------------------------------- */
137 struct dd_iohandle h;
138 struct smbcli_state * cli;
143 #define IO_HANDLE_TO_SMB(h) ((struct smb_handle *)(h))
145 BOOL smb_seek_func(void * handle, uint64_t offset)
147 IO_HANDLE_TO_SMB(handle)->offset = offset;
151 BOOL smb_read_func(void * handle, uint8_t * buf,
152 uint64_t wanted, uint64_t * actual)
156 struct smb_handle * smbh;
159 smbh = IO_HANDLE_TO_SMB(handle);
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;
168 /* FIXME: Should I really set readx.in.remaining? That just seems
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));
178 /* Trap integer wrap. */
179 SMB_ASSERT((smbh->offset + r.readx.out.nread) >= smbh->offset);
181 *actual = r.readx.out.nread;
182 smbh->offset += r.readx.out.nread;
186 BOOL smb_write_func(void * handle, uint8_t * buf,
187 uint64_t wanted, uint64_t * actual)
191 struct smb_handle * smbh;
194 smbh = IO_HANDLE_TO_SMB(handle);
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;
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));
209 *actual = w.writex.out.nwritten;
210 smbh->offset += w.writex.out.nwritten;
214 static struct smbcli_state * init_smb_session(const char * host, const char * share)
217 struct smbcli_state * cli = NULL;
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.
222 ret = smbcli_full_connection(NULL, &cli, host, share,
223 NULL /* devtype */, cmdline_credentials, NULL /* events */);
225 if (!NT_STATUS_IS_OK(ret)) {
226 fprintf(stderr, "%s: connecting to //%s/%s: %s\n",
227 PROGNAME, host, share, nt_errstr(ret));
234 static int open_smb_file(struct smbcli_state * cli, const char * path, int options)
241 o.ntcreatex.level = RAW_OPEN_NTCREATEX;
242 o.ntcreatex.in.fname = path;
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.
247 if (options & DD_DIRECT_IO)
248 o.ntcreatex.in.flags |= FILE_FLAG_NO_BUFFERING;
250 if (options & DD_SYNC_IO)
251 o.ntcreatex.in.flags |= FILE_FLAG_WRITE_THROUGH;
253 o.ntcreatex.in.access_mask |=
254 (options & DD_WRITE) ? SEC_FILE_WRITE_DATA
255 : SEC_FILE_READ_DATA;
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;
262 o.ntcreatex.in.share_access =
263 NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
265 if (options & DD_OPLOCK) {
266 o.ntcreatex.in.flags |= NTCREATEX_FLAGS_REQUEST_OPLOCK;
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));
276 return(o.ntcreatex.out.fnum);
279 static struct dd_iohandle * open_smb_handle(const char * host, const char * share,
280 const char * path, uint64_t iosz, int options)
282 struct smb_handle * smbh;
284 DEBUG(4, ("opening SMB stream to //%s/%s for %s\n",
287 if ((smbh = talloc_zero(NULL, struct smb_handle)) == NULL) {
291 smbh->h.io_read = smb_read_func;
292 smbh->h.io_write = smb_write_func;
293 smbh->h.io_seek = smb_seek_func;
295 if ((smbh->cli = init_smb_session(host, share)) == NULL) {
299 DEBUG(4, ("connected to //%s/%s with xmit size of %u bytes\n",
300 host, share, smbh->cli->transport->negotiate.max_xmit));
302 smbh->fnum = open_smb_file(smbh->cli, path, options);
306 /* ------------------------------------------------------------------------- */
307 /* Abstract IO interface. */
308 /* ------------------------------------------------------------------------- */
310 struct dd_iohandle * dd_open_path(const char * path, uint64_t iosz, int options)
312 if (file_exist(path)) {
313 return(open_fd_handle(path, iosz, options));
318 if (smbcli_parse_unc(path, NULL, &host, &share)) {
320 remain = strstr(path, share) + strlen(share);
322 /* Skip over leading directory separators. */
323 while (*remain == '/' || *remain == '\\') { remain++; }
325 return(open_smb_handle(host, share, remain,
329 return(open_fd_handle(path, iosz, options));
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
337 * NOTE: The IO buffer is guaranteed to be big enough to fit needsz + blocksz
340 BOOL dd_fill_block(struct dd_iohandle * h, uint8_t * buf,
341 uint64_t * bufsz, uint64_t needsz, uint64_t blocksz)
345 SMB_ASSERT(blocksz > 0);
346 SMB_ASSERT(needsz > 0);
348 while (*bufsz < needsz) {
350 if (!h->io_read(h, buf + (*bufsz), blocksz, &readsz)) {
355 h->io_flags |= DD_END_OF_FILE;
359 DEBUG(6, ("added %llu bytes to IO buffer (need %llu bytes)\n",
360 (unsigned long long)readsz, (unsigned long long)needsz));
363 dd_stats.in.bytes += readsz;
365 if (readsz == blocksz) {
366 dd_stats.in.fblocks++;
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++;
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.
381 BOOL dd_flush_block(struct dd_iohandle * h, uint8_t * buf,
382 uint64_t * bufsz, uint64_t blocksz)
385 uint64_t totalsz = 0;
387 SMB_ASSERT(blocksz > 0);
389 /* We have explicitly been asked to write a partial block. */
390 if ((*bufsz) < blocksz) {
392 if (!h->io_write(h, buf, *bufsz, &writesz)) {
397 fprintf(stderr, "%s: unexpectedly wrote 0 bytes\n",
403 dd_stats.out.bytes += writesz;
404 dd_stats.out.pblocks++;
407 /* Write as many full blocks as there are in the buffer. */
408 while (((*bufsz) - totalsz) >= blocksz) {
410 if (!h->io_write(h, buf + totalsz, blocksz, &writesz)) {
415 fprintf(stderr, "%s: unexpectedly wrote 0 bytes\n",
420 if (writesz == blocksz) {
421 dd_stats.out.fblocks++;
423 dd_stats.out.pblocks++;
427 dd_stats.out.bytes += writesz;
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)));
434 SMB_ASSERT(totalsz > 0);
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.
440 if ((*bufsz) > totalsz) {
441 uint64_t remain = (*bufsz) - totalsz;
443 DEBUG(3, ("shifting %llu remainder bytes to IO buffer head\n",
444 (unsigned long long)remain));
446 memmove(buf, buf + totalsz, remain);
448 } else if ((*bufsz) == totalsz) {
451 /* Else buffer contains bufsz bytes that we will append
452 * to next time round.
454 DEBUG(3, ("%llu unflushed bytes left in IO buffer\n",
455 (unsigned long long)(*bufsz)));
461 /* vim: set sw=8 sts=8 ts=8 tw=79 : */